mirror of
https://github.com/xroche/httrack.git
synced 2026-06-19 00:33:03 +03:00
Compare commits
3 Commits
feature/ap
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
fb098b27b4 | ||
|
|
5f6a3fb917 | ||
|
|
f9e676dbe3 |
@@ -3129,6 +3129,43 @@ static int hts_main_internal(int argc, char **argv, httrackp * opt) {
|
||||
htsmain_free();
|
||||
return err;
|
||||
} break;
|
||||
case 'Q': { // cookie request-header selftest: httrack -#Q
|
||||
static t_cookie cookie;
|
||||
char hdr[1024];
|
||||
/* RFC 6265: bare name=value pairs, no $Version/$Path (#151). */
|
||||
const char *expected = "Cookie: name=value; has_js=1" H_CRLF;
|
||||
int err = 0;
|
||||
|
||||
const char *dom = "www.example.com";
|
||||
int added;
|
||||
|
||||
cookie.max_len = (int) sizeof(cookie.data);
|
||||
cookie.data[0] = '\0';
|
||||
added = cookie_add(&cookie, "name", "value", dom, "/");
|
||||
added |= cookie_add(&cookie, "has_js", "1", dom, "/");
|
||||
/* different domain: must be filtered out */
|
||||
added |= cookie_add(&cookie, "junk", "x", "other.org", "/");
|
||||
if (added) {
|
||||
printf("cookie-header: FAIL (cookie_add setup)\n");
|
||||
htsmain_free();
|
||||
return 1;
|
||||
}
|
||||
|
||||
http_cookie_header_selftest(&cookie, dom, "/", hdr,
|
||||
sizeof(hdr));
|
||||
if (strcmp(hdr, expected) != 0)
|
||||
err = 1;
|
||||
if (strstr(hdr, "$Version") != NULL ||
|
||||
strstr(hdr, "$Path") != NULL)
|
||||
err = 1;
|
||||
if (strstr(hdr, "junk") != NULL) // wrong-domain cookie leaked
|
||||
err = 1;
|
||||
printf("cookie-header: %s\n", err ? "FAIL" : "OK");
|
||||
if (err)
|
||||
printf(" got: %s\n", hdr);
|
||||
htsmain_free();
|
||||
return err;
|
||||
} break;
|
||||
case '!':
|
||||
HTS_PANIC_PRINTF
|
||||
("Option #! is disabled for security reasons");
|
||||
|
||||
73
src/htslib.c
73
src/htslib.c
@@ -874,6 +874,50 @@ static void print_buffer(buff_struct*const str, const char *format, ...) {
|
||||
assertf(str->pos < str->capacity);
|
||||
}
|
||||
|
||||
/* Append the request "Cookie:" header line for every stored cookie matching
|
||||
domain/path. RFC 6265 form: bare "name=value" pairs joined by "; ", no
|
||||
$Version/$Path attributes (those are RFC 2965 syntax that modern servers
|
||||
reject, issue #151). Returns the number of cookies emitted. */
|
||||
static int append_cookie_header(buff_struct *bstr, t_cookie *cookie,
|
||||
const char *domain, const char *path) {
|
||||
char buffer[8192];
|
||||
char *b;
|
||||
int cook = 0;
|
||||
int max_cookies = 8;
|
||||
|
||||
if (cookie == NULL)
|
||||
return 0;
|
||||
b = cookie->data;
|
||||
do {
|
||||
b = cookie_find(b, "", domain, path); // next matching cookie
|
||||
if (b != NULL) {
|
||||
max_cookies--;
|
||||
if (!cook) {
|
||||
print_buffer(bstr, "Cookie: ");
|
||||
cook = 1;
|
||||
} else
|
||||
print_buffer(bstr, "; ");
|
||||
print_buffer(bstr, "%s", cookie_get(buffer, b, 5));
|
||||
print_buffer(bstr, "=%s", cookie_get(buffer, b, 6));
|
||||
b = cookie_nextfield(b);
|
||||
}
|
||||
} while (b != NULL && max_cookies > 0);
|
||||
if (cook)
|
||||
print_buffer(bstr, H_CRLF);
|
||||
return cook;
|
||||
}
|
||||
|
||||
/* Self-test entry for append_cookie_header(): build the request Cookie line
|
||||
into dst (always NUL-terminated). Returns the number of cookies emitted. */
|
||||
int http_cookie_header_selftest(t_cookie *cookie, const char *domain,
|
||||
const char *path, char *dst, size_t dst_size) {
|
||||
buff_struct bstr = {dst, dst_size, 0};
|
||||
|
||||
assertf(dst != NULL && dst_size > 0);
|
||||
dst[0] = '\0';
|
||||
return append_cookie_header(&bstr, cookie, domain, path);
|
||||
}
|
||||
|
||||
// envoi d'une requète
|
||||
int http_sendhead(httrackp * opt, t_cookie * cookie, int mode,
|
||||
const char *xsend, const char *adr, const char *fil,
|
||||
@@ -1048,34 +1092,9 @@ int http_sendhead(httrackp * opt, t_cookie * cookie, int mode,
|
||||
search_tag + strlen(POSTTOK) + 1))));
|
||||
}
|
||||
}
|
||||
// gestion cookies?
|
||||
// send stored cookies matching this host/path
|
||||
if (cookie) {
|
||||
char buffer[8192];
|
||||
char *b = cookie->data;
|
||||
int cook = 0;
|
||||
int max_cookies = 8;
|
||||
|
||||
do {
|
||||
b = cookie_find(b, "", jump_identification_const(adr), fil); // prochain cookie satisfaisant aux conditions
|
||||
if (b != NULL) {
|
||||
max_cookies--;
|
||||
if (!cook) {
|
||||
print_buffer(&bstr, "Cookie: $Version=1; ");
|
||||
cook = 1;
|
||||
} else
|
||||
print_buffer(&bstr, "; ");
|
||||
print_buffer(&bstr, "%s", cookie_get(buffer, b, 5));
|
||||
print_buffer(&bstr, "=%s", cookie_get(buffer, b, 6));
|
||||
print_buffer(&bstr, "; $Path=%s", cookie_get(buffer, b, 2));
|
||||
b = cookie_nextfield(b);
|
||||
}
|
||||
} while(b != NULL && max_cookies > 0);
|
||||
if (cook) { // on a envoyé un (ou plusieurs) cookie?
|
||||
print_buffer(&bstr, H_CRLF);
|
||||
#if DEBUG_COOK
|
||||
printf("Header:\n%s\n", bstr.buffer);
|
||||
#endif
|
||||
}
|
||||
append_cookie_header(&bstr, cookie, jump_identification_const(adr), fil);
|
||||
}
|
||||
// gérer le keep-alive (garder socket)
|
||||
if (retour->req.http11 && !retour->req.nokeepalive) {
|
||||
|
||||
@@ -182,6 +182,11 @@ int http_sendhead(httrackp * opt, t_cookie * cookie, int mode, const char *xsend
|
||||
const char *adr, const char *fil,
|
||||
const char *referer_adr, const char *referer_fil,
|
||||
htsblk * retour);
|
||||
/* Build the request "Cookie:" header line for stored cookies matching
|
||||
domain/path into dst (NUL-terminated). Exposed for the -#Q self-test;
|
||||
wraps the same logic http_sendhead() uses. Returns cookies emitted. */
|
||||
int http_cookie_header_selftest(t_cookie *cookie, const char *domain,
|
||||
const char *path, char *dst, size_t dst_size);
|
||||
|
||||
//int newhttp(char* iadr,char* err=NULL);
|
||||
T_SOC newhttp(httrackp * opt, const char *iadr, htsblk * retour, int port,
|
||||
|
||||
15
tests/01_engine-cookies.test
Executable file
15
tests/01_engine-cookies.test
Executable file
@@ -0,0 +1,15 @@
|
||||
#!/bin/bash
|
||||
#
|
||||
# Issue #151 guard: the request Cookie header must be bare RFC 6265 name=value
|
||||
# pairs, no $Version/$Path attributes. Driven by the 'httrack -#Q' selftest.
|
||||
|
||||
set -eu
|
||||
|
||||
# A trailing token is required; a bare '-#Q' falls through to the usage screen.
|
||||
out=$(httrack -#Q run)
|
||||
|
||||
# Exact-match the success line so a fall-through to usage can't pass the test.
|
||||
test "$out" = "cookie-header: OK" || {
|
||||
echo "expected 'cookie-header: OK', got: $out" >&2
|
||||
exit 1
|
||||
}
|
||||
@@ -24,6 +24,7 @@ TESTS = \
|
||||
01_engine-cache-golden.test \
|
||||
01_engine-charset.test \
|
||||
01_engine-cmdline.test \
|
||||
01_engine-cookies.test \
|
||||
01_engine-copyopt.test \
|
||||
01_engine-doitlog.test \
|
||||
01_engine-entities.test \
|
||||
|
||||
Reference in New Issue
Block a user