Annotation of parser3/src/targets/cgi/parser3.C, revision 1.112
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.27 paf 5:
1.43 paf 6: Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1 paf 7: */
1.111 parser 8: static const char *RCSId="$Id: parser3.C,v 1.110.4.3 2001/09/17 14:26:56 parser Exp $";
1.1 paf 9:
1.40 paf 10: #include "pa_config_includes.h"
1.3 paf 11:
12: #ifdef WIN32
13: # include <windows.h>
14: #endif
1.27 paf 15:
1.37 paf 16: #include "pa_sapi.h"
1.76 paf 17: #include "classes.h"
1.24 paf 18: #include "pa_common.h"
1.2 paf 19: #include "pa_request.h"
1.57 paf 20: #include "pa_socks.h"
1.68 paf 21: #include "pa_version.h"
1.69 paf 22:
1.84 parser 23: //#define DEBUG_POOL_MALLOC
24:
1.109 parser 25: // consts
1.84 parser 26:
1.42 paf 27: /// IIS refuses to read bigger chunks
28: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M
29:
1.45 paf 30: const char *argv0;
1.42 paf 31: Pool pool(0); // global pool [dont describe to doxygen: it confuses it with param names]
1.27 paf 32: bool cgi; ///< we were started as CGI?
1.5 paf 33:
1.40 paf 34: #ifdef WIN32
35: /// global system errors into parser exceptions converter
1.43 paf 36: static LONG WINAPI TopLevelExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo) {
1.5 paf 37: char buf[MAX_STRING];
38: if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.36 paf 39: struct _EXCEPTION_RECORD *er=ExceptionInfo->ExceptionRecord;
1.46 paf 40: snprintf(buf, MAX_STRING, "Exception 0x%X at %p",
1.31 paf 41: er->ExceptionCode,
42: er->ExceptionAddress);
1.5 paf 43: } else
44: strcpy(buf, "Exception <unknown>");
45:
46: PTHROW(0, 0,
47: 0,
48: buf);
49:
50: return EXCEPTION_EXECUTE_HANDLER; // never reached
51: }
1.27 paf 52: #endif
53:
1.46 paf 54: // SAPI
1.86 parser 55:
56: // appends to parser3.log located beside my binary if openable, to stderr otherwize
1.61 paf 57: void SAPI::log(Pool& pool, const char *fmt, ...) {
58: bool opened;
59: FILE *f=0;
60:
61: if(argv0) {
62: // beside by binary
63: char file_spec[MAX_STRING];
1.98 parser 64: strncpy(file_spec, argv0, MAX_STRING-1); file_spec[MAX_STRING-1]=0; // filespec of my binary
1.61 paf 65: rsplit(file_spec, '/'); rsplit(file_spec, '\\');// strip filename
66: strcat(file_spec, "/parser3.log");
67: f=fopen(file_spec, "at");
68: }
69: opened=f!=0;
70: if(!opened)
71: f=stderr;
72:
73: // prefix
74: time_t t=time(0);
75: const char *stamp=ctime(&t);
76: fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
77: // message
78: va_list args;
79: va_start(args,fmt);
80: vfprintf(f, fmt, args);
81: va_end(args);
82: // newline
83: fprintf(f, "\n");
84:
85: if(opened)
86: fclose(f);
1.85 parser 87: else
88: fflush(f);
1.61 paf 89: }
90:
1.37 paf 91: const char *SAPI::get_env(Pool& pool, const char *name) {
1.109 parser 92: return getenv(name);
1.28 paf 93: }
94:
1.59 paf 95: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
96: size_t read_size=0;
1.12 paf 97: do {
1.36 paf 98: int chunk_size=read(fileno(stdin),
1.42 paf 99: buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.12 paf 100: if(chunk_size<0)
101: break;
102: read_size+=chunk_size;
103: } while(read_size<max_bytes);
104:
105: return read_size;
1.10 paf 106: }
107:
1.37 paf 108: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.68 paf 109: if(cgi)
1.20 paf 110: printf("%s: %s\n", key, value);
1.19 paf 111: }
112:
1.56 paf 113: /// @todo intelligent cache-control
1.37 paf 114: void SAPI::send_header(Pool& pool) {
1.33 paf 115: if(cgi) {
1.55 paf 116: puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33 paf 117:
118: // header | body delimiter
1.20 paf 119: puts("");
1.33 paf 120: }
1.30 paf 121: }
1.20 paf 122:
1.53 paf 123: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.19 paf 124: stdout_write(buf, size);
1.58 paf 125: }
126:
1.97 parser 127: //
128:
129: char *full_file_spec(char *file_name) {
1.108 parser 130: if(file_name && !strchr(file_name, '/')) {
1.97 parser 131: static char cwd[MAX_STRING]; getcwd(cwd, MAX_STRING);
132: static char buf[MAX_STRING];
133: snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
134: return buf;
135: }
136: return file_name;
137: }
138:
1.40 paf 139: /**
140: main workhorse
1.19 paf 141:
1.56 paf 142: @todo
1.40 paf 143: IIS: remove trailing default-document[index.html] from $request.uri.
144: to do that we need to consult metabase,
145: wich is tested but seems slow.
146: */
1.5 paf 147: int main(int argc, char *argv[]) {
1.109 parser 148: int result;
1.45 paf 149: argv0=argv[0];
150:
1.32 paf 151: umask(2);
152:
1.3 paf 153: // were we started as CGI?
1.20 paf 154: cgi=
1.109 parser 155: getenv("SERVER_SOFTWARE") ||
156: getenv("SERVER_NAME") ||
157: getenv("GATEWAY_INTERFACE") ||
158: getenv("REQUEST_METHOD");
1.5 paf 159:
1.10 paf 160: if(!cgi) {
161: if(argc<2) {
1.69 paf 162: printf(
1.100 parser 163: "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
164: "Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)\n"
165: "\n"
166: "Usage: %s <file>\n",
1.69 paf 167: PARSER_VERSION,
168: argv0?argv0:"parser3");
1.67 paf 169: return 1;
1.10 paf 170: }
171: }
172:
1.100 parser 173: #ifdef WIN32
174: setmode(fileno(stdin), _O_BINARY);
175: setmode(fileno(stdout), _O_BINARY);
176: setmode(fileno(stderr), _O_BINARY);
177: #endif
178:
1.109 parser 179: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36 paf 180: #ifdef WIN32
1.43 paf 181: back_slashes_to_slashes(filespec_to_process);
1.36 paf 182: #endif
1.97 parser 183: filespec_to_process=full_file_spec(filespec_to_process);
1.10 paf 184:
1.109 parser 185: const char *request_method=getenv("REQUEST_METHOD");
1.35 paf 186: bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5 paf 187: PTRY { // global try
188: // must be first in PTRY{}PCATCH
1.40 paf 189: #ifdef WIN32
1.5 paf 190: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
191: #endif
1.2 paf 192:
1.62 paf 193: // init socks
1.57 paf 194: init_socks(pool);
195:
1.73 paf 196: // init global classes
197: init_methoded_array(pool);
1.10 paf 198: // init global variables
1.37 paf 199: pa_globals_init(pool);
1.10 paf 200:
1.13 paf 201: if(!filespec_to_process)
202: PTHROW(0, 0,
203: 0,
1.68 paf 204: "Parser/%s", PARSER_VERSION);
1.13 paf 205:
1.10 paf 206: // Request info
207: Request::Info request_info;
1.39 paf 208: if(cgi) {
1.51 paf 209: if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
1.39 paf 210: request_info.document_root=env_document_root;
1.51 paf 211: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
1.40 paf 212: // IIS
1.39 paf 213: size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40 paf 214: char *buf=(char *)pool.malloc(len+1);
1.98 parser 215: memcpy(buf, filespec_to_process, len); buf[len]=0;
1.39 paf 216: request_info.document_root=buf;
217: } else
218: PTHROW(0, 0,
219: 0,
1.43 paf 220: "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.39 paf 221: } else {
222: static char buf[MAX_STRING];
1.98 parser 223: strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0;
1.70 paf 224: if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
225: request_info.document_root=buf;
226: else
227: request_info.document_root="";
1.13 paf 228: }
1.10 paf 229: request_info.path_translated=filespec_to_process;
1.107 parser 230: request_info.method=request_method ? request_method : "GET";
1.51 paf 231: const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
1.40 paf 232: request_info.query_string=query_string;
1.68 paf 233: if(cgi) {
1.51 paf 234: if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
1.42 paf 235: request_info.uri=env_request_uri;
1.51 paf 236: else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
1.42 paf 237: if(query_string) {
238: char *reconstructed_uri=(char *)malloc(
239: strlen(path_info)+1/*'?'*/+
240: strlen(query_string)+1/*0*/);
241: strcpy(reconstructed_uri, path_info);
242: strcat(reconstructed_uri, "?");
243: strcat(reconstructed_uri, query_string);
244: request_info.uri=reconstructed_uri;
245: } else
246: request_info.uri=path_info;
247: else
248: PTHROW(0, 0,
249: 0,
1.43 paf 250: "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.68 paf 251:
1.72 paf 252: if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
253: size_t script_name_len=strlen(script_name);
254: size_t uri_len=strlen(request_info.uri);
255: if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
256: script_name_len != uri_len) // under IIS they are the same
257: PTHROW(0, 0,
258: 0,
259: "CGI: illegal call");
260: }
1.68 paf 261: } else
1.42 paf 262: request_info.uri=0;
1.40 paf 263:
1.51 paf 264: request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
265: const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
1.10 paf 266: request_info.content_length=(content_length?atoi(content_length):0);
1.51 paf 267: request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
268: request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
1.10 paf 269:
1.5 paf 270: // prepare to process request
1.38 paf 271: Request request(pool,
1.10 paf 272: request_info,
1.91 parser 273: cgi ? String::UL_USER_HTML : String::UL_AS_IS
1.5 paf 274: );
275:
276: // some root-controlled location
1.102 parser 277: #ifdef SYSCONFDIR
1.111 parser 278: const char *root_config_filespec=SYSCONFDIR "/" CONFIG_FILE_NAME;
1.102 parser 279: #else
280: # ifdef WIN32
1.10 paf 281: // c:\windows
1.111 parser 282: char root_config_path[MAX_STRING];
283: GetWindowsDirectory(root_config_path, MAX_STRING);
284:
285: char root_config_filespec[MAX_STRING];
286: snprintf(root_config_filespec, MAX_STRING,
287: "%s/%s",
288: root_config_path, CONFIG_FILE_NAME);
1.101 parser 289: # else
1.102 parser 290: #error must be compiled either configure/make or MSVC++
1.101 parser 291: # endif
1.5 paf 292: #endif
293:
294: // beside by binary
1.111 parser 295: // @todo full path, not ./!
296: static char site_config_path[MAX_STRING];
297: strncpy(site_config_path, argv0, MAX_STRING-1); site_config_path[MAX_STRING-1]=0; // filespec of my binary
1.88 parser 298: if(!(
1.111 parser 299: rsplit(site_config_path, '/') ||
300: rsplit(site_config_path, '\\'))) { // strip filename
1.88 parser 301: // no path, just filename
1.111 parser 302: site_config_path[0]='.'; site_config_path[1]=0;
1.88 parser 303: }
1.111 parser 304:
305: char site_config_filespec[MAX_STRING];
306: snprintf(site_config_filespec, MAX_STRING,
307: "%s/%s",
308: site_config_path, CONFIG_FILE_NAME);
1.5 paf 309:
310: // process the request
1.35 paf 311: request.core(
1.111 parser 312: root_config_filespec, false,
313: site_config_filespec, false,
1.35 paf 314: header_only);
1.57 paf 315:
316: //
317: done_socks();
1.84 parser 318:
319: #ifdef DEBUG_POOL_MALLOC
1.78 parser 320: extern void log_pool_stats(Pool& pool);
321: log_pool_stats(pool);
1.84 parser 322: #endif
1.16 paf 323:
1.22 paf 324: // must be last in PTRY{}PCATCH
1.40 paf 325: #ifdef WIN32
1.5 paf 326: SetUnhandledExceptionFilter(0);
1.25 paf 327: #endif
1.100 parser 328:
1.16 paf 329: // successful finish
1.109 parser 330: result=0;
1.16 paf 331: } PCATCH(e) { // global problem
1.43 paf 332: // must be first in PCATCH{}
333: #ifdef WIN32
334: SetUnhandledExceptionFilter(0);
335: #endif
1.44 paf 336: // don't allocate anything on pool here:
337: // possible pool' exception not catch-ed now
338: // and there could be out-of-memory exception
1.43 paf 339:
1.19 paf 340: const char *body=e.comment();
1.44 paf 341: // log it
342: SAPI::log(pool, "exception in request exception handler: %s", body);
343:
344: //
1.19 paf 345: int content_length=strlen(body);
1.5 paf 346:
1.35 paf 347: // prepare header
1.37 paf 348: SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19 paf 349: char content_length_cstr[MAX_NUMBER];
1.60 paf 350: snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.37 paf 351: SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35 paf 352:
353: // send header
1.37 paf 354: SAPI::send_header(pool);
1.19 paf 355:
356: // body
1.35 paf 357: if(!header_only)
1.37 paf 358: SAPI::send_body(pool, body, content_length);
1.94 parser 359:
1.16 paf 360: // unsuccessful finish
1.109 parser 361: result=1;
1.16 paf 362: }
363: PEND_CATCH
1.109 parser 364:
365: #ifndef WIN32
366: //
367: if(!cgi)
368: SAPI::send_body(pool, "\n", 1);
369: #endif
370: return result;
1.1 paf 371: }
E-mail: