From 7206286408c66377161beca33276d2337268eb06 Mon Sep 17 00:00:00 2001 From: nishi Date: Tue, 27 Feb 2024 05:11:48 +0000 Subject: [PATCH] removing libserver from the deps git-svn-id: file:///raid/svn-main/nishi-libw3/trunk@257 d27a3e52-49c5-7645-884c-6793ebffc270 --- Example/httpd/Makefile | 2 +- Example/httpd/httpd.c | 61 +++++++++++++++++++++++++++++++++++++++--- W3Version.h.m4 | 2 +- 3 files changed, 59 insertions(+), 6 deletions(-) diff --git a/Example/httpd/Makefile b/Example/httpd/Makefile index 81b59a3..13fb04e 100644 --- a/Example/httpd/Makefile +++ b/Example/httpd/Makefile @@ -2,7 +2,7 @@ .PHONY: clean install ./httpd$(SUFFIX): ./httpd.c $(RESFILE) - $(CC) -g -o $@ -I ../../Library -L ../../Library $^ -lw3 -lserver + $(CC) -g -o $@ -I ../../Library -L ../../Library $^ -lw3 ../libw3.res: $(MAKE) -C .. ./libw3.res WINDRES=$(WINDRES) diff --git a/Example/httpd/httpd.c b/Example/httpd/httpd.c index a605980..55bac1b 100644 --- a/Example/httpd/httpd.c +++ b/Example/httpd/httpd.c @@ -15,8 +15,11 @@ #include #include #include - -#include +#include +#include +#include +#include +#include char* root = NULL; @@ -478,10 +481,60 @@ int main(int argc, char** argv) { } W3_Library_Init(); + int exitcode = 0; + int port = atoi(portstr); + int server_socket; + if((server_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0){ + return -1;goto exitnow; + } + struct sockaddr_in6 server_address; + if((server_socket = socket(AF_INET6, SOCK_STREAM, IPPROTO_TCP)) < 0){ + return -1;goto exitnow; + } + int yes = 1; + if(setsockopt(server_socket, SOL_SOCKET, SO_REUSEADDR, &yes, sizeof(yes)) < 0){ + close(server_socket); + return -1;goto exitnow; + } + if(setsockopt(server_socket, IPPROTO_TCP, TCP_NODELAY, &yes, sizeof(yes)) < 0){ + close(server_socket); + return 1;goto exitnow; + } + int no = 0; + if(setsockopt(server_socket, IPPROTO_IPV6, IPV6_V6ONLY, &no, sizeof(no)) < 0){ + close(server_socket); + return 1;goto exitnow; + } + memset(&server_address, 0, sizeof(server_address)); + server_address.sin6_family = AF_INET6; + server_address.sin6_addr = in6addr_any; + server_address.sin6_port = htons(port); + if(bind(server_socket, (struct sockaddr*)&server_address, sizeof(server_address)) < 0){ + close(server_socket); + return -1;goto exitnow; + } + if(listen(server_socket, 128) < 0){ + close(server_socket); + return -1;goto exitnow; + } + signal(SIGCHLD, SIG_IGN); __W3_Debug("HTTPd", "Ready"); - int st = ls_start_server(portstr == NULL ? 80 : atoi(portstr), http_handler); - if(st == -1) { + while(1){ + struct sockaddr_in claddr; + int clen = sizeof(claddr); + int sock = accept(server_socket, (struct sockaddr*)&claddr, &clen); + pid_t p = fork(); + if(p == 0){ + http_handler(sock); + _exit(-1); + }else{ + close(sock); + } + } +exitnow: + if(exitcode != 0) { fprintf(stderr, "%s: failed to start the server\n", argv[0]); return 1; } + return exitcode; } diff --git a/W3Version.h.m4 b/W3Version.h.m4 index 573ef63..20170b5 100644 --- a/W3Version.h.m4 +++ b/W3Version.h.m4 @@ -6,7 +6,7 @@ extern "C" { #endif -#define LIBW3_VERSION "2.20" \ +#define LIBW3_VERSION "2.20A" \ SUFFIX ifdef(`HTTP_SUPPORT', `#define LIBW3_HTTP_SUPPORT', `') -- 2.43.0