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