summaryrefslogtreecommitdiff
path: root/cfws.c
diff options
context:
space:
mode:
authorcflip <cflip@cflip.net>2023-05-30 16:18:31 -0600
committercflip <cflip@cflip.net>2023-05-30 16:18:31 -0600
commit40b66da398695590f82594bb92fd824bfdb44836 (patch)
treecedd9dbd2ecb647dc409405e7375192bd2bd62c5 /cfws.c
parent51c9a57339b499d3c0210dac26537ca14a01f995 (diff)
Move request handling code out of net.c and into the main file
Diffstat (limited to 'cfws.c')
-rw-r--r--cfws.c45
1 files changed, 42 insertions, 3 deletions
diff --git a/cfws.c b/cfws.c
index 0c96b99..9ccf7b2 100644
--- a/cfws.c
+++ b/cfws.c
@@ -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);
+}