summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--http.c29
-rw-r--r--http.h2
-rw-r--r--plop.c8
3 files changed, 37 insertions, 2 deletions
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 <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;
+}
diff --git a/http.h b/http.h
index 0cee2ab..5a9283e 100644
--- a/http.h
+++ b/http.h
@@ -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
diff --git a/plop.c b/plop.c
index 794715a..9e44ab7 100644
--- a/plop.c
+++ b/plop.c
@@ -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;