Bugfix for proxy

This commit is contained in:
2021-01-08 18:01:19 +01:00
parent debac11f90
commit b676388018
4 changed files with 33 additions and 12 deletions

View File

@ -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;

View File

@ -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) {
if (ret == -1) {
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 (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;
}
}
}

View File

@ -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;
}

View File

@ -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