Remove gotos from proxy.c
This commit is contained in:
103
src/lib/proxy.c
103
src/lib/proxy.c
@ -300,25 +300,8 @@ int proxy_response_header(http_req *req, http_res *res, host_config_t *conf) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
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];
|
||||
const char *connection, *upgrade, *ws_version;
|
||||
long ret;
|
||||
int tries = 0, retry = 0;
|
||||
|
||||
*proxy_ptr = proxy_get_by_conf(conf);
|
||||
proxy_ctx_t *proxy = *proxy_ptr;
|
||||
proxy->client = NULL;
|
||||
|
||||
if (proxy->initialized && sock_has_pending(&proxy->proxy) == 0)
|
||||
goto proxy;
|
||||
|
||||
retry:
|
||||
if (proxy->initialized)
|
||||
proxy_close(proxy);
|
||||
|
||||
retry = 0;
|
||||
tries++;
|
||||
static int proxy_connect(proxy_ctx_t *proxy, host_config_t *conf, http_res *res, http_status_ctx *ctx, char *err_msg) {
|
||||
char err_buf[256], addr_buf[1024];
|
||||
|
||||
int fd;
|
||||
if ((fd = socket(AF_INET6, SOCK_STREAM, 0)) == -1) {
|
||||
@ -329,8 +312,13 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
}
|
||||
sock_init(&proxy->proxy, fd, 0);
|
||||
|
||||
if (sock_set_socket_timeout(&proxy->proxy, 1) != 0 || sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT_INIT) != 0)
|
||||
goto proxy_timeout_err;
|
||||
if (sock_set_socket_timeout(&proxy->proxy, 1) != 0 || sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT_INIT) != 0) {
|
||||
res->status = http_get_status(500);
|
||||
ctx->origin = INTERNAL;
|
||||
error("Unable to set timeout for reverse proxy socket");
|
||||
sprintf(err_msg, "Unable to set timeout for reverse proxy socket: %s", error_str(errno, err_buf, sizeof(err_buf)));
|
||||
return -1;
|
||||
}
|
||||
|
||||
struct hostent *host_ent = gethostbyname2(conf->proxy.hostname, AF_INET6);
|
||||
if (host_ent == NULL) {
|
||||
@ -340,7 +328,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
ctx->origin = SERVER_REQ;
|
||||
error("Unable to connect to server: Name or service not known");
|
||||
sprintf(err_msg, "Unable to connect to server: Name or service not known.");
|
||||
goto proxy_err;
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
@ -353,9 +341,9 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
memcpy(&address.sin6_addr, addr, 16);
|
||||
}
|
||||
|
||||
inet_ntop(address.sin6_family, (void *) &address.sin6_addr, buffer, sizeof(buffer));
|
||||
inet_ntop(address.sin6_family, (void *) &address.sin6_addr, addr_buf, sizeof(addr_buf));
|
||||
|
||||
info(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "...", buffer, conf->proxy.port);
|
||||
info(BLUE_STR "Connecting to " BLD_STR "[%s]:%i" CLR_STR BLUE_STR "...", addr_buf, conf->proxy.port);
|
||||
if (connect(proxy->proxy.socket, (struct sockaddr *) &address, sizeof(address)) < 0) {
|
||||
if (errno == ETIMEDOUT || errno == EINPROGRESS) {
|
||||
res->status = http_get_status(504);
|
||||
@ -367,18 +355,17 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
res->status = http_get_status(500);
|
||||
ctx->origin = INTERNAL;
|
||||
}
|
||||
error("Unable to connect to [%s]:%i", buffer, conf->proxy.port);
|
||||
error("Unable to connect to [%s]:%i", addr_buf, conf->proxy.port);
|
||||
sprintf(err_msg, "Unable to connect to server: %s.", error_str(errno, err_buf, sizeof(err_buf)));
|
||||
goto proxy_err;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (sock_set_timeout(&proxy->proxy, SERVER_TIMEOUT) != 0) {
|
||||
proxy_timeout_err:
|
||||
res->status = http_get_status(500);
|
||||
ctx->origin = INTERNAL;
|
||||
error("Unable to set timeout for reverse proxy socket");
|
||||
sprintf(err_msg, "Unable to set timeout for reverse proxy socket: %s", error_str(errno, err_buf, sizeof(err_buf)));
|
||||
goto proxy_err;
|
||||
return -1;
|
||||
}
|
||||
|
||||
if (conf->proxy.enc) {
|
||||
@ -386,6 +373,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
SSL_set_fd(proxy->proxy.ssl, proxy->proxy.socket);
|
||||
SSL_set_connect_state(proxy->proxy.ssl);
|
||||
|
||||
int ret;
|
||||
if ((ret = SSL_do_handshake(proxy->proxy.ssl)) != 1) {
|
||||
sock_error(&proxy->proxy, (int) ret);
|
||||
SSL_free(proxy->proxy.ssl);
|
||||
@ -393,7 +381,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
ctx->origin = SERVER_REQ;
|
||||
error("Unable to perform handshake");
|
||||
sprintf(err_msg, "Unable to perform handshake: %s.", error_str(errno, err_buf, sizeof(err_buf)));
|
||||
goto proxy_err;
|
||||
return -1;
|
||||
}
|
||||
proxy->proxy.enc = 1;
|
||||
}
|
||||
@ -401,13 +389,38 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
proxy->initialized = 1;
|
||||
proxy->cnx_s = clock_micros();
|
||||
proxy->host = conf->name;
|
||||
info(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i", buffer, conf->proxy.port);
|
||||
info(BLUE_STR "Established new connection with " BLD_STR "[%s]:%i", addr_buf, conf->proxy.port);
|
||||
|
||||
proxy:
|
||||
connection = http_get_header_field(&req->hdr, "Connection");
|
||||
return 0;
|
||||
}
|
||||
|
||||
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];
|
||||
long ret;
|
||||
int tries = 0, retry = 1;
|
||||
|
||||
*proxy_ptr = proxy_get_by_conf(conf);
|
||||
proxy_ctx_t *proxy = *proxy_ptr;
|
||||
proxy->client = NULL;
|
||||
|
||||
while (retry) {
|
||||
errno = 0;
|
||||
|
||||
if (!proxy->initialized || sock_has_pending(&proxy->proxy) != 0) {
|
||||
if (proxy->initialized)
|
||||
proxy_close(proxy);
|
||||
|
||||
retry = 0;
|
||||
tries++;
|
||||
|
||||
if (proxy_connect(proxy, conf, res, ctx, err_msg) != 0)
|
||||
continue;
|
||||
}
|
||||
|
||||
const char *connection = http_get_header_field(&req->hdr, "Connection");
|
||||
if (strcontains(connection, "upgrade") || strcontains(connection, "Upgrade")) {
|
||||
upgrade = http_get_header_field(&req->hdr, "Upgrade");
|
||||
ws_version = http_get_header_field(&req->hdr, "Sec-WebSocket-Version");
|
||||
const char *upgrade = http_get_header_field(&req->hdr, "Upgrade");
|
||||
const char *ws_version = http_get_header_field(&req->hdr, "Sec-WebSocket-Version");
|
||||
if (streq(upgrade, "websocket") && streq(ws_version, "13")) {
|
||||
ctx->ws_key = http_get_header_field(&req->hdr, "Sec-WebSocket-Key");
|
||||
} else {
|
||||
@ -434,7 +447,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
error("Unable to send request to server (1)");
|
||||
sprintf(err_msg, "Unable to send request to server: %s.", error_str(errno, err_buf, sizeof(err_buf)));
|
||||
retry = tries < 4;
|
||||
goto proxy_err;
|
||||
continue;
|
||||
}
|
||||
|
||||
const char *content_length = http_get_header_field(&req->hdr, "Content-Length");
|
||||
@ -455,7 +468,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
error("Unable to send request to server (2)");
|
||||
sprintf(err_msg, "Unable to send request to server: %s.", error_str(errno, err_buf, sizeof(err_buf)));
|
||||
retry = tries < 4;
|
||||
goto proxy_err;
|
||||
continue;
|
||||
} else if (ret == -1) {
|
||||
res->status = http_get_status(400);
|
||||
ctx->origin = CLIENT_REQ;
|
||||
@ -482,7 +495,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
error("Unable to receive response from server");
|
||||
sprintf(err_msg, "Unable to receive response from server: %s.", error_str(errno, err_buf, sizeof(err_buf)));
|
||||
retry = tries < 4;
|
||||
goto proxy_err;
|
||||
continue;
|
||||
}
|
||||
buffer[ret] = 0;
|
||||
|
||||
@ -494,7 +507,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
ctx->origin = SERVER_RES;
|
||||
error("Unable to parse header: End of header not found");
|
||||
sprintf(err_msg, "Unable to parser header: End of header not found.");
|
||||
goto proxy_err;
|
||||
continue;
|
||||
}
|
||||
|
||||
for (int i = 0; i < header_len; i++) {
|
||||
@ -503,7 +516,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
ctx->origin = SERVER_RES;
|
||||
error("Unable to parse header: Header contains illegal characters");
|
||||
sprintf(err_msg, "Unable to parse header: Header contains illegal characters.");
|
||||
goto proxy_err;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
|
||||
@ -515,7 +528,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
ctx->origin = SERVER_RES;
|
||||
error("Unable to parse header: Invalid header format");
|
||||
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
||||
goto proxy_err;
|
||||
continue;
|
||||
}
|
||||
if (ptr == buf) {
|
||||
if (!strstarts(ptr, "HTTP/")) {
|
||||
@ -523,7 +536,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
ctx->origin = SERVER_RES;
|
||||
error("Unable to parse header: Invalid header format");
|
||||
sprintf(err_msg, "Unable to parse header: Invalid header format.");
|
||||
goto proxy_err;
|
||||
continue;
|
||||
}
|
||||
int status_code = (int) strtol(ptr + 9, NULL, 10);
|
||||
res->status = http_get_status(status_code);
|
||||
@ -538,7 +551,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
ctx->origin = SERVER_RES;
|
||||
error("Unable to parse header: Invalid or unknown status code");
|
||||
sprintf(err_msg, "Unable to parse header: Invalid or unknown status code.");
|
||||
goto proxy_err;
|
||||
continue;
|
||||
}
|
||||
} else {
|
||||
if (http_parse_header_field(&res->hdr, ptr, pos0, 0) != 0) {
|
||||
@ -546,7 +559,7 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
ctx->origin = SERVER_RES;
|
||||
error("Unable to parse header");
|
||||
sprintf(err_msg, "Unable to parse header.");
|
||||
goto proxy_err;
|
||||
continue;
|
||||
}
|
||||
}
|
||||
if (pos0[2] == '\r' && pos0[3] == '\n') {
|
||||
@ -564,10 +577,8 @@ int proxy_init(proxy_ctx_t **proxy_ptr, http_req *req, http_res *res, http_statu
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
proxy_err:
|
||||
errno = 0;
|
||||
if (retry) goto retry;
|
||||
return -1;
|
||||
}
|
||||
|
||||
|
Reference in New Issue
Block a user