summaryrefslogtreecommitdiffhomepage
path: root/response.c
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-08-15 02:06:51 +0200
committerAki <please@ignore.pl>2020-08-15 02:06:51 +0200
commitd90d0050c2ee91d11fce3a5fd45c5fb64018392c (patch)
treeb1950c053f8c7a66d2722cf095733b0a7ec0dd9e /response.c
parentc8ddc4b5ad62e7ec90f25c6f647bf1caf38f80e6 (diff)
downloadplop-d90d0050c2ee91d11fce3a5fd45c5fb64018392c.zip
plop-d90d0050c2ee91d11fce3a5fd45c5fb64018392c.tar.gz
plop-d90d0050c2ee91d11fce3a5fd45c5fb64018392c.tar.bz2
Split http into request and response headers
Diffstat (limited to 'response.c')
-rw-r--r--response.c43
1 files changed, 43 insertions, 0 deletions
diff --git a/response.c b/response.c
new file mode 100644
index 0000000..df465d0
--- /dev/null
+++ b/response.c
@@ -0,0 +1,43 @@
+#include "response.h"
+
+#include <stdio.h>
+#include <unistd.h>
+
+#include "http.h"
+
+/// 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_str[status]);
+}
+
+/// Sends a response with a status and a body to the client.
+/// \param fd File descriptor of the client socket
+/// \param status HTTP response status code
+/// \param body Content that will be sent
+/// \param size Size of the content in bytes
+/// \return Negative value if an error was encountered; numbers of bytes written otherwise
+int respond_with_body(const int fd, const enum status status, const char * body, const int size)
+{
+ static const char * pattern =
+ "HTTP/1.1 %s\r\n"
+ "Connection: close\r\n"
+ "Content-Type: application/json\r\n"
+ "Content-Size: %d\r\n"
+ "\r\n";
+
+ if (0 > dprintf(fd, pattern, status_str[status], size))
+ {
+ return -1; // TODO: Handle errors properly
+ }
+
+ return write(fd, body, size);
+}