From 4baf6839119f5e805e1f41b4fc89fff96a52c833 Mon Sep 17 00:00:00 2001 From: Laurent Mazet Date: Tue, 23 May 2023 18:08:17 +0200 Subject: [PATCH] quick commit --- file.c | 36 ++++++++++++++++++++++++++++++++++++ file.h | 8 ++++++++ http.c | 36 +++++++++++++++++++++++++++++++----- http.h | 2 +- webserver.c | 28 +++++++++++++++++++++++++--- 5 files changed, 101 insertions(+), 9 deletions(-) create mode 100644 file.c create mode 100644 file.h diff --git a/file.c b/file.c new file mode 100644 index 0000000..c720dba --- /dev/null +++ b/file.c @@ -0,0 +1,36 @@ +#include +#include +#include + +#include "debug.h" + +#include "file.h" + +/* read full file */ + +int readfile (unsigned char **buffer, char *filename) +{ + + /* open file */ + + FILE *fd = fopen (filename, "rb"); + if (fd == NULL) { + VERBOSE (WARNING, PRINT ("Can't open file (%s)\n", filename)); + return -1; + } + + fseek (fd, 0, SEEK_END); + int size = ftell (fd); + fseek (fd, 0, SEEK_SET); /* same as rewind(f); */ + + /* read full file */ + + *buffer = calloc (size + 1, 1); + fread (*buffer, size, 1, fd); + fclose (fd); + + *buffer[size] = 0; + return size; +} + +/* vim: set ts=4 sw=4 et: */ diff --git a/file.h b/file.h new file mode 100644 index 0000000..165c276 --- /dev/null +++ b/file.h @@ -0,0 +1,8 @@ +#ifndef __FILE_H__ +#define __FILE_H__ + +int readfile (unsigned char **buffer, char *filename); + +#endif /* __FILE_H__ */ + +/* vim: set ts=4 sw=4 et: */ diff --git a/http.c b/http.c index 20301d6..1322321 100644 --- a/http.c +++ b/http.c @@ -3,6 +3,7 @@ #include #include "debug.h" +#include "file.h" #include "http.h" @@ -202,6 +203,18 @@ int error_400 (char **buffer) return add_entity (buffer, (unsigned char *)response, strlen (response), "text/html", "iso-8859-1"); } +/* error 404 */ + +int error_404 (char **buffer, char *uri) +{ + add_status_line (buffer, c404); + add_general_header (buffer); + add_response_header (buffer, uri); + + char *response = "Error 404

File not found

"; + return add_entity (buffer, (unsigned char *)response, strlen (response), "text/html", "iso-8859-1"); +} + /* response html */ int response_html (char **buffer, char *location, char *response) @@ -227,7 +240,7 @@ char *trim (char *str) /* main HTTP processing */ -int processing (char *data, int len, char **pdata) +int processing (char *data, int len, char *root, char **pdata) { char *saved_data = data; VERBOSE (DEBUG, PRINT ("Start processing\n")); @@ -260,6 +273,11 @@ int processing (char *data, int len, char **pdata) } VERBOSE (INFO, PRINT ("%s %s (%s)\n", (type == get_e) ? "Get" : (type == head_e) ? "Head" : "Post", uri, version)); + /* analyse uri */ + char *filename = calloc (strlen (root) + strlen (uri) + 2, 1); + //sprintf (filename, "%s%s%s", root, ((root[strlen (root) - 1] != '/') && (uri[0] != '/')) ? "/" : "", uri); + sprintf (filename, "%s/%s", root, uri); + /* check header */ header_t header = {0}; while (strcmp (line = find_sequence (data, len, "\r\n", &data), "") != 0) { @@ -315,13 +333,17 @@ int processing (char *data, int len, char **pdata) VERBOSE (DEBUG, PRINT ("Processing data: %s\n", data)); } - /* cleaning */ - free (saved_data); - /* response */ + char *buffer = NULL; switch (type) { case get_e: - len = response_html (pdata, "http://localhost/", "Test

This a test

"); + len = readfile ((unsigned char **)&buffer, filename); + if (len == 0) { + len = error_404 (pdata, "http://localhost/"); + } else { + len = response_html (pdata, "http://localhost/", buffer); + free (buffer); + } break; case head_e: break; @@ -331,6 +353,10 @@ int processing (char *data, int len, char **pdata) break; } + /* cleaning */ + free (saved_data); + free (filename); + return len; } diff --git a/http.h b/http.h index 17db432..4e6307f 100644 --- a/http.h +++ b/http.h @@ -1,7 +1,7 @@ #ifndef __HTTP_H__ #define __HTTP_H__ -int processing (char *data, int len, char **pdata); +int processing (char *data, int len, char *root, char **pdata); #endif /* __HTTP_H__ */ diff --git a/webserver.c b/webserver.c index 366973f..b8c6e8c 100644 --- a/webserver.c +++ b/webserver.c @@ -1,8 +1,9 @@ /* depend: */ /* cflags: */ -/* linker: color.o debug.o http.o server.o */ +/* linker: color.o debug.o file.o http.o server.o */ #include +#include #include #include #include @@ -16,6 +17,7 @@ /* constants */ #define BUFFER_SIZE 4096 +#define ROOT_DIR "webroot" /* macros */ @@ -23,6 +25,7 @@ char *progname = NULL; int port = 8080; +char *root = ROOT_DIR; /* help function */ @@ -32,6 +35,7 @@ int usage (int ret) fprintf (fid, "usage: %s\n", progname); fprintf (fid, " -h : help message\n"); fprintf (fid, " -p : port number (%d)\n", port); + fprintf (fid, " -r : web root directory (%s)\n", root); fprintf (fid, " -v : verbose level (%d)\n", verbose); return ret; @@ -57,7 +61,7 @@ int main (int argc, char *argv[]) /* argument processing */ - while (argc-- > 1) { + while (argc-- > 1) { char *arg = *(++argv); if (arg[0] != '-') { VERBOSE (ERROR, PERROR ("%s: invalid option -- '%s'\n", progname, arg); usage (1)); @@ -77,6 +81,14 @@ int main (int argc, char *argv[]) return 1; } break; + case 'r': + arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; + if (arg == NULL) { + VERBOSE (ERROR, PERROR ("%s: missing directory name\n", progname); usage (1)); + return 1; + } + root = arg; + break; case 'v': arg = (arg[2]) ? arg + 2 : (--argc > 0) ? *(++argv) : NULL; if (arg == NULL) { @@ -91,6 +103,16 @@ int main (int argc, char *argv[]) } } + /* check root directory */ + VERBOSE (DEBUG, PRINT ("Check web root\n")); + DIR *pdir = opendir (root); + if (pdir == NULL) { + VERBOSE (ERROR, PERROR ("Can't read directory (%s)\n", root)); + return 1; + } + closedir (pdir); + + /* init network stack */ VERBOSE (DEBUG, PRINT ("Initializing socket\n")); init_network_context (); if (open_listening_socket (port) == 0) { @@ -120,7 +142,7 @@ int main (int argc, char *argv[]) // processing VERBOSE (DEBUG, PRINT ("Processing %s\n", data)); - len = processing ((char *)data, len, (char **)&data); + len = processing ((char *)data, len, root, (char **)&data); int rc = send_data (data, len); if (rc == 0) { -- 2.30.2