diff options
author | Aki <please@ignore.pl> | 2020-05-01 20:06:31 +0200 |
---|---|---|
committer | Aki <please@ignore.pl> | 2020-05-01 20:06:31 +0200 |
commit | 22441dfbf83c845740e2834aca16e46a9ba75bae (patch) | |
tree | 80101e8d7d7d027130a2bce5ec74d6db1dbbc835 /http.c | |
parent | 170e1a8657cd806df13905bbab2c58365b3d62d0 (diff) | |
download | plop-22441dfbf83c845740e2834aca16e46a9ba75bae.zip plop-22441dfbf83c845740e2834aca16e46a9ba75bae.tar.gz plop-22441dfbf83c845740e2834aca16e46a9ba75bae.tar.bz2 |
Server now echoes received request
Diffstat (limited to 'http.c')
-rw-r--r-- | http.c | 29 |
1 files changed, 29 insertions, 0 deletions
@@ -1,6 +1,8 @@ #include "http.h" +#include <errno.h> #include <stdio.h> +#include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> @@ -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; +} |