summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-05-01 20:06:31 +0200
committerAki <please@ignore.pl>2020-05-01 20:06:31 +0200
commit22441dfbf83c845740e2834aca16e46a9ba75bae (patch)
tree80101e8d7d7d027130a2bce5ec74d6db1dbbc835
parent170e1a8657cd806df13905bbab2c58365b3d62d0 (diff)
downloadplop-22441dfbf83c845740e2834aca16e46a9ba75bae.zip
plop-22441dfbf83c845740e2834aca16e46a9ba75bae.tar.gz
plop-22441dfbf83c845740e2834aca16e46a9ba75bae.tar.bz2
Server now echoes received request
-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;