From 22441dfbf83c845740e2834aca16e46a9ba75bae Mon Sep 17 00:00:00 2001 From: Aki Date: Fri, 1 May 2020 20:06:31 +0200 Subject: Server now echoes received request --- http.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) (limited to 'http.c') diff --git a/http.c b/http.c index 978af38..264c7b5 100644 --- a/http.c +++ b/http.c @@ -1,6 +1,8 @@ #include "http.h" +#include #include +#include #include #include #include @@ -59,3 +61,30 @@ int respond_with_body(const int fd, const enum status status, const char * body) return send(fd, body, strlen(body), 0); } + +/// +/// \param fd +/// \param request +/// \return +int parse_request(const int fd, struct request * const request) +{ + char buffer[10240]; + + int length = recv(fd, buffer, 10240, 0); + + if (-1 == length && EWOULDBLOCK != errno && EAGAIN != errno) + { + return -1; + } + + if (0 == length) + { + return -1; // TODO: Handle errors properly + } + + request->body = malloc(length + 1); + memcpy(request->body, buffer, length); + request->body[length] = 0; + + return length; +} -- cgit v1.1