summaryrefslogtreecommitdiff
path: root/gopher.c
diff options
context:
space:
mode:
authornirav <nirav@teisuu.com>2019-09-19 15:36:02 +0000
committernirav <nirav@teisuu.com>2019-09-19 15:44:14 +0000
commitd5f4022408249c5a513ae795b7b360854bd75a6c (patch)
treead20cbe6aba24649647b7aa0520f25392571aeeb /gopher.c
parente985bfa6b8facac58da9a20c3c840498e2517bb5 (diff)
downloadgopherd-d5f4022408249c5a513ae795b7b360854bd75a6c.tar.gz
gopherd-d5f4022408249c5a513ae795b7b360854bd75a6c.zip
Update
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;
+}