Annotation of parser3/src/targets/cgi/parser3.C, revision 1.198
1.27 paf 1: /** @file
2: Parser: scripting and CGI main.
3:
1.155 paf 4: Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)
1.154 paf 5: Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
1.189 paf 6: */
1.27 paf 7:
1.198 ! paf 8: static const char* IDENT_PARSER3_C="$Date: 2002/08/23 07:32:24 $";
1.1 paf 9:
1.40 paf 10: #include "pa_config_includes.h"
1.3 paf 11:
1.139 paf 12: #if _MSC_VER
1.131 paf 13: # include <new.h>
1.148 paf 14: # include <crtdbg.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.149 paf 25: #ifdef WIN32
26: # include <windows.h>
1.184 paf 27: # include "getopt.h"
28: #else
29: # include <getopt.h>
1.120 parser 30: #endif
31:
1.158 paf 32: //#define DEBUG_POOL_MALLOC
1.162 paf 33: //#define DEBUG_STRING_APPENDS_VS_EXPANDS
1.196 paf 34: //#define DEBUG_MAILRECEIVE "mailreceive.eml"
1.160 paf 35:
36: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
37: extern ulong
38: string_piece_appends,
39: wcontext_result_size,
40: total_alloc_size,
41: string_string_shortcut_economy;
42: #endif
1.84 parser 43:
1.109 parser 44: // consts
1.84 parser 45:
1.175 paf 46: #define REDIRECT_PREFIX "REDIRECT_"
1.181 paf 47: #define PARSER_CONFIG_ENV_NAME "CGI_PARSER_CONFIG"
1.159 paf 48:
1.42 paf 49: /// IIS refuses to read bigger chunks
50: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
51:
1.184 paf 52: static const char *argv0;
1.193 paf 53: static const char *config_filespec_cstr=0;
54: static bool fail_on_config_read_problem=true;
1.192 paf 55:
1.198 ! paf 56: static Pool_storage global_pool_storage; ///< global pool storage
! 57: static Pool global_pool(&global_pool_storage); ///< global pool
1.184 paf 58: static bool cgi; ///< we were started as CGI?
59: static bool mail_received=false; ///< we were started with -m option? [asked to parse incoming message to $mail:received]
1.5 paf 60:
1.46 paf 61: // SAPI
1.86 parser 62:
1.124 parser 63: static void log(const char *fmt, va_list args) {
1.193 paf 64: bool opened=false;
1.61 paf 65: FILE *f=0;
66:
1.193 paf 67: if(config_filespec_cstr) {
68: char beside_config_path[MAX_STRING];
69: strncpy(beside_config_path, config_filespec_cstr, MAX_STRING-1); beside_config_path[MAX_STRING-1]=0;
70: if(!(
71: rsplit(beside_config_path, '/') ||
72: rsplit(beside_config_path, '\\'))) { // strip filename
73: // no path, just filename
74: beside_config_path[0]='.'; beside_config_path[1]=0;
75: }
76:
77: char file_spec[MAX_STRING];
78: snprintf(file_spec, MAX_STRING,
79: "%s/parser3.log", beside_config_path);
80: f=fopen(file_spec, "at");
81: opened=f!=0;
82: }
1.192 paf 83: // fallback to stderr
1.61 paf 84: if(!opened)
85: f=stderr;
86:
87: // prefix
88: time_t t=time(0);
1.171 paf 89: if(const char *stamp=ctime(&t)) { // never saw that
1.173 paf 90: if(size_t len=strlen(stamp)) // saw once stamp being =""
1.172 paf 91: fprintf(f, "[%.*s] ", len-1, stamp);
1.171 paf 92: }
1.61 paf 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);
1.198 ! paf 120: log_pool_stats(global_pool);
1.137 paf 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
1.198 ! paf 141: SAPI::add_header_attribute(global_pool, "status", "500");
! 142: SAPI::add_header_attribute(global_pool, "content-type", "text/plain");
1.134 paf 143: char content_length_cstr[MAX_NUMBER];
1.168 paf 144: snprintf(content_length_cstr, sizeof(content_length_cstr), "%u", content_length);
1.198 ! paf 145: SAPI::add_header_attribute(global_pool, "content-length", content_length_cstr);
1.134 paf 146:
147: // send header
1.198 ! paf 148: SAPI::send_header(global_pool);
1.134 paf 149:
150: // body
1.198 ! paf 151: SAPI::send_body(global_pool, body, content_length);
1.134 paf 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.180 paf 160: const char *const *SAPI::environment(Pool&) {
161: #ifdef _MSC_VER
162: extern char **_environ;
163: return _environ;
164: #else
165: extern char **environ;
166: return environ;
167: #endif
168: }
169:
1.122 parser 170: size_t SAPI::read_post(Pool& , char *buf, size_t max_bytes) {
1.59 paf 171: size_t read_size=0;
1.12 paf 172: do {
1.36 paf 173: int chunk_size=read(fileno(stdin),
1.42 paf 174: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.129 paf 175: if(chunk_size<=0)
1.12 paf 176: break;
177: read_size+=chunk_size;
178: } while(read_size<max_bytes);
179:
180: return read_size;
1.10 paf 181: }
182:
1.122 parser 183: void SAPI::add_header_attribute(Pool& , const char *key, const char *value) {
1.68 paf 184: if(cgi)
1.20 paf 185: printf("%s: %s\n", key, value);
1.19 paf 186: }
187:
1.56 paf 188: /// @todo intelligent cache-control
1.122 parser 189: void SAPI::send_header(Pool& ) {
1.33 paf 190: if(cgi) {
1.147 paf 191: // puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 192:
193: // header | body delimiter
1.20 paf 194: puts("");
1.33 paf 195: }
1.30 paf 196: }
1.20 paf 197:
1.122 parser 198: void SAPI::send_body(Pool& , const void *buf, size_t size) {
1.19 paf 199: stdout_write(buf, size);
1.58 paf 200: }
201:
1.97 parser 202: //
203:
1.184 paf 204: static void full_file_spec(const char *file_name, char *buf, size_t buf_size) {
1.167 paf 205: if(file_name)
206: if(file_name[0]=='/'
207: #ifdef WIN32
208: || (file_name[0] && file_name[1]==':')
209: #endif
210: )
211: strncpy(buf, file_name, buf_size);
212: else {
213: char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING);
214: snprintf(buf, buf_size, "%s/%s", cwd, file_name);
215: }
216: else
217: buf[0]=0;
1.166 paf 218: #ifdef WIN32
219: back_slashes_to_slashes(buf);
220: #endif
1.97 parser 221: }
222:
1.40 paf 223: /**
1.122 parser 224: main workhorse
1.19 paf 225:
1.122 parser 226: @todo
1.40 paf 227: IIS: remove trailing default-document[index.html] from $request.uri.
228: to do that we need to consult metabase,
229: wich is tested but seems slow.
1.144 paf 230: IIS5 todo find out proper 'illegal call' check
1.40 paf 231: */
1.184 paf 232: static void real_parser_handler(
1.122 parser 233: const char *filespec_to_process,
234: const char *request_method, bool header_only) {
235: // init socks
1.198 ! paf 236: init_socks(global_pool);
1.122 parser 237:
238: // init global classes
1.198 ! paf 239: init_methoded_array(global_pool);
1.122 parser 240: // init global variables
1.198 ! paf 241: pa_globals_init(global_pool);
1.122 parser 242:
1.198 ! paf 243: // request pool, must be different ptr from global [used in VStateless_class.add_method]
! 244: Pool request_pool(&global_pool_storage);
! 245:
1.186 paf 246: if(!filespec_to_process || !*filespec_to_process)
1.144 paf 247: SAPI::die("Parser/%s", PARSER_VERSION);
1.122 parser 248:
249: // Request info
250: Request::Info request_info;
1.166 paf 251: char document_root_buf[MAX_STRING];
1.122 parser 252: if(cgi) {
1.198 ! paf 253: if(const char *env_document_root=SAPI::get_env(request_pool, "DOCUMENT_ROOT"))
1.122 parser 254: request_info.document_root=env_document_root;
1.198 ! paf 255: else if(const char *path_info=SAPI::get_env(request_pool, "PATH_INFO")) {
1.122 parser 256: // IIS
1.166 paf 257: size_t len=min(sizeof(document_root_buf)-1, strlen(filespec_to_process)-strlen(path_info));
258: memcpy(document_root_buf, filespec_to_process, len); document_root_buf[len]=0;
259: request_info.document_root=document_root_buf;
1.122 parser 260: } else
1.165 paf 261: throw Exception("parser.runtime",
262: 0,
263: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.122 parser 264: } else {
1.166 paf 265: full_file_spec("", document_root_buf, sizeof(document_root_buf));
266: request_info.document_root=document_root_buf;
1.122 parser 267: }
268: request_info.path_translated=filespec_to_process;
269: request_info.method=request_method ? request_method : "GET";
1.198 ! paf 270: const char *query_string=SAPI::get_env(request_pool, "QUERY_STRING");
1.122 parser 271: request_info.query_string=query_string;
272: if(cgi) {
1.198 ! paf 273: if(const char *env_request_uri=SAPI::get_env(request_pool, "REQUEST_URI"))
1.122 parser 274: request_info.uri=env_request_uri;
1.198 ! paf 275: else if(const char *path_info=SAPI::get_env(request_pool, "PATH_INFO"))
1.122 parser 276: if(query_string) {
1.198 ! paf 277: char *reconstructed_uri=(char *)request_pool.malloc(
1.122 parser 278: strlen(path_info)+1/*'?'*/+
279: strlen(query_string)+1/*0*/);
280: strcpy(reconstructed_uri, path_info);
281: strcat(reconstructed_uri, "?");
282: strcat(reconstructed_uri, query_string);
283: request_info.uri=reconstructed_uri;
284: } else
285: request_info.uri=path_info;
1.179 paf 286: else
287: throw Exception("parser.runtime",
288: 0,
289: "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.145 paf 290:
291: #ifndef WIN32
292: // they've changed this under IIS5.
1.198 ! paf 293: if(const char *script_name=SAPI::get_env(request_pool, "SCRIPT_NAME")) {
1.122 parser 294: size_t script_name_len=strlen(script_name);
295: size_t uri_len=strlen(request_info.uri);
296: if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
297: script_name_len != uri_len) // under IIS they are the same
1.144 paf 298: SAPI::die("CGI: illegal call");
1.122 parser 299: }
1.145 paf 300: #endif
1.122 parser 301: } else
1.177 paf 302: request_info.uri="";
1.122 parser 303:
1.198 ! paf 304: request_info.content_type=SAPI::get_env(request_pool, "CONTENT_TYPE");
! 305: const char *content_length=SAPI::get_env(request_pool, "CONTENT_LENGTH");
1.122 parser 306: request_info.content_length=(content_length?atoi(content_length):0);
1.198 ! paf 307: request_info.cookie=SAPI::get_env(request_pool, "HTTP_COOKIE");
1.184 paf 308: request_info.mail_received=mail_received;
309:
1.198 ! paf 310:
1.122 parser 311: // prepare to process request
1.198 ! paf 312: Request request(request_pool,
1.122 parser 313: request_info,
1.184 paf 314: /*#ifdef _DEBUG
1.143 paf 315: String::UL_HTML|String::UL_OPTIMIZE_BIT
1.184 paf 316: #else*/
1.143 paf 317: cgi ? String::UL_HTML|String::UL_OPTIMIZE_BIT : String::UL_AS_IS
1.184 paf 318: /*#endif*/
1.143 paf 319: ,
1.130 paf 320: true /* status_allowed */);
1.122 parser 321:
1.181 paf 322: char config_filespec_buf[MAX_STRING];
1.193 paf 323: if(!config_filespec_cstr) {
324: const char *config_by_env=getenv(PARSER_CONFIG_ENV_NAME);
325: if(!config_by_env)
326: config_by_env=getenv(REDIRECT_PREFIX PARSER_CONFIG_ENV_NAME);
327: if(config_by_env)
328: config_filespec_cstr=config_by_env;
329: else {
330: // beside by binary
331: char beside_binary_path[MAX_STRING];
332: strncpy(beside_binary_path, argv0, MAX_STRING-1); beside_binary_path[MAX_STRING-1]=0; // filespec of my binary
333: if(!(
334: rsplit(beside_binary_path, '/') ||
335: rsplit(beside_binary_path, '\\'))) { // strip filename
336: // no path, just filename
337: // @todo full path, not ./!
338: beside_binary_path[0]='.'; beside_binary_path[1]=0;
339: }
340: snprintf(config_filespec_buf, MAX_STRING,
341: "%s/%s",
342: beside_binary_path, AUTO_FILE_NAME);
343: config_filespec_cstr=config_filespec_buf;
1.197 paf 344: fail_on_config_read_problem=entry_exists(config_filespec_cstr);
1.193 paf 345: }
1.122 parser 346: }
347:
348: // process the request
349: request.core(
1.193 paf 350: config_filespec_cstr, fail_on_config_read_problem,
1.122 parser 351: header_only);
352:
353: //
354: done_socks();
355:
356: #ifdef DEBUG_POOL_MALLOC
357: extern void log_pool_stats(Pool& pool);
1.198 ! paf 358: log_pool_stats(request_pool);
1.122 parser 359: #endif
1.160 paf 360:
361: #ifdef DEBUG_STRING_APPENDS_VS_EXPANDS
1.198 ! paf 362: SAPI::log(global_pool,
1.161 paf 363: "string piece appends=%lu, wcontext_result_size=%lu, string_string_shortcut_economy_closer=%lu, total_alloc_size=%lu",
1.160 paf 364: string_piece_appends,
365: wcontext_result_size,
366: string_string_shortcut_economy,
367: total_alloc_size);
368: #endif
369:
1.122 parser 370: }
371:
1.184 paf 372: static void call_real_parser_handler__do_SEH(
1.122 parser 373: const char *filespec_to_process,
374: const char *request_method, bool header_only) {
1.133 paf 375: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 376: LPEXCEPTION_POINTERS system_exception=0;
377: __try {
378: #endif
379: real_parser_handler(
380: filespec_to_process,
381: request_method, header_only);
382:
1.133 paf 383: #if _MSC_VER && !defined(_DEBUG)
1.122 parser 384: } __except (
385: (system_exception=GetExceptionInformation()),
386: EXCEPTION_EXECUTE_HANDLER) {
387:
388: if(system_exception)
389: if(_EXCEPTION_RECORD *er=system_exception->ExceptionRecord)
1.165 paf 390: throw Exception(0,
391: 0,
392: "Exception 0x%08X at 0x%08X", er->ExceptionCode, er->ExceptionAddress);
1.122 parser 393: else
1.165 paf 394: throw Exception(0, 0, "Exception <no exception record>");
1.122 parser 395: else
1.165 paf 396: throw Exception(0, 0, "Exception <no exception information>");
1.122 parser 397: }
398: #endif
399: }
400:
1.139 paf 401: #if _MSC_VER
1.135 paf 402: int failed_new(size_t size) {
403: SAPI::die("out of memory in 'new', failed to allocated %u bytes", size);
404: return 0; // not reached
1.131 paf 405: }
1.135 paf 406: #endif
1.131 paf 407:
1.135 paf 408: #ifdef HAVE_SET_NEW_HANDLER
1.184 paf 409: static void failed_new() {
1.135 paf 410: SAPI::die("out of memory in 'new'");
1.131 paf 411: }
412: #endif
413:
1.184 paf 414: static void usage(const char *program) {
1.188 paf 415: printf(
1.184 paf 416: "Parser/%s Copyright(c) 2001, 2002 ArtLebedev Group (http://www.artlebedev.com)\n"
417: "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
418: "\n"
419: "Usage: %s [options] file\n"
420: "Options are:\n"
1.185 paf 421: #ifdef WITH_MAILRECEIVE
1.193 paf 422: " -m Parse mail, put received letter to $mail:received\n"
1.184 paf 423: #endif
1.193 paf 424: " -f config_file Use this config file (/path/to/auto.p)\n"
425: " -h Display usage information (this message)\n"
1.184 paf 426: , PARSER_VERSION,
427: program);
428: exit(EINVAL);
429: }
430:
1.195 paf 431: int main(int argc, char *argv[]) {
1.185 paf 432: #ifdef DEBUG_MAILRECEIVE
433: if(FILE *fake_in=fopen(DEBUG_MAILRECEIVE, "rt")) {
1.184 paf 434: dup2(fake_in->_file, 0/*STDIN_FILENO*/);
435: }
436: #endif
437:
1.178 paf 438: #ifdef _DEBUG
439: // _crtBreakAlloc=33112;
440: #endif
1.144 paf 441: // _asm int 3;
1.193 paf 442: argv0=argv[0];
1.45 paf 443:
1.32 paf 444: umask(2);
445:
1.3 paf 446: // were we started as CGI?
1.146 paf 447: cgi=
1.109 parser 448: getenv("SERVER_SOFTWARE") ||
449: getenv("SERVER_NAME") ||
450: getenv("GATEWAY_INTERFACE") ||
451: getenv("REQUEST_METHOD");
1.5 paf 452:
1.184 paf 453: char *raw_filespec_to_process;
454: if(cgi)
455: raw_filespec_to_process=getenv("PATH_TRANSLATED");
456: else {
457: optind = 1;
458: opterr = 0;
459: int c;
1.193 paf 460: while((c = getopt(argc, argv, "hf:"
1.185 paf 461: #ifdef WITH_MAILRECEIVE
1.184 paf 462: "m"
463: #endif
464: )) > 0) {
465: switch (c) {
466: case 'h':
467: usage(argv[0]);
1.193 paf 468: break;
469: case 'f':
470: config_filespec_cstr=optarg;
1.184 paf 471: break;
1.185 paf 472: #ifdef WITH_MAILRECEIVE
1.184 paf 473: case 'm':
474: mail_received=true;
475: break;
476: #endif
477: default:
478: fprintf(stderr, "%s: invalid option '%c'\n", argv[0], optopt);
479: usage(argv[0]);
480: break;
481: }
482: }
483: if (optind != argc - 1) {
484: fprintf(stderr, "%s: file not specified\n", argv[0]);
485: usage(argv[0]);
1.10 paf 486: }
1.184 paf 487:
488: raw_filespec_to_process=argv[optind++];
1.10 paf 489: }
490:
1.100 parser 491: #ifdef WIN32
492: setmode(fileno(stdin), _O_BINARY);
493: setmode(fileno(stdout), _O_BINARY);
494: setmode(fileno(stderr), _O_BINARY);
495: #endif
496:
1.139 paf 497: #if _MSC_VER
1.138 paf 498: _set_new_handler(failed_new);
1.148 paf 499:
500: #ifdef _DEBUG
501: // Get current flag
502: int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
503:
504: // Turn on leak-checking bit
505: tmpFlag |= _CRTDBG_LEAK_CHECK_DF;
506:
507: // Set flag to the new value
508: _CrtSetDbgFlag( tmpFlag );
509: // _CrtSetBreakAlloc(471);
510:
511: #endif
512:
1.138 paf 513: #endif
514:
515: #ifdef HAVE_SET_NEW_HANDLER
516: std::set_new_handler(failed_new);
517: #endif
518:
1.166 paf 519: char filespec_to_process[MAX_STRING];
520: full_file_spec(raw_filespec_to_process, filespec_to_process, sizeof(filespec_to_process));
1.10 paf 521:
1.109 parser 522: const char *request_method=getenv("REQUEST_METHOD");
1.35 paf 523: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.131 paf 524:
1.122 parser 525: try { // global try
526: call_real_parser_handler__do_SEH(
527: filespec_to_process,
528: request_method, header_only);
529: } catch(const Exception& e) { // global problem
1.44 paf 530: // don't allocate anything on pool here:
531: // possible pool' exception not catch-ed now
532: // and there could be out-of-memory exception
1.43 paf 533:
1.144 paf 534: SAPI::die("exception in request exception handler: %s", e.comment());
1.134 paf 535: #ifndef _DEBUG
1.131 paf 536: } catch(...) {
1.134 paf 537: SAPI::die("<unknown exception>");
1.133 paf 538: #endif
1.16 paf 539: }
1.122 parser 540:
1.109 parser 541:
542: #ifndef WIN32
543: //
544: if(!cgi)
1.198 ! paf 545: SAPI::send_body(global_pool, "\n", 1);
1.109 parser 546: #endif
1.156 paf 547: //_asm int 3;
1.134 paf 548: return 0;
1.1 paf 549: }
E-mail: