chibipub

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

commit 6832d4043024ccc6d9d796e7e5b1c6bfc3c57b19
parent 641b87c34ec88ccd9ccc785dd758212d896d8324
Author: William Casarin <jb55@jb55.com>
Date:   Thu, 19 Nov 2020 10:39:54 -0800

push data to an arena

Diffstat:
Msrc/wolfsocks.c | 67++++++++++++++++++++++++++++++++++++++++++++++++++-----------------
1 file changed, 50 insertions(+), 17 deletions(-)

diff --git a/src/wolfsocks.c b/src/wolfsocks.c @@ -12,13 +12,23 @@ #include "cursor.h" #define BUF_SIZE 4096 +#define ARENA_SIZE 8192 struct http_req { - char method[12]; - char path[256]; - char ver[8]; + char *method; + char *path; + char *ver; }; +struct parser { + struct cursor cur; + struct cursor arena; +}; + +struct http_header { + char *name; + char *value; +}; static void error(char *msg) { @@ -26,40 +36,48 @@ static void error(char *msg) exit(1); } -static int parse_segment(struct cursor *cur, char *method, int method_len) +static int parse_segment(struct parser *p, char **seg) { - int i; + char c; + + *seg = (char*)p->arena.p; - for (i = 0; i < method_len-1; i++) { - if (!pull_byte(cur, (unsigned char*)&method[i])) + while (1) { + if (!pull_byte(&p->cur, (unsigned char*)&c)) return 0; - if (method[i] == ' ') { - method[i] = 0; + if (c == ' ' || c == '\r' || c == '\n') { + if (!push_byte(&p->arena, 0)) + return 0; break; + } else { + if (!push_byte(&p->arena, c)) + return 0; } } return 1; } -static int parse_http_request(struct http_req *req, unsigned char *buffer, int buflen) +static int parse_header_segment(struct http_req *req, struct parser *p) { - struct cursor c; - make_cursor(buffer, buffer + buflen, &c); - - if (!parse_segment(&c, req->method, sizeof(req->method))) + if (!parse_segment(p, &req->method)) return 0; - if (!parse_segment(&c, req->path, sizeof(req->path))) + if (!parse_segment(p, &req->path)) return 0; - if (!parse_segment(&c, req->ver, sizeof(req->ver))) + if (!parse_segment(p, &req->ver)) return 0; return 1; } +static int parse_http_request(struct http_req *req, struct parser *p) +{ + return parse_header_segment(req, p); +} + static void print_http_request(struct http_req *req) { printf("%s %s %s\r\n", req->method, req->path, req->ver); @@ -67,7 +85,9 @@ static void print_http_request(struct http_req *req) void run_http_server() { + static unsigned char arena[ARENA_SIZE]; static unsigned char buffer[BUF_SIZE]; + ssize_t len; socklen_t client_len; @@ -79,8 +99,13 @@ void run_http_server() struct http_req req; + struct parser parser; + const int port = 5188; + make_cursor(buffer, buffer + BUF_SIZE, &parser.cur); + make_cursor(arena, arena + ARENA_SIZE, &parser.arena); + if ((parent = socket(AF_INET, SOCK_STREAM, 0)) < 0) { error("socket"); } @@ -127,21 +152,29 @@ void run_http_server() error("inet_htoa"); } + while(1) { len = read(client, (void*)buffer, BUF_SIZE); if (len == 0) { break; } - if (parse_http_request(&req, buffer, BUF_SIZE)) { + if (parse_http_request(&req, &parser)) { print_http_request(&req); } + printf("\nRAW:\n\n"); + if (write(1, buffer, len) == -1) { + perror("write"); + } + if (len != BUF_SIZE) { break; } } + printf("\n"); + close(client); } }