implement head method
authorLaurent Mazet <mazet@softndesign.org>
Fri, 26 May 2023 22:11:17 +0000 (00:11 +0200)
committerLaurent Mazet <mazet@softndesign.org>
Fri, 26 May 2023 22:11:17 +0000 (00:11 +0200)
http.c
webserver.c

diff --git a/http.c b/http.c
index c17781fc76b3d24e8e6f9dc73b642e1284a8adc0..f0e53afe908f511a7724546750fb5fbef265d5c7 100644 (file)
--- a/http.c
+++ b/http.c
@@ -349,46 +349,29 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
         }
         
         VERBOSE (DEBUG, PRINT ("Analyse field\n"));
-        if (strcmp (field, "Allow") == 0) {
-            header.allow = value;
-        } else if (strcmp (field, "Authorization") == 0) {
-            header.authorization = value;
-        } else if (strcmp (field, "Content-Encoding") == 0) {
-            header.content_encoding = value;
-        } else if (strcmp (field, "Content-Length") == 0) {
-            header.content_length = value;
-        } else if (strcmp (field, "Content-Type") == 0) {
-            header.content_type = value;
-        } else if (strcmp (field, "Date") == 0) {
-            header.date = value;
-        } else if (strcmp (field, "Expires") == 0) {
-            header.expires = value;
-        } else if (strcmp (field, "From") == 0) {
-            header.from = value;
-        } else if (strcmp (field, "If-Modified-Since") == 0) {
-            header.if_modified_since = value;
-        } else if (strcmp (field, "Last-Modified") == 0) {
-            header.last_modified = value;
-        } else if (strcmp (field, "Location") == 0) {
-            header.location = value;
-        } else if (strcmp (field, "Pragma") == 0) {
-            header.pragma = value;
-        } else if (strcmp (field, "Referer") == 0) {
-            header.referer = value;
-        } else if (strcmp (field, "Server") == 0) {
-            header.server = value;
-        } else if (strcmp (field, "User-Agent") == 0) {
-            header.user_agent = value;
-        } else if (strcmp (field, "WWW-Authenticate") == 0) {
-            header.www_authenticate = value;
-        } else {
-            VERBOSE (WARNING, PRINT ("Unknown header field: '%s'\n", field));
-        }
+        if (strcmp (field, "Allow") == 0) { header.allow = value; }
+        else if (strcmp (field, "Authorization") == 0) { header.authorization = value; }
+        else if (strcmp (field, "Content-Encoding") == 0) { header.content_encoding = value; }
+        else if (strcmp (field, "Content-Length") == 0) { header.content_length = value; }
+        else if (strcmp (field, "Content-Type") == 0) { header.content_type = value; }
+        else if (strcmp (field, "Date") == 0) { header.date = value; }
+        else if (strcmp (field, "Expires") == 0) { header.expires = value; }
+        else if (strcmp (field, "From") == 0) { header.from = value; }
+        else if (strcmp (field, "If-Modified-Since") == 0) { header.if_modified_since = value; }
+        else if (strcmp (field, "Last-Modified") == 0) { header.last_modified = value; }
+        else if (strcmp (field, "Location") == 0) { header.location = value; }
+        else if (strcmp (field, "Pragma") == 0) { header.pragma = value; }
+        else if (strcmp (field, "Referer") == 0) { header.referer = value; }
+        else if (strcmp (field, "Server") == 0) { header.server = value; }
+        else if (strcmp (field, "User-Agent") == 0) { header.user_agent = value; }
+        else if (strcmp (field, "WWW-Authenticate") == 0) { header.www_authenticate = value; }
+        else { VERBOSE (WARNING, PRINT ("Unknown header field: '%s'\n", field)); }
     }
     VERBOSE (DEBUG, print_header_values (&header));
 
     /* response */
     char *buffer = NULL;
+    FILE *fid = NULL;
     switch (type) {
     case get_e:
         VERBOSE (DEBUG, PRINT ("Read file %s\n", filename));
@@ -403,6 +386,16 @@ int processing (char *data, int len, conf_t *conf, char **pdata)
         }
         break;
     case head_e:
+        VERBOSE (DEBUG, PRINT ("Read file %s\n", filename));
+        fid = fopen (filename, "rb");
+        if (fid == NULL) {
+            sprintf (location, "http://%s/", conf->servername);
+            len = error_404 (pdata, location);
+        } else {
+            fclose (fid);
+            sprintf (location, "http://%s%s", conf->servername, filename);
+            len = generic_response (pdata, location, NULL, 0);
+        }
         break;
     case post_e:
         VERBOSE (DEBUG, PRINT ("Write file %s\n", filename));
index 770d75dd5d68e5680f827ecfa918bdd65ef520df..d74145f8cc82db36efa035287341061783bc8f36 100644 (file)
@@ -206,11 +206,12 @@ int main (int argc, char *argv[])
 // test: webserver.exe -p -1 2>&1 | grep -q 'incorrect port number'
 // test: webserver.exe -s 2>&1 | grep -q 'missing server name'
 // test: webserver.exe -r 2>&1 | grep -q 'missing directory name'
-// test: webserver.exe >& test.log & pid=$!; sleep 1; kill -QUIT $pid; grep -q 'Listening socket on port 8080' test.log
-// test: webserver.exe -p 8000 >& test.log & pid=$!; sleep 1; kill -ABRT $pid; grep -q 'Listening socket on port 8000' test.log
+// test: webserver.exe >& test.log & pid=$!; sleep 1; kill -INT $pid; grep -q 'Listening socket on port 8080' test.log
+// test: webserver.exe -p 8000 >& test.log & pid=$!; sleep 1; kill -INT $pid; grep -q 'Listening socket on port 8000' test.log
 // test: webserver.exe -c iso-8859-1 -r webroot -s localhost >&/dev/null & pid=$!; sleep 1; curl http://localhost:8080/index.html > test; kill -TERM $pid; grep -q '<title>Test</title>' test
 // test: webserver.exe -v 3 -p 8001 >&/dev/null & pid=$!; sleep 1; curl http://localhost:8001/index.html > test; kill -TERM $pid
 // test: webserver.exe -v 3 -p 8002 >&/dev/null & pid=$!; sleep 1; curl http://localhost:8002/not_found.html > test; kill -TERM $pid; grep -q '404' test
+// test: webserver.exe -v 3 -p 8003 >&/dev/null & pid=$!; sleep 1; curl -I http://localhost:8003/index.html > test; kill -TERM $pid
 
 
 /* vim: set ts=4 sw=4 et: */