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