myserver-commit
[Top][All Lists]
Advanced

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

[myserver-commit] [3094] Added basic Proxy features.


From: Giuseppe Scrivano
Subject: [myserver-commit] [3094] Added basic Proxy features.
Date: Sat, 23 May 2009 21:12:13 +0000

Revision: 3094
          http://svn.sv.gnu.org/viewvc/?view=rev&root=myserver&revision=3094
Author:   gscrivano
Date:     2009-05-23 21:12:13 +0000 (Sat, 23 May 2009)
Log Message:
-----------
Added basic Proxy features.

Modified Paths:
--------------
    trunk/myserver/configure.in
    trunk/myserver/documentation/mime_types.texi
    trunk/myserver/include/http_handler/Makefile.am
    trunk/myserver/include/protocol/http/http.h
    trunk/myserver/src/http_handler/Makefile.am
    trunk/myserver/src/protocol/http/http.cpp

Added Paths:
-----------
    trunk/myserver/include/http_handler/proxy/
    trunk/myserver/include/http_handler/proxy/Makefile.am
    trunk/myserver/include/http_handler/proxy/proxy.h
    trunk/myserver/src/http_handler/proxy/
    trunk/myserver/src/http_handler/proxy/Makefile.am
    trunk/myserver/src/http_handler/proxy/proxy.cpp

Modified: trunk/myserver/configure.in
===================================================================
--- trunk/myserver/configure.in 2009-05-22 22:00:24 UTC (rev 3093)
+++ trunk/myserver/configure.in 2009-05-23 21:12:13 UTC (rev 3094)
@@ -350,6 +350,7 @@
     include/http_handler/mscgi/Makefile
     include/http_handler/fastcgi/Makefile
     include/http_handler/http_file/Makefile
+    include/http_handler/proxy/Makefile
     include/http_handler/scgi/Makefile
     include/http_handler/isapi/Makefile
     include/http_handler/http_dir/Makefile
@@ -402,6 +403,7 @@
     src/http_handler/fastcgi/Makefile
     src/http_handler/http_file/Makefile
     src/http_handler/scgi/Makefile
+    src/http_handler/proxy/Makefile
     src/http_handler/isapi/Makefile
     src/http_handler/http_dir/Makefile
     src/base/Makefile

Modified: trunk/myserver/documentation/mime_types.texi
===================================================================
--- trunk/myserver/documentation/mime_types.texi        2009-05-22 22:00:24 UTC 
(rev 3093)
+++ trunk/myserver/documentation/mime_types.texi        2009-05-23 21:12:13 UTC 
(rev 3094)
@@ -12,13 +12,13 @@
 
 @example
 <MIME mime="text/html" handler="SEND" param="">
-<EXTENSION value="html"/>
-<FILTER value="gzip"/>
+      <EXTENSION value="html"/>
+      <FILTER value="gzip"/>
 </MIME>
 
 <MIME mime="text/html" handler="CGI" param="/usr/bin/perl" selfExecuted="NO">
-<EXTENSION value="pl"/>
-<DEFINE name="http.error.file.404" value="404.html"/>
+      <EXTENSION value="pl"/>
+      <DEFINE name="http.error.file.404" value="404.html"/>
 </MIME>
 @end example
 
@@ -107,6 +107,10 @@
 This protocol is obsoleted by ISAPI.  As CGI it needs a new process
 for every request and it uses files to communicate with the web
 server.  It is very inefficient.
address@hidden PROXY
+The HTTP request is forwarded to the HTTP server specified in the
address@hidden  This is not a real proxy, there is not caching and
+message is forwarded as it is without any change.
 @end enumerate
 
 It is possible to match the file name against a regular expression

Modified: trunk/myserver/include/http_handler/Makefile.am
===================================================================
--- trunk/myserver/include/http_handler/Makefile.am     2009-05-22 22:00:24 UTC 
(rev 3093)
+++ trunk/myserver/include/http_handler/Makefile.am     2009-05-23 21:12:13 UTC 
(rev 3094)
@@ -1,4 +1,4 @@
 http_handlerincludedir=$(includedir)/myserver/include/http_handler
 http_handlerinclude_HEADERS =
-SUBDIRS = cgi fastcgi http_dir http_file isapi mscgi scgi wincgi
+SUBDIRS = cgi fastcgi http_dir http_file isapi mscgi proxy scgi wincgi
 

Added: trunk/myserver/include/http_handler/proxy/Makefile.am
===================================================================
--- trunk/myserver/include/http_handler/proxy/Makefile.am                       
        (rev 0)
+++ trunk/myserver/include/http_handler/proxy/Makefile.am       2009-05-23 
21:12:13 UTC (rev 3094)
@@ -0,0 +1,3 @@
+proxyincludedir=$(includedir)/myserver/include/http_handler/proxy
+proxyinclude_HEADERS = proxy.h
+SUBDIRS =

Added: trunk/myserver/include/http_handler/proxy/proxy.h
===================================================================
--- trunk/myserver/include/http_handler/proxy/proxy.h                           
(rev 0)
+++ trunk/myserver/include/http_handler/proxy/proxy.h   2009-05-23 21:12:13 UTC 
(rev 3094)
@@ -0,0 +1,43 @@
+/* -*- mode: c++ -*- */
+/*
+MyServer
+Copyright (C) 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 PROXY_H
+#define PROXY_H
+
+#include <include/protocol/http/http_response.h>
+#include <include/protocol/http/http_request.h>
+#include <include/protocol/http/http_headers.h>
+#include <include/protocol/http/http_data_handler.h>
+
+class FiltersChain;
+
+class Proxy : public HttpDataHandler
+{
+public:
+  static void setTimeout (int);
+  static int getTimeout ();
+
+       virtual int send (HttpThreadContext*, ConnectionPtr s,
+                   const char* scriptpath, const char* exec = 0,
+                   int execute = 0, int onlyHeader = 0);
+protected:
+  int flushToClient (HttpThreadContext* td, Socket& client,
+                     FiltersChain &out, int onlyHeader);
+  static int timeout;
+};
+#endif

Modified: trunk/myserver/include/protocol/http/http.h
===================================================================
--- trunk/myserver/include/protocol/http/http.h 2009-05-22 22:00:24 UTC (rev 
3093)
+++ trunk/myserver/include/protocol/http/http.h 2009-05-23 21:12:13 UTC (rev 
3094)
@@ -67,6 +67,7 @@
   HttpDataHandler *scgi;
   HttpDataHandler *fastcgi;
   HttpDataHandler *httpFile;
+  HttpDataHandler *proxy;
   HttpDataHandler *httpDir;
 
 

Modified: trunk/myserver/src/http_handler/Makefile.am
===================================================================
--- trunk/myserver/src/http_handler/Makefile.am 2009-05-22 22:00:24 UTC (rev 
3093)
+++ trunk/myserver/src/http_handler/Makefile.am 2009-05-23 21:12:13 UTC (rev 
3094)
@@ -1,5 +1,5 @@
 lib_LIBRARIES = libhttp_handler.a
 libhttp_handler_a_SOURCES =
-SUBDIRS = cgi fastcgi http_dir http_file isapi mscgi scgi wincgi
+SUBDIRS = cgi fastcgi http_dir http_file isapi mscgi proxy scgi wincgi
 INCLUDES = $(all_includes)
 

Added: trunk/myserver/src/http_handler/proxy/Makefile.am
===================================================================
--- trunk/myserver/src/http_handler/proxy/Makefile.am                           
(rev 0)
+++ trunk/myserver/src/http_handler/proxy/Makefile.am   2009-05-23 21:12:13 UTC 
(rev 3094)
@@ -0,0 +1,5 @@
+lib_LIBRARIES = libproxy.a
+libproxy_a_SOURCES = proxy.cpp
+SUBDIRS =
+INCLUDES = $(all_includes)
+

Added: trunk/myserver/src/http_handler/proxy/proxy.cpp
===================================================================
--- trunk/myserver/src/http_handler/proxy/proxy.cpp                             
(rev 0)
+++ trunk/myserver/src/http_handler/proxy/proxy.cpp     2009-05-23 21:12:13 UTC 
(rev 3094)
@@ -0,0 +1,235 @@
+/*
+MyServer
+Copyright (C) 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/>.
+*/
+
+#include "stdafx.h"
+#include <include/http_handler/proxy/proxy.h>
+
+#include <include/protocol/http/http_thread_context.h>
+#include <include/protocol/http/http.h>
+#include <include/protocol/url.h>
+#include <include/base/socket/socket.h>
+#include <include/protocol/http/http_request.h>
+#include <include/protocol/http/http_data_handler.h>
+#include <include/protocol/http/http_headers.h>
+#include <include/filter/filters_chain.h>
+
+#include <sstream>
+
+int Proxy::timeout = MYSERVER_SEC (10);
+
+/*
+ *Forward the HTTP requesto to another server.
+ *
+ *\param td The HTTP thread context.
+ *\param s A pointer to the connection structure.
+ *\param scriptpath Not used.
+ *\param exec The remote server Url.
+ *\param execute Not used.
+ *\param onlyHeader Specify if send only the HTTP header.
+ */
+int Proxy::send (HttpThreadContext *td, ConnectionPtr s,
+                 const char* scriptpath,
+                 const char* exec,
+                 int execute,
+                 int onlyHeader)
+{
+  Url destUrl (exec, 80);
+  Socket sock;
+  FiltersChain chain;
+  HttpRequestHeader req;
+  u_long nbw;
+
+  for (HashMap<string, HttpRequestHeader::Entry*>::Iterator it = 
td->request.begin ();
+       it != td->request.end ();
+       it++)
+    {
+      HttpRequestHeader::Entry *e = *it;
+      req.setValue (e->name->c_str (), e->value->c_str ());
+    }
+
+  if (destUrl.getProtocol ().compare ("HTTP"))
+    {
+      ostringstream msg;
+      msg << "Proxy: " << destUrl.getProtocol () << " is not known.";
+      td->connection->host->warningsLogWrite (msg.str ().c_str ());
+      return 0;
+    }
+
+  /*Use HTTP/1.0 until we accept chunks from clients.  */
+  req.ver.assign ("HTTP/1.0");
+  req.cmd.assign ("GET");
+  req.uri.assign ("/");
+  req.uri.append (destUrl.getResource ());
+  req.uri.append (td->pathInfo);
+  req.setValue ("Connection", "Close");
+
+  ostringstream host;
+  host << destUrl.getHost ();
+  if (destUrl.getPort () != 80 )
+    host << ":" << destUrl.getPort ();
+
+  req.setValue ("Host", host.str ().c_str ());
+
+       HttpHeaders::buildHTTPRequestHeader (td->secondaryBuffer->getBuffer (),
+                                       &req);
+
+  if (sock.connect (destUrl.getHost ().c_str (), destUrl.getPort ()))
+    return td->http->raiseHTTPError (500);
+
+  if (td->request.uriOptsPtr &&
+      td->inputData.fastCopyToSocket (&sock, 0, td->secondaryBuffer, &nbw))
+    {
+      sock.close ();
+      return td->http->raiseHTTPError (500);
+    }
+
+  if (sock.write (td->secondaryBuffer->getBuffer (),
+                  strlen (td->secondaryBuffer->getBuffer ()), &nbw))
+    {
+      sock.close ();
+      return td->http->raiseHTTPError (500);
+    }
+
+  chain.setProtocol(td->http);
+  chain.setProtocolData(td);
+  chain.setStream(td->connection->socket);
+
+  if(td->mime && Server::getInstance()->getFiltersFactory()->chain(&chain,
+                                                                   
td->mime->filters,
+                                                                   
td->connection->socket,
+                                                                   &nbw,
+                                                                   1))
+    {
+      sock.close ();
+      return td->http->raiseHTTPError (500);
+    }
+
+  int ret = flushToClient (td, sock, chain, onlyHeader);
+
+
+  chain.clearAllFilters();
+  sock.close ();
+
+  return ret;
+}
+
+/*!
+ *Flush the server reply to the client.
+ */
+int Proxy::flushToClient (HttpThreadContext* td, Socket& client,
+                          FiltersChain &out, int onlyHeader)
+{
+  u_long read = 0;
+  u_long headerLength;
+  int ret;
+  u_long nbw;
+  bool useChunks = false;
+  bool keepalive = false;
+
+  do
+    {
+      ret = client.recv (td->secondaryBuffer->getBuffer () + read,
+                         td->secondaryBuffer->getRealLength () - read,
+                         0,
+                         timeout);
+
+      if (ret == 0)
+        return td->http->raiseHTTPError (500);
+
+      read += ret;
+
+      ret = HttpHeaders::buildHTTPResponseHeaderStruct 
(td->secondaryBuffer->getBuffer (),
+                                                        &td->response,
+                                                        &headerLength);
+    }
+  while (ret == -1);
+
+  checkDataChunks (td, &keepalive, &useChunks);
+
+
+  HttpHeaders::buildHTTPResponseHeader (td->buffer->getBuffer (),
+                                        &td->response);
+
+  if (out.getStream ()->write (td->buffer->getBuffer (),
+                               strlen (td->buffer->getBuffer ()),
+                               &nbw))
+    return 0;
+
+  if (onlyHeader)
+    return keepalive;
+
+  if (read - headerLength)
+    {
+      if (HttpDataHandler::appendDataToHTTPChannel(td,
+                                                   
td->secondaryBuffer->getBuffer() +
+                                                     headerLength,
+                                                   read - headerLength,
+                                                   &(td->outputData),
+                                                   &out,
+                                                   td->appendOutputs,
+                                                   useChunks))
+        return 0;
+
+      td->sentData += read - headerLength;
+    }
+
+  while (ret = client.recv (td->secondaryBuffer->getBuffer (),
+                            td->secondaryBuffer->getRealLength (),
+                            0,
+                            timeout))
+    {
+
+      if (ret == -1)
+        break;
+
+      if (HttpDataHandler::appendDataToHTTPChannel(td,
+                                                   
td->secondaryBuffer->getBuffer(),
+                                                   ret,
+                                                   &(td->outputData),
+                                                   &out,
+                                                   td->appendOutputs,
+                                                   useChunks))
+        return 0;
+
+      td->sentData += ret;
+    }
+
+
+  if(useChunks && out.write ("0\r\n\r\n", 5, &nbw))
+    {
+      return 0;
+    }
+
+  return keepalive;
+}
+
+/*!
+ *Set the CGI timeout for the new processes.
+ *\param nt The new timeout value.
+ */
+void Proxy::setTimeout(int nt)
+{
+   timeout = nt;
+}
+
+/*!
+ *Get the timeout value for HTTP requests.
+ */
+int Proxy::getTimeout()
+{
+  return timeout;
+}

Modified: trunk/myserver/src/protocol/http/http.cpp
===================================================================
--- trunk/myserver/src/protocol/http/http.cpp   2009-05-22 22:00:24 UTC (rev 
3093)
+++ trunk/myserver/src/protocol/http/http.cpp   2009-05-23 21:12:13 UTC (rev 
3094)
@@ -39,6 +39,7 @@
 #include <include/http_handler/mscgi/mscgi.h>
 #include <include/http_handler/isapi/isapi.h>
 #include <include/http_handler/http_file/http_file.h>
+#include <include/http_handler/proxy/proxy.h>
 #include <include/http_handler/http_dir/http_dir.h>
 #include <include/protocol/http/http_data_read.h>
 
@@ -82,10 +83,12 @@
   fastcgi = new FastCgi();
   httpFile = new HttpFile();
   httpDir = new HttpDir();
+  proxy = new Proxy ();
 }
 
 HttpStaticData::~HttpStaticData ()
 {
+  delete proxy;
   delete mscgi;
   delete wincgi;
   delete isapi;
@@ -1887,6 +1890,7 @@
   staticHttp.dynManagerList.addHttpManager ("WINCGI", staticHttp.wincgi);
   staticHttp.dynManagerList.addHttpManager ("FASTCGI", staticHttp.fastcgi);
   staticHttp.dynManagerList.addHttpManager ("ISAPI", staticHttp.isapi);
+  staticHttp.dynManagerList.addHttpManager ("PROXY", staticHttp.proxy);
 
   data = Server::getInstance ()->getHashedData ("vhost.allow_mime");
   if(data)
@@ -1907,6 +1911,7 @@
   Scgi::setTimeout (staticHttp.cgiTimeout);
   WinCgi::setTimeout (staticHttp.cgiTimeout);
   Isapi::setTimeout (staticHttp.cgiTimeout);
+  Proxy::setTimeout (staticHttp.cgiTimeout);
 
   return 1;
 }





reply via email to

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