From b676388018a6c9a92310a152d03fa612201e8205 Mon Sep 17 00:00:00 2001 From: Lorenz Stechauner Date: Fri, 8 Jan 2021 18:01:19 +0100 Subject: [PATCH] Bugfix for proxy --- src/http.c | 3 ++- src/rev_proxy.c | 31 +++++++++++++++++++++---------- src/sock.c | 7 ++++++- src/sock.h | 4 ++++ 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/src/http.c b/src/http.c index 5e11cd3..b70a3fd 100644 --- a/src/http.c +++ b/src/http.c @@ -238,7 +238,8 @@ int http_send_request(sock *server, http_req *req) { off += sprintf(buf + off, "%s: %s\r\n", req->hdr.fields[i][0], req->hdr.fields[i][1]); } off += sprintf(buf + off, "\r\n"); - if (sock_send(server, buf, off, 0) <= 0) { + long ret = sock_send(server, buf, off, 0); + if (ret <= 0) { return -1; } return 0; diff --git a/src/rev_proxy.c b/src/rev_proxy.c index 0444bbf..bc46c61 100644 --- a/src/rev_proxy.c +++ b/src/rev_proxy.c @@ -19,9 +19,11 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client int tries = 0; int retry = 0; - if (rev_proxy.socket != 0 && rev_proxy_host == conf->name) { + if (rev_proxy.socket != 0 && strcmp(rev_proxy_host, conf->name) == 0 && sock_check(&rev_proxy) == 0) { goto rev_proxy; - } else if (rev_proxy.socket != 0) { + } + + if (rev_proxy.socket != 0) { sock_close(&rev_proxy); } @@ -95,14 +97,13 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client print(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i" CLR_STR, buffer, conf->rev_proxy.port); rev_proxy: - // TODO strange behaviour with Edge http_remove_header_field(&req->hdr, "Connection", HTTP_REMOVE_ALL); http_add_header_field(&req->hdr, "Connection", "keep-alive"); ret = http_send_request(&rev_proxy, req); if (ret < 0) { res->status = http_get_status(502); - print(ERR_STR "Unable to send request to server: %s" CLR_STR, sock_strerror(&rev_proxy)); + print(ERR_STR "Unable to send request to server (1): %s" CLR_STR, sock_strerror(&rev_proxy)); sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy)); retry = tries < 4; goto proxy_err; @@ -119,7 +120,7 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client ret = sock_send(&rev_proxy, client->buf, len, 0); if (ret <= 0) { res->status = http_get_status(502); - print(ERR_STR "Unable to send request to server: %s" CLR_STR, sock_strerror(&rev_proxy)); + print(ERR_STR "Unable to send request to server (2): %s" CLR_STR, sock_strerror(&rev_proxy)); sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy)); retry = tries < 4; goto proxy_err; @@ -129,11 +130,21 @@ int rev_proxy_init(http_req *req, http_res *res, host_config *conf, sock *client if (content_len > 0) { ret = sock_splice(&rev_proxy, client, buffer, sizeof(buffer), content_len); if (ret <= 0) { - res->status = http_get_status(502); - print(ERR_STR "Unable to send request to server: %s" CLR_STR, sock_strerror(&rev_proxy)); - sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy)); - retry = tries < 4; - goto proxy_err; + if (ret == -1) { + res->status = http_get_status(502); + print(ERR_STR "Unable to send request to server (3): %s" CLR_STR, sock_strerror(&rev_proxy)); + sprintf(err_msg, "Unable to send request to server: %s.", sock_strerror(&rev_proxy)); + retry = tries < 4; + goto proxy_err; + } else if (ret == -2) { + res->status = http_get_status(400); + print(ERR_STR "Unable to receive request from client: %s" CLR_STR, sock_strerror(client)); + sprintf(err_msg, "Unable to receive request from client: %s.", sock_strerror(client)); + return -1; + } + res->status = http_get_status(500); + print(ERR_STR "Unknown Error" CLR_STR); + return -1; } } } diff --git a/src/sock.c b/src/sock.c index 4d090a4..649466c 100644 --- a/src/sock.c +++ b/src/sock.c @@ -78,7 +78,7 @@ long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigne unsigned long send_len = 0; unsigned long next_len; while (send_len < len) { - next_len = buf_len < len - send_len ? buf_len : len - send_len; + next_len = (buf_len < (len - send_len)) ? buf_len : (len - send_len); ret = sock_recv(src, buf, next_len, 0); if (ret < 0) return -2; if (ret != next_len) return -3; @@ -102,3 +102,8 @@ int sock_close(sock *s) { s->ssl = NULL; return 0; } + +int sock_check(sock *s) { + char buf; + return recv(s->socket, &buf, 1, MSG_PEEK | MSG_DONTWAIT) == 1; +} diff --git a/src/sock.h b/src/sock.h index 5673dd5..543c7ac 100644 --- a/src/sock.h +++ b/src/sock.h @@ -29,4 +29,8 @@ long sock_recv(sock *s, void *buf, unsigned long len, int flags); long sock_splice(sock *dst, sock *src, void *buf, unsigned long buf_len, unsigned long len); +int sock_close(sock *s); + +int sock_check(sock *s); + #endif //NECRONDA_SERVER_SOCK_H