myserver-commit
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

[Myserver-commit] [2741] Method refactoring


From: Giuseppe Scrivano
Subject: [Myserver-commit] [2741] Method refactoring
Date: Wed, 06 Aug 2008 22:11:37 +0000

Revision: 2741
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=2741
Author:   gscrivano
Date:     2008-08-06 22:11:36 +0000 (Wed, 06 Aug 2008)

Log Message:
-----------
Method refactoring

Modified Paths:
--------------
    trunk/myserver/include/env.h
    trunk/myserver/include/http_dir.h
    trunk/myserver/src/cgi.cpp
    trunk/myserver/src/env.cpp
    trunk/myserver/src/http_dir.cpp

Modified: trunk/myserver/include/env.h
===================================================================
--- trunk/myserver/include/env.h        2008-08-06 17:36:52 UTC (rev 2740)
+++ trunk/myserver/include/env.h        2008-08-06 22:11:36 UTC (rev 2741)
@@ -29,6 +29,9 @@
 {
 public:
        static void buildEnvironmentString(HttpThreadContext*, char*, int = 1);
+private:
+  static void buildProcessEnvString(MemBuf& memCgi);
+  static void buildHttpHeaderEnvString(MemBuf& memCgi, HttpRequestHeader & 
req);
 };
 
 #endif

Modified: trunk/myserver/include/http_dir.h
===================================================================
--- trunk/myserver/include/http_dir.h   2008-08-06 17:36:52 UTC (rev 2740)
+++ trunk/myserver/include/http_dir.h   2008-08-06 22:11:36 UTC (rev 2741)
@@ -44,6 +44,7 @@
   HttpDir();
   virtual ~HttpDir();
 private:
+  void formatHtml(string & in, string& out);
   static bool charIsLess(char i, char j);
   static bool compareFileStructByName (HttpDir::FileStruct i, 
HttpDir::FileStruct j);
   static bool compareFileStructByTime (HttpDir::FileStruct i, 
HttpDir::FileStruct j);

Modified: trunk/myserver/src/cgi.cpp
===================================================================
--- trunk/myserver/src/cgi.cpp  2008-08-06 17:36:52 UTC (rev 2740)
+++ trunk/myserver/src/cgi.cpp  2008-08-06 22:11:36 UTC (rev 2741)
@@ -92,61 +92,62 @@
    */
   Pipe stdOutFile;
   File stdInFile;
+  int subString = cgipath[0] == '"';
+  int len = strlen(cgipath);
+  int i;
 
   td->scriptPath.assign(scriptpath);
   
+  
+  /* Do not modify the text between " and ".  */
+
+  /* Are we in a string block?  */
+  for(i = 1; i < len; i++)
   {
-    /* Do not modify the text between " and ".  */
+    if(!subString && cgipath[i] == ' ')
+      break;
+    if(cgipath[i] == '"' && cgipath[i - 1] != '\\')
+      subString = !subString;
+  }
 
-    /* Are we in a string block?  */
-    int subString = cgipath[0] == '"';
-    int len = strlen(cgipath);
-    int i;
-    for(i = 1; i < len; i++)
-    {
-      if(!subString && cgipath[i] == ' ')
-        break;
-      if(cgipath[i] == '"' && cgipath[i - 1] != '\\')
-        subString = !subString;
-    }
+  checkDataChunks(td, &keepalive, &useChunks);
 
-    checkDataChunks(td, &keepalive, &useChunks);
-
-    /*
-     *Save the cgi path and the possible arguments.
-     *the (x < len) case is when additional arguments are specified. 
-     *If the cgipath is enclosed between " and " do not consider them 
-     *when splitting directory and file name.
-     */
-    if(i < len)
-    {
-      string tmpString(cgipath);
-      int begin = tmpString[0] == '"' ? 1 : 0;
-      int end   = tmpString[i] == '"' ? i : i - 1;
-      tmpCgiPath.assign(tmpString.substr(begin, end - 1));
-      moreArg.assign(tmpString.substr(i, len - 1));  
-    }
-    else
-    {
-      int begin = (cgipath[0] == '"') ? 1 : 0;
-      int end   = (cgipath[len] == '"') ? len-1 : len;
-      tmpCgiPath.assign(&cgipath[begin], end-begin);
-      moreArg.assign("");
-    }
-    FilesUtility::splitPath(tmpCgiPath, td->cgiRoot, td->cgiFile);
+  /*
+   *Save the cgi path and the possible arguments.
+   *the (x < len) case is when additional arguments are specified. 
+   *If the cgipath is enclosed between " and " do not consider them 
+   *when splitting directory and file name.
+   */
+  if(i < len)
+  {
+    string tmpString(cgipath);
+    int begin = tmpString[0] == '"' ? 1 : 0;
+    int end   = tmpString[i] == '"' ? i : i - 1;
+    tmpCgiPath.assign(tmpString.substr(begin, end - 1));
+    moreArg.assign(tmpString.substr(i, len - 1));  
+  }
+  else
+  {
+    int begin = (cgipath[0] == '"') ? 1 : 0;
+    int end   = (cgipath[len] == '"') ? len-1 : len;
+    tmpCgiPath.assign(&cgipath[begin], end-begin);
+    moreArg.assign("");
+  }
+  FilesUtility::splitPath(tmpCgiPath, td->cgiRoot, td->cgiFile);
     
-    tmpScriptPath.assign(scriptpath);
-    FilesUtility::splitPath(tmpScriptPath, td->scriptDir, td->scriptFile);
-  }
+  tmpScriptPath.assign(scriptpath);
+  FilesUtility::splitPath(tmpScriptPath, td->scriptDir, td->scriptFile);
+  
 
   chain.setProtocol(td->http);
   chain.setProtocolData(td);
   chain.setStream(td->connection->socket);
+
   if(td->mime)
   {
     if(td->mime && Server::getInstance()->getFiltersFactory()->chain(&chain,
-                              td->mime->filters, 
-                              td->connection->socket, &nbw, 1))
+                                                        td->mime->filters, 
+                                                      td->connection->socket, 
&nbw, 1))
       {
         td->connection->host->warningsLogRequestAccess(td->id);
         td->connection->host->warningsLogWrite("Cgi: Error loading filters");

Modified: trunk/myserver/src/env.cpp
===================================================================
--- trunk/myserver/src/env.cpp  2008-08-06 17:36:52 UTC (rev 2740)
+++ trunk/myserver/src/env.cpp  2008-08-06 22:11:36 UTC (rev 2741)
@@ -47,7 +47,10 @@
                                  int processEnv)
 {
   MemBuf memCgi;
+  MemBuf portBuffer;
+  MemBuf remotePortBuffer;
   char strTmp[32];
+  HttpRequestHeader::Entry* reqEntry = NULL;
 
   memCgi.setExternalBuffer(cgiEnv, td->buffer2->getRealLength());
   memCgi << "SERVER_SOFTWARE=GNU MyServer " << versionOfSoftware;
@@ -66,36 +69,33 @@
   
   memCgi << end_str << "SERVER_NAME=";
   memCgi << Server::getInstance()->getServerName();
-
+  
   memCgi << end_str << "SERVER_SIGNATURE=";
   memCgi << "<address>GNU MyServer ";
   memCgi << versionOfSoftware;
   memCgi << "</address>";
-
+  
   memCgi << end_str << "SERVER_PROTOCOL=";
   memCgi << td->request.ver.c_str();  
   
-  {
-    MemBuf portBuffer;
-    portBuffer.uintToStr( td->connection->getLocalPort());
-    memCgi << end_str << "SERVER_PORT="<< portBuffer;
-  }
+  portBuffer.uintToStr( td->connection->getLocalPort());
+  memCgi << end_str << "SERVER_PORT="<< portBuffer;
 
   memCgi << end_str << "SERVER_ADMIN=";
   memCgi << Server::getInstance()->getServerAdmin();
 
   memCgi << end_str << "REQUEST_METHOD=";
   memCgi << td->request.cmd.c_str();
-
+  
   memCgi << end_str << "REQUEST_URI=";
   
-   memCgi << td->request.uri.c_str();
+  memCgi << td->request.uri.c_str();
 
   memCgi << end_str << "QUERY_STRING=";
   memCgi << td->request.uriOpts.c_str();
-
+  
   memCgi << end_str << "GATEWAY_INTERFACE=CGI/1.1";
-
+  
   if(td->request.contentLength.length())
   {
     memCgi << end_str << "CONTENT_LENGTH=";
@@ -133,30 +133,17 @@
 
   }
 
-  if(td->cgiRoot.length())
-  {
-    memCgi << end_str << "CGI_ROOT=";
-    memCgi << td->cgiRoot;
-  }
+  memCgi << end_str << "CGI_ROOT=";
+  memCgi << td->cgiRoot;
 
-  if(td->connection->getIpAddr()[0])
-  {
-    memCgi << end_str << "REMOTE_ADDR=";
-    memCgi << td->connection->getIpAddr();
-  }
+  memCgi << end_str << "REMOTE_ADDR=";
+  memCgi << td->connection->getIpAddr();
 
-  if(td->connection->getPort())
-  {
-    MemBuf remotePortBuffer;
-    remotePortBuffer.MemBuf::uintToStr(td->connection->getPort() );
-     memCgi << end_str << "REMOTE_PORT=" << remotePortBuffer;
-  }
+  remotePortBuffer.MemBuf::uintToStr(td->connection->getPort() );
+  memCgi << end_str << "REMOTE_PORT=" << remotePortBuffer;
 
-  if(td->connection->getLogin()[0])
-  {
-    memCgi << end_str << "REMOTE_USER=";
-    memCgi << td->connection->getLogin();
-  }
+  memCgi << end_str << "REMOTE_USER=";
+  memCgi << td->connection->getLogin();
   
   if(td->http->getProtocolOptions() & PROTOCOL_USES_SSL)
     memCgi << end_str << "SSL=ON";
@@ -178,7 +165,7 @@
     memCgi << td->filenamePath;
   }
 
-   memCgi << end_str << "SCRIPT_FILENAME=";
+  memCgi << end_str << "SCRIPT_FILENAME=";
   memCgi << td->filenamePath;
   
   /*
@@ -195,7 +182,7 @@
   getRFC822GMTTime(strTmp, HTTP_RESPONSE_DATE_DIM);
   memCgi << strTmp;
 
-   memCgi << end_str << "DATE_LOCAL=";
+  memCgi << end_str << "DATE_LOCAL=";
   getRFC822LocalTime(strTmp, HTTP_RESPONSE_DATE_DIM);
   memCgi << strTmp;
 
@@ -204,52 +191,61 @@
 
   memCgi << end_str << "DOCUMENT_URI=";
   memCgi << td->request.uri.c_str();
-  
+
   memCgi << end_str << "DOCUMENT_NAME=";
   memCgi << td->filenamePath;
 
-  if(td->connection->getLogin()[0])
-  {
-      memCgi << end_str << "REMOTE_IDENT=";
-      memCgi << td->connection->getLogin();
-  }
+  memCgi << end_str << "REMOTE_IDENT=";
+  memCgi << td->connection->getLogin();
 
-  if(td->request.auth.length())
-  {
-    memCgi << end_str << "AUTH_TYPE=";
-    memCgi << td->request.auth.c_str();
-  }
+  memCgi << end_str << "AUTH_TYPE=";
+  memCgi << td->request.auth.c_str();
 
 
+  reqEntry = td->request.other.get("Content-Type");
+
+  if(reqEntry)
   {
-    HttpRequestHeader::Entry* e = td->request.other.get("Content-Type");
-    if(e)
-    {
-      memCgi << end_str << "CONTENT_TYPE=";
-      memCgi << e->value->c_str();
-    }
+    memCgi << end_str << "CONTENT_TYPE=";
+    memCgi << reqEntry->value->c_str();
   }
 
+  buildHttpHeaderEnvString(memCgi, td->request);
 
-  {
+  buildProcessEnvString(memCgi);
 
-    HashMap<string, HttpRequestHeader::Entry*>::Iterator it = 
td->request.begin();
-    for(; it != td->request.end(); it++)
-    {
-      HttpRequestHeader::Entry* en = *it;
-      string name;
 
-      name.assign("HTTP_");
-      name.append(en->name->c_str());
-      transform(name.begin()+5, name.end(), name.begin()+5, ::toupper);
-      for(int i = name.length(); i > 5; i--)
-        if(name[i] == '-')
-          name[i] = '_';
+  memCgi << end_str << end_str  << end_str  << end_str  << end_str  ;
+}
 
-      memCgi  << end_str << name.c_str() << "=" << en->value->c_str();
-    }
+/*!
+ *Append to the environment string variables from the HTTP request header.
+ */
+void Env::buildHttpHeaderEnvString(MemBuf& memCgi, HttpRequestHeader & req)
+{
+
+  HashMap<string, HttpRequestHeader::Entry*>::Iterator it = req.begin();
+  for(; it != req.end(); it++)
+  {
+    HttpRequestHeader::Entry* en = *it;
+    string name;
+    
+    name.assign("HTTP_");
+    name.append(en->name->c_str());
+    transform(name.begin()+5, name.end(), name.begin()+5, ::toupper);
+    for(int i = name.length(); i > 5; i--)
+      if(name[i] == '-')
+        name[i] = '_';
+    
+    memCgi  << end_str << name.c_str() << "=" << en->value->c_str();
   }
+}
 
+/*!
+ *Append to the environment string process env variables.
+ */
+void Env::buildProcessEnvString(MemBuf& memCgi)
+{
 #ifdef WIN32
   if(processEnv)
   {
@@ -269,5 +265,4 @@
       }
   }
 #endif
-  memCgi << end_str << end_str  << end_str  << end_str  << end_str  ;
 }

Modified: trunk/myserver/src/http_dir.cpp
===================================================================
--- trunk/myserver/src/http_dir.cpp     2008-08-06 17:36:52 UTC (rev 2740)
+++ trunk/myserver/src/http_dir.cpp     2008-08-06 22:11:36 UTC (rev 2741)
@@ -236,8 +236,7 @@
   int ret;
   FindData fd;
   FiltersChain chain;
-  int startchar = 0;
-  int nDirectories = 0;
+  int lastSlash = 0;
   bool useChunks = false;
   u_long sentData = 0;
   int i;
@@ -266,30 +265,8 @@
     return td->http->raiseHTTPError(500);
   }
 
-  for(i = 0; td->request.uri[i]; i++)
-  {
-    if(td->request.uri[i] == '/')
-      nDirectories++;
-  }
+  lastSlash = td->request.uri.rfind('/') + 1;
 
-  for(startchar = 0, i = 0; td->request.uri[i]; i++)
-  {
-    if(td->request.uri[i] == '/')
-    {
-      startchar++;
-      if(startchar == nDirectories)
-      {
-        /*
-         *At the end of the loop set startchar to te real value.
-         *startchar indicates the initial position in td->request.uri 
-         *of the file path.
-         */
-        startchar = i + 1;
-        break;
-      }
-    }
-  }
-
   checkDataChunks(td, &keepalive, &useChunks);
 
   td->response.contentType.assign("text/html");
@@ -384,7 +361,7 @@
   filename = directory;
   td->buffer2->setLength(0);
   *td->buffer2 << "<body>\r\n<h1>Contents of directory ";
-  *td->buffer2 <<  &td->request.uri[startchar] ;
+  *td->buffer2 <<  &td->request.uri[lastSlash] ;
   *td->buffer2 << "</h1>\r\n<hr />\r\n";
 
   ret = appendDataToHTTPChannel(td, td->buffer2->getBuffer(),
@@ -516,7 +493,6 @@
       it != files.end(); it++)
   {  
     string formattedName;
-    string::size_type pos = 0;
 
     FileStruct& file = *it;
 
@@ -525,32 +501,12 @@
     *td->buffer2 << "<tr>\r\n<td><a href=\"";
     if(!td->request.uriEndsWithSlash)
     {
-      *td->buffer2 << &td->request.uri[startchar];
+      *td->buffer2 << &td->request.uri[lastSlash];
       *td->buffer2 << "/" ;
     }
     formattedName.assign(file.name);
 
-    /*
-     *Replace characters in the ranges 32-65 91-96 123-126 160-255
-     *with "&#CODE;".
-     */
-    for(pos = 0; formattedName[pos] != '\0'; pos++)
-    {
-      if(((u_char)formattedName[pos] >= 32 && 
-          (u_char)formattedName[pos] <= 65)   ||
-         ((u_char)formattedName[pos] >= 91 && 
-          (u_char)formattedName[pos] <= 96)   ||
-         ((u_char)formattedName[pos] >= 123 && 
-          (u_char)formattedName[pos] <= 126) ||
-        ((u_char)formattedName[pos] >= 160 && 
-         (u_char)formattedName[pos] < 255))
-      {
-        ostringstream os;
-        os << "&#" << (int)((unsigned char)formattedName[pos]) << ";";
-        formattedName.replace(pos, 1, os.str());
-        pos += os.str().length() - 1;
-      }
-    }
+    formatHtml(file.name, formattedName);
 
     *td->buffer2 << formattedName ;
     *td->buffer2 << "\">" ;
@@ -642,3 +598,35 @@
   return 1;
 
 }
+
+/*!
+ *Format a string to html.
+ *\param name String to convert.
+ *\param out HTML converted string.
+ */
+void HttpDir::formatHtml(string& in, string& out)
+{
+  string::size_type pos = 0;
+  out.assign(in);
+  /*
+   *Replace characters in the ranges 32-65 91-96 123-126 160-255
+   *with "&#CODE;".
+   */
+  for(pos = 0; out[pos] != '\0'; pos++)
+  {
+    if(((u_char)out[pos] >= 32 && 
+        (u_char)out[pos] <= 65)   ||
+       ((u_char)out[pos] >= 91 && 
+        (u_char)out[pos] <= 96)   ||
+       ((u_char)out[pos] >= 123 && 
+        (u_char)out[pos] <= 126) ||
+       ((u_char)out[pos] >= 160 && 
+        (u_char)out[pos] < 255))
+    {
+      ostringstream os;
+      os << "&#" << (int)((unsigned char)out[pos]) << ";";
+      out.replace(pos, 1, os.str());
+      pos += os.str().length() - 1;
+    }
+  }
+}






reply via email to

[Prev in Thread] Current Thread [Next in Thread]