int check_if_redirect( lsi_cb_param_t * rec ) { const char *uri; const char *qs; int action = LSI_URI_REWRITE; int useHandler = 0; int len; uri = g_api->get_req_uri(rec->_session, &len); if ( len >= strlen(TEST_URL) && strncasecmp(uri, TEST_URL, strlen(TEST_URL)) == 0 ) { qs = g_api->get_req_query_string(rec->_session, NULL ); if ( parse_qs( qs, &action, &useHandler ) < 0 ) { report_error( rec->_session, qs ); return LSI_RET_OK; } if ( test_range( action ) < 0 ) report_error( rec->_session, qs ); else if ( !useHandler ) g_api->set_uri_qs(rec->_session, action, DEST_URL, sizeof(DEST_URL) -1, "", 0 ); else if ( action > 1 ) g_api->register_req_handler(rec->_session, &MNAME, TEST_URL_LEN); else report_error( rec->_session, qs ); } return LSI_RET_OK; }
static int handlerBeginProcess( lsi_session_t *session ) { const char *qs; int action = LSI_URI_REWRITE; qs = g_api->get_req_query_string(session, NULL ); if ( parse_qs( qs, &action, NULL ) < 0 ) { report_error( session, qs ); return LSI_RET_OK; } if ( action == 17 || action == 33 || action == 49 ) report_error( session, qs ); else g_api->set_uri_qs( session, action, DEST_URL, sizeof(DEST_URL) -1, "", 0 ); return 0; }
void request_handler(int sock) { // read: parse GET char buf[BUF_SIZE], req[BUF_SIZE], filename[BUF_SIZE], filepath[BUF_SIZE]; int fsize, ext; FILE *fp; read(sock, buf, sizeof(buf)); fprintf(stderr, "read: %s\n", buf); parse_request(buf, req); fprintf(stderr, "req: %s\n", req); parse_qs(req, filename); strcpy(filepath, DOCUMENT_ROOT); strcat(filepath, filename); ext = get_ext(filename); if(ext == EXT_CGI) { // handle cgi fprintf(stderr, "is cgi\n"); write_short_header(sock); dup2(sock, STDIN_FILENO); dup2(sock, STDOUT_FILENO); if( execl(filepath, filepath, NULL) <0 ) { perror("execl"); } } else { // normal files fp = fopen(filepath, "r"); if( fp == NULL ) { perror("fopen"); } fseek(fp, 0, SEEK_END); fsize = ftell(fp); fseek(fp, 0, SEEK_SET); memset(buf, 0, BUF_SIZE); if( fsize != fread(buf, fsize, 1, fp) ) { perror("fread"); } fprintf(stderr, "file: %s\n", buf); write_header(sock, ext); write(sock, buf, strlen(buf)); fclose(fp); } // if normal files: fread -> write socket // if .cig -> dup2 -> exec }