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