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