Fix reverse proxy error

This commit is contained in:
2023-01-03 14:52:36 +01:00
parent ec3183e99b
commit f8d3253a9f
3 changed files with 21 additions and 19 deletions

View File

@ -217,13 +217,14 @@ int proxy_response_header(http_req *req, http_res *res, host_config_t *conf) {
return 0;
}
proxy_ctx_t *proxy_init(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];
const char *connection, *upgrade, *ws_version;
long ret;
int tries = 0, retry = 0;
proxy_ctx_t *proxy = proxy_get_by_conf(conf);
*proxy_ptr = proxy_get_by_conf(conf);
proxy_ctx_t *proxy = *proxy_ptr;
proxy->in_use = 1;
if (proxy->initialized && sock_has_pending(&proxy->proxy) == 0)
@ -243,7 +244,7 @@ proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host
error("Unable to create socket");
res->status = http_get_status(500);
ctx->origin = INTERNAL;
return NULL;
return -1;
}
if (sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT_INIT) != 0)
@ -331,7 +332,7 @@ proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host
} else {
res->status = http_get_status(501);
ctx->origin = INTERNAL;
return NULL;
return -1;
}
} else {
http_remove_header_field(&req->hdr, "Connection", HTTP_REMOVE_ALL);
@ -342,7 +343,7 @@ proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host
if (ret != 0) {
res->status = http_get_status(500);
ctx->origin = INTERNAL;
return NULL;
return -1;
}
ret = http_send_request(&proxy->proxy, req);
@ -379,12 +380,12 @@ proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host
ctx->origin = CLIENT_REQ;
error("Unable to receive request from client: %s", sock_strerror(client));
sprintf(err_msg, "Unable to receive request from client: %s.", sock_strerror(client));
return NULL;
return -1;
}
res->status = http_get_status(500);
ctx->origin = INTERNAL;
error("Unknown Error");
return NULL;
return -1;
}
ret = sock_recv(&proxy->proxy, buffer, sizeof(buffer), MSG_PEEK);
@ -480,15 +481,15 @@ proxy_ctx_t *proxy_init(http_req *req, http_res *res, http_status_ctx *ctx, host
if (ret != 0) {
res->status = http_get_status(500);
ctx->origin = INTERNAL;
return NULL;
return -1;
}
return proxy;
return 0;
proxy_err:
errno = 0;
if (retry) goto retry;
return NULL;
return -1;
}
int proxy_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags) {

View File

@ -35,7 +35,7 @@ int proxy_request_header(http_req *req, sock *sock);
int proxy_response_header(http_req *req, http_res *res, host_config_t *conf);
proxy_ctx_t *proxy_init(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, 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_send(proxy_ctx_t *proxy, sock *client, unsigned long len_to_send, int flags);

View File

@ -25,7 +25,14 @@ void proxy_handler_func(client_ctx_t *ctx) {
proxy_handler_1(ctx);
respond(ctx);
if (ctx->use_proxy == 2) {
if (ctx->use_proxy == 0) {
proxy_close(ctx->proxy);
request_complete(ctx);
handle_request(ctx);
} else if (ctx->use_proxy == 1) {
proxy_handler_2(ctx);
request_complete(ctx);
} else if (ctx->use_proxy == 2) {
// WebSocket
sock_set_timeout(&ctx->socket, WS_TIMEOUT);
sock_set_timeout(&ctx->proxy->proxy, WS_TIMEOUT);
@ -37,11 +44,6 @@ void proxy_handler_func(client_ctx_t *ctx) {
info("WebSocket connection closed");
return;
}
proxy_handler_2(ctx);
request_complete(ctx);
handle_request(ctx);
}
static int proxy_handler_1(client_ctx_t *ctx) {
@ -54,8 +56,7 @@ static int proxy_handler_1(client_ctx_t *ctx) {
http_remove_header_field(&res->hdr, "Date", HTTP_REMOVE_ALL);
http_remove_header_field(&res->hdr, "Server", HTTP_REMOVE_ALL);
ctx->proxy = proxy_init(&ctx->req, res, status, ctx->conf, &ctx->socket, &ctx->custom_status, ctx->err_msg);
ctx->use_proxy = (ctx->proxy != NULL);
ctx->use_proxy = proxy_init(&ctx->proxy, &ctx->req, res, status, ctx->conf, &ctx->socket, &ctx->custom_status, ctx->err_msg) == 0;
if (res->status->code == 101) {
const char *connection = http_get_header_field(&res->hdr, "Connection");