summaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorAki <please@ignore.pl>2020-05-01 17:22:20 +0200
committerAki <please@ignore.pl>2020-05-01 17:28:48 +0200
commit6997e142fd46c114e7d7bb6cc0da1fbf2ff172cc (patch)
treea45622efd7dc78f67b22bbcfb239bd2f31fb31d3
parent0ce61f57957d99d43e77514fa62a5c72f953b2ce (diff)
downloadplop-6997e142fd46c114e7d7bb6cc0da1fbf2ff172cc.zip
plop-6997e142fd46c114e7d7bb6cc0da1fbf2ff172cc.tar.gz
plop-6997e142fd46c114e7d7bb6cc0da1fbf2ff172cc.tar.bz2
Added back response with a body
-rw-r--r--http.c24
-rw-r--r--http.h1
-rw-r--r--plop.c4
3 files changed, 28 insertions, 1 deletions
diff --git a/http.c b/http.c
index dc0e9c8..978af38 100644
--- a/http.c
+++ b/http.c
@@ -1,6 +1,9 @@
#include "http.h"
#include <stdio.h>
+#include <string.h>
+#include <sys/socket.h>
+#include <sys/types.h>
const char * method_str[] = {
[METHOD_GET] = "GET",
@@ -35,3 +38,24 @@ int respond_only_status(const int fd, const enum status status)
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
+/// \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)
+{
+ static const char * pattern =
+ "HTTP/1.1 %s\r\n"
+ "Connection: close\r\n"
+ "Content-Type: text/plain\r\n"
+ "\r\n";
+
+ if (0 > dprintf(fd, pattern, status_str[status]))
+ {
+ return -1; // TODO: Handle errors properly
+ }
+
+ return send(fd, body, strlen(body), 0);
+}
diff --git a/http.h b/http.h
index f92f7fc..88a793d 100644
--- a/http.h
+++ b/http.h
@@ -29,3 +29,4 @@ extern const char * method_str[];
extern const char * status_str[];
int respond_only_status(int, enum status);
+int respond_with_body(int, enum status, const char *);
diff --git a/plop.c b/plop.c
index 16f486c..794715a 100644
--- a/plop.c
+++ b/plop.c
@@ -85,7 +85,9 @@ int handle_client(struct pollfd * pfd, const int shift_by)
return -1; // TODO: Handle errors properly
}
- if (-1 == respond_only_status(pfd->fd, STATUS_NOT_IMPLEMENTED))
+ static const char * body = "plop!";
+
+ if (-1 == respond_with_body(pfd->fd, STATUS_OK, body))
{
// TODO: Handle errors properly
}