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