summaryrefslogtreecommitdiff
path: root/gopher.c
diff options
context:
space:
mode:
Diffstat (limited to 'gopher.c')
-rw-r--r--gopher.c56
1 files changed, 56 insertions, 0 deletions
diff --git a/gopher.c b/gopher.c
new file mode 100644
index 0000000..c1cb2eb
--- /dev/null
+++ b/gopher.c
@@ -0,0 +1,56 @@
+#define _POSIX_C_SOURCE 200112L
+
+#include <sys/socket.h>
+
+#include <stdio.h>
+#include <string.h>
+
+#include "log.h"
+
+static char *res = "1Entry 1 /entry1 localhost 3000\r\n\
+1Entry 2 /entry2 localhost 3000\r\n\
+1Entry 3 /entry3 localhost 3000\r\n\
+.";
+
+static char *res1 = "0SubEntry 1 /entry1 localhost 3000\r\n\
+0SubEntry 2 /entry2 localhost 3000\r\n\
+0SubEntry 3 /entry3 localhost 3000\r\n\
+.";
+
+static int
+sendall(int fd, const char *buf, size_t len)
+{
+ size_t sent = 0;
+ ssize_t n;
+
+ while (sent < len) {
+ n = send(fd, buf + sent, len - sent, 0);
+ if (n == -1) {
+ perror("send");
+ return -1;
+ }
+ sent += n;
+ }
+
+ return 0;
+}
+
+int
+handle_path(int fd, const char *path)
+{
+ const char *data;
+
+ if (strcmp(path, "") == 0 || strcmp(path, "/") == 0) {
+ data = res;
+ } else if (strcmp(path, "/entry1") == 0) {
+ data = res1;
+ } else {
+ data = "";
+ }
+
+ if (sendall(fd, data, strlen(data)) == -1) {
+ logmsg(LOG_INFO, "failed to send data");
+ return -1;
+ }
+ return 0;
+}