Annotation of parser3/src/targets/cgi/parser3.C, revision 1.12
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.12 ! paf 6: $Id: parser3.C,v 1.11 2001/03/14 17:15:08 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.8 paf 30: # if MSVC
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.11 paf 92: char *name=rsplit(binary, PATH_DELIMITER_CHAR);
93: printf("Usage: %s <file>\n", name?name:"parser3");
1.10 paf 94: exit(1);
95: }
96: }
97:
98: const char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
99:
1.5 paf 100: char *result; char error[MAX_STRING]; error[0]=0;
101: PTRY { // global try
102: // must be first in PTRY{}PCATCH
103: #ifdef WIN32
1.8 paf 104: # if MSVC
1.5 paf 105: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
106: //TODO: initSocks();
1.8 paf 107: # endif
1.5 paf 108: #endif
1.2 paf 109:
1.10 paf 110: // init global variables
1.9 paf 111: globals_init(pool);
1.10 paf 112:
113: // Request info
1.5 paf 114: // TODO: ifdef WIN32 flip \\ to /
1.10 paf 115: Request::Info request_info;
116: request_info.document_root="Y:/parser3/src/";
117: request_info.path_translated=filespec_to_process;
118: request_info.request_method=getenv("REQUEST_METHOD");
119: request_info.query_string=getenv("QUERY_STRING");
120: request_info.request_uri=getenv("REQUEST_URI");
121: request_info.content_type=getenv("CONTENT_TYPE");
122: const char *content_length=getenv("CONTENT_LENGTH");
123: request_info.content_length=(content_length?atoi(content_length):0);
124:
1.5 paf 125: // prepare to process request
1.6 paf 126: Request request(Pool(),
1.10 paf 127: request_info,
128: cgi ? String::Untaint_lang::HTML_TYPO : String::Untaint_lang::NO
1.5 paf 129: );
130:
131: // some root-controlled location
132: char *sys_auto_path1;
1.3 paf 133: #ifdef WIN32
1.10 paf 134: // c:\windows
1.5 paf 135: sys_auto_path1=(char *)pool.malloc(MAX_STRING);
1.7 paf 136: GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.10 paf 137: strcat(sys_auto_path1, PATH_DELIMITER_STRING);
1.3 paf 138: #else
1.10 paf 139: // ~nobody
1.5 paf 140: sys_auto_path1=getenv("HOME");
141: #endif
142:
143: // beside by binary
144: char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6 paf 145: strncpy(sys_auto_path2, argv[0], MAX_STRING); // filespec of my binary
1.7 paf 146: rsplit(sys_auto_path2, PATH_DELIMITER_CHAR); // strip filename
1.5 paf 147:
148: // process the request
149: result=request.core(
150: sys_auto_path1,
151: sys_auto_path2);
152: // set error, will be reported in case result==0
153: strcpy(error, "exception occured in request exception handler");
154:
155: // must be last in PTRY{}PCATCH
156: #ifdef WIN32
1.8 paf 157: # if MSVC
1.5 paf 158: SetUnhandledExceptionFilter(0);
1.8 paf 159: # endif
1.3 paf 160: #endif
1.7 paf 161: } PCATCH(e) { // global problem @globals fill @Request create @prepare to .core()
1.5 paf 162: result=0;
163: strcpy(error, e.comment());
164: }
165: PEND_CATCH
166:
167: // write out the result
1.3 paf 168: if(cgi) {
1.4 paf 169: if(result) {
170: const char *content_type="text/html";
171: printf(
172: "Content-type: %s\n"
173: "Content-length: %d\n"
174: "\n",
1.3 paf 175: content_type,
176: strlen(result));
1.4 paf 177: stdout_write(result);
178: } else {
179: printf(
180: "Content-type: text/plain\n"
181: "Content-length: %d\n"
182: "\n",
183: strlen(error));
184: stdout_write(error);
185: }
1.3 paf 186: } else
1.4 paf 187: if(result)
188: printf("%s", result);
189: else
190: fputs(error, stderr);
1.2 paf 191:
1.1 paf 192: return 0;
193: }
E-mail: