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