2 * Copyright (c) 2005-2006 Robert N. M. Watson
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
8 * 1. Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * 2. Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in the
12 * documentation and/or other materials provided with the distribution.
14 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15 * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17 * ARE DISCLAIMED. IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18 * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19 * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20 * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22 * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23 * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
29 #include <sys/types.h>
31 #include <sys/socket.h>
33 #include <sys/utsname.h>
36 #include <netinet/in.h>
38 #include <arpa/inet.h>
52 static int threaded; /* 1 for threaded, 0 for forked. */
55 * Simple, multi-threaded/multi-process HTTP server. Very dumb.
57 * If a path is specified as an argument, only that file is served. If no
58 * path is specified, httpd will create one file to send per server thread.
64 #define HTTP_OK "HTTP/1.1 200 OK\n"
65 #define HTTP_SERVER1 "Server rwatson_httpd/1.0 ("
66 #define HTTP_SERVER2 ")\n"
67 #define HTTP_CONNECTION "Connection: close\n"
68 #define HTTP_CONTENT "Content-Type: text/html\n\n"
71 * In order to support both multi-threaded and multi-process operation but
72 * use a single shared memory statistics model, we create a page-aligned
73 * statistics buffer. For threaded operation, it's just shared memory due to
74 * threading; for multi-process operation, we mark it as INHERIT_SHARE, so we
75 * must put it in page-aligned memory that isn't shared with other memory, or
76 * risk accidental sharing of other statep.
79 struct httpd_thread_statep {
80 pthread_t hts_thread; /* Multi-thread. */
81 pid_t hts_pid; /* Multi-process. */
88 struct utsname utsname;
92 * Borrowed from sys/param.h.
94 #define roundup(x, y) ((((x)+((y)-1))/(y))*(y)) /* to any y */
97 * Given an open client socket, process its request. No notion of timeout.
100 http_serve(int sock, int fd)
102 struct iovec header_iovec[6];
103 struct sf_hdtr sf_hdtr;
108 /* Read until \n\n. Not very smart. */
111 len = recv(sock, buffer, BUFFER, 0);
118 for (i = 0; i < len; i++) {
135 bzero(&sf_hdtr, sizeof(sf_hdtr));
136 bzero(&header_iovec, sizeof(header_iovec));
137 header_iovec[0].iov_base = HTTP_OK;
138 header_iovec[0].iov_len = strlen(HTTP_OK);
139 header_iovec[1].iov_base = HTTP_SERVER1;
140 header_iovec[1].iov_len = strlen(HTTP_SERVER1);
141 header_iovec[2].iov_base = statep->utsname.sysname;
142 header_iovec[2].iov_len = strlen(statep->utsname.sysname);
143 header_iovec[3].iov_base = HTTP_SERVER2;
144 header_iovec[3].iov_len = strlen(HTTP_SERVER2);
145 header_iovec[4].iov_base = HTTP_CONNECTION;
146 header_iovec[4].iov_len = strlen(HTTP_CONNECTION);
147 header_iovec[5].iov_base = HTTP_CONTENT;
148 header_iovec[5].iov_len = strlen(HTTP_CONTENT);
149 sf_hdtr.headers = header_iovec;
151 sf_hdtr.trailers = NULL;
154 if (sendfile(fd, sock, 0, 0, &sf_hdtr, NULL, 0) < 0)
161 httpd_worker(void *arg)
163 struct httpd_thread_statep *htsp;
169 sock = accept(statep->listen_sock, NULL, NULL);
172 (void)http_serve(sock, htsp->hts_fd);
182 for (i = 0; i < THREADS; i++) {
183 if (statep->hts[i].hts_pid != 0)
184 (void)kill(statep->hts[i].hts_pid, SIGTERM);
192 fprintf(stderr, "httpd [-t] port [path]\n");
197 main(int argc, char *argv[])
199 u_char filebuffer[FILESIZE];
200 char temppath[PATH_MAX];
201 struct sockaddr_in sin;
208 while ((ch = getopt(argc, argv, "t")) != -1) {
221 if (argc != 1 && argc != 2)
224 len = roundup(sizeof(struct state), getpagesize());
225 pagebuffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
226 if (pagebuffer == MAP_FAILED)
228 if (minherit(pagebuffer, len, INHERIT_SHARE) < 0)
230 statep = (struct state *)pagebuffer;
232 if (uname(&statep->utsname) < 0)
235 statep->listen_sock = socket(PF_INET, SOCK_STREAM, 0);
236 if (statep->listen_sock < 0)
237 err(-1, "socket(PF_INET, SOCK_STREAM)");
239 bzero(&sin, sizeof(sin));
240 sin.sin_len = sizeof(sin);
241 sin.sin_family = AF_INET;
242 sin.sin_port = htons(atoi(argv[0]));
245 * If a path is specified, use it. Otherwise, create temporary files
246 * with some data for each thread.
248 statep->path = argv[1];
249 if (statep->path != NULL) {
250 statep->data_file = open(statep->path, O_RDONLY);
251 if (statep->data_file < 0)
252 err(-1, "open: %s", statep->path);
253 for (i = 0; i < THREADS; i++)
254 statep->hts[i].hts_fd = statep->data_file;
256 memset(filebuffer, 'A', FILESIZE - 1);
257 filebuffer[FILESIZE - 1] = '\n';
258 for (i = 0; i < THREADS; i++) {
259 snprintf(temppath, PATH_MAX, "/tmp/httpd.XXXXXXXXXXX");
260 statep->hts[i].hts_fd = mkstemp(temppath);
261 if (statep->hts[i].hts_fd < 0)
263 (void)unlink(temppath);
264 len = write(statep->hts[i].hts_fd, filebuffer,
269 errx(-1, "write: short");
273 if (bind(statep->listen_sock, (struct sockaddr *)&sin,
277 if (listen(statep->listen_sock, -1) < 0)
280 for (i = 0; i < THREADS; i++) {
282 if (pthread_create(&statep->hts[i].hts_thread, NULL,
283 httpd_worker, &statep->hts[i]) != 0)
284 err(-1, "pthread_create");
294 httpd_worker(&statep->hts[i]);
295 statep->hts[i].hts_pid = pid;
299 for (i = 0; i < THREADS; i++) {
301 if (pthread_join(statep->hts[i].hts_thread, NULL)
303 err(-1, "pthread_join");
305 pid = waitpid(statep->hts[i].hts_pid, NULL, 0);
306 if (pid == statep->hts[i].hts_pid)
307 statep->hts[i].hts_pid = 0;