diff options
author | cflip <cflip@cflip.net> | 2023-05-30 16:18:31 -0600 |
---|---|---|
committer | cflip <cflip@cflip.net> | 2023-05-30 16:18:31 -0600 |
commit | 40b66da398695590f82594bb92fd824bfdb44836 (patch) | |
tree | cedd9dbd2ecb647dc409405e7375192bd2bd62c5 | |
parent | 51c9a57339b499d3c0210dac26537ca14a01f995 (diff) |
Move request handling code out of net.c and into the main file
-rw-r--r-- | cfws.c | 45 | ||||
-rw-r--r-- | net.c | 50 | ||||
-rw-r--r-- | net.h | 10 |
3 files changed, 58 insertions, 47 deletions
@@ -4,27 +4,66 @@ #include <sys/socket.h> #include <unistd.h> +#include "file.h" +#include "http.h" #include "net.h" #define CFWS_DEFAULT_PORT 8080 +static void handle_request(const struct http_request *, int); + int main(int argc, char *argv[]) { int port = CFWS_DEFAULT_PORT; int serverfd, clientfd; + struct http_request request; - serverfd = initialize_server(port); + serverfd = net_init_server(port); if (serverfd == -1) return 1; printf("Serving a directory at localhost:%d\n", port); while (1) { - clientfd = accept(serverfd, NULL, NULL); - handle_connection(clientfd); + request = net_next_request(serverfd, &clientfd); + + handle_request(&request, clientfd); + + http_free_request(&request); close(clientfd); } close(serverfd); return 0; } + +static void handle_request(const struct http_request *req, int sockfd) +{ + char *filepath; + enum http_res_code res_code; + enum serve_method method; + + /* Find the local path for the resource and decide how to serve it. */ + filepath = file_path_for_uri(req->uri); + method = file_method_for_path(filepath, &res_code); + + /* Write the status line and (TODO) extra headers */ + http_response_statusline(res_code, sockfd); + + /* Use the chosen method to fill in the rest of the response */ + switch (method) { + case SERVE_METHOD_FILE: + file_read(filepath, sockfd); + break; + case SERVE_METHOD_PHP: + file_read_php(filepath, sockfd); + break; + case SERVE_METHOD_ERROR: { + const char *errmsg = "Content-Type: text/plain\r\n\r\nEpic fail"; + write(sockfd, errmsg, strlen(errmsg)); + break; + } + } + + free(filepath); +} @@ -10,10 +10,7 @@ #include <sys/types.h> #include <unistd.h> -#include "file.h" -#include "http.h" - -int initialize_server(int port) +int net_init_server(int port) { struct sockaddr_in addr; int rc; @@ -42,7 +39,7 @@ int initialize_server(int port) return -1; } - rc = listen(sockfd, CFWS_MAXCONN); + rc = listen(sockfd, CFWS_NET_MAXCONN); if (rc == -1) { perror("Failed to listen on server socket"); return -1; @@ -51,46 +48,19 @@ int initialize_server(int port) return sockfd; } -void handle_connection(int connfd) +struct http_request net_next_request(int serverfd, int *clientfd) { - char readbuf[CFWS_MAXREAD]; + int connfd; + char readbuf[CFWS_NET_MAXREAD]; struct http_request req; - char *filepath; - enum http_res_code res_code; - enum serve_method method; + connfd = accept(serverfd, NULL, NULL); /* Read and parse the HTTP request */ - memset(readbuf, 0, CFWS_MAXREAD); - read(connfd, readbuf, CFWS_MAXREAD - 1); + memset(readbuf, 0, CFWS_NET_MAXREAD); + read(connfd, readbuf, CFWS_NET_MAXREAD - 1); req = http_parse_request(readbuf); - /* Get the local file path for the given URI. */ - filepath = file_path_for_uri(req.uri); - - printf("GET %s : %s\n", req.uri, filepath); - - /* Determine the method that should be used to serve this file */ - method = file_method_for_path(filepath, &res_code); - - /* Write the HTTP response status and any required headers */ - http_response_statusline(res_code, connfd); - - /* Use the chosen method to fill in the rest of the response */ - switch (method) { - case SERVE_METHOD_FILE: - file_read(filepath, connfd); - break; - case SERVE_METHOD_PHP: - file_read_php(filepath, connfd); - break; - case SERVE_METHOD_ERROR: { - const char *errmsg = "Content-Type: text/plain\r\n\r\nEpic fail"; - write(connfd, errmsg, strlen(errmsg)); - break; - } - } - - free(filepath); - http_free_request(&req); + *clientfd = connfd; + return req; } @@ -1,10 +1,12 @@ #ifndef _H_NET #define _H_NET -#define CFWS_MAXCONN 10 /* Max connections allowed by listen(). */ -#define CFWS_MAXREAD 1024 /* Size of buffer used for reading from client. */ +#define CFWS_NET_MAXCONN 10 /* Max connections allowed by listen(). */ +#define CFWS_NET_MAXREAD 1024 /* Size of buffer used for reading from client. */ -int initialize_server(int); -void handle_connection(int); +#include "http.h" + +int net_init_server(int); +struct http_request net_next_request(int serverfd, int *clientfd); #endif |