test_uri_normalize_2(CuTest *tc) { char *expect = strdup("http://www.example.com/test/func.cgi?x=y&z=j"); uriobj_t uri; uri_parse(&uri, re, expect); int err = uri_normalize(&uri); CuAssertStrEquals(tc,*uri.uri_host,"www.example.com"); }
test_uri_normalize_1(CuTest *tc) { char *expect = strdup("http://192.168.1.100:8080/test/func.cgi?x=y&z=j"); uriobj_t uri; uri_parse(&uri, re, expect); int err = uri_normalize(&uri); CuAssertStrEquals(tc,*uri.uri_ip,"192.168.1.100"); }
static void test_uri_normalize_one_slash(void) { static const char uri1[] = "eXAMPLE://a"; static const char uri2[] = "eXAMPLE://a/"; uri_t *u = uri_new(); g_assert(uri_parse(u, uri1, strlen(uri1), NULL)); uri_normalize(u); g_assert_cmpstr("/", ==, u->path); uri_clear(u); g_assert(uri_parse(u, uri2, strlen(uri2), NULL)); uri_normalize(u); g_assert_cmpstr("/", ==, u->path); uri_free(u); }
static void test_uri_normalize_all_slashes(void) { static const char uri1[] = "eXAMPLE://a//////"; uri_t *u = uri_new(); g_assert(uri_parse(u, uri1, strlen(uri1), NULL)); uri_normalize(u); g_assert_cmpstr("/", ==, u->path); uri_free(u); }
static void test_uri_parse_badencode(void) { static const char uri1[] = "http://b.scorecardresearch.com/b?c1=2&c2=6035223rn=1404429288&c7=http%3A%2F%2Fdetnews.com%2Farticle%2F20110121%2FMETRO01%2F101210376%2FDetroit-women-get-no-help-in-arrest-of-alleged-car-thief&c8=Detroit%20women%20get%20no%20help%20in%20arrest%20of%20alleged%2&cv=2.2&cs=js"; const gchar *error_at = NULL; uri_t *u = uri_new(); int st = uri_parse(u, uri1, strlen(uri1), &error_at); if (error_at) g_test_message("uri_parse failed at -> %s", error_at); uri_normalize(u); uri_free(u); g_assert(st); }
static void test_uri_parse_double_percent(void) { static const char uri1[] = "http://bh.contextweb.com/bh/getuid?url=http://image2.pubmatic.com/AdServer/Pug?vcode=bz0yJnR5cGU9MSZqcz0xJmNvZGU9ODI1JnRsPTQzMjAw&piggybackCookie=%%CWGUID%%,User_tokens:%%USER_TOKENS%%"; const gchar *error_at = NULL; uri_t *u = uri_new(); int st = uri_parse(u, uri1, strlen(uri1), &error_at); if (error_at) g_test_message("uri_parse failed at -> %s", error_at); uri_normalize(u); uri_free(u); g_assert(st); }
static void test_uri_parse_unicode_escape(void) { static const char uri1[] = "http://b.scorecardresearch.com/b?C1=8&C2=6035047&C3=463.9924&C4=ad21868c&C5=173229&C6=16jfaue1ukmeoq&C7=http%3A//remotecontrol.mtv.com/2011/01/20/sammi-sweetheart-giancoloa-terrell-owens-hair/&C8=Hot%20Shots%3A%20Sammi%20%u2018Sweetheart%u2019%20Lets%20Terrell%20Owens%20Play%20With%20Her%20Hair%20%BB%20MTV%20Remote%20Control%20Blog&C9=&C10=1680x1050rn=58013009"; const gchar *error_at = NULL; uri_t *u = uri_new(); int st = uri_parse(u, uri1, strlen(uri1), &error_at); if (error_at) g_test_message("uri_parse failed at -> %s", error_at); uri_normalize(u); uri_free(u); g_assert(st); }
static void test_uri_normalize_host(void) { static const char uri1[] = "eXAMPLE://ExAmPlE.CoM/"; uri_t *u = uri_new(); g_assert(uri_parse(u, uri1, strlen(uri1), NULL)); uri_normalize(u); g_assert_cmpstr("example.com", ==, u->host); uri_free(u); }
static void test_uri_compose(void) { static const char uri1[] = "eXAMPLE://ExAmPlE.CoM/foo/../boo/%25hi%0b/.t%65st/./this?query=string#frag"; uri_t *u = uri_new(); g_assert(uri_parse(u, uri1, strlen(uri1), NULL)); uri_normalize(u); char *s = uri_compose(u); g_assert_cmpstr("example://example.com/boo/%25hi%0B/.test/this?query=string#frag", ==, s); free(s); uri_free(u); }
static void test_uri_normalize(void) { static const char uri1[] = "eXAMPLE://a/./b/../b/%63/%7bfoo%7d"; uri_t *u = uri_new(); g_assert(uri_parse(u, uri1, strlen(uri1), NULL)); uri_normalize(u); g_assert_cmpstr("/b/c/%7Bfoo%7D", ==, u->path); uri_clear(u); static const char uri2[] = "http://host/../"; g_assert(uri_parse(u, uri2, strlen(uri2), NULL)); uri_normalize(u); g_assert_cmpstr(u->path, ==, "/"); uri_clear(u); static const char uri3[] = "http://host/./"; g_assert(uri_parse(u, uri3, strlen(uri3), NULL)); uri_normalize(u); g_assert_cmpstr("/", ==, u->path); uri_free(u); }
/* TODO this is the messiest function, would be nice to clean it up * with some better url parsing. */ int plxr_serve_file_or_dir(struct plxr_connection *conn) { DIR *dir; char *path; char buf[256] = {0}; char *uri; int ret; int filetype; /* Buffer for processing the uri. */ strncpy(buf, conn->request.uri, sizeof(buf)); /* Clean up preceding and trailing slashes (not part of the spec, just a design choice). */ uri = uri_normalize(buf); /* Map the uri to a file path, if one is found. */ path = plxr_router(&filetype, uri); switch (filetype) { case PLX_FILE_REG: return plxr_serve_file(conn, 200, path); case PLX_FILE_DIR: /* open up the directory and render it into an html page */ if ((dir = opendir(path)) == NULL) { ERROR("opendir"); return -1; } ret = plxr_serve_dir(conn, path, dir); closedir(dir); return ret; case PLX_FILE_NOT_FOUND: return plxr_serve_string(conn, 404, "text/html", plxr_html_404); case PLX_FILE_ERR: /* fallthrough */ default: break; } return plxr_serve_string(conn, 500, "text/html", plxr_html_500); }
void *handle_connection(void *arg) { st_netfd_t client_nfd = (st_netfd_t)arg; struct http_stream *s = http_stream_create(HTTP_SERVER, SEC2USEC(5)); char buf[4*1024]; int error = 0; struct http_stream *cs = NULL; uri_t *u = uri_new(); int should_close = 1; for (;;) { should_close = 1; if (s->status != HTTP_STREAM_OK) break; cs = NULL; error = 0; s->timeout = SEC2USEC(5); int status = http_stream_request_read(s, client_nfd); s->timeout = SEC2USEC(30); // longer timeout for the rest if (status != HTTP_STREAM_OK) { if (s->status == HTTP_STREAM_CLOSED || s->status == HTTP_STREAM_TIMEOUT) { error = 1; } else { error = 400; } goto release; } cs = http_stream_create(HTTP_CLIENT, SEC2USEC(30)); //http_request_debug_print(s->req); fprintf(stderr, "request uri: %s\n", s->req->uri); const char *error_at = NULL; uri_clear(u); if (uri_parse(u, s->req->uri, strlen(s->req->uri), &error_at) == 0) { fprintf(stderr, "uri_parse error: %s\n", error_at); error = 400; goto release; } uri_normalize(u); if (http_stream_connect(cs, u->host, u->port) != HTTP_STREAM_OK) { error = 504; goto release; } http_request_header_remove(s->req, "Accept-Encoding"); http_request_header_remove(s->req, "Proxy-Connection"); /* TODO: need to expose a copy api for http message */ http_request_t *tmp_req = cs->req; cs->req = s->req; char *request_uri = uri_compose_partial(u); char *tmp_uri = s->req->uri; cs->req->uri = request_uri; if (http_stream_request_send(cs) != HTTP_STREAM_OK) { error = 504; goto release; } cs->req = tmp_req; s->req->uri = tmp_uri; free(request_uri); /* TODO: fix this. post might not contain data. probably move this logic into stream */ size_t total = 0; if (g_strcmp0("POST", s->req->method) == 0) { for (;;) { ssize_t nr = sizeof(buf); status = http_stream_read(s, buf, &nr); fprintf(stderr, "server http_stream_read nr: %zd\n", nr); if (nr < 0 || status != HTTP_STREAM_OK) { error = 1; goto release; } if (nr == 0) break; /*fwrite(buf, sizeof(char), nr, stdout);*/ ssize_t nw = st_write(cs->nfd, buf, nr, s->timeout); if (nw != nr) { error=1; goto release; } fprintf(stderr, "st_write nw: %zd\n", nr); total += nr; } fprintf(stderr, "http_stream_read total: %zu\n", total); } if (http_stream_response_read(cs) != HTTP_STREAM_OK) { error = 502; goto release; } /* TODO: properly create a new response and copy headers */ http_response_t *tmp_resp = s->resp; s->resp = cs->resp; s->resp->http_version = "HTTP/1.1"; http_response_header_remove(s->resp, "Content-Length"); http_response_header_remove(s->resp, "Transfer-Encoding"); if (s->resp->status_code != 204) http_response_header_append(s->resp, "Transfer-Encoding", "chunked"); ssize_t nw = http_stream_response_send(s, 0); s->resp = tmp_resp; fprintf(stderr, "http_stream_response_send: %zd\n", nw); if (s->resp->status_code != 204 && (cs->content_size > 0 || cs->transfer_encoding == TE_CHUNKED)) { total = 0; fprintf(stderr, "content size: %zd\n", cs->content_size); for (;;) { ssize_t nr = sizeof(buf); status = http_stream_read(cs, buf, &nr); fprintf(stderr, "client http_stream_read nr: %zd\n", nr); if (nr <= 0 || status != HTTP_STREAM_OK) break; /*fwrite(buf, sizeof(char), nr, stdout);*/ total += nr; if (http_stream_send_chunk(s, buf, nr) != HTTP_STREAM_OK) break; } fprintf(stderr, "written to client: %zu\n", total); if (total > 0 && s->status == HTTP_STREAM_OK) { http_stream_send_chunk_end(s); } else { fprintf(stderr, "for request: %s status: %d\n", s->req->uri, s->status); } } release: if (!error) { if ((g_strcmp0("HTTP/1.1", s->req->http_version) == 0) && (g_strcmp0(http_request_header_getstr(s->req, "Connection"), "close") != 0)) { // if HTTP/1.1 client and no Connection: close, then don't close should_close = 0; } else if (g_strcmp0(http_request_header_getstr(s->req, "Connection"), "keepalive") == 0) { should_close = 0; } } http_request_clear(s->req); uri_clear(u); if (cs) http_stream_close(cs); /* TODO: break loop if HTTP/1.0 and not keep-alive */ if (error) { fprintf(stderr, "ERROR: %d STATUS: %d, exiting\n", error, s->status); /* TODO: use reason string */ if (error >= 400 && s->status != HTTP_STREAM_CLOSED) { http_response_free(s->resp); s->resp = http_response_new(error, "Error"); http_response_header_append(s->resp, "Content-Length", "0"); s->status = HTTP_STREAM_OK; /* TODO: might want to move this logic into http_stream */ http_stream_response_send(s, 0); } break; } if (should_close) break; } fprintf(stderr, "exiting handle_connection (should_close: %u)\n", should_close); uri_free(u); http_stream_close(s); return NULL; }