<commit_before>#include "server.h"
#include "core.h"
#include "_string.h"
#include "util.h"
#include "proto/s5.h"
#include "proto/http.h"


rps_status_t
server_init(struct server *s, struct config_server *cfg) {
    int err;
    int status;

    err = uv_loop_init(&s->loop);
    if (err != 0) {
        UV_SHOW_ERROR(err, "loop init");
        return RPS_ERROR;
    }

    err = uv_tcp_init(&s->loop, &s->us);
    if (err !=0 ) {
        UV_SHOW_ERROR(err, "tcp init");
        return RPS_ERROR;
    }

    s->us.data = s;

    if (rps_strcmp(cfg->proxy.data, "socks5") == 0 ) {
        s->proxy = SOCKS5;
    } else if (rps_strcmp(cfg->proxy.data, "http") == 0 ) {
        s->proxy = HTTP;
    }
#ifdef SOCKS4_PROXY_SUPPORT
     else if (rps_strcmp(cfg->proxy.data, "socks4") == 0 ) {
        s->proxy = SOCKS4;
     }
#endif
#ifdef PRIVATE_PROXY_SUPPORT
     else if (rps_strcmp(cfg->proxy.data, "private") == 0 ) {
        s->proxy = PRIVATE;
     }
#endif
    else{
        log_error("unsupport proxy type: %s", cfg->proxy.data);
        return RPS_ERROR;
    }
     
    status = rps_resolve_inet((const char *)cfg->listen.data, cfg->port, &s->listen);
    if (status < 0) {
        log_error("resolve inet %s:%d failed", cfg->listen.data, cfg->port);
        return RPS_ERROR;
    }

    s->cfg = cfg;

    return RPS_OK;
}


void
server_deinit(struct server *s) {
    uv_loop_close(&s->loop);

    /* Make valgrind happy */
    uv_loop_delete(&s->loop);
}

static void
server_sess_init(rps_sess_t *sess) {
    sess->request = NULL;
    sess->forward = NULL;
}

static void
server_sess_free(rps_sess_t *sess) {
    if (sess->request != NULL) {
        ASSERT(!(sess->request->state &  c_connect));
    }
    if (sess->forward != NULL) {
        ASSERT(!(sess->forward->state &  c_connect));
    }
    rps_free(sess);
}

static void
server_ctx_init(rps_ctx_t *ctx, rps_sess_t *sess, uint8_t flag, rps_proxy_t proxy) {
    ctx->sess = sess;
    ctx->handle.handle.data  = ctx;
    ctx->flag = flag;
    ctx->state = c_init;
    ctx->proxy = proxy;

    switch (ctx->proxy) {
        case SOCKS5:
            ctx->do_parse = s5_do_parse;
            break;
        case HTTP:
            ctx->do_parse = http_do_parse;
            break;
        #ifdef SOCKS4_PROXY_SUPPORT
        case SOCKS4:
           ctx->do_parse = s4_do_parse; 
           break;
        #endif
        #if PRIVATE_PROXY_SUPPORT
        case PRIVATE:
           ctx->do_parse = private_do_parse;
           break;
        #endif
        default:
           NOT_REACHED();
    }

    return;
}

static void 
server_on_ctx_close(uv_handle_t* handle) {
    //Set flag be closed and 
    rps_ctx_t *ctx;
    ctx = handle->data;
    ctx->state = c_closed;

    switch (ctx->flag) {
        case c_request:
            log_debug("Request from %s be closed", ctx->peername);
            break;
        case c_forward:
            log_debug("Forward to %s be closed.", ctx->peername);
            break;
        default:
            NOT_REACHED();
    }

    rps_free(ctx);
}

static void
server_ctx_close(rps_ctx_t *ctx) {
    ASSERT((ctx->state & c_connect));
    ctx->state = c_closing;
    uv_close(&ctx->handle.handle, (uv_close_cb)server_on_ctx_close);<commit_msg>Set null after free memory<commit_after>#include "server.h"
#include "core.h"
#include "_string.h"
#include "util.h"
#include "proto/s5.h"
#include "proto/http.h"


rps_status_t
server_init(struct server *s, struct config_server *cfg) {
    int err;
    int status;

    err = uv_loop_init(&s->loop);
    if (err != 0) {
        UV_SHOW_ERROR(err, "loop init");
        return RPS_ERROR;
    }

    err = uv_tcp_init(&s->loop, &s->us);
    if (err !=0 ) {
        UV_SHOW_ERROR(err, "tcp init");
        return RPS_ERROR;
    }

    s->us.data = s;

    if (rps_strcmp(cfg->proxy.data, "socks5") == 0 ) {
        s->proxy = SOCKS5;
    } else if (rps_strcmp(cfg->proxy.data, "http") == 0 ) {
        s->proxy = HTTP;
    }
#ifdef SOCKS4_PROXY_SUPPORT
     else if (rps_strcmp(cfg->proxy.data, "socks4") == 0 ) {
        s->proxy = SOCKS4;
     }
#endif
#ifdef PRIVATE_PROXY_SUPPORT
     else if (rps_strcmp(cfg->proxy.data, "private") == 0 ) {
        s->proxy = PRIVATE;
     }
#endif
    else{
        log_error("unsupport proxy type: %s", cfg->proxy.data);
        return RPS_ERROR;
    }
     
    status = rps_resolve_inet((const char *)cfg->listen.data, cfg->port, &s->listen);
    if (status < 0) {
        log_error("resolve inet %s:%d failed", cfg->listen.data, cfg->port);
        return RPS_ERROR;
    }

    s->cfg = cfg;

    return RPS_OK;
}


void
server_deinit(struct server *s) {
    uv_loop_close(&s->loop);

    /* Make valgrind happy */
    uv_loop_delete(&s->loop);
}

static void
server_sess_init(rps_sess_t *sess) {
    sess->request = NULL;
    sess->forward = NULL;
}

static void
server_sess_free(rps_sess_t *sess) {
    if (sess->request != NULL) {
        ASSERT(!(sess->request->state &  c_connect));
    }
    if (sess->forward != NULL) {
        ASSERT(!(sess->forward->state &  c_connect));
    }
    rps_free(sess);
}

static void
server_ctx_init(rps_ctx_t *ctx, rps_sess_t *sess, uint8_t flag, rps_proxy_t proxy) {
    ctx->sess = sess;
    ctx->handle.handle.data  = ctx;
    ctx->flag = flag;
    ctx->state = c_init;
    ctx->proxy = proxy;

    switch (ctx->proxy) {
        case SOCKS5:
            ctx->do_parse = s5_do_parse;
            break;
        case HTTP:
            ctx->do_parse = http_do_parse;
            break;
        #ifdef SOCKS4_PROXY_SUPPORT
        case SOCKS4:
           ctx->do_parse = s4_do_parse; 
           break;
        #endif
        #if PRIVATE_PROXY_SUPPORT
        case PRIVATE:
           ctx->do_parse = private_do_parse;
           break;
        #endif
        default:
           NOT_REACHED();
    }

    return;
}

static void 
server_on_ctx_close(uv_handle_t* handle) {
    //Set flag be closed and 
    rps_ctx_t *ctx;
    rps_sess_t *sess;

    ctx = handle->data;
    sess = ctx->sess;
    ctx->state = c_closed;

    switch (ctx->flag) {
        case c_request:
            log_debug("Request from %s be closed", ctx->peername);
            sess->request = NULL;       
            break;
        case c_forward:
            log_debug("Forward to %s be closed.", ctx->peername);
            sess->forward = NULL;       
            break;
        default:
            NOT_REACHED();
    }

    rps_free(ctx);
}

static void
server_ctx_close(rps_ctx_t *ctx) {
    ASSERT((ctx->state & c_connect));
    ctx->state = c_closing;
    uv_close(&ctx->handle.handle, (uv_close_cb)server_on_ctx_close);
{'fraction_non_alphanumeric': 0.1377643504531722, 'fraction_numerical': 0.005740181268882176, 'mean_word_length': 2.2140776699029128, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 100, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '449717', 'n_tokens_mistral': 2481, 'n_tokens_neox': 2431, 'n_words': 489}