From 0339fb0bcbc5e6c57f63da7f8ee9b10dc60e6a4d Mon Sep 17 00:00:00 2001 From: nishi Date: Sat, 17 Feb 2024 06:42:54 +0000 Subject: [PATCH] httpd git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@240 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/httpd/httpd.c | 97 +++++++++++++++++++++++++++++++++++++++- Example/httpd/httpd.conf | 3 ++ W3Version.h.m4 | 2 +- 3 files changed, 99 insertions(+), 3 deletions(-) diff --git a/Example/httpd/httpd.c b/Example/httpd/httpd.c index b77c771..705c1e8 100644 --- a/Example/httpd/httpd.c +++ b/Example/httpd/httpd.c @@ -30,6 +30,28 @@ char* badreq; char* badreq_header; char* notfound; char* notfound_header; +char** types = NULL; +int ntypes = 0; + +void add_type(char* key, char* value){ + if(types == NULL){ + types = malloc(sizeof(*types) * 2); + types[0] = __W3_Strdup(key); + types[1] = __W3_Strdup(value); + ntypes = 2; + }else{ + char** oldtypes = types; + int i; + for(i = 0; i < ntypes; i++){ + types[i] = oldtypes[i]; + } + types = malloc(sizeof(*types) * (ntypes + 2)); + types[ntypes] = __W3_Strdup(key); + types[ntypes + 1] = __W3_Strdup(value); + ntypes += 2; + free(oldtypes); + } +} void http_handler(int sock){ char* buf = malloc(BUFFER_SIZE); @@ -136,7 +158,7 @@ response: free(tmp); tmp = html; - html = __W3_Concat(tmp, "
"); + html = __W3_Concat(tmp, "
"); free(tmp); struct dirent** namelist; @@ -164,8 +186,45 @@ rep:; free(tmp); tmp = html; - html = __W3_Concat3(tmp, dir ? "/" : "", ""); + html = __W3_Concat3(tmp, dir ? "/" : "", ""); + free(tmp); + + char* bytes = malloc(2049); + char* suffix = __W3_Strdup(" B"); + + double bsize = s2.st_size; + + if(S_ISDIR(s2.st_mode)){ + sprintf(bytes, "<Directory>"); + free(suffix); + suffix = __W3_Strdup(""); + }else{ + if(s2.st_size > 1024 * 1024 * 1024){ + free(suffix); + suffix = __W3_Strdup(" GB"); + bsize /= 1024 * 1024 * 1024; + }else if(s2.st_size > 1024 * 1024){ + free(suffix); + suffix = __W3_Strdup(" MB"); + bsize /= 1024 * 1024; + }else if(s2.st_size > 1024){ + free(suffix); + suffix = __W3_Strdup(" KB"); + bsize /= 1024; + } + sprintf(bytes, "%.2f", bsize); + } + + tmp = html; + html = __W3_Concat3(tmp, ""); + free(tmp); + + free(bytes); + free(suffix); } } if(!dir) free(namelist[i]); @@ -202,6 +261,34 @@ rep:; send(sock, "\r\n", 2, 0); } }else{ + 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; + for(i = 0; i < ntypes; i += 2){ + printf("%s\n", types[i]); + } + } + 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, "\r\n", 2, 0); + FILE* f = fopen(realpath, "r"); + 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); } }else{ send(sock, notfound_header, strlen(notfound_header), 0); @@ -302,6 +389,12 @@ int main(int argc, char** argv) { } if(root != NULL) free(root); root = __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); + err++; + } + add_type("txt", "text/plain"); }else{ fprintf(stderr, "%s: config line %d, unknown directive\n", argv[0], linenum); err++; diff --git a/Example/httpd/httpd.conf b/Example/httpd/httpd.conf index 98fd1a6..8102c4d 100644 --- a/Example/httpd/httpd.conf +++ b/Example/httpd/httpd.conf @@ -1,2 +1,5 @@ # $Id$ Root /var/www +MIME html text/html +MIME txt text/plain + diff --git a/W3Version.h.m4 b/W3Version.h.m4 index cad0086..59274de 100644 --- a/W3Version.h.m4 +++ b/W3Version.h.m4 @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.16E" \ +#define LIBW3_VERSION "2.16F" \ SUFFIX ifdef(`HTTP_SUPPORT', `#define LIBW3_HTTP_SUPPORT', `') -- 2.43.0
NameSize
", bytes); free(tmp); + + tmp = html; + html = __W3_Concat3(tmp, suffix, "