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