links

lynx-like text mode web browser
git clone anongit@rnpnr.xyz:links.git
Log | Files | Refs | Feed | README | LICENSE

Commit: fd46338c3de84a0aba110038e38edc5eee6b3e78
Parent: a241cc7a5529d58c8cffd2141ddb25a58e71456f
Author: opask
Date:   Wed,  8 Aug 2018 22:11:07 -0600

style: http.c

Diffstat:
Mhttp.c | 528++++++++++++++++++++++++++++++++++++++++++++++---------------------------------
1 file changed, 308 insertions(+), 220 deletions(-)

diff --git a/http.c b/http.c @@ -4,6 +4,9 @@ * This file is a part of the Links program, released under GPL. */ +#include <stdlib.h> +#include <string.h> + #include "links.h" struct http_connection_info { @@ -44,25 +47,30 @@ static void add_extra_options(unsigned char **hdr, int *l); unsigned char *parse_http_header(unsigned char *head, unsigned char *item, unsigned char **ptr) { unsigned char *i, *f, *g, *h; - if (!head) return NULL; + if (!head) + return NULL; for (f = head; *f; f++) { - if (*f != 10) continue; + if (*f != 10) + continue; f++; for (i = item; *i && *f; i++, f++) - if (upcase(*i) != upcase(*f)) goto cont; - if (!*f) break; + if (upcase(*i) != upcase(*f)) + goto cont; + if (!*f) + break; if (f[0] == ':') { - while (f[1] == ' ') f++; + while (f[1] == ' ') + f++; for (g = ++f; *g >= ' '; g++) ; - while (g > f && g[-1] == ' ') g--; + while (g > f && g[-1] == ' ') + g--; h = memacpy(f, g - f); - if (ptr) { + if (ptr) *ptr = f; - } return h; } - cont: +cont: f--; } return NULL; @@ -71,47 +79,60 @@ unsigned char *parse_http_header(unsigned char *head, unsigned char *item, unsig unsigned char *parse_header_param(unsigned char *x, unsigned char *e, int all) { unsigned char u; - size_t le = strlen(cast_const_char e); + size_t le = strlen((char *)e); int lp; unsigned char *y = x; if (!all) { - a: - if (!(y = cast_uchar strchr(cast_const_char y, ';'))) return NULL; +a: + if (!(y = cast_uchar strchr((char *)y, ';'))) + return NULL; } while (*y && (*y == ';' || *y <= ' ')) y++; - if (strlen(cast_const_char y) < le) return NULL; - if (casecmp(y, e, le)) goto a; + if (strlen((char *)y) < le) + return NULL; + if (casecmp(y, e, le)) + goto a; y += le; - while (*y && (*y <= ' ' || *y == '=')) y++; + while (*y && (*y <= ' ' || *y == '=')) + y++; u = ';'; - if (*y == '\'' || *y == '"') u = *y++; + if (*y == '\'' || *y == '"') + u = *y++; lp = 0; while (y[lp] >= ' ' && y[lp] != u) { lp++; - if (lp == MAXINT) overalloc(); + if (lp == MAXINT) + overalloc(); } return memacpy(y, lp); } int get_http_code(unsigned char *head, int *code, int *version) { - if (!head) return -1; - while (head[0] == ' ') head++; - if (upcase(head[0]) != 'H' || upcase(head[1]) != 'T' || upcase(head[2]) != 'T' || - upcase(head[3]) != 'P') return -1; + if (!head) + return -1; + while (head[0] == ' ') + head++; + if (upcase(head[0]) != 'H' || upcase(head[1]) != 'T' || upcase(head[2]) != 'T' + || upcase(head[3]) != 'P') return -1; if (head[4] == '/' && head[5] >= '0' && head[5] <= '9' - && head[6] == '.' && head[7] >= '0' && head[7] <= '9' && head[8] <= ' ') { - if (version) *version = (head[5] - '0') * 10 + head[7] - '0'; - } else if (version) *version = 0; + && head[6] == '.' && head[7] >= '0' && head[7] <= '9' && head[8] <= ' ') { + if (version) + *version = (head[5] - '0') * 10 + head[7] - '0'; + } else if (version) + *version = 0; for (head += 4; *head > ' '; head++) ; - if (*head++ != ' ') return -1; - if (head[0] < '1' || head [0] > '9' || head[1] < '0' || head[1] > '9' || - head[2] < '0' || head [2] > '9') { - if (code) *code = 200; + if (*head++ != ' ') + return -1; + if (head[0] < '1' || head [0] > '9' || head[1] < '0' || head[1] > '9' + || head[2] < '0' || head [2] > '9') { + if (code) + *code = 200; return 0; } - if (code) *code = (head[0]-'0')*100 + (head[1]-'0')*10 + head[2]-'0'; + if (code) + *code = (head[0]-'0')*100 + (head[1]-'0')*10 + head[2]-'0'; return 0; } @@ -133,10 +154,14 @@ static int check_http_server_bugs(unsigned char *url, struct http_connection_inf { unsigned char *server; int i, bugs; - if (!http_options.allow_blacklist || info->http10) return 0; - if (!(server = parse_http_header(head, cast_uchar "Server", NULL))) return 0; + if (!http_options.allow_blacklist || info->http10) + return 0; + if (!(server = parse_http_header(head, cast_uchar "Server", NULL))) + return 0; bugs = 0; - for (i = 0; buggy_servers[i].name; i++) if (strstr(cast_const_char server, cast_const_char buggy_servers[i].name)) bugs |= buggy_servers[i].bugs; + for (i = 0; buggy_servers[i].name; i++) + if (strstr((char *)server, (char *)buggy_servers[i].name)) + bugs |= buggy_servers[i].bugs; free(server); if (bugs && (server = get_host_name(url))) { add_blacklist_entry(server, bugs); @@ -148,21 +173,19 @@ static int check_http_server_bugs(unsigned char *url, struct http_connection_inf static void http_end_request(struct connection *c, int notrunc, int nokeepalive, int state) { - if (state == S__OK) { - if (c->cache) { - if (!notrunc) truncate_entry(c->cache, c->from, 1); - c->cache->incomplete = 0; - } + if (state == S__OK && c->cache) { + if (!notrunc) + truncate_entry(c->cache, c->from, 1); + c->cache->incomplete = 0; } setcstate(c, state); - if (c->info && - !((struct http_connection_info *)c->info)->close && - !((struct http_connection_info *)c->info)->send_close && - !nokeepalive) { + if (c->info + && !((struct http_connection_info *)c->info)->close + && !((struct http_connection_info *)c->info)->send_close + && !nokeepalive) { add_keepalive_socket(c, HTTP_KEEPALIVE_TIMEOUT, 0); - } else { + } else abort_connection(c); - } } void http_func(struct connection *c) @@ -177,9 +200,8 @@ void http_func(struct connection *c) return; } make_connection(c, p, &c->sock1, http_send_header); - } else { + } else http_send_header(c); - } } void proxy_func(struct connection *c) @@ -193,11 +215,10 @@ static void add_url_to_str(unsigned char **str, int *l, unsigned char *url) for (sp = url; *sp && *sp != POST_CHAR; sp++) { if (*sp <= ' ' || *sp >= 127) { unsigned char esc[4]; - sprintf(cast_char esc, "%%%02X", (int)*sp); + sprintf((char *)esc, "%%%02X", (int)*sp); add_to_str(str, l, esc); - } else { + } else add_chr_to_str(str, l, *sp); - } } } @@ -213,10 +234,9 @@ static void http_send_header(struct connection *c) unsigned char *post = NULL; unsigned char *host; - if (!c->cache) { + if (!c->cache) if (!find_in_cache(c->url, &c->cache)) c->cache->refcount--; - } proxy = is_proxy_url(c->url); host = remove_proxy_prefix(c->url); @@ -224,7 +244,8 @@ static void http_send_header(struct connection *c) info = mem_calloc(sizeof(struct http_connection_info)); c->info = info; info->https_forward = !c->ssl && proxy && host && !casecmp(host, cast_uchar "https://", 8); - if (c->ssl) proxy = 0; + if (c->ssl) + proxy = 0; hdr = init_str(); if (!host) { http_bad_url: @@ -236,10 +257,11 @@ static void http_send_header(struct connection *c) info->bl_flags = get_blacklist_flags(h); free(h); } - if (info->bl_flags & BL_HTTP10) http10 = 1; + if (info->bl_flags & BL_HTTP10) + http10 = 1; info->http10 = http10; if (!info->https_forward) { - post = cast_uchar strchr(cast_const_char host, POST_CHAR); + post = cast_uchar strchr((char *)host, POST_CHAR); if (post) post++; } info->send_close = info->https_forward || http10 || (post && http_options.bug_post_no_keepalive); @@ -275,7 +297,9 @@ static void http_send_header(struct connection *c) unsigned char *u_host; int u_host_len; int u2_len = 0; - if (parse_url(u, NULL, NULL, NULL, NULL, NULL, &u_host, &u_host_len, NULL, NULL, NULL, NULL, NULL)) goto http_bad_url; + if (parse_url(u, NULL, NULL, NULL, NULL, NULL, &u_host, + &u_host_len, NULL, NULL, NULL, NULL, NULL)) + goto http_bad_url; u2 = init_str(); add_bytes_to_str(&u2, &u2_len, u, u_host + u_host_len - u); add_to_str(&u2, &u2_len, proxies.dns_append); @@ -284,16 +308,16 @@ static void http_send_header(struct connection *c) add_url_to_str(&hdr, &l, u2); if (u2 != u) free(u2); - added_connect: +added_connect: if (!http10) add_to_str(&hdr, &l, cast_uchar " HTTP/1.1\r\n"); else add_to_str(&hdr, &l, cast_uchar " HTTP/1.0\r\n"); if (!info->https_forward && (h = get_host_name(host))) { add_to_str(&hdr, &l, cast_uchar "Host: "); - if (*h && h[strlen(cast_const_char h) - 1] == '.') { - h[strlen(cast_const_char h) - 1] = 0; + if (*h && h[strlen((char *)h) - 1] == '.') { + h[strlen((char *)h) - 1] = 0; } - if (h[0] == '[' && h[strlen(cast_const_char h) - 1] == ']') { - unsigned char *pc = cast_uchar strchr(cast_const_char h, '%'); + if (h[0] == '[' && h[strlen((char *)h) - 1] == ']') { + char *pc = strchr((char *)h, '%'); if (pc) { pc[0] = ']'; pc[1] = 0; @@ -309,7 +333,8 @@ static void http_send_header(struct connection *c) add_to_str(&hdr, &l, cast_uchar "\r\n"); } add_user_agent(&hdr, &l); - if (proxy) add_proxy_auth_string(&hdr, &l, c->url); + if (proxy) + add_proxy_auth_string(&hdr, &l, c->url); if (!info->https_forward) { test_restart(c); add_referer(&hdr, &l, host, c->prev_url); @@ -369,9 +394,9 @@ static void test_restart(struct connection *c) static void add_user_agent(unsigned char **hdr, int *l) { add_to_str(hdr, l, cast_uchar "User-Agent: "); - if (SCRUB_HEADERS) { + if (SCRUB_HEADERS) add_to_str(hdr, l, cast_uchar "Mozilla/5.0 (Windows NT 6.1; rv:52.0) Gecko/20100101 Firefox/52.0\r\n"); - } else if (!(*http_options.header.fake_useragent)) { + else if (!(*http_options.header.fake_useragent)) { add_to_str(hdr, l, cast_uchar("Links (" VERSION "; ")); add_to_str(hdr, l, system_name); add_to_str(hdr, l, cast_uchar "; "); @@ -380,13 +405,11 @@ static void add_user_agent(unsigned char **hdr, int *l) add_to_str(hdr, l, t); } #ifdef G - else if (F && drv) { + else if (F && drv) add_to_str(hdr, l, drv->name); - } #endif - else { + else add_to_str(hdr, l, cast_uchar "dump"); - } add_to_str(hdr, l, cast_uchar ")\r\n"); } else { add_to_str(hdr, l, http_options.header.fake_useragent); @@ -396,84 +419,90 @@ static void add_user_agent(unsigned char **hdr, int *l) static void add_referer(unsigned char **hdr, int *l, unsigned char *url, unsigned char *prev_url) { + switch (http_options.header.referer) { - case REFERER_FAKE: { - add_to_str(hdr, l, cast_uchar "Referer: "); - add_to_str(hdr, l, http_options.header.fake_referer); - add_to_str(hdr, l, cast_uchar "\r\n"); - break; - } + case REFERER_FAKE: + add_to_str(hdr, l, cast_uchar "Referer: "); + add_to_str(hdr, l, http_options.header.fake_referer); + add_to_str(hdr, l, cast_uchar "\r\n"); + break; - case REFERER_SAME_URL: { - add_to_str(hdr, l, cast_uchar "Referer: "); - add_url_to_str(hdr, l, url); - add_to_str(hdr, l, cast_uchar "\r\n"); - break; - } + case REFERER_SAME_URL: + add_to_str(hdr, l, cast_uchar "Referer: "); + add_url_to_str(hdr, l, url); + add_to_str(hdr, l, cast_uchar "\r\n"); + break; - case REFERER_REAL_SAME_SERVER: { - unsigned char *h, *j; - int brk = 1; - if ((h = get_host_name(url))) { - if ((j = get_host_name(prev_url))) { - if (!casestrcmp(h, j)) brk = 0; - else if (!casestrcmp(h, cast_uchar "imageproxy.jxs.cz")) { - int l = (int)strlen(cast_const_char j); - int q = (int)strlen(".blog.cz"); - if (l > q && !casestrcmp((j + l - q), cast_uchar ".blog.cz")) brk = 0; - else if (!casestrcmp(j, cast_uchar "blog.cz")) brk = 0; - } else if (!casestrcmp(h, cast_uchar "www.google.com")) { - unsigned char *c = get_url_data(url); - if (c && !strncmp(cast_const_char c, "recaptcha/api/", 14)) brk = 0; - } - free(j); + case REFERER_REAL_SAME_SERVER: { + unsigned char *h, *j; + int brk = 1; + if ((h = get_host_name(url))) { + if ((j = get_host_name(prev_url))) { + if (!casestrcmp(h, j)) + brk = 0; + else if (!casestrcmp(h, cast_uchar "imageproxy.jxs.cz")) { + int l = strlen((char *)j); + int q = strlen(".blog.cz"); + if (l > q && !casestrcmp((j + l - q), cast_uchar ".blog.cz")) + brk = 0; + else if (!casestrcmp(j, cast_uchar "blog.cz")) + brk = 0; + } else if (!casestrcmp(h, cast_uchar "www.google.com")) { + unsigned char *c = get_url_data(url); + if (c && !strncmp((char *)c, "recaptcha/api/", 14)) + brk = 0; } - free(h); + free(j); } - if (brk) break; + free(h); } - /*-fallthrough*/ - case REFERER_REAL: { - unsigned char *ref; - unsigned char *user, *ins; - int ulen; - if (!prev_url) break; /* no referrer */ - - ref = stracpy(prev_url); - if (!parse_url(ref, NULL, &user, &ulen, NULL, NULL, &ins, NULL, NULL, NULL, NULL, NULL, NULL) && ulen && ins) { - memmove(user, ins, strlen(cast_const_char ins) + 1); - } - add_to_str(hdr, l, cast_uchar "Referer: "); - add_url_to_str(hdr, l, ref); - add_to_str(hdr, l, cast_uchar "\r\n"); - free(ref); + if (brk) break; - } + /*fallthrough*/ + } + case REFERER_REAL: { + unsigned char *ref; + unsigned char *user, *ins; + int ulen; + if (!prev_url) + break; + + ref = stracpy(prev_url); + if (!parse_url(ref, NULL, &user, &ulen, NULL, NULL, &ins, NULL, + NULL, NULL, NULL, NULL, NULL) + && ulen && ins) + memmove(user, ins, strlen(cast_const_char ins) + 1); + add_to_str(hdr, l, cast_uchar "Referer: "); + add_url_to_str(hdr, l, ref); + add_to_str(hdr, l, cast_uchar "\r\n"); + free(ref); + break; + } } } static void add_accept(unsigned char **hdr, int *l) { - if (SCRUB_HEADERS) { + if (SCRUB_HEADERS) add_to_str(hdr, l, cast_uchar "Accept: text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8\r\n"); - } else { + else add_to_str(hdr, l, cast_uchar "Accept: */*\r\n"); - } } static void add_accept_language(unsigned char **hdr, int *l, struct http_connection_info *info) { if (!(info->bl_flags & BL_NO_ACCEPT_LANGUAGE)) { add_to_str(hdr, l, cast_uchar "Accept-Language: "); - if (SCRUB_HEADERS) { + if (SCRUB_HEADERS) add_to_str(hdr, l, cast_uchar "en-US,en;q=0.5\r\n"); - } else { + else { int la; la = *l; add_to_str(hdr, l, get_text_translation(TEXT_(T__ACCEPT_LANGUAGE), NULL)); add_to_str(hdr, l, cast_uchar ","); - if (!strstr(cast_const_char(*hdr + la), "en,") && - !strstr(cast_const_char(*hdr + la), "en;")) add_to_str(hdr, l, cast_uchar "en;q=0.2,"); + if (!strstr((char *)(*hdr + la), "en,") + && !strstr((char *)(*hdr + la), "en;")) + add_to_str(hdr, l, cast_uchar "en;q=0.2,"); add_to_str(hdr, l, cast_uchar "*;q=0.1\r\n"); } } @@ -483,16 +512,17 @@ static int advertise_compression(unsigned char *url, struct connection *c) { struct http_connection_info *info = c->info; unsigned char *extd; - if (c->no_compress || http_options.no_compression || info->bl_flags & BL_NO_COMPRESSION) + if (c->no_compress || http_options.no_compression + || info->bl_flags & BL_NO_COMPRESSION) return 0; /* Fix for bugzilla. The attachment may be compressed and if the server compresses it again, we can't decompress the inner compression */ - if (strstr(cast_const_char url, "/attachment.cgi?")) + if (strstr((char *)url, "/attachment.cgi?")) return 0; extd = cast_uchar strrchr(cast_const_char url, '.'); - if (extd && get_compress_by_extension(extd + 1, cast_uchar strchr(cast_const_char(extd + 1), 0))) + if (extd && get_compress_by_extension(extd + 1, cast_uchar strchr((char *)(extd + 1), 0))) return 0; return 1; } @@ -505,10 +535,13 @@ static void add_accept_encoding(unsigned char **hdr, int *l, unsigned char *url, int l1; add_to_str(hdr, l, cast_uchar "Accept-Encoding: "); l1 = *l; - if (*l != l1) add_to_str(hdr, l, cast_uchar ", "); + if (*l != l1) + add_to_str(hdr, l, cast_uchar ", "); add_to_str(hdr, l, cast_uchar "gzip, deflate"); - if (*l != l1) add_to_str(hdr, l, cast_uchar "\r\n"); - else *l = orig_l; + if (*l != l1) + add_to_str(hdr, l, cast_uchar "\r\n"); + else + *l = orig_l; } #undef info } @@ -517,9 +550,9 @@ static void add_accept_charset(unsigned char **hdr, int *l, struct http_connecti { static unsigned char *accept_charset = NULL; - if (SCRUB_HEADERS || - info->bl_flags & BL_NO_CHARSET || - http_options.no_accept_charset) + if (SCRUB_HEADERS + || info->bl_flags & BL_NO_CHARSET + || http_options.no_accept_charset) return; if (!accept_charset) { @@ -528,12 +561,15 @@ static void add_accept_charset(unsigned char **hdr, int *l, struct http_connecti int aclen = 0; ac = init_str(); for (i = 0; (cs = get_cp_mime_name(i)); i++) { - if (aclen) add_to_str(&ac, &aclen, cast_uchar ","); - else add_to_str(&ac, &aclen, cast_uchar "Accept-Charset: "); + if (aclen) + add_to_str(&ac, &aclen, cast_uchar ","); + else + add_to_str(&ac, &aclen, cast_uchar "Accept-Charset: "); add_to_str(&ac, &aclen, cs); } - if (aclen) add_to_str(&ac, &aclen, cast_uchar "\r\n"); - if (!(accept_charset = cast_uchar strdup(cast_const_char ac))) { + if (aclen) + add_to_str(&ac, &aclen, cast_uchar "\r\n"); + if (!(accept_charset = cast_uchar strdup((char *)ac))) { add_to_str(hdr, l, ac); free(ac); return; @@ -546,18 +582,21 @@ static void add_accept_charset(unsigned char **hdr, int *l, struct http_connecti static void add_connection(unsigned char **hdr, int *l, int http10, int proxy, int alive) { if (!http10) { - if (!proxy) add_to_str(hdr, l, cast_uchar "Connection: "); - else add_to_str(hdr, l, cast_uchar "Proxy-Connection: "); - if (alive) add_to_str(hdr, l, cast_uchar "keep-alive\r\n"); - else add_to_str(hdr, l, cast_uchar "close\r\n"); + if (!proxy) + add_to_str(hdr, l, cast_uchar "Connection: "); + else + add_to_str(hdr, l, cast_uchar "Proxy-Connection: "); + if (alive) + add_to_str(hdr, l, cast_uchar "keep-alive\r\n"); + else + add_to_str(hdr, l, cast_uchar "close\r\n"); } } static void add_upgrade(unsigned char **hdr, int *l) { - if (proxies.only_proxies) { + if (proxies.only_proxies) add_to_str(hdr, l, cast_uchar "Upgrade-Insecure-Requests: 1\r\n"); - } } static void add_if_modified(unsigned char **hdr, int *l, struct connection *c) @@ -565,21 +604,24 @@ static void add_if_modified(unsigned char **hdr, int *l, struct connection *c) struct cache_entry *e; if ((e = c->cache)) { int code = 0; /* against warning */ - if (get_http_code(e->head, &code, NULL) || code >= 400) goto skip_ifmod; + if (get_http_code(e->head, &code, NULL) || code >= 400) + goto skip_ifmod; if (!e->incomplete && e->head && c->no_cache <= NC_IF_MOD) { unsigned char *m; - if (e->last_modified) m = stracpy(e->last_modified); + if (e->last_modified) + m = stracpy(e->last_modified); else if ((m = parse_http_header(e->head, cast_uchar "Date", NULL))) ; else if ((m = parse_http_header(e->head, cast_uchar "Expires", NULL))) ; - else goto skip_ifmod; + else + goto skip_ifmod; add_to_str(hdr, l, cast_uchar "If-Modified-Since: "); add_to_str(hdr, l, m); add_to_str(hdr, l, cast_uchar "\r\n"); free(m); } - skip_ifmod:; +skip_ifmod:; } } @@ -592,7 +634,7 @@ static void add_range(unsigned char **hdr, int *l, unsigned char *url, struct co if (!get_http_code(e->head, &code, NULL) && code >= 300) return; } - if (c->from /*&& (c->est_length == -1 || c->from < c->est_length)*/ && c->no_cache < NC_IF_MOD && !(info->bl_flags & BL_NO_RANGE)) { + if (c->from && c->no_cache < NC_IF_MOD && !(info->bl_flags & BL_NO_RANGE)) { add_to_str(hdr, l, cast_uchar "Range: bytes="); add_num_to_str(hdr, l, c->from); add_to_str(hdr, l, cast_uchar "-\r\n"); @@ -601,7 +643,8 @@ static void add_range(unsigned char **hdr, int *l, unsigned char *url, struct co static void add_pragma_no_cache(unsigned char **hdr, int *l, int no_cache) { - if (no_cache >= NC_PR_NO_CACHE) add_to_str(hdr, l, cast_uchar "Pragma: no-cache\r\nCache-Control: no-cache\r\n"); + if (no_cache >= NC_PR_NO_CACHE) + add_to_str(hdr, l, cast_uchar "Pragma: no-cache\r\nCache-Control: no-cache\r\n"); } static void add_proxy_auth_string(unsigned char **hdr, int *l, unsigned char *url) @@ -625,7 +668,7 @@ static void add_auth_string(unsigned char **hdr, int *l, unsigned char *url) static void add_post_header(unsigned char **hdr, int *l, unsigned char **post) { if (*post) { - unsigned char *pd = cast_uchar strchr(cast_const_char *post, '\n'); + unsigned char *pd = cast_uchar strchr((char *)*post, '\n'); if (pd) { add_to_str(hdr, l, cast_uchar "Content-Type: "); add_bytes_to_str(hdr, l, *post, pd - *post); @@ -633,7 +676,7 @@ static void add_post_header(unsigned char **hdr, int *l, unsigned char **post) *post = pd + 1; } add_to_str(hdr, l, cast_uchar "Content-Length: "); - add_num_to_str(hdr, l, strlen(cast_const_char *post) / 2); + add_num_to_str(hdr, l, strlen((char *)*post) / 2); add_to_str(hdr, l, cast_uchar "\r\n"); } } @@ -642,13 +685,13 @@ static void add_extra_options(unsigned char **hdr, int *l) { unsigned char *p = http_options.header.extra_header; while (1) { - unsigned char *q = p + strcspn(cast_const_char p, "\\"); + unsigned char *q = p + strcspn((char *)p, "\\"); if (p != q) { unsigned char *c; unsigned char *s = memacpy(p, q - p); - c = cast_uchar strchr(cast_const_char s, ':'); + c = cast_uchar strchr((char *)s, ':'); if (c && casecmp(s, cast_uchar "Cookie:", 7)) { - unsigned char *v = NULL; /* against warning */ + unsigned char *v = NULL; unsigned char *cc = memacpy(s, c - s); unsigned char *x = parse_http_header(*hdr, cc, &v); free(cc); @@ -662,7 +705,7 @@ static void add_extra_options(unsigned char **hdr, int *l) while (*++c == ' ') ; add_to_str(&new_hdr, &new_l, c); - add_to_str(&new_hdr, &new_l, v + strcspn(cast_const_char v, "\r\n")); + add_to_str(&new_hdr, &new_l, v + strcspn((char *)v, "\r\n")); free(*hdr); *hdr = new_hdr; *l = new_l; @@ -671,10 +714,11 @@ static void add_extra_options(unsigned char **hdr, int *l) } add_to_str(hdr, l, s); add_to_str(hdr, l, cast_uchar "\r\n"); - already_added: +already_added: free(s); } - if (!*q) break; + if (!*q) + break; p = q + 1; } } @@ -683,10 +727,14 @@ static int is_line_in_buffer(struct read_buffer *rb) { int l; for (l = 0; l < rb->len; l++) { - if (rb->data[l] == 10) return l + 1; - if (l < rb->len - 1 && rb->data[l] == 13 && rb->data[l + 1] == 10) return l + 2; - if (l == rb->len - 1 && rb->data[l] == 13) return 0; - if (rb->data[l] < ' ') return -1; + if (rb->data[l] == 10) + return l + 1; + if (l < rb->len - 1 && rb->data[l] == 13 && rb->data[l + 1] == 10) + return l + 2; + if (l == rb->len - 1 && rb->data[l] == 13) + return 0; + if (rb->data[l] < ' ') + return -1; } return 0; } @@ -702,7 +750,8 @@ static void read_http_data(struct connection *c, struct read_buffer *rb) } if (info->length != -2) { int l = rb->len; - if (info->length >= 0 && info->length < l) l = (int)info->length; + if (info->length >= 0 && info->length < l) + l = (int)info->length; if ((off_t)(0UL + c->from + l) < 0) { setcstate(c, S_LARGE_FILE); abort_connection(c); @@ -715,8 +764,10 @@ static void read_http_data(struct connection *c, struct read_buffer *rb) abort_connection(c); return; } - if (a == 1) c->tries = 0; - if (info->length >= 0) info->length -= l; + if (a == 1) + c->tries = 0; + if (info->length >= 0) + info->length -= l; c->from += l; kill_buffer_data(rb, l); if (!info->length && !rb->close) { @@ -724,7 +775,7 @@ static void read_http_data(struct connection *c, struct read_buffer *rb) return; } } else { - next_chunk: +next_chunk: if (info->chunk_remaining == -2) { int l; if ((l = is_line_in_buffer(rb))) { @@ -744,15 +795,17 @@ static void read_http_data(struct connection *c, struct read_buffer *rb) int l; if ((l = is_line_in_buffer(rb))) { unsigned char *de; - long n = 0; /* warning, go away */ - if (l != -1) n = strtol(cast_const_char rb->data, (char **)(void *)&de, 16); + long n = 0; + if (l != -1) + n = strtol((char *)rb->data, (char **)(void *)&de, 16); if (l == -1 || n < 0 || n >= MAXINT || de == rb->data) { setcstate(c, S_HTTP_ERROR); abort_connection(c); return; } kill_buffer_data(rb, l); - if (!(info->chunk_remaining = (int)n)) info->chunk_remaining = -2; + if (!(info->chunk_remaining = (int)n)) + info->chunk_remaining = -2; goto next_chunk; } } else { @@ -770,19 +823,23 @@ static void read_http_data(struct connection *c, struct read_buffer *rb) abort_connection(c); return; } - if (a == 1) c->tries = 0; + if (a == 1) + c->tries = 0; info->chunk_remaining -= l; c->from += l; kill_buffer_data(rb, l); if (!info->chunk_remaining && rb->len >= 1) { - if (rb->data[0] == 10) kill_buffer_data(rb, 1); + if (rb->data[0] == 10) + kill_buffer_data(rb, 1); else { - if (rb->data[0] != 13 || (rb->len >= 2 && ((unsigned char *)rb->data)[1] != 10)) { + if (rb->data[0] != 13 || (rb->len >= 2 + && ((unsigned char *)rb->data)[1] != 10)) { setcstate(c, S_HTTP_ERROR); abort_connection(c); return; } - if (rb->len < 2) goto read_more; + if (rb->len < 2) + goto read_more; kill_buffer_data(rb, 2); } info->chunk_remaining = -1; @@ -798,22 +855,34 @@ static void read_http_data(struct connection *c, struct read_buffer *rb) static int get_header(struct read_buffer *rb) { int i; - if (rb->len <= 0) return 0; - if (rb->data[0] != 'H') return -2; - if (rb->len <= 1) return 0; - if (((unsigned char *)rb->data)[1] != 'T') return -2; - if (rb->len <= 2) return 0; - if (((unsigned char *)rb->data)[2] != 'T') return -2; - if (rb->len <= 3) return 0; - if (((unsigned char *)rb->data)[3] != 'P') return -2; + if (rb->len <= 0) + return 0; + if (rb->data[0] != 'H') + return -2; + if (rb->len <= 1) + return 0; + if (((unsigned char *)rb->data)[1] != 'T') + return -2; + if (rb->len <= 2) + return 0; + if (((unsigned char *)rb->data)[2] != 'T') + return -2; + if (rb->len <= 3) + return 0; + if (((unsigned char *)rb->data)[3] != 'P') + return -2; for (i = 0; i < rb->len; i++) { unsigned char a = rb->data[i]; - if (/*a < ' ' && a != 10 && a != 13*/ !a) return -1; - if (i < rb->len - 1 && a == 10 && rb->data[i + 1] == 10) return i + 2; + if (!a) + return -1; + if (i < rb->len - 1 && a == 10 && rb->data[i + 1] == 10) + return i + 2; if (i < rb->len - 3 && a == 13) { - if (rb->data[i + 1] != 10) return -1; + if (rb->data[i + 1] != 10) + return -1; if (rb->data[i + 2] == 13) { - if (rb->data[i + 3] != 10) return -1; + if (rb->data[i + 3] != 10) + return -1; return i + 4; } } @@ -826,7 +895,7 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) off_t cf; int state = c->state != S_PROC ? S_GETH : S_PROC; unsigned char *head; - int a, h = 0, version = 0; /* against warning */ + int a, h = 0, version = 0; unsigned char *d; struct cache_entry *e; int previous_http_code; @@ -837,9 +906,9 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) if (rb->close == 2) { unsigned char *hs; if (!c->tries && (hs = get_host_name(host))) { - if (info->bl_flags & BL_NO_CHARSET) { + if (info->bl_flags & BL_NO_CHARSET) del_blacklist_entry(hs, BL_NO_CHARSET); - } else { + else { add_blacklist_entry(hs, BL_NO_CHARSET); c->tries = -1; } @@ -850,7 +919,7 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) return; } rb->close = 0; - again: +again: if ((a = get_header(rb)) == -1) { setcstate(c, S_HTTP_ERROR); abort_connection(c); @@ -864,9 +933,8 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) if (a != -2) { head = memacpy(rb->data, a); kill_buffer_data(rb, a); - } else { + } else head = stracpy(cast_uchar "HTTP/0.9 200 OK\r\nContent-Type: text/html\r\n\r\n"); - } if (get_http_code(head, &h, &version) || h == 101) { free(head); setcstate(c, S_HTTP_ERROR); @@ -940,8 +1008,9 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) return; } } - if ((h == 500 || h == 502 || h == 503 || h == 504) && http_options.retry_internal_errors && is_connection_restartable(c)) { - /* !!! FIXME: wait some time ... */ + if ((h == 500 || h == 502 || h == 503 || h == 504) + && http_options.retry_internal_errors && is_connection_restartable(c)) { + /* !!! FIXME: wait some time ... */ if (is_last_try(c)) { unsigned char *h; if ((h = get_host_name(host))) { @@ -981,18 +1050,20 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) unsigned char *f = d; while (1) { while (*f && (*f == ' ' || *f == ',')) f++; - if (!*f) break; - if (!casecmp(f, cast_uchar "no-cache", 8) || !casecmp(f, cast_uchar "must-revalidate", 15)) { + if (!*f) + break; + if (!casecmp(f, cast_uchar "no-cache", 8) + || !casecmp(f, cast_uchar "must-revalidate", 15)) e->expire_time = 1; - } if (!casecmp(f, cast_uchar "max-age=", 8)) { if (e->expire_time != 1) { errno = 0; EINTRLOOPX(e->expire_time, time(NULL), (time_t)-1); - e->expire_time += atoi(cast_const_char(f + 8)); + e->expire_time += atoi((char *)(f + 8)); } } - while (*f && *f != ',') f++; + while (*f && *f != ',') + f++; } free(d); } @@ -1002,23 +1073,27 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) } free(e->redirect); e->redirect = NULL; - if ((h == 302 || h == 303 || h == 307 || h == 511) && !e->expire_time) e->expire_time = 1; + if ((h == 302 || h == 303 || h == 307 || h == 511) && !e->expire_time) + e->expire_time = 1; if (h == 301 || h == 302 || h == 303 || h == 307) { if ((d = parse_http_header(e->head, cast_uchar "Location", NULL))) { unsigned char *user, *ins; unsigned char *newuser, *newpassword; - if (!parse_url(d, NULL, &user, NULL, NULL, NULL, &ins, NULL, NULL, NULL, NULL, NULL, NULL) && !user && ins && (newuser = get_user_name(host))) { + if (!parse_url(d, NULL, &user, NULL, NULL, NULL, &ins, + NULL, NULL, NULL, NULL, NULL, NULL) + && !user && ins && (newuser = get_user_name(host))) { if (*newuser) { int ins_off = (int)(ins - d); newpassword = get_pass(host); - if (!newpassword) newpassword = stracpy(cast_uchar ""); + if (!newpassword) + newpassword = stracpy(cast_uchar ""); add_to_strn(&newuser, cast_uchar ":"); add_to_strn(&newuser, newpassword); add_to_strn(&newuser, cast_uchar "@"); - extend_str(&d, (int)strlen(cast_const_char newuser)); + extend_str(&d, strlen((char *)newuser)); ins = d + ins_off; - memmove(ins + strlen(cast_const_char newuser), ins, strlen(cast_const_char ins) + 1); - memcpy(ins, newuser, strlen(cast_const_char newuser)); + memmove(ins + strlen((char *)newuser), ins, strlen((char *)ins) + 1); + memcpy(ins, newuser, strlen((char *)newuser)); free(newpassword); } free(newuser); @@ -1028,22 +1103,27 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) e->redirect_get = h == 303; } } - if (!e->expire_time && strchr(cast_const_char c->url, POST_CHAR)) e->expire_time = 1; + if (!e->expire_time && strchr((char *)c->url, POST_CHAR)) + e->expire_time = 1; info->close = 0; info->length = -1; info->version = version; - if ((d = parse_http_header(e->head, cast_uchar "Connection", NULL)) || (d = parse_http_header(e->head, cast_uchar "Proxy-Connection", NULL))) { - if (!casestrcmp(d, cast_uchar "close")) info->close = 1; + if ((d = parse_http_header(e->head, cast_uchar "Connection", NULL)) + || (d = parse_http_header(e->head, cast_uchar "Proxy-Connection", NULL))) { + if (!casestrcmp(d, cast_uchar "close")) + info->close = 1; free(d); - } else if (version < 11) info->close = 1; + } else if (version < 11) + info->close = 1; cf = c->from; c->from = 0; if ((d = parse_http_header(e->head, cast_uchar "Content-Range", NULL))) { - if (strlen(cast_const_char d) > 6) { + if (strlen((char *)d) > 6) { d[5] = 0; if (!(casestrcmp(d, cast_uchar "bytes")) && d[6] >= '0' && d[6] <= '9') { my_strtoll_t f = my_strtoll(d + 6, NULL); - if (f >= 0 && (off_t)f >= 0 && (off_t)f == f) c->from = f; + if (f >= 0 && (off_t)f >= 0 && (off_t)f == f) + c->from = f; } } free(d); @@ -1051,7 +1131,8 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) /* Hmm ... some servers send 206 partial but don't send Content-Range */ c->from = cf; } - if (cf && !c->from && !c->unrestartable) c->unrestartable = 1; + if (cf && !c->from && !c->unrestartable) + c->unrestartable = 1; if (c->from > cf || c->from < 0) { setcstate(c, S_HTTP_ERROR); abort_connection(c); @@ -1059,20 +1140,23 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) } if ((d = parse_http_header(e->head, cast_uchar "Content-Length", NULL))) { unsigned char *ep; - my_strtoll_t l = my_strtoll(d, &ep); + long l = strtol((char *)d, &ep, 10); if (!*ep && l >= 0 && (off_t)l >= 0 && (off_t)l == l) { - if (!info->close || version >= 11) info->length = l; - if (c->from + l >= 0) c->est_length = c->from + l; + if (!info->close || version >= 11) + info->length = l; + if (c->from + l >= 0) + c->est_length = c->from + l; } free(d); } if ((d = parse_http_header(e->head, cast_uchar "Accept-Ranges", NULL))) { - if (!casestrcmp(d, cast_uchar "none") && !c->unrestartable) c->unrestartable = 1; + if (!casestrcmp(d, cast_uchar "none") && !c->unrestartable) + c->unrestartable = 1; free(d); - } else { - if (!c->unrestartable && !c->from) c->unrestartable = 1; - } - if (info->bl_flags & BL_NO_RANGE && !c->unrestartable) c->unrestartable = 1; + } else if (!c->unrestartable && !c->from) + c->unrestartable = 1; + if (info->bl_flags & BL_NO_RANGE && !c->unrestartable) + c->unrestartable = 1; if ((d = parse_http_header(e->head, cast_uchar "Transfer-Encoding", NULL))) { if (!casestrcmp(d, cast_uchar "chunked")) { info->length = -2; @@ -1080,7 +1164,8 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) } free(d); } - if (!info->close && info->length == -1) info->close = 1; + if (!info->close && info->length == -1) + info->close = 1; if ((d = parse_http_header(e->head, cast_uchar "Last-Modified", NULL))) { if (e->last_modified && casestrcmp(e->last_modified, d)) { delete_entry_content(e); @@ -1092,13 +1177,16 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) return; } } - if (!e->last_modified) e->last_modified = d; + if (!e->last_modified) + e->last_modified = d; else free(d); } - if (!e->last_modified && (d = parse_http_header(e->head, cast_uchar "Date", NULL))) + if (!e->last_modified + && (d = parse_http_header(e->head, cast_uchar "Date", NULL))) e->last_modified = d; - if (info->length == -1 || (version < 11 && info->close)) rb->close = 1; + if (info->length == -1 || (version < 11 && info->close)) + rb->close = 1; /* @@ -1111,9 +1199,8 @@ static void http_got_header(struct connection *c, struct read_buffer *rb) if ((d = parse_http_header(e->head, cast_uchar "Content-Encoding", NULL))) { free(d); truncate_entry(e, c->from, 0); - } else if (previous_http_code == 401 || previous_http_code == 407) { + } else if (previous_http_code == 401 || previous_http_code == 407) truncate_entry(e, c->from, 0); - } if (info->https_forward && h == 407) { http_end_request(c, 0, 1, S__OK); @@ -1127,7 +1214,8 @@ static void http_get_header(struct connection *c) { struct read_buffer *rb; set_connection_timeout(c); - if (!(rb = alloc_read_buffer(c))) return; + if (!(rb = alloc_read_buffer(c))) + return; rb->close = 1; read_from_socket(c, c->sock1, rb, http_got_header); }