Dependencies:   mbed

Dependents:   TCP

Committer:
slowness
Date:
Tue Sep 06 18:05:46 2011 +0000
Revision:
0:58c3d014a4e7

        

Who changed what in which revision?

UserRevisionLine numberNew contents of line
slowness 0:58c3d014a4e7 1
slowness 0:58c3d014a4e7 2 /*
slowness 0:58c3d014a4e7 3 Copyright (c) 2010 Donatien Garnier (donatiengar [at] gmail [dot] com)
slowness 0:58c3d014a4e7 4
slowness 0:58c3d014a4e7 5 Permission is hereby granted, free of charge, to any person obtaining a copy
slowness 0:58c3d014a4e7 6 of this software and associated documentation files (the "Software"), to deal
slowness 0:58c3d014a4e7 7 in the Software without restriction, including without limitation the rights
slowness 0:58c3d014a4e7 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
slowness 0:58c3d014a4e7 9 copies of the Software, and to permit persons to whom the Software is
slowness 0:58c3d014a4e7 10 furnished to do so, subject to the following conditions:
slowness 0:58c3d014a4e7 11
slowness 0:58c3d014a4e7 12 The above copyright notice and this permission notice shall be included in
slowness 0:58c3d014a4e7 13 all copies or substantial portions of the Software.
slowness 0:58c3d014a4e7 14
slowness 0:58c3d014a4e7 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
slowness 0:58c3d014a4e7 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
slowness 0:58c3d014a4e7 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
slowness 0:58c3d014a4e7 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
slowness 0:58c3d014a4e7 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
slowness 0:58c3d014a4e7 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
slowness 0:58c3d014a4e7 21 THE SOFTWARE.
slowness 0:58c3d014a4e7 22 */
slowness 0:58c3d014a4e7 23
slowness 0:58c3d014a4e7 24 /** \file
slowness 0:58c3d014a4e7 25 HTTP Server header file
slowness 0:58c3d014a4e7 26 */
slowness 0:58c3d014a4e7 27
slowness 0:58c3d014a4e7 28 #ifndef HTTP_SERVER_H
slowness 0:58c3d014a4e7 29 #define HTTP_SERVER_H
slowness 0:58c3d014a4e7 30
slowness 0:58c3d014a4e7 31 class HTTPRequestHandler;
slowness 0:58c3d014a4e7 32 class HTTPRequestDispatcher;
slowness 0:58c3d014a4e7 33
slowness 0:58c3d014a4e7 34 #include "core/net.h"
slowness 0:58c3d014a4e7 35 #include "HTTPRequestHandler.h"
slowness 0:58c3d014a4e7 36 #include "HTTPRequestDispatcher.h"
slowness 0:58c3d014a4e7 37
slowness 0:58c3d014a4e7 38 #include <string>
slowness 0:58c3d014a4e7 39 using std::string;
slowness 0:58c3d014a4e7 40
slowness 0:58c3d014a4e7 41 #include <map>
slowness 0:58c3d014a4e7 42 using std::map;
slowness 0:58c3d014a4e7 43
slowness 0:58c3d014a4e7 44 ///A simple HTTP server implementation
slowness 0:58c3d014a4e7 45 /**
slowness 0:58c3d014a4e7 46 The HTTPServer is composed of:
slowness 0:58c3d014a4e7 47 - The actual server (HTTPServer)
slowness 0:58c3d014a4e7 48 - A request dispatcher, instanciated on each request (HTTPRequestDispatcher)
slowness 0:58c3d014a4e7 49 - Request handlers instanciated by the dispatcher(deriving from HTTPRequestHandler)
slowness 0:58c3d014a4e7 50 */
slowness 0:58c3d014a4e7 51 class HTTPServer
slowness 0:58c3d014a4e7 52 {
slowness 0:58c3d014a4e7 53 public:
slowness 0:58c3d014a4e7 54 ///Instantiates the HTTP Server
slowness 0:58c3d014a4e7 55 HTTPServer();
slowness 0:58c3d014a4e7 56 ~HTTPServer();
slowness 0:58c3d014a4e7 57
slowness 0:58c3d014a4e7 58 struct handlersComp //Used to order handlers in the right way
slowness 0:58c3d014a4e7 59 {
slowness 0:58c3d014a4e7 60 bool operator() (const string& handler1, const string& handler2) const
slowness 0:58c3d014a4e7 61 {
slowness 0:58c3d014a4e7 62 //The first handler is longer than the second one
slowness 0:58c3d014a4e7 63 if (handler1.length() > handler2.length())
slowness 0:58c3d014a4e7 64 return true; //Returns true if handler1 is to appear before handler2
slowness 0:58c3d014a4e7 65 else if (handler1.length() < handler2.length())
slowness 0:58c3d014a4e7 66 return false;
slowness 0:58c3d014a4e7 67 else //To avoid the == case, sort now by address
slowness 0:58c3d014a4e7 68 return ((&handler1)>(&handler2));
slowness 0:58c3d014a4e7 69 }
slowness 0:58c3d014a4e7 70 };
slowness 0:58c3d014a4e7 71
slowness 0:58c3d014a4e7 72 ///Adds a handler
slowness 0:58c3d014a4e7 73 /**
slowness 0:58c3d014a4e7 74 Appends a handler to the handlers list
slowness 0:58c3d014a4e7 75 @param T : class which will be instanciated to serve these requests
slowness 0:58c3d014a4e7 76 @param path : requests starting with this path will be served using this handler
slowness 0:58c3d014a4e7 77 */
slowness 0:58c3d014a4e7 78 template<typename T>
slowness 0:58c3d014a4e7 79 void addHandler(const char* path) //Template decl in header
slowness 0:58c3d014a4e7 80 { m_lpHandlers[path] = &T::inst; }
slowness 0:58c3d014a4e7 81
slowness 0:58c3d014a4e7 82 ///Starts listening
slowness 0:58c3d014a4e7 83 /**
slowness 0:58c3d014a4e7 84 Binds server to a specific port and starts listening
slowness 0:58c3d014a4e7 85 @param port : port on which to listen for incoming connections
slowness 0:58c3d014a4e7 86 */
slowness 0:58c3d014a4e7 87 void bind(int port = 80);
slowness 0:58c3d014a4e7 88
slowness 0:58c3d014a4e7 89 private:
slowness 0:58c3d014a4e7 90 friend class HTTPRequestDispatcher;
slowness 0:58c3d014a4e7 91
slowness 0:58c3d014a4e7 92 void onTCPSocketEvent(TCPSocketEvent e);
slowness 0:58c3d014a4e7 93
slowness 0:58c3d014a4e7 94 TCPSocket* m_pTCPSocket;
slowness 0:58c3d014a4e7 95 map< string, HTTPRequestHandler*(*)(const char*, const char*, TCPSocket*), handlersComp > m_lpHandlers;
slowness 0:58c3d014a4e7 96
slowness 0:58c3d014a4e7 97 };
slowness 0:58c3d014a4e7 98
slowness 0:58c3d014a4e7 99 //Including handlers here for more convenience
slowness 0:58c3d014a4e7 100 #include "impl/RPCHandler.h"
slowness 0:58c3d014a4e7 101 #include "impl/FSHandler.h"
slowness 0:58c3d014a4e7 102 #include "impl/SimpleHandler.h"
slowness 0:58c3d014a4e7 103
slowness 0:58c3d014a4e7 104 #endif