Annotation of parser3/src/targets/cgi/parser3.C, revision 1.27
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.27 ! paf 8: $Id: parser3.C,v 1.26.2.4 2001/03/21 10:58:08 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.27 ! paf 37: Pool pool; ///< global pool
! 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.12 paf 81: int read_post(char *buf, int max_bytes) {
82: int read_size=0;
83: do {
84: int chunk_size=read
85: (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
86: if(chunk_size<0)
87: break;
88: read_size+=chunk_size;
89: } while(read_size<max_bytes);
90:
91: return read_size;
1.10 paf 92: }
93:
1.19 paf 94: void output_header_attribute(const char *key, const char *value) {
1.20 paf 95: if(cgi)
96: printf("%s: %s\n", key, value);
1.19 paf 97: }
98:
99: void output_body(const char *buf, size_t size) {
1.20 paf 100: if(cgi) // header | body delimiter
101: puts("");
102:
1.19 paf 103: stdout_write(buf, size);
1.17 paf 104: }
105:
1.19 paf 106: // main
107:
1.5 paf 108: int main(int argc, char *argv[]) {
1.23 paf 109: // TODO:umask(2);
1.27 ! paf 110: //\#ifdef WIN32
! 111: setmode(fileno(stdin), _O_BINARY);
! 112: setmode(fileno(stdout), _O_BINARY);
! 113: setmode(fileno(stderr), _O_BINARY);
! 114: //\#endif
1.12 paf 115:
1.10 paf 116: // Service funcs
117: service_funcs.read_post=read_post;
1.19 paf 118: service_funcs.output_header_attribute=output_header_attribute;
119: service_funcs.output_body=output_body;
1.10 paf 120:
1.3 paf 121: // were we started as CGI?
1.20 paf 122: cgi=
1.2 paf 123: getenv("SERVER_SOFTWARE") ||
124: getenv("SERVER_NAME") ||
125: getenv("GATEWAY_INTERFACE") ||
126: getenv("REQUEST_METHOD");
1.5 paf 127:
1.10 paf 128: if(!cgi) {
129: if(argc<2) {
130: char *binary=argv[0];
1.13 paf 131: printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10 paf 132: exit(1);
133: }
134: }
135:
1.26 paf 136: char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.27 ! paf 137: //\#ifdef WIN32
1.26 paf 138: fix_slashes(filespec_to_process);
1.27 ! paf 139: //\#endif
1.10 paf 140:
1.5 paf 141: PTRY { // global try
142: // must be first in PTRY{}PCATCH
143: #ifdef WIN32
1.14 paf 144: # if _MSC_VER
1.5 paf 145: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
146: //TODO: initSocks();
1.8 paf 147: # endif
1.5 paf 148: #endif
1.2 paf 149:
1.10 paf 150: // init global variables
1.9 paf 151: globals_init(pool);
1.10 paf 152:
1.13 paf 153: if(!filespec_to_process)
154: PTHROW(0, 0,
155: 0,
156: "no file to process");
157:
1.10 paf 158: // Request info
159: Request::Info request_info;
1.13 paf 160: const char *document_root=getenv("DOCUMENT_ROOT");
161: if(!document_root) {
162: static char fake_document_root[MAX_STRING];
163: strncpy(fake_document_root, filespec_to_process, MAX_STRING);
164: rsplit(fake_document_root, '/'); rsplit(fake_document_root, '\\');// strip filename
165: document_root=fake_document_root;
166: }
167: request_info.document_root=document_root;
1.10 paf 168: request_info.path_translated=filespec_to_process;
1.15 paf 169: request_info.method=getenv("REQUEST_METHOD");
1.10 paf 170: request_info.query_string=getenv("QUERY_STRING");
1.15 paf 171: request_info.uri=getenv("REQUEST_URI");
1.10 paf 172: request_info.content_type=getenv("CONTENT_TYPE");
173: const char *content_length=getenv("CONTENT_LENGTH");
174: request_info.content_length=(content_length?atoi(content_length):0);
1.21 paf 175: request_info.cookie=getenv("HTTP_COOKIE");
1.10 paf 176:
1.5 paf 177: // prepare to process request
1.27 ! paf 178: Pool request_pool;
! 179: Request request(request_pool,
1.10 paf 180: request_info,
1.27 ! paf 181: cgi ? String::UL_HTML_TYPO : String::UL_NO
1.5 paf 182: );
183:
184: // some root-controlled location
185: char *sys_auto_path1;
1.3 paf 186: #ifdef WIN32
1.10 paf 187: // c:\windows
1.5 paf 188: sys_auto_path1=(char *)pool.malloc(MAX_STRING);
1.7 paf 189: GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.10 paf 190: strcat(sys_auto_path1, PATH_DELIMITER_STRING);
1.3 paf 191: #else
1.10 paf 192: // ~nobody
1.5 paf 193: sys_auto_path1=getenv("HOME");
194: #endif
195:
196: // beside by binary
197: char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6 paf 198: strncpy(sys_auto_path2, argv[0], MAX_STRING); // filespec of my binary
1.13 paf 199: rsplit(sys_auto_path2, '/'); rsplit(sys_auto_path2, '\\');// strip filename
200: strcat(sys_auto_path2, PATH_DELIMITER_STRING);
1.5 paf 201:
202: // process the request
1.16 paf 203: request.core(pool.exception(),
1.5 paf 204: sys_auto_path1,
205: sys_auto_path2);
1.19 paf 206: // no actions with request' data past this point
207: // request.exception not not handled here, but all
208: // request' data are associated with it's pool=exception
1.16 paf 209:
1.22 paf 210: // must be last in PTRY{}PCATCH
1.5 paf 211: #ifdef WIN32
1.14 paf 212: # if _MSC_VER
1.5 paf 213: SetUnhandledExceptionFilter(0);
1.8 paf 214: # endif
1.25 paf 215: #endif
1.16 paf 216: // successful finish
217: return 0;
218: } PCATCH(e) { // global problem
1.19 paf 219: const char *body=e.comment();
220: int content_length=strlen(body);
1.5 paf 221:
1.19 paf 222: // header
223: (*service_funcs.output_header_attribute)("content-type", "text/plain");
224: char content_length_cstr[MAX_NUMBER];
225: snprintf(content_length_cstr, MAX_NUMBER, "%d", content_length);
226: (*service_funcs.output_header_attribute)("content-length",
227: content_length_cstr);
228:
229: // body
230: (*service_funcs.output_body)(body, content_length);
1.2 paf 231:
1.16 paf 232: // unsuccessful finish
233: return 1;
234: }
235: PEND_CATCH
1.1 paf 236: }
E-mail: