Annotation of parser3/src/targets/cgi/parser3.C, revision 1.136
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.128 paf 5: Author: Alexander Petrosyan <paf@design.ru>(http://paf.design.ru)
1.27 paf 6:
1.136 ! paf 7: $Id: parser3.C,v 1.135 2001/11/16 11:06:08 paf Exp $
1.1 paf 8: */
9:
1.40 paf 10: #include "pa_config_includes.h"
1.3 paf 11:
12: #ifdef WIN32
13: # include <windows.h>
1.131 paf 14: # include <new.h>
1.3 paf 15: #endif
1.27 paf 16:
1.37 paf 17: #include "pa_sapi.h"
1.76 paf 18: #include "classes.h"
1.24 paf 19: #include "pa_common.h"
1.2 paf 20: #include "pa_request.h"
1.57 paf 21: #include "pa_socks.h"
1.68 paf 22: #include "pa_version.h"
1.125 parser 23: #include "pool_storage.h"
1.69 paf 24:
1.120 parser 25: #ifdef XML
26: #include <XalanTransformer/XalanCAPI.h>
27: #endif
28:
1.127 paf 29: //#define DEBUG_POOL_MALLOC
1.84 parser 30:
1.109 parser 31: // consts
1.113 parser 32:
33: extern const char *main_RCSIds[];
1.116 parser 34: #ifdef USE_SMTP
1.113 parser 35: extern const char *smtp_RCSIds[];
1.114 parser 36: #endif
1.113 parser 37: extern const char *gd_RCSIds[];
38: extern const char *classes_RCSIds[];
39: extern const char *types_RCSIds[];
1.115 parser 40: extern const char *parser3_RCSIds[];
1.119 parser 41: #ifdef XML
42: extern const char *xalan_patched_RCSIds[];
43: #endif
1.113 parser 44: const char **RCSIds[]={
45: main_RCSIds,
1.116 parser 46: #ifdef USE_SMTP
1.113 parser 47: smtp_RCSIds,
1.114 parser 48: #endif
1.113 parser 49: gd_RCSIds,
50: classes_RCSIds,
51: types_RCSIds,
1.115 parser 52: parser3_RCSIds,
1.118 parser 53: #ifdef XML
54: xalan_patched_RCSIds,
55: #endif
1.113 parser 56: 0
57: };
1.84 parser 58:
1.42 paf 59: /// IIS refuses to read bigger chunks
60: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
61:
1.45 paf 62: const char *argv0;
1.125 parser 63: Pool_storage pool_storage;
64: Pool pool(&pool_storage); // global pool [dont describe to doxygen: it confuses it with param names]
1.27 paf 65: bool cgi; ///< we were started as CGI?
1.5 paf 66:
1.46 paf 67: // SAPI
1.86 parser 68:
1.124 parser 69: static void log(const char *fmt, va_list args) {
1.61 paf 70: bool opened;
71: FILE *f=0;
72:
73: if(argv0) {
74: // beside by binary
75: char file_spec[MAX_STRING];
1.98 parser 76: strncpy(file_spec, argv0, MAX_STRING-1); file_spec[MAX_STRING-1]=0; // filespec of my binary
1.61 paf 77: rsplit(file_spec, '/'); rsplit(file_spec, '\\');// strip filename
78: strcat(file_spec, "/parser3.log");
79: f=fopen(file_spec, "at");
80: }
81: opened=f!=0;
82: if(!opened)
83: f=stderr;
84:
85: // prefix
86: time_t t=time(0);
87: const char *stamp=ctime(&t);
88: fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
89: // message
1.117 parser 90:
91: char buf[MAX_STRING];
92: size_t size=vsnprintf(buf, MAX_STRING, fmt, args);
93: remove_crlf(buf, buf+size);
94:
95: fwrite(buf, size, 1, f);
1.61 paf 96: // newline
97: fprintf(f, "\n");
98:
99: if(opened)
100: fclose(f);
1.85 parser 101: else
102: fflush(f);
1.124 parser 103: }
104:
105: // appends to parser3.log located beside my binary if openable, to stderr otherwize
106: void SAPI::log(Pool& , const char *fmt, ...) {
107: va_list args;
108: va_start(args,fmt);
109: ::log(fmt, args);
110: va_end(args);
111: }
112:
113: void SAPI::die(const char *fmt, ...) {
114: va_list args;
115: va_start(args,fmt);
116: ::log(fmt, args);
117: va_end(args);
118:
1.134 paf 119: char body[MAX_STRING];
120: size_t size=vsnprintf(body, MAX_STRING, fmt, args);
121:
122: //
123: int content_length=strlen(body);
124:
125: // prepare header
126: // let's be honest, that's bad we couldn't produce valid output
127: SAPI::add_header_attribute(pool, "status", "500");
128: SAPI::add_header_attribute(pool, "content-type", "text/plain");
129: char content_length_cstr[MAX_NUMBER];
130: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
131: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
132:
133: // send header
134: SAPI::send_header(pool);
135:
136: // body
137: SAPI::send_body(pool, body, content_length);
138:
1.124 parser 139: exit(1);
1.61 paf 140: }
141:
1.122 parser 142: const char *SAPI::get_env(Pool& , const char *name) {
1.109 parser 143: return getenv(name);
1.28 paf 144: }
145:
1.122 parser 146: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59 paf 147: size_t read_size=0;
1.12 paf 148: do {
1.36 paf 149: int chunk_size=read(fileno(stdin),
1.42 paf 150: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129 paf 151: if(chunk_size<=0)
1.12 paf 152: break;
153: read_size+=chunk_size;
154: } while(read_size<max_bytes);
155:
156: return read_size;
1.10 paf 157: }
158:
1.122 parser 159: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68 paf 160: if(cgi)
1.20 paf 161: printf("%s: %s\n", key, value);
1.19 paf 162: }
163:
1.56 paf 164: /// @todo intelligent cache-control
1.122 parser 165: void SAPI::send_header(Pool& ) {
1.33 paf 166: if(cgi) {
1.55 paf 167: puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 168:
169: // header | body delimiter
1.20 paf 170: puts("");
1.33 paf 171: }
1.30 paf 172: }
1.20 paf 173:
1.122 parser 174: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19 paf 175: stdout_write(buf, size);
1.58 paf 176: }
177:
1.97 parser 178: //
179:
180: char *full_file_spec(char *file_name) {
1.108 parser 181: if(file_name && !strchr(file_name, '/')) {
1.97 parser 182: static char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING);
183: static char buf[MAX_STRING];
184: snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
185: return buf;
186: }
187: return file_name;
188: }
189:
1.40 paf 190: /**
1.122 parser 191: main workhorse
1.19 paf 192:
1.122 parser 193: @todo
1.40 paf 194: IIS: remove trailing default-document[index.html] from $request.uri.
195: to do that we need to consult metabase,
196: wich is tested but seems slow.
197: */
1.122 parser 198: void real_parser_handler(
199: const char *filespec_to_process,
200: const char *request_method, bool header_only) {
201: // init socks
202: init_socks(pool);
203:
204: #ifdef XML
205: /**
206: * Initialize Xerces and Xalan.
207: *
208: * Should be called only once per process before making
209: * any other API calls.
210: */
211: //_asm int 3;
212: XalanInitialize();
213: #endif
214:
215: // init global classes
216: init_methoded_array(pool);
217: // init global variables
218: pa_globals_init(pool);
219:
220: if(!filespec_to_process)
221: throw Exception(0, 0,
222: 0,
223: "Parser/%s", PARSER_VERSION);
224:
225: // Request info
226: Request::Info request_info;
227: if(cgi) {
228: if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
229: request_info.document_root=env_document_root;
230: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
231: // IIS
232: size_t len=strlen(filespec_to_process)-strlen(path_info);
233: char *buf=(char *)pool.malloc(len+1);
234: memcpy(buf, filespec_to_process, len); buf[len]=0;
235: request_info.document_root=buf;
236: } else
237: throw Exception(0, 0,
238: 0,
239: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
240: } else {
241: static char buf[MAX_STRING];
242: strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0;
243: if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
244: request_info.document_root=buf;
245: else
246: request_info.document_root="";
247: }
248: request_info.path_translated=filespec_to_process;
249: request_info.method=request_method ? request_method : "GET";
250: const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
251: request_info.query_string=query_string;
252: if(cgi) {
253: if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
254: request_info.uri=env_request_uri;
255: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
256: if(query_string) {
257: char *reconstructed_uri=(char *)pool.malloc(
258: strlen(path_info)+1/*'?'*/+
259: strlen(query_string)+1/*0*/);
260: strcpy(reconstructed_uri, path_info);
261: strcat(reconstructed_uri, "?");
262: strcat(reconstructed_uri, query_string);
263: request_info.uri=reconstructed_uri;
264: } else
265: request_info.uri=path_info;
266: else
267: throw Exception(0, 0,
268: 0,
269: "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
270:
271: if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
272: size_t script_name_len=strlen(script_name);
273: size_t uri_len=strlen(request_info.uri);
274: if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
275: script_name_len != uri_len) // under IIS they are the same
276: throw Exception(0, 0,
277: 0,
278: "CGI: illegal call");
279: }
280: } else
281: request_info.uri=0;
282:
283: request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
284: const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
285: request_info.content_length=(content_length?atoi(content_length):0);
286: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
287: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
288:
289: // prepare to process request
290: Request request(pool,
291: request_info,
1.136 ! paf 292: cgi ? String::UL_OPTIMIZED_HTML : String::UL_AS_IS,
1.130 paf 293: true /* status_allowed */);
1.122 parser 294:
295: // some root-controlled location
296: #ifdef SYSCONFDIR
297: const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME;
298: #else
299: # ifdef WIN32
300: // c:\windows
301: char root_config_path[MAX_STRING];
302: GetWindowsDirectory(root_config_path, MAX_STRING);
303:
304: char root_config_filespec[MAX_STRING];
305: snprintf(root_config_filespec, MAX_STRING,
306: "%s/%s",
307: root_config_path, CONFIG_FILE_NAME);
308: # else
309: #error must be compiled either configure/make or MSVC++
310: # endif
311: #endif
312:
313: // beside by binary
314: // @todo full path, not ./!
315: static char site_config_path[MAX_STRING];
316: strncpy(site_config_path, argv0, MAX_STRING-1); site_config_path[MAX_STRING-1]=0; // filespec of my binary
317: if(!(
318: rsplit(site_config_path, '/') ||
319: rsplit(site_config_path, '\\'))) { // strip filename
320: // no path, just filename
321: site_config_path[0]='.'; site_config_path[1]=0;
322: }
323:
324: char site_config_filespec[MAX_STRING];
325: snprintf(site_config_filespec, MAX_STRING,
326: "%s/%s",
327: site_config_path, CONFIG_FILE_NAME);
328:
329: // process the request
330: request.core(
331: root_config_filespec, false,
332: site_config_filespec, false,
333: header_only);
334:
335: //
336: done_socks();
337:
338: #ifdef DEBUG_POOL_MALLOC
339: extern void log_pool_stats(Pool& pool);
340: log_pool_stats(pool);
341: #endif
342: }
343:
344: void call_real_parser_handler__do_SEH(
345: const char *filespec_to_process,
346: const char *request_method, bool header_only) {
1.133 paf 347: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 348: LPEXCEPTION_POINTERS system_exception=0;
349: __try {
350: #endif
351: real_parser_handler(
352: filespec_to_process,
353: request_method, header_only);
354:
1.133 paf 355: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 356: } __except (
357: (system_exception=GetExceptionInformation()),
358: EXCEPTION_EXECUTE_HANDLER) {
359:
360: if(system_exception)
361: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
362: throw Exception(0, 0,
363: 0,
364: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
365: else
366: throw Exception(0, 0, 0, "Exception <no exception record>");
367: else
368: throw Exception(0, 0, 0, "Exception <no exception information>");
369: }
370: #endif
371: }
372:
1.135 paf 373: #ifdef WIN32
374: int failed_new(size_t size) {
375: SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
376: return 0; // not reached
1.131 paf 377: }
1.135 paf 378: #endif
1.131 paf 379:
1.135 paf 380: #ifdef HAVE_SET_NEW_HANDLER
1.134 paf 381: void failed_new() {
1.135 paf 382: SAPI::die("out of memory in 'new'");
1.131 paf 383: }
384: #endif
385:
1.5 paf 386: int main(int argc, char *argv[]) {
1.45 paf 387: argv0=argv[0];
388:
1.32 paf 389: umask(2);
390:
1.3 paf 391: // were we started as CGI?
1.20 paf 392: cgi=
1.109 parser 393: getenv("SERVER_SOFTWARE") ||
394: getenv("SERVER_NAME") ||
395: getenv("GATEWAY_INTERFACE") ||
396: getenv("REQUEST_METHOD");
1.5 paf 397:
1.10 paf 398: if(!cgi) {
399: if(argc<2) {
1.69 paf 400: printf(
1.100 parser 401: "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
1.128 paf 402: "Author: Alexander Petrosyan <paf@design.ru>(http://paf.design.ru)\n"
1.100 parser 403: "\n"
404: "Usage: %s <file>\n",
1.69 paf 405: PARSER_VERSION,
406: argv0?argv0:"parser3");
1.67 paf 407: return 1;
1.10 paf 408: }
409: }
410:
1.100 parser 411: #ifdef WIN32
412: setmode(fileno(stdin), _O_BINARY);
413: setmode(fileno(stdout), _O_BINARY);
414: setmode(fileno(stderr), _O_BINARY);
415: #endif
416:
1.109 parser 417: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 418: #ifdef WIN32
1.43 paf 419: back_slashes_to_slashes(filespec_to_process);
1.36 paf 420: #endif
1.97 parser 421: filespec_to_process=full_file_spec(filespec_to_process);
1.10 paf 422:
1.109 parser 423: const char *request_method=getenv("REQUEST_METHOD");
1.35 paf 424: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131 paf 425:
426: #ifdef WIN32
427: _set_new_handler(failed_new);
428: #endif
429:
430: #ifdef HAVE_SET_NEW_HANDLER
1.132 paf 431: std::set_new_handler(failed_new);
1.131 paf 432: #endif
433:
1.122 parser 434: try { // global try
435: call_real_parser_handler__do_SEH(
436: filespec_to_process,
437: request_method, header_only);
438: } catch(const Exception& e) { // global problem
1.44 paf 439: // don't allocate anything on pool here:
440: // possible pool' exception not catch-ed now
441: // and there could be out-of-memory exception
1.43 paf 442:
1.134 paf 443: SAPI::die("exception in request exception handler: ", e.comment());
444: #ifndef _DEBUG
1.131 paf 445: } catch(...) {
1.134 paf 446: SAPI::die("<unknown exception>");
1.133 paf 447: #endif
1.16 paf 448: }
1.122 parser 449:
1.109 parser 450:
451: #ifndef WIN32
452: //
453: if(!cgi)
454: SAPI::send_body(pool, "\n", 1);
455: #endif
1.134 paf 456: return 0;
1.1 paf 457: }
E-mail: