char *mget_cookie_create_request_header(const MGET_IRI *iri) { int it, init = 0; time_t now = time(NULL); mget_buffer_t buf; debug_printf("cookie_create_request_header for host=%s path=%s\n",iri->host,iri->path); pthread_mutex_lock(&cookies_mutex); for (it = 0; it < mget_vector_size(cookies); it++) { MGET_COOKIE *cookie = mget_vector_get(cookies, it); if (((!cookie->host_only && domain_match(cookie->domain, iri->host)) || (cookie->host_only && !strcasecmp(cookie->domain, iri->host))) && (!cookie->expires || cookie->expires >= now) && (!cookie->secure_only || (cookie->secure_only && iri->scheme == IRI_SCHEME_HTTPS)) && path_match(cookie->path, iri->path)) { if (!init) { mget_buffer_init(&buf, NULL, 128); init = 1; } if (buf.length) mget_buffer_printf_append2(&buf, "; %s=%s", cookie->name, cookie->value); else mget_buffer_printf_append2(&buf, "%s=%s", cookie->name, cookie->value); } } pthread_mutex_unlock(&cookies_mutex); return init ? buf.data : NULL; }
const char *mget_iri_escape_path(const char *src, mget_buffer_t *buf) { const char *begin; for (begin = src; *src; src++) { if (!mget_iri_isunreserved_path(*src)) { if (begin != src) mget_buffer_memcat(buf, begin, src - begin); begin = src + 1; mget_buffer_printf_append2(buf, "%%%02X", (unsigned char)*src); } } if (begin != src) mget_buffer_memcat(buf, begin, src - begin); return buf->data; }
const char *mget_iri_escape_query(const char *src, mget_buffer_t *buf) { const char *begin; for (begin = src; *src; src++) { if (!mget_iri_isunreserved(*src) && *src != '=') { if (begin != src) mget_buffer_memcat(buf, begin, src - begin); begin = src + 1; if (*src == ' ') mget_buffer_memcat(buf, "+", 1); else mget_buffer_printf_append2(buf, "%%%02X", (unsigned char)*src); } } if (begin != src) mget_buffer_memcat(buf, begin, src - begin); return buf->data; }
static void test_buffer(void) { char sbuf[16]; mget_buffer_t buf, *bufp; // testing buffer on stack, using initial stack memory // without resizing mget_buffer_init(&buf, sbuf, sizeof(sbuf)); mget_buffer_deinit(&buf); // testing buffer on stack, using initial stack memory // with resizing mget_buffer_init(&buf, sbuf, sizeof(sbuf)); _test_buffer(&buf, "Test 1"); mget_buffer_deinit(&buf); // testing buffer on stack, using initial heap memory // without resizing mget_buffer_init(&buf, NULL, 16); mget_buffer_deinit(&buf); // testing buffer on stack, using initial heap memory // with resizing mget_buffer_init(&buf, NULL, 16); _test_buffer(&buf, "Test 2"); mget_buffer_deinit(&buf); // testing buffer on heap, using initial stack memory // without resizing bufp = mget_buffer_init(NULL, sbuf, sizeof(sbuf)); mget_buffer_deinit(bufp); bufp = mget_buffer_init(NULL, sbuf, sizeof(sbuf)); mget_buffer_free(&bufp); // testing buffer on heap, using initial stack memory // with resizing bufp = mget_buffer_init(NULL, sbuf, sizeof(sbuf)); _test_buffer(bufp, "Test 3"); mget_buffer_deinit(bufp); bufp = mget_buffer_init(NULL, sbuf, sizeof(sbuf)); _test_buffer(bufp, "Test 4"); mget_buffer_free(&bufp); // testing buffer on heap, using initial heap memory // without resizing bufp = mget_buffer_alloc(16); mget_buffer_free(&bufp); // testing buffer on heap, using initial heap memory // with resizing bufp = mget_buffer_alloc(16); _test_buffer(bufp, "Test 5"); mget_buffer_free(&bufp); // check that appending works mget_buffer_init(&buf, sbuf, sizeof(sbuf)); mget_buffer_strcpy(&buf, "A"); mget_buffer_strcat(&buf, "B"); mget_buffer_memcat(&buf, "C", 1); mget_buffer_memset_append(&buf, 'D', 1); mget_buffer_printf_append2(&buf, "%s", "E"); if (!strcmp(buf.data, "ABCDE")) ok++; else { failed++; info_printf("test_buffer.append: got %s (expected %s)\n", buf.data, "ABCDE"); } mget_buffer_deinit(&buf); // test mget_buffer_trim() mget_buffer_init(&buf, sbuf, sizeof(sbuf)); for (int mid_ws = 0; mid_ws <= 2; mid_ws++) { char expected[16]; snprintf(expected, sizeof(expected), "x%.*sy", mid_ws, " "); for (int lead_ws = 0; lead_ws <= 2; lead_ws++) { for (int trail_ws = 0; trail_ws <= 2; trail_ws++) { mget_buffer_printf2(&buf, "%.*sx%.*sy%.*s", lead_ws, " ", mid_ws, " ", trail_ws, " "); mget_buffer_trim(&buf); if (!strcmp(buf.data, expected)) ok++; else { failed++; info_printf("test_buffer_trim: got '%s' (expected '%s') (%d, %d, %d)\n", buf.data, expected, lead_ws, mid_ws, trail_ws); } } } } mget_buffer_deinit(&buf); }