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