quick commit
authorLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 23 May 2023 16:08:17 +0000 (18:08 +0200)
committerLaurent Mazet <laurent.mazet@thalesgroup.com>
Tue, 23 May 2023 16:08:17 +0000 (18:08 +0200)
file.c [new file with mode: 0644]
file.h [new file with mode: 0644]
http.c
http.h
webserver.c

diff --git a/file.c b/file.c
new file mode 100644 (file)
index 0000000..c720dba
--- /dev/null
+++ b/file.c
@@ -0,0 +1,36 @@
+#include <malloc.h>
+#include <stdio.h>
+#include <stdlib.h>
+
+#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 (file)
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 20301d6c8e3646540a641a8df7a4a4bcbea64c15..13223210cba9497a1a5cbd67caf66c58feca1889 100644 (file)
--- a/http.c
+++ b/http.c
@@ -3,6 +3,7 @@
 #include <time.h>
 
 #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 = "<html><head><title>Error 404</title></head><body><p>File not found</p></body></html>";
+    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/", "<html><head><title>Test</title></head><body><p>This a test</p></body></html>");
+        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 17db432b769eaf92c222a21f71910a91ad816934..4e6307fef043054037c39865b9ca2730d38bc8cd 100644 (file)
--- 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__ */
 
index 366973f9ae607636f498fd55c15e963640f06c02..b8c6e8ced64251dadfdfb92d38204d5cfcf21573 100644 (file)
@@ -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 <assert.h>
+#include <dirent.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <unistd.h>
@@ -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) {