#include #include #include #include #include #include #include "log.h" #include "server.h" #define BUF_CAP 80000 #define COMMON_CAP 1024 #define PORT "8079" #define RM_LF(str) do { \ signed long len = strlen(str)-1; \ if(len >= 0 && str[len] == '\n') str[len] = '\0'; \ } while(0) #define _SEND_BUF_ADD(str) do { \ memcpy(&send_buf[send_buf_sz], str, strlen(str)); \ send_buf_sz += strlen(str); \ } while(0) #define _SEND_BUF_ADD_LINE(str) do { \ _SEND_BUF_ADD(str); \ _SEND_BUF_ADD("\r\n"); \ } while(0) #define SEND_BUF_ADD(str) do { \ memcpy(&send_buf[*send_buf_sz], str, strlen(str)); \ *send_buf_sz += strlen(str); \ } while(0) #define SEND_BUF_ADD_LINE(str) do { \ SEND_BUF_ADD(str); \ SEND_BUF_ADD("\r\n"); \ } while(0) #define LOAD_FILE() do { \ if(is_binary) { \ fseek(fp, 0, SEEK_END); \ size_t file_sz = ftell(fp); \ rewind(fp); \ \ fread(&send_buf[*send_buf_sz], sizeof(char), file_sz, fp); \ *send_buf_sz += file_sz; \ } else { \ char line[BUF_CAP]; \ while(fgets(line, sizeof(line), fp) != NULL) \ { \ RM_LF(line); \ SEND_BUF_ADD_LINE(line); \ } \ } \ } while(0) static int get_content_type(char *file_path, char *content_type) { char cmd[COMMON_CAP + 64]; sprintf(cmd, "/usr/bin/file -ib %s", file_path); FILE *fp = popen(cmd, "r"); if(!fp) { err("popen: cmd %s: %s", cmd, strerror(errno)); return 1; } if(fgets(content_type, COMMON_CAP, fp) == NULL) { err("get_mime_type: no output from fgets"); return 1; } pclose(fp); RM_LF(content_type); char ct_cpy[COMMON_CAP]; strcpy(ct_cpy, content_type); strtok(ct_cpy, "="); // the only thing after the '=' should be the either binary or something else (text) char *charset = strtok(NULL, "="); if(charset == NULL || strcmp(charset, "binary") != 0) { return 0; } return -1; } static int try_file(char *req_path, FILE **fp, char *content_type) { char file_path[COMMON_CAP]; sprintf(file_path, "%s%s", FILES, (strlen(req_path) == 1) ? "/index.html" : req_path); *fp = fopen(file_path, "r"); if(!(*fp)) { err("fopen: file %s: %s", file_path, strerror(errno)); return 1; } info("fopen: file %s was opened", file_path); return get_content_type(file_path, content_type); } static int on_get(char *send_buf, ssize_t *send_buf_sz, char *req_path) { char content_type[COMMON_CAP]; int is_binary; // 0 - no; -1 - yes; 1 - error FILE *fp; if((is_binary = try_file(req_path, &fp, content_type)) == 1) { if(errno == 2) { // 2 is 'No such file or directory' SEND_BUF_ADD_LINE("404"); if((is_binary = try_file("/404.html", &fp, content_type)) != 0) return 1; } else { SEND_BUF_ADD_LINE("500"); if((is_binary = try_file("/500.html", &fp, content_type)) != 0) return 1; } } else { SEND_BUF_ADD_LINE("200"); } SEND_BUF_ADD_LINE("Server: potato"); SEND_BUF_ADD("Content-Type: "); SEND_BUF_ADD_LINE(content_type); SEND_BUF_ADD("\r\n"); LOAD_FILE(); fclose(fp); return 0; } static int handle_connection(sock_t *conn) { char recv_buf[BUF_CAP] = {0}; ssize_t recv_buf_len = server_recv(conn, recv_buf, BUF_CAP); if(recv_buf_len == 0) { return 0; } else if(recv_buf_len < 0) { err("server_recv: %s", strerror(errno)); return 1; } /* HANDLE REQUEST */ char send_buf[BUF_CAP]; ssize_t send_buf_sz = 0; _SEND_BUF_ADD("HTTP/1.1 "); char recv_buf_cpy[BUF_CAP]; memcpy(recv_buf_cpy, recv_buf, recv_buf_len); char *req_method = strtok(recv_buf_cpy, " "); if(strcmp(req_method, "GET") == 0) { if(on_get(send_buf, &send_buf_sz, strtok(NULL, " "))) { err("on_get: failed"); return 1; } } else { err("request method %s has not been implemented", req_method); return 1; } // TODO: handle when the whole message is not sent ssize_t sent_sz = server_send(conn, send_buf, send_buf_sz); if(sent_sz < 0) { err("server_send: %s", strerror(errno)); return 1; } info("server_send: sent %ld out of %ld bytes", sent_sz, send_buf_sz); return 0; } int main(void) { signal(SIGCHLD, SIG_IGN); // now i dont have to wait() sock_t *sock = server_sock_create(); if(server_start(PORT, sock) != 0) { err("server_init: failed"); return 1; } for(int i = 0; ; i++) { sock_t *conn = server_sock_create(); if(server_accept(sock, conn) != 0) { err("server_accept: failed"); } info("server: got connection with %s", server_connection_ip(conn)); pid_t p = fork(); if(p == 0) { server_sock_close(sock); if(handle_connection(conn) != 0) { err("handle_connection: failed"); } else { info("server: got disconnected with %s", server_connection_ip(conn)); } server_sock_close(conn); return 0; } else if (p < 0){ err("fork: %s", strerror(errno)); return 1; } server_sock_close(conn); } server_sock_close(sock); return 0; }