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