mirror of
https://github.com/fumiama/simple-http-server.git
synced 2026-06-10 21:16:26 +08:00
优化代码与ipv6处理
This commit is contained in:
49
server.c
49
server.c
@@ -433,9 +433,11 @@ static void serve_file(int client, const char *filename) {
|
|||||||
#ifdef LISTEN_ON_IPV6
|
#ifdef LISTEN_ON_IPV6
|
||||||
static socklen_t struct_len = sizeof(struct sockaddr_in6);
|
static socklen_t struct_len = sizeof(struct sockaddr_in6);
|
||||||
static struct sockaddr_in6 name;
|
static struct sockaddr_in6 name;
|
||||||
|
static struct sockaddr_in6 client_name;
|
||||||
#else
|
#else
|
||||||
static socklen_t struct_len = sizeof(struct sockaddr_in);
|
static socklen_t struct_len = sizeof(struct sockaddr_in);
|
||||||
static struct sockaddr_in name;
|
static struct sockaddr_in name;
|
||||||
|
static struct sockaddr_in client_name;
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
static int startup(uint16_t *port) {
|
static int startup(uint16_t *port) {
|
||||||
@@ -483,20 +485,31 @@ static void unimplemented(int client) {
|
|||||||
/* simple-http-server
|
/* simple-http-server
|
||||||
* Usage: simple-http-server [-d] [-p <port>] [-r <rootdir>] [-u <uid>]
|
* Usage: simple-http-server [-d] [-p <port>] [-r <rootdir>] [-u <uid>]
|
||||||
/**********************************************************************/
|
/**********************************************************************/
|
||||||
#define accept_client(client_sock, server_sock, client_name, client_name_len, newthread) {\
|
static pthread_attr_t attr;
|
||||||
signal(SIGCHLD, SIG_IGN);\
|
static pthread_t accept_thread;
|
||||||
signal(SIGQUIT, handle_quit);\
|
static socklen_t client_name_len = sizeof(client_name);
|
||||||
signal(SIGPIPE, handle_quit);\
|
int accept_client(int server_sock) {
|
||||||
pthread_attr_t attr;\
|
signal(SIGCHLD, SIG_IGN);
|
||||||
pthread_attr_init(&attr);\
|
signal(SIGQUIT, handle_quit);
|
||||||
pthread_attr_setdetachstate(&attr, 1);\
|
signal(SIGPIPE, handle_quit);
|
||||||
while(1) {\
|
pthread_attr_init(&attr);
|
||||||
client_sock = accept(server_sock,(struct sockaddr *)&client_name, &client_name_len);\
|
pthread_attr_setdetachstate(&attr, 1);
|
||||||
if(client_sock == -1) break;\
|
while(1) {
|
||||||
if(pthread_create(&newthread, &attr, &accept_request, client_sock) != 0) perror("pthread_create");\
|
int client_sock = accept(server_sock, (struct sockaddr *)&client_name, &client_name_len);
|
||||||
}\
|
if(client_sock == -1) continue;
|
||||||
close(client_sock);\
|
uint16_t port = ntohs(client_name.sin_port);
|
||||||
error_die("accept");\
|
#ifdef LISTEN_ON_IPV6
|
||||||
|
struct in6_addr in = client_name.sin6_addr;
|
||||||
|
char str[INET6_ADDRSTRLEN]; // 46
|
||||||
|
inet_ntop(AF_INET6, &in, str, sizeof(str));
|
||||||
|
#else
|
||||||
|
struct in_addr in = client_name.sin_addr;
|
||||||
|
char str[INET_ADDRSTRLEN]; // 16
|
||||||
|
inet_ntop(AF_INET, &in, str, sizeof(str));
|
||||||
|
#endif
|
||||||
|
printf("Accept client %s:%u\n", str, port);
|
||||||
|
if(pthread_create(&accept_thread, &attr, &accept_request, client_sock) != 0) perror("pthread_create");
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#define argequ(arg) (*(uint16_t*)argv[i] == *(uint16_t*)(arg))
|
#define argequ(arg) (*(uint16_t*)argv[i] == *(uint16_t*)(arg))
|
||||||
@@ -508,11 +521,7 @@ int main(int argc, char **argv) {
|
|||||||
char *cdir = "./";
|
char *cdir = "./";
|
||||||
uid_t uid = -1;
|
uid_t uid = -1;
|
||||||
int server_sock = -1;
|
int server_sock = -1;
|
||||||
int client_sock = -1;
|
|
||||||
int pid = -1;
|
int pid = -1;
|
||||||
struct sockaddr_in client_name;
|
|
||||||
socklen_t client_name_len = sizeof(client_name);
|
|
||||||
pthread_t newthread;
|
|
||||||
|
|
||||||
for(int i = 1; i < argc; i++) {
|
for(int i = 1; i < argc; i++) {
|
||||||
if(!as_daemon && argequ("-d")) as_daemon = 1;
|
if(!as_daemon && argequ("-d")) as_daemon = 1;
|
||||||
@@ -538,7 +547,7 @@ int main(int argc, char **argv) {
|
|||||||
pid = fork();
|
pid = fork();
|
||||||
}
|
}
|
||||||
if(pid < 0) perror("fork");
|
if(pid < 0) perror("fork");
|
||||||
else accept_client(client_sock, server_sock, client_name, client_name_len, newthread);
|
else accept_client(server_sock);
|
||||||
} else accept_client(client_sock, server_sock, client_name, client_name_len, newthread);
|
} else accept_client(server_sock);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user