From c452c3dc47bacf1cb4dfd7a8129d62fa8819282e Mon Sep 17 00:00:00 2001 From: Aleksey Veresov Date: Mon, 13 Jul 2020 06:35:05 +0300 Subject: Buffer for post input. --- src/parse.c | 29 ++++++++++++++++++++++++++--- src/request.c | 1 + 2 files changed, 27 insertions(+), 3 deletions(-) (limited to 'src') diff --git a/src/parse.c b/src/parse.c index 58ae001..83884f0 100644 --- a/src/parse.c +++ b/src/parse.c @@ -147,9 +147,31 @@ static char *bound(const char *type) return magi_str_create_copy(type, strcspn(type, " \t")); } -static int next() +static int next(void *userdata) { - return getchar(); + int size = *(int *)userdata; + static char *buffer = 0; + static int left = 0; + static int last = 0; + static int pos; + if (!buffer) { + if (last) { + return EOF; + } + buffer = malloc(size); + } + if (!left) { + if (last) { + free(buffer); + buffer = 0; + return EOF; + } + left = fread(buffer, 1, size, stdin); + last = left != size; + pos = 0; + } + left--; + return buffer[pos++]; } /* Interfacial CGI Request Handling */ @@ -180,7 +202,8 @@ int magi_parse_body(magi_request *request) if (!strncmp(t, "multipart/form-data", 19)) { char *boundary = bound(t); if (boundary && *boundary) { - magi_parse_multipart(request, boundary, next, 0); + magi_parse_multipart(request, boundary, next, + &request->limits.read_buffer); } else { *e = magi_error_nobound; } diff --git a/src/request.c b/src/request.c index bf8fc1d..22b3f30 100644 --- a/src/request.c +++ b/src/request.c @@ -13,6 +13,7 @@ void magi_request_init(magi_request *request) request->limits.params_meta = 0; request->limits.params_head = 0; request->limits.params_body = 0; + request->limits.read_buffer = 65536; } } -- cgit v1.2.3