This programme Sets a web server using light weigh ip (using Donatien Garnier's server code) which reads a voltage and saves it value in an htm file to be displayed on local page. Server 2 2nd edition Two issues here 1 ) the compiler throws a warning about assignment in RPCHandler.cpp and 2) the local .htm file created on the mbed flash memory is always date stamped with the default date 01/01/2008 11:00

Dependencies:   EthernetNetIf NTPClient_NetServices mbed

Committer:
pmr1
Date:
Sun Aug 08 22:00:39 2010 +0000
Revision:
0:03e1db2fe866

        

Who changed what in which revision?

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