Annotation of parser3/src/targets/cgi/parser3.C, revision 1.11
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.11 ! paf 6: $Id: parser3.C,v 1.10 2001/03/14 16:47:34 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.10 paf 58: size_t read_post(char *&buf, size_t max_bytes) {
59: return 0;
60: }
61:
1.5 paf 62: int main(int argc, char *argv[]) {
1.10 paf 63: // Service funcs
64: service_funcs.read_post=read_post;
65:
1.3 paf 66: // were we started as CGI?
1.2 paf 67: bool cgi=
68: getenv("SERVER_SOFTWARE") ||
69: getenv("SERVER_NAME") ||
70: getenv("GATEWAY_INTERFACE") ||
71: getenv("REQUEST_METHOD");
1.5 paf 72:
1.10 paf 73: if(!cgi) {
74: if(argc<2) {
75: char *binary=argv[0];
1.11 ! paf 76: char *name=rsplit(binary, PATH_DELIMITER_CHAR);
! 77: printf("Usage: %s <file>\n", name?name:"parser3");
1.10 paf 78: exit(1);
79: }
80: }
81:
82: const char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
83:
1.5 paf 84: char *result; char error[MAX_STRING]; error[0]=0;
85: PTRY { // global try
86: // must be first in PTRY{}PCATCH
87: #ifdef WIN32
1.8 paf 88: # if MSVC
1.5 paf 89: SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
90: //TODO: initSocks();
1.8 paf 91: # endif
1.5 paf 92: #endif
1.2 paf 93:
1.10 paf 94: // init global variables
1.9 paf 95: globals_init(pool);
1.10 paf 96:
97: // Request info
1.5 paf 98: // TODO: ifdef WIN32 flip \\ to /
1.10 paf 99: Request::Info request_info;
100: request_info.document_root="Y:/parser3/src/";
101: request_info.path_translated=filespec_to_process;
102: request_info.request_method=getenv("REQUEST_METHOD");
103: request_info.query_string=getenv("QUERY_STRING");
104: request_info.request_uri=getenv("REQUEST_URI");
105: request_info.content_type=getenv("CONTENT_TYPE");
106: const char *content_length=getenv("CONTENT_LENGTH");
107: request_info.content_length=(content_length?atoi(content_length):0);
108:
1.5 paf 109: // prepare to process request
1.6 paf 110: Request request(Pool(),
1.10 paf 111: request_info,
112: cgi ? String::Untaint_lang::HTML_TYPO : String::Untaint_lang::NO
1.5 paf 113: );
114:
115: // some root-controlled location
116: char *sys_auto_path1;
1.3 paf 117: #ifdef WIN32
1.10 paf 118: // c:\windows
1.5 paf 119: sys_auto_path1=(char *)pool.malloc(MAX_STRING);
1.7 paf 120: GetWindowsDirectory(sys_auto_path1, MAX_STRING);
1.10 paf 121: strcat(sys_auto_path1, PATH_DELIMITER_STRING);
1.3 paf 122: #else
1.10 paf 123: // ~nobody
1.5 paf 124: sys_auto_path1=getenv("HOME");
125: #endif
126:
127: // beside by binary
128: char *sys_auto_path2=(char *)pool.malloc(MAX_STRING);
1.6 paf 129: strncpy(sys_auto_path2, argv[0], MAX_STRING); // filespec of my binary
1.7 paf 130: rsplit(sys_auto_path2, PATH_DELIMITER_CHAR); // strip filename
1.5 paf 131:
132: // process the request
133: result=request.core(
134: sys_auto_path1,
135: sys_auto_path2);
136: // set error, will be reported in case result==0
137: strcpy(error, "exception occured in request exception handler");
138:
139: // must be last in PTRY{}PCATCH
140: #ifdef WIN32
1.8 paf 141: # if MSVC
1.5 paf 142: SetUnhandledExceptionFilter(0);
1.8 paf 143: # endif
1.3 paf 144: #endif
1.7 paf 145: } PCATCH(e) { // global problem @globals fill @Request create @prepare to .core()
1.5 paf 146: result=0;
147: strcpy(error, e.comment());
148: }
149: PEND_CATCH
150:
151: // write out the result
1.3 paf 152: if(cgi) {
1.4 paf 153: if(result) {
154: const char *content_type="text/html";
155: printf(
156: "Content-type: %s\n"
157: "Content-length: %d\n"
158: "\n",
1.3 paf 159: content_type,
160: strlen(result));
1.4 paf 161: stdout_write(result);
162: } else {
163: printf(
164: "Content-type: text/plain\n"
165: "Content-length: %d\n"
166: "\n",
167: strlen(error));
168: stdout_write(error);
169: }
1.3 paf 170: } else
1.4 paf 171: if(result)
172: printf("%s", result);
173: else
174: fputs(error, stderr);
1.2 paf 175:
1.1 paf 176: return 0;
177: }
E-mail: