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