diff options
| -rw-r--r-- | Makefile | 4 | ||||
| -rw-r--r-- | server-connection.c | 76 | ||||
| -rw-r--r-- | server-connection.h | 12 | ||||
| -rw-r--r-- | server.c | 62 |
4 files changed, 92 insertions, 62 deletions
@@ -3,8 +3,8 @@ all: server browser .PHONY: server server: - gcc -o server -I. sds/sds.c util.c server.c + gcc -g -o server -I. sds/sds.c util.c server.c server-connection.c .PHONY: browser browser: - gcc -o browser -I. sds/sds.c util.c browser.c + gcc -g -o browser -I. sds/sds.c util.c browser.c diff --git a/server-connection.c b/server-connection.c new file mode 100644 index 0000000..f0ac1e6 --- /dev/null +++ b/server-connection.c @@ -0,0 +1,76 @@ +#include <server-connection.h> + +#include <stdio.h> +#include <unistd.h> +#include <fcntl.h> + +#include <string.h> + +sds constructFilePath(const sds root, const char* file) { + sds path = sdsdup(root); + if (root[sdslen(root)-1] != '/' && file[0] != '/') + path = sdscat(path, "/"); + path = sdscat(path, file); + if (file[strlen(file)-1] == '/') + path = sdscat(path, "index.md"); + return path; +} + +void on_connection(const char* client, const int fd_client, sds **vhosts, const int vhostsc) { + printf("[%s@%d] Connected successfully!\n", client, fd_client); + + /* Get address request */ + char address[256]; + memset(address, 0, 256); + + read(fd_client, address, 256); + printf("[%s@%d] Requested %s\n", client, fd_client, address); + + /* Does vhosts contain an address with the username? */ + int usernameLen = strchr(address, '@') - address; + + const sds *vhost = NULL; + for (int i = 0; i < vhostsc; i++) { + if (strncmp(vhosts[i][vh_user], address, usernameLen) == 0) { + vhost = *vhosts + i; + break; + } + } + + if (vhost == NULL) { + fprintf(stderr, "[%s@%d] Unknown username in address %s\n", client, fd_client, address); + return; + } + + /* Try to open the requested file or the error file */ + sds filePath = constructFilePath(vhost[vh_path], address + usernameLen + 1); + + int fd = open(filePath, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "[%s@%d] Error opening %s\n", client, fd_client, filePath); + + sdsfree(filePath); + filePath = constructFilePath(vhost[vh_path], vhost[vh_error]); + fd = open(filePath, O_RDONLY); + if (fd < 0) { + fprintf(stderr, "[%s@%d] Error opening %s\n", client, fd_client, filePath); + sdsfree(filePath); + return; + } + } + + /* Send the file to the client */ + printf("[%s@%d] Serving %s\n", client, fd_client, filePath); + sdsfree(filePath); + + char buff[256]; + memset(buff, 0, sizeof(buff)); + while (read(fd, buff, 256)) { + write(fd_client, buff, strlen(buff)); + memset(buff, 0, sizeof(buff)); + } + + /* Finalize */ + close(fd); + printf("[%s@%d] Served!\n", client, fd_client); +} diff --git a/server-connection.h b/server-connection.h new file mode 100644 index 0000000..71447ff --- /dev/null +++ b/server-connection.h @@ -0,0 +1,12 @@ +#ifndef H_SERVER_CONNECTION +#define H_SERVER_CONNECTION + +#include <sds/sds.h> + +#define vh_user 0 +#define vh_path 1 +#define vh_error 2 + +void on_connection(const char* client, const int fd_client, sds **vhosts, const int vhostsc); + +#endif @@ -1,5 +1,7 @@ /* The server recieves connections and passes files to the clients */ +#include <server-connection.h> + #include <sys/socket.h> #include <netinet/in.h> #include <arpa/inet.h> @@ -14,9 +16,6 @@ #include <sds/sds.h> #include <util.h> -#define username 0 -#define path_root 1 -#define path_error 2 sds constructFilePath(const sds root, const char* file) { sds path = sdsdup(root); @@ -28,63 +27,6 @@ sds constructFilePath(const sds root, const char* file) { return path; } -void on_connection(const char* client, const int fd_client, sds **vhosts, const int vhostsc) { - printf("[%s@%d] Connected successfully!\n", client, fd_client); - - /* Get address request */ - char address[256]; - read(fd_client, address, 256); - printf("[%s@%d] Requested %s\n", client, fd_client, address); - - /* Does vhosts contain an address with the username? */ - int usernameLen = strchr(address, '@') - address; - - const sds *vhost = NULL; - for (int i = 0; i < vhostsc; i++) { - if (strncmp(vhosts[i][username], address, usernameLen) == 0) { - vhost = *vhosts + i; - break; - } - } - - if (vhost == NULL) { - fprintf(stderr, "[%s@%d] Unknown username in address %s\n", client, fd_client, address); - return; - } - - /* Try to open the requested file or the error file */ - sds filePath = constructFilePath(vhost[path_root], address + usernameLen + 1); - - int fd = open(filePath, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "[%s@%d] Error opening %s\n", client, fd_client, filePath); - - sdsfree(filePath); - filePath = constructFilePath(vhost[path_root], vhost[path_error]); - fd = open(filePath, O_RDONLY); - if (fd < 0) { - fprintf(stderr, "[%s@%d] Error opening %s\n", client, fd_client, filePath); - sdsfree(filePath); - return; - } - } - - /* Send the file to the client */ - printf("[%s@%d] Serving %s\n", client, fd_client, filePath); - sdsfree(filePath); - - char buff[256]; - memset(buff, 0, sizeof(buff)); - while (read(fd, buff, 256)) { - write(fd_client, buff, strlen(buff)); - memset(buff, 0, sizeof(buff)); - } - - /* Finalize */ - close(fd); - printf("[%s@%d] Served!\n", client, fd_client); -} - void freeVhosts(sds **vhosts, int argc) { for (int i = 1; i < argc; i++) { |
