int main(void) { char *empty = (char *)""; wget_info_printf("%d\n", wget_base64_is_string("")); // base64.c wget_buffer_alloc(0); // buffer.c wget_buffer_printf((wget_buffer_t *)1, "%s", ""); // buffer_printf.c strlcpy((char *)"", "", 0); // strlcpy.c wget_css_parse_buffer((const char *)1, 0, NULL, NULL, NULL); // css.c wget_decompress_close(NULL); // decompressor.c wget_hashmap_create(0, 0, NULL, NULL); // hashmap.c wget_fdgetline(&empty, (size_t *)1, 0); // io.c wget_iri_parse("", NULL); // iri.c wget_list_free((wget_list_t **)1); // list.c wget_debug_write("", 0); // log.c wget_logger_set_file(NULL, ""); // logger.c wget_tcp_set_connect_timeout(NULL, 0); // net.c wget_netrc_deinit(NULL); // netrc.c wget_strdup(""); // mem.c // wget_popenf("r", "%s", ""); // pipe.c // wget_bsprintf(NULL, NULL, "%s", ""); // printf.c wget_ssl_set_config_int(0, 0); // ssl_[gnutls].c wget_stringmap_create(0); // stringmap.c if (wget_strcmp("", "")) {}; // utils.c wget_vector_set_destructor(NULL, NULL); // vector.c wget_malloc(1); // xalloc.c wget_xml_parse_buffer("", NULL, NULL, 0); // xml.c }
static void _add_mirror(_metalink_context_t *ctx, const char *value) { if (wget_strncasecmp_ascii(value, "http:", 5) && wget_strncasecmp_ascii(value, "https:", 6)) return; wget_metalink_t *metalink = ctx->metalink; wget_metalink_mirror_t mirror; memset(&mirror, 0, sizeof(wget_metalink_mirror_t)); strlcpy(mirror.location, ctx->location, sizeof(mirror.location)); mirror.priority = ctx->priority; mirror.iri = wget_iri_parse(value, NULL); if (!mirror.iri) return; if (!metalink->mirrors) { metalink->mirrors = wget_vector_create(4, 4, NULL); wget_vector_set_destructor(metalink->mirrors, (void(*)(void *))_free_mirror); } wget_vector_add(metalink->mirrors, &mirror, sizeof(wget_metalink_mirror_t)); *ctx->location = 0; ctx->priority = 999999; }
// Callback function, called from CSS parser for each URI found. static void _css_get_url(void *context, const char *url, size_t len, size_t pos) { _CSS_CONTEXT *ctx = context; WGET_PARSED_URL parsed_url = { .len = len, .pos = pos, .url = wget_strmemdup(url, len), .abs_url = NULL }; if (!ctx->uris) { ctx->uris = wget_vector_create(16, -2, NULL); wget_vector_set_destructor(ctx->uris, (wget_vector_destructor_t)_free_url); } wget_vector_add(ctx->uris, &parsed_url, sizeof(parsed_url)); } static void _urls_to_absolute(wget_vector_t *urls, wget_iri_t *base) { if (base && urls) { wget_buffer_t buf; wget_buffer_init(&buf, NULL, 1024); for (int it = 0; it < wget_vector_size(urls); it++) { WGET_PARSED_URL *url = wget_vector_get(urls, it); if (wget_iri_relative_to_abs(base, url->url, url->len, &buf)) url->abs_url = wget_strmemdup(buf.data, buf.length); else error_printf("Cannot resolve relative URI '%s'\n", url->url); } wget_buffer_deinit(&buf); } } wget_vector_t *wget_css_get_urls(const char *css, size_t len, wget_iri_t *base, const char **encoding) { _CSS_CONTEXT context = { .encoding = encoding }; wget_css_parse_buffer(css, len, _css_get_url, encoding ? _css_get_encoding : NULL, &context); _urls_to_absolute(context.uris, base); return context.uris; }
static void test_stringmap(void) { wget_stringmap_t *m; char key[128], value[128], *val; int run, it; size_t valuesize; // the initial size of 16 forces the internal reshashing function to be called twice m = wget_stringmap_create(16); for (run = 0; run < 2; run++) { if (run) { wget_stringmap_clear(m); wget_stringmap_sethashfunc(m, hash_txt); } for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); valuesize = sprintf(value, "%d.html", it); if (wget_stringmap_put(m, key, value, valuesize + 1)) { failed++; info_printf("stringmap_put(%s) returns unexpected old value\n", key); } else ok++; } if ((it = wget_stringmap_size(m)) != 26) { failed++; info_printf("stringmap_size() returned %d (expected %d)\n", it, 26); } else ok++; // now, look up every single entry for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); sprintf(value, "%d.html", it); if (!(val = wget_stringmap_get(m, key))) { failed++; info_printf("stringmap_get(%s) didn't find entry\n", key); } else if (strcmp(val, value)) { failed++; info_printf("stringmap_get(%s) found '%s' (expected '%s')\n", key, val, value); } else ok++; } wget_stringmap_clear(m); if ((it = wget_stringmap_size(m)) != 0) { failed++; info_printf("stringmap_size() returned %d (expected 0)\n", it); } else ok++; for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); valuesize = sprintf(value, "%d.html", it); if (wget_stringmap_put(m, key, value, valuesize + 1)) { failed++; info_printf("stringmap_put(%s) returns unexpected old value\n", key); } else ok++; } if ((it = wget_stringmap_size(m)) != 26) { failed++; info_printf("stringmap_size() returned %d (expected %d)\n", it, 26); } else ok++; // now, remove every single entry for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); sprintf(value, "%d.html", it); wget_stringmap_remove(m, key); } if ((it = wget_stringmap_size(m)) != 0) { failed++; info_printf("stringmap_size() returned %d (expected 0)\n", it); } else ok++; for (it = 0; it < 26; it++) { sprintf(key, "http://www.example.com/subdir/%d.html", it); valuesize = sprintf(value, "%d.html", it); if (wget_stringmap_put(m, key, value, valuesize + 1)) { failed++; info_printf("stringmap_put(%s) returns unexpected old value\n", key); } else ok++; } if ((it = wget_stringmap_size(m)) != 26) { failed++; info_printf("stringmap_size() returned %d (expected %d)\n", it, 26); } else ok++; } // testing alloc/free in stringmap/hashmap wget_stringmap_clear(m); wget_stringmap_put(m, "thekey", NULL, 0) ? failed++ : ok++; wget_stringmap_put(m, "thekey", NULL, 0) ? ok++ : failed++; wget_stringmap_put(m, "thekey", "thevalue", 9) ? ok++ : failed++; wget_stringmap_put(m, "thekey", "thevalue", 9) ? ok++ : failed++; wget_stringmap_put(m, "thekey", NULL, 0) ? ok++ : failed++; // testing key/value identity alloc/free in stringmap/hashmap wget_stringmap_clear(m); wget_stringmap_put(m, "thekey", NULL, 0) ? failed++ : ok++; wget_stringmap_put(m, "thekey", NULL, 0) ? ok++ : failed++; wget_stringmap_put(m, "thekey", "thevalue", 9) ? ok++ : failed++; wget_stringmap_put(m, "thekey", NULL, 0) ? ok++ : failed++; wget_stringmap_free(&m); wget_http_challenge_t challenge; wget_http_parse_challenge("Basic realm=\"test realm\"", &challenge); wget_http_free_challenge(&challenge); wget_vector_t *challenges; challenges = wget_vector_create(2, 2, NULL); wget_vector_set_destructor(challenges, (void(*)(void *))wget_http_free_challenge); wget_http_parse_challenge("Basic realm=\"test realm\"", &challenge); wget_vector_add(challenges, &challenge, sizeof(challenge)); wget_http_free_challenges(&challenges); char *response_text = strdup( "HTTP/1.1 401 Authorization Required\r\n"\ "Date: Sun, 23 Dec 2012 21:03:45 GMT\r\n"\ "Server: Apache/2.2.22 (Debian)\r\n"\ "WWW-Authenticate: Digest realm=\"therealm\", nonce=\"Ip6MaovRBAA=c4af733c51270698260f5d357724c2cbce20fa3d\", algorithm=MD5, domain=\"/prot_digest_md5\", qop=\"auth\"\r\n"\ "Vary: Accept-Encoding\r\n"\ "Content-Length: 476\r\n"\ "Keep-Alive: timeout=5, max=99\r\n"\ "Connection: Keep-Alive\r\n"\ "Content-Type: text/html; charset=iso-8859-1\r\n\r\n"); wget_iri_t *iri = wget_iri_parse("http://localhost/prot_digest_md5/", NULL); wget_http_request_t *req = wget_http_create_request(iri, "GET"); wget_http_response_t *resp = wget_http_parse_response_header(response_text); wget_http_add_credentials(req, wget_vector_get(resp->challenges, 0), "tim", "123"); // for (it=0;it<vec_size(req->lines);it++) { // info_printf("%s\n", (char *)vec_get(req->lines, it)); // } wget_http_free_response(&resp); wget_http_free_request(&req); wget_iri_free(&iri); xfree(response_text); // Authorization: Digest username="******", realm="therealm", nonce="Ip6MaovRBAA=c4af733c51270698260f5d357724c2cbce20fa3d", uri="/prot_digest_md5/", response="a99e2012d507a73dd46eb044d3f4641c", qop=auth, nc=00000001, cnonce="3d20faa1" }
static void test_parse_challenge(void) { static const struct test_data { const char * input; const char * scheme[3]; } test_data[] = { { // simplebasic "Basic realm=\"foo\"", { "Basic", NULL } }, { // simplebasicucase "BASIC REALM=\"foo\"", { "Basic", NULL } }, { // simplebasicucase "Basic , realm=\"foo\"", { "Basic", NULL } }, { // "Basic realm=\"test realm\"", { "Basic", NULL } }, { // "Basic realm=\"test-äöÜ\"", { "Basic", NULL } }, { // "Basic realm=\"basic\", Newauth realm=\"newauth\"", { "Basic", "Newauth", NULL } }, }; wget_vector_t *challenges; wget_http_challenge_t *challenge; // Testcases found here http://greenbytes.de/tech/tc/httpauth/ challenges = wget_vector_create(2, 2, NULL); wget_vector_set_destructor(challenges, (void(*)(void *))wget_http_free_challenge); for (unsigned it = 0; it < countof(test_data); it++) { const struct test_data *t = &test_data[it]; wget_http_parse_challenges(t->input, challenges); for (unsigned nchal = 0; nchal < countof(test_data[0].scheme) && t->scheme[nchal]; nchal++) { challenge = wget_vector_get(challenges, nchal); if (!t->scheme[nchal]) { if (challenge) { failed++; info_printf("Failed [%u]: wget_http_parse_challenges(%s) found %d challenges (expected %u)\n", it, t->input, wget_vector_size(challenges), nchal); } break; } if (!challenge) { failed++; info_printf("Failed [%u]: wget_http_parse_challenges(%s) did not find enough challenges\n", it, t->input); break; } if (!wget_strcasecmp_ascii(challenge->auth_scheme, t->scheme[nchal])) { ok++; } else { failed++; info_printf("Failed [%u]: wget_http_parse_challenges(%s) -> '%s' (expected '%s')\n", it, t->input, challenge->auth_scheme, t->scheme[nchal]); } } wget_vector_clear(challenges); } wget_http_free_challenges(&challenges); }