|
|
@@ -574,7 +574,8 @@ static unsigned int req_app_dec(char *buf, unsigned int pos, unsigned int cap,
|
|
|
|
|
|
/* Read the status line and headers of the current response. Returns 0 and
|
|
|
* sets redirect/location when the response is a redirect with a target. */
|
|
|
-static int read_response_head(struct uhttp *h, char *location, int *have_loc)
|
|
|
+static int read_response_head(struct uhttp *h, char *location, int *have_loc,
|
|
|
+ unsigned int timeout_ms)
|
|
|
{
|
|
|
char line[UHTTP_LINE_MAX];
|
|
|
unsigned int status = 0;
|
|
|
@@ -597,7 +598,7 @@ static int read_response_head(struct uhttp *h, char *location, int *have_loc)
|
|
|
*have_loc = 0;
|
|
|
location[0] = '\0';
|
|
|
|
|
|
- rv = reader_readline(h, line, sizeof(line), UHTTP_IDLE_TIMEOUT_MS);
|
|
|
+ rv = reader_readline(h, line, sizeof(line), timeout_ms);
|
|
|
if (rv < 0)
|
|
|
return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
|
|
|
UHTTP_ERR_TIMEOUT;
|
|
|
@@ -605,9 +606,17 @@ static int read_response_head(struct uhttp *h, char *location, int *have_loc)
|
|
|
h->resp.status = (int)status;
|
|
|
if (h->hooks && h->hooks->status)
|
|
|
h->hooks->status(h->hooks->ctx, (int)status, line);
|
|
|
+ /* HTTP/1.0 has no persistent connections unless the response
|
|
|
+ * explicitly opts in. Ignoring this kept a half-closed socket for
|
|
|
+ * the next request, which then waited for a response that could not
|
|
|
+ * arrive: the next batch of images appeared to hang. */
|
|
|
+ if (line[0] == 'H' && line[1] == 'T' && line[2] == 'T' &&
|
|
|
+ line[3] == 'P' && line[4] == '/' && line[5] == '1' &&
|
|
|
+ line[6] == '.' && line[7] == '0')
|
|
|
+ h->peer_close = 1;
|
|
|
|
|
|
for (;;) {
|
|
|
- rv = reader_readline(h, line, sizeof(line), UHTTP_IDLE_TIMEOUT_MS);
|
|
|
+ rv = reader_readline(h, line, sizeof(line), timeout_ms);
|
|
|
if (rv < 0)
|
|
|
return (h->error == UHTTP_ERR_RESET) ? UHTTP_ERR_RESET :
|
|
|
UHTTP_ERR_TIMEOUT;
|
|
|
@@ -633,9 +642,11 @@ static int read_response_head(struct uhttp *h, char *location, int *have_loc)
|
|
|
} else if (hdr_match(line, "location")) {
|
|
|
copy_after_colon(line, location, UHTTP_LOC_MAX);
|
|
|
*have_loc = location[0] != '\0';
|
|
|
- } else if (hdr_match(line, "connection") &&
|
|
|
- contains_ci(line, "close")) {
|
|
|
- h->peer_close = 1;
|
|
|
+ } else if (hdr_match(line, "connection")) {
|
|
|
+ if (contains_ci(line, "close"))
|
|
|
+ h->peer_close = 1;
|
|
|
+ else if (contains_ci(line, "keep-alive"))
|
|
|
+ h->peer_close = 0;
|
|
|
} else if (h->jar && hdr_match(line, "set-cookie")) {
|
|
|
char *v = line + 11; /* past "set-cookie:" */
|
|
|
|
|
|
@@ -808,7 +819,9 @@ int uhttp_open(struct uhttp *h, const char *url, unsigned int flags,
|
|
|
rv = send_request(h, flags);
|
|
|
if (rv == 0)
|
|
|
rv = read_response_head(h, location,
|
|
|
- &have_loc);
|
|
|
+ &have_loc,
|
|
|
+ used_reuse ? 3000 :
|
|
|
+ UHTTP_IDLE_TIMEOUT_MS);
|
|
|
if (rv == 0)
|
|
|
break;
|
|
|
if (!used_reuse)
|