Annotation of parser3/src/targets/cgi/parser3.C, revision 1.95

1.27      paf         1: /** @file
                      2:        Parser: scripting and CGI main.
                      3: 
1.43      paf         4:        Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)
1.27      paf         5: 
1.43      paf         6:        Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)
1.1       paf         7: */
1.95    ! parser      8: static const char *RCSId="$Id: parser3.C,v 1.94 2001/08/24 09:25:17 parser Exp $"; 
1.1       paf         9: 
1.40      paf        10: #include "pa_config_includes.h"
1.3       paf        11: 
                     12: #ifdef WIN32
                     13: #      include <windows.h>
                     14: #endif
1.27      paf        15: 
1.37      paf        16: #include "pa_sapi.h"
1.76      paf        17: #include "classes.h"
1.24      paf        18: #include "pa_common.h"
1.2       paf        19: #include "pa_request.h"
1.57      paf        20: #include "pa_socks.h"
1.68      paf        21: #include "pa_version.h"
1.69      paf        22: 
1.84      parser     23: //#define DEBUG_POOL_MALLOC
                     24: 
                     25: 
1.42      paf        26: /// IIS refuses to read bigger chunks
                     27: const size_t READ_POST_CHUNK_SIZE=0x400*0x400; // 1M 
                     28: 
1.45      paf        29: const char *argv0;
1.42      paf        30: Pool pool(0); // global pool [dont describe to doxygen: it confuses it with param names]
1.27      paf        31: bool cgi; ///< we were started as CGI?
1.5       paf        32: 
1.40      paf        33: #ifdef WIN32
                     34: /// global system errors into parser exceptions converter
1.43      paf        35: static LONG WINAPI TopLevelExceptionFilter(struct _EXCEPTION_POINTERS *ExceptionInfo) {
1.5       paf        36:        char buf[MAX_STRING];
                     37:        if(ExceptionInfo && ExceptionInfo->ExceptionRecord) {
1.36      paf        38:                struct _EXCEPTION_RECORD *er=ExceptionInfo->ExceptionRecord;
1.46      paf        39:                snprintf(buf, MAX_STRING, "Exception 0x%X at %p", 
1.31      paf        40:                        er->ExceptionCode, 
                     41:                        er->ExceptionAddress);
1.5       paf        42:        } else 
                     43:                strcpy(buf, "Exception <unknown>");
                     44:        
                     45:        PTHROW(0, 0,
                     46:                0,
                     47:                buf);
                     48: 
                     49:        return EXCEPTION_EXECUTE_HANDLER; // never reached
                     50: }
1.27      paf        51: #endif
                     52: 
1.46      paf        53: // SAPI
1.86      parser     54: 
                     55: // appends to parser3.log located beside my binary if openable, to stderr otherwize
1.61      paf        56: void SAPI::log(Pool& pool, const char *fmt, ...) {
                     57:        bool opened;
                     58:        FILE *f=0;
                     59: 
                     60:        if(argv0) {
                     61:                // beside by binary
                     62:                char file_spec[MAX_STRING];
                     63:                strncpy(file_spec, argv0, MAX_STRING);  // filespec of my binary
                     64:                rsplit(file_spec, '/');  rsplit(file_spec, '\\');// strip filename
                     65:                strcat(file_spec, "/parser3.log");
                     66:                f=fopen(file_spec, "at");
                     67:        }
                     68:        opened=f!=0;
                     69:        if(!opened)
                     70:                f=stderr;
                     71: 
                     72:        // prefix
                     73:        time_t t=time(0);
                     74:        const char *stamp=ctime(&t);
                     75:        fprintf(f, "[%.*s] ", strlen(stamp)-1, stamp);
                     76:        // message
                     77:     va_list args;
                     78:        va_start(args,fmt);
                     79:        vfprintf(f, fmt, args);
                     80:        va_end(args);
                     81:        // newline
                     82:        fprintf(f, "\n");
                     83: 
                     84:        if(opened)
                     85:                fclose(f);
1.85      parser     86:        else
                     87:                fflush(f);
1.61      paf        88: }
                     89: 
1.37      paf        90: const char *SAPI::get_env(Pool& pool, const char *name) {
1.28      paf        91:        return getenv(name);
                     92: }
                     93: 
1.59      paf        94: size_t SAPI::read_post(Pool& pool, char *buf, size_t max_bytes) {
                     95:        size_t read_size=0;
1.12      paf        96:        do {
1.36      paf        97:                int chunk_size=read(fileno(stdin), 
1.42      paf        98:                        buf+read_size, min(READ_POST_CHUNK_SIZE, max_bytes-read_size));
1.12      paf        99:                if(chunk_size<0)
                    100:                        break;
                    101:                read_size+=chunk_size;
                    102:        } while(read_size<max_bytes);
                    103: 
                    104:        return read_size;
1.10      paf       105: }
                    106: 
1.37      paf       107: void SAPI::add_header_attribute(Pool& pool, const char *key, const char *value) {
1.68      paf       108:        if(cgi)
1.20      paf       109:                printf("%s: %s\n", key, value);
1.19      paf       110: }
                    111: 
1.56      paf       112: /// @todo intelligent cache-control
1.37      paf       113: void SAPI::send_header(Pool& pool) {
1.33      paf       114:        if(cgi) {
1.55      paf       115:                puts("expires: Fri, 23 Mar 2001 09:32:23 GMT");
1.33      paf       116: 
                    117:                // header | body  delimiter
1.20      paf       118:                puts("");
1.33      paf       119:        }
1.30      paf       120: }
1.20      paf       121: 
1.53      paf       122: void SAPI::send_body(Pool& pool, const void *buf, size_t size) {
1.19      paf       123:        stdout_write(buf, size);
1.58      paf       124: }
                    125: 
1.40      paf       126: /**
                    127:        main workhorse
1.19      paf       128: 
1.56      paf       129:        @todo 
1.40      paf       130:                IIS: remove trailing default-document[index.html] from $request.uri.
                    131:                to do that we need to consult metabase,
                    132:                wich is tested but seems slow.
                    133: */
1.5       paf       134: int main(int argc, char *argv[]) {
1.45      paf       135:        argv0=argv[0];
                    136: 
1.32      paf       137:        umask(2);
                    138: 
                    139: #ifdef WIN32
1.27      paf       140:        setmode(fileno(stdin), _O_BINARY);
                    141:        setmode(fileno(stdout), _O_BINARY);
                    142:        setmode(fileno(stderr), _O_BINARY);
1.32      paf       143: #endif
1.12      paf       144: 
1.3       paf       145:        // were we started as CGI?
1.20      paf       146:        cgi=
1.2       paf       147:                getenv("SERVER_SOFTWARE") || 
                    148:                getenv("SERVER_NAME") || 
                    149:                getenv("GATEWAY_INTERFACE") || 
                    150:                getenv("REQUEST_METHOD");
1.5       paf       151:        
1.10      paf       152:        if(!cgi) {
                    153:                if(argc<2) {
1.69      paf       154:                        printf(
                    155:                                "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
                    156:                                "Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)\n"
                    157:                                "\n"
                    158:                                "Usage: %s <file>\n",
                    159:                                PARSER_VERSION, 
                    160:                                argv0?argv0:"parser3");
1.67      paf       161:                        return 1;
1.10      paf       162:                }
                    163:        }
                    164: 
1.26      paf       165:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36      paf       166: #ifdef WIN32
1.43      paf       167:        back_slashes_to_slashes(filespec_to_process);
1.36      paf       168: #endif
1.10      paf       169: 
1.35      paf       170:        const char *request_method=getenv("REQUEST_METHOD");
                    171:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5       paf       172:        PTRY { // global try
                    173:                // must be first in PTRY{}PCATCH
1.40      paf       174: #ifdef WIN32
1.5       paf       175:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    176: #endif
1.2       paf       177: 
1.62      paf       178:                // init socks
1.57      paf       179:                init_socks(pool);
                    180: 
1.73      paf       181:                // init global classes
                    182:                init_methoded_array(pool);
1.10      paf       183:                // init global variables
1.37      paf       184:                pa_globals_init(pool);
1.10      paf       185: 
1.13      paf       186:                if(!filespec_to_process)
                    187:                        PTHROW(0, 0,
                    188:                                0,
1.68      paf       189:                                "Parser/%s", PARSER_VERSION);
1.13      paf       190: 
1.10      paf       191:                // Request info
                    192:                Request::Info request_info;
1.39      paf       193:                if(cgi) {
1.51      paf       194:                        if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
1.39      paf       195:                                request_info.document_root=env_document_root;
1.51      paf       196:                        else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
1.40      paf       197:                                // IIS
1.39      paf       198:                                size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40      paf       199:                                char *buf=(char *)pool.malloc(len+1);
                    200:                                strncpy(buf, filespec_to_process, len);
1.39      paf       201:                                buf[len]=0;
                    202:                                request_info.document_root=buf;
                    203:                        } else
                    204:                                PTHROW(0, 0,
                    205:                                        0,
1.43      paf       206:                                        "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.39      paf       207:                } else {
                    208:                        static char buf[MAX_STRING];
                    209:                        strncpy(buf, filespec_to_process, MAX_STRING);
1.70      paf       210:                        if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
                    211:                                request_info.document_root=buf;
                    212:                        else
                    213:                                request_info.document_root="";
1.13      paf       214:                }
1.10      paf       215:                request_info.path_translated=filespec_to_process;
1.35      paf       216:                request_info.method=request_method;
1.51      paf       217:                const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
1.40      paf       218:                request_info.query_string=query_string;
1.68      paf       219:                if(cgi) {
1.51      paf       220:                        if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
1.42      paf       221:                                request_info.uri=env_request_uri;
1.51      paf       222:                        else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
1.42      paf       223:                                if(query_string) {
                    224:                                        char *reconstructed_uri=(char *)malloc(
                    225:                                                strlen(path_info)+1/*'?'*/+
                    226:                                                strlen(query_string)+1/*0*/);
                    227:                                        strcpy(reconstructed_uri, path_info);
                    228:                                        strcat(reconstructed_uri, "?");
                    229:                                        strcat(reconstructed_uri, query_string);
                    230:                                        request_info.uri=reconstructed_uri;
                    231:                                } else
                    232:                                        request_info.uri=path_info;
                    233:                        else
                    234:                                PTHROW(0, 0,
                    235:                                        0,
1.43      paf       236:                                        "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.68      paf       237: 
1.72      paf       238:                        if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
                    239:                                size_t script_name_len=strlen(script_name);
                    240:                                size_t uri_len=strlen(request_info.uri);
                    241:                                if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
                    242:                                        script_name_len != uri_len) // under IIS they are the same
                    243:                                        PTHROW(0, 0,
                    244:                                                0,
                    245:                                                "CGI: illegal call");
                    246:                        }
1.68      paf       247:                } else
1.42      paf       248:                        request_info.uri=0;
1.40      paf       249: 
1.51      paf       250:                request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
                    251:                const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
1.10      paf       252:                request_info.content_length=(content_length?atoi(content_length):0);
1.51      paf       253:                request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    254:                request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
1.10      paf       255: 
1.5       paf       256:                // prepare to process request
1.38      paf       257:                Request request(pool,
1.10      paf       258:                        request_info,
1.91      parser    259:                        cgi ? String::UL_USER_HTML : String::UL_AS_IS
1.5       paf       260:                        );
                    261:                
                    262:                // some root-controlled location
1.3       paf       263: #ifdef WIN32
1.10      paf       264:                // c:\windows
1.36      paf       265:                static char root_auto_path[MAX_STRING];
1.30      paf       266:                GetWindowsDirectory(root_auto_path, MAX_STRING);
1.3       paf       267: #else
1.40      paf       268:                // ~nobody  todo: figure out a better place
1.64      paf       269:                const char *root_auto_path=SAPI::get_env(pool, "HOME");
1.5       paf       270: #endif
                    271:                
                    272:                // beside by binary
1.36      paf       273:                static char site_auto_path[MAX_STRING];
1.88      parser    274:                strncpy(site_auto_path, argv0, MAX_STRING);  // filespec of my binary
                    275:                if(!(
                    276:                        rsplit(site_auto_path, '/') || 
1.89      parser    277:                        rsplit(site_auto_path, '\\'))) { // strip filename
1.88      parser    278:                        // no path, just filename
                    279:                        site_auto_path[0]='.'; site_auto_path[1]=0;
                    280:                }
1.5       paf       281:                
                    282:                // process the request
1.35      paf       283:                request.core(
1.30      paf       284:                        root_auto_path, false,
1.31      paf       285:                        site_auto_path, false,
1.35      paf       286:                        header_only);
1.57      paf       287: 
                    288:                //
                    289:                done_socks();
1.84      parser    290: 
                    291: #ifdef DEBUG_POOL_MALLOC
1.78      parser    292:                extern void log_pool_stats(Pool& pool);
                    293:                log_pool_stats(pool);
1.84      parser    294: #endif
1.16      paf       295: 
1.22      paf       296:                // must be last in PTRY{}PCATCH
1.40      paf       297: #ifdef WIN32
1.5       paf       298:                SetUnhandledExceptionFilter(0);
1.25      paf       299: #endif
1.16      paf       300:                // successful finish
                    301:                return 0;
                    302:        } PCATCH(e) { // global problem 
1.43      paf       303:                // must be first in PCATCH{}
                    304: #ifdef WIN32
                    305:                SetUnhandledExceptionFilter(0);
                    306: #endif
1.44      paf       307:                // don't allocate anything on pool here:
                    308:                //   possible pool' exception not catch-ed now
                    309:                //   and there could be out-of-memory exception
1.43      paf       310: 
1.19      paf       311:                const char *body=e.comment();
1.44      paf       312:                // log it
                    313:                SAPI::log(pool, "exception in request exception handler: %s", body);
                    314: 
                    315:                //
1.19      paf       316:                int content_length=strlen(body);
1.5       paf       317: 
1.35      paf       318:                // prepare header
1.37      paf       319:                SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19      paf       320:                char content_length_cstr[MAX_NUMBER];
1.60      paf       321:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.37      paf       322:                SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35      paf       323: 
                    324:                // send header
1.37      paf       325:                SAPI::send_header(pool);
1.19      paf       326: 
                    327:                // body
1.35      paf       328:                if(!header_only)
1.37      paf       329:                        SAPI::send_body(pool, body, content_length);
1.94      parser    330: 
1.16      paf       331:                // unsuccessful finish
                    332:                return 1;
                    333:        }
                    334:        PEND_CATCH
1.95    ! parser    335: 
        !           336:        if(!cgi)
        !           337:                SAPI::send_body(pool, "\n", 1);
1.1       paf       338: }

E-mail: