📄 test_http.c
字号:
http_range_t *rng; rng = http_range_make(home, "bytes = 0 - 499"); TEST_1(rng); TEST_S(rng->rng_unit, "bytes"); TEST_1(rng->rng_specs); TEST_S(rng->rng_specs[0], "0-499"); TEST(rng->rng_specs[1], NULL); rng = http_range_make(home, "bytes=,500 - 999"); TEST_1(rng); TEST_S(rng->rng_unit, "bytes"); TEST_1(rng->rng_specs); TEST_S(rng->rng_specs[0], "500-999"); TEST(rng->rng_specs[1], NULL); rng = http_range_make(home, "bytes= - 500"); TEST_1(rng); TEST_S(rng->rng_unit, "bytes"); TEST_1(rng->rng_specs); TEST_S(rng->rng_specs[0], "-500"); TEST(rng->rng_specs[1], NULL); rng = http_range_make(home, "bytes=9500-"); TEST_1(rng); TEST_S(rng->rng_unit, "bytes"); TEST_1(rng->rng_specs); TEST_S(rng->rng_specs[0], "9500-"); TEST(rng->rng_specs[1], NULL); rng = http_range_make(home, "bytes=0- 0 , - 1"); TEST_1(rng); TEST_S(rng->rng_unit, "bytes"); TEST_1(rng->rng_specs); TEST_S(rng->rng_specs[0], "0-0"); TEST_S(rng->rng_specs[1], "-1"); TEST(rng->rng_specs[2], NULL); rng = http_range_make(home, "bytes=500-600 , 601 - 999 ,,"); TEST_1(rng); TEST_S(rng->rng_unit, "bytes"); TEST_1(rng->rng_specs); TEST_S(rng->rng_specs[0], "500-600"); TEST_S(rng->rng_specs[1], "601-999"); TEST(rng->rng_specs[2], NULL); rng = http_range_make(home, "bytes=500-700,601-999"); TEST_1(rng); TEST_S(rng->rng_unit, "bytes"); TEST_1(rng->rng_specs); TEST_S(rng->rng_specs[0], "500-700"); TEST_S(rng->rng_specs[1], "601-999"); TEST(rng->rng_specs[2], NULL); } { http_date_t *d; char const *s; char b[64]; d = http_date_make(home, s = "Wed, 15 Nov 1995 06:25:24 GMT"); TEST_1(d); TEST(d->d_time, 2208988800UL + 816416724); TEST_1(http_date_e(b, sizeof b, (msg_header_t*)d, 0) > 0); TEST_S(b, s); d = http_date_make(home, s = "Wed, 15 Nov 1995 04:58:08 GMT"); TEST_1(d); TEST(d->d_time, 2208988800UL + 816411488); TEST_1(http_date_e(b, sizeof b, (msg_header_t*)d, 0) > 0); TEST_S(b, s); d = http_date_make(home, s = "Tue, 15 Nov 1994 08:12:31 GMT"); TEST_1(d); TEST(d->d_time, 2208988800UL + 784887151); TEST_1(http_date_e(b, sizeof b, (msg_header_t*)d, 0) > 0); TEST_S(b, s); d = http_date_make(home, "Fri Jan 30 12:21:09 2004"); TEST_1(d); TEST(d->d_time, 2208988800UL + 1075465269); d = http_date_make(home, "Fri Jan 1 12:21:09 2004"); TEST_1(d); TEST(d->d_time, 2208988800UL + 1072959669); d = http_date_make(home, "Tuesday, 15-Nov-94 08:12:31 GMT"); TEST_1(d); TEST(d->d_time, 2208988800UL + 784887151); } { http_retry_after_t *ra; char const *s; char b[64]; ra = http_retry_after_make(home, s = "Wed, 15 Nov 1995 06:25:24 GMT"); TEST_1(ra); TEST(ra->ra_date + ra->ra_delta, 2208988800UL + 816416724); TEST_1(http_retry_after_e(b, sizeof b, (msg_header_t*)ra, 0) > 0); TEST_S(b, s); ra = http_retry_after_make(home, s = "Wed, 15 Nov 1995 04:58:08 GMT"); TEST_1(ra); TEST(ra->ra_date + ra->ra_delta, 2208988800UL + 816411488); TEST_1(http_retry_after_e(b, sizeof b, (msg_header_t*)ra, 0) > 0); TEST_S(b, s); ra = http_retry_after_make(home, s = "Tue, 15 Nov 1994 08:12:31 GMT"); TEST_1(ra); TEST(ra->ra_date + ra->ra_delta, 2208988800UL + 784887151); TEST_1(http_retry_after_e(b, sizeof b, (msg_header_t*)ra, 0) > 0); TEST_S(b, s); ra = http_retry_after_make(home, "Fri Jan 30 12:21:09 2004"); TEST_1(ra); TEST(ra->ra_date + ra->ra_delta, 2208988800UL + 1075465269); ra = http_retry_after_make(home, "Fri Jan 1 12:21:09 2004"); TEST_1(ra); TEST(ra->ra_date + ra->ra_delta, 2208988800UL + 1072959669); ra = http_retry_after_make(home, "Tuesday, 15-Nov-94 08:12:31 GMT"); TEST_1(ra); TEST(ra->ra_date + ra->ra_delta, 2208988800UL + 784887151); ra = http_retry_after_make(home, "121"); TEST_1(ra); TEST(ra->ra_date, 0); TEST(ra->ra_delta, 121); } { http_location_t *l; TEST_1(l = http_location_make(home, "http://www.google.fi/cxfer?c=PREF%3D:TM%3D1105378671:S%3DfoewuOwfszMIFJbP&prev=/")); } su_home_deinit(home); END();}static int http_parser_test(void){ msg_t *msg; http_t *http; BEGIN(); { char data[] = "HTTP/1.1 200\r\n" "Server: Apache/1.3.11 (Unix) tomcat/1.0\r\n" "Transfer-Encoding: chunked, gzip\r\n" "Transfer-Encoding: deflate\r\n" "TE: chunked, gzip\r\n" "TE: deflate\r\n" "Content-Encoding: identity, gzip\r\n" "Content-Type: text/html\r\n" "Content-Encoding: deflate\r\n" "Set-Cookie: PREF=ID=1eab07c269cd7e5a:LD=fi:TM=1094601448:LM=1094601448:S=Ik6IEs3W3vamd8Xu; " "expires=Sun, 17-Jan-2038 19:14:07 GMT ; path=/; domain=.google.fi\r\n" "Set-Cookie: CUSTOMER=WILE_E_COYOTE; " "path=/; " "expires=Wednesday, 09-Nov-99 23:12:40 GMT\r\n" "\r\n" "4c\r\n" "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n" "\r\n" "<html>\r\n" "\r\n" "\r\n9\r\n" "</html>\r\n" "\r\n0\r\n" "Date: Mon, 21 Oct 2002 13:10:41 GMT\r\n" "\n"; TEST_1(msg = read_message_byte_by_byte(data)); http = msg_object(msg); TEST_1(http); TEST_1(http->http_status); TEST_1(http->http_payload); TEST_1(http->http_payload->pl_next); TEST_1(http->http_date); TEST_1(http->http_te); TEST_1(http->http_transfer_encoding); TEST_1(http->http_transfer_encoding->k_items); TEST_1(http->http_content_encoding); TEST_1(http->http_content_encoding->k_items); TEST_1(http->http_content_encoding->k_items); TEST_S(http->http_content_encoding->k_items[0], "identity"); TEST_S(http->http_content_encoding->k_items[1], "gzip"); TEST_S(http->http_content_encoding->k_items[2], "deflate"); TEST(http->http_content_encoding->k_items[3], NULL); TEST_1(http->http_set_cookie); TEST_1(http->http_set_cookie->sc_next); msg_destroy(msg); } END();}static int test_http_encoding(void){ http_header_t *h, *h1; msg_t *msg; http_t *http; su_home_t *home; char b[160]; int n; BEGIN(); TEST_1(home = su_home_new(sizeof *home)); msg = read_message( "GET http://bar.com/foo/ HTTP/1.1\r\n" "Accept: text/html\r\n" "Accept-Charset: iso-8859-1\r\n" "Accept-Encoding: gzip\r\n" "Accept-Language: fi\r\n" "Authorization: Basic dXNlcjE6c2VjcmV0\r\n" "Expect: 100-continue\r\n" "From: user@nokia.com\r\n" "Host: www.nokia.com:80\r\n" "If-Match: \"entity_tag001\"\r\n" "If-Modified-Since: Tue, 11 Jul 2000 18:23:51 GMT\r\n" "If-None-Match: \"entity_tag001\", \"tag02\"\r\n" "If-Range: Tue, 11 Jul 2000 18:23:51 GMT\r\n" "If-Unmodified-Since: Tue, 11 Jul 2000 18:23:51 GMT\r\n" "Location: a+a+://\r\n" "Max-Forwards: 3\r\n" "Proxy-Authorization: Basic dXNlcjE6c2VjcmV0\r\n" "Range: bytes=100-599\r\n" "Referer: http://www.microsoft.com/resources.asp\r\n" "TE: trailers\r\n" "User-Agent: Mozilla/4.0 (compatible; MSIE 5.5; Windows NT 5.0)\r\n" "Cookie: $Version=\"1\";user=\"WILE_E_COYOTE\";$Path=\"/acme\"\r\n" "Cache-Control: max-age=10\r\n" "Pragma: no-cache\r\n" "Transfer-Encoding: chunked, deflate\r\n" "Upgrade: SHTTP/1.3, TLS/1.0\r\n" "Via: HTTP/1.1 Proxy1\r\n" "Proxy-Connection: keep-alive\r\n" "Connection: close\r\n" "Date: Tue, 11 Jul 2000 18:23:51 GMT\r\n" "Trailer: Date, Connection\r\n" "Warning: 2112 www.nokia.com \"Disconnected Operation\"\r\n" /* This is here just because we cannot include two Retry-After headers in a single message */ "Retry-After: 60\r\n" "\r\n" ); http = http_object(msg); TEST_1(msg); TEST_1(http); TEST_1(!http->http_error); for (h = (http_header_t *)http->http_request; h; h = h->sh_succ) { if (h == (http_header_t*)http->http_payload) break; TEST_1(h1 = msg_header_dup(home, h)); n = msg_header_e(b, sizeof b, h1, 0); if (n != h->sh_len) TEST(n, h->sh_len); TEST_M(b, h->sh_data, n); su_free(home, h1); } msg_destroy(msg), msg = NULL; msg = read_message( "HTTP/1.1 200 Ok\r\n" "Accept-Ranges: none\r\n" "Age: 2147483648\r\n" "ETag: \"b38b9-17dd-367c5dcd\"\r\n" "Location: http://localhost/redirecttarget.asp\r\n" "Proxy-Authenticate: Basic realm=\"Nokia Internet Proxy\"\r\n" "Retry-After: Tue, 11 Jul 2000 18:23:51 GMT\r\n" "Server: Microsoft-IIS/5.0\r\n" "Vary: Date\r\n" "WWW-Authenticate: Digest realm=\"Nokia Intranet\", nonce=\"3wWGOvaWn3n+hFv8PK2ABQ==\", opaque=\"+GNywA==\", algorithm=MD5, qop=\"auth-int\"\r\n" "Set-Cookie: user=\"WILE_E_COYOTE\";Version=\"1\";Path=\"/acme\"\r\n" "Allow: GET, HEAD\r\n" /* This is here just because we cannot include two If-Range headers in a single message */ "If-Range: \"tag02\"\r\n" "Content-Encoding: gzip\r\n" "Content-Language: en\r\n" "Content-Length: 70\r\n" "Content-Location: http://localhost/page.asp\r\n" "Content-MD5: LLO7gLaGqGt4BI6HouiWng==\r\n" "Content-Range: bytes 2543-4532/7898\r\n" "Content-Type: text/html\r\n" "Expires: Tue, 11 Jul 2000 18:23:51 GMT\r\n" "Last-Modified: Tue, 11 Jul 2000 18:23:51 GMT\r\n" "\r\n" "<html><head><title>Heippa!</title></head><body>Heippa!</body></html>" "\r\n"); http = http_object(msg); TEST_1(msg); TEST_1(http); TEST_1(!http->http_error); for (h = (http_header_t *)http->http_status; h; h = h->sh_succ) { if (h == (http_header_t*)http->http_payload) break; TEST_1(h1 = msg_header_dup(home, h)); n = msg_header_e(b, sizeof b, h1, 0); TEST(n, h->sh_len); TEST_M(b, h->sh_data, n); su_free(home, h1); } msg_destroy(msg), msg = NULL; su_home_check(home); su_home_zap(home); END();}static int http_chunk_test(void){ msg_t *msg; http_t *http; BEGIN(); { char data[] = "\nHTTP/1.1 200 OK\r\n" "Server: Apache/1.3.11 (Unix) tomcat/1.0\r\n" "Transfer-Encoding: chunked\r\n" "Content-Type: text/html\r\n" "\r\n" "4c\r\n" "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r\n" "\r\n" "<html>\r\n" "\r\n" "\r\n9\r\n" "</html>\r\n" "\r\n0\r\n" "Date: Mon, 21 Oct 2002 13:10:41 GMT\r\n" "\n"; TEST_1(msg = read_message_byte_by_byte(data)); http = msg_object(msg); TEST_1(http); TEST_1(http->http_status); TEST_1(http->http_payload); TEST_1(http->http_payload->pl_next); // TEST(http->http_payload->pl_next->pl_next, NULL); TEST_1(http->http_date); msg_destroy(msg); } { /* Use LF only as line delimiter */ char data[] = "HTTP/1.1 200 OK\n" "Server: Apache/1.3.11 (Unix) tomcat/1.0\n" "Transfer-Encoding: chunked\n" "Content-Type: text/html\n" "\n" "48\n" "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\n" "\n" "<html>\n" "\n" "\n8\n" "</html>\n" "\n0\n" "Date: Mon, 21 Oct 2002 13:10:41 GMT\n" "\n"; TEST_1(msg = read_message_byte_by_byte(data)); http = msg_object(msg); TEST_1(http); TEST_1(http->http_status); TEST_1(http->http_payload); TEST_1(http->http_payload->pl_next); // TEST(http->http_payload->pl_next->pl_next, NULL); TEST_1(http->http_date); msg_destroy(msg); } { /* Use CR only as line delimiter */ char data[] = "HTTP/1.1 200 OK\r" "Server: Apache/1.3.11 (Unix) tomcat/1.0\r" "Transfer-Encoding: chunked\r" "Content-Type: text/html\r" "\r" "48\r" "<!DOCTYPE HTML PUBLIC \"-//W3C//DTD HTML 4.0 Transitional//EN\">\r" "\r" "<html>\r" "\r" "\r8\r" "</html>\r" "\r0\r" "Date: Mon, 21 Oct 2002 13:10:41 GMT\r" "\r"; TEST_1(msg = read_message_byte_by_byte(data)); http = msg_object(msg); TEST_1(http); TEST_1(http->http_status); TEST_1(http->http_payload); TEST_1(http->http_payload->pl_next); // TEST(http->http_payload->pl_next->pl_next, NULL); TEST_1(http->http_date); msg_destroy(msg); } END();}static int http_tag_test(void){ BEGIN(); { msg_t *msg; http_t *http; http_referer_t *r; http_upgrade_t *u; char data[] = "HTTP/1.1 200 OK\r\n" "Server: Apache/1.3.11 (Unix) tomcat/1.0\r\n" "\r\n"; TEST_1(msg = read_message_byte_by_byte(data)); http = msg_object(msg); TEST_1(http); TEST_1(http->http_status); TEST_1(http->http_server); r = http_referer_make(NULL, "ftp://ftp.funet.fi"); TEST_1(r); u = http_upgrade_make(NULL, "HTTP/1.1, TLS/1.1"); TEST_1(u); TEST_1(u->k_items); TEST_S(u->k_items[0], "HTTP/1.1"); TEST_S(u->k_items[1], "TLS/1.1"); TEST_1(!u->k_items[2]); TEST(http_add_tl(msg, http, HTTPTAG_SERVER(HTTP_NONE->sh_server), HTTPTAG_USER_AGENT_STR(NULL), HTTPTAG_USER_AGENT(NULL), HTTPTAG_REFERER(r), HTTPTAG_MAX_FORWARDS_STR("1"), HTTPTAG_HEADER((http_header_t*)u), HTTPTAG_HEADER_STR("Vary: *\r\n\r\nfoo"), TAG_END()), 5); TEST(http->http_server, NULL); TEST_1(http->http_referer); TEST_1(http->http_max_forwards); TEST_1(http->http_upgrade); TEST_1(http->http_vary); TEST_1(http->http_payload); msg_destroy(msg); } END();}staticint test_query_parser(void){ BEGIN(); { char query[] = "foo=bar&bar=baz"; char *foo = NULL, *bar = NULL, *baz = "default"; TEST(http_query_parse(NULL, NULL), -1); TEST(http_query_parse(query, "foo=", &foo, "bar=", &bar, "baz=", &baz, NULL), 2); TEST_S(foo, "bar"); TEST_S(bar, "baz"); TEST_S(baz, "default"); } { char q2[] = "f%6fo=b%61r&bar=baz&bazibazuki"; char *foo = NULL, *bar = NULL, *baz = NULL; TEST(http_query_parse(q2, "foo=", &foo, "bar=", &bar, "baz", &baz, NULL), 3); TEST_S(foo, "bar"); TEST_S(bar, "baz"); TEST_S(baz, "ibazuki"); } END();}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -