Experimental HTTPClient with proxy support

Committer:
igorsk
Date:
Wed Jun 29 16:01:58 2011 +0000
Revision:
0:b56b6a05cad4

        

Who changed what in which revision?

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