Annotation of parser3/src/targets/cgi/parser3.C, revision 1.14
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.14 ! paf 6: $Id: parser3.C,v 1.13 2001/03/15 09:37:56 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>
17: #endif
18: #include <stdlib.h>
1.4 paf 19: #include <stdio.h>
20: #include <string.h>
21: #include <fcntl.h>
1.3 paf 22:
1.5 paf 23: #include "pa_globals.h"
1.2 paf 24: #include "pa_request.h"
25: #include "pa_common.h"
1.1 paf 26:
1.5 paf 27: Pool pool; // global pool
28:
1.3 paf 29: #ifdef WIN32
1.14 ! paf 30: # if _MSC_VER
1.5 paf 31: // intercept global system errors
32: LONG WINAPI TopLevelExceptionFilter (
33: struct _EXCEPTION_POINTERS *ExceptionInfo
34: ) {
35: char buf[MAX_STRING];
36: if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
37: struct _EXCEPTION_RECORD *r=ExceptionInfo->ExceptionRecord;
38:
39: int printed=0;
40: printed+=snprintf(buf+printed, MAX_STRING-printed, "Exception 0x%X at 0x%p",
41: r->ExceptionCode,
42: r->ExceptionAddress);
43: for(unsigned int i=0; i<r->NumberParameters; i++)
44: printed+=snprintf(buf+printed, MAX_STRING-printed, ", 0x%X",
45: r->ExceptionInformation[i]);
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.8 paf 55: # endif
1.3 paf 56: #endif
57:
1.12 paf 58: int read_post(char *buf, int max_bytes) {
59: int read_size=0;
60: do {
61: int chunk_size=read
62: (fileno(stdin), buf+read_size, min(0x400*0x400, max_bytes-read_size));
63: if(chunk_size<0)
64: break;
65: read_size+=chunk_size;
66: } while(read_size<max_bytes);
67:
68: return read_size;
1.10 paf 69: }
70:
1.5 paf 71: int main(int argc, char *argv[]) {
1.12 paf 72: //TODO: umask(2);
73: #ifdef WIN32
74: _setmode(fileno(stdin), _O_BINARY);
75: _setmode(fileno(stdout), _O_BINARY);
76: _setmode(fileno(stderr), _O_BINARY);
77: #endif
78:
1.10 paf 79: // Service funcs
80: service_funcs.read_post=read_post;
81:
1.3 paf 82: // were we started as CGI?
1.2 paf 83: bool cgi=
84: getenv("SERVER_SOFTWARE") ||
85: getenv("SERVER_NAME") ||
86: getenv("GATEWAY_INTERFACE") ||
87: getenv("REQUEST_METHOD");
1.5 paf 88:
1.10 paf 89: if(!cgi) {
90: if(argc<2) {
91: char *binary=argv[0];
1.13 paf 92: printf("Usage: %s <file>\n", binary?binary:"parser3");
1.10 paf 93: exit(1);
94: }
95: }
96:
97: const char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
98:
1.5 paf 99: char *result; char error[MAX_STRING]; error[0]=0;
100: PTRY { // global try
101: // must be first in PTRY{}PCATCH
102: #ifdef WIN32
1.14 ! paf 103: # if _MSC_VER
1.5 paf 104: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
105: //TODO: initSocks();
1.8 paf 106: # endif
1.5 paf 107: #endif
1.2 paf 108:
1.10 paf 109: // init global variables
1.9 paf 110: globals_init(pool);
1.10 paf 111:
1.13 paf 112: if(!filespec_to_process)
113: PTHROW(0, 0,
114: 0,
115: "no file to process");
116:
1.10 paf 117: // Request info
1.5 paf 118: // TODO: ifdef WIN32 flip \\ to /
1.10 paf 119: Request::Info request_info;
1.13 paf 120: const char *document_root=getenv("DOCUMENT_ROOT");
121: if(!document_root) {
122: static char fake_document_root[MAX_STRING];
123: strncpy(fake_document_root, filespec_to_process, MAX_STRING);
124: rsplit(fake_document_root, '/'); rsplit(fake_document_root, '\\');// strip filename
125: document_root=fake_document_root;
126: }
127: request_info.document_root=document_root;
1.10 paf 128: request_info.path_translated=filespec_to_process;
129: request_info.request_method=getenv("REQUEST_METHOD");
130: request_info.query_string=getenv("QUERY_STRING");
131: request_info.request_uri=getenv("REQUEST_URI");
132: request_info.content_type=getenv("CONTENT_TYPE");
133: const char *content_length=getenv("CONTENT_LENGTH");
134: request_info.content_length=(content_length?atoi(content_length):0);
135:
1.5 paf 136: // prepare to process request
1.6 paf 137: Request request(Pool(),
1.10 paf 138: request_info,
139: cgi ? String::Untaint_lang::HTML_TYPO : String::Untaint_lang::NO
1.5 paf 140: );
141:
142: // some root-controlled location
143: char *sys_auto_path1;
1.3 paf 144: #ifdef WIN32
1.10 paf 145: // c:\windows
1.5 paf 146: sys_auto_path1=(char *)pool.malloc(MAX_STRING);
1.7 paf 147: GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.10 paf 148: strcat(sys_auto_path1, PATH_DELIMITER_STRING);
1.3 paf 149: #else
1.10 paf 150: // ~nobody
1.5 paf 151: sys_auto_path1=getenv("HOME");
152: #endif
153:
154: // beside by binary
155: char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6 paf 156: strncpy(sys_auto_path2, argv[0], MAX_STRING); // filespec of my binary
1.13 paf 157: rsplit(sys_auto_path2, '/'); rsplit(sys_auto_path2, '\\');// strip filename
158: strcat(sys_auto_path2, PATH_DELIMITER_STRING);
1.5 paf 159:
160: // process the request
161: result=request.core(
162: sys_auto_path1,
163: sys_auto_path2);
164: // set error, will be reported in case result==0
165: strcpy(error, "exception occured in request exception handler");
166:
167: // must be last in PTRY{}PCATCH
168: #ifdef WIN32
1.14 ! paf 169: # if _MSC_VER
1.5 paf 170: SetUnhandledExceptionFilter(0);
1.8 paf 171: # endif
1.3 paf 172: #endif
1.7 paf 173: } PCATCH(e) { // global problem @globals fill @Request create @prepare to .core()
1.5 paf 174: result=0;
175: strcpy(error, e.comment());
176: }
177: PEND_CATCH
178:
179: // write out the result
1.3 paf 180: if(cgi) {
1.4 paf 181: if(result) {
182: const char *content_type="text/html";
183: printf(
184: "Content-type: %s\n"
185: "Content-length: %d\n"
186: "\n",
1.3 paf 187: content_type,
188: strlen(result));
1.4 paf 189: stdout_write(result);
190: } else {
191: printf(
192: "Content-type: text/plain\n"
193: "Content-length: %d\n"
194: "\n",
195: strlen(error));
196: stdout_write(error);
197: }
1.3 paf 198: } else
1.4 paf 199: if(result)
200: printf("%s", result);
201: else
202: fputs(error, stderr);
1.2 paf 203:
1.1 paf 204: return 0;
205: }
E-mail: