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