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