From e54d5fc57ce23cbbe988fe242aee25c50049978b Mon Sep 17 00:00:00 2001 From: Azat Khuzhin Date: Sun, 12 Jan 2020 15:14:24 +0300 Subject: [PATCH] http-connect: cleanup and helpers --- sample/http-connect.c | 81 ++++++++++++++++++++++++------------------- 1 file changed, 46 insertions(+), 35 deletions(-) diff --git a/sample/http-connect.c b/sample/http-connect.c index af2c86a8..16376ddc 100644 --- a/sample/http-connect.c +++ b/sample/http-connect.c @@ -23,6 +23,38 @@ struct connect_base struct evhttp_uri *location; }; +static struct evhttp_uri* uri_parse(const char *str) +{ + struct evhttp_uri *uri; + VERIFY(uri = evhttp_uri_parse(str)); + VERIFY(evhttp_uri_get_host(uri)); + VERIFY(evhttp_uri_get_port(uri) > 0); + return uri; +} +static char* uri_path(struct evhttp_uri *uri, char buffer[URL_MAX]) +{ + struct evhttp_uri *path; + + VERIFY(evhttp_uri_join(uri, buffer, URL_MAX)); + + path = evhttp_uri_parse(buffer); + evhttp_uri_set_scheme(path, NULL); + evhttp_uri_set_userinfo(path, 0); + evhttp_uri_set_host(path, NULL); + evhttp_uri_set_port(path, -1); + VERIFY(evhttp_uri_join(path, buffer, URL_MAX)); + return buffer; +} +static char* uri_hostport(struct evhttp_uri *uri, char buffer[URL_MAX]) +{ + VERIFY(evhttp_uri_join(uri, buffer, URL_MAX)); + VERIFY(evhttp_uri_get_host(uri)); + VERIFY(evhttp_uri_get_port(uri) > 0); + evutil_snprintf(buffer, URL_MAX, "%s:%d", + evhttp_uri_get_host(uri), evhttp_uri_get_port(uri)); + return buffer; +} + static void get_cb(struct evhttp_request *req, void *arg) { ev_ssize_t len; @@ -41,26 +73,25 @@ static void get_cb(struct evhttp_request *req, void *arg) static void connect_cb(struct evhttp_request *proxy_req, void *arg) { - char buffer[URL_MAX]; - struct connect_base *base = arg; struct evhttp_connection *evcon = base->evcon; struct evhttp_uri *location = base->location; + struct evhttp_request *req; + char buffer[URL_MAX]; VERIFY(proxy_req); - if (evcon) { - struct evhttp_request *req = evhttp_request_new(get_cb, NULL); - evhttp_add_header(req->output_headers, "Connection", "close"); - VERIFY(!evhttp_make_request(evcon, req, EVHTTP_REQ_GET, - evhttp_uri_join(location, buffer, URL_MAX))); - } + VERIFY(evcon); + + req = evhttp_request_new(get_cb, NULL); + evhttp_add_header(req->output_headers, "Connection", "close"); + VERIFY(!evhttp_make_request(evcon, req, EVHTTP_REQ_GET, + uri_path(location, buffer))); } int main(int argc, const char **argv) { char buffer[URL_MAX]; - struct evhttp_uri *host_port; struct evhttp_uri *location; struct evhttp_uri *proxy; @@ -75,28 +106,8 @@ int main(int argc, const char **argv) return 1; } - { - VERIFY(proxy = evhttp_uri_parse(argv[1])); - VERIFY(evhttp_uri_get_host(proxy)); - VERIFY(evhttp_uri_get_port(proxy) > 0); - } - { - host_port = evhttp_uri_parse(argv[2]); - evhttp_uri_set_scheme(host_port, NULL); - evhttp_uri_set_userinfo(host_port, NULL); - evhttp_uri_set_path(host_port, NULL); - evhttp_uri_set_query(host_port, NULL); - evhttp_uri_set_fragment(host_port, NULL); - VERIFY(evhttp_uri_get_host(host_port)); - VERIFY(evhttp_uri_get_port(host_port) > 0); - } - { - location = evhttp_uri_parse(argv[2]); - evhttp_uri_set_scheme(location, NULL); - evhttp_uri_set_userinfo(location, 0); - evhttp_uri_set_host(location, NULL); - evhttp_uri_set_port(location, -1); - } + proxy = uri_parse(argv[1]); + location = uri_parse(argv[2]); VERIFY(base = event_base_new()); VERIFY(evcon = evhttp_connection_base_new(base, NULL, @@ -107,15 +118,15 @@ int main(int argc, const char **argv) evhttp_add_header(req->output_headers, "Connection", "keep-alive"); evhttp_add_header(req->output_headers, "Proxy-Connection", "keep-alive"); - evutil_snprintf(buffer, URL_MAX, "%s:%d", - evhttp_uri_get_host(host_port), evhttp_uri_get_port(host_port)); - evhttp_make_request(evcon, req, EVHTTP_REQ_CONNECT, buffer); + evhttp_make_request(evcon, req, EVHTTP_REQ_CONNECT, + uri_hostport(location, buffer)); event_base_dispatch(base); + evhttp_connection_free(evcon); event_base_free(base); evhttp_uri_free(proxy); - evhttp_uri_free(host_port); evhttp_uri_free(location); + return 0; } -- 2.40.0