From 1c79bf5f79365739fd07f08e0d21e973537fc5ec Mon Sep 17 00:00:00 2001 From: nirav Date: Sat, 21 Sep 2019 13:30:12 +0000 Subject: Add gph parser and client --- gopher.c | 113 ++++++++++++++++++++++++++++++++++++++++++++++++++++----------- 1 file changed, 93 insertions(+), 20 deletions(-) (limited to 'gopher.c') diff --git a/gopher.c b/gopher.c index c1cb2eb..32f1513 100644 --- a/gopher.c +++ b/gopher.c @@ -1,23 +1,18 @@ -#define _POSIX_C_SOURCE 200112L - #include +#include +#include #include #include +#include #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\ -."; +#define GOPHERROOT "/tmp/gopherroot" +#define GOPHERHOST "localhost" +#define GOPHERPORT "70" -static int +static int sendall(int fd, const char *buf, size_t len) { size_t sent = 0; @@ -35,22 +30,100 @@ sendall(int fd, const char *buf, size_t len) return 0; } -int +static int +print_indexfile(int fd, const char *path) +{ + FILE *f; + char buf[512]; + + f = fopen(path, "r"); + if (f == NULL) { + perror("fopen"); + return -1; + } + while (!feof(f)) { + if (fgets(buf, sizeof(buf), f) == NULL) + continue; + if (sendall(fd, buf, strlen(buf)) == -1) { + logmsg(LOG_INFO, "failed to send data"); + return -1; + fclose(f); + } + } + + fclose(f); + return 0; +} + +static int +print_dir(int fd, const char *path) +{ + DIR *root; + struct dirent *node; + char *buf; + int type; + size_t buflen; + + root = opendir(path); + if (root == NULL) { + perror("opendir"); + return -1; + } + while ((node = readdir(root)) != NULL) { + buflen = sizeof(type) + strlen(node->d_name) + + strlen(GOPHERROOT) + strlen(GOPHERHOST) + + strlen(GOPHERPORT) + 6; + if ((buf = malloc(buflen)) == NULL) { + perror("malloc"); + return -1; + } + + switch (node->d_type) { + case 4: + type = 1; + break; + case 8: + type = 0; + break; + } + + sprintf(buf, "%d%s %s %s %s\r\n", + type, node->d_name, GOPHERROOT, "localhost", "3000"); + if (sendall(fd, buf, buflen) == -1) { + logmsg(LOG_INFO, "failed to send data"); + free(buf); + return -1; + } + free(buf); + } + + closedir(root); + + 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; + if (print_indexfile(fd, "index.gph") == -1) { + logmsg(LOG_INFO, "failed to send data"); + return -1; + } + } else if (strcmp(path, "/list") == 0) { + if (print_dir(fd, GOPHERROOT) == -1) { + logmsg(LOG_INFO, "failed to send data"); + return -1; + } } else { data = ""; + if (sendall(fd, data, strlen(data)) == -1) { + logmsg(LOG_INFO, "failed to send data"); + return -1; + } } - if (sendall(fd, data, strlen(data)) == -1) { - logmsg(LOG_INFO, "failed to send data"); - return -1; - } return 0; } -- cgit v1.2.3