<commit_before>#pragma comment (lib, "libuv.lib")
#pragma comment (lib, "ws2_32.lib")
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "Iphlpapi.lib")

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "uv.h"
#include "http_parser.h"

#define UVERR(err, msg) fprintf(stderr, "%s: %s\n", msg, uv_strerror(err))
#define CHECK(r, msg) \
  if (r) { \
    uv_err_t err = uv_last_error(uv_loop); \
    UVERR(err, msg); \
    exit(1); \
  }

//"Connection: Keep-Alive\r\n" \

#define RESPONSE \
  "HTTP/1.1 200 OK\r\n" \
  "Content-Type: text/plain\r\n" \
  "Content-Length: 13\r\n" \
  "Connection: Keep-Alive\r\n" \
  "\r\n" \
  "hello world\n"

static uv_loop_t* uv_loop;
static uv_tcp_t server;
static http_parser_settings parser_settings;

static uv_buf_t resbuf;

typedef struct 
{
	uv_tcp_t handle;
	http_parser parser;
	uv_write_t write_req;
	int request_num;
} client_t;

void on_close(uv_handle_t* handle)
{
    printf("on_close()\n");
	client_t* client = (client_t*) handle->data;
	free(client);
}

uv_buf_t on_alloc(uv_handle_t* client, size_t suggested_size) 
{
	uv_buf_t buf;
	buf.base = (char *)malloc(suggested_size * 10); // CHANGED
	buf.len = suggested_size * 10;
	return buf;
}

void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) 
{
    printf("on_read()\n");
    size_t parsed;
    client_t* client = (client_t*) tcp->data;

    if (nread >= 0) 
    {
        parsed = http_parser_execute(&client->parser, &parser_settings, buf.base, nread);
        if (parsed < nread) 
        {
            printf("ERROR on_read() parsed < nread\n");
            //uv_close((uv_handle_t*) &client->handle, on_close);
            //uv_read_start((uv_stream_t*)&client->handle, on_alloc, on_read);
        }
    } 
    else 
    {
        printf("ERROR on_read() nread < 0\n");
        uv_err_t err = uv_last_error(uv_loop);
        if (err.code != UV_EOF) 
        {
            UVERR(err, "read");
        }
        uv_close((uv_handle_t*) &client->handle, on_close);
    }
    free(buf.base);
}

static int request_num = 1;

void on_connect(uv_stream_t* server_handle, int status) 
{
    printf("on_connect() status:%d\n", status);
    int r;

    client_t* client = (client_t *)malloc(sizeof(client_t));
    client->request_num = request_num;

    uv_tcp_init(uv_loop, &client->handle);
    http_parser_init(&client->parser, HTTP_REQUEST);

    client->parser.data = client;<commit_msg>Update webserver.c<commit_after>#pragma comment (lib, "libuv.lib")
#pragma comment (lib, "ws2_32.lib")
#pragma comment(lib, "psapi.lib")
#pragma comment(lib, "Iphlpapi.lib")

#include <stdio.h>
#include <stdlib.h>
#include <assert.h>
#include "uv.h"
#include "http_parser.h"

#define UVERR(err, msg) fprintf(stderr, "%s: %s\n", msg, uv_strerror(err))
#define CHECK(r, msg) \
  if (r) { \
    uv_err_t err = uv_last_error(uv_loop); \
    UVERR(err, msg); \
    exit(1); \
  }

//"Connection: Keep-Alive\r\n" \

#define RESPONSE \
  "HTTP/1.1 200 OK\r\n" \
  "Content-Type: text/plain\r\n" \
  "Content-Length: 13\r\n" \
  "Connection: Keep-Alive\r\n" \
  "\r\n" \
  "hello world\n"

static uv_loop_t* uv_loop;
static uv_tcp_t server;
static http_parser_settings parser_settings;

static uv_buf_t resbuf;

typedef struct 
{
	uv_tcp_t handle;
	http_parser parser;
	uv_write_t write_req;
	int request_num;
} client_t;

void on_close(uv_handle_t* handle)
{
    printf("on_close()\n");
	client_t* client = (client_t*) handle->data;
	free(client);
}

uv_buf_t on_alloc(uv_handle_t* client, size_t suggested_size) 
{
	uv_buf_t buf;
	buf.base = (char *)malloc(suggested_size * 10); // CHANGED
	buf.len = suggested_size * 10;
	return buf;
}

void on_read(uv_stream_t* tcp, ssize_t nread, uv_buf_t buf) 
{
    printf("on_read()\n");
    size_t parsed;
    client_t* client = (client_t*) tcp->data;

    if (nread >= 0) 
    {
        parsed = http_parser_execute(&client->parser, &parser_settings, buf.base, nread);
        if (parsed < nread) 
        {
            printf("ERROR on_read() parsed < nread\n");
            //uv_close((uv_handle_t*) &client->handle, on_close);
        }
    } 
    else 
    {
        printf("ERROR on_read() nread < 0\n");
        uv_err_t err = uv_last_error(uv_loop);
        if (err.code != UV_EOF) 
        {
            UVERR(err, "read");
        }
        uv_close((uv_handle_t*) &client->handle, on_close);
    }
    free(buf.base);
}

static int request_num = 1;

void on_connect(uv_stream_t* server_handle, int status) 
{
    printf("on_connect() status:%d\n", status);
    int r;

    client_t* client = (client_t *)malloc(sizeof(client_t));
    client->request_num = request_num;

    uv_tcp_init(uv_loop, &client->handle);
    http_parser_init(&client->parser, HTTP_REQUEST);

    client->parser.data = client;
{'fraction_non_alphanumeric': 0.15442719261435164, 'fraction_numerical': 0.007553503986571549, 'mean_word_length': 3.244879786286732, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 30, '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': '7610927', 'n_tokens_mistral': 1941, 'n_tokens_neox': 1865, 'n_words': 369}