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