Fix proxy try-loop
This commit is contained in:
@ -362,7 +362,6 @@ static int proxy_connect(proxy_ctx_t *proxy, host_config_t *conf, http_res *res,
|
|||||||
int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, http_status *custom_status, char *err_msg) {
|
int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_status_ctx *ctx, host_config_t *conf, sock *client, http_status *custom_status, char *err_msg) {
|
||||||
char buffer[CHUNK_SIZE], err_buf[256];
|
char buffer[CHUNK_SIZE], err_buf[256];
|
||||||
long ret;
|
long ret;
|
||||||
int tries = 0, retry = 1, srv_error = 0;
|
|
||||||
|
|
||||||
*proxy_ptr = proxy_get_by_conf(conf);
|
*proxy_ptr = proxy_get_by_conf(conf);
|
||||||
proxy_ctx_t *proxy = *proxy_ptr;
|
proxy_ctx_t *proxy = *proxy_ptr;
|
||||||
@ -391,8 +390,10 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
return -1;
|
return -1;
|
||||||
}
|
}
|
||||||
|
|
||||||
while (retry) {
|
for (int retry = 1, srv_error = 0, tries = 0;; tries++) {
|
||||||
errno = 0;
|
errno = 0;
|
||||||
|
if (!retry)
|
||||||
|
return -1;
|
||||||
|
|
||||||
if (!proxy->initialized || sock_has_pending(&proxy->proxy, SOCK_DONTWAIT) != 0 || srv_error ||
|
if (!proxy->initialized || sock_has_pending(&proxy->proxy, SOCK_DONTWAIT) != 0 || srv_error ||
|
||||||
(proxy->http_timeout != 0 && (clock_micros() - proxy->proxy.ts_last) >= proxy->http_timeout))
|
(proxy->http_timeout != 0 && (clock_micros() - proxy->proxy.ts_last) >= proxy->http_timeout))
|
||||||
@ -419,6 +420,9 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
continue;
|
continue;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
break;
|
||||||
|
}
|
||||||
|
|
||||||
const char *content_length = http_get_header_field(&req->hdr, "Content-Length");
|
const char *content_length = http_get_header_field(&req->hdr, "Content-Length");
|
||||||
unsigned long content_len = content_length != NULL ? strtoul(content_length, NULL, 10) : 0;
|
unsigned long content_len = content_length != NULL ? strtoul(content_length, NULL, 10) : 0;
|
||||||
const char *transfer_encoding = http_get_header_field(&req->hdr, "Transfer-Encoding");
|
const char *transfer_encoding = http_get_header_field(&req->hdr, "Transfer-Encoding");
|
||||||
@ -474,7 +478,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
ctx->origin = SERVER_RES;
|
ctx->origin = SERVER_RES;
|
||||||
error("Unable to parse header: End of header not found");
|
error("Unable to parse header: End of header not found");
|
||||||
sprintf(err_msg, "Unable to parser header: End of header not found.");
|
sprintf(err_msg, "Unable to parser header: End of header not found.");
|
||||||
continue;
|
return -2;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < header_len; i++) {
|
for (int i = 0; i < header_len; i++) {
|
||||||
@ -483,7 +487,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
ctx->origin = SERVER_RES;
|
ctx->origin = SERVER_RES;
|
||||||
error("Unable to parse header: Header contains illegal characters");
|
error("Unable to parse header: Header contains illegal characters");
|
||||||
sprintf(err_msg, "Unable to parse header: Header contains illegal characters.");
|
sprintf(err_msg, "Unable to parse header: Header contains illegal characters.");
|
||||||
continue;
|
return -2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -495,7 +499,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
ctx->origin = SERVER_RES;
|
ctx->origin = SERVER_RES;
|
||||||
error("Unable to parse header: Invalid header format");
|
error("Unable to parse header: Invalid header format");
|
||||||
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
||||||
continue;
|
return -2;
|
||||||
}
|
}
|
||||||
if (ptr == buf) {
|
if (ptr == buf) {
|
||||||
if (!strstarts(ptr, "HTTP/")) {
|
if (!strstarts(ptr, "HTTP/")) {
|
||||||
@ -503,7 +507,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
ctx->origin = SERVER_RES;
|
ctx->origin = SERVER_RES;
|
||||||
error("Unable to parse header: Invalid header format");
|
error("Unable to parse header: Invalid header format");
|
||||||
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
||||||
continue;
|
return -2;
|
||||||
}
|
}
|
||||||
int status_code = (int) strtol(ptr + 9, NULL, 10);
|
int status_code = (int) strtol(ptr + 9, NULL, 10);
|
||||||
res->status = http_get_status(status_code);
|
res->status = http_get_status(status_code);
|
||||||
@ -518,7 +522,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
ctx->origin = SERVER_RES;
|
ctx->origin = SERVER_RES;
|
||||||
error("Unable to parse header: Invalid or unknown status code");
|
error("Unable to parse header: Invalid or unknown status code");
|
||||||
sprintf(err_msg, "Unable to parse header: Invalid or unknown status code.");
|
sprintf(err_msg, "Unable to parse header: Invalid or unknown status code.");
|
||||||
continue;
|
return -2;
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
if (http_parse_header_field(&res->hdr, ptr, pos0, 0) != 0) {
|
if (http_parse_header_field(&res->hdr, ptr, pos0, 0) != 0) {
|
||||||
@ -526,7 +530,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
ctx->origin = SERVER_RES;
|
ctx->origin = SERVER_RES;
|
||||||
error("Unable to parse header");
|
error("Unable to parse header");
|
||||||
sprintf(err_msg, "Unable to parse header.");
|
sprintf(err_msg, "Unable to parse header.");
|
||||||
continue;
|
return -2;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
if (pos0[2] == '\r' && pos0[3] == '\n') {
|
if (pos0[2] == '\r' && pos0[3] == '\n') {
|
||||||
@ -534,7 +538,8 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
}
|
}
|
||||||
ptr = pos0 + 2;
|
ptr = pos0 + 2;
|
||||||
}
|
}
|
||||||
sock_recv_x(&proxy->proxy, buffer, header_len, 0);
|
if (sock_recv_x(&proxy->proxy, buffer, header_len, 0) == -1)
|
||||||
|
return -1;
|
||||||
|
|
||||||
long keep_alive_timeout = http_get_keep_alive_timeout(&res->hdr);
|
long keep_alive_timeout = http_get_keep_alive_timeout(&res->hdr);
|
||||||
proxy->http_timeout = (keep_alive_timeout > 0) ? keep_alive_timeout * 1000000 : 0;
|
proxy->http_timeout = (keep_alive_timeout > 0) ? keep_alive_timeout * 1000000 : 0;
|
||||||
@ -549,9 +554,6 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
|||||||
return 0;
|
return 0;
|
||||||
}
|
}
|
||||||
|
|
||||||
return -1;
|
|
||||||
}
|
|
||||||
|
|
||||||
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags) {
|
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags) {
|
||||||
char buffer[CHUNK_SIZE];
|
char buffer[CHUNK_SIZE];
|
||||||
if (sock_splice(client, &proxy->proxy, buffer, sizeof(buffer), len_to_send) == -1)
|
if (sock_splice(client, &proxy->proxy, buffer, sizeof(buffer), len_to_send) == -1)
|
||||||
|
Reference in New Issue
Block a user