summary refs log tree commit diff
path: root/bin/quick.c
blob: ae2c99fa10cbc655011778332a36fec86e00cd9c (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
164
165
166
167
168
169
170
171
172
173
174
/* Copyright (C) 2021  June 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 <ctype.h>
#include <err.h>
#include <fcntl.h>
#include <netdb.h>
#include <netinet/in.h>
#include <signal.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[]) {
	FILE *req = fdopen(dup(sock), "r");
	if (!req) err(EX_OSERR, "fdopen");
	fcntl(fileno(req), F_SETFD, FD_CLOEXEC);

	size_t cap = 0;
	char *buf = NULL;
	ssize_t len = getline(&buf, &cap, req);
	if (len < 0) goto close;

	char *ptr = buf;
	char *method = strsep(&ptr, " ");
	char *query = strsep(&ptr, " ");
	char *path = strsep(&query, "?");
	char *proto = strsep(&ptr, "\r\n");
	if (!method || !path || !proto) goto close;

	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");

	size_t bodyLen = 0;
	while (0 <= (len = getline(&buf, &cap, req))) {
		if (len && buf[len-1] == '\n') buf[--len] = '\0';
		if (len && buf[len-1] == '\r') buf[--len] = '\0';
		if (!len) break;

		char *value = buf;
		char *header = strsep(&value, ":");
		if (!header || !value++) goto close;

		if (!strcasecmp(header, "Content-Type")) {
			setenv("CONTENT_TYPE", value, 1);
		} else if (!strcasecmp(header, "Content-Length")) {
			bodyLen = strtoull(value, NULL, 10);
			setenv("CONTENT_LENGTH", value, 1);
		} else {
			char buf[256];
			for (char *ch = header; *ch; ++ch) {
				*ch = (*ch == '-' ? '_' : toupper(*ch));
			}
			snprintf(buf, sizeof(buf), "HTTP_%s", header);
			setenv(buf, value, 1);
		}
	}

	int rw[2];
	int error = pipe(rw);
	if (error) err(EX_OSERR, "pipe");
	fcntl(rw[0], F_SETFD, FD_CLOEXEC);
	fcntl(rw[1], F_SETFD, FD_CLOEXEC);

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

	close(rw[0]);
	char body[4096];
	while (bodyLen) {
		size_t cap = (bodyLen < sizeof(body) ? bodyLen : sizeof(body));
		size_t len = fread(&body, 1, cap, req);
		if (!len) break;
		write(rw[1], body, len);
		bodyLen -= len;
	}
	close(rw[1]);

	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));
	}

close:
	fclose(req);
	free(buf);
}

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);

	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);

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