diff options
-rw-r--r-- | http.c | 29 | ||||
-rw-r--r-- | http.h | 2 | ||||
-rw-r--r-- | plop.c | 8 |
3 files changed, 37 insertions, 2 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; +} @@ -15,6 +15,7 @@ enum method struct request { enum method method; + char * body; }; enum status @@ -33,5 +34,6 @@ extern const char * status_str[]; int respond_only_status(int, enum status); int respond_with_body(int, enum status, const char *); +int parse_request(int, struct request *); #endif // HTTP_H @@ -2,6 +2,7 @@ #include <fcntl.h> #include <netdb.h> #include <poll.h> +#include <stdlib.h> #include <string.h> #include <sys/socket.h> #include <sys/types.h> @@ -85,13 +86,16 @@ int handle_client(struct pollfd * pfd, const int shift_by) return -1; // TODO: Handle errors properly } - static const char * body = "plop!"; + struct request request = {0}; + parse_request(pfd->fd, &request); - if (-1 == respond_with_body(pfd->fd, STATUS_OK, body)) + if (-1 == respond_with_body(pfd->fd, STATUS_OK, request.body)) { // TODO: Handle errors properly } + free(request.body); + close(pfd->fd); (pfd - shift_by)->fd = -1; (pfd - shift_by)->events = pfd->events; |