struct link *http_query_size_via_proxy(const char *proxy, const char *urlin, const char *action, INT64_T * size, time_t stoptime, int cache_reload) { char url[HTTP_LINE_MAX]; char newurl[HTTP_LINE_MAX]; char line[HTTP_LINE_MAX]; char addr[LINK_ADDRESS_MAX]; struct link *link; int save_errno; int response; char actual_host[HTTP_LINE_MAX]; int actual_port; *size = 0; url_encode(urlin, url, sizeof(url)); if(proxy && !strcmp(proxy, "DIRECT")) proxy = 0; if(proxy) { int fields = sscanf(proxy, "http://%[^:]:%d", actual_host, &actual_port); if(fields == 2) { /* host and port are good */ } else if(fields == 1) { actual_port = HTTP_PORT; } else { debug(D_HTTP, "invalid proxy syntax: %s", proxy); return 0; } } else { int fields = sscanf(url, "http://%[^:]:%d", actual_host, &actual_port); if(fields != 2) { fields = sscanf(url, "http://%[^/]", actual_host); if(fields == 1) { actual_port = HTTP_PORT; } else { debug(D_HTTP, "malformed url: %s", url); return 0; } } } debug(D_HTTP, "connect %s port %d", actual_host, actual_port); if(!domain_name_lookup(actual_host, addr)) return 0; link = link_connect(addr, actual_port, stoptime); if(!link) { errno = ECONNRESET; return 0; } { buffer_t B; buffer_init(&B); buffer_abortonfailure(&B, 1); buffer_printf(&B, "%s %s HTTP/1.1\r\n", action, url); if(cache_reload) buffer_putliteral(&B, "Cache-Control: max-age=0\r\n"); buffer_putliteral(&B, "Connection: close\r\n"); buffer_printf(&B, "Host: %s\r\n", actual_host); if(getenv("HTTP_USER_AGENT")) buffer_printf(&B, "User-Agent: Mozilla/5.0 (compatible; CCTools %d.%d.%s Parrot; http://www.nd.edu/~ccl/ %s)\r\n", CCTOOLS_VERSION_MAJOR, CCTOOLS_VERSION_MINOR, CCTOOLS_VERSION_MICRO, getenv("HTTP_USER_AGENT")); else buffer_printf(&B, "User-Agent: Mozilla/5.0 (compatible; CCTools %d.%d.%s Parrot; http://www.nd.edu/~ccl/)\r\n", CCTOOLS_VERSION_MAJOR, CCTOOLS_VERSION_MINOR, CCTOOLS_VERSION_MICRO); buffer_putliteral(&B, "\r\n"); /* header terminator */ debug(D_HTTP, "%s", buffer_tostring(&B, NULL)); link_putstring(link, buffer_tostring(&B, NULL), stoptime); buffer_free(&B); } if(link_readline(link, line, HTTP_LINE_MAX, stoptime)) { string_chomp(line); debug(D_HTTP, "%s", line); if(sscanf(line, "HTTP/%*d.%*d %d", &response) == 1) { newurl[0] = 0; while(link_readline(link, line, HTTP_LINE_MAX, stoptime)) { string_chomp(line); debug(D_HTTP, "%s", line); sscanf(line, "Location: %s", newurl); sscanf(line, "Content-Length: %" SCNd64, size); if(strlen(line) <= 2) { break; } } switch (response) { case 200: return link; break; case 301: case 302: case 303: case 307: link_close(link); if(newurl[0]) { if(!strcmp(url, newurl)) { debug(D_HTTP, "error: server gave %d redirect from %s back to the same url!", response, url); errno = EIO; return 0; } else { return http_query_size_via_proxy(proxy,newurl,action,size,stoptime,cache_reload); } } else { errno = ENOENT; return 0; } break; default: link_close(link); errno = http_response_to_errno(response); return 0; break; } } else { debug(D_HTTP, "malformed response"); save_errno = ECONNRESET; } } else { debug(D_HTTP, "malformed response"); save_errno = ECONNRESET; } link_close(link); errno = save_errno; return 0; }
struct link *http_query_size_via_proxy(const char *proxy, const char *urlin, const char *action, INT64_T * size, time_t stoptime, int cache_reload) { char url[HTTP_LINE_MAX]; char newurl[HTTP_LINE_MAX]; char line[HTTP_LINE_MAX]; char addr[LINK_ADDRESS_MAX]; struct link *link; int save_errno; int response; char actual_host[HTTP_LINE_MAX]; int actual_port; *size = 0; url_encode(urlin, url, sizeof(url)); if(proxy && !strcmp(proxy, "DIRECT")) proxy = 0; if(proxy) { int fields = sscanf(proxy, "http://%[^:]:%d", actual_host, &actual_port); if(fields == 2) { /* host and port are good */ } else if(fields == 1) { actual_port = HTTP_PORT; } else { debug(D_HTTP, "invalid proxy syntax: %s", proxy); return 0; } } else { int fields = sscanf(url, "http://%[^:]:%d", actual_host, &actual_port); if(fields != 2) { fields = sscanf(url, "http://%[^/]", actual_host); if(fields == 1) { actual_port = HTTP_PORT; } else { debug(D_HTTP, "malformed url: %s", url); return 0; } } } debug(D_HTTP, "connect %s port %d", actual_host, actual_port); if(!domain_name_lookup(actual_host, addr)) return 0; link = link_connect(addr, actual_port, stoptime); if(!link) { errno = ECONNRESET; return 0; } if(cache_reload == 0) { debug(D_HTTP, "%s %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", action, url, actual_host); link_putfstring(link, "%s %s HTTP/1.0\r\nHost: %s\r\nConnection: close\r\n\r\n", stoptime, action, url, actual_host); } else { // force refresh of cache end-to-end (RFC 2616) debug(D_HTTP, "%s %s HTTP/1.1\r\nHost: %s\r\nCache-Control: max-age=0\r\nConnection: close\r\n\r\n", action, url, actual_host); link_putfstring(link, "%s %s HTTP/1.1\r\nHost: %s\r\nCache-Control: max-age=0\r\nConnection: close\r\n\r\n", stoptime, action, url, actual_host); } if(link_readline(link, line, HTTP_LINE_MAX, stoptime)) { string_chomp(line); debug(D_HTTP, "%s", line); if(sscanf(line, "HTTP/%*d.%*d %d", &response) == 1) { newurl[0] = 0; while(link_readline(link, line, HTTP_LINE_MAX, stoptime)) { string_chomp(line); debug(D_HTTP, "%s", line); sscanf(line, "Location: %s", newurl); sscanf(line, "Content-Length: %lld", size); if(strlen(line) <= 2) { break; } } switch (response) { case 200: return link; break; case 301: case 302: case 303: case 307: link_close(link); if(newurl[0]) { if(!strcmp(url, newurl)) { debug(D_HTTP, "error: server gave %d redirect from %s back to the same url!", response, url); errno = EIO; return 0; } else { return http_query(newurl, action, stoptime); } } else { errno = ENOENT; return 0; } break; default: link_close(link); errno = http_response_to_errno(response); return 0; break; } } else { debug(D_HTTP, "malformed response"); save_errno = ECONNRESET; } } else { debug(D_HTTP, "malformed response"); save_errno = ECONNRESET; } link_close(link); errno = save_errno; return 0; }