Single instance HTTP server using new Ethernet Interface

Dependents:   websocketandnode

Fork of HTTPServer by Donatien Garnier

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPServer.h Source File

HTTPServer.h

Go to the documentation of this file.
00001 
00002 /*
00003 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
00004  
00005 Permission is hereby granted, free of charge, to any person obtaining a copy
00006 of this software and associated documentation files (the "Software"), to deal
00007 in the Software without restriction, including without limitation the rights
00008 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
00009 copies of the Software, and to permit persons to whom the Software is
00010 furnished to do so, subject to the following conditions:
00011  
00012 The above copyright notice and this permission notice shall be included in
00013 all copies or substantial portions of the Software.
00014  
00015 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
00016 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
00017 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
00018 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
00019 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
00020 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
00021 THE SOFTWARE.
00022 */
00023 
00024 /** \file
00025 HTTP Server header file
00026 */
00027 
00028 #ifndef HTTP_SERVER_H
00029 #define HTTP_SERVER_H
00030 
00031 class HTTPRequestHandler;
00032 class HTTPRequestDispatcher;
00033 
00034 #include "core/net.h"
00035 #include "HTTPRequestHandler.h"
00036 #include "HTTPRequestDispatcher.h"
00037 
00038 #include <string>
00039 using std::string;
00040 
00041 #include <map>
00042 using std::map;
00043 
00044 ///A simple HTTP server implementation
00045 /**
00046 The HTTPServer is composed of:
00047 - The actual server (HTTPServer)
00048 - A request dispatcher, instanciated on each request (HTTPRequestDispatcher)
00049 - Request handlers instanciated by the dispatcher(deriving from HTTPRequestHandler) 
00050 */
00051 class HTTPServer
00052 {
00053 public:
00054   ///Instantiates the HTTP Server
00055   HTTPServer();
00056   ~HTTPServer();
00057   
00058   struct handlersComp //Used to order handlers in the right way
00059   {
00060     bool operator() (const string& handler1, const string& handler2) const
00061     {
00062       //The first handler is longer than the second one
00063       if (handler1.length() > handler2.length())
00064         return true; //Returns true if handler1 is to appear before handler2
00065       else if (handler1.length() < handler2.length())
00066         return false;
00067       else //To avoid the == case, sort now by address
00068         return ((&handler1)>(&handler2));
00069     }
00070   };
00071 
00072   ///Adds a handler
00073   /**
00074   Appends a handler to the handlers list
00075   @param T : class which will be instanciated to serve these requests
00076   @param path : requests starting with this path will be served using this handler
00077   */
00078   template<typename T>
00079   void addHandler(const char* path) //Template decl in header
00080   { m_lpHandlers[path] = &T::inst; }
00081   
00082   ///Starts listening
00083   /**
00084   Binds server to a specific port and starts listening
00085   @param port : port on which to listen for incoming connections
00086   */
00087   void bind(int port = 80);
00088   
00089 private:
00090   friend class HTTPRequestDispatcher;
00091 
00092   void onTCPSocketEvent(TCPSocketEvent e);
00093   
00094   TCPSocket* m_pTCPSocket;
00095   map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*), handlersComp > m_lpHandlers;
00096 
00097 };
00098 
00099 //Including handlers here for more convenience
00100 #include "impl/RPCHandler.h"
00101 #include "impl/FSHandler.h"
00102 #include "impl/SimpleHandler.h"
00103 
00104 #endif