Annotation of parser3/src/targets/cgi/parser3.C, revision 1.29
1.27 paf 1: /** @file
2: Parser: scripting and CGI main.
3:
1.1 paf 4: Copyright (c) 2001 ArtLebedev Group (http://www.artlebedev.com)
1.27 paf 5:
1.1 paf 6: Author: Alexander Petrosyan <paf@design.ru> (http://design.ru/paf)
7:
1.29 ! paf 8: $Id: parser3.C,v 1.28 2001/03/21 15:53:28 paf Exp $
1.1 paf 9: */
10:
1.5 paf 11: #ifdef HAVE_CONFIG_H
12: # include "pa_config.h"
13: #endif
14:
1.3 paf 15:
16: #ifdef WIN32
17: # include <windows.h>
18: # include <io.h>
1.27 paf 19: #else
20: # include <unistd.h>
1.3 paf 21: #endif
1.27 paf 22:
23: //\ifwin32
24: #include <io.h>
25: //#include <fcntl.h>
26: //\endifwin32
27:
1.3 paf 28: #include <stdlib.h>
1.4 paf 29: #include <stdio.h>
30: #include <string.h>
31: #include <fcntl.h>
1.3 paf 32:
1.24 paf 33: #include "pa_common.h"
1.5 paf 34: #include "pa_globals.h"
1.2 paf 35: #include "pa_request.h"
1.1 paf 36:
1.29 ! paf 37: Pool pool; // global pool
1.27 paf 38: bool cgi; ///< we were started as CGI?
1.5 paf 39:
1.3 paf 40: #ifdef WIN32
1.14 paf 41: # if _MSC_VER
1.5 paf 42: // intercept global system errors
43: LONG WINAPI TopLevelExceptionFilter (
44: struct _EXCEPTION_POINTERS *ExceptionInfo
45: ) {
46: char buf[MAX_STRING];
47: if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
48: struct _EXCEPTION_RECORD *r=ExceptionInfo->ExceptionRecord;
49:
50: int printed=0;
51: printed+=snprintf(buf+printed, MAX_STRING-printed, "Exception 0x%X at 0x%p",
52: r->ExceptionCode,
53: r->ExceptionAddress);
54: for(unsigned int i=0; i<r->NumberParameters; i++)
55: printed+=snprintf(buf+printed, MAX_STRING-printed, ", 0x%X",
56: r->ExceptionInformation[i]);
57: } else
58: strcpy(buf, "Exception <unknown>");
59:
60: PTHROW(0, 0,
61: 0,
62: buf);
63:
64: return EXCEPTION_EXECUTE_HANDLER; // never reached
65: }
1.8 paf 66: # endif
1.26 paf 67:
1.27 paf 68: #endif
69:
70: //\if
1.26 paf 71: void fix_slashes(char *s) {
1.27 paf 72: if(s)
73: for(; *s; s++)
74: if(*s=='\\')
75: *s='/';
1.26 paf 76: }
1.27 paf 77: //\endif
1.3 paf 78:
1.19 paf 79: // service funcs
80:
1.28 paf 81: const char *get_env(Pool& pool, const char *name) {
82: return getenv(name);
83: }
84:
1.12 paf 85: int read_post(char *buf, int max_bytes) {
86: int read_size=0;
87: do {
88: int chunk_size=read
89: (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
90: if(chunk_size<0)
91: break;
92: read_size+=chunk_size;
93: } while(read_size<max_bytes);
94:
95: return read_size;
1.10 paf 96: }
97:
1.19 paf 98: void output_header_attribute(const char *key, const char *value) {
1.20 paf 99: if(cgi)
100: printf("%s: %s\n", key, value);
1.19 paf 101: }
102:
103: void output_body(const char *buf, size_t size) {
1.20 paf 104: if(cgi) // header | body delimiter
105: puts("");
106:
1.19 paf 107: stdout_write(buf, size);
1.17 paf 108: }
109:
1.19 paf 110: // main
111:
1.5 paf 112: int main(int argc, char *argv[]) {
1.23 paf 113: // TODO:umask(2);
1.27 paf 114: //\#ifdef WIN32
115: setmode(fileno(stdin), _O_BINARY);
116: setmode(fileno(stdout), _O_BINARY);
117: setmode(fileno(stderr), _O_BINARY);
118: //\#endif
1.12 paf 119:
1.10 paf 120: // Service funcs
1.28 paf 121: service_funcs.get_env=get_env;
1.10 paf 122: service_funcs.read_post=read_post;
1.19 paf 123: service_funcs.output_header_attribute=output_header_attribute;
124: service_funcs.output_body=output_body;
1.10 paf 125:
1.3 paf 126: // were we started as CGI?
1.20 paf 127: cgi=
1.2 paf 128: getenv("SERVER_SOFTWARE") ||
129: getenv("SERVER_NAME") ||
130: getenv("GATEWAY_INTERFACE") ||
131: getenv("REQUEST_METHOD");
1.5 paf 132:
1.10 paf 133: if(!cgi) {
134: if(argc<2) {
135: char *binary=argv[0];
1.13 paf 136: printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10 paf 137: exit(1);
138: }
139: }
140:
1.26 paf 141: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.27 paf 142: //\#ifdef WIN32
1.26 paf 143: fix_slashes(filespec_to_process);
1.27 paf 144: //\#endif
1.10 paf 145:
1.5 paf 146: PTRY { // global try
147: // must be first in PTRY{}PCATCH
148: #ifdef WIN32
1.14 paf 149: # if _MSC_VER
1.5 paf 150: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
151: //TODO: initSocks();
1.8 paf 152: # endif
1.5 paf 153: #endif
1.2 paf 154:
1.10 paf 155: // init global variables
1.9 paf 156: globals_init(pool);
1.10 paf 157:
1.13 paf 158: if(!filespec_to_process)
159: PTHROW(0, 0,
160: 0,
161: "no file to process");
162:
1.10 paf 163: // Request info
164: Request::Info request_info;
1.13 paf 165: const char *document_root=getenv("DOCUMENT_ROOT");
166: if(!document_root) {
167: static char fake_document_root[MAX_STRING];
168: strncpy(fake_document_root, filespec_to_process, MAX_STRING);
169: rsplit(fake_document_root, '/'); rsplit(fake_document_root, '\\');// strip filename
170: document_root=fake_document_root;
171: }
172: request_info.document_root=document_root;
1.10 paf 173: request_info.path_translated=filespec_to_process;
1.15 paf 174: request_info.method=getenv("REQUEST_METHOD");
1.10 paf 175: request_info.query_string=getenv("QUERY_STRING");
1.15 paf 176: request_info.uri=getenv("REQUEST_URI");
1.10 paf 177: request_info.content_type=getenv("CONTENT_TYPE");
178: const char *content_length=getenv("CONTENT_LENGTH");
179: request_info.content_length=(content_length?atoi(content_length):0);
1.21 paf 180: request_info.cookie=getenv("HTTP_COOKIE");
1.10 paf 181:
1.5 paf 182: // prepare to process request
1.27 paf 183: Pool request_pool;
184: Request request(request_pool,
1.10 paf 185: request_info,
1.27 paf 186: cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5 paf 187: );
188:
189: // some root-controlled location
190: char *sys_auto_path1;
1.3 paf 191: #ifdef WIN32
1.10 paf 192: // c:\windows
1.5 paf 193: sys_auto_path1=(char *)pool.malloc(MAX_STRING);
1.7 paf 194: GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.10 paf 195: strcat(sys_auto_path1, PATH_DELIMITER_STRING);
1.3 paf 196: #else
1.10 paf 197: // ~nobody
1.5 paf 198: sys_auto_path1=getenv("HOME");
199: #endif
200:
201: // beside by binary
202: char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6 paf 203: strncpy(sys_auto_path2, argv[0], MAX_STRING); // filespec of my binary
1.13 paf 204: rsplit(sys_auto_path2, '/'); rsplit(sys_auto_path2, '\\');// strip filename
205: strcat(sys_auto_path2, PATH_DELIMITER_STRING);
1.5 paf 206:
207: // process the request
1.16 paf 208: request.core(pool.exception(),
1.5 paf 209: sys_auto_path1,
210: sys_auto_path2);
1.19 paf 211: // no actions with request' data past this point
212: // request.exception not not handled here, but all
213: // request' data are associated with it's pool=exception
1.16 paf 214:
1.22 paf 215: // must be last in PTRY{}PCATCH
1.5 paf 216: #ifdef WIN32
1.14 paf 217: # if _MSC_VER
1.5 paf 218: SetUnhandledExceptionFilter(0);
1.8 paf 219: # endif
1.25 paf 220: #endif
1.16 paf 221: // successful finish
222: return 0;
223: } PCATCH(e) { // global problem
1.19 paf 224: const char *body=e.comment();
225: int content_length=strlen(body);
1.5 paf 226:
1.19 paf 227: // header
228: (*service_funcs.output_header_attribute)("content-type", "text/plain");
229: char content_length_cstr[MAX_NUMBER];
230: snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
231: (*service_funcs.output_header_attribute)("content-length",
232: content_length_cstr);
233:
234: // body
235: (*service_funcs.output_body)(body, content_length);
1.2 paf 236:
1.16 paf 237: // unsuccessful finish
238: return 1;
239: }
240: PEND_CATCH
1.1 paf 241: }
E-mail: