myserver-commit
[Top][All Lists]
Advanced

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

[myserver-commit] [SCM] GNU MyServer branch, master, updated. 0_9-350-g8


From: Giuseppe Scrivano
Subject: [myserver-commit] [SCM] GNU MyServer branch, master, updated. 0_9-350-g828a911
Date: Thu, 05 Nov 2009 10:57:22 +0000

This is an automated email from the git hooks/post-receive script. It was
generated because a ref change was pushed to the repository containing
the project "GNU MyServer".

The branch, master has been updated
       via  828a911010e1cb1391316cfc2bef87e093a0b6e3 (commit)
       via  28f71690b3810c7d704d507a5f1aeee31117b5db (commit)
      from  27a3b5c2698d4346f24471f96d48fe526af603e0 (commit)

Those revisions listed above that are new to this repository have
not appeared on any other notification email; so we list those
revisions in full, below.

- Log -----------------------------------------------------------------


commit 828a911010e1cb1391316cfc2bef87e093a0b6e3
Author: Giuseppe Scrivano <address@hidden>
Date:   Thu Nov 5 11:57:08 2009 +0100

    The HTTP response header is send once, not multiple times.

diff --git a/myserver/include/protocol/http/http.h 
b/myserver/include/protocol/http/http.h
index b320edc..054f656 100644
--- a/myserver/include/protocol/http/http.h
+++ b/myserver/include/protocol/http/http.h
@@ -1,19 +1,19 @@
 /* -*- mode: c++ -*- */
 /*
-MyServer
-Copyright (C) 2002-2009 Free Software Foundation, Inc.
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  MyServer
+  Copyright (C) 2002-2009 Free Software Foundation, Inc.
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef HTTP_H
@@ -104,25 +104,25 @@ public:
   int requestAuthorization ();
 
   int sendHTTPResource (string& filename,
+                        int systemrequest = 0,
+                        int onlyHeader = 0,
+                        int yetMapped = 0);
+
+  int putHTTPRESOURCE (string &filename,
                        int systemrequest = 0,
                        int onlyHeader = 0,
                        int yetMapped = 0);
 
-  int putHTTPRESOURCE (string &filename,
-                      int systemrequest = 0,
-                      int onlyHeader = 0,
-                      int yetMapped = 0);
-
   int optionsHTTPRESOURCE (string &filename,
-                          int yetMapped = 0);
+                           int yetMapped = 0);
 
   int traceHTTPRESOURCE (string& filename,
-                        int yetMapped = 0);
+                         int yetMapped = 0);
 
   int deleteHTTPRESOURCE (string &filename,
-                         int systemrequest = 0,
-                         int onlyHeader = 0,
-                         int yetMapped = 0);
+                          int systemrequest = 0,
+                          int onlyHeader = 0,
+                          int yetMapped = 0);
 
   bool allowMethod (const char *name);
 
@@ -134,26 +134,26 @@ public:
 
 
   int getPath (string& filenamePath,
-                     const string& filename,
-                     int systemrequest)
+               const string& filename,
+               int systemrequest)
   {return getPath (td, filenamePath, filename.c_str (), systemrequest);}
 
   int getPath (string& filenamePath,
-                     const char *filename,
-                     int systemrequest)
+               const char *filename,
+               int systemrequest)
   {return getPath (td, filenamePath, filename, systemrequest);}
 
 
   static int getPath (HttpThreadContext* td,
-                     string& filenamePath,
-                     const string& filename,
-                     int systemrequest)
+                      string& filenamePath,
+                      const string& filename,
+                      int systemrequest)
   {return getPath (td, filenamePath, filename.c_str (), systemrequest);}
 
   static int getPath (HttpThreadContext* td,
-                     string& filenamePath,
-                     const char *filename,
-                     int systemrequest);
+                      string& filenamePath,
+                      const char *filename,
+                      int systemrequest);
 
   MimeRecord* getMIME (string& filename);
 
@@ -165,7 +165,7 @@ public:
   virtual char* registerName (char* out,int len){return registerNameImpl (out, 
len);}
 
   static char* registerNameImpl (char*, int len);
-       int controlConnection (ConnectionPtr con, char *request, char *auxBuf,
+  int controlConnection (ConnectionPtr con, char *request, char *auxBuf,
                          u_long reqBufLen, u_long auxBufLen, u_long reqLen,
                          u_long tid);
 
@@ -174,18 +174,17 @@ public:
 
   u_long getTimeout ();
   int preprocessHttpRequest (string& filename, int yetmapped,
-                            int* permissions);
+                             int* permissions);
 
   int getFilePermissions (string& filename, string& directory,
-                         string& file, string &filenamePath,
-                         int yetmapped, int* permissions);
+                          string& file, string &filenamePath,
+                          int yetmapped, int* permissions);
 
   static HttpStaticData* getStaticData ();
 
   SecurityToken *getSecurityToken (){return &(td->securityToken);}
 
 protected:
-
   int processDefaultFile (string& uri, int permissions, int onlyHeader);
 
   struct HttpThreadContext *td;
@@ -201,14 +200,13 @@ protected:
 class HttpProtocol : public Protocol
 {
 public:
-       HttpProtocol ()
+  HttpProtocol ()
   {
     protocolOptions = 0;
   }
 
   virtual ~HttpProtocol ()
   {
-
   }
 
   virtual char* registerName (char* out, int len)
@@ -216,7 +214,7 @@ public:
     return Http::registerNameImpl (out, len);
   }
 
-       virtual int controlConnection (ConnectionPtr con, char *request,
+  virtual int controlConnection (ConnectionPtr con, char *request,
                                  char *auxBuf, u_long reqBufLen,
                                  u_long auxBufLen, u_long reqLen,
                                  u_long tid)
@@ -226,7 +224,7 @@ public:
                                    reqLen, tid);
   }
 
-       virtual int loadProtocol ()
+  virtual int loadProtocol ()
   {
     return Http::loadProtocolStatic ();
   }
@@ -234,9 +232,7 @@ public:
   virtual int unLoadProtocol ()
   {
     return Http::unLoadProtocolStatic ();
-
   }
-
 };
 
 
diff --git a/myserver/include/protocol/http/http_thread_context.h 
b/myserver/include/protocol/http/http_thread_context.h
index 7058306..7a069e4 100644
--- a/myserver/include/protocol/http/http_thread_context.h
+++ b/myserver/include/protocol/http/http_thread_context.h
@@ -1,19 +1,19 @@
 /* -*- mode: c++ -*- */
 /*
-MyServer
-Copyright (C) 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
+  MyServer
+  Copyright (C) 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
 
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
 
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef HTTP_THREAD_CONTEXT_H
@@ -51,7 +51,7 @@ class MimeRecord;
  */
 struct HttpThreadContext
 {
-       int appendOutputs;
+  int appendOutputs;
 
   /*! Set by raiseHTTPError.  */
   int lastError;
@@ -59,41 +59,44 @@ struct HttpThreadContext
   /*! Is the client asking only for the header?  */
   bool onlyHeader;
 
-       ConnectionPtr connection;
-       MemBuf *buffer;
-       MemBuf *secondaryBuffer;
-       u_long buffersize;
-       u_long id;
-       u_long nBytesToRead;
-       u_long nHeaderChars;
-       HttpResponseHeader response;
-       HttpRequestHeader  request;
-       string filenamePath;
-       string pathInfo;
-       string pathTranslated;
-       string cgiRoot;
-       string cgiFile;
-       string scriptPath;
-       string scriptDir;
-       string scriptFile;
-       string inputDataPath;
-       string outputDataPath;
-       string vhostDir;
-       string vhostSys;
-       HashMap<string,string*> other;
-       char identity[32];
-       File inputData;
-       File outputData;
-       int authScheme;
-       Http* http;
-       MimeRecord *mime;
-       int sentData;
+  /*! Was the HTTP header already flushed to the client?  */
+  bool headerSent;
+
+  ConnectionPtr connection;
+  MemBuf *buffer;
+  MemBuf *secondaryBuffer;
+  u_long buffersize;
+  u_long id;
+  u_long nBytesToRead;
+  u_long nHeaderChars;
+  HttpResponseHeader response;
+  HttpRequestHeader  request;
+  string filenamePath;
+  string pathInfo;
+  string pathTranslated;
+  string cgiRoot;
+  string cgiFile;
+  string scriptPath;
+  string scriptDir;
+  string scriptFile;
+  string inputDataPath;
+  string outputDataPath;
+  string vhostDir;
+  string vhostSys;
+  HashMap<string,string*> other;
+  char identity[32];
+  File inputData;
+  File outputData;
+  int authScheme;
+  Http* http;
+  MimeRecord *mime;
+  int sentData;
   SecurityToken securityToken;
   int permissions;
 
-       const char* getVhostDir ();
-       const char* getVhostSys ();
-       const char* getData (const char *name);
+  const char* getVhostDir ();
+  const char* getVhostSys ();
+  const char* getData (const char *name);
 };
 
 #endif
diff --git a/myserver/src/protocol/http/http.cpp 
b/myserver/src/protocol/http/http.cpp
index 551240a..fdafbdb 100644
--- a/myserver/src/protocol/http/http.cpp
+++ b/myserver/src/protocol/http/http.cpp
@@ -785,7 +785,8 @@ int Http::controlConnection (ConnectionPtr a, char*, char*, 
u_long, u_long,
       td->filenamePath.assign ("");
       td->outputDataPath.assign ("");
       td->inputDataPath.assign ("");
-      td->mime = 0;
+      td->mime = NULL;
+      td->headerSent = false;
       td->sentData = 0;
       td->vhostDir.assign ("");
       td->vhostSys.assign ("");
@@ -1333,8 +1334,8 @@ int Http::raiseHTTPError (int ID)
       HttpRequestHeader::Entry *host = td->request.other.get ("Host");
       HttpRequestHeader::Entry *connection = td->request.other.get 
("Connection");
       const char *useMessagesVal = td->securityToken.getData 
("http.use_error_file",
-                                                          MYSERVER_VHOST_CONF
-                                                        | MYSERVER_SERVER_CONF,
+                                                              
MYSERVER_VHOST_CONF
+                                                              | 
MYSERVER_SERVER_CONF,
                                                               NULL);
       if (useMessagesVal)
         {
@@ -1362,15 +1363,15 @@ int Http::raiseHTTPError (int ID)
       char errorName [32];
       sprintf (errorName, "http.error.file.%i", ID);
       const char *defErrorFile = td->securityToken.getData (errorName,
-                                                                  
MYSERVER_SECURITY_CONF |
-                                                                  
MYSERVER_VHOST_CONF |
-                                                                  
MYSERVER_SERVER_CONF);
+                                                            
MYSERVER_SECURITY_CONF
+                                                            | 
MYSERVER_VHOST_CONF
+                                                            | 
MYSERVER_SERVER_CONF);
       if (defErrorFile)
         {
           ostringstream nURL;
           int isPortSpecified = 0;
           const char* hostStr = host ? host->value->c_str () : "";
-           /* Change the URI to reflect the default file name.  */
+          /* Change the URI to reflect the default file name.  */
           nURL << protocolPrefix << hostStr;
           for (int i = 0; hostStr[i]; i++)
             {
@@ -1438,21 +1439,19 @@ int Http::raiseHTTPError (int ID)
             td->response.contentLength.assign (size.str ());
           }
       }
-      u_long hdrLen = HttpHeaders::buildHTTPResponseHeader 
(td->buffer->getBuffer (),
-                                                            &td->response);
 
-      if (td->connection->socket->send (td->buffer->getBuffer (), hdrLen, 0) 
== -1)
+      if (HttpHeaders::sendHeader (td->response, *td->connection->socket,
+                                   *td->buffer, td))
         return 0;
 
-      if (errorBodyLength && (td->connection->socket->send 
(errorBodyMessage.str ().c_str (),
-                                                            errorBodyLength, 0)
-                              == -1))
+      if (errorBodyLength
+          && (td->connection->socket->send (errorBodyMessage.str ().c_str (),
+                                            errorBodyLength, 0) < 0))
         {
           td->connection->host->warningsLogWrite (_("HTTP: socket error"));
           return 0;
         }
 
-
       return 1;
     }
   catch (...)
diff --git a/myserver/src/protocol/http/http_headers.cpp 
b/myserver/src/protocol/http/http_headers.cpp
index 3105ff1..36344d3 100644
--- a/myserver/src/protocol/http/http_headers.cpp
+++ b/myserver/src/protocol/http/http_headers.cpp
@@ -883,12 +883,15 @@ int HttpHeaders::sendHeader (HttpResponseHeader 
&response, Stream &stream,
                              MemBuf &memBuf, HttpThreadContext *ctx)
 {
   int ret = 0;
-  if (ctx == NULL || !ctx->appendOutputs)
+  if (ctx == NULL || (!ctx->appendOutputs && !ctx->headerSent))
     {
       u_long nbw;
       buildHTTPResponseHeader (memBuf.getBuffer (), &response);
       ret = stream.write (memBuf.getBuffer (), strlen (memBuf.getBuffer ()),
                           &nbw);
+
+      if (ctx)
+        ctx->headerSent = true;
     }
 
   return ret;



commit 28f71690b3810c7d704d507a5f1aeee31117b5db
Author: Giuseppe Scrivano <address@hidden>
Date:   Thu Nov 5 11:40:47 2009 +0100

    Define the new helper function HttpHeaders::sendHeader and use it in the 
HTTP handlers.

diff --git a/myserver/include/connection/connection.h 
b/myserver/include/connection/connection.h
index 317c36b..21ba5f3 100644
--- a/myserver/include/connection/connection.h
+++ b/myserver/include/connection/connection.h
@@ -1,19 +1,19 @@
 /* -*- mode: c++ -*- */
 /*
-MyServer
-Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009 Free Software Foundation, Inc.
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  MyServer
+  Copyright (C) 2002, 2003, 2004, 2007, 2008, 2009 Free Software Foundation, 
Inc.
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef CONNECTION_H
@@ -36,16 +36,13 @@ class ClientsThread;
 
 using namespace std;
 
-
-
 class Connection;
 
 typedef  Connection* ConnectionPtr;
 
-typedef int (*continuationPROC)(ConnectionPtr con, char *request, char *auxBuf,
-                                u_long reqBufLen, u_long auxBufLen,
-                                u_long reqLen, u_long tid);
-
+typedef int (*continuationPROC) (ConnectionPtr con, char *request, char 
*auxBuf,
+                                 u_long reqBufLen, u_long auxBufLen,
+                                 u_long reqLen, u_long tid);
 
 class Connection
 {
@@ -58,8 +55,8 @@ public:
   void init ();
   void destroy ();
 
-       int getPriority ();
-       void setPriority (int);
+  int getPriority ();
+  void setPriority (int);
 
   u_long getID ();
   void setID (u_long);
@@ -80,9 +77,9 @@ public:
   const char* getPassword ();
   void setPassword (const char*);
 
-       void setnTries (char);
-       char getnTries ();
-       void incnTries ();
+  void setnTries (char);
+  char getnTries ();
+  void incnTries ();
 
   const char* getIpAddr ();
   void setIpAddr (const char*);
@@ -90,14 +87,14 @@ public:
   const char* getLocalIpAddr ();
   void setLocalIpAddr (const char*);
 
-       u_long getTimeout ();
+  u_long getTimeout ();
   void setTimeout (u_long);
 
-       /*! Connection socket.  */
-       Socket *socket;
+  /*! Connection socket.  */
+  Socket *socket;
 
-       /*! Pointer to an host structure.  */
-       Vhost *host;
+  /*! Pointer to an host structure.  */
+  Vhost *host;
 
   int getToRemove ();
   void setToRemove (int);
@@ -105,14 +102,14 @@ public:
   int isForceControl ();
   void setForceControl (int);
 
-       /*! Buffer for the connection struct. Used by protocols.  */
-       ProtocolBuffer *protocolBuffer;
+  /*! Buffer for the connection struct. Used by protocols.  */
+  ProtocolBuffer *protocolBuffer;
 
-       /*! Set the thread that is currently using the connection.  */
-       void setActiveThread (ClientsThread* t){thread = t;}
+  /*! Set the thread that is currently using the connection.  */
+  void setActiveThread (ClientsThread* t){thread = t;}
 
-       /*! Get the thread that is using the connection.  */
-       ClientsThread* getActiveThread (){return thread;}
+  /*! Get the thread that is using the connection.  */
+  ClientsThread* getActiveThread (){return thread;}
 
   Connection ()
   {
@@ -136,10 +133,10 @@ public:
   MemBuf *getConnectionBuffer (){return connectionBuffer;}
 protected:
 
-       /*! This buffer must be used only by the ClientsTHREAD class.  */
-       MemBuf *connectionBuffer;
+  /*! This buffer must be used only by the ClientsTHREAD class.  */
+  MemBuf *connectionBuffer;
 
-       ClientsThread *thread;
+  ClientsThread *thread;
 
   /*! Continuation function.  */
   continuationPROC continuation;
@@ -147,46 +144,46 @@ protected:
   /*! Identifier for the connection.  */
   u_long ID;
 
-       /*! The server/server.has scheduled this connection.  */
-       int scheduled;
+  /*! The server/server.has scheduled this connection.  */
+  int scheduled;
 
-       /*! Remote port used.  */
-       u_short port;
+  /*! Remote port used.  */
+  u_short port;
 
-       /*! Login name.  */
-       string *login;
+  /*! Login name.  */
+  string *login;
 
-       /*! Password used to log in.  */
-       string *password;
+  /*! Password used to log in.  */
+  string *password;
 
-       /*! # of tries for an authorized login.  */
-       char nTries;
+  /*! # of tries for an authorized login.  */
+  char nTries;
 
-       /*! Remote IP address.  */
-       string *ipAddr;
+  /*! Remote IP address.  */
+  string *ipAddr;
 
-       /*! Local IP used to connect to.  */
-       string *localIpAddr;
+  /*! Local IP used to connect to.  */
+  string *localIpAddr;
 
-       /*! Local port used to connect to.  */
-       u_short localPort;
+  /*! Local port used to connect to.  */
+  u_short localPort;
 
-       /*! Current timeout for the connection.  */
-       u_long timeout;
+  /*! Current timeout for the connection.  */
+  u_long timeout;
 
-       /*
+  /*
    *!If nonzero the server is saying to the protocol to remove the connection.
    *Protocols can not consider this but is a good idea do it to avoid server
    *overloads.
    *Reasons to remove the connection are defined at the begin of this file.
    */
-       int toRemove;
+  int toRemove;
 
-       /*! Force the connection to be parsed.  */
-       int forceControl;
+  /*! Force the connection to be parsed.  */
+  int forceControl;
 
-       /*! Connection priority, used by the scheduler.  */
-       int priority;
+  /*! Connection priority, used by the scheduler.  */
+  int priority;
 
 };
 
diff --git a/myserver/include/filter/stream.h b/myserver/include/filter/stream.h
index cdfb7d3..45eccda 100644
--- a/myserver/include/filter/stream.h
+++ b/myserver/include/filter/stream.h
@@ -1,19 +1,19 @@
 /* -*- mode: c++ -*- */
 /*
-MyServer
-Copyright (C) 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  MyServer
+  Copyright (C) 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef STREAM_H
@@ -41,7 +41,7 @@ class Stream
 public:
   virtual int read (char* buffer, u_long len, u_long*);
   virtual int write (const char* buffer, u_long len, u_long*);
-       virtual int flush (u_long*);
+  virtual int flush (u_long*);
   virtual Handle getHandle ();
   virtual int close ();
   Stream ();
diff --git a/myserver/include/protocol/http/http_headers.h 
b/myserver/include/protocol/http/http_headers.h
index a480da2..9685536 100644
--- a/myserver/include/protocol/http/http_headers.h
+++ b/myserver/include/protocol/http/http_headers.h
@@ -1,19 +1,19 @@
 /* -*- mode: c++ -*- */
 /*
-MyServer
-Copyright (C) 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
-This program is free software; you can redistribute it and/or modify
-it under the terms of the GNU General Public License as published by
-the Free Software Foundation; either version 3 of the License, or
-(at your option) any later version.
-
-This program is distributed in the hope that it will be useful,
-but WITHOUT ANY WARRANTY; without even the implied warranty of
-MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
-GNU General Public License for more details.
-
-You should have received a copy of the GNU General Public License
-along with this program.  If not, see <http://www.gnu.org/licenses/>.
+  MyServer
+  Copyright (C) 2002, 2003, 2004, 2008, 2009 Free Software Foundation, Inc.
+  This program is free software; you can redistribute it and/or modify
+  it under the terms of the GNU General Public License as published by
+  the Free Software Foundation; either version 3 of the License, or
+  (at your option) any later version.
+
+  This program is distributed in the hope that it will be useful,
+  but WITHOUT ANY WARRANTY; without even the implied warranty of
+  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+  GNU General Public License for more details.
+
+  You should have received a copy of the GNU General Public License
+  along with this program.  If not, see <http://www.gnu.org/licenses/>.
 */
 
 #ifndef HTTP_HEADERS_H
@@ -22,7 +22,8 @@ along with this program.  If not, see 
<http://www.gnu.org/licenses/>.
 # include "stdafx.h"
 # include <include/connection/connection.h>
 
-extern "C" {
+extern "C"
+{
 # ifdef WIN32
 #  include <direct.h>
 #  include <errno.h>
@@ -32,13 +33,14 @@ extern "C" {
 # endif
 }
 
-
 # include <string>
 using namespace std;
 
-
-# define HTTP_AUTH_SCHEME_BASIC 0
-# define HTTP_AUTH_SCHEME_DIGEST 1
+enum
+  {
+    HTTP_AUTH_SCHEME_BASIC,
+    HTTP_AUTH_SCHEME_DIGEST
+  };
 
 struct HttpThreadContext;
 struct HttpRequestHeader;
@@ -47,27 +49,30 @@ struct HttpResponseHeader;
 class HttpHeaders
 {
 public:
-       static int buildHTTPRequestHeaderStruct (const char* input,
+  static int buildHTTPRequestHeaderStruct (const char* input,
                                            u_long inputSize,
                                            u_long* nHeaderChars,
                                            HttpRequestHeader *request,
                                            Connection* connection);
 
-       static int buildHTTPResponseHeaderStruct (const char *input,
-                                           HttpResponseHeader *response,
-                                           u_long* nbtr);
+  static int buildHTTPResponseHeaderStruct (const char *input,
+                                            HttpResponseHeader *response,
+                                            u_long* nbtr);
+
+  static int sendHeader (HttpResponseHeader &response, Stream &stream,
+                         MemBuf &memBuf, HttpThreadContext *ctx);
 
-       static int validHTTPRequest (const char*, u_long, u_long*, u_long*);
-       static int validHTTPResponse (const char*, u_long*, u_long*);
+  static int validHTTPRequest (const char*, u_long, u_long*, u_long*);
+  static int validHTTPResponse (const char*, u_long*, u_long*);
 
-       static void resetHTTPRequest (HttpRequestHeader *request);
-       static void resetHTTPResponse (HttpResponseHeader *response);
+  static void resetHTTPRequest (HttpRequestHeader *request);
+  static void resetHTTPResponse (HttpResponseHeader *response);
 
-       static void buildDefaultHTTPResponseHeader (HttpResponseHeader*);
-       static void buildDefaultHTTPRequestHeader (HttpRequestHeader*);
+  static void buildDefaultHTTPResponseHeader (HttpResponseHeader*);
+  static void buildDefaultHTTPRequestHeader (HttpRequestHeader*);
 
-       static u_long buildHTTPResponseHeader (char*, HttpResponseHeader*);
-       static u_long buildHTTPRequestHeader (char*, HttpRequestHeader*);
+  static u_long buildHTTPResponseHeader (char*, HttpResponseHeader*);
+  static u_long buildHTTPRequestHeader (char*, HttpRequestHeader*);
 
 protected:
   static int readReqAuthLine (HttpRequestHeader *request,
diff --git a/myserver/src/http_handler/cgi/cgi.cpp 
b/myserver/src/http_handler/cgi/cgi.cpp
index 444c01e..537d73a 100644
--- a/myserver/src/http_handler/cgi/cgi.cpp
+++ b/myserver/src/http_handler/cgi/cgi.cpp
@@ -514,7 +514,7 @@ int Cgi::sendHeader (HttpThreadContext* td, Pipe 
&stdOutFile, FiltersChain& chai
        */
       if (!td->appendOutputs)
         {
-          string* location = td->response.getValue ("Location", 0);
+          string* location = td->response.getValue ("Location", NULL);
 
           /* If it is present "Location: foo" in the header then send a 
redirect
            * to `foo'.  */
@@ -524,18 +524,13 @@ int Cgi::sendHeader (HttpThreadContext* td, Pipe 
&stdOutFile, FiltersChain& chai
               return 1;
             }
 
-          u_long hdrLen = HttpHeaders::buildHTTPResponseHeader 
(td->buffer->getBuffer (),
-                                                                &td->response);
-
-          td->buffer->setLength (hdrLen);
-
-          if (chain.getStream ()->write (td->buffer->getBuffer (),
-                                         static_cast<int> 
(td->buffer->getLength ()),
-                                         &nbw))
+          if (HttpHeaders::sendHeader (td->response, *chain.getStream (),
+                                       *td->buffer, td))
             {
               *ret = 0;
               return 1;
             }
+
         }
     }
 
diff --git a/myserver/src/http_handler/fastcgi/fastcgi.cpp 
b/myserver/src/http_handler/fastcgi/fastcgi.cpp
index 580b8be..0927a23 100644
--- a/myserver/src/http_handler/fastcgi/fastcgi.cpp
+++ b/myserver/src/http_handler/fastcgi/fastcgi.cpp
@@ -783,17 +783,13 @@ int FastCgi::handleHeader (FcgiContext* con, 
FiltersChain* chain, bool* response
           *responseCompleted = true;
           return con->td->http->sendHTTPRedirect (location->c_str ());
         }
+    }
 
-      u_long hdrLen = HttpHeaders::buildHTTPResponseHeader 
(con->td->secondaryBuffer->getBuffer (),
-                                                            
&con->td->response);
-
-      if (con->td->connection->socket->send 
(con->td->secondaryBuffer->getBuffer (),
-                                             hdrLen,
-                                             0) == SOCKET_ERROR )
-        {
-          *responseCompleted = true;
-          return 1;
-        }
+  if (HttpHeaders::sendHeader (con->td->response, *con->td->connection->socket,
+                               *con->td->secondaryBuffer, con->td))
+    {
+      *responseCompleted = true;
+      return 1;
     }
 
   con->headerSent = true;
diff --git a/myserver/src/http_handler/http_dir/http_dir.cpp 
b/myserver/src/http_handler/http_dir/http_dir.cpp
index 21e3c1d..89c9e32 100644
--- a/myserver/src/http_handler/http_dir/http_dir.cpp
+++ b/myserver/src/http_handler/http_dir/http_dir.cpp
@@ -361,19 +361,12 @@ int HttpDir::send (HttpThreadContext* td,
         }
     }
 
-  if (!td->appendOutputs)
-    {
-
-      HttpHeaders::buildHTTPResponseHeader (td->buffer->getBuffer (),
-                                            &(td->response));
-
-      if (td->connection->socket->send (td->buffer->getBuffer (),
-                                   strlen (td->buffer->getBuffer ()), 0) < 0)
-        return 0;
-    }
+  if (HttpHeaders::sendHeader (td->response, *td->connection->socket,
+                               *td->buffer, td))
+    return 1;
 
   if (onlyHeader)
-    return 1;
+    return 0;
 
   sortIndex = td->request.uriOpts.find ("sort=");
 
@@ -676,14 +669,14 @@ void HttpDir::formatHtml (string& in, string& out)
    */
   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))
+      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]) << ";";
diff --git a/myserver/src/http_handler/http_file/http_file.cpp 
b/myserver/src/http_handler/http_file/http_file.cpp
index 4df8458..d22c223 100644
--- a/myserver/src/http_handler/http_file/http_file.cpp
+++ b/myserver/src/http_handler/http_file/http_file.cpp
@@ -420,23 +420,13 @@ int HttpFile::send (HttpThreadContext* td, const char 
*filenamePath,
          delete e;
       }
 
-    u_long hdrLen = HttpHeaders::buildHTTPResponseHeader 
(td->buffer->getBuffer (),
-                                                          &td->response);
-
-    td->buffer->setLength (hdrLen);
-
-    if (!td->appendOutputs)
+    if (HttpHeaders::sendHeader (td->response, *td->connection->socket,
+                                 *td->buffer, td))
       {
-        /* Send the HTTP header.  */
-        if (td->connection->socket->send (td->buffer->getBuffer (),
-                                          (u_long) td->buffer->getLength (),
-                                          0) == SOCKET_ERROR)
-          {
-            file->close ();
-            delete file;
-            chain.clearAllFilters ();
-            return 1;
-          }
+        file->close ();
+        delete file;
+        chain.clearAllFilters ();
+        return 1;
       }
 
     /*
diff --git a/myserver/src/http_handler/isapi/isapi.cpp 
b/myserver/src/http_handler/isapi/isapi.cpp
index 1a0b81e..4283296 100644
--- a/myserver/src/http_handler/isapi/isapi.cpp
+++ b/myserver/src/http_handler/isapi/isapi.cpp
@@ -217,158 +217,156 @@ BOOL WINAPI ISAPI_WriteClientExport (HCONN hConn, 
LPVOID Buffer, LPDWORD lpdwByt
 
   /*If the HTTP header was sent do not send it again. */
   if (!ConnInfo->headerSent)
-  {
-    int headerSize = 0;
-    u_long size = (u_long)strlen (buffer);
-    strncat (buffer, (char*)Buffer, *lpdwBytes);
-    ConnInfo->headerSize += *lpdwBytes;
-    if (buffer[0] == '\r')
-    {
-      if (buffer[1] == '\n')
-        headerSize = 2;
-    }
-    else
     {
-      for (u_long i = 0; i < size; i++)
-      {
-         if (buffer[i] == '\r')
-          if (buffer[i+1] == '\n')
-            if (buffer[i+2] == '\r')
-              if (buffer[i+3] == '\n')
-              {
-                headerSize = i + 4;
-                buffer[i + 2]='\0';
-                break;
-              }
-        if (buffer[i] == '\n')
+      int headerSize = 0;
+      u_long size = (u_long)strlen (buffer);
+      strncat (buffer, (char*)Buffer, *lpdwBytes);
+      ConnInfo->headerSize += *lpdwBytes;
+      if (buffer[0] == '\r')
         {
-          if (buffer[i+1] == '\n')
-          {
-            headerSize = i + 2;
-            buffer[i + 1]='\0';
-            break;
-          }
+          if (buffer[1] == '\n')
+            headerSize = 2;
         }
-      }
-    }
-    /*!
-     *Handle the HTTP header if exists.
-     */
-    if (headerSize)
-    {
-      int len = ConnInfo->headerSize-headerSize;
-
-      HttpHeaders::buildHTTPResponseHeaderStruct 
(ConnInfo->td->buffer->getBuffer (),
-                                                 &ConnInfo->td->response,
-                                                 
&(ConnInfo->td->nBytesToRead));
-
-
-      if (!ConnInfo->td->appendOutputs)
-      {
-        if (keepalive)
+      else
         {
-          HttpResponseHeader::Entry *e;
-          e = ConnInfo->td->response.other.get ("Transfer-Encoding");
-          if (e)
-            e->value->assign ("chunked");
-          else
-          {
-            e = new HttpResponseHeader::Entry ();
-            e->name->assign ("Transfer-Encoding");
-            e->value->assign ("chunked");
-            ConnInfo->td->response.other.put (*(e->name), e);
-          }
+          for (u_long i = 0; i < size; i++)
+            {
+              if (buffer[i] == '\r')
+                if (buffer[i+1] == '\n')
+                  if (buffer[i+2] == '\r')
+                    if (buffer[i+3] == '\n')
+                      {
+                        headerSize = i + 4;
+                        buffer[i + 2]='\0';
+                        break;
+                      }
+              if (buffer[i] == '\n')
+                {
+                  if (buffer[i+1] == '\n')
+                    {
+                      headerSize = i + 2;
+                      buffer[i + 1]='\0';
+                      break;
+                    }
+                }
+            }
         }
-        else
-          ConnInfo->td->response.setValue ("Connection", "Close");
-
-        u_long hdrLen = HttpHeaders::buildHTTPResponseHeader 
((char*)ConnInfo->td->secondaryBuffer->getBuffer (),
-                                                              
&(ConnInfo->td->response));
+      /*!
+       *Handle the HTTP header if exists.
+       */
+      if (headerSize)
+        {
+          int len = ConnInfo->headerSize-headerSize;
+
+          HttpHeaders::buildHTTPResponseHeaderStruct 
(ConnInfo->td->buffer->getBuffer (),
+                                                      &ConnInfo->td->response,
+                                                      
&(ConnInfo->td->nBytesToRead));
+
+
+          if (!ConnInfo->td->appendOutputs)
+            {
+              if (keepalive)
+                {
+                  HttpResponseHeader::Entry *e;
+                  e = ConnInfo->td->response.other.get ("Transfer-Encoding");
+                  if (e)
+                    e->value->assign ("chunked");
+                  else
+                    {
+                      e = new HttpResponseHeader::Entry ();
+                      e->name->assign ("Transfer-Encoding");
+                      e->value->assign ("chunked");
+                      ConnInfo->td->response.other.put (*(e->name), e);
+                    }
+                }
+              else
+                ConnInfo->td->response.setValue ("Connection", "Close");
+
+
+              if (HttpHeaders::sendHeader (ConnInfo->td->response, 
*td->connection->socket,
+                                           *ConnInfo->td->secondaryBuffer, 
ConnInfo->td))
+                return 1;
+            }
+          /*! Save the headerSent status. */
+          ConnInfo->headerSent=1;
+
+          /*! If only the header was requested return. */
+          if (ConnInfo->headerSent && ConnInfo->onlyHeader
+              || ConnInfo->td->response.getStatusType () == 
HttpResponseHeader::SUCCESSFUL)
+            return 0;
 
-        if (ConnInfo->connection->socket->send (
-                     (char*)ConnInfo->td->secondaryBuffer->getBuffer (), 
hdrLen, 0)==-1)
-          return 0;
-      }
-      /*! Save the headerSent status. */
-      ConnInfo->headerSent=1;
-
-      /*! If only the header was requested return. */
-      if (ConnInfo->headerSent && ConnInfo->onlyHeader
-          || ConnInfo->td->response.getStatusType () == 
HttpResponseHeader::SUCCESSFUL)
-        return 0;
-
-      /*!Send the first chunk. */
-      if (len)
-      {
-        /*! With keep-alive connections use chunks.*/
-        if (keepalive && (!ConnInfo->td->appendOutputs))
+          /*!Send the first chunk. */
+          if (len)
+            {
+              /*! With keep-alive connections use chunks.*/
+              if (keepalive && (!ConnInfo->td->appendOutputs))
+                {
+                  sprintf (chunkSize, "%x\r\n", len);
+                  if (ConnInfo->chain.getStream ()->write (chunkSize,
+                                                           (int)strlen 
(chunkSize), &nbw))
+                    return 0;
+                }
+
+              if (ConnInfo->td->appendOutputs)
+                {
+                  if (ConnInfo->td->outputData.writeToFile ((char*)(buffer + 
headerSize),
+                                                            len, &nbw))
+                    return 0;
+                  ConnInfo->dataSent += nbw;
+                }
+              else
+                {
+                  if (ConnInfo->chain.write ((char*)(buffer + headerSize),
+                                             len, &nbw))
+                    return 0;
+                  ConnInfo->dataSent += nbw;
+                }
+
+              /*! Send the chunk footer.  */
+              if (keepalive && (!ConnInfo->td->appendOutputs))
+                {
+                  if (ConnInfo->chain.getStream ()->write ("\r\n", 2, &nbw))
+                    return 0;
+                }
+            }
+        }
+      else
         {
-          sprintf (chunkSize, "%x\r\n", len);
-          if (ConnInfo->chain.getStream ()->write (chunkSize,
-                                                 (int)strlen (chunkSize), 
&nbw))
+          nbw = *lpdwBytes;
+        }
+    }
+  else/*!Continue to send data chunks*/
+    {
+      if (keepalive  && (!ConnInfo->td->appendOutputs))
+        {
+          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->appendOutputs)
         {
-          if (ConnInfo->td->outputData.writeToFile ((char*)(buffer + 
headerSize),
-                                                  len, &nbw))
+          if (ConnInfo->td->outputData.writeToFile ((char*)Buffer,*lpdwBytes, 
&nbw))
             return 0;
           ConnInfo->dataSent += nbw;
         }
-        else
+      else
         {
-          if (ConnInfo->chain.write ((char*)(buffer + headerSize),
-                                    len, &nbw))
+          if (ConnInfo->chain.write ((char*)Buffer,*lpdwBytes, &nbw))
             return 0;
           ConnInfo->dataSent += nbw;
         }
 
-        /*! Send the chunk footer.  */
-        if (keepalive && (!ConnInfo->td->appendOutputs))
+      if (keepalive  && (!ConnInfo->td->appendOutputs))
         {
-          if (ConnInfo->chain.getStream ()->write ("\r\n", 2, &nbw))
+          nbw = ConnInfo->connection->socket->send ("\r\n", 2, 0);
+          if ((nbw == (u_long)-1) || (!nbw))
             return 0;
         }
-      }
-    }
-    else
-    {
-      nbw = *lpdwBytes;
-    }
-  }
-  else/*!Continue to send data chunks*/
-  {
-    if (keepalive  && (!ConnInfo->td->appendOutputs))
-    {
-      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))
-        return 0;
-      ConnInfo->dataSent += nbw;
-    }
-    else
-    {
-      if (ConnInfo->chain.write ((char*)Buffer,*lpdwBytes, &nbw))
-        return 0;
-      ConnInfo->dataSent += nbw;
     }
 
-    if (keepalive  && (!ConnInfo->td->appendOutputs))
-    {
-      nbw = ConnInfo->connection->socket->send ("\r\n", 2, 0);
-      if ((nbw == (u_long)-1) || (!nbw))
-        return 0;
-    }
-  }
-
   *lpdwBytes = nbw;
 
   ConnInfo->td->sentData += ConnInfo->dataSent;
@@ -409,84 +407,84 @@ BOOL WINAPI ISAPI_ReadClientExport (HCONN hConn, LPVOID 
lpvBuffer,
 }
 
 /*!
- *Get server environment variable.
+ * Get server environment variable.
  */
 BOOL WINAPI ISAPI_GetServerVariableExport (HCONN hConn,
-                                          LPSTR lpszVariableName,
-                                          LPVOID lpvBuffer,
-                                          LPDWORD lpdwSize)
+                                           LPSTR lpszVariableName,
+                                           LPVOID lpvBuffer,
+                                           LPDWORD lpdwSize)
 {
   ConnTableRecord *ConnInfo;
-  BOOL ret =1;
+  BOOL ret = 1;
   Isapi::isapiMutex->lock ();
   ConnInfo = Isapi::HConnRecord (hConn);
   Isapi::isapiMutex->unlock ();
   if (ConnInfo == NULL)
-  {
-    Server::getInstance ()->log (
-                       "Isapi::GetServerVariableExport: invalid hConn");
-    return 0;
-  }
+    {
+      Server::getInstance ()->log (
+                            "Isapi::GetServerVariableExport: invalid hConn");
+      return 0;
+    }
 
   if (!strcmp (lpszVariableName, "ALL_HTTP"))
-  {
+    {
 
-        if (Isapi::buildAllHttpHeaders (ConnInfo->td,ConnInfo->connection, 
lpvBuffer, lpdwSize))
-      ret=1;
+      if (Isapi::buildAllHttpHeaders (ConnInfo->td,ConnInfo->connection,
+                                      lpvBuffer, lpdwSize))
+        ret = 1;
 
-        else
-    {
-      SetLastError (ERROR_INSUFFICIENT_BUFFER);
-      ret=0;
-    }
+      else
+        {
+          SetLastError (ERROR_INSUFFICIENT_BUFFER);
+          ret = 0;
+        }
 
-  }else if (!strcmp (lpszVariableName, "ALL_RAW"))
-  {
-    if (Isapi::buildAllRawHeaders (ConnInfo->td,ConnInfo->connection,
-                                 lpvBuffer, lpdwSize))
-      ret = 1;
-    else
-    {
-      SetLastError (ERROR_INSUFFICIENT_BUFFER);
-      ret = 0;
     }
+  else if (!strcmp (lpszVariableName, "ALL_RAW"))
+    {
+      if (Isapi::buildAllRawHeaders (ConnInfo->td,ConnInfo->connection,
+                                     lpvBuffer, lpdwSize))
+        ret = 1;
+      else
+        {
+          SetLastError (ERROR_INSUFFICIENT_BUFFER);
+          ret = 0;
+        }
 
-  }
+    }
   else
-  {
-    /*!
-     *find in ConnInfo->envString the value lpszVariableName
-     *and copy next string in lpvBuffer.
-     */
-    char *localEnv;
-    int variableNameLen;
-    ((char*)lpvBuffer)[0]='\0';
-    localEnv = ConnInfo->envString;
-    variableNameLen = (int)strlen (lpszVariableName);
-    for (u_long i = 0;;i += (u_long)strlen (&localEnv[i]) + 1)
     {
-      if (((localEnv[i + variableNameLen]) == '=') &&
-         (!strncmp (&localEnv[i], lpszVariableName, variableNameLen)))
-      {
-        strncpy ((char*)lpvBuffer, &localEnv[i + variableNameLen + 1],
-                *lpdwSize);
-        break;
-      }
-      else if ((localEnv[i] == '\0') && (localEnv[i + 1] == '\0'))
-      {
-        break;
-      }
+      /*!
+       * Find in ConnInfo->envString the value lpszVariableName
+       * and copy next string in lpvBuffer.
+       */
+      char *localEnv;
+      int variableNameLen;
+      ((char*)lpvBuffer)[0]='\0';
+      localEnv = ConnInfo->envString;
+      variableNameLen = (int)strlen (lpszVariableName);
+      for (u_long i = 0;;i += (u_long)strlen (&localEnv[i]) + 1)
+        {
+          if (((localEnv[i + variableNameLen]) == '=') &&
+              (!strncmp (&localEnv[i], lpszVariableName, variableNameLen)))
+            {
+              strncpy ((char*)lpvBuffer, &localEnv[i + variableNameLen + 1],
+                       *lpdwSize);
+              break;
+            }
+          else if ((localEnv[i] == '\0') && (localEnv[i + 1] == '\0'))
+            break;
+        }
     }
-  }
-  *lpdwSize =(DWORD)strlen ((char*)lpvBuffer);
+  *lpdwSize = (DWORD) strlen ((char*)lpvBuffer);
   return ret;
 }
 
 /*!
- *Build the string that contains all the HTTP headers.
+ * Build the string that contains all the HTTP headers.
  */
 BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, ConnectionPtr /*!a*/,
-                                LPVOID output, LPDWORD dwMaxLen)
+                                 LPVOID output, LPDWORD dwMaxLen)
 {
   DWORD valLen = 0;
   DWORD maxLen = *dwMaxLen;
@@ -496,38 +494,36 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
 
   if (accept && accept->value->length () && (valLen+30<maxLen))
     valLen += sprintf (&ValStr[valLen],"HTTP_ACCEPT:%s\n",
-                    accept->value->c_str ());
+                       accept->value->c_str ());
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (cache && cache->value->length () && (valLen+30<maxLen))
     valLen += sprintf (&ValStr[valLen], "HTTP_CACHE_CONTROL:%s\n",
-                    cache->value->c_str ());
+                       cache->value->c_str ());
   else if (valLen + 30 < maxLen)
     return 0;
 
   if ((td->request.rangeByteBegin || td->request.rangeByteEnd) &&
-     (valLen + 30 < maxLen))
-  {
-    ostringstream rangeBuffer;
-    rangeBuffer << "HTTP_RANGE:" << td->request.rangeType << "=" ;
-    if (td->request.rangeByteBegin)
+      (valLen + 30 < maxLen))
     {
-      rangeBuffer << (int)td->request.rangeByteBegin;
+      ostringstream rangeBuffer;
+      rangeBuffer << "HTTP_RANGE:" << td->request.rangeType << "=" ;
+      if (td->request.rangeByteBegin)
+        {
+          rangeBuffer << (int)td->request.rangeByteBegin;
+        }
+      rangeBuffer << "-";
+      if (td->request.rangeByteEnd)
+        rangeBuffer << td->request.rangeByteEnd;
+      valLen += sprintf (&ValStr[valLen], "%s\n", rangeBuffer.str ().c_str ());
     }
-    rangeBuffer << "-";
-    if (td->request.rangeByteEnd)
-    {
-      rangeBuffer << td->request.rangeByteEnd;
-     }
-    valLen += sprintf (&ValStr[valLen], "%s\n", rangeBuffer.str ().c_str ());
-  }
 
   {
     HttpRequestHeader::Entry* e = td->request.other.get ("Accept-Encoding");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen], "HTTP_ACCEPT_ENCODING:%s\n",
-                        e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -537,7 +533,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("Accept-Language");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_ACCEPT_LANGUAGE:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -547,7 +543,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("Accept-Charset");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_ACCEPT_CHARSET:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -556,7 +552,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("Pragma");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_PRAGMA:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -565,7 +561,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("Connection");
     if (e && (valLen + 30< maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_CONNECTION:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -574,7 +570,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("Cookie");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_COOKIE:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -583,7 +579,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("Host");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_HOST:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -592,7 +588,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("Date");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_DATE:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -601,7 +597,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("If-Modified-Since");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_IF_MODIFIED_SINCE:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -610,7 +606,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("Referer");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_REFERER:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -619,7 +615,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("User-Agent");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_USER_AGENT:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -628,7 +624,7 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
     HttpRequestHeader::Entry* e = td->request.other.get ("From");
     if (e && (valLen + 30 < maxLen))
       valLen += sprintf (&ValStr[valLen],"HTTP_FROM:%s\n",
-                      e->value->c_str ());
+                         e->value->c_str ());
     else if (valLen + 30 < maxLen)
       return 0;
   }
@@ -637,10 +633,10 @@ BOOL Isapi::buildAllHttpHeaders (HttpThreadContext* td, 
ConnectionPtr /*!a*/,
 }
 
 /*!
- *Build the string that contains all the headers.
+ * Build the string that contains all the headers.
  */
 BOOL Isapi::buildAllRawHeaders (HttpThreadContext* td,ConnectionPtr a,
-                               LPVOID output,LPDWORD dwMaxLen)
+                                LPVOID output,LPDWORD dwMaxLen)
 {
   DWORD valLen = 0;
   DWORD maxLen = *dwMaxLen;
@@ -651,74 +647,74 @@ BOOL Isapi::buildAllRawHeaders (HttpThreadContext* 
td,ConnectionPtr a,
 
   if (td->pathInfo.length () && (valLen + 30 < maxLen))
     valLen += sprintf (&ValStr[valLen], "PATH_INFO:%s\n",
-                      td->pathInfo.c_str ());
+                       td->pathInfo.c_str ());
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (td->pathTranslated.length () && (valLen + 30 < maxLen))
     valLen += sprintf (&ValStr[valLen], "PATH_INFO:%s\n",
-                      td->pathTranslated.c_str ());
+                       td->pathTranslated.c_str ());
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (td->request.uriOpts[0] && (valLen + 30 < maxLen))
     valLen += sprintf (&ValStr[valLen], "QUERY_STRING:%s\n",
-                      td->request.uriOpts[0]);
+                       td->request.uriOpts[0]);
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (td->request.cmd[0] && (valLen + 30 < maxLen))
     valLen += sprintf (&ValStr[valLen], "REQUEST_METHOD:%s\n",
-                      td->request.cmd[0]);
+                       td->request.cmd[0]);
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (td->filenamePath.length () && (valLen + 30 < maxLen))
     valLen += sprintf (&ValStr[valLen], "SCRIPT_FILENAME:%s\n",
-                      td->filenamePath[0]);
+                       td->filenamePath[0]);
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (valLen + 30 < maxLen)
     valLen += sprintf (&ValStr[valLen], "SERVER_PORT:%u\n",
-                      td->connection->getLocalPort ());
+                       td->connection->getLocalPort ());
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (valLen + 30 < maxLen)
     valLen += sprintf (&ValStr[valLen],
-                      "SERVER_SIGNATURE:<address>%s</address>\n",
-                      MYSERVER_VERSION);
+                       "SERVER_SIGNATURE:<address>%s</address>\n",
+                       MYSERVER_VERSION);
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (td->connection->getIpAddr ()[0] && valLen + 30 < maxLen)
     valLen += sprintf (&ValStr[valLen], "REMOTE_ADDR:\n",
-                      td->connection->getIpAddr ());
+                       td->connection->getIpAddr ());
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (td->connection->getPort () && valLen + 30 < maxLen)
     valLen += sprintf (&ValStr[valLen], "REMOTE_PORT:%u\n",
-                      td->connection->getPort ());
+                       td->connection->getPort ());
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (valLen + 30 < maxLen)
     valLen += sprintf (&ValStr[valLen], "SERVER_ADMIN:%s\n",
-                      td->securityToken.getData ("server.admin", 
MYSERVER_VHOST_CONF |
-                                                       MYSERVER_SERVER_CONF, 
""));
+                       td->securityToken.getData ("server.admin", 
MYSERVER_VHOST_CONF |
+                                                  MYSERVER_SERVER_CONF, ""));
   else if (valLen + 30 < maxLen)
     return 0;
 
   if (valLen + MAX_PATH < maxLen)
-  {
-    valLen += sprintf (&ValStr[valLen],"SCRIPT_NAME:");
-    lstrcpyn (&ValStr[valLen], td->request.uri.c_str (),
-             td->request.uri.length ()- td->pathInfo.length () + 1);
-    valLen += (DWORD)td->request.uri.length ()-td->pathInfo.length () + 1;
-    valLen += (DWORD)sprintf (&ValStr[valLen],"\n");
-  }
+    {
+      valLen += sprintf (&ValStr[valLen],"SCRIPT_NAME:");
+      lstrcpyn (&ValStr[valLen], td->request.uri.c_str (),
+                td->request.uri.length ()- td->pathInfo.length () + 1);
+      valLen += (DWORD)td->request.uri.length ()-td->pathInfo.length () + 1;
+      valLen += (DWORD)sprintf (&ValStr[valLen],"\n");
+    }
   else if (valLen + 30 < maxLen)
     return 0;
   return 1;
@@ -731,11 +727,11 @@ BOOL Isapi::buildAllRawHeaders (HttpThreadContext* 
td,ConnectionPtr a,
  */
 int Isapi::send (HttpThreadContext* td,
                 const char* scriptpath, const char *cgipath,
-                bool execute, bool onlyHeader)
+                 bool execute, bool onlyHeader)
 {
-/*!
- *ISAPI works only on the windows architecture.
- */
+  /*!
+   *ISAPI works only on the windows architecture.
+   */
 #ifdef WIN32
   DWORD Ret;
   EXTENSION_CONTROL_BLOCK ExtCtrlBlk;
@@ -750,16 +746,16 @@ int Isapi::send (HttpThreadContext* td,
   char fullpath[MAX_PATH * 2 + 5];
 
   if (!execute)
-  {
-    if (cgipath && strlen (cgipath))
-      sprintf (fullpath, "%s \"%s\"", cgipath, td->filenamePath.c_str ());
-    else
-      sprintf (fullpath, "%s", td->filenamePath.c_str ());
-  }
+    {
+      if (cgipath && strlen (cgipath))
+        sprintf (fullpath, "%s \"%s\"", cgipath, td->filenamePath.c_str ());
+      else
+        sprintf (fullpath, "%s", td->filenamePath.c_str ());
+    }
   else
-  {
-    sprintf (fullpath, "%s", cgipath);
-  }
+    {
+      sprintf (fullpath, "%s", cgipath);
+    }
 
   if (!(td->permissions & MYSERVER_PERMISSION_EXECUTE))
     return td->http->sendAuth ();
@@ -771,17 +767,17 @@ int Isapi::send (HttpThreadContext* td,
   Isapi::isapiMutex->lock ();
   while ((connTable[connIndex].Allocated != 0) &&
          (connIndex < maxConnections))
-  {
-    connIndex++;
-  }
+    {
+      connIndex++;
+    }
   Isapi::isapiMutex->unlock ();
   LeaveCriticalSection (&GetTableEntryCritSec);
 
   if (connIndex == maxConnections)
-  {
-    td->connection->host->warningsLogWrite (_("ISAPI: max connections"));
-    return td->http->raiseHTTPError (503);
-  }
+    {
+      td->connection->host->warningsLogWrite (_("ISAPI: max connections"));
+      return td->http->raiseHTTPError (503);
+    }
   if (execute)
     loadLib = scriptpath;
   else
@@ -799,19 +795,19 @@ int Isapi::send (HttpThreadContext* td,
   connTable[connIndex].chain.setProtocolData (td);
   connTable[connIndex].chain.setStream (td->connection->socket);
   if (td->mime)
-  {
-    u_long nbw;
-    if (td->mime && Server::getInstance ()->getFiltersFactory ()->chain (
-                                          &(connTable[connIndex].chain),
-                                          td->mime->filters,
-                                          td->connection->socket,
-                                          &nbw, 1))
-      {
-        td->connection->host->warningsLogWrite (_("ISAPI: internal error"));
-        connTable[connIndex].chain.clearAllFilters ();
-        return td->http->raiseHTTPError (500);
-      }
-  }
+    {
+      u_long nbw;
+      if (td->mime && Server::getInstance ()->getFiltersFactory ()->chain (
+                                                                           
&(connTable[connIndex].chain),
+                                                                           
td->mime->filters,
+                                                                           
td->connection->socket,
+                                                                           
&nbw, 1))
+        {
+          td->connection->host->warningsLogWrite (_("ISAPI: internal error"));
+          connTable[connIndex].chain.clearAllFilters ();
+          return td->http->raiseHTTPError (500);
+        }
+    }
 
   connTable[connIndex].connection = td->connection;
   connTable[connIndex].td = td;
@@ -826,27 +822,27 @@ int Isapi::send (HttpThreadContext* td,
   GetExtensionVersion = (PFN_GETEXTENSIONVERSION)appHnd.getProc 
("GetExtensionVersion");
 
   if (GetExtensionVersion == NULL)
-  {
-    td->connection->host->warningsLogWrite (_("ISAPI: internal error"));
-    appHnd.close ();
-    connTable[connIndex].chain.clearAllFilters ();
-    return td->http->raiseHTTPError (500);
-  }
+    {
+      td->connection->host->warningsLogWrite (_("ISAPI: internal error"));
+      appHnd.close ();
+      connTable[connIndex].chain.clearAllFilters ();
+      return td->http->raiseHTTPError (500);
+    }
   if (!GetExtensionVersion (&Ver))
-  {
-    td->connection->host->warningsLogWrite (_("ISAPI: internal error"));
-    appHnd.close ();
-    connTable[connIndex].chain.clearAllFilters ();
-    return td->http->raiseHTTPError (500);
-  }
+    {
+      td->connection->host->warningsLogWrite (_("ISAPI: internal error"));
+      appHnd.close ();
+      connTable[connIndex].chain.clearAllFilters ();
+      return td->http->raiseHTTPError (500);
+    }
   if (Ver.dwExtensionVersion > MAKELONG (HSE_VERSION_MINOR,
-                                        HSE_VERSION_MAJOR))
-  {
-    td->connection->host->warningsLogWrite (_("ISAPI: version not supported"));
-    appHnd.close ();
-    connTable[connIndex].chain.clearAllFilters ();
-    return td->http->raiseHTTPError (500);
-  }
+                                         HSE_VERSION_MAJOR))
+    {
+      td->connection->host->warningsLogWrite (_("ISAPI: version not 
supported"));
+      appHnd.close ();
+      connTable[connIndex].chain.clearAllFilters ();
+      return td->http->raiseHTTPError (500);
+    }
   /*!
    *Store the environment string in the secondaryBuffer.
    */
@@ -890,25 +886,23 @@ int Isapi::send (HttpThreadContext* td,
     HttpRequestHeader::Entry *content =
       td->request.other.get ("Content-type");
     ExtCtrlBlk.lpszContentType = content ? (char*)content->value->c_str ()
-                                         : 0;
+      : 0;
   }
 
   connTable[connIndex].td->buffer->setLength (0);
   connTable[connIndex].td->buffer->getAt (0)='\0';
   HttpExtensionProc = (PFN_HTTPEXTENSIONPROC)appHnd.getProc 
("HttpExtensionProc");
   if (HttpExtensionProc == NULL)
-  {
-    td->connection->host->warningsLogWrite (_("ISAPI: internal error"));
-    appHnd.close ();
-    connTable[connIndex].chain.clearAllFilters ();
-    return td->http->raiseHTTPError (500);
-  }
+    {
+      td->connection->host->warningsLogWrite (_("ISAPI: internal error"));
+      appHnd.close ();
+      connTable[connIndex].chain.clearAllFilters ();
+      return td->http->raiseHTTPError (500);
+    }
 
   Ret = HttpExtensionProc (&ExtCtrlBlk);
   if (Ret == HSE_STATUS_PENDING)
-  {
     WaitForSingleObject (connTable[connIndex].ISAPIDoneEvent, 
td->http->getTimeout ());
-  }
 
   {
     u_long nbw = 0;
@@ -919,7 +913,7 @@ int Isapi::send (HttpThreadContext* td,
   }
 
   switch (Ret)
-  {
+    {
     case HSE_STATUS_SUCCESS_AND_KEEP_CONN:
       retvalue = 1;
       break;
@@ -929,7 +923,7 @@ int Isapi::send (HttpThreadContext* td,
     default:
       retvalue = 0;
       break;
-  }
+    }
 
   appHnd.close ();
 
@@ -974,18 +968,18 @@ int Isapi::load ()
 
   connTable = new ConnTableRecord[maxConnections];
   for (int i=0;i<maxConnections; i++)
-  {
-    connTable[i].Allocated=0;
-    connTable[i].onlyHeader=0;
-    connTable[i].headerSent=0;
-    connTable[i].headerSize=0;
-    connTable[i].td=0;
-    connTable[i].dataSent=0;
-    connTable[i].envString=0;
-    connTable[i].connection=0;
-    connTable[i].ISAPIDoneEvent=0;
-    connTable[i].isapi=0;
-  }
+    {
+      connTable[i].Allocated=0;
+      connTable[i].onlyHeader=0;
+      connTable[i].headerSent=0;
+      connTable[i].headerSize=0;
+      connTable[i].td=0;
+      connTable[i].dataSent=0;
+      connTable[i].envString=0;
+      connTable[i].connection=0;
+      connTable[i].ISAPIDoneEvent=0;
+      connTable[i].isapi=0;
+    }
   InitializeCriticalSection (&GetTableEntryCritSec);
   initialized=1;
 #endif
@@ -1002,8 +996,8 @@ int Isapi::unLoad ()
   DeleteCriticalSection (&GetTableEntryCritSec);
   if (connTable)
     delete [] connTable;
-  connTable=0;
-  initialized=0;
+  connTable = 0;
+  initialized = 0;
 #endif
   return 0;
 }
diff --git a/myserver/src/http_handler/mscgi/mscgi.cpp 
b/myserver/src/http_handler/mscgi/mscgi.cpp
index 6ddc726..a3d8995 100644
--- a/myserver/src/http_handler/mscgi/mscgi.cpp
+++ b/myserver/src/http_handler/mscgi/mscgi.cpp
@@ -160,11 +160,8 @@ int MsCgi::write (const char* data, u_long len, MsCgiData* 
mcd)
   if (mcd->error)
     return 1;
 
-  if (!mcd->headerSent)
-    {
-      if (sendHeader (mcd))
-        return 1;
-    }
+  if (!mcd->headerSent && sendHeader (mcd))
+    return 1;
 
   if (mcd->onlyHeader)
     return 0;
@@ -187,22 +184,17 @@ int MsCgi::write (const char* data, u_long len, 
MsCgiData* mcd)
  */
 int MsCgi::sendHeader (MsCgiData* mcd)
 {
+  HttpThreadContext* td = mcd->td;
+
   if (mcd->error)
     return 1;
 
   if (mcd->headerSent)
     return 0;
 
-  if (!mcd->td->appendOutputs)
-    {
-      HttpThreadContext* td = mcd->td;
-      char *buffer = td->secondaryBuffer->getBuffer ();
-      ConnectionPtr s = td->connection;
-
-      u_long hdrLen = HttpHeaders::buildHTTPResponseHeader (buffer, 
&(td->response));
-      if (td->connection->socket->send (buffer, hdrLen, 0) == SOCKET_ERROR)
-        return 1;
-    }
+  if (HttpHeaders::sendHeader (td->response, *td->connection->socket,
+                               *td->secondaryBuffer, td))
+    return 1;
 
   mcd->headerSent = true;
   return 0;
diff --git a/myserver/src/http_handler/proxy/proxy.cpp 
b/myserver/src/http_handler/proxy/proxy.cpp
index e8c8cd4..2276e67 100644
--- a/myserver/src/http_handler/proxy/proxy.cpp
+++ b/myserver/src/http_handler/proxy/proxy.cpp
@@ -88,19 +88,17 @@ int Proxy::send (HttpThreadContext *td,
   xForwardedFor.append (td->connection->getIpAddr ());
   req.setValue ("X-Forwarded-For", xForwardedFor.c_str ());
 
-       u_long hdrLen = HttpHeaders::buildHTTPRequestHeader 
(td->secondaryBuffer->getBuffer (),
-                                                       &req);
-
   if (sock.connect (destUrl.getHost ().c_str (), destUrl.getPort ()))
     return td->http->raiseHTTPError (500);
 
-  if (sock.write (td->secondaryBuffer->getBuffer (), hdrLen, &nbw))
+  if (HttpHeaders::sendHeader (td->response, *td->connection->socket,
+                               *td->secondaryBuffer, td))
     {
-      sock.close ();
-      return td->http->raiseHTTPError (500);
+      chain.clearAllFilters ();
+      return 1;
     }
 
- if (td->request.uriOptsPtr &&
+  if (td->request.uriOptsPtr &&
       td->inputData.fastCopyToSocket (&sock, 0, td->secondaryBuffer, &nbw))
     {
       sock.close ();
@@ -112,10 +110,10 @@ int Proxy::send (HttpThreadContext *td,
   chain.setStream (td->connection->socket);
 
   if (td->mime && Server::getInstance ()->getFiltersFactory ()->chain (&chain,
-                                                                   
td->mime->filters,
-                                                                   
td->connection->socket,
-                                                                   &nbw,
-                                                                   1))
+                                                            td->mime->filters,
+                                                       td->connection->socket,
+                                                                       &nbw,
+                                                                       1))
     {
       sock.close ();
       return td->http->raiseHTTPError (500);
diff --git a/myserver/src/http_handler/scgi/scgi.cpp 
b/myserver/src/http_handler/scgi/scgi.cpp
index 838214d..6b85719 100644
--- a/myserver/src/http_handler/scgi/scgi.cpp
+++ b/myserver/src/http_handler/scgi/scgi.cpp
@@ -216,7 +216,7 @@ int Scgi::send (HttpThreadContext* td, const char* 
scriptpath,
 
 
 /*!
- *Send the response to the client.
+ * Send the response to the client.
  */
 int Scgi::sendResponse (ScgiContext* ctx, int onlyHeader, FiltersChain* chain)
 {
@@ -233,94 +233,90 @@ int Scgi::sendResponse (ScgiContext* ctx, int onlyHeader, 
FiltersChain* chain)
   checkDataChunks (td, &keepalive, &useChunks);
 
   for (;;)
-  {
-    while (!ctx->sock.bytesToRead ())
     {
-      if ((clock_t)(getTicks () - initialTicks) > td->http->getTimeout ())
-        break;
-      Thread::wait (1);
-    }
-
-    if (!ctx->sock.bytesToRead ())
-      return -1;
-
-    nbr = ctx->sock.recv (td->secondaryBuffer->getBuffer () + read,
-                        td->secondaryBuffer->getRealLength () - read,
-                         td->http->getTimeout ());
-
-    read += nbr;
-
+      while (!ctx->sock.bytesToRead ())
+        {
+          if ((clock_t)(getTicks () - initialTicks) > td->http->getTimeout ())
+            break;
+          Thread::wait (1);
+        }
+
+      if (!ctx->sock.bytesToRead ())
+        return -1;
 
-    for (tmpHeaderSize = (tmpHeaderSize > 3)
-          ? tmpHeaderSize - 4 : tmpHeaderSize;
-        tmpHeaderSize < read - 4; tmpHeaderSize++)
-      if ((td->secondaryBuffer->getBuffer ()[tmpHeaderSize] == '\r') &&
-         (td->secondaryBuffer->getBuffer ()[tmpHeaderSize + 1] == '\n') &&
-         (td->secondaryBuffer->getBuffer ()[tmpHeaderSize + 2] == '\r') &&
-         (td->secondaryBuffer->getBuffer ()[tmpHeaderSize + 3] == '\n'))
-      {
-        headerSize = tmpHeaderSize + 4;
+      nbr = ctx->sock.recv (td->secondaryBuffer->getBuffer () + read,
+                            td->secondaryBuffer->getRealLength () - read,
+                            td->http->getTimeout ());
+
+      read += nbr;
+
+      for (tmpHeaderSize = (tmpHeaderSize > 3)
+             ? tmpHeaderSize - 4 : tmpHeaderSize;
+           tmpHeaderSize < read - 4; tmpHeaderSize++)
+        if ((td->secondaryBuffer->getBuffer ()[tmpHeaderSize] == '\r')
+            && (td->secondaryBuffer->getBuffer ()[tmpHeaderSize + 1] == '\n')
+            && (td->secondaryBuffer->getBuffer ()[tmpHeaderSize + 2] == '\r')
+            && (td->secondaryBuffer->getBuffer ()[tmpHeaderSize + 3] == '\n'))
+          {
+            headerSize = tmpHeaderSize + 4;
+            break;
+          }
+
+      if (headerSize)
         break;
-      }
-    if (headerSize)
-      break;
-  }
+    }
 
+  if (headerSize)
+    HttpHeaders::buildHTTPResponseHeaderStruct (td->secondaryBuffer->getBuffer 
(),
+                                                &td->response,
+                                                &(td->nBytesToRead));
 
-  if (!td->appendOutputs)
-  {
-    if (headerSize)
-    {
-      HttpHeaders::buildHTTPResponseHeaderStruct 
(td->secondaryBuffer->getBuffer (),
-                                                 &td->response,
-                                                 &(td->nBytesToRead));
-    }
-    u_long hdrLen = HttpHeaders::buildHTTPResponseHeader 
(td->buffer->getBuffer (),
-                                                         &td->response);
+  if (HttpHeaders::sendHeader (td->response, *td->connection->socket,
+                               *td->secondaryBuffer, td))
+    return 1;
 
-    if (chain->write (td->buffer->getBuffer (), hdrLen, &nbw))
-      return -1;
-  }
+  if (onlyHeader)
+    return 0;
 
   if (read - headerSize)
     if (appendDataToHTTPChannel (td, td->secondaryBuffer->getBuffer () + 
headerSize,
-                               read - headerSize,
-                               &(td->outputData),
-                               chain,
-                               td->appendOutputs,
-                               useChunks))
-     return -1;
-
-  sentData += read - headerSize;
-
-  if (td->response.getStatusType () == HttpResponseHeader::SUCCESSFUL)
-  {
-    for (;;)
-    {
-      nbr = ctx->sock.recv (td->secondaryBuffer->getBuffer (),
-                           td->secondaryBuffer->getRealLength (),
-                           0);
-
-      if (!nbr || (nbr == (u_long)-1))
-        break;
-
-      if (appendDataToHTTPChannel (td, td->secondaryBuffer->getBuffer (),
-                                 nbr,
+                                 read - headerSize,
                                  &(td->outputData),
                                  chain,
                                  td->appendOutputs,
                                  useChunks))
-        return -1;
+      return -1;
 
-      sentData += nbr;
-    }
+  sentData += read - headerSize;
 
-    if (!td->appendOutputs && useChunks)
+  if (td->response.getStatusType () == HttpResponseHeader::SUCCESSFUL)
     {
-      if (chain->getStream ()->write ("0\r\n\r\n", 5, &nbw))
-        return -1;
+      for (;;)
+        {
+          nbr = ctx->sock.recv (td->secondaryBuffer->getBuffer (),
+                                td->secondaryBuffer->getRealLength (),
+                                0);
+
+          if (!nbr || (nbr == (u_long)-1))
+            break;
+
+          if (appendDataToHTTPChannel (td, td->secondaryBuffer->getBuffer (),
+                                       nbr,
+                                       &(td->outputData),
+                                       chain,
+                                       td->appendOutputs,
+                                       useChunks))
+            return -1;
+
+          sentData += nbr;
+        }
+
+      if (!td->appendOutputs && useChunks)
+        {
+          if (chain->getStream ()->write ("0\r\n\r\n", 5, &nbw))
+            return -1;
+        }
     }
-  }
   /* For logging activity.  */
   td->sentData += sentData;
 
@@ -348,27 +344,23 @@ int Scgi::sendNetString (ScgiContext* ctx, const char* 
data, int len)
 }
 
 /*!
- *Send the post data to the SCGI server.
+ * Send the post data to the SCGI server.
  */
 int Scgi::sendPostData (ScgiContext* ctx)
 {
-    u_long nbr;
-    do
+  u_long nbr;
+  do
     {
       if (ctx->td->inputData.read (ctx->td->secondaryBuffer->getBuffer (),
-                                         
ctx->td->secondaryBuffer->getRealLength (),
-                                         &nbr))
-      {
+                                   ctx->td->secondaryBuffer->getRealLength (),
+                                   &nbr))
         return -1;
-      }
 
       if (nbr && (ctx->sock.send (ctx->td->secondaryBuffer->getBuffer (), nbr, 
0) == -1))
-      {
         return -1;
-      }
     }
-    while (nbr);
-    return 0;
+  while (nbr);
+  return 0;
 }
 
 /*!
@@ -376,7 +368,7 @@ int Scgi::sendPostData (ScgiContext* ctx)
  *string.
  */
 int Scgi::buildScgiEnvironmentString (HttpThreadContext* td, char* src,
-                                     char* dest)
+                                      char* dest)
 {
   char *ptr = dest;
   char *sptr = src;
@@ -388,7 +380,7 @@ int Scgi::buildScgiEnvironmentString (HttpThreadContext* 
td, char* src,
 
   if ( td->request.contentLength.size ())
     ptr += myserver_strlcpy (ptr, td->request.contentLength.c_str (),
-                            td->request.contentLength.size () + 1);
+                             td->request.contentLength.size () + 1);
   else
     *ptr++ = '0';
 
@@ -401,52 +393,52 @@ int Scgi::buildScgiEnvironmentString (HttpThreadContext* 
td, char* src,
   *ptr++ = '\0';
 
   for (;;)
-  {
-    int i;
-    int max = 100;
-    int varNameLen;
-    int varValueLen;
-
-    varNameLen = varValueLen = 0;
-    varName[0] = '\0';
-    varValue[0] = '\0';
-
-    while (*sptr == '\0')
-      sptr++;
-
-    while ((--max) && *sptr != '=')
     {
-      varName[varNameLen++] = *sptr++;
-      varName[varNameLen] = '\0';
-    }
-    if (max == 0)
-      return -1;
-    sptr++;
-    max = 2500;
-    while ((--max) && *sptr != '\0')
-     {
-      varValue[varValueLen++] = *sptr++;
-      varValue[varValueLen] = '\0';
-    }
-
-    if (max == 0)
-      return -1;
+      int i;
+      int max = 100;
+      int varNameLen;
+      int varValueLen;
+
+      varNameLen = varValueLen = 0;
+      varName[0] = '\0';
+      varValue[0] = '\0';
+
+      while (*sptr == '\0')
+        sptr++;
+
+      while ((--max) && *sptr != '=')
+        {
+          varName[varNameLen++] = *sptr++;
+          varName[varNameLen] = '\0';
+        }
+      if (max == 0)
+        return -1;
+      sptr++;
+      max = 2500;
+      while ((--max) && *sptr != '\0')
+        {
+          varValue[varValueLen++] = *sptr++;
+          varValue[varValueLen] = '\0';
+        }
+
+      if (max == 0)
+        return -1;
 
-    if (!strcmpi (varName, "CONTENT_LENGTH") || !strcmpi (varName, "SCGI") ||
-       !varNameLen || !varValueLen)
-      continue;
+      if (!strcmpi (varName, "CONTENT_LENGTH") || !strcmpi (varName, "SCGI") ||
+          !varNameLen || !varValueLen)
+        continue;
 
-    for (i = 0; i < varNameLen; i++)
-      *ptr++ = varName[i];
-    *ptr++ = '\0';
+      for (i = 0; i < varNameLen; i++)
+        *ptr++ = varName[i];
+      *ptr++ = '\0';
 
-    for (i = 0; i < varValueLen; i++)
-      *ptr++ = varValue[i];
-    *ptr++ = '\0';
+      for (i = 0; i < varValueLen; i++)
+        *ptr++ = varValue[i];
+      *ptr++ = '\0';
 
-    if (*(++sptr) == '\0')
-      break;
-  }
+      if (*(++sptr) == '\0')
+        break;
+    }
   return static_cast<int>(ptr - dest);
 }
 
@@ -500,12 +492,12 @@ ScgiServer* Scgi::connect (ScgiContext* con, const char* 
path)
    *If we find a valid server try the connection to it.
    */
   if (server)
-  {
-    int ret = processServerManager->connect (&(con->sock), server);
+    {
+      int ret = processServerManager->connect (&(con->sock), server);
 
-    if (ret == -1)
-      return 0;
-  }
+      if (ret == -1)
+        return 0;
+    }
   return server;
 }
 
@@ -515,7 +507,7 @@ ScgiServer* Scgi::connect (ScgiContext* con, const char* 
path)
  *remote server.
  */
 ScgiServer* Scgi::runScgiServer (ScgiContext* context,
-                                const char* path)
+                                 const char* path)
 {
   /* This method needs a better home (and maybe better code).
    * Compute a simple hash from the IP address.  */
@@ -525,28 +517,28 @@ ScgiServer* Scgi::runScgiServer (ScgiContext* context,
     seed = *c * 21 + seed;
 
   ScgiServer* server =  processServerManager->getServer (SERVERS_DOMAIN,
-                                                        path,
-                                                        seed);
+                                                         path,
+                                                         seed);
   if (server)
     return server;
 
   /* If the path starts with @ then the server is remote.  */
   if (path[0] == '@')
-  {
-    int i = 1;
-    char host[128];
-    char port[6];
+    {
+      int i = 1;
+      char host[128];
+      char port[6];
 
-    while (path[i] && path[i] != ':')
-      i++;
+      while (path[i] && path[i] != ':')
+        i++;
 
-    myserver_strlcpy (host, &path[1], min (128, i));
+      myserver_strlcpy (host, &path[1], min (128, i));
 
-    myserver_strlcpy (port, &path[i + 1], 6);
+      myserver_strlcpy (port, &path[i + 1], 6);
 
-    return processServerManager->addRemoteServer (SERVERS_DOMAIN, path,
-                                                 host, atoi (port));
-  }
+      return processServerManager->addRemoteServer (SERVERS_DOMAIN, path,
+                                                    host, atoi (port));
+    }
 
   return processServerManager->runAndAddServer (SERVERS_DOMAIN, path);
 }
diff --git a/myserver/src/http_handler/wincgi/wincgi.cpp 
b/myserver/src/http_handler/wincgi/wincgi.cpp
index f19b397..ca29171 100644
--- a/myserver/src/http_handler/wincgi/wincgi.cpp
+++ b/myserver/src/http_handler/wincgi/wincgi.cpp
@@ -345,21 +345,14 @@ int WinCgi::send (HttpThreadContext* td, const char* 
scriptpath,
   td->response.contentLength.assign (stream.str ());
   if (!td->appendOutputs)
     {
-      u_long nbw2;
-      /*!
-       *Send the header if it is not appending.
-       */
-      u_long hdrLen = HttpHeaders::buildHTTPResponseHeader 
(td->buffer->getBuffer (),
-                                                            &td->response);
-      if (chain.getStream ()->write ((const char*)td->buffer->getBuffer (),
-                                    hdrLen,
-                                    &nbw2))
+      if (HttpHeaders::sendHeader (td->response, *chain.getStream (),
+                                   *td->buffer, td))
         {
           OutFileHandle.close ();
           FilesUtility::deleteFile (outFilePath);
           FilesUtility::deleteFile (dataFilePath);
           chain.clearAllFilters ();
-          return 0;
+          return 1;
         }
 
       if (onlyHeader)
@@ -370,8 +363,9 @@ int WinCgi::send (HttpThreadContext* td, const char* 
scriptpath,
           chain.clearAllFilters ();
           return 1;
         }
+
       /*!
-       *Send other data in the buffer.
+       * Send other data in the buffer.
        */
       chain.write ((char*)(buffer + headerSize), nBytesRead - headerSize,
                    &nbw2);
diff --git a/myserver/src/protocol/http/http_headers.cpp 
b/myserver/src/protocol/http/http_headers.cpp
index b8173fc..3105ff1 100644
--- a/myserver/src/protocol/http/http_headers.cpp
+++ b/myserver/src/protocol/http/http_headers.cpp
@@ -870,6 +870,32 @@ int HttpHeaders::readReqAuthLine (HttpRequestHeader 
*request,
 }
 
 /*!
+ * Flush a HTTP response to the specified stream.
+ *
+ * \param response Response object to flush.
+ * \param stream Destination stream.
+ * \param memBuf Temporary mem buffer to use, it is used internally.
+ * \param ctx Http Context object, it can be NULL.
+ *
+ * \return 0 on success.
+ */
+int HttpHeaders::sendHeader (HttpResponseHeader &response, Stream &stream,
+                             MemBuf &memBuf, HttpThreadContext *ctx)
+{
+  int ret = 0;
+  if (ctx == NULL || !ctx->appendOutputs)
+    {
+      u_long nbw;
+      buildHTTPResponseHeader (memBuf.getBuffer (), &response);
+      ret = stream.write (memBuf.getBuffer (), strlen (memBuf.getBuffer ()),
+                          &nbw);
+    }
+
+  return ret;
+}
+
+
+/*!
  *Build the HTTP RESPONSE HEADER string.
  *If no input is specified the input is the main buffer of the
  *HttpThreadContext structure.

-----------------------------------------------------------------------

Summary of changes:
 myserver/include/connection/connection.h           |  123 ++--
 myserver/include/filter/stream.h                   |   30 +-
 myserver/include/protocol/http/http.h              |   86 ++--
 myserver/include/protocol/http/http_headers.h      |   67 ++-
 .../include/protocol/http/http_thread_context.h    |   93 ++--
 myserver/src/http_handler/cgi/cgi.cpp              |   13 +-
 myserver/src/http_handler/fastcgi/fastcgi.cpp      |   16 +-
 myserver/src/http_handler/http_dir/http_dir.cpp    |   31 +-
 myserver/src/http_handler/http_file/http_file.cpp  |   22 +-
 myserver/src/http_handler/isapi/isapi.cpp          |  608 ++++++++++----------
 myserver/src/http_handler/mscgi/mscgi.cpp          |   22 +-
 myserver/src/http_handler/proxy/proxy.cpp          |   20 +-
 myserver/src/http_handler/scgi/scgi.cpp            |  282 +++++-----
 myserver/src/http_handler/wincgi/wincgi.cpp        |   16 +-
 myserver/src/protocol/http/http.cpp                |   27 +-
 myserver/src/protocol/http/http_headers.cpp        |   29 +
 16 files changed, 729 insertions(+), 756 deletions(-)


hooks/post-receive
-- 
GNU MyServer




reply via email to

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