Refactor proxy a bit
This commit is contained in:
		@@ -22,8 +22,7 @@
 | 
			
		||||
#include "config.h"
 | 
			
		||||
 | 
			
		||||
typedef struct {
 | 
			
		||||
    unsigned char initialized:1;
 | 
			
		||||
    unsigned char in_use:1;
 | 
			
		||||
    unsigned char initialized:1, in_use:1;
 | 
			
		||||
    sock proxy;
 | 
			
		||||
    char *host;
 | 
			
		||||
} proxy_ctx_t;
 | 
			
		||||
 
 | 
			
		||||
@@ -130,6 +130,9 @@ static void terminate_gracefully(int sig) {
 | 
			
		||||
    sigaction(SIGINT, &act, NULL);
 | 
			
		||||
    sigaction(SIGTERM, &act, NULL);
 | 
			
		||||
 | 
			
		||||
    // TODO close client connections
 | 
			
		||||
    // TODO close proxy connections
 | 
			
		||||
 | 
			
		||||
    workers_stop();
 | 
			
		||||
    workers_destroy();
 | 
			
		||||
 | 
			
		||||
 
 | 
			
		||||
@@ -18,7 +18,7 @@
 | 
			
		||||
typedef struct {
 | 
			
		||||
    sock socket;
 | 
			
		||||
    int req_num;
 | 
			
		||||
    unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1;
 | 
			
		||||
    unsigned char in_use: 1, s_keep_alive:1, c_keep_alive:1, use_fastcgi:4, use_proxy:2, ws_close:2;
 | 
			
		||||
    char cc[3], host[256];
 | 
			
		||||
    char req_host[256], err_msg[256];
 | 
			
		||||
    char log_prefix[128];
 | 
			
		||||
@@ -29,7 +29,6 @@ typedef struct {
 | 
			
		||||
    http_uri uri;
 | 
			
		||||
    http_status_ctx status;
 | 
			
		||||
    http_status custom_status;
 | 
			
		||||
    int use_fastcgi, use_proxy;
 | 
			
		||||
    host_config_t *conf;
 | 
			
		||||
    FILE *file;
 | 
			
		||||
    long content_length;
 | 
			
		||||
@@ -60,4 +59,6 @@ void request_complete(client_ctx_t *ctx);
 | 
			
		||||
 | 
			
		||||
void tcp_close(client_ctx_t *ctx);
 | 
			
		||||
 | 
			
		||||
void proxy_close(proxy_ctx_t *ctx);
 | 
			
		||||
 | 
			
		||||
#endif //SESIMOS_FUNC_H
 | 
			
		||||
 
 | 
			
		||||
@@ -14,6 +14,7 @@
 | 
			
		||||
#include "../workers.h"
 | 
			
		||||
 | 
			
		||||
#include <string.h>
 | 
			
		||||
#include <errno.h>
 | 
			
		||||
 | 
			
		||||
static int proxy_handler_1(client_ctx_t *ctx);
 | 
			
		||||
static int proxy_handler_2(client_ctx_t *ctx);
 | 
			
		||||
@@ -23,6 +24,17 @@ void proxy_handler_func(client_ctx_t *ctx) {
 | 
			
		||||
 | 
			
		||||
    proxy_handler_1(ctx);
 | 
			
		||||
    respond(ctx);
 | 
			
		||||
 | 
			
		||||
    if (ctx->use_proxy == 2) {
 | 
			
		||||
        // WebSocket
 | 
			
		||||
        info("Upgrading connection to WebSocket connection");
 | 
			
		||||
        if (ws_handle_connection(&ctx->socket, &ctx->proxy->proxy) != 0) {
 | 
			
		||||
            ctx->c_keep_alive = 0;
 | 
			
		||||
            proxy_close(ctx->proxy);
 | 
			
		||||
        }
 | 
			
		||||
        info("WebSocket connection closed");
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    proxy_handler_2(ctx);
 | 
			
		||||
    request_complete(ctx);
 | 
			
		||||
 | 
			
		||||
@@ -126,3 +138,11 @@ static int proxy_handler_2(client_ctx_t *ctx) {
 | 
			
		||||
 | 
			
		||||
    return ret;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void proxy_close(proxy_ctx_t *ctx) {
 | 
			
		||||
    info(BLUE_STR "Closing proxy connection");
 | 
			
		||||
    sock_close(&ctx->proxy);
 | 
			
		||||
 | 
			
		||||
    memset(ctx, 0, sizeof(*ctx));
 | 
			
		||||
    errno = 0;
 | 
			
		||||
}
 | 
			
		||||
 
 | 
			
		||||
@@ -316,10 +316,7 @@ int respond(client_ctx_t *ctx) {
 | 
			
		||||
        }
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    int close_proxy = 0;
 | 
			
		||||
    if (ctx->use_proxy != 2) {
 | 
			
		||||
        const char *conn = http_get_header_field(&res->hdr, "Connection");
 | 
			
		||||
        close_proxy = (conn == NULL || (strstr(conn, "keep-alive") == NULL && strstr(conn, "Keep-Alive") == NULL));
 | 
			
		||||
        http_remove_header_field(&res->hdr, "Connection", HTTP_REMOVE_ALL);
 | 
			
		||||
        http_remove_header_field(&res->hdr, "Keep-Alive", HTTP_REMOVE_ALL);
 | 
			
		||||
        if (ctx->s_keep_alive && ctx->c_keep_alive) {
 | 
			
		||||
@@ -340,16 +337,8 @@ int respond(client_ctx_t *ctx) {
 | 
			
		||||
 | 
			
		||||
    // TODO access/error log file
 | 
			
		||||
 | 
			
		||||
    if (ctx->use_proxy == 2) {
 | 
			
		||||
        // WebSocket
 | 
			
		||||
        info("Upgrading connection to WebSocket connection");
 | 
			
		||||
        ret = ws_handle_connection(client, &ctx->proxy->proxy);
 | 
			
		||||
        if (ret != 0) {
 | 
			
		||||
            ctx->c_keep_alive = 0;
 | 
			
		||||
            close_proxy = 1;
 | 
			
		||||
        }
 | 
			
		||||
        info("WebSocket connection closed");
 | 
			
		||||
    } else if (ctx->use_proxy) {
 | 
			
		||||
    if (ctx->use_proxy) {
 | 
			
		||||
        // reverse proxy
 | 
			
		||||
        return 3;
 | 
			
		||||
    } else if (strcmp(req->method, "HEAD") != 0) {
 | 
			
		||||
        // default response
 | 
			
		||||
@@ -376,21 +365,13 @@ int respond(client_ctx_t *ctx) {
 | 
			
		||||
            return 2;
 | 
			
		||||
        }
 | 
			
		||||
 | 
			
		||||
        if (ret < 0) {
 | 
			
		||||
            ctx->c_keep_alive = 0;
 | 
			
		||||
        }
 | 
			
		||||
        if (ret < 0) ctx->c_keep_alive = 0;
 | 
			
		||||
    }
 | 
			
		||||
 | 
			
		||||
    return 0;
 | 
			
		||||
}
 | 
			
		||||
 | 
			
		||||
void request_complete(client_ctx_t *ctx) {
 | 
			
		||||
    // FIXME
 | 
			
		||||
    //if (close_proxy && proxy.socket != 0) {
 | 
			
		||||
    //    info(BLUE_STR "Closing proxy connection");
 | 
			
		||||
    //    sock_close(&proxy);
 | 
			
		||||
    //}
 | 
			
		||||
 | 
			
		||||
    char buf[32];
 | 
			
		||||
    ctx->req_e = clock_micros();
 | 
			
		||||
    info("Transfer complete: %s", format_duration(ctx->req_e - ctx->req_s, buf));
 | 
			
		||||
 
 | 
			
		||||
		Reference in New Issue
	
	Block a user