Michiel Berckvens / Mbed 2 deprecated ProjectHTTP

Dependencies:   DS1307 TextLCD mbed

Committer:
Michielber
Date:
Thu Dec 04 10:36:40 2014 +0000
Revision:
0:f615d151a72c
Berckvens Michiel & Basteyns Jonas 4/12/2014

Who changed what in which revision?

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