myserver-commit
[Top][All Lists]
Advanced

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

[myserver-commit] [3095] Code refactoring, `HttpFile::appendDataToHTTPCh


From: Giuseppe Scrivano
Subject: [myserver-commit] [3095] Code refactoring, `HttpFile::appendDataToHTTPChannel ()' was moved to the `HttpDataHandler' class.
Date: Sat, 23 May 2009 22:31:47 +0000

Revision: 3095
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3095
Author:   gscrivano
Date:     2009-05-23 22:31:47 +0000 (Sat, 23 May 2009)
Log Message:
-----------
Code refactoring, `HttpFile::appendDataToHTTPChannel ()' was moved to the 
`HttpDataHandler' class.

Modified Paths:
--------------
    trunk/myserver/include/http_handler/http_file/http_file.h
    trunk/myserver/include/protocol/http/http_data_handler.h
    trunk/myserver/src/http_handler/cgi/cgi.cpp
    trunk/myserver/src/http_handler/fastcgi/fastcgi.cpp
    trunk/myserver/src/http_handler/http_dir/http_dir.cpp
    trunk/myserver/src/http_handler/http_file/http_file.cpp
    trunk/myserver/src/http_handler/isapi/isapi.cpp
    trunk/myserver/src/http_handler/mscgi/mscgi.cpp
    trunk/myserver/src/http_handler/scgi/scgi.cpp
    trunk/myserver/src/http_handler/wincgi/wincgi.cpp
    trunk/myserver/src/protocol/http/http_data_handler.cpp

Modified: trunk/myserver/include/http_handler/http_file/http_file.h
===================================================================
--- trunk/myserver/include/http_handler/http_file/http_file.h   2009-05-23 
21:12:13 UTC (rev 3094)
+++ trunk/myserver/include/http_handler/http_file/http_file.h   2009-05-23 
22:31:47 UTC (rev 3095)
@@ -23,7 +23,6 @@
 #include <include/protocol/protocol.h>
 #include <include/protocol/http/http_headers.h>
 #include <include/protocol/http/http_data_handler.h>
-#include <include/filter/memory_stream.h>
 
 class HttpFile  : public HttpDataHandler
 {
@@ -41,16 +40,6 @@
                string& filename);
   int deleteFile (HttpThreadContext* td,
                   string& filename);
-private:
-  static int appendDataToHTTPChannel(HttpThreadContext* td, 
-                                     char* buffer, 
-                                     u_long size,
-                                     File* appendFile, 
-                                     FiltersChain* chain,
-                                     bool append, 
-                                     bool useChunks,
-                                     u_long realBufferSize,
-                                     MemoryStream *tmpStream);
 };
 
 

Modified: trunk/myserver/include/protocol/http/http_data_handler.h
===================================================================
--- trunk/myserver/include/protocol/http/http_data_handler.h    2009-05-23 
21:12:13 UTC (rev 3094)
+++ trunk/myserver/include/protocol/http/http_data_handler.h    2009-05-23 
22:31:47 UTC (rev 3095)
@@ -23,7 +23,9 @@
 #include <include/protocol/protocol.h>
 #include "include/protocol/http/http_headers.h"
 #include <include/filter/filters_chain.h>
+#include <include/filter/memory_stream.h>
 
+
 /*!
  *Base class to handle HTTP data.
  */
@@ -41,10 +43,22 @@
   virtual ~HttpDataHandler();
 
        static void checkDataChunks(HttpThreadContext*, bool*, bool*);
+
+  static int appendDataToHTTPChannel (HttpThreadContext* td,
+                                      char* buffer,
+                                      u_long size,
+                                      File* appendFile,
+                                      FiltersChain* chain,
+                                      bool append,
+                                      bool useChunks,
+                                      u_long realBufferSize,
+                                      MemoryStream *tmpStream);
+
        static int appendDataToHTTPChannel(HttpThreadContext* td, 
-                                     char* buffer, u_long size,
+                                     char* buffer,
+                                     u_long size,
                                      File* appendFile, 
-                                     Stream* chain,
+                                     FiltersChain* chain,
                                      bool append, 
                                      bool useChunks);
 

Modified: trunk/myserver/src/http_handler/cgi/cgi.cpp
===================================================================
--- trunk/myserver/src/http_handler/cgi/cgi.cpp 2009-05-23 21:12:13 UTC (rev 
3094)
+++ trunk/myserver/src/http_handler/cgi/cgi.cpp 2009-05-23 22:31:47 UTC (rev 
3095)
@@ -420,7 +420,7 @@
   
   
     /* Send the last null chunk if needed.  */
-    if(useChunks && chain.write("0\r\n\r\n", 5, &nbw2))
+    if(useChunks && chain.getStream ()->write("0\r\n\r\n", 5, &nbw2))
     {
       return 0;       
     }
@@ -555,8 +555,9 @@
 
       td->buffer->setLength((u_int)strlen(td->buffer->getBuffer()));
 
-      if (chain.write(td->buffer->getBuffer(),
-                      static_cast<int>(td->buffer->getLength()), &nbw))
+      if (chain.getStream ()->write(td->buffer->getBuffer(),
+                                    static_cast<int> (td->buffer->getLength()),
+                                    &nbw))
       {
         *ret = 0;
         return 1;

Modified: trunk/myserver/src/http_handler/fastcgi/fastcgi.cpp
===================================================================
--- trunk/myserver/src/http_handler/fastcgi/fastcgi.cpp 2009-05-23 21:12:13 UTC 
(rev 3094)
+++ trunk/myserver/src/http_handler/fastcgi/fastcgi.cpp 2009-05-23 22:31:47 UTC 
(rev 3095)
@@ -328,7 +328,7 @@
   /* Send the last null chunk if needed.  */
   if(con.useChunks && 
      (td->response.getStatusType () == HttpResponseHeader::SUCCESSFUL))
-    chain.write("0\r\n\r\n", 5, &nbw);
+    chain.getStream ()->write("0\r\n\r\n", 5, &nbw);
 
   chain.clearAllFilters();
   con.sock.close();

Modified: trunk/myserver/src/http_handler/http_dir/http_dir.cpp
===================================================================
--- trunk/myserver/src/http_handler/http_dir/http_dir.cpp       2009-05-23 
21:12:13 UTC (rev 3094)
+++ trunk/myserver/src/http_handler/http_dir/http_dir.cpp       2009-05-23 
22:31:47 UTC (rev 3095)
@@ -311,7 +311,8 @@
   bool sortReverse = false;
   string linkPrefix;
   const char *formatString = td->securityToken.getHashedData 
("http.dir.format", 
-                                                              
MYSERVER_SECURITY_CONF | MYSERVER_VHOST_CONF |
+                                                              
MYSERVER_SECURITY_CONF |
+                                                              
MYSERVER_VHOST_CONF |
                                                               
MYSERVER_SERVER_CONF, "%f%t%s");
 
 
@@ -633,7 +634,7 @@
 
   if(!td->appendOutputs && useChunks)
   {
-    if(chain.write("0\r\n\r\n", 5, &nbw))
+    if(chain.getStream ()->write ("0\r\n\r\n", 5, &nbw))
       return 1;
   }
 

Modified: trunk/myserver/src/http_handler/http_file/http_file.cpp
===================================================================
--- trunk/myserver/src/http_handler/http_file/http_file.cpp     2009-05-23 
21:12:13 UTC (rev 3094)
+++ trunk/myserver/src/http_handler/http_file/http_file.cpp     2009-05-23 
22:31:47 UTC (rev 3095)
@@ -22,6 +22,7 @@
 #include <include/filter/gzip/gzip.h>
 #include <include/server/server.h>
 #include <include/filter/filters_chain.h>
+#include <include/filter/memory_stream.h>
 
 #include <sstream>
 #include <algorithm>
@@ -394,10 +395,10 @@
     if(td->mime)
     {
       if(td->mime && 
-         Server::getInstance()->getFiltersFactory()->chain(&chain, 
-                                                           td->mime->filters, 
-                                                           &memStream, 
-                                                           &nbw))
+         Server::getInstance()->getFiltersFactory()->chain (&chain,
+                                                            td->mime->filters,
+                                                            &memStream,
+                                                            &nbw))
       {
         file->close();
         delete file;
@@ -421,7 +422,7 @@
         chain.clearAllFilters();
         return 0;
       }
-      if(chain.addFilter(gzipFilter, &nbw))
+      if(chain.addFilter (gzipFilter, &nbw))
       {
         delete gzipFilter;
         file->close();
@@ -567,7 +568,7 @@
                                                     td->buffer->getBuffer(), 
                                                     nbr,
                                                     &(td->outputData), 
-                                                    chain.getStream(),
+                                                    &chain,
                                                     td->appendOutputs, 
                                                     useChunks))
         {
@@ -607,14 +608,14 @@
         bytesToSend -= nbr;
 
 
-        ret = appendDataToHTTPChannel(td, td->buffer->getBuffer(),
-                                      nbr,
-                                      &(td->outputData), 
-                                      &chain,
-                                      td->appendOutputs, 
-                                      useChunks,
-                                      td->buffer->getRealLength(),
-                                      &memStream);
+        ret = appendDataToHTTPChannel (td, td->buffer->getBuffer (),
+                                       nbr,
+                                       &(td->outputData),
+                                       &chain,
+                                       td->appendOutputs,
+                                       useChunks,
+                                       td->buffer->getRealLength (),
+                                       &memStream);
         if(ret)
           break;     
           
@@ -660,7 +661,7 @@
                                                          
td->buffer->getBuffer(), 
                                                          nbr,
                                                          &(td->outputData), 
-                                                         chain.getStream(),
+                                                         &chain,
                                                          td->appendOutputs, 
                                                          useChunks);
           if(ret)
@@ -670,7 +671,7 @@
                                                          0,
                                                          0,
                                                          &(td->outputData), 
-                                                         chain.getStream(),
+                                                         &chain,
                                                          td->appendOutputs, 
                                                          useChunks);
         
@@ -685,34 +686,34 @@
     file->close();
     delete file;
   }
-  catch(bad_alloc &ba)
+  catch (bad_alloc &ba)
   {
-    file->close();
+    file->close ();
     delete file;
-    s->host->warningsLogWrite("HttpFile: Error allocating memory");
-    chain.clearAllFilters();
-    return td->http->raiseHTTPError(500);
+    s->host->warningsLogWrite ("HttpFile: Error allocating memory");
+    chain.clearAllFilters ();
+    return td->http->raiseHTTPError (500);
   }
-  catch(...)
+  catch (...)
   {
-    file->close();
+    file->close ();
     delete file;
-    s->host->warningsLogWrite("HttpFile: Internal error");
+    s->host->warningsLogWrite ("HttpFile: Internal error");
     chain.clearAllFilters();
-    return td->http->raiseHTTPError(500);
+    return td->http->raiseHTTPError (500);
   };
  
   /* For logging activity.  */
   td->sentData += dataSent;
 
-  chain.clearAllFilters();
+  chain.clearAllFilters ();
   return !ret;
 }
 
 /*!
  *Constructor for the class.
  */
-HttpFile::HttpFile()
+HttpFile::HttpFile ()
 {
 
 }
@@ -720,7 +721,7 @@
 /*!
  *Destroy the object.
  */
-HttpFile::~HttpFile()
+HttpFile::~HttpFile ()
 {
 
 }
@@ -737,67 +738,7 @@
 /*!
  *Unload the static elements.
  */
-int HttpFile::unLoad()
+int HttpFile::unLoad ()
 {
   return 0;
 }
-
-/*!
- *Custom version for the appendDataToHTTPChannel function, this is
- *slower that the HttpDataHandler one but the internal buffer is
- *needed by the filters chain.
- *\param td The HTTP thread context.
- *\param buffer Data to send.
- *\param size Size of the buffer.
- *\param appendFile The file where append if in append mode.
- *\param chain Where send data if not append.
- *\param append Append to the file?
- *\param useChunks Can we use HTTP chunks to send data?
- *\param realBufferSize The real dimension of the buffer that can be
- *used by this method.
- *\param tmpStream A support on memory read/write stream used
- *internally by the function.
- */
-int HttpFile::appendDataToHTTPChannel(HttpThreadContext* td, 
-                                      char* buffer, 
-                                      u_long size,
-                                      File* appendFile, 
-                                      FiltersChain* chain,
-                                      bool append, 
-                                      bool useChunks,
-                                      u_long realBufferSize,
-                                      MemoryStream *tmpStream)
-{
-  u_long nbr, nbw;
-  Stream *oldStream = chain->getStream();
-
-  /* 
-   *This function can't append directly to the chain because we can't
-   *know in advance the data chunk size.  Therefore we replace the
-   *final stream with a memory buffer and write there the final data
-   *chunk content, finally we read from it and send directly on the
-   *original stream.
-   */
-  chain->setStream(tmpStream);
-  
-  if(chain->write(buffer, size, &nbw))
-    return 1;
-
-  if(tmpStream->read(buffer, realBufferSize, &nbr))
-    return 1;
-  
-  chain->setStream(oldStream);
-  
-  /*
-   *Use of chain->getStream() is needed to write directly on the
-   *final stream.
-   */
-  return HttpDataHandler::appendDataToHTTPChannel(td, 
-                                                  buffer, 
-                                                  nbr, 
-                                                  appendFile, 
-                                                  chain->getStream(), 
-                                                  append, 
-                                                  useChunks);
-  
-}

Modified: trunk/myserver/src/http_handler/isapi/isapi.cpp
===================================================================
--- trunk/myserver/src/http_handler/isapi/isapi.cpp     2009-05-23 21:12:13 UTC 
(rev 3094)
+++ trunk/myserver/src/http_handler/isapi/isapi.cpp     2009-05-23 22:31:47 UTC 
(rev 3095)
@@ -311,9 +311,9 @@
         /*! With keep-alive connections use chunks.*/
         if(keepalive && (!ConnInfo->td->appendOutputs))
         {
-          sprintf(chunkSize,"%x\r\n",len);
-          if(ConnInfo->chain.write(chunkSize, 
-                                   (int)strlen(chunkSize), &nbw))      
+          sprintf(chunkSize, "%x\r\n", len);
+          if(ConnInfo->chain.getStream ()->write(chunkSize,
+                                                 (int)strlen(chunkSize), &nbw))
             return 0;
         }
         
@@ -326,16 +326,16 @@
         }
         else
         {
-          if(ConnInfo->chain.write((char*)(buffer + headerSize), 
-                                   len, &nbw))
+          if(ConnInfo->chain.write ((char*)(buffer + headerSize),
+                                    len, &nbw))
             return 0;
           ConnInfo->dataSent += nbw;
         }
 
-        /*! Send the chunk tailer.*/
+        /*! Send the chunk footer.  */
         if(keepalive && (!ConnInfo->td->appendOutputs))
         {
-          if(ConnInfo->chain.write("\r\n", 2, &nbw))          
+          if (ConnInfo->chain.getStream ()->write ("\r\n", 2, &nbw))
             return 0;
         }
       }
@@ -349,13 +349,13 @@
   {
     if(keepalive  && (!ConnInfo->td->appendOutputs))
     {
-      sprintf(chunkSize,"%x\r\n",*lpdwBytes);
-      nbw = ConnInfo->connection->socket->send(chunkSize, 
-                                               (int)strlen(chunkSize), 0);
+      sprintf (chunkSize, "%x\r\n", *lpdwBytes);
+      nbw = ConnInfo->connection->socket->send (chunkSize, 
+                                                (int)strlen(chunkSize), 0);
       if((nbw == (u_long)-1) || (!nbw))
         return 0;
     }
-    
+
     if(ConnInfo->td->appendOutputs)
     {
       if(ConnInfo->td->outputData.writeToFile((char*)Buffer,*lpdwBytes, &nbw))
@@ -371,7 +371,7 @@
 
     if(keepalive  && (!ConnInfo->td->appendOutputs))
     {
-      nbw = ConnInfo->connection->socket->send("\r\n",2, 0);
+      nbw = ConnInfo->connection->socket->send("\r\n", 2, 0);
       if((nbw == (u_long)-1) || (!nbw))
         return 0;
     }
@@ -946,7 +946,7 @@
   }
 
   Ret = HttpExtensionProc(&ExtCtrlBlk);
-  if (Ret == HSE_STATUS_PENDING) 
+  if (Ret == HSE_STATUS_PENDING)
   {
     WaitForSingleObject(connTable[connIndex].ISAPIDoneEvent, timeout);
   }
@@ -956,7 +956,7 @@
     HttpRequestHeader::Entry *connection = 
connTable[connIndex].td->request.other.get("Connection");
 
     if(connection && !stringcmpi(connection->value->c_str(), "keep-alive"))
-      Ret = connTable[connIndex].chain.write("0\r\n\r\n", 5, &nbw);
+      Ret = connTable[connIndex].chain.getStream ()->write("0\r\n\r\n", 5, 
&nbw);
   }
   
   switch(Ret) 

Modified: trunk/myserver/src/http_handler/mscgi/mscgi.cpp
===================================================================
--- trunk/myserver/src/http_handler/mscgi/mscgi.cpp     2009-05-23 21:12:13 UTC 
(rev 3094)
+++ trunk/myserver/src/http_handler/mscgi/mscgi.cpp     2009-05-23 22:31:47 UTC 
(rev 3095)
@@ -154,7 +154,7 @@
 
   if(!td->appendOutputs && data.useChunks && !data.error)
   {
-    if(chain.write("0\r\n\r\n", 5, &nbw))
+    if(chain.getStream ()->write("0\r\n\r\n", 5, &nbw))
       return 0;
   }
 
@@ -193,7 +193,7 @@
                                               (char*) data,
                                               len,
                                               &(mcd->td->outputData), 
-                                              mcd->filtersChain->getStream(),
+                                              mcd->filtersChain,
                                               mcd->td->appendOutputs, 
                                               mcd->useChunks))
     return 1;

Modified: trunk/myserver/src/http_handler/scgi/scgi.cpp
===================================================================
--- trunk/myserver/src/http_handler/scgi/scgi.cpp       2009-05-23 21:12:13 UTC 
(rev 3094)
+++ trunk/myserver/src/http_handler/scgi/scgi.cpp       2009-05-23 22:31:47 UTC 
(rev 3095)
@@ -338,7 +338,7 @@
     
     if(!td->appendOutputs && useChunks)
     {
-      if(chain->write("0\r\n\r\n", 5, &nbw))
+      if(chain->getStream ()->write("0\r\n\r\n", 5, &nbw))
         return -1;
     }
   }

Modified: trunk/myserver/src/http_handler/wincgi/wincgi.cpp
===================================================================
--- trunk/myserver/src/http_handler/wincgi/wincgi.cpp   2009-05-23 21:12:13 UTC 
(rev 3094)
+++ trunk/myserver/src/http_handler/wincgi/wincgi.cpp   2009-05-23 22:31:47 UTC 
(rev 3095)
@@ -375,9 +375,9 @@
      */
     HttpHeaders::buildHTTPResponseHeader(td->buffer->getBuffer(),
                                           &td->response);
-    if(chain.write((const char*)td->buffer->getBuffer(),
-                          (int)strlen((const char*)td->buffer->getBuffer()), 
-                   &nbw2))
+    if(chain.getStream ()->write((const char*)td->buffer->getBuffer(),
+                                 (int)strlen((const 
char*)td->buffer->getBuffer()), 
+                                 &nbw2))
     {
       OutFileHandle.close();
       FilesUtility::deleteFile(outFilePath);

Modified: trunk/myserver/src/protocol/http/http_data_handler.cpp
===================================================================
--- trunk/myserver/src/protocol/http/http_data_handler.cpp      2009-05-23 
21:12:13 UTC (rev 3094)
+++ trunk/myserver/src/protocol/http/http_data_handler.cpp      2009-05-23 
22:31:47 UTC (rev 3095)
@@ -61,7 +61,74 @@
 }
 
 /*!
- *Send data over the HTTP channel.
+ *Send data over the HTTP channel.  This method considers modifier filters.
+ *in the filters chain.
+ *\param td The HTTP thread context.
+ *\param buffer Data to send.
+ *\param size Size of the buffer.
+ *\param appendFile The file where append if in append mode.
+ *\param chain Where send data if not append.
+ *\param append Append to the file?
+ *\param useChunks Can we use HTTP chunks to send data?
+ *\param realBufferSize The real dimension of the buffer that can be
+ *used by this method.
+ *\param tmpStream A support on memory read/write stream used
+ *internally by the function.
+ */
+int HttpDataHandler::appendDataToHTTPChannel (HttpThreadContext* td,
+                                              char* buffer,
+                                              u_long size,
+                                              File* appendFile,
+                                              FiltersChain* chain,
+                                              bool append,
+                                              bool useChunks,
+                                              u_long realBufferSize,
+                                              MemoryStream *tmpStream)
+{
+  u_long nbr, nbw;
+  Stream *oldStream = chain->getStream ();
+
+  if (!chain->hasModifiersFilters ())
+    return appendDataToHTTPChannel (td,
+                                    buffer,
+                                    size,
+                                    appendFile,
+                                    chain,
+                                    append,
+                                    useChunks);
+
+  /*
+   *This function can't append directly to the chain because we can't
+   *know in advance the data chunk size.  Therefore we replace the
+   *final stream with a memory buffer and write there the final data
+   *chunk content, finally we read from it and send directly to the
+   *original stream.
+   */
+  chain->setStream (tmpStream);
+
+  if (chain->write (buffer, size, &nbw))
+    return 1;
+
+  if (tmpStream->read (buffer, realBufferSize, &nbr))
+    return 1;
+
+  chain->setStream (oldStream);
+
+  /*
+   *Use of chain->getStream() is needed to write directly on the
+   *final stream.
+   */
+  return appendDataToHTTPChannel (td,
+                                  buffer,
+                                  nbr,
+                                  appendFile,
+                                  chain,
+                                  append,
+                                  useChunks);
+}
+
+/*!
+ *Send raw data over the HTTP channel.  It doesn't consider modifier filters.
  *Return zero on success.
  *\param td The HTTP thread context.
  *\param buffer Data to send.
@@ -71,45 +138,45 @@
  *\param append Append to the file?
  *\param useChunks Can we use HTTP chunks to send data?
  */
-int HttpDataHandler::appendDataToHTTPChannel(HttpThreadContext* td, 
-                                             char* buffer, u_long size,
-                                             File* appendFile, 
-                                             Stream* chain,
-                                             bool append, 
-                                             bool useChunks)
+int HttpDataHandler::appendDataToHTTPChannel (HttpThreadContext* td,
+                                              char* buffer,
+                                              u_long size,
+                                              File* appendFile,
+                                              FiltersChain* chain,
+                                              bool append,
+                                              bool useChunks)
 {
   u_long nbw;
+
+  if (chain->hasModifiersFilters ())
+    return 1;
+
   if(append)
-  {
     return appendFile->writeToFile(buffer, size, &nbw);
-  }
   else
-  {
-    if(useChunks)
-      {
-      ostringstream chunkHeader;
-      u_long flushNbw = 0;
-      chunkHeader << hex << size << "\r\n"; 
+    {
+      if(useChunks)
+        {
+          ostringstream chunkHeader;
+          u_long flushNbw = 0;
+          chunkHeader << hex << size << "\r\n";
 
-      if(chain->flush(&flushNbw))
-        return 1;
+          if(chain->flush(&flushNbw))
+            return 1;
 
-      if(chain->write(chunkHeader.str().c_str(), 
-                                   chunkHeader.str().length(), &nbw))
+          if(chain->getStream ()->write(chunkHeader.str().c_str(),
+                                        chunkHeader.str().length(), &nbw))
+            return 1;
+        }
+
+      if(size && chain->write(buffer, size, &nbw))
         return 1;
-    }
 
-    if(size)
-      if(chain->write(buffer, size, &nbw))
+      if(useChunks && chain->getStream ()->write("\r\n", 2, &nbw))
         return 1;
 
-    if(useChunks)
-
-    if(chain->write("\r\n", 2, &nbw))
-      return 1;
-
-    return 0;
-  }
+      return 0;
+    }
   return 1;
 }
 





reply via email to

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