1
0
mirror of https://github.com/fumiama/simple-http-server.git synced 2026-06-19 11:10:26 +08:00

fix accept

This commit is contained in:
源文雨
2022-03-04 19:30:04 +08:00
parent 7b5c807ed0
commit 64ec1113fd

View File

@@ -486,6 +486,7 @@ static int startup(uint16_t *port) {
} }
static struct sockaddr_un uname; static struct sockaddr_un uname;
static struct sockaddr_un uclient_name;
static int startupunix(char *path) { static int startupunix(char *path) {
int httpd = socket(AF_UNIX, SOCK_STREAM, 0); int httpd = socket(AF_UNIX, SOCK_STREAM, 0);
uname.sun_family = AF_UNIX; uname.sun_family = AF_UNIX;
@@ -529,16 +530,22 @@ static void unimplemented(int client) {
/************************************************************************/ /************************************************************************/
static pthread_attr_t attr; static pthread_attr_t attr;
static pthread_t accept_thread; static pthread_t accept_thread;
static socklen_t client_name_len = sizeof(client_name); static int accept_client(int server_sock, int is_unix_sock) {
static int accept_client(int server_sock) { socklen_t client_name_len = is_unix_sock?sizeof(uclient_name):sizeof(client_name);
signal(SIGCHLD, SIG_IGN); signal(SIGCHLD, SIG_IGN);
signal(SIGQUIT, handle_quit); signal(SIGQUIT, handle_quit);
signal(SIGPIPE, handle_quit); signal(SIGPIPE, handle_quit);
pthread_attr_init(&attr); pthread_attr_init(&attr);
pthread_attr_setdetachstate(&attr, 1); pthread_attr_setdetachstate(&attr, 1);
while(1) { while(1) {
int client_sock = accept(server_sock, (struct sockaddr *)&client_name, &client_name_len); int client_sock = accept(server_sock, (struct sockaddr *)(is_unix_sock?&uclient_name:&client_name), &client_name_len);
if(client_sock == -1) continue; if(client_sock == -1) continue;
if(is_unix_sock) {
uclient_name.sun_path[sizeof(uclient_name.sun_path)-1] = 0;
printf("Accept client %s\n", uclient_name.sun_path);
}
else {
#ifdef LISTEN_ON_IPV6 #ifdef LISTEN_ON_IPV6
uint16_t port = ntohs(client_name.sin6_port); uint16_t port = ntohs(client_name.sin6_port);
struct in6_addr in = client_name.sin6_addr; struct in6_addr in = client_name.sin6_addr;
@@ -551,6 +558,7 @@ static int accept_client(int server_sock) {
inet_ntop(AF_INET, &in, str, sizeof(str)); inet_ntop(AF_INET, &in, str, sizeof(str));
#endif #endif
printf("Accept client %s:%u\n", str, port); printf("Accept client %s:%u\n", str, port);
}
if(pthread_create(&accept_thread, &attr, &accept_request, client_sock) != 0) perror("pthread_create"); if(pthread_create(&accept_thread, &attr, &accept_request, client_sock) != 0) perror("pthread_create");
} }
} }
@@ -606,7 +614,7 @@ int main(int argc, char **argv) {
pid = fork(); pid = fork();
} }
if(pid < 0) perror("fork"); if(pid < 0) perror("fork");
else accept_client(server_sock); else accept_client(server_sock, !port);
} else accept_client(server_sock); } else accept_client(server_sock, !port);
} }
} }