summary refs log tree commit diff
path: root/bin/quick.c
blob: edf35a3bc3e309aec0abbca0b7cfd63147320fe5 (plain) (blame)
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
/* Copyright (C) 2021  C. McEnroe <june@causal.agency>
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Affero General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Affero General Public License for more details.
 *
 * You should have received a copy of the GNU Affero General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */

#include <arpa/inet.h>
#include <err.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <strings.h>
#include <sys/socket.h>
#include <sys/wait.h>
#include <sysexits.h>
#include <unistd.h>

static void request(int sock, char *argv[]) {
	struct pollfd pfd = { .fd = sock, .events = POLLIN };
	int nfds = poll(&pfd, 1, -1);
	if (nfds < 0) err(EX_OSERR, "poll");

	char buf[4096];
	ssize_t len = recv(sock, buf, sizeof(buf)-1, MSG_PEEK);
	if (len < 0) {
		warn("recv");
		return;
	}
	char *blank = memmem(buf, len, "\r\n\r\n", 4);
	if (!blank) {
		warnx("can't find end of request headers in peek");
		return;
	}
	len = recv(sock, buf, &blank[4] - buf, 0);
	if (len < 0) {
		warn("recv");
		return;
	}
	buf[len] = '\0';

	char *ptr = buf;
	char *req = strsep(&ptr, "\r\n");
	char *method = strsep(&req, " ");
	char *query = strsep(&req, " ");
	char *path = strsep(&query, "?");
	char *proto = strsep(&req, " ");
	if (!method || !path || !proto) {
		warnx("invalid request line");
		return;
	}
	setenv("REQUEST_METHOD", method, 1);
	setenv("PATH_INFO", path, 1);
	setenv("QUERY_STRING", (query ? query : ""), 1);
	setenv("SERVER_PROTOCOL", proto, 1);

	unsetenv("CONTENT_TYPE");
	unsetenv("CONTENT_LENGTH");
	unsetenv("HTTP_HOST");
	while (ptr) {
		char *value = strsep(&ptr, "\r\n");
		if (!value[0]) continue;
		char *header = strsep(&value, ":");
		if (!header || !value++) {
			warnx("invalid header");
			return;
		}
		if (!strcasecmp(header, "Content-Type")) {
			setenv("CONTENT_TYPE", value, 1);
		} else if (!strcasecmp(header, "Content-Length")) {
			setenv("CONTENT_LENGTH", value, 1);
		} else if (!strcasecmp(header, "Host")) {
			setenv("HTTP_HOST", value, 1);
		}
	}

	dprintf(sock, "HTTP/1.1 200 OK\nConnection: close\n");
	pid_t pid = fork();
	if (pid < 0) err(EX_OSERR, "fork");
	if (!pid) {
		dup2(sock, STDIN_FILENO);
		dup2(sock, STDOUT_FILENO);
		execv(argv[0], argv);
		warn("%s", argv[0]);
		_exit(127);
	}

	int status;
	pid = wait(&status);
	if (pid < 0) err(EX_OSERR, "wait");
	if (WIFEXITED(status) && WEXITSTATUS(status)) {
		warnx("%s exited %d", argv[0], WEXITSTATUS(status));
	} else if (WIFSIGNALED(status)) {
		warnx("%s killed %d", argv[0], WTERMSIG(status));
	}
}

int main(int argc, char *argv[]) {
	short port = 0;
	for (int opt; 0 < (opt = getopt(argc, argv, "p:"));) {
		switch (opt) {
			break; case 'p': port = atoi(optarg);
			break; default:  return EX_USAGE;
		}
	}
	if (optind == argc) errx(EX_USAGE, "script required");

	int server = socket(AF_INET, SOCK_STREAM, 0);
	if (server < 0) err(EX_OSERR, "socket");
	fcntl(server, F_SETFD, FD_CLOEXEC);

	int on = 1;
	setsockopt(server, SOL_SOCKET, SO_REUSEADDR, &on, sizeof(on));

	struct sockaddr_in addr = {
		.sin_family = AF_INET,
		.sin_port = htons(port),
		.sin_addr.s_addr = htonl(INADDR_LOOPBACK),
	};
	socklen_t addrlen = sizeof(addr);
	int error = 0
		|| bind(server, (struct sockaddr *)&addr, addrlen)
		|| getsockname(server, (struct sockaddr *)&addr, &addrlen)
		|| listen(server, -1);
	if (error) err(EX_UNAVAILABLE, "%hd", port);

	char host[NI_MAXHOST], serv[NI_MAXSERV];
	error = getnameinfo(
		(struct sockaddr *)&addr, addrlen,
		host, sizeof(host), serv, sizeof(serv),
		NI_NOFQDN | NI_NUMERICSERV
	);
	if (error) errx(EX_UNAVAILABLE, "getnameinfo: %s", gai_strerror(error));
	printf("http://%s:%s/\n", host, serv);
	fflush(stdout);

	setenv("SERVER_SOFTWARE", "quick (and dirty)", 1);
	setenv("GATEWAY_INTERFACE", "CGI/1.1", 1);
	setenv("SERVER_NAME", host, 1);
	setenv("SERVER_PORT", serv, 1);
	setenv("REMOTE_ADDR", "127.0.0.1", 1);
	setenv("REMOTE_HOST", host, 1);
	setenv("SCRIPT_NAME", "/", 1);

	for (int sock; 0 <= (sock = accept(server, NULL, NULL)); close(sock)) {
		fcntl(sock, F_SETFD, FD_CLOEXEC);
		request(sock, &argv[optind]);
	}
	err(EX_IOERR, "accept");
}