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; 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]; char buffer[CHUNK_SIZE];
const char *connection, *upgrade, *ws_version; const char *connection, *upgrade, *ws_version;
long ret; long ret;
int tries = 0, retry = 0; 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; proxy->in_use = 1;
if (proxy->initialized && sock_has_pending(&proxy->proxy) == 0) 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"); error("Unable to create socket");
res->status = http_get_status(500); res->status = http_get_status(500);
ctx->origin = INTERNAL; ctx->origin = INTERNAL;
return NULL; return -1;
} }
if (sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT_INIT) != 0) 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 { } else {
res->status = http_get_status(501); res->status = http_get_status(501);
ctx->origin = INTERNAL; ctx->origin = INTERNAL;
return NULL; return -1;
} }
} else { } else {
http_remove_header_field(&req->hdr, "Connection", HTTP_REMOVE_ALL); 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) { if (ret != 0) {
res->status = http_get_status(500); res->status = http_get_status(500);
ctx->origin = INTERNAL; ctx->origin = INTERNAL;
return NULL; return -1;
} }
ret = http_send_request(&proxy->proxy, req); 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; ctx->origin = CLIENT_REQ;
error("Unable to receive request from client: %s", sock_strerror(client)); error("Unable to receive request from client: %s", sock_strerror(client));
sprintf(err_msg, "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); res->status = http_get_status(500);
ctx->origin = INTERNAL; ctx->origin = INTERNAL;
error("Unknown Error"); error("Unknown Error");
return NULL; return -1;
} }
ret = sock_recv(&proxy->proxy, buffer, sizeof(buffer), MSG_PEEK); 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) { if (ret != 0) {
res->status = http_get_status(500); res->status = http_get_status(500);
ctx->origin = INTERNAL; ctx->origin = INTERNAL;
return NULL; return -1;
} }
return proxy; return 0;
proxy_err: proxy_err:
errno = 0; errno = 0;
if (retry) goto retry; if (retry) goto retry;
return NULL; 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) {

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); 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); 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); proxy_handler_1(ctx);
respond(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 // WebSocket
sock_set_timeout(&ctx->socket, WS_TIMEOUT); sock_set_timeout(&ctx->socket, WS_TIMEOUT);
sock_set_timeout(&ctx->proxy->proxy, 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"); info("WebSocket connection closed");
return; return;
} }
proxy_handler_2(ctx);
request_complete(ctx);
handle_request(ctx);
} }
static int proxy_handler_1(client_ctx_t *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, "Date", HTTP_REMOVE_ALL);
http_remove_header_field(&res->hdr, "Server", 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 = proxy_init(&ctx->proxy, &ctx->req, res, status, ctx->conf, &ctx->socket, &ctx->custom_status, ctx->err_msg) == 0;
ctx->use_proxy = (ctx->proxy != NULL);
if (res->status->code == 101) { if (res->status->code == 101) {
const char *connection = http_get_header_field(&res->hdr, "Connection"); const char *connection = http_get_header_field(&res->hdr, "Connection");