-
Notifications
You must be signed in to change notification settings - Fork 6
Expand file tree
/
Copy pathminiquark.c
More file actions
211 lines (177 loc) · 4.03 KB
/
Copy pathminiquark.c
File metadata and controls
211 lines (177 loc) · 4.03 KB
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
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
/*
* miniquark.c
* A minimal web server for retrieving files
*/
#include <sys/socket.h>
#include <sys/wait.h>
#include <netinet/in.h>
#include <err.h>
#include <poll.h>
#include <signal.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include "http.h"
#include "sock.h"
/* forwards */
static void serve(int, struct sockaddr_storage *);
static void sigcleanup(int);
static void handlesignals(void (*hdl)(int));
static void usage(bool);
/* globals */
struct server {
char *listen_addr;
char *port;
} s;
static void
serve(int infd, struct sockaddr_storage *in_sa) {
struct request r;
enum status status;
char inaddr[INET6_ADDRSTRLEN /* > INET_ADDRSTRLEN */];
/* set connection timeout */
if (sock_set_timeout(infd, 30)) {
goto cleanup;
}
/* handle request */
if (!(status = http_get_request(infd, &r))) {
status = http_send_response(infd, &r);
}
if (inaddr_to_str(in_sa, inaddr, LEN(inaddr))) {
goto cleanup;
}
printf("%lu\t%s\t%d\t%s\t%s\n", r.bytes_sent, inaddr, status, r.field[REQ_AGENT], r.target);
cleanup:
/* clean up and finish */
shutdown(infd, SHUT_RD);
shutdown(infd, SHUT_WR);
close(infd);
}
static void
sigcleanup(int sig) {
kill(0, sig);
_exit(1);
}
static void
handlesignals(void (*hdl)(int)) {
struct sigaction sa = {
.sa_handler = hdl,
};
sigemptyset(&sa.sa_mask);
sigaction(SIGTERM, &sa, NULL);
sigaction(SIGHUP, &sa, NULL);
sigaction(SIGINT, &sa, NULL);
sigaction(SIGQUIT, &sa, NULL);
}
static void
usage(bool summary) {
fprintf(stderr, "release: %s\n", RELEASE);
fprintf(stderr, "usage: miniquark [-d dir] [-l address] port\n");
if (!summary) {
fprintf(stderr, "hint: use -h to display option summary\n");
goto end;
}
printf("summary:\n"
" -d dir Switch to the specified directory\n"
" -l address Hostname or IP address listen on\n");
printf("docs:\n"
" man miniquark\n");
end:
exit(1);
}
int
main(int argc, char *argv[]) {
int ch;
struct sockaddr_storage in_sa;
socklen_t in_sa_len;
int status = 0, infd;
int addr_count, nready, n;
char inaddr[INET6_ADDRSTRLEN];
struct pollfd pfd[LISTEN_MAX];
struct sockaddr_storage resolved[LISTEN_MAX];
/* defaults */
char *servedir = ".";
s.listen_addr = "localhost";
s.port = NULL;
if (argv[1] && strcmp(argv[1], "-h") == 0)
usage(true);
opterr = 0;
while ((ch = getopt(argc, argv, "d:l:")) != -1) {
switch (ch) {
case 'd':
servedir = optarg;
break;
case 'l':
s.listen_addr = optarg;
break;
default:
usage(false);
}
}
if (argc != optind + 1)
usage(false);
s.port = argv[optind];
/* Open a new process group */
setpgid(0, 0);
handlesignals(sigcleanup);
switch (fork()) {
case -1:
warn("fork");
break;
case 0:
/* restore default handlers */
handlesignals(SIG_DFL);
/* reap children automatically */
if (signal(SIGCHLD, SIG_IGN) == SIG_ERR) {
err(1, "Failed to set SIG_IGN on SIGCHLD");
}
if (chdir(servedir) < 0)
err(1, "chdir '%s'", servedir);
addr_count = addr_listen(s.listen_addr, s.port, pfd, resolved);
printf("Listening on");
for (n = 0; n < addr_count; n++) {
inaddr_to_str(&resolved[n], inaddr, sizeof(inaddr));
printf(" %s", inaddr);
}
printf(" port %s\n", s.port);
fflush(stdout);
/* accept incoming connections */
while (1) {
infd = 0;
nready = poll(pfd, addr_count, -1);
if (nready < 1)
err(1, "poll");
for (n = 0; n < addr_count; n++) {
if (pfd[n].revents & (POLLERR | POLLNVAL))
errx(1, "bad fd %d", pfd[n].fd);
if (pfd[n].revents & (POLLIN | POLLHUP)) {
in_sa_len = sizeof(in_sa);
infd = accept(pfd[n].fd, (struct sockaddr *) &in_sa, &in_sa_len);
if (infd < 0)
err(1, "accept");
}
}
if (!infd)
continue;
/* fork and handle */
switch (fork()) {
case 0:
serve(infd, &in_sa);
exit(0);
break;
case -1:
warn("fork");
/* fallthrough */
default:
/* close the connection in the parent */
close(infd);
}
}
exit(0);
default:
while (wait(&status) > 0)
;
}
return status;
}