43b7d945aa439ea36ef54ada5bd8534716fd0d00
[userv-utils] / www-cgi / ucgicommon.c
1 /*
2 * Copyright (C) 1998-1999,2003 Ian Jackson
3 *
4 * This is free software; you can redistribute it and/or modify it
5 * under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
8 *
9 * This program is distributed in the hope that it will be useful, but
10 * WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * General Public License for more details.
13 *
14 * You should have received a copy of the GNU General Public License
15 * along with userv-utils; if not, write to the Free Software
16 * Foundation, 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
17 *
18 * $Id$
19 */
20
21 #include <stdio.h>
22 #include <string.h>
23 #include <errno.h>
24
25 #include "ucgi.h"
26
27 const char *const envok[]= {
28 "AUTH_TYPE",
29 "CONTENT_LENGTH",
30 "CONTENT_TYPE",
31 "DOCUMENT_ROOT",
32 "GATEWAY_INTERFACE",
33 "HTTP_ACCEPT",
34 "HTTP_ACCEPT_CHARSET",
35 "HTTP_ACCEPT_ENCODING",
36 "HTTP_ACCEPT_LANGUAGE",
37 "HTTP_CACHE_CONTROL",
38 "HTTP_CONNECTION",
39 "HTTP_CONTENT_ENCODING",
40 "HTTP_COOKIE",
41 "HTTP_DNT",
42 "HTTP_HOST",
43 "HTTP_KEEP_ALIVE",
44 "HTTP_NEGOTIATE",
45 "HTTP_PRAGMA",
46 "HTTP_REFERER",
47 "HTTP_USER_AGENT",
48 "HTTP_VIA",
49 "HTTP_X_FORWARDED_FOR",
50 "HTTPS",
51 "PATH_INFO",
52 "PATH_TRANSLATED",
53 "QUERY_STRING",
54 "REMOTE_ADDR",
55 "REMOTE_HOST",
56 "REMOTE_USER",
57 "REMOTE_IDENT",
58 "REQUEST_METHOD",
59 "REQUEST_URI",
60 "SCRIPT_FILENAME",
61 "SCRIPT_NAME",
62 "SCRIPT_URI",
63 "SCRIPT_URL",
64 "SERVER_ADDR",
65 "SERVER_ADMIN",
66 "SERVER_NAME",
67 "SERVER_PORT",
68 "SERVER_PROTOCOL",
69 "SERVER_SIGNATURE",
70 "SERVER_SOFTWARE",
71 0
72 };
73 const int nenvok= sizeof(envok)/sizeof(envok[0]);
74
75 int debugmode= 0;
76
77 static void outerror(void) {
78 perror("stdout");
79 exit(debugmode ? 0 : -1);
80 }
81
82 void syserror(const char *m) {
83 if (printf("Content-Type: text/plain\n\n"
84 "ucgi: system call error:\n"
85 "%s: %s\n",
86 m,strerror(errno))==EOF || fflush(stdout)) outerror();
87 exit(0);
88 }
89
90 void error(const char *m) {
91 if (printf("Content-Type: text/plain\n\n"
92 "ucgi: error:\n"
93 "%s\n",
94 m)==EOF || fflush(stdout)) outerror();
95 exit(0);
96 }
97
98 void *xmalloc(size_t sz) {
99 void *r;
100
101 r= malloc(sz);
102 if (!r) syserror("malloc failed");
103 return r;
104 }
105
106 void *xrealloc(void *ptr, size_t sz) {
107 void *r;
108
109 r= realloc(ptr,sz);
110 if (!r) syserror("realloc failed");
111 return r;
112 }
113
114 void xsetenv(const char *en, const char *ev, int overwrite) {
115 if (setenv(en,ev,overwrite)) syserror("setenv");
116 }