From: nishi Date: Tue, 27 Feb 2024 14:04:31 +0000 (+0000) Subject: making httpd show index.html if it exists X-Git-Url: http://10.10.0.4:5575/?a=commitdiff_plain;h=d532118005ec8a7d3f5a473928742543ef7924cd;p=libw3.git making httpd show index.html if it exists git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@258 d27a3e52-49c5-7645-884c-6793ebffc270 --- diff --git a/Example/httpd/httpd.c b/Example/httpd/httpd.c index 55bac1b..73c4750 100644 --- a/Example/httpd/httpd.c +++ b/Example/httpd/httpd.c @@ -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); diff --git a/Example/httpd/httpd.conf b/Example/httpd/httpd.conf index 8102c4d..49b1d17 100644 --- a/Example/httpd/httpd.conf +++ b/Example/httpd/httpd.conf @@ -2,4 +2,4 @@ Root /var/www MIME html text/html MIME txt text/plain - +Port 2048 diff --git a/W3Version.h.m4 b/W3Version.h.m4 index 20170b5..301191d 100644 --- a/W3Version.h.m4 +++ b/W3Version.h.m4 @@ -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 }