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