]> Nishi Git Mirror - libw3.git/commitdiff
making httpd show index.html if it exists
authornishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Tue, 27 Feb 2024 14:04:31 +0000 (14:04 +0000)
committernishi <nishi@d27a3e52-49c5-7645-884c-6793ebffc270>
Tue, 27 Feb 2024 14:04:31 +0000 (14:04 +0000)
git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@258 d27a3e52-49c5-7645-884c-6793ebffc270

Example/httpd/httpd.c
Example/httpd/httpd.conf
W3Version.h.m4

index 55bac1b8934e7879c57918127bd8d209b9feaeda..73c4750ef400528e1203eaa5b7890e16d12bd743 100644 (file)
@@ -148,6 +148,75 @@ response : {
        if(stat(realpath, &s) == 0) {
                if(S_ISDIR(s.st_mode)) {
                        if(path[strlen(path) - 1] == '/') {
+                               struct stat old_s = s;
+                               char* index = __W3_Concat(path, "index.html");
+                               if(stat(index, &s)){
+                                       send(sock, "HTTP/1.1 200 OK\r\n", 17, 0);
+                                       send(sock, "Connection: close\r\n", 19, 0);
+                                       char* length = malloc(1025);
+                                       sprintf(length, "%d", s.st_size);
+                                       send(sock, "Content-Type: ", 14, 0);
+                                       if(types == NULL) {
+                                               send(sock, "application/octet-stream", 24, 0);
+                                       } else {
+                                               int i;
+                                               char* fpath = __W3_Strdup(index);
+                                               bool found = false;
+                                               for(i = strlen(fpath) - 1; i >= 0; i--) {
+                                                       if(fpath[i] == '.') {
+                                                               fpath[i] = 0;
+                                                               found = true;
+                                                               break;
+                                                       }
+                                               }
+                                               if(found) {
+                                                       found = false;
+                                                       int start = i + 1;
+                                                       for(i = 0; i < ntypes; i += 2) {
+                                                               if(strcasecmp(types[i], fpath + start) == 0) {
+                                                                       found = true;
+                                                                       send(sock, types[i + 1], strlen(types[i + 1]), 0);
+                                                                       break;
+                                                               }
+                                                       }
+                                                       if(!found) {
+                                                               send(sock, "application/octet-stream", 24, 0);
+                                                       }
+                                               } else {
+                                                       send(sock, "application/octet-stream", 24, 0);
+                                               }
+                                               free(fpath);
+                                       }
+                                       send(sock, "\r\n", 2, 0);
+                                       send(sock, "Content-Length: ", 16, 0);
+                                       send(sock, length, strlen(length), 0);
+                                       free(length);
+                                       send(sock, "\r\n", 2, 0);
+                                       send(sock, "Server: ", 8, 0);
+                                       send(sock, "LibW3-HTTPd (LibW3/", 19, 0);
+                                       send(sock, LIBW3_VERSION, strlen(LIBW3_VERSION), 0);
+                                       send(sock, ")", 1, 0);
+                                       send(sock, "\r\n", 2, 0);
+                                       send(sock, "\r\n", 2, 0);
+                                       if(strcasecmp(method, "HEAD") != 0) {
+                                               char* realpath_index = __W3_Concat3(root, "/", index);
+                                               FILE* f = fopen(realpath_index, "r");
+                                               free(realpath_index);
+                                               char* buf = malloc(BUFFER_SIZE);
+                                               while(true) {
+                                                       int len = fread(buf, 1, BUFFER_SIZE, f);
+                                                       if(len <= 0) break;
+                                                       send(sock, buf, len, 0);
+                                               }
+                                               fclose(f);
+                                               free(buf);
+                                       }
+                                       free(index);
+                                       goto out;
+                               }
+                               s = old_s;
+                               free(index);
+
                                char* html = malloc(1);
                                html[0] = 0;
                                char* tmp;
@@ -259,6 +328,7 @@ response : {
                                send(sock, "\r\n", 2, 0);
                                if(strcasecmp(method, "HEAD") != 0) send(sock, html, strlen(html), 0);
                                free(html);
+out:;
                        } else {
                                send(sock, "HTTP/1.1 308 Permanent Redirect\r\n", 33, 0);
                                send(sock, "Connection: close\r\n", 19, 0);
@@ -427,6 +497,13 @@ int main(int argc, char** argv) {
                                                }
                                                if(root != NULL) free(root);
                                                root = __W3_Strdup(line + j + 1);
+                                       }else if(strcasecmp(line, "Port") == 0) {
+                                               if(!hasparam) {
+                                                       fprintf(stderr, "%s: config line %d, directive needs a parameter\n", argv[0], linenum);
+                                                       err++;
+                                               }
+                                               if(portstr != NULL) free(portstr);
+                                               portstr = __W3_Strdup(line + j + 1);
                                        } else if(strcasecmp(line, "MIME") == 0) {
                                                if(!hasparam) {
                                                        fprintf(stderr, "%s: config line %d, directive needs a parameter\n", argv[0], linenum);
index 8102c4d8deac233afec2bcc59a4063661dbd24c2..49b1d17d80efec0e9b26e3100d1399a1be7adaa8 100644 (file)
@@ -2,4 +2,4 @@
 Root /var/www
 MIME html text/html
 MIME txt text/plain
-
+Port 2048
index 20170b5b5d8f544a7021dba647c642837ef7307f..301191d14ac008ffbd39517971f332d411503717 100644 (file)
@@ -6,7 +6,7 @@
 extern "C" {
 #endif
 
-#define LIBW3_VERSION "2.20A" \
+#define LIBW3_VERSION "2.20B" \
 SUFFIX
 
 ifdef(`HTTP_SUPPORT', `#define LIBW3_HTTP_SUPPORT', `')
@@ -16,6 +16,8 @@ ifdef(`GEMINI_SUPPORT', `#define LIBW3_GEMINI_SUPPORT', `')
 ifdef(`FINGER_SUPPORT', `#define LIBW3_FINGER_SUPPORT', `')
 ifdef(`NEX_SUPPORT', `#define LIBW3_NEX_SUPPORT', `')
 ifdef(`FILE_SUPPORT', `#define LIBW3_FILE_SUPPORT', `')
+ifdef(`IRC_SUPPORT', `#define LIBW3_IRC_SUPPORT', `')
+ifdef(`NNTP_SUPPORT', `#define LIBW3_NNTP_SUPPORT', `')
 
 #ifdef __cplusplus
 }