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