Diff for /parser3/src/targets/cgi/parser3.C between versions 1.327 and 1.331

version 1.327, 2020/12/15 10:25:17 version 1.331, 2020/12/15 20:57:56
Line 1 Line 1
 /** @file  /** @file
         Parser: scripting and CGI main.          Parser: scripting and CGI main.
   
         Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)          Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com)
         Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)          Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)
 */  */
   
Line 21  volatile const char * IDENT_PARSER3_C="$ Line 21  volatile const char * IDENT_PARSER3_C="$
 #include <crtdbg.h>  #include <crtdbg.h>
 #include <windows.h>  #include <windows.h>
 #include <direct.h>  #include <direct.h>
   
   extern "C" HANDLE WINAPI GC_CreateThread(LPSECURITY_ATTRIBUTES, DWORD, LPTHREAD_START_ROUTINE, LPVOID, DWORD, LPDWORD);
   
   #else
   
   extern "C" int GC_pthread_create(pthread_t *, const pthread_attr_t *, void *(*)(void *), void * /* arg */);
   
 #endif  #endif
   
 // defines  // defines
Line 45  static bool mail_received = false; // -m Line 52  static bool mail_received = false; // -m
 static char* parser3_filespec = 0; // argv[0]  static char* parser3_filespec = 0; // argv[0]
 static char** argv_extra = NULL;  static char** argv_extra = NULL;
   
 // for signal handlers  
 static THREAD_LOCAL Request *request=0;  
   
 // for error logging  // for error logging
 static THREAD_LOCAL Request_info request_info; // global for correct log() reporting  static THREAD_LOCAL Request_info request_info; // global for correct log() reporting
 static const char* filespec_4log = NULL; // null only if system-wide auto.p used  static const char* filespec_4log = NULL; // null only if system-wide auto.p used
Line 302  static void config_handler(SAPI_Info &in Line 306  static void config_handler(SAPI_Info &in
   
         // prepare to process request          // prepare to process request
         Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));          Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
         {          // only once
                 // initing ::request ptr for signal handlers          config_filespec = locate_config(config_filespec, parser3_filespec);
                 RequestController rc(&r);          // process main auto.p only
                 // only once          r.core(config_filespec, false, String::Empty);
                 config_filespec = locate_config(config_filespec, parser3_filespec);  
                 // process main auto.p only  
                 r.core(config_filespec, false, String::Empty);  
                 // clearing ::request in RequestController desctructor to prevent signal handlers from accessing invalid memory  
         }  
 }  }
   
 static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection) {  static void connection_handler(SAPI_Info_HTTPD &info, HTTPD_Connection &connection) {
         try {          try {
                   memset(&request_info, 0, sizeof(request_info));
   
                 connection.read_header();                  connection.read_header();
                 info.populate_env();                  info.populate_env();
   
                 char document_root_buf[MAX_STRING];                  char document_root_buf[MAX_STRING];
                 full_disk_path("", document_root_buf, sizeof(document_root_buf));                  full_disk_path("", document_root_buf, sizeof(document_root_buf));
   
                 memset(&request_info, 0, sizeof(request_info));  
                 request_info.document_root = document_root_buf;                  request_info.document_root = document_root_buf;
                 request_info.path_translated = filespec_to_process;                  request_info.path_translated = filespec_to_process;
                 request_info.method = connection.method();                  request_info.method = connection.method();
Line 335  static void connection_handler(SAPI_Info Line 335  static void connection_handler(SAPI_Info
   
                 // prepare to process request                  // prepare to process request
                 Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));                  Request r(info, request_info, String::Language(String::L_HTML|String::L_OPTIMIZE_BIT));
                 {                  // process the request
                         // initing ::request ptr for signal handlers                  r.core(config_filespec, strcasecmp(request_info.method, "HEAD")==0, String("httpd-main"));
                         RequestController rc(&r);  
                         // process the request  
                         r.core(config_filespec, strcasecmp(request_info.method, "HEAD")==0, String("httpd-main"));  
                         // clearing ::request in RequestController desctructor to prevent signal handlers from accessing invalid memory  
                 }  
         } catch(const Exception& e) { // exception in connection handling or unhandled exception          } catch(const Exception& e) { // exception in connection handling or unhandled exception
                 SAPI::log(info, "%s", e.comment());                  SAPI::log(info, "%s", e.comment());
                 SAPI::send_error(info, e.comment(), info.exception_http_status(e.type()));                  SAPI::send_error(info, e.comment(), info.exception_http_status(e.type()));
         }          }
 }  }
   
   #ifdef _MSC_VER
   DWORD WINAPI connection_thread(void *arg){
   #else
 static void *connection_thread(void *arg){  static void *connection_thread(void *arg){
   #endif
         HTTPD_Connection &connection=*(HTTPD_Connection*)arg;          HTTPD_Connection &connection=*(HTTPD_Connection*)arg;
         SAPI_Info_HTTPD info(connection);          SAPI_Info_HTTPD info(connection);
   
Line 359  static void *connection_thread(void *arg Line 358  static void *connection_thread(void *arg
         }          }
   
         delete(&connection);          delete(&connection);
         return NULL;          return 0;
 }  }
   
 static void httpd_mode() {  static void httpd_mode() {
Line 375  static void httpd_mode() { Line 374  static void httpd_mode() {
 #endif  #endif
   
         while(1){          while(1){
   #ifndef _MSC_VER
                 pid_t pid=1;                  pid_t pid=1;
   #endif
                 try {                  try {
                           request_info.method=NULL; // wipe previous request for correct logging
                         HTTPD_Connection connection;                          HTTPD_Connection connection;
                         if(!connection.accept(sock, 5))                          if(!connection.accept(sock, 5))
                                 continue;                                  continue;
   
                         switch (HTTPD_Server::mode) {                          switch (HTTPD_Server::mode) {
                                 case HTTPD_Server::MULTITHREADED:                                  case HTTPD_Server::MULTITHREADED:
   #ifdef _MSC_VER
                                           if (!GC_CreateThread(0, 0, connection_thread, new HTTPD_Connection(connection), 0, 0))
                                                   throw Exception("httpd.fork", 0, "thread creation failed");
                                           connection.sock = -1;
                                           break;
   #else
                                         pthread_t thread;                                          pthread_t thread;
                                         pthread_attr_t attr;                                          pthread_attr_t attr;
                                         pthread_attr_init(&attr);                                          pthread_attr_init(&attr);
Line 393  static void httpd_mode() { Line 400  static void httpd_mode() {
                                                 throw Exception("httpd.fork", 0, "thread creation failed (%d)", result);                                                  throw Exception("httpd.fork", 0, "thread creation failed (%d)", result);
                                         connection.sock=-1;                                          connection.sock=-1;
                                         break;                                          break;
 #ifndef _MSC_VER  
                                 case HTTPD_Server::PARALLEL:                                  case HTTPD_Server::PARALLEL:
                                         pid=fork();                                          pid=fork();
                                         if(pid<0)                                          if(pid<0)
Line 411  static void httpd_mode() { Line 417  static void httpd_mode() {
                         SAPI::log(*sapiInfo, "%s", e.comment());                          SAPI::log(*sapiInfo, "%s", e.comment());
                 }                  }
   
   #ifndef _MSC_VER
                 if(pid==0) // fork child                  if(pid==0) // fork child
                         exit(0);                          exit(0);
   #endif
         }          }
 }  }
   
Line 552  static void call_real_parser_handler__su Line 560  static void call_real_parser_handler__su
 static void usage(const char* program) {  static void usage(const char* program) {
         printf(          printf(
                 "Parser/%s\n"                  "Parser/%s\n"
                 "Copyright (c) 2001-2017 Art. Lebedev Studio (http://www.artlebedev.com)\n"                  "Copyright (c) 2001-2020 Art. Lebedev Studio (http://www.artlebedev.com)\n"
                 "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"                  "Author: Alexandr Petrosian <paf@design.ru> (http://paf.design.ru)\n"
                 "\n"                  "\n"
                 "Usage: %s [options] [file]\n"                  "Usage: %s [options] [file]\n"

Removed from v.1.327  
changed lines
  Added in v.1.331


E-mail: