Example #1
0
void HttpServer::HttpCallback(
                              struct mg_connection *conn,
                              const struct mg_request_info *request_info,
                              void *user_data
                             )
{
 const char * url = request_info->uri;
 const char * query = request_info->query_string;

 HttpServer *This = (HttpServer*)user_data;

 char *outdata=NULL;
 int freer_type=0;

 try
  {
   This->StartProcessingQuery(url);

   char * content_type = "text/html";

   bool page_generated=false;
   if( strcmp(request_info->request_method,"GET")==0 )
    {
     page_generated = This->GenerateHtml( url, query, &outdata, &freer_type, &content_type );
    }
   else if( strcmp(request_info->request_method,"POST")==0 )
    {
     page_generated = This->ProcessPostRequest( url, query, request_info->post_data, request_info->post_data_len, &outdata, &freer_type, &content_type );
    }

   if( page_generated )
    This->WriteHeader( conn, content_type );
   else
    This->PageNotFound( conn );

   if( outdata!=NULL )
    {
     const int l = strlen(outdata);
     if( l<8000 )
      {
       mg_printf( conn, "%s", outdata );
      }
     else
      {
       int tail=l;
       int ipos=0; 

       while( tail>0 )
        {
         int chunk=8000; 
         if( tail<8000 )
          chunk = tail;

         char c = outdata[ipos+chunk];
         outdata[ipos+chunk] = 0; 
         mg_printf( conn, "%s", outdata+ipos );
         outdata[ipos+chunk] = c;
         ipos += chunk;
         tail -= chunk;  
        }
      }
    }
  }
 catch(...)
  {
   if( lem::LogFile::IsOpen() )
    {
     lem::MemFormatter mem;
     mem.printf( "Inhandled exception in file %s line %d", __FILE__, __LINE__ );
     lem::LogFile::Print( mem.string() );
    }
  }

 try
  {
   switch( freer_type )
   {
    case 0: break;
    case 1: delete[] outdata; break;
    case 2: free(outdata); break;
   }
  }
 catch(...)
  {
   if( lem::LogFile::IsOpen() )
    {
     lem::MemFormatter mem;
     mem.printf( "Inhandled exception in file %s line %d", __FILE__, __LINE__ );
     lem::LogFile::Print( mem.string() );
    }
  } 

 try
  {
   This->StartProcessingQuery(url);
  }
 catch(...)
  {
   if( lem::LogFile::IsOpen() )
    {
     lem::MemFormatter mem;
     mem.printf( "Inhandled exception in file %s line %d", __FILE__, __LINE__ );
     lem::LogFile::Print( mem.string() );
    }
  } 

 return;
}