1150990Srwatson/*-
2155891Srwatson * Copyright (c) 2005-2006 Robert N. M. Watson
3150990Srwatson * All rights reserved.
4150990Srwatson *
5150990Srwatson * Redistribution and use in source and binary forms, with or without
6150990Srwatson * modification, are permitted provided that the following conditions
7150990Srwatson * are met:
8150990Srwatson * 1. Redistributions of source code must retain the above copyright
9150990Srwatson *    notice, this list of conditions and the following disclaimer.
10150990Srwatson * 2. Redistributions in binary form must reproduce the above copyright
11150990Srwatson *    notice, this list of conditions and the following disclaimer in the
12150990Srwatson *    documentation and/or other materials provided with the distribution.
13150990Srwatson *
14150990Srwatson * THIS SOFTWARE IS PROVIDED BY THE AUTHOR AND CONTRIBUTORS ``AS IS'' AND
15150990Srwatson * ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
16150990Srwatson * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
17150990Srwatson * ARE DISCLAIMED.  IN NO EVENT SHALL THE AUTHOR OR CONTRIBUTORS BE LIABLE
18150990Srwatson * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL
19150990Srwatson * DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS
20150990Srwatson * OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
21150990Srwatson * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT
22150990Srwatson * LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
23150990Srwatson * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
24150990Srwatson * SUCH DAMAGE.
25150990Srwatson *
26150990Srwatson * $FreeBSD$
27150990Srwatson */
28150990Srwatson
29150990Srwatson#include <sys/types.h>
30155891Srwatson#include <sys/mman.h>
31150990Srwatson#include <sys/socket.h>
32150992Srwatson#include <sys/uio.h>
33151654Srwatson#include <sys/utsname.h>
34155891Srwatson#include <sys/wait.h>
35150990Srwatson
36150990Srwatson#include <netinet/in.h>
37150990Srwatson
38150990Srwatson#include <arpa/inet.h>
39150990Srwatson
40150990Srwatson#include <err.h>
41155891Srwatson#include <errno.h>
42150990Srwatson#include <fcntl.h>
43151654Srwatson#include <limits.h>
44150990Srwatson#include <pthread.h>
45155891Srwatson#include <signal.h>
46150990Srwatson#include <stdio.h>
47150990Srwatson#include <stdlib.h>
48150990Srwatson#include <string.h>
49155891Srwatson#include <sysexits.h>
50150990Srwatson#include <unistd.h>
51150990Srwatson
52155891Srwatsonstatic int	threaded;		/* 1 for threaded, 0 for forked. */
53155891Srwatson
54150990Srwatson/*
55155891Srwatson * Simple, multi-threaded/multi-process HTTP server.  Very dumb.
56151654Srwatson *
57151654Srwatson * If a path is specified as an argument, only that file is served.  If no
58151654Srwatson * path is specified, httpd will create one file to send per server thread.
59150990Srwatson */
60151654Srwatson#define	THREADS		128
61151654Srwatson#define	BUFFER		1024
62151654Srwatson#define	FILESIZE	1024
63150990Srwatson
64151654Srwatson#define	HTTP_OK		"HTTP/1.1 200 OK\n"
65151654Srwatson#define	HTTP_SERVER1	"Server rwatson_httpd/1.0 ("
66151654Srwatson#define	HTTP_SERVER2	")\n"
67151654Srwatson#define	HTTP_CONNECTION	"Connection: close\n"
68151654Srwatson#define	HTTP_CONTENT	"Content-Type: text/html\n\n"
69150992Srwatson
70155891Srwatson/*
71155891Srwatson * In order to support both multi-threaded and multi-process operation but
72155891Srwatson * use a single shared memory statistics model, we create a page-aligned
73155891Srwatson * statistics buffer.  For threaded operation, it's just shared memory due to
74155891Srwatson * threading; for multi-process operation, we mark it as INHERIT_SHARE, so we
75155891Srwatson * must put it in page-aligned memory that isn't shared with other memory, or
76155891Srwatson * risk accidental sharing of other statep.
77155891Srwatson */
78155891Srwatsonstatic struct state {
79155891Srwatson	struct httpd_thread_statep {
80155891Srwatson		pthread_t	hts_thread;	/* Multi-thread. */
81155891Srwatson		pid_t		hts_pid;	/* Multi-process. */
82155891Srwatson		int		hts_fd;
83155891Srwatson	} hts[THREADS];
84151654Srwatson
85155891Srwatson	const char	*path;
86155891Srwatson	int		 data_file;
87155891Srwatson	int		 listen_sock;
88155891Srwatson	struct utsname	 utsname;
89155891Srwatson} *statep;
90150990Srwatson
91150990Srwatson/*
92155891Srwatson * Borrowed from sys/param.h.
93155891Srwatson */
94155891Srwatson#define	roundup(x, y)	((((x)+((y)-1))/(y))*(y))	/* to any y */
95155891Srwatson
96155891Srwatson/*
97150990Srwatson * Given an open client socket, process its request.  No notion of timeout.
98150990Srwatson */
99150990Srwatsonstatic int
100151654Srwatsonhttp_serve(int sock, int fd)
101150990Srwatson{
102151654Srwatson	struct iovec header_iovec[6];
103150992Srwatson	struct sf_hdtr sf_hdtr;
104151654Srwatson	char buffer[BUFFER];
105150990Srwatson	ssize_t len;
106151654Srwatson	int i, ncount;
107150990Srwatson
108150990Srwatson	/* Read until \n\n.  Not very smart. */
109150990Srwatson	ncount = 0;
110150990Srwatson	while (1) {
111151654Srwatson		len = recv(sock, buffer, BUFFER, 0);
112150990Srwatson		if (len < 0) {
113150990Srwatson			warn("recv");
114150990Srwatson			return (-1);
115150990Srwatson		}
116150990Srwatson		if (len == 0)
117150990Srwatson			return (-1);
118151654Srwatson		for (i = 0; i < len; i++) {
119151654Srwatson			switch (buffer[i]) {
120151654Srwatson			case '\n':
121151654Srwatson				ncount++;
122151654Srwatson				break;
123151654Srwatson
124151654Srwatson			case '\r':
125151654Srwatson				break;
126151654Srwatson
127151654Srwatson			default:
128151654Srwatson				ncount = 0;
129151654Srwatson			}
130151654Srwatson		}
131150990Srwatson		if (ncount == 2)
132150990Srwatson			break;
133150990Srwatson	}
134150990Srwatson
135150992Srwatson	bzero(&sf_hdtr, sizeof(sf_hdtr));
136150992Srwatson	bzero(&header_iovec, sizeof(header_iovec));
137150992Srwatson	header_iovec[0].iov_base = HTTP_OK;
138150992Srwatson	header_iovec[0].iov_len = strlen(HTTP_OK);
139151654Srwatson	header_iovec[1].iov_base = HTTP_SERVER1;
140151654Srwatson	header_iovec[1].iov_len = strlen(HTTP_SERVER1);
141155891Srwatson	header_iovec[2].iov_base = statep->utsname.sysname;
142155891Srwatson	header_iovec[2].iov_len = strlen(statep->utsname.sysname);
143151654Srwatson	header_iovec[3].iov_base = HTTP_SERVER2;
144151654Srwatson	header_iovec[3].iov_len = strlen(HTTP_SERVER2);
145151654Srwatson	header_iovec[4].iov_base = HTTP_CONNECTION;
146151654Srwatson	header_iovec[4].iov_len = strlen(HTTP_CONNECTION);
147151654Srwatson	header_iovec[5].iov_base = HTTP_CONTENT;
148151654Srwatson	header_iovec[5].iov_len = strlen(HTTP_CONTENT);
149150992Srwatson	sf_hdtr.headers = header_iovec;
150151654Srwatson	sf_hdtr.hdr_cnt = 6;
151150992Srwatson	sf_hdtr.trailers = NULL;
152150992Srwatson	sf_hdtr.trl_cnt = 0;
153150992Srwatson
154151654Srwatson	if (sendfile(fd, sock, 0, 0, &sf_hdtr, NULL, 0) < 0)
155150990Srwatson		warn("sendfile");
156150990Srwatson
157150990Srwatson	return (0);
158150990Srwatson}
159150990Srwatson
160150990Srwatsonstatic void *
161150990Srwatsonhttpd_worker(void *arg)
162150990Srwatson{
163155891Srwatson	struct httpd_thread_statep *htsp;
164150990Srwatson	int sock;
165150990Srwatson
166151654Srwatson	htsp = arg;
167151654Srwatson
168150990Srwatson	while (1) {
169155891Srwatson		sock = accept(statep->listen_sock, NULL, NULL);
170150990Srwatson		if (sock < 0)
171150990Srwatson			continue;
172151654Srwatson		(void)http_serve(sock, htsp->hts_fd);
173150990Srwatson		close(sock);
174150990Srwatson	}
175150990Srwatson}
176150990Srwatson
177155891Srwatsonstatic void
178155891Srwatsonkillall(void)
179155891Srwatson{
180155891Srwatson	int i;
181155891Srwatson
182155891Srwatson	for (i = 0; i < THREADS; i++) {
183155891Srwatson		if (statep->hts[i].hts_pid != 0)
184155891Srwatson			(void)kill(statep->hts[i].hts_pid, SIGTERM);
185155891Srwatson	}
186155891Srwatson}
187155891Srwatson
188155891Srwatsonstatic void
189155891Srwatsonusage(void)
190155891Srwatson{
191155891Srwatson
192155891Srwatson	fprintf(stderr, "httpd [-t] port [path]\n");
193155891Srwatson	exit(EX_USAGE);
194155891Srwatson}
195155891Srwatson
196150990Srwatsonint
197150990Srwatsonmain(int argc, char *argv[])
198150990Srwatson{
199151654Srwatson	u_char filebuffer[FILESIZE];
200151654Srwatson	char temppath[PATH_MAX];
201150990Srwatson	struct sockaddr_in sin;
202155891Srwatson	int ch, error, i;
203155891Srwatson	char *pagebuffer;
204151654Srwatson	ssize_t len;
205155891Srwatson	pid_t pid;
206150990Srwatson
207150990Srwatson
208155891Srwatson	while ((ch = getopt(argc, argv, "t")) != -1) {
209155891Srwatson		switch (ch) {
210155891Srwatson		case 't':
211155891Srwatson			threaded = 1;
212155891Srwatson			break;
213155891Srwatson
214155891Srwatson		default:
215155891Srwatson			usage();
216155891Srwatson		}
217155891Srwatson	}
218155891Srwatson	argc -= optind;
219155891Srwatson	argv += optind;
220155891Srwatson
221155891Srwatson	if (argc != 1 && argc != 2)
222155891Srwatson		usage();
223155891Srwatson
224155891Srwatson	len = roundup(sizeof(struct state), getpagesize());
225155891Srwatson	pagebuffer = mmap(NULL, len, PROT_READ | PROT_WRITE, MAP_ANON, -1, 0);
226155891Srwatson	if (pagebuffer == MAP_FAILED)
227155891Srwatson		err(-1, "mmap");
228155891Srwatson	if (minherit(pagebuffer, len, INHERIT_SHARE) < 0)
229155891Srwatson		err(-1, "minherit");
230155891Srwatson	statep = (struct state *)pagebuffer;
231155891Srwatson
232155891Srwatson	if (uname(&statep->utsname) < 0)
233151654Srwatson		err(-1, "utsname");
234151654Srwatson
235155891Srwatson	statep->listen_sock = socket(PF_INET, SOCK_STREAM, 0);
236155891Srwatson	if (statep->listen_sock < 0)
237150990Srwatson		err(-1, "socket(PF_INET, SOCK_STREAM)");
238150990Srwatson
239150990Srwatson	bzero(&sin, sizeof(sin));
240150990Srwatson	sin.sin_len = sizeof(sin);
241150990Srwatson	sin.sin_family = AF_INET;
242155891Srwatson	sin.sin_port = htons(atoi(argv[0]));
243150990Srwatson
244151654Srwatson	/*
245151654Srwatson	 * If a path is specified, use it.  Otherwise, create temporary files
246151654Srwatson	 * with some data for each thread.
247151654Srwatson	 */
248155891Srwatson	statep->path = argv[1];
249155891Srwatson	if (statep->path != NULL) {
250155891Srwatson		statep->data_file = open(statep->path, O_RDONLY);
251155891Srwatson		if (statep->data_file < 0)
252155891Srwatson			err(-1, "open: %s", statep->path);
253151654Srwatson		for (i = 0; i < THREADS; i++)
254155891Srwatson			statep->hts[i].hts_fd = statep->data_file;
255151654Srwatson	} else {
256151654Srwatson		memset(filebuffer, 'A', FILESIZE - 1);
257151654Srwatson		filebuffer[FILESIZE - 1] = '\n';
258151654Srwatson		for (i = 0; i < THREADS; i++) {
259151654Srwatson			snprintf(temppath, PATH_MAX, "/tmp/httpd.XXXXXXXXXXX");
260155891Srwatson			statep->hts[i].hts_fd = mkstemp(temppath);
261155891Srwatson			if (statep->hts[i].hts_fd < 0)
262151654Srwatson				err(-1, "mkstemp");
263151654Srwatson			(void)unlink(temppath);
264155891Srwatson			len = write(statep->hts[i].hts_fd, filebuffer,
265155891Srwatson			    FILESIZE);
266151654Srwatson			if (len < 0)
267151654Srwatson				err(-1, "write");
268151654Srwatson			if (len < FILESIZE)
269151654Srwatson				errx(-1, "write: short");
270151654Srwatson		}
271151654Srwatson	}
272150990Srwatson
273155891Srwatson	if (bind(statep->listen_sock, (struct sockaddr *)&sin,
274155891Srwatson	    sizeof(sin)) < 0)
275150990Srwatson		err(-1, "bind");
276150990Srwatson
277155891Srwatson	if (listen(statep->listen_sock, -1) < 0)
278150990Srwatson		err(-1, "listen");
279150990Srwatson
280150990Srwatson	for (i = 0; i < THREADS; i++) {
281155891Srwatson		if (threaded) {
282155891Srwatson			if (pthread_create(&statep->hts[i].hts_thread, NULL,
283203800Sru			    httpd_worker, &statep->hts[i]) != 0)
284155891Srwatson				err(-1, "pthread_create");
285155891Srwatson		} else {
286155891Srwatson			pid = fork();
287155891Srwatson			if (pid < 0) {
288155891Srwatson				error = errno;
289155891Srwatson				killall();
290155891Srwatson				errno = error;
291155891Srwatson				err(-1, "fork");
292155891Srwatson			}
293155891Srwatson			if (pid == 0)
294155891Srwatson				httpd_worker(&statep->hts[i]);
295155891Srwatson			statep->hts[i].hts_pid = pid;
296155891Srwatson		}
297150990Srwatson	}
298150990Srwatson
299150990Srwatson	for (i = 0; i < THREADS; i++) {
300155891Srwatson		if (threaded) {
301155891Srwatson			if (pthread_join(statep->hts[i].hts_thread, NULL)
302203800Sru			    != 0)
303155891Srwatson				err(-1, "pthread_join");
304155891Srwatson		} else {
305155891Srwatson			pid = waitpid(statep->hts[i].hts_pid, NULL, 0);
306155891Srwatson			if (pid == statep->hts[i].hts_pid)
307155891Srwatson				statep->hts[i].hts_pid = 0;
308155891Srwatson		}
309150990Srwatson	}
310155891Srwatson	if (!threaded)
311155891Srwatson		killall();
312150990Srwatson	return (0);
313150990Srwatson}
314