diff options
| author | Syndamia <kamen@syndamia.com> | 2024-01-03 10:43:28 +0200 |
|---|---|---|
| committer | Syndamia <kamen@syndamia.com> | 2024-01-03 10:43:28 +0200 |
| commit | 0d63973a7186fc9ff2748a8cdf0f686e552cf1d9 (patch) | |
| tree | f8de70eac77fdd83964231972a5c454e2a68214e | |
| parent | c633a581d0154e55cfd4773729376afe7bb40d2f (diff) | |
| download | pico-web-0d63973a7186fc9ff2748a8cdf0f686e552cf1d9.tar pico-web-0d63973a7186fc9ff2748a8cdf0f686e552cf1d9.tar.gz pico-web-0d63973a7186fc9ff2748a8cdf0f686e552cf1d9.zip | |
[server-connection] Generalizing and reorganization
| -rw-r--r-- | server-connection.c | 118 |
1 files changed, 63 insertions, 55 deletions
diff --git a/server-connection.c b/server-connection.c index 6a89148..285cc1d 100644 --- a/server-connection.c +++ b/server-connection.c @@ -8,49 +8,10 @@ #include <string.h> #include <util.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 sanitizeAddress(char* address) { - /* Remove host and port */ - char* startPath = strchr(address, '/'); - if (startPath == NULL) - startPath = strchr(address, '\0'); - - char* startHost = strchr(address, '@'); - shiftLeft(startHost + 1, address - startHost, startPath - startHost - 1); - - /* Remove ../ */ - for (char* prev = startHost+1, *i = startHost+1; i != NULL && *i != '\0';) { - if (i[1] == '.' && i[2] == '.' && i[3] == '/') { - shiftLeft(prev, strlen(prev), i - prev + 3); - i = prev; - } - else { - prev = i; - i = strchr(i+1, '/'); - } - } -} - -int openError(sds* filePath, int* fd, const sds* vhost, const char* client, const int fd_client) { - 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 1; - } - return 0; -} +sds constructFilePath(const sds root, const char* file); +void sanitizeAddress(char* address); +sds* findVhost(char* address, sds** vhosts, const int vhostsc); +int openError(sds* filePath, int* fd, const sds* vhost, const char* client, const int fd_client); void on_connection(const char* client, const int fd_client, sds **vhosts, const int vhostsc) { printf("[%s@%d] Connected successfully!\n", client, fd_client); @@ -63,24 +24,15 @@ void on_connection(const char* client, const int fd_client, sds **vhosts, const sanitizeAddress(address); 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; - } - } - + /* Is the username connected with any file path? */ + const sds *vhost = findVhost(address, vhosts, vhostsc); 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); + sds filePath = constructFilePath(vhost[vh_path], strchr(address, '@') + 1); int fd = 0; @@ -123,3 +75,59 @@ void on_connection(const char* client, const int fd_client, sds **vhosts, const close(fd); printf("[%s@%d] Served!\n", client, fd_client); } + +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 sanitizeAddress(char* address) { + /* Remove host and port */ + char* startPath = strchr(address, '/'); + if (startPath == NULL) + startPath = strchr(address, '\0'); + + char* startHost = strchr(address, '@'); + shiftLeft(startHost + 1, address - startHost, startPath - startHost - 1); + + /* Remove ../ */ + for (char* prev = startHost+1, *i = startHost+1; i != NULL && *i != '\0';) { + if (i[1] == '.' && i[2] == '.' && i[3] == '/') { + shiftLeft(prev, strlen(prev), i - prev + 3); + i = prev; + } + else { + prev = i; + i = strchr(i+1, '/'); + } + } +} + +sds* findVhost(char* address, sds** vhosts, const int vhostsc) { + sds* vhost = NULL; + int usernameLen = strchr(address, '@') - address; + for (int i = 0; i < vhostsc; i++) { + if (strncmp(vhosts[i][vh_user], address, usernameLen) == 0) { + vhost = *vhosts + i; + break; + } + } + return vhost; +} + +int openError(sds* filePath, int* fd, const sds* vhost, const char* client, const int fd_client) { + 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 1; + } + return 0; +} |
