2 * Copyright (c) 2021 Omar Polo <op@omarpolo.com>
4 * Permission to use, copy, modify, and distribute this software for any
5 * purpose with or without fee is hereby granted, provided that the above
6 * copyright notice and this permission notice appear in all copies.
8 * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
9 * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
10 * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
11 * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
12 * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
13 * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
14 * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
26 * the inverse of this algorithm, i.e. starting from the start of the
27 * path + strlen(cgi), and checking if each component, should be
28 * faster. But it's tedious to write. This does the opposite: starts
29 * from the end and strip one component at a time, until either an
30 * executable is found or we emptied the path.
33 check_for_cgi(char *path, char *query, struct pollfd *fds, struct client *c)
36 end = strchr(path, '\0');
38 /* NB: assume CGI is enabled and path matches cgi */
41 /* go up one level. UNIX paths are simple and POSIX
42 * dirname, with its ambiguities on if the given path
43 * is changed or not, gives me headaches. */
48 switch (check_path(c, path, &c->fd)) {
50 return start_cgi(path, end+1, query, fds,c);
62 if (!start_reply(fds, c, NOT_FOUND, "not found"))
69 start_cgi(const char *spath, const char *relpath, const char *query,
70 struct pollfd *fds, struct client *c)
72 char addr[NI_MAXHOST];
73 const char *ruser, *cissuer, *chash;
76 e = getnameinfo((struct sockaddr*)&c->addr, sizeof(c->addr),
83 if (tls_peer_cert_provided(c->ctx)) {
84 ruser = tls_peer_cert_subject(c->ctx);
85 cissuer = tls_peer_cert_issuer(c->ctx);
86 chash = tls_peer_cert_hash(c->ctx);
93 if (!send_string(exfd, spath)
94 || !send_string(exfd, relpath)
95 || !send_string(exfd, query)
96 || !send_string(exfd, addr)
97 || !send_string(exfd, ruser)
98 || !send_string(exfd, cissuer)
99 || !send_string(exfd, chash)
100 || !send_vhost(exfd, c->host))
104 if ((c->fd = recv_fd(exfd)) == -1) {
105 if (!start_reply(fds, c, TEMP_FAILURE, "internal server error"))
111 c->state = S_SENDING;
112 cgi_poll_on_child(fds, c);
113 /* handle_cgi(fds, c); */
117 /* fatal("cannot talk to the executor process: %s", strerror(errno)); */
118 err(1, "cannot talk to the executor process");
122 cgi_poll_on_child(struct pollfd *fds, struct client *c)
126 if (c->waiting_on_child)
128 c->waiting_on_child = 1;
130 fds->events = POLLIN;
138 cgi_poll_on_client(struct pollfd *fds, struct client *c)
142 if (!c->waiting_on_child)
144 c->waiting_on_child = 0;
152 handle_cgi(struct pollfd *fds, struct client *c)
156 /* ensure c->fd is the child and fds->fd the client */
157 cgi_poll_on_client(fds, c);
161 if ((r = read(c->fd, c->sbuf, sizeof(c->sbuf))) == 0)
164 if (errno == EAGAIN || errno == EWOULDBLOCK) {
165 cgi_poll_on_child(fds, c);
175 switch (r = tls_write(c->ctx, c->sbuf + c->off, c->len)) {
179 case TLS_WANT_POLLOUT:
180 fds->events = POLLOUT;
183 case TLS_WANT_POLLIN:
184 fds->events = POLLIN;