don't munge "=> ?!"
[gemrepl.git] / gemscgi_wrap.c
blobb4fec3b691ad93116a3c247f305e6303c6c9c125
1 #include <signal.h>
2 #include <stdio.h>
3 #include <stdlib.h>
4 #include <string.h>
5 #include <sys/wait.h>
6 #include <unistd.h>
8 #include "gemscgi.h"
10 void respond(void *object, const Request_Info *request_info, int socket)
12 char **args = object;
14 char tlsenv[64+16+1];
15 char queryenv[1024+13+1];
16 char pathenv[1024+10+1];
17 if (request_info->tls_client_hash != NULL) {
18 snprintf(tlsenv, sizeof(tlsenv), "TLS_CLIENT_HASH=%s", request_info->tls_client_hash);
20 if (request_info->query_string_decoded != NULL) {
21 snprintf(queryenv, sizeof(queryenv), "QUERY_STRING=%s", request_info->query_string_decoded);
23 if (request_info->path_info != NULL) {
24 snprintf(pathenv, sizeof(pathenv), "PATH_INFO=%s", request_info->path_info);
27 const pid_t pid = fork();
28 if (pid == -1) {
29 perror("fork");
30 return;
31 } else if (pid > 0) {
32 close(socket);
33 return;
36 putenv(tlsenv);
37 putenv(queryenv);
38 putenv(pathenv);
40 dup2(socket, 1);
41 setbuffer(stdout, NULL, 0);
43 execvp(args[0], args);
44 exit(1);
47 static void usage()
49 printf("Usage: gemscgi_wrap PATH COMMAND [ARG]...\n");
52 int main(int argc, char **argv)
54 if (argc < 2) {
55 usage();
56 exit(1);
59 /* Prevent zombies (on modern posix) */
60 signal(SIGCHLD, SIG_IGN);
62 runSCGI(argv[1], respond, &argv[2]);