Compare commits
3 Commits
c7be0adc66
...
b369a1116e
Author | SHA1 | Date | |
---|---|---|---|
b369a1116e
|
|||
950bf19331
|
|||
993cb65724
|
@@ -24,7 +24,9 @@
|
||||
|
||||
static SSL_CTX *proxy_ctx = NULL;
|
||||
static proxy_ctx_t *proxies = NULL;
|
||||
static sem_t *available = NULL;
|
||||
static sem_t lock;
|
||||
static int num_proxy_hosts = -1;
|
||||
|
||||
int proxy_preload(void) {
|
||||
int n = 0;
|
||||
@@ -45,42 +47,53 @@ int proxy_preload(void) {
|
||||
proxy_unload();
|
||||
return -1;
|
||||
}
|
||||
|
||||
memset(proxies, 0, n * MAX_PROXY_CNX_PER_HOST * sizeof(proxy_ctx_t));
|
||||
|
||||
available = malloc(n * sizeof(*available));
|
||||
if (available == NULL) {
|
||||
proxy_unload();
|
||||
return -1;
|
||||
}
|
||||
for (int i = 0; i < n; i++) {
|
||||
if (sem_init(&available[i], 0, MAX_PROXY_CNX_PER_HOST) != 0) {
|
||||
proxy_unload();
|
||||
return -1;
|
||||
}
|
||||
}
|
||||
|
||||
if (sem_init(&lock, 0, 1) != 0) {
|
||||
proxy_unload();
|
||||
return -1;
|
||||
}
|
||||
|
||||
num_proxy_hosts = n;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
void proxy_unload(void) {
|
||||
int e = errno;
|
||||
sem_destroy(&lock);
|
||||
SSL_CTX_free(proxy_ctx);
|
||||
sem_destroy(&lock);
|
||||
if (num_proxy_hosts != -1) {
|
||||
for (int i = 0; i < num_proxy_hosts; i++) {
|
||||
sem_destroy(&available[i]);
|
||||
}
|
||||
}
|
||||
free(available);
|
||||
free(proxies);
|
||||
errno = e;
|
||||
}
|
||||
|
||||
void proxy_close_all(void) {
|
||||
int n = 0;
|
||||
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
|
||||
host_config_t *hc = &config.hosts[i];
|
||||
if (hc->type == CONFIG_TYPE_UNSET) break;
|
||||
if (hc->type != CONFIG_TYPE_REVERSE_PROXY) continue;
|
||||
n++;
|
||||
}
|
||||
|
||||
proxy_ctx_t *ptr = proxies;
|
||||
for (int i = 0; i < MAX_PROXY_CNX_PER_HOST * n; i++, ptr++) {
|
||||
for (int i = 0; i < MAX_PROXY_CNX_PER_HOST * num_proxy_hosts; i++, ptr++) {
|
||||
if (ptr->initialized)
|
||||
proxy_close(ptr);
|
||||
}
|
||||
}
|
||||
|
||||
static proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) {
|
||||
proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) {
|
||||
int n = 0;
|
||||
for (int i = 0; i < CONFIG_MAX_HOST_CONFIG; i++) {
|
||||
host_config_t *hc = &config.hosts[i];
|
||||
@@ -90,11 +103,21 @@ static proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) {
|
||||
n++;
|
||||
}
|
||||
|
||||
try_again:
|
||||
try_again_1:
|
||||
if (sem_wait(&available[n]) != 0) {
|
||||
if (errno == EINTR) {
|
||||
goto try_again_1;
|
||||
} else {
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
|
||||
try_again_2:
|
||||
if (sem_wait(&lock) != 0) {
|
||||
if (errno == EINTR) {
|
||||
goto try_again;
|
||||
goto try_again_2;
|
||||
} else {
|
||||
sem_post(&available[n]);
|
||||
return NULL;
|
||||
}
|
||||
}
|
||||
@@ -109,9 +132,16 @@ static proxy_ctx_t *proxy_get_by_conf(host_config_t *conf) {
|
||||
}
|
||||
|
||||
sem_post(&lock);
|
||||
sem_post(&available[n]);
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void proxy_unlock_ctx(proxy_ctx_t *ctx) {
|
||||
int n = (int) ((ctx - proxies) / MAX_PROXY_CNX_PER_HOST);
|
||||
ctx->in_use = 0;
|
||||
sem_post(&available[n]);
|
||||
}
|
||||
|
||||
int proxy_request_header(http_req *req, sock *sock) {
|
||||
char buf1[256], buf2[256];
|
||||
int p_len;
|
||||
|
@@ -34,6 +34,10 @@ void proxy_unload(void);
|
||||
|
||||
void proxy_close_all(void);
|
||||
|
||||
proxy_ctx_t *proxy_get_by_conf(host_config_t *conf);
|
||||
|
||||
void proxy_unlock_ctx(proxy_ctx_t *ctx);
|
||||
|
||||
int proxy_request_header(http_req *req, sock *sock);
|
||||
|
||||
int proxy_response_header(http_req *req, http_res *res, host_config_t *conf);
|
||||
|
@@ -306,6 +306,7 @@ int main(int argc, char *const argv[]) {
|
||||
|
||||
notice("Ready to accept connections");
|
||||
|
||||
// TODO handle timeouts in epoll
|
||||
async_thread();
|
||||
|
||||
notice("Goodbye!");
|
||||
|
@@ -26,11 +26,14 @@ void proxy_handler_func(client_ctx_t *ctx) {
|
||||
respond(ctx);
|
||||
|
||||
if (ret == 1) {
|
||||
|
||||
proxy_unlock_ctx(ctx->proxy);
|
||||
ctx->proxy = NULL;
|
||||
} else if (ctx->use_proxy == 0) {
|
||||
proxy_close(ctx->proxy);
|
||||
} else if (ctx->use_proxy == 1) {
|
||||
proxy_handler_2(ctx);
|
||||
proxy_unlock_ctx(ctx->proxy);
|
||||
ctx->proxy = NULL;
|
||||
} else if (ctx->use_proxy == 2) {
|
||||
// WebSocket
|
||||
ws_handle_connection(ctx);
|
||||
@@ -54,7 +57,9 @@ static int proxy_handler_1(client_ctx_t *ctx) {
|
||||
ctx->use_proxy = proxy_init(&ctx->proxy, &ctx->req, res, status, ctx->conf, &ctx->socket, &ctx->custom_status, ctx->err_msg) == 0;
|
||||
ctx->proxy->client = ctx;
|
||||
|
||||
if (res->status->code == 101) {
|
||||
if (strcmp(ctx->req.method, "HEAD") == 0) {
|
||||
return 1;
|
||||
} else if (res->status->code == 101) {
|
||||
const char *connection = http_get_header_field(&res->hdr, "Connection");
|
||||
const char *upgrade = http_get_header_field(&res->hdr, "Upgrade");
|
||||
if (connection != NULL && upgrade != NULL &&
|
||||
@@ -78,7 +83,6 @@ static int proxy_handler_1(client_ctx_t *ctx) {
|
||||
const char *content_length_f = http_get_header_field(&res->hdr, "Content-Length");
|
||||
const char *content_encoding = http_get_header_field(&res->hdr, "Content-Encoding");
|
||||
if (content_encoding == NULL && (
|
||||
strcmp(ctx->req.method, "HEAD") == 0 ||
|
||||
(content_length_f != NULL && strcmp(content_length_f, "0") == 0) ||
|
||||
(content_type != NULL && content_length_f != NULL && strncmp(content_type, "text/html", 9) == 0)))
|
||||
{
|
||||
@@ -137,8 +141,6 @@ static int proxy_handler_2(client_ctx_t *ctx) {
|
||||
|
||||
int flags = (chunked ? PROXY_CHUNKED : 0) | (ctx->use_proxy & PROXY_COMPRESS);
|
||||
int ret = proxy_send(ctx->proxy, &ctx->socket, len_to_send, flags);
|
||||
ctx->proxy->in_use = 0;
|
||||
ctx->proxy = NULL;
|
||||
|
||||
if (ret < 0) {
|
||||
ctx->c_keep_alive = 0;
|
||||
|
Reference in New Issue
Block a user