summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
-rw-r--r--Makefile2
-rw-r--r--http.c27
-rw-r--r--http.h30
-rw-r--r--plop.c10
4 files changed, 61 insertions, 8 deletions
diff --git a/Makefile b/Makefile
index 75c8fba..48c0901 100644
--- a/Makefile
+++ b/Makefile
@@ -1,6 +1,6 @@
CFLAGS+=-Wall -Wextra -Wpedantic
-plop: plop.o
+plop: plop.o http.o http.h
clean:
rm -f plop *.o
diff --git a/http.c b/http.c
new file mode 100644
index 0000000..706b982
--- /dev/null
+++ b/http.c
@@ -0,0 +1,27 @@
+#include "http.h"
+
+#include <stdio.h>
+
+const char * status_string[] = {
+ [STATUS_OK] = "200 OK",
+ [STATUS_BAD_REQUEST] = "400 Bad Request",
+ [STATUS_METHOD_NOT_ALLOWED] = "405 Method Not Allowed",
+ [STATUS_REQUEST_TIMEOUT] = "408 Request Timeout",
+ [STATUS_INTERNAL_SERVER_ERROR] = "500 Internal Server Error",
+ [STATUS_NOT_IMPLEMENTED] = "501 Not Implemented",
+ [STATUS_VERSION_NOT_SUPPORTED] = "505 Version Not Supported",
+};
+
+/// Sends a simple response only with a status to the client.
+/// \param fd File descriptor of the client socket
+/// \param status HTTP response status code
+/// \return Negative value if an error was encountered; numbers of bytes written otherwise
+int respond_only_status(const int fd, const enum status status)
+{
+ static const char * pattern =
+ "HTTP/1.1 %s\r\n"
+ "Connection: close\r\n"
+ "\r\n";
+
+ return dprintf(fd, pattern, status_string[status]);
+}
diff --git a/http.h b/http.h
new file mode 100644
index 0000000..cef3048
--- /dev/null
+++ b/http.h
@@ -0,0 +1,30 @@
+enum method
+{
+ METHOD_GET,
+ METHOD_HEAD,
+ METHOD_POST,
+ METHOD_PUT,
+ METHOD_DELETE,
+ METHOD_OPTIONS,
+ METHOD_PATCH,
+};
+
+struct request
+{
+ enum method method;
+};
+
+enum status
+{
+ STATUS_OK = 200,
+ STATUS_BAD_REQUEST = 400,
+ STATUS_METHOD_NOT_ALLOWED = 405,
+ STATUS_REQUEST_TIMEOUT = 408,
+ STATUS_INTERNAL_SERVER_ERROR = 500,
+ STATUS_NOT_IMPLEMENTED = 501,
+ STATUS_VERSION_NOT_SUPPORTED = 505,
+};
+
+extern const char * status_str[];
+
+int respond_only_status(int, enum status);
diff --git a/plop.c b/plop.c
index bc30138..16f486c 100644
--- a/plop.c
+++ b/plop.c
@@ -7,6 +7,8 @@
#include <sys/types.h>
#include <unistd.h>
+#include "http.h"
+
/// Tries to create, bind and start listening on INET server socket.
/// \param node Hostname
/// \param service Port
@@ -83,13 +85,7 @@ int handle_client(struct pollfd * pfd, const int shift_by)
return -1; // TODO: Handle errors properly
}
- static const char * response =
- "HTTP/1.1 200 OK\r\n"
- "Content-Type: text/plain; charset=utf-8\r\n"
- "\r\n"
- "plop\n";
-
- if (-1 == send(pfd->fd, response, strlen(response), 0))
+ if (-1 == respond_only_status(pfd->fd, STATUS_NOT_IMPLEMENTED))
{
// TODO: Handle errors properly
}