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

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.106   ! parser      8: static const char *RCSId="$Id: parser3.C,v 1.105 2001/09/04 15:57:23 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];
1.98      parser     63:                strncpy(file_spec, argv0, MAX_STRING-1);  file_spec[MAX_STRING-1]=0; // filespec of my binary
1.61      paf        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.97      parser    126: //
                    127: 
                    128: char *full_file_spec(char *file_name) {
                    129:        if(!strchr(file_name, '/')) {
                    130:                static char cwd[MAX_STRING];  getcwd(cwd, MAX_STRING);
                    131:                static char buf[MAX_STRING];
                    132:                snprintf(buf, MAX_STRING, "%s/%s", cwd, file_name);
                    133:                return buf;
                    134:        }
                    135:        return file_name;
                    136: }
                    137: 
1.40      paf       138: /**
                    139:        main workhorse
1.19      paf       140: 
1.56      paf       141:        @todo 
1.40      paf       142:                IIS: remove trailing default-document[index.html] from $request.uri.
                    143:                to do that we need to consult metabase,
                    144:                wich is tested but seems slow.
                    145: */
1.5       paf       146: int main(int argc, char *argv[]) {
1.45      paf       147:        argv0=argv[0];
                    148: 
1.32      paf       149:        umask(2);
                    150: 
1.3       paf       151:        // were we started as CGI?
1.20      paf       152:        cgi=
1.2       paf       153:                getenv("SERVER_SOFTWARE") || 
                    154:                getenv("SERVER_NAME") || 
                    155:                getenv("GATEWAY_INTERFACE") || 
                    156:                getenv("REQUEST_METHOD");
1.5       paf       157:        
1.10      paf       158:        if(!cgi) {
                    159:                if(argc<2) {
1.69      paf       160:                        printf(
1.100     parser    161:                                "Parser/%s Copyright(c) 2001 ArtLebedev Group(http://www.artlebedev.com)\n"
                    162:                                "Author: Alexander Petrosyan <paf@design.ru>(http://design.ru/paf)\n"
                    163:                                "\n"
                    164:                                "Usage: %s <file>\n",
1.69      paf       165:                                PARSER_VERSION, 
                    166:                                argv0?argv0:"parser3");
1.67      paf       167:                        return 1;
1.10      paf       168:                }
                    169:        }
                    170: 
1.100     parser    171: #ifdef WIN32
                    172:        setmode(fileno(stdin), _O_BINARY);
                    173:        setmode(fileno(stdout), _O_BINARY);
                    174:        setmode(fileno(stderr), _O_BINARY);
                    175: #endif
                    176: 
1.26      paf       177:        char *filespec_to_process=cgi?getenv("PATH_TRANSLATED"):argv[1];
1.36      paf       178: #ifdef WIN32
1.43      paf       179:        back_slashes_to_slashes(filespec_to_process);
1.36      paf       180: #endif
1.97      parser    181:        filespec_to_process=full_file_spec(filespec_to_process);
1.10      paf       182: 
1.35      paf       183:        const char *request_method=getenv("REQUEST_METHOD");
                    184:        bool header_only=request_method && strcasecmp(request_method, "HEAD")==0;
1.5       paf       185:        PTRY { // global try
                    186:                // must be first in PTRY{}PCATCH
1.40      paf       187: #ifdef WIN32
1.5       paf       188:                SetUnhandledExceptionFilter(&TopLevelExceptionFilter);
                    189: #endif
1.2       paf       190: 
1.62      paf       191:                // init socks
1.57      paf       192:                init_socks(pool);
                    193: 
1.73      paf       194:                // init global classes
                    195:                init_methoded_array(pool);
1.10      paf       196:                // init global variables
1.37      paf       197:                pa_globals_init(pool);
1.10      paf       198: 
1.13      paf       199:                if(!filespec_to_process)
                    200:                        PTHROW(0, 0,
                    201:                                0,
1.68      paf       202:                                "Parser/%s", PARSER_VERSION);
1.13      paf       203: 
1.10      paf       204:                // Request info
                    205:                Request::Info request_info;
1.39      paf       206:                if(cgi) {
1.51      paf       207:                        if(const char *env_document_root=SAPI::get_env(pool, "DOCUMENT_ROOT"))
1.39      paf       208:                                request_info.document_root=env_document_root;
1.51      paf       209:                        else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO")) {
1.40      paf       210:                                // IIS
1.39      paf       211:                                size_t len=strlen(filespec_to_process)-strlen(path_info);
1.40      paf       212:                                char *buf=(char *)pool.malloc(len+1);
1.98      parser    213:                                memcpy(buf, filespec_to_process, len); buf[len]=0;
1.39      paf       214:                                request_info.document_root=buf;
                    215:                        } else
                    216:                                PTHROW(0, 0,
                    217:                                        0,
1.43      paf       218:                                        "CGI: no PATH_INFO defined(in reinventing DOCUMENT_ROOT)");
1.39      paf       219:                } else {
                    220:                        static char buf[MAX_STRING];
1.98      parser    221:                        strncpy(buf, filespec_to_process, MAX_STRING-1); buf[MAX_STRING-1]=0;
1.70      paf       222:                        if(rsplit(buf, '/') || rsplit(buf, '\\')) // strip filename
                    223:                                request_info.document_root=buf;
                    224:                        else
                    225:                                request_info.document_root="";
1.13      paf       226:                }
1.10      paf       227:                request_info.path_translated=filespec_to_process;
1.35      paf       228:                request_info.method=request_method;
1.51      paf       229:                const char *query_string=SAPI::get_env(pool, "QUERY_STRING");
1.40      paf       230:                request_info.query_string=query_string;
1.68      paf       231:                if(cgi) {
1.51      paf       232:                        if(const char *env_request_uri=SAPI::get_env(pool, "REQUEST_URI"))
1.42      paf       233:                                request_info.uri=env_request_uri;
1.51      paf       234:                        else if(const char *path_info=SAPI::get_env(pool, "PATH_INFO"))
1.42      paf       235:                                if(query_string) {
                    236:                                        char *reconstructed_uri=(char *)malloc(
                    237:                                                strlen(path_info)+1/*'?'*/+
                    238:                                                strlen(query_string)+1/*0*/);
                    239:                                        strcpy(reconstructed_uri, path_info);
                    240:                                        strcat(reconstructed_uri, "?");
                    241:                                        strcat(reconstructed_uri, query_string);
                    242:                                        request_info.uri=reconstructed_uri;
                    243:                                } else
                    244:                                        request_info.uri=path_info;
                    245:                        else
                    246:                                PTHROW(0, 0,
                    247:                                        0,
1.43      paf       248:                                        "CGI: no PATH_INFO defined(in reinventing REQUEST_URI)");
1.68      paf       249: 
1.72      paf       250:                        if(const char *script_name=SAPI::get_env(pool, "SCRIPT_NAME")) {
                    251:                                size_t script_name_len=strlen(script_name);
                    252:                                size_t uri_len=strlen(request_info.uri);
                    253:                                if(strncmp(request_info.uri,script_name, script_name_len)==0 &&
                    254:                                        script_name_len != uri_len) // under IIS they are the same
                    255:                                        PTHROW(0, 0,
                    256:                                                0,
                    257:                                                "CGI: illegal call");
                    258:                        }
1.68      paf       259:                } else
1.42      paf       260:                        request_info.uri=0;
1.40      paf       261: 
1.51      paf       262:                request_info.content_type=SAPI::get_env(pool, "CONTENT_TYPE");
                    263:                const char *content_length=SAPI::get_env(pool, "CONTENT_LENGTH");
1.10      paf       264:                request_info.content_length=(content_length?atoi(content_length):0);
1.51      paf       265:                request_info.cookie=SAPI::get_env(pool, "HTTP_COOKIE");
                    266:                request_info.user_agent=SAPI::get_env(pool, "HTTP_USER_AGENT");
1.10      paf       267: 
1.5       paf       268:                // prepare to process request
1.38      paf       269:                Request request(pool,
1.10      paf       270:                        request_info,
1.91      parser    271:                        cgi ? String::UL_USER_HTML : String::UL_AS_IS
1.5       paf       272:                        );
                    273:                
                    274:                // some root-controlled location
1.102     parser    275: #ifdef SYSCONFDIR
                    276:                const char *root_auto_path=SYSCONFDIR;
                    277: #else
                    278: #      ifdef WIN32
1.10      paf       279:                // c:\windows
1.36      paf       280:                static char root_auto_path[MAX_STRING];
1.30      paf       281:                GetWindowsDirectory(root_auto_path, MAX_STRING);
1.101     parser    282: #      else
1.102     parser    283:                #error must be compiled either configure/make or MSVC++
1.101     parser    284: #      endif
1.5       paf       285: #endif
                    286:                
                    287:                // beside by binary
1.36      paf       288:                static char site_auto_path[MAX_STRING];
1.98      parser    289:                strncpy(site_auto_path, argv0, MAX_STRING-1);  site_auto_path[MAX_STRING-1]=0; // filespec of my binary
1.88      parser    290:                if(!(
                    291:                        rsplit(site_auto_path, '/') || 
1.89      parser    292:                        rsplit(site_auto_path, '\\'))) { // strip filename
1.88      parser    293:                        // no path, just filename
                    294:                        site_auto_path[0]='.'; site_auto_path[1]=0;
                    295:                }
1.5       paf       296:                
                    297:                // process the request
1.35      paf       298:                request.core(
1.30      paf       299:                        root_auto_path, false,
1.31      paf       300:                        site_auto_path, false,
1.35      paf       301:                        header_only);
1.57      paf       302: 
                    303:                //
                    304:                done_socks();
1.84      parser    305: 
                    306: #ifdef DEBUG_POOL_MALLOC
1.78      parser    307:                extern void log_pool_stats(Pool& pool);
                    308:                log_pool_stats(pool);
1.84      parser    309: #endif
1.16      paf       310: 
1.22      paf       311:                // must be last in PTRY{}PCATCH
1.40      paf       312: #ifdef WIN32
1.5       paf       313:                SetUnhandledExceptionFilter(0);
1.25      paf       314: #endif
1.100     parser    315: 
                    316: #ifndef WIN32
1.96      parser    317:                // 
                    318:                if(!cgi)
1.100     parser    319:                        SAPI::send_body(pool, "\n", 1);
                    320: #endif
1.96      parser    321: 
1.16      paf       322:                // successful finish
                    323:                return 0;
                    324:        } PCATCH(e) { // global problem 
1.43      paf       325:                // must be first in PCATCH{}
                    326: #ifdef WIN32
                    327:                SetUnhandledExceptionFilter(0);
                    328: #endif
1.44      paf       329:                // don't allocate anything on pool here:
                    330:                //   possible pool' exception not catch-ed now
                    331:                //   and there could be out-of-memory exception
1.43      paf       332: 
1.19      paf       333:                const char *body=e.comment();
1.44      paf       334:                // log it
                    335:                SAPI::log(pool, "exception in request exception handler: %s", body);
                    336: 
                    337:                //
1.19      paf       338:                int content_length=strlen(body);
1.5       paf       339: 
1.35      paf       340:                // prepare header
1.37      paf       341:                SAPI::add_header_attribute(pool, "content-type", "text/plain");
1.19      paf       342:                char content_length_cstr[MAX_NUMBER];
1.60      paf       343:                snprintf(content_length_cstr, MAX_NUMBER, "%u", content_length);
1.37      paf       344:                SAPI::add_header_attribute(pool, "content-length", content_length_cstr);
1.35      paf       345: 
                    346:                // send header
1.37      paf       347:                SAPI::send_header(pool);
1.19      paf       348: 
                    349:                // body
1.35      paf       350:                if(!header_only)
1.37      paf       351:                        SAPI::send_body(pool, body, content_length);
1.94      parser    352: 
1.100     parser    353: #ifndef WIN32
1.96      parser    354:                // 
                    355:                if(!cgi)
1.100     parser    356:                        SAPI::send_body(pool, "\n", 1);
                    357: #endif
1.96      parser    358: 
1.16      paf       359:                // unsuccessful finish
                    360:                return 1;
                    361:        }
                    362:        PEND_CATCH
1.1       paf       363: }

E-mail: