aboutsummaryrefslogtreecommitdiff
path: root/server.c
diff options
context:
space:
mode:
authorSyndamia <kamen@syndamia.com>2023-12-07 13:23:13 +0200
committerSyndamia <kamen@syndamia.com>2023-12-07 13:23:13 +0200
commit6d647bdab78f2493d544638779340df26d1d7d67 (patch)
tree70d5b920b568cc7d66e2317f0504b8b0d27b2289 /server.c
parentb97ca5c6abab378f5224cb276fa5b19a598099c7 (diff)
downloadpico-web-6d647bdab78f2493d544638779340df26d1d7d67.tar
pico-web-6d647bdab78f2493d544638779340df26d1d7d67.tar.gz
pico-web-6d647bdab78f2493d544638779340df26d1d7d67.zip
[server] Proper file handling and added server log
Diffstat (limited to 'server.c')
-rw-r--r--server.c51
1 files changed, 41 insertions, 10 deletions
diff --git a/server.c b/server.c
index f162d88..e702a7a 100644
--- a/server.c
+++ b/server.c
@@ -19,13 +19,35 @@ struct se_vhost {
char *path_error;
};
-void on_connection(int fd_client, struct se_vhost *vhosts, int vhostsc) {
+char* constructFilePath(const char* root, const char* file) {
+ if (root == NULL || file == NULL) return NULL;
+
+ int rootLen = strlen(root), fileLen = strlen(file);
+
+ int rootEndOnSlash = root[rootLen - 1] == '/' || file[0] == '/';
+ int fileEndOnSlash = file[fileLen - 1] == '/';
+
+ int pathLen = rootLen + !rootEndOnSlash + fileLen + (fileEndOnSlash * 8) + 1;
+ char* path = malloc(pathLen * sizeof(char));
+ memset(path, 0, pathLen);
+ strncpy(path, root, rootLen);
+ if (!rootEndOnSlash) strcat(path, "/");
+ strcat(path, file);
+ if (fileEndOnSlash) strcat(path, "index.md");
+
+ return path;
+}
+
+void on_connection(const int fd_client, const struct se_vhost *vhosts, const int vhostsc) {
+ printf("[%d] Connected successfully!\n", fd_client);
+
char address[256];
read(fd_client, address, 256);
+ printf("[%d] Requested %s\n", fd_client, address);
int usernameLen = strchr(address, '@') - address;
- struct se_vhost *vhost = NULL;
+ const struct se_vhost *vhost = NULL;
for (int i = 0; i < vhostsc; i++) {
if (strncmp(vhosts[i].username, address, usernameLen) == 0) {
vhost = vhosts + i;
@@ -38,17 +60,24 @@ void on_connection(int fd_client, struct se_vhost *vhosts, int vhostsc) {
return;
}
- int filePathSize = strlen(vhost->path_root) + strlen(address + usernameLen + 1) + 1;
- char* filePath = malloc(filePathSize * sizeof(char));
- memset(filePath, 0, filePathSize);
- strncpy(filePath, vhost->path_root, strlen(vhost->path_root));
- strcat(filePath, address + usernameLen + 1);
-
- printf("%s\n", filePath);
+ char* filePath = constructFilePath(vhost->path_root, address + usernameLen + 1);
int fd = open(filePath, O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "[%d] Error opening %s\n", fd_client, filePath);
+
+ free(filePath);
+ filePath = constructFilePath(vhost->path_root, vhost->path_error);
+ fd = open(filePath, O_RDONLY);
+ if (fd < 0) {
+ fprintf(stderr, "[%d] Error opening %s\n", fd_client, filePath);
+ free(filePath);
+ return;
+ }
+ }
+
+ printf("[%d] Serving %s\n", fd_client, filePath);
free(filePath);
- herr(fd, "open");
char buff[256];
memset(buff, 0, sizeof(buff));
@@ -57,6 +86,8 @@ void on_connection(int fd_client, struct se_vhost *vhosts, int vhostsc) {
memset(buff, 0, sizeof(buff));
}
close(fd);
+
+ printf("[%d] Served!\n", fd_client);
}
int main(int argc, char* argv[]) {