summaryrefslogtreecommitdiff
path: root/main.c
blob: 0747724029d8c91cf6ff4233a79a290b72fefe29 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
#define _GNU_SOURCE
#include <stdio.h>
#include <stdbool.h>
#include <stdarg.h>
#include <string.h>
#include <signal.h>
#include <sys/socket.h>
#include <netinet/in.h>
#include <arpa/inet.h>
#include <unistd.h>
#include <errno.h>
#include "http.h"
#include "memory.h"


#define PORT 8080


int num_threads=0;


static void signal_handler(int sig){
	if(sig==SIGCHLD){
		int saved_errno=errno;
		while(true){
			int status;
			pid_t pid=waitpid(-1,&status,WNOHANG);
			if(pid<=0){
				break;
			}
			if(WIFEXITED(status)){
				num_threads--;
			}
		}
		errno=saved_errno;
	}
}


// Returns -1 on error, 0 on success
static int sendall(int sock,const char *buf,ssize_t len){
	if(len==-1){
		len=strlen(buf);
	}

	ssize_t sent=0;
	while(sent<len){
		ssize_t ret=send(sock,buf+sent,len-sent,0);
		if(ret<0){
			if(errno==EINTR){
				continue;
			} else {
				return -1;
			}
		}
		sent+=ret;
	}

	return 0;
}

__attribute__((format (printf, 2, 3)))
static int sendallf(int sock,const char *format,...){
	va_list ap;
	va_start(ap,format);
	char *buf;
	int len=vasprintf(&buf,format,ap);
	va_end(ap);
	if(len<0){
		return -1;
	}
	int ret=sendall(sock,buf,len);
	free(buf);
	return ret;
}

static void connection_handler(int sock){
	Headers *headers=http_get_headers(sock);
	if(headers==NULL){
		return;
	}

	sendall(sock,"HTTP/1.1 200 OK\r\n"
	             "Content-Type: text/plain; charset=UTF-8\r\n"
	             "Server: cserver\r\n"
	             "\r\n",-1);

	sendallf(sock,"Method: %s\n",headers->method);
	sendallf(sock,"URL: %s\n",headers->url);
	sendallf(sock,"Version: %s\n",headers->version);
	for(int i=0;i<headers->nheaders;i++){
		sendallf(sock,"Header '%s': '%s'\n",headers->headers[i][0],headers->headers[i][1]);
	}
}


int main(void){
	int sock=socket(PF_INET,SOCK_STREAM,0);
	if(sock<0){
		perror("socket");
		return 1;
	}

	int one=1;
	if(setsockopt(sock,SOL_SOCKET,SO_REUSEADDR,&one,sizeof(int))<0){
		perror("setsockopt");
		return 1;
	}

	struct sockaddr_in sin;
	sin.sin_family=AF_INET;
	sin.sin_addr.s_addr=htonl(INADDR_ANY);
	sin.sin_port=htons(PORT);

	if(bind(sock,(struct sockaddr*)&sin,sizeof(sin))<0){
		close(sock);
		perror("bind");
		return 1;
	}

	if(listen(sock,8)<0){
		perror("listen");
		return 1;
	}

	signal(SIGCHLD,signal_handler);

	printf("Listening on port %d\n",PORT);

	while(true){
		struct sockaddr_storage client_addr;
		socklen_t client_addr_sz=sizeof(client_addr);
		int clientsock=accept(sock,(struct sockaddr*)&client_addr,&client_addr_sz);
		if(clientsock<0){
			perror("accept");
			continue;
		}

		char str[INET_ADDRSTRLEN];
		inet_ntop(client_addr.ss_family,&((struct sockaddr_in*)&client_addr)->sin_addr,
		          str,sizeof(str));

		pid_t pid=fork();
		if(pid==0){
			close(sock);
			connection_handler(clientsock);
			close(clientsock);
			exit(0);
		}
		if(pid<0){
			perror("fork");
			return 1;
		}
		num_threads++;
		printf("Accept from %s; child pid %d, %d thread%s\n",str,pid,num_threads,
		       num_threads==1?"":"s");
		close(clientsock);
	}
}