Annotation of parser3/src/targets/cgi/parser3.C, revision 1.67
1.27 paf 1: /** @file
2: Parser: scripting and CGI main.
3:
1.43 paf 4: Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.27 paf 5:
1.43 paf 6: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1 paf 7:
1.67 ! paf 8: $Id: parser3.C,v 1.66 2001/04/20 09:04:16 paf Exp $
1.1 paf 9: */
10:
1.40 paf 11: #include "pa_config_includes.h"
1.3 paf 12:
13: #ifdef WIN32
14: # include <windows.h>
15: #endif
1.27 paf 16:
1.37 paf 17: #include "pa_sapi.h"
1.24 paf 18: #include "pa_common.h"
1.5 paf 19: #include "pa_globals.h"
1.2 paf 20: #include "pa_request.h"
1.57 paf 21: #include "pa_socks.h"
1.65 paf 22:
23: /// @test disable /cgi-bin/parser3/auto.p
1.1 paf 24:
1.42 paf 25: /// IIS refuses to read bigger chunks
26: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
27:
1.45 paf 28: const char *argv0;
1.42 paf 29: Pool pool(0); // global pool [dont describe to doxygen: it confuses it with param names]
1.27 paf 30: bool cgi; ///< we were started as CGI?
1.5 paf 31:
1.40 paf 32: #ifdef WIN32
33: /// global system errors into parser exceptions converter
1.43 paf 34: static LONG WINAPI TopLevelExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo) {
1.5 paf 35: char buf[MAX_STRING];
36: if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.36 paf 37: struct _EXCEPTION_RECORD *er=ExceptionInfo->ExceptionRecord;
1.46 paf 38: snprintf(buf, MAX_STRING, "Exception 0x%X at %p",
1.31 paf 39: er->ExceptionCode,
40: er->ExceptionAddress);
1.5 paf 41: } else
42: strcpy(buf, "Exception <unknown>");
43:
44: PTHROW(0, 0,
45: 0,
46: buf);
47:
48: return EXCEPTION_EXECUTE_HANDLER; // never reached
49: }
1.27 paf 50: #endif
51:
1.46 paf 52: // SAPI
1.61 paf 53: // appends to parser3.log located beside my binary
54: void SAPI::log(Pool& pool, const char *fmt, ...) {
55: bool opened;
56: FILE *f=0;
57:
58: if(argv0) {
59: // beside by binary
60: char file_spec[MAX_STRING];
61: strncpy(file_spec, argv0, MAX_STRING); // filespec of my binary
62: rsplit(file_spec, '/'); rsplit(file_spec, '\\');// strip filename
63: strcat(file_spec, "/parser3.log");
64: f=fopen(file_spec, "at");
65: }
66: opened=f!=0;
67: if(!opened)
68: f=stderr;
69:
70: // prefix
71: time_t t=time(0);
72: const char *stamp=ctime(&t);
73: fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
74: // message
75: va_list args;
76: va_start(args,fmt);
77: vfprintf(f, fmt, args);
78: va_end(args);
79: // newline
80: fprintf(f, "\n");
81:
82: if(opened)
83: fclose(f);
84: }
85:
1.37 paf 86: const char *SAPI::get_env(Pool& pool, const char *name) {
1.28 paf 87: return getenv(name);
88: }
89:
1.59 paf 90: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
91: size_t read_size=0;
1.12 paf 92: do {
1.36 paf 93: int chunk_size=read(fileno(stdin),
1.42 paf 94: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.12 paf 95: if(chunk_size<0)
96: break;
97: read_size+=chunk_size;
98: } while(read_size<max_bytes);
99:
100: return read_size;
1.10 paf 101: }
102:
1.37 paf 103: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.66 paf 104: if(1||cgi)
1.20 paf 105: printf("%s: %s\n", key, value);
1.19 paf 106: }
107:
1.56 paf 108: /// @todo intelligent cache-control
1.37 paf 109: void SAPI::send_header(Pool& pool) {
1.33 paf 110: if(cgi) {
1.55 paf 111: puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 112:
113: // header | body delimiter
1.20 paf 114: puts("");
1.33 paf 115: }
1.30 paf 116: }
1.20 paf 117:
1.53 paf 118: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.19 paf 119: stdout_write(buf, size);
1.58 paf 120: }
121:
1.40 paf 122: /**
123: main workhorse
1.19 paf 124:
1.56 paf 125: @todo
1.40 paf 126: IIS: remove trailing default-document[index.html] from $request.uri.
127: to do that we need to consult metabase,
128: wich is tested but seems slow.
129: */
1.5 paf 130: int main(int argc, char *argv[]) {
1.45 paf 131: argv0=argv[0];
132:
1.32 paf 133: umask(2);
134:
135: #ifdef WIN32
1.27 paf 136: setmode(fileno(stdin), _O_BINARY);
137: setmode(fileno(stdout), _O_BINARY);
138: setmode(fileno(stderr), _O_BINARY);
1.32 paf 139: #endif
1.12 paf 140:
1.3 paf 141: // were we started as CGI?
1.20 paf 142: cgi=
1.2 paf 143: getenv("SERVER_SOFTWARE") ||
144: getenv("SERVER_NAME") ||
145: getenv("GATEWAY_INTERFACE") ||
146: getenv("REQUEST_METHOD");
1.5 paf 147:
1.10 paf 148: if(!cgi) {
149: if(argc<2) {
1.45 paf 150: printf("Usage: %s <file>\n", argv0?argv0:"parser3");
1.67 ! paf 151: return 1;
1.10 paf 152: }
153: }
154:
1.26 paf 155: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 156: #ifdef WIN32
1.43 paf 157: back_slashes_to_slashes(filespec_to_process);
1.36 paf 158: #endif
1.10 paf 159:
1.35 paf 160: const char *request_method=getenv("REQUEST_METHOD");
161: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5 paf 162: PTRY { // global try
163: // must be first in PTRY{}PCATCH
1.40 paf 164: #ifdef WIN32
1.5 paf 165: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
166: #endif
1.2 paf 167:
1.62 paf 168: // init socks
1.57 paf 169: init_socks(pool);
170:
1.10 paf 171: // init global variables
1.37 paf 172: pa_globals_init(pool);
1.10 paf 173:
1.13 paf 174: if(!filespec_to_process)
175: PTHROW(0, 0,
176: 0,
177: "no file to process");
178:
1.10 paf 179: // Request info
180: Request::Info request_info;
1.39 paf 181: if(cgi) {
1.51 paf 182: if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
1.39 paf 183: request_info.document_root=env_document_root;
1.51 paf 184: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
1.40 paf 185: // IIS
1.39 paf 186: size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40 paf 187: char *buf=(char *)pool.malloc(len+1);
188: strncpy(buf, filespec_to_process, len);
1.39 paf 189: buf[len]=0;
190: request_info.document_root=buf;
191: } else
192: PTHROW(0, 0,
193: 0,
1.43 paf 194: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.39 paf 195: } else {
196: static char buf[MAX_STRING];
197: strncpy(buf, filespec_to_process, MAX_STRING);
198: rsplit(buf, '/'); rsplit(buf, '\\');// strip filename
199: request_info.document_root=buf;
1.13 paf 200: }
1.10 paf 201: request_info.path_translated=filespec_to_process;
1.35 paf 202: request_info.method=request_method;
1.51 paf 203: const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
1.40 paf 204: request_info.query_string=query_string;
1.42 paf 205: if(cgi)
1.51 paf 206: if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
1.42 paf 207: request_info.uri=env_request_uri;
1.51 paf 208: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
1.42 paf 209: if(query_string) {
210: char *reconstructed_uri=(char *)malloc(
211: strlen(path_info)+1/*'?'*/+
212: strlen(query_string)+1/*0*/);
213: strcpy(reconstructed_uri, path_info);
214: strcat(reconstructed_uri, "?");
215: strcat(reconstructed_uri, query_string);
216: request_info.uri=reconstructed_uri;
217: } else
218: request_info.uri=path_info;
219: else
220: PTHROW(0, 0,
221: 0,
1.43 paf 222: "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.42 paf 223: else
224: request_info.uri=0;
1.40 paf 225:
1.51 paf 226: request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
227: const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
1.10 paf 228: request_info.content_length=(content_length?atoi(content_length):0);
1.51 paf 229: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
230: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
1.10 paf 231:
1.5 paf 232: // prepare to process request
1.38 paf 233: Request request(pool,
1.10 paf 234: request_info,
1.66 paf 235: cgi ? String::UL_HTML_TYPO : String::UL_AS_IS
1.5 paf 236: );
237:
238: // some root-controlled location
1.3 paf 239: #ifdef WIN32
1.10 paf 240: // c:\windows
1.36 paf 241: static char root_auto_path[MAX_STRING];
1.30 paf 242: GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3 paf 243: #else
1.40 paf 244: // ~nobody todo: figure out a better place
1.64 paf 245: const char *root_auto_path=SAPI::get_env(pool, "HOME");
1.5 paf 246: #endif
247:
248: // beside by binary
1.36 paf 249: static char site_auto_path[MAX_STRING];
1.30 paf 250: strncpy(site_auto_path, argv[0], MAX_STRING); // filespec of my binary
251: rsplit(site_auto_path, '/'); rsplit(site_auto_path, '\\');// strip filename
1.5 paf 252:
253: // process the request
1.35 paf 254: request.core(
1.30 paf 255: root_auto_path, false,
1.31 paf 256: site_auto_path, false,
1.35 paf 257: header_only);
1.57 paf 258:
259: //
260: done_socks();
1.16 paf 261:
1.22 paf 262: // must be last in PTRY{}PCATCH
1.40 paf 263: #ifdef WIN32
1.5 paf 264: SetUnhandledExceptionFilter(0);
1.25 paf 265: #endif
1.16 paf 266: // successful finish
267: return 0;
268: } PCATCH(e) { // global problem
1.43 paf 269: // must be first in PCATCH{}
270: #ifdef WIN32
271: SetUnhandledExceptionFilter(0);
272: #endif
1.44 paf 273: // don't allocate anything on pool here:
274: // possible pool' exception not catch-ed now
275: // and there could be out-of-memory exception
1.43 paf 276:
1.19 paf 277: const char *body=e.comment();
1.44 paf 278: // log it
279: SAPI::log(pool, "exception in request exception handler: %s", body);
280:
281: //
1.19 paf 282: int content_length=strlen(body);
1.5 paf 283:
1.35 paf 284: // prepare header
1.37 paf 285: SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19 paf 286: char content_length_cstr[MAX_NUMBER];
1.60 paf 287: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.37 paf 288: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35 paf 289:
290: // send header
1.37 paf 291: SAPI::send_header(pool);
1.19 paf 292:
293: // body
1.35 paf 294: if(!header_only)
1.37 paf 295: SAPI::send_body(pool, body, content_length);
1.2 paf 296:
1.16 paf 297: // unsuccessful finish
298: return 1;
299: }
300: PEND_CATCH
1.1 paf 301: }
E-mail: