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