petfeeder st ver

Fork of httpServer_ntpsetting by justin kim

Committer:
justinkim
Date:
Fri Jun 09 04:44:18 2017 +0000
Revision:
7:2a38bd4be00f
Parent:
2:dd293a10a772
petfeeder st ver

Who changed what in which revision?

UserRevisionLine numberNew contents of line
hjjeon 0:e59cc54df17c 1 /* HTTPServer.cpp */
hjjeon 0:e59cc54df17c 2 /*
hjjeon 0:e59cc54df17c 3 Copyright (c) 2013 Henry Leinen (henry[dot]leinen [at] online [dot] de)
hjjeon 0:e59cc54df17c 4
hjjeon 0:e59cc54df17c 5 Permission is hereby granted, free of charge, to any person obtaining a copy
hjjeon 0:e59cc54df17c 6 of this software and associated documentation files (the "Software"), to deal
hjjeon 0:e59cc54df17c 7 in the Software without restriction, including without limitation the rights
hjjeon 0:e59cc54df17c 8 to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
hjjeon 0:e59cc54df17c 9 copies of the Software, and to permit persons to whom the Software is
hjjeon 0:e59cc54df17c 10 furnished to do so, subject to the following conditions:
hjjeon 0:e59cc54df17c 11
hjjeon 0:e59cc54df17c 12 The above copyright notice and this permission notice shall be included in
hjjeon 0:e59cc54df17c 13 all copies or substantial portions of the Software.
hjjeon 0:e59cc54df17c 14
hjjeon 0:e59cc54df17c 15 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
hjjeon 0:e59cc54df17c 16 IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
hjjeon 0:e59cc54df17c 17 FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
hjjeon 0:e59cc54df17c 18 AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
hjjeon 0:e59cc54df17c 19 LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
hjjeon 0:e59cc54df17c 20 OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
hjjeon 0:e59cc54df17c 21 THE SOFTWARE.
hjjeon 0:e59cc54df17c 22 */
hjjeon 0:e59cc54df17c 23 #ifndef __HTTPSERVER_H__
hjjeon 0:e59cc54df17c 24 #define __HTTPSERVER_H__
hjjeon 0:e59cc54df17c 25 #include "mbed.h"
hjjeon 0:e59cc54df17c 26 #include "EthernetInterface.h"
hjjeon 0:e59cc54df17c 27 #include "HTTPConnection.h"
hjjeon 0:e59cc54df17c 28 #include "HTTPRequestHandler.h"
hjjeon 0:e59cc54df17c 29
hjjeon 0:e59cc54df17c 30 #include <map>
hjjeon 0:e59cc54df17c 31 using std::map;
hjjeon 0:e59cc54df17c 32 #include <string>
hjjeon 0:e59cc54df17c 33 using std::string;
hjjeon 0:e59cc54df17c 34
hjjeon 0:e59cc54df17c 35 #include <TCPSocketConnection.h>
hjjeon 0:e59cc54df17c 36 #include <TCPSocketServer.h>
hjjeon 0:e59cc54df17c 37
hjjeon 0:e59cc54df17c 38 /** Typedefinition for a handler function
hjjeon 0:e59cc54df17c 39 */
hjjeon 0:e59cc54df17c 40 typedef void (*HTTPRequestHandlerFunction)(HTTPConnection::HTTPMessage&, TCPSocketConnection&);
hjjeon 0:e59cc54df17c 41
hjjeon 0:e59cc54df17c 42
hjjeon 0:e59cc54df17c 43 /** Class HTTPServer for use with <a href="http://mbed.org/users/samux/code/WiflyInterface/">WiflyInterface</a>.
hjjeon 0:e59cc54df17c 44 * This class is a simple implementation of an HTTP Server for use with the WiFly interface.
hjjeon 0:e59cc54df17c 45 * The class will listen for incoming connections on the (configurable) HTTP port. For each
hjjeon 0:e59cc54df17c 46 * incoming connection, one request will be processed.
hjjeon 0:e59cc54df17c 47 * After instantiating this class, add all required handlers using the \c addHandler function,
hjjeon 0:e59cc54df17c 48 * replace the default error handler using \c addErrorHandler if needed and call the \c start
hjjeon 0:e59cc54df17c 49 * method to initialize the class.
hjjeon 0:e59cc54df17c 50 * You need to continuously poll for any new incoming connections after one request has been
hjjeon 0:e59cc54df17c 51 * served using the \c poll member function.
hjjeon 0:e59cc54df17c 52 *
hjjeon 0:e59cc54df17c 53 * \b Example:
hjjeon 0:e59cc54df17c 54 * @code
hjjeon 0:e59cc54df17c 55 * #include "mbed.h"
hjjeon 0:e59cc54df17c 56 * #include "HTTPServer.h"
hjjeon 0:e59cc54df17c 57 * #include "LocalFileSystem.h"
hjjeon 0:e59cc54df17c 58 *
hjjeon 0:e59cc54df17c 59 * LocalFileSystem local("local");
hjjeon 0:e59cc54df17c 60 *
hjjeon 0:e59cc54df17c 61 * void main(void)
hjjeon 0:e59cc54df17c 62 * {
hjjeon 0:e59cc54df17c 63 * HTTPServer svr;
hjjeon 0:e59cc54df17c 64 * svr.mount("/local/", "/");
hjjeon 0:e59cc54df17c 65 * svr.addHandler<HTTPFsRequestHandler>( "/" );
hjjeon 0:e59cc54df17c 66 * svr.start();
hjjeon 0:e59cc54df17c 67 * while(1)
hjjeon 0:e59cc54df17c 68 * {
hjjeon 0:e59cc54df17c 69 * if (svr.poll() < 0)
hjjeon 0:e59cc54df17c 70 * exit(0);
hjjeon 0:e59cc54df17c 71 * }
hjjeon 0:e59cc54df17c 72 * }
hjjeon 0:e59cc54df17c 73 * @endcode
hjjeon 0:e59cc54df17c 74 *
hjjeon 0:e59cc54df17c 75 * An alternate approach e.g. if you need to perform additional tasks using the EthernetInterface
hjjeon 0:e59cc54df17c 76 * there is the possibility to provide the EthernetInterface object in an initialized and connected
hjjeon 0:e59cc54df17c 77 * state. __NOTE: You should choose this scenario for compatibility reasons.___
hjjeon 0:e59cc54df17c 78 *
hjjeon 0:e59cc54df17c 79 * \b Example2:
hjjeon 0:e59cc54df17c 80 * @code
hjjeon 0:e59cc54df17c 81 * #include "mbed.h"
hjjeon 0:e59cc54df17c 82 * #include "HTTPServer.h"
hjjeon 0:e59cc54df17c 83 * #include "EthernetInterface.h"
hjjeon 0:e59cc54df17c 84 * #include "LocalFileSystem.h"
hjjeon 0:e59cc54df17c 85 *
hjjeon 0:e59cc54df17c 86 * LocalFileSystem local("local");
hjjeon 0:e59cc54df17c 87 * EthernetInterface eth;
hjjeon 0:e59cc54df17c 88 *
hjjeon 0:e59cc54df17c 89 * void main(void)
hjjeon 0:e59cc54df17c 90 * {
hjjeon 0:e59cc54df17c 91 * HTTPServer svr;
hjjeon 0:e59cc54df17c 92 * // Initialize the ethernet interface
hjjeon 0:e59cc54df17c 93 * if (eth.init() != 0) {
hjjeon 0:e59cc54df17c 94 * printf("Initialization of EthernetInterface failed !");
hjjeon 0:e59cc54df17c 95 * exit(0);
hjjeon 0:e59cc54df17c 96 * }
hjjeon 0:e59cc54df17c 97 * // Connect using DHCP
hjjeon 0:e59cc54df17c 98 * if (eth.connect() !=0) {
hjjeon 0:e59cc54df17c 99 * printf("Failed to connect using DHCP !");
hjjeon 0:e59cc54df17c 100 * exit(0);
hjjeon 0:e59cc54df17c 101 * }
hjjeon 0:e59cc54df17c 102 *
hjjeon 0:e59cc54df17c 103 * // Moint the local file system and provide a handler for 'root'.
hjjeon 0:e59cc54df17c 104 * svr.mount("/local/", "/");
hjjeon 0:e59cc54df17c 105 * svr.addHandler<HTTPFsRequestHandler>( "/" );
hjjeon 0:e59cc54df17c 106 * // Start the server on port 80, providing our own ethernet interface object.
hjjeon 0:e59cc54df17c 107 * svr.start(80, &eth);
hjjeon 0:e59cc54df17c 108 * while(1)
hjjeon 0:e59cc54df17c 109 * {
hjjeon 0:e59cc54df17c 110 * if (svr.poll() < 0)
hjjeon 0:e59cc54df17c 111 * exit(0);
hjjeon 0:e59cc54df17c 112 * }
hjjeon 0:e59cc54df17c 113 * }
hjjeon 0:e59cc54df17c 114 * @endcode
hjjeon 0:e59cc54df17c 115 *
hjjeon 0:e59cc54df17c 116 */
hjjeon 0:e59cc54df17c 117 class HTTPServer
hjjeon 0:e59cc54df17c 118 {
hjjeon 0:e59cc54df17c 119 TCPSocketServer m_Svr;
hjjeon 0:e59cc54df17c 120 bool m_bServerListening;
hjjeon 0:e59cc54df17c 121 EthernetInterface* m_pEthernet;
hjjeon 0:e59cc54df17c 122
hjjeon 0:e59cc54df17c 123 public:
hjjeon 0:e59cc54df17c 124 /** Constructor for HTTPServer objects.
hjjeon 0:e59cc54df17c 125 */
hjjeon 0:e59cc54df17c 126 HTTPServer();
hjjeon 0:e59cc54df17c 127
hjjeon 0:e59cc54df17c 128 /** Destructor for HTTPServer objects.
hjjeon 0:e59cc54df17c 129 */
hjjeon 0:e59cc54df17c 130 ~HTTPServer();
hjjeon 0:e59cc54df17c 131
hjjeon 0:e59cc54df17c 132 /**
hjjeon 0:e59cc54df17c 133 * Structure which will allow to order the stored handlers according to their associated path.
hjjeon 0:e59cc54df17c 134 */
hjjeon 0:e59cc54df17c 135 struct handlersComp //Used to order handlers in the right way
hjjeon 0:e59cc54df17c 136 {
hjjeon 0:e59cc54df17c 137 bool operator() (const string& handler1, const string& handler2) const
hjjeon 0:e59cc54df17c 138 {
hjjeon 0:e59cc54df17c 139 //The first handler is longer than the second one
hjjeon 0:e59cc54df17c 140 if (handler1.length() > handler2.length())
hjjeon 0:e59cc54df17c 141 return true; //Returns true if handler1 is to appear before handler2
hjjeon 0:e59cc54df17c 142 else if (handler1.length() < handler2.length())
hjjeon 0:e59cc54df17c 143 return false;
hjjeon 0:e59cc54df17c 144 else //To avoid the == case, sort now by address
hjjeon 0:e59cc54df17c 145 return ((&handler1)>(&handler2));
hjjeon 0:e59cc54df17c 146 }
hjjeon 0:e59cc54df17c 147 };
hjjeon 0:e59cc54df17c 148
hjjeon 0:e59cc54df17c 149 /**
hjjeon 0:e59cc54df17c 150 * Adds a request handler to the handlers list. You will have to use one of the existing implementations.
hjjeon 0:e59cc54df17c 151 * With each handler a \c uri or \c path is associated. Whenever a request is received the server will
hjjeon 0:e59cc54df17c 152 * walk through all registered handlers and check which \c path is matching.
hjjeon 0:e59cc54df17c 153 * @param T : class which will be instanciated to serve these requests for the associated \b path.
hjjeon 0:e59cc54df17c 154 * @param path : request uri starting with this \c path will be served using this handler.
hjjeon 0:e59cc54df17c 155 */
hjjeon 0:e59cc54df17c 156 template<typename T>
hjjeon 0:e59cc54df17c 157 void addHandler(const char* path)
hjjeon 0:e59cc54df17c 158 { m_lpHandlers[path] = &T::create; }
hjjeon 0:e59cc54df17c 159
hjjeon 0:e59cc54df17c 160 /**
hjjeon 0:e59cc54df17c 161 * Replaces the standard error Handler. The error Handler will be called everytime a request is not
hjjeon 0:e59cc54df17c 162 * matching any of the registered \c paths or \c uris.
hjjeon 0:e59cc54df17c 163 * @param hdlFunc: User specified handler function which will be used in error conditions.
hjjeon 0:e59cc54df17c 164 */
hjjeon 0:e59cc54df17c 165 void addErrorHandler(HTTPRequestHandlerFunction hdlFunc)
hjjeon 0:e59cc54df17c 166 { m_pErrorHandler = hdlFunc!=NULL ?hdlFunc : StdErrorHandler; }
hjjeon 0:e59cc54df17c 167
hjjeon 0:e59cc54df17c 168 /** Binds server to a specific port and starts listening. This member prepares the internal variables and the server socket
hjjeon 0:e59cc54df17c 169 * and terminates after successfull initialization
hjjeon 0:e59cc54df17c 170 * @param port : port on which to listen for incoming connections
hjjeon 0:e59cc54df17c 171 * @param pEthernet : a pointer to an existing EthernetInterface object or NULL if the HTTPServer shall allocate the object. _Please note that for compatibility reasons
hjjeon 0:e59cc54df17c 172 * your should consider to create the EthernetInterface as a static variable. Otherwise the the object will be created on the heap._
hjjeon 0:e59cc54df17c 173 * @returns : false if an unrecoverable error occured or if the ethernet interface was not set or not initialized correctly, or true if everything was ok.
hjjeon 0:e59cc54df17c 174 */
hjjeon 0:e59cc54df17c 175 bool start(int port = 80, EthernetInterface* pEthernet = NULL);
hjjeon 0:e59cc54df17c 176
hjjeon 0:e59cc54df17c 177 /** Performs the regular polling of the server component. Needs to be called cyclically.
hjjeon 0:e59cc54df17c 178 * The function will internally check whether new connections are requested by a client and will also poll all existing client connections.
hjjeon 0:e59cc54df17c 179 * @param blocking : if true,
hjjeon 0:e59cc54df17c 180 * @returns -1 if there was a problem. If 0 is returned, the latest request was served successfully and the server is
hjjeon 0:e59cc54df17c 181 * ready for processing the next request. Simply call \c poll as long as you want to serve new incoming requests.
hjjeon 0:e59cc54df17c 182 */
hjjeon 0:e59cc54df17c 183 int poll(bool blocking = true);
hjjeon 0:e59cc54df17c 184
hjjeon 0:e59cc54df17c 185 private:
hjjeon 0:e59cc54df17c 186
hjjeon 0:e59cc54df17c 187 /** The standard error handler function.
hjjeon 0:e59cc54df17c 188 * @param msg : Request message data.
hjjeon 0:e59cc54df17c 189 * @param tcp : Socket to be used for responding.
hjjeon 0:e59cc54df17c 190 */
hjjeon 0:e59cc54df17c 191 static void StdErrorHandler(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp);
hjjeon 0:e59cc54df17c 192
hjjeon 0:e59cc54df17c 193 /** Internal function which processes a request and which will try to find the matching handler function
hjjeon 0:e59cc54df17c 194 * for the given request. Please note that the function will search through the list of handlers, iterating
hjjeon 0:e59cc54df17c 195 * from longest to shortest \c paths. If the registered \c path is a subset of the request the associated
hjjeon 0:e59cc54df17c 196 * handler is considered as being a match.
hjjeon 0:e59cc54df17c 197 * @param msg : Request message data. Contains the requested logical \c uri.
hjjeon 0:e59cc54df17c 198 * @param tcp : Socket to be used for communication with the client.
hjjeon 0:e59cc54df17c 199 */
hjjeon 0:e59cc54df17c 200 void HandleRequest(HTTPConnection::HTTPMessage& msg, TCPSocketConnection& tcp);
hjjeon 0:e59cc54df17c 201
hjjeon 0:e59cc54df17c 202 /** Map of handler objects. Can be any object derived from \ref HTTPRequestHeader. Use the \ref addHandler function
hjjeon 0:e59cc54df17c 203 * to register new handler objects.
hjjeon 0:e59cc54df17c 204 */
hjjeon 0:e59cc54df17c 205 map<string, HTTPRequestHandler* (*)(const char*, const char*, HTTPConnection::HTTPMessage&, TCPSocketConnection&), handlersComp> m_lpHandlers;
hjjeon 0:e59cc54df17c 206 HTTPRequestHandlerFunction m_pErrorHandler;
hjjeon 0:e59cc54df17c 207
hjjeon 0:e59cc54df17c 208 };
hjjeon 0:e59cc54df17c 209
justinkim 2:dd293a10a772 210 #endif //__HTTPSERVER_H__