Port of LwIP performed by Ralf in 2010. Not recommended for use with recent mbed libraries, but good demos of raw LwIP possible
Dependents: LwIP_raw_API_serverExample tiny-dtls
HTTPServer/HTTPServer.h@0:0791c1fece8e, 2012-09-18 (annotated)
- Committer:
- RodColeman
- Date:
- Tue Sep 18 14:41:24 2012 +0000
- Revision:
- 0:0791c1fece8e
[mbed] converted /Eth_TCP_Wei_Server/lwip
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
RodColeman | 0:0791c1fece8e | 1 | #ifndef HTTPSERVER_H |
RodColeman | 0:0791c1fece8e | 2 | #define HTTPSERVER_H |
RodColeman | 0:0791c1fece8e | 3 | |
RodColeman | 0:0791c1fece8e | 4 | #include "TCPConnection.h" |
RodColeman | 0:0791c1fece8e | 5 | #include "TCPListener.h" |
RodColeman | 0:0791c1fece8e | 6 | #include "NetServer.h" |
RodColeman | 0:0791c1fece8e | 7 | |
RodColeman | 0:0791c1fece8e | 8 | #include <map> |
RodColeman | 0:0791c1fece8e | 9 | #include <set> |
RodColeman | 0:0791c1fece8e | 10 | #include <list> |
RodColeman | 0:0791c1fece8e | 11 | |
RodColeman | 0:0791c1fece8e | 12 | #define HTTP_MAX_EMPTYPOLLS 100 |
RodColeman | 0:0791c1fece8e | 13 | #define GET 4 |
RodColeman | 0:0791c1fece8e | 14 | #define POST 5 |
RodColeman | 0:0791c1fece8e | 15 | |
RodColeman | 0:0791c1fece8e | 16 | //extern unsigned int gconnections; |
RodColeman | 0:0791c1fece8e | 17 | |
RodColeman | 0:0791c1fece8e | 18 | using namespace std; |
RodColeman | 0:0791c1fece8e | 19 | |
RodColeman | 0:0791c1fece8e | 20 | namespace mbed { |
RodColeman | 0:0791c1fece8e | 21 | |
RodColeman | 0:0791c1fece8e | 22 | class HTTPServer; |
RodColeman | 0:0791c1fece8e | 23 | class HTTPHandler; |
RodColeman | 0:0791c1fece8e | 24 | class HTTPConnection; |
RodColeman | 0:0791c1fece8e | 25 | |
RodColeman | 0:0791c1fece8e | 26 | /** |
RodColeman | 0:0791c1fece8e | 27 | * A simple HASH function to reduce the size of stored Header Fields |
RodColeman | 0:0791c1fece8e | 28 | * TODO: Make the Hash case insensetive. |
RodColeman | 0:0791c1fece8e | 29 | */ |
RodColeman | 0:0791c1fece8e | 30 | unsigned int hash(unsigned char *str); |
RodColeman | 0:0791c1fece8e | 31 | |
RodColeman | 0:0791c1fece8e | 32 | /** |
RodColeman | 0:0791c1fece8e | 33 | * The Status of an HTTP Request |
RodColeman | 0:0791c1fece8e | 34 | * Nedded for HTTPHandler subclasses to define there reults in the HTTPHandler:init Method. |
RodColeman | 0:0791c1fece8e | 35 | */ |
RodColeman | 0:0791c1fece8e | 36 | enum HTTPStatus { |
RodColeman | 0:0791c1fece8e | 37 | HTTP_OK = 200, |
RodColeman | 0:0791c1fece8e | 38 | HTTP_BadRequest = 400, |
RodColeman | 0:0791c1fece8e | 39 | HTTP_Unauthorized = 401, |
RodColeman | 0:0791c1fece8e | 40 | HTTP_Forbidden = 403, |
RodColeman | 0:0791c1fece8e | 41 | HTTP_NotFound = 404, |
RodColeman | 0:0791c1fece8e | 42 | HTTP_MethodNotAllowed = 405, |
RodColeman | 0:0791c1fece8e | 43 | HTTP_InternalServerError = 500, |
RodColeman | 0:0791c1fece8e | 44 | HTTP_NotImplemented = 501, |
RodColeman | 0:0791c1fece8e | 45 | }; |
RodColeman | 0:0791c1fece8e | 46 | |
RodColeman | 0:0791c1fece8e | 47 | /** |
RodColeman | 0:0791c1fece8e | 48 | * The result of a chunk of data used for the HTTPHandler Methodes data and send |
RodColeman | 0:0791c1fece8e | 49 | */ |
RodColeman | 0:0791c1fece8e | 50 | enum HTTPHandle { |
RodColeman | 0:0791c1fece8e | 51 | /** Execution Succeeded but more data expected. */ |
RodColeman | 0:0791c1fece8e | 52 | HTTP_Success = 600, |
RodColeman | 0:0791c1fece8e | 53 | |
RodColeman | 0:0791c1fece8e | 54 | /** Running out of memory waiting for memory */ |
RodColeman | 0:0791c1fece8e | 55 | HTTP_SenderMemory = 601, |
RodColeman | 0:0791c1fece8e | 56 | |
RodColeman | 0:0791c1fece8e | 57 | |
RodColeman | 0:0791c1fece8e | 58 | /** Execution Succeeded and no more data expected. */ |
RodColeman | 0:0791c1fece8e | 59 | HTTP_SuccessEnded = 700, |
RodColeman | 0:0791c1fece8e | 60 | |
RodColeman | 0:0791c1fece8e | 61 | /** Execution failed. Close conection*/ |
RodColeman | 0:0791c1fece8e | 62 | HTTP_Failed = 701, |
RodColeman | 0:0791c1fece8e | 63 | |
RodColeman | 0:0791c1fece8e | 64 | /** This module will deliver the data. */ |
RodColeman | 0:0791c1fece8e | 65 | HTTP_Deliver = 800, |
RodColeman | 0:0791c1fece8e | 66 | |
RodColeman | 0:0791c1fece8e | 67 | /** This module has add header fields to the request. */ |
RodColeman | 0:0791c1fece8e | 68 | HTTP_AddFields = 801, |
RodColeman | 0:0791c1fece8e | 69 | }; |
RodColeman | 0:0791c1fece8e | 70 | |
RodColeman | 0:0791c1fece8e | 71 | /** |
RodColeman | 0:0791c1fece8e | 72 | * A parent object for a data storage container for all HTTPHandler objects. |
RodColeman | 0:0791c1fece8e | 73 | */ |
RodColeman | 0:0791c1fece8e | 74 | class HTTPData { |
RodColeman | 0:0791c1fece8e | 75 | public: |
RodColeman | 0:0791c1fece8e | 76 | HTTPData() {} |
RodColeman | 0:0791c1fece8e | 77 | virtual ~HTTPData() {} |
RodColeman | 0:0791c1fece8e | 78 | }; |
RodColeman | 0:0791c1fece8e | 79 | |
RodColeman | 0:0791c1fece8e | 80 | /** |
RodColeman | 0:0791c1fece8e | 81 | * A HTTPHandler will serve the requested data if there is an object of a |
RodColeman | 0:0791c1fece8e | 82 | * child class from HTTPHandler which is registert to an matching prefix. |
RodColeman | 0:0791c1fece8e | 83 | * To see how to implement your own HTTPHandler classes have a look at |
RodColeman | 0:0791c1fece8e | 84 | * HTTPRPC HTTPStaticPage and HTTPFileSystemHandler. |
RodColeman | 0:0791c1fece8e | 85 | */ |
RodColeman | 0:0791c1fece8e | 86 | class HTTPHandler { |
RodColeman | 0:0791c1fece8e | 87 | public: |
RodColeman | 0:0791c1fece8e | 88 | HTTPHandler(const char *prefix) : _prefix(prefix) {}; |
RodColeman | 0:0791c1fece8e | 89 | virtual ~HTTPHandler() { |
RodColeman | 0:0791c1fece8e | 90 | delete _prefix; |
RodColeman | 0:0791c1fece8e | 91 | }; |
RodColeman | 0:0791c1fece8e | 92 | |
RodColeman | 0:0791c1fece8e | 93 | protected: |
RodColeman | 0:0791c1fece8e | 94 | /** |
RodColeman | 0:0791c1fece8e | 95 | * Register needed header fields by the HTTPServer. |
RodColeman | 0:0791c1fece8e | 96 | * Because of memory size the server will throw away all request header fields which are not registert. |
RodColeman | 0:0791c1fece8e | 97 | * Register the fields you need in your implementation of this method. |
RodColeman | 0:0791c1fece8e | 98 | */ |
RodColeman | 0:0791c1fece8e | 99 | virtual void reg(HTTPServer *) {}; |
RodColeman | 0:0791c1fece8e | 100 | |
RodColeman | 0:0791c1fece8e | 101 | /** |
RodColeman | 0:0791c1fece8e | 102 | * This Method returns if you will deliver the requested page or not. |
RodColeman | 0:0791c1fece8e | 103 | * It will only executed if the prefix is matched by the URL. |
RodColeman | 0:0791c1fece8e | 104 | * If you want to add something to the headerfiles use this method and return HTTP_AddFields. See HTTPFields |
RodColeman | 0:0791c1fece8e | 105 | * This would be the right method to implement an Auth Handler. |
RodColeman | 0:0791c1fece8e | 106 | */ |
RodColeman | 0:0791c1fece8e | 107 | virtual HTTPHandle action(HTTPConnection *) const {return HTTP_Deliver;} |
RodColeman | 0:0791c1fece8e | 108 | |
RodColeman | 0:0791c1fece8e | 109 | /** |
RodColeman | 0:0791c1fece8e | 110 | * If action returned HTTP_Deliver. |
RodColeman | 0:0791c1fece8e | 111 | * This function will be executed and it means your handler will be deliver the requested data. |
RodColeman | 0:0791c1fece8e | 112 | * In this method is the right place to allocate the needed space for your request data and to prepare the sended Header. |
RodColeman | 0:0791c1fece8e | 113 | */ |
RodColeman | 0:0791c1fece8e | 114 | virtual HTTPStatus init(HTTPConnection *) const {return HTTP_NotFound;} |
RodColeman | 0:0791c1fece8e | 115 | |
RodColeman | 0:0791c1fece8e | 116 | /** |
RodColeman | 0:0791c1fece8e | 117 | * If data from a post request is arrived for an request you accepted this function will be executed with the data. |
RodColeman | 0:0791c1fece8e | 118 | * @param data A pointer to the received data. |
RodColeman | 0:0791c1fece8e | 119 | * @param len The length of the received data. |
RodColeman | 0:0791c1fece8e | 120 | * @return Return an HTTPHandle. For example HTTP_SuccessEnded if you received all your needed data and want to close the conection (normally not the case). |
RodColeman | 0:0791c1fece8e | 121 | */ |
RodColeman | 0:0791c1fece8e | 122 | virtual HTTPHandle data(HTTPConnection *, void *data, int len) const {return HTTP_SuccessEnded;} |
RodColeman | 0:0791c1fece8e | 123 | |
RodColeman | 0:0791c1fece8e | 124 | /** |
RodColeman | 0:0791c1fece8e | 125 | * If tere is new space in the sendbuffer this function is executed. You can send maximal Bytes of data. |
RodColeman | 0:0791c1fece8e | 126 | * @return Return an HTTPHandle. For example HTTP_SuccessEnded if you send out all your data and you want to close the connection. |
RodColeman | 0:0791c1fece8e | 127 | */ |
RodColeman | 0:0791c1fece8e | 128 | virtual HTTPHandle send(HTTPConnection *, int) const {return HTTP_SuccessEnded;} |
RodColeman | 0:0791c1fece8e | 129 | |
RodColeman | 0:0791c1fece8e | 130 | /** |
RodColeman | 0:0791c1fece8e | 131 | * returns the Prefix from the HTTPHandler |
RodColeman | 0:0791c1fece8e | 132 | */ |
RodColeman | 0:0791c1fece8e | 133 | const char *getPrefix() const {return _prefix;} |
RodColeman | 0:0791c1fece8e | 134 | |
RodColeman | 0:0791c1fece8e | 135 | const char *_prefix; |
RodColeman | 0:0791c1fece8e | 136 | |
RodColeman | 0:0791c1fece8e | 137 | friend HTTPServer; |
RodColeman | 0:0791c1fece8e | 138 | friend HTTPConnection; |
RodColeman | 0:0791c1fece8e | 139 | }; |
RodColeman | 0:0791c1fece8e | 140 | |
RodColeman | 0:0791c1fece8e | 141 | /** |
RodColeman | 0:0791c1fece8e | 142 | * For every incomming connection we have a HTTPConnection object which will handle the requests of this connection. |
RodColeman | 0:0791c1fece8e | 143 | */ |
RodColeman | 0:0791c1fece8e | 144 | class HTTPConnection : public TCPConnection { |
RodColeman | 0:0791c1fece8e | 145 | public: |
RodColeman | 0:0791c1fece8e | 146 | /** |
RodColeman | 0:0791c1fece8e | 147 | * Constructs a new connection object from a server. |
RodColeman | 0:0791c1fece8e | 148 | * It just need the server object to contact the handlers |
RodColeman | 0:0791c1fece8e | 149 | * and the tcp connection pcb. |
RodColeman | 0:0791c1fece8e | 150 | * @param parent The server which created the connection. |
RodColeman | 0:0791c1fece8e | 151 | * @param pcb The pcb object NetServers internal representation of an TCP Connection |
RodColeman | 0:0791c1fece8e | 152 | */ |
RodColeman | 0:0791c1fece8e | 153 | HTTPConnection(HTTPServer *parent, struct tcp_pcb *pcb); |
RodColeman | 0:0791c1fece8e | 154 | /** |
RodColeman | 0:0791c1fece8e | 155 | * Default destructor. Simple cleanup. |
RodColeman | 0:0791c1fece8e | 156 | */ |
RodColeman | 0:0791c1fece8e | 157 | virtual ~HTTPConnection(); |
RodColeman | 0:0791c1fece8e | 158 | |
RodColeman | 0:0791c1fece8e | 159 | /** |
RodColeman | 0:0791c1fece8e | 160 | * Get the requested url. |
RodColeman | 0:0791c1fece8e | 161 | * Only set if a request ist received. |
RodColeman | 0:0791c1fece8e | 162 | */ |
RodColeman | 0:0791c1fece8e | 163 | char *getURL() const { return _request_url; } |
RodColeman | 0:0791c1fece8e | 164 | |
RodColeman | 0:0791c1fece8e | 165 | /** |
RodColeman | 0:0791c1fece8e | 166 | * Gets a string of set fields to send with the answere header. |
RodColeman | 0:0791c1fece8e | 167 | */ |
RodColeman | 0:0791c1fece8e | 168 | const char *getHeaderFields() const { return (_request_headerfields)?_request_headerfields:""; } |
RodColeman | 0:0791c1fece8e | 169 | |
RodColeman | 0:0791c1fece8e | 170 | /** |
RodColeman | 0:0791c1fece8e | 171 | * Gets the length of the anwere in bytes. This is requiered for the HTTP Header. |
RodColeman | 0:0791c1fece8e | 172 | * It should be set over setLength by an HTTPHandler in the init() method. |
RodColeman | 0:0791c1fece8e | 173 | */ |
RodColeman | 0:0791c1fece8e | 174 | const int &getLength() const { return _request_length; } |
RodColeman | 0:0791c1fece8e | 175 | |
RodColeman | 0:0791c1fece8e | 176 | /** |
RodColeman | 0:0791c1fece8e | 177 | * Gets POST or GET or 0 depends on wether ther is a request and what type is requested. |
RodColeman | 0:0791c1fece8e | 178 | */ |
RodColeman | 0:0791c1fece8e | 179 | const char &getType() const { return _request_type; } |
RodColeman | 0:0791c1fece8e | 180 | |
RodColeman | 0:0791c1fece8e | 181 | /** |
RodColeman | 0:0791c1fece8e | 182 | * Gets a value from a header field of the request header. |
RodColeman | 0:0791c1fece8e | 183 | * But you must have registerd this headerfield by the HTTPServer before. |
RodColeman | 0:0791c1fece8e | 184 | * Use the HTTPHandler::reg() method for the registration of important header fields for your Handler. |
RodColeman | 0:0791c1fece8e | 185 | */ |
RodColeman | 0:0791c1fece8e | 186 | const char *getField(char *key) const; |
RodColeman | 0:0791c1fece8e | 187 | |
RodColeman | 0:0791c1fece8e | 188 | /** |
RodColeman | 0:0791c1fece8e | 189 | * For internal usage. Adds an header field value to its hash. |
RodColeman | 0:0791c1fece8e | 190 | * If it was registered You can see the Value with the getField method |
RodColeman | 0:0791c1fece8e | 191 | */ |
RodColeman | 0:0791c1fece8e | 192 | void addField(char *key, char *value); |
RodColeman | 0:0791c1fece8e | 193 | |
RodColeman | 0:0791c1fece8e | 194 | /** |
RodColeman | 0:0791c1fece8e | 195 | * Sets the result length for an request shoud be set in an HTTPHandler.init() call. |
RodColeman | 0:0791c1fece8e | 196 | * This Value will be send with the response header before the first chunk of data is send. |
RodColeman | 0:0791c1fece8e | 197 | */ |
RodColeman | 0:0791c1fece8e | 198 | void setLength(const int &value) { _request_length = value; } |
RodColeman | 0:0791c1fece8e | 199 | |
RodColeman | 0:0791c1fece8e | 200 | /** |
RodColeman | 0:0791c1fece8e | 201 | * Set the response header field to a value. |
RodColeman | 0:0791c1fece8e | 202 | * Should be used in the HTTPHandler::init() method. |
RodColeman | 0:0791c1fece8e | 203 | * For example if you want to set caching methods. |
RodColeman | 0:0791c1fece8e | 204 | */ |
RodColeman | 0:0791c1fece8e | 205 | void setHeaderFields(char *value) { _request_headerfields = value; } |
RodColeman | 0:0791c1fece8e | 206 | |
RodColeman | 0:0791c1fece8e | 207 | /** Indicates that if a request is received the header is incomplete until now. */ |
RodColeman | 0:0791c1fece8e | 208 | bool request_incomplete; |
RodColeman | 0:0791c1fece8e | 209 | |
RodColeman | 0:0791c1fece8e | 210 | /** If an request is complete HTTPHandler:init will be called and can store here its connection data. */ |
RodColeman | 0:0791c1fece8e | 211 | HTTPData *data; |
RodColeman | 0:0791c1fece8e | 212 | |
RodColeman | 0:0791c1fece8e | 213 | /** The handler which handles the current request. Depends on the prefix of the URL. */ |
RodColeman | 0:0791c1fece8e | 214 | HTTPHandler *request_handler; |
RodColeman | 0:0791c1fece8e | 215 | |
RodColeman | 0:0791c1fece8e | 216 | /** The status of the request. Will be set as result of HTTPHandler::init. */ |
RodColeman | 0:0791c1fece8e | 217 | HTTPStatus request_status; |
RodColeman | 0:0791c1fece8e | 218 | |
RodColeman | 0:0791c1fece8e | 219 | /** The HTTTPServer which created this connection. */ |
RodColeman | 0:0791c1fece8e | 220 | HTTPServer *parent; |
RodColeman | 0:0791c1fece8e | 221 | private: |
RodColeman | 0:0791c1fece8e | 222 | virtual void err(err_t err); |
RodColeman | 0:0791c1fece8e | 223 | virtual err_t poll(); |
RodColeman | 0:0791c1fece8e | 224 | virtual err_t sent(u16_t len); |
RodColeman | 0:0791c1fece8e | 225 | virtual err_t recv(struct pbuf *q, err_t err); |
RodColeman | 0:0791c1fece8e | 226 | |
RodColeman | 0:0791c1fece8e | 227 | /** We will not make any DNS requests. */ |
RodColeman | 0:0791c1fece8e | 228 | virtual void dnsreply(const char *, struct ip_addr *) {} |
RodColeman | 0:0791c1fece8e | 229 | |
RodColeman | 0:0791c1fece8e | 230 | /** If a request is finished it will be deleted with this method. Simple cleanup. */ |
RodColeman | 0:0791c1fece8e | 231 | void deleteRequest(); |
RodColeman | 0:0791c1fece8e | 232 | |
RodColeman | 0:0791c1fece8e | 233 | /** Call the handler to send the next chunk of data. */ |
RodColeman | 0:0791c1fece8e | 234 | void send(); |
RodColeman | 0:0791c1fece8e | 235 | |
RodColeman | 0:0791c1fece8e | 236 | /** Call the handler if we received new data. */ |
RodColeman | 0:0791c1fece8e | 237 | void store(void *d, struct pbuf *p); |
RodColeman | 0:0791c1fece8e | 238 | |
RodColeman | 0:0791c1fece8e | 239 | /** |
RodColeman | 0:0791c1fece8e | 240 | * If a request header is not complete we can colect needed header fields. |
RodColeman | 0:0791c1fece8e | 241 | * This happens in here. |
RodColeman | 0:0791c1fece8e | 242 | */ |
RodColeman | 0:0791c1fece8e | 243 | void getFields(struct pbuf **q, char **d); |
RodColeman | 0:0791c1fece8e | 244 | |
RodColeman | 0:0791c1fece8e | 245 | char *_request_url; |
RodColeman | 0:0791c1fece8e | 246 | char _request_type; |
RodColeman | 0:0791c1fece8e | 247 | char *_request_headerfields; |
RodColeman | 0:0791c1fece8e | 248 | map<unsigned int, char *> _request_fields; |
RodColeman | 0:0791c1fece8e | 249 | int _request_length; |
RodColeman | 0:0791c1fece8e | 250 | char *_request_arg_key; |
RodColeman | 0:0791c1fece8e | 251 | char *_request_arg_value; |
RodColeman | 0:0791c1fece8e | 252 | char _request_arg_state; |
RodColeman | 0:0791c1fece8e | 253 | |
RodColeman | 0:0791c1fece8e | 254 | unsigned int emptypolls; // Last time for timeout |
RodColeman | 0:0791c1fece8e | 255 | unsigned int _timeout_max; |
RodColeman | 0:0791c1fece8e | 256 | }; |
RodColeman | 0:0791c1fece8e | 257 | |
RodColeman | 0:0791c1fece8e | 258 | /* Class HTTPServer |
RodColeman | 0:0791c1fece8e | 259 | * An object of this class is an HTTPServer instance and will anwere requests on a given port. |
RodColeman | 0:0791c1fece8e | 260 | * It will deliver HTTP pages |
RodColeman | 0:0791c1fece8e | 261 | */ |
RodColeman | 0:0791c1fece8e | 262 | class HTTPServer : public TCPListener { |
RodColeman | 0:0791c1fece8e | 263 | public: |
RodColeman | 0:0791c1fece8e | 264 | |
RodColeman | 0:0791c1fece8e | 265 | /* Constructor: HTTPServer |
RodColeman | 0:0791c1fece8e | 266 | * Creates an HTTPServer object. You might want to initialise the network server befor. |
RodColeman | 0:0791c1fece8e | 267 | * If you dont do it it will be happen by the first post or get request you make. |
RodColeman | 0:0791c1fece8e | 268 | * |
RodColeman | 0:0791c1fece8e | 269 | * To initialize the network server on creation of the HTTPServer object it's possible to parse some arguments: |
RodColeman | 0:0791c1fece8e | 270 | * Variables: |
RodColeman | 0:0791c1fece8e | 271 | * hostname - A host name for the device. Might take a while to appear in the network, |
RodColeman | 0:0791c1fece8e | 272 | * depends on the network infrastructure. Furthermore in most cases you have |
RodColeman | 0:0791c1fece8e | 273 | * to add your domainname after the host name to address the device. |
RodColeman | 0:0791c1fece8e | 274 | * Default is NULL. |
RodColeman | 0:0791c1fece8e | 275 | * ip - The device ipaddress or ip_addr_any for dhcp. Default is ip_addr_any |
RodColeman | 0:0791c1fece8e | 276 | * nm - The device netmask or ip_addr_any for dhcp. Default is ip_addr_any. |
RodColeman | 0:0791c1fece8e | 277 | * gw - The device gateway or ip_addr_any for dhcp. Default is ip_addr_any. |
RodColeman | 0:0791c1fece8e | 278 | * dns - The device first dns server ip or ip_addr_any for dhcp. Default is ip_addr_any. |
RodColeman | 0:0791c1fece8e | 279 | * |
RodColeman | 0:0791c1fece8e | 280 | * Example: |
RodColeman | 0:0791c1fece8e | 281 | * > HTTPServer http; // Simple DHCP, brings up the TCP/IP stack on bind(). Default prot is port 80. |
RodColeman | 0:0791c1fece8e | 282 | * > HTTPServer http(8080); // Port is here 8080. |
RodColeman | 0:0791c1fece8e | 283 | * |
RodColeman | 0:0791c1fece8e | 284 | * > HTTPServer http("worf"); // Brings up the device with DHCP and sets the host name "worf" |
RodColeman | 0:0791c1fece8e | 285 | * > // The device will be available under worf.<your local domain> |
RodColeman | 0:0791c1fece8e | 286 | * > // for example worf.1-2-3-4.dynamic.sky.com |
RodColeman | 0:0791c1fece8e | 287 | * |
RodColeman | 0:0791c1fece8e | 288 | * > HTTPServer http("wolf", // Brings up the device with static IP address and domain name. |
RodColeman | 0:0791c1fece8e | 289 | * > IPv4(192,168,0,44), // IPv4 is a helper function which allows to rtype ipaddresses direct |
RodColeman | 0:0791c1fece8e | 290 | * > IPv4(255,255,255,0), // as numbers in C++. |
RodColeman | 0:0791c1fece8e | 291 | * > IPv4(192,168,0,1), // the device address is set to 192.168.0.44, netmask 255.255.255.0 |
RodColeman | 0:0791c1fece8e | 292 | * > IPv4(192,168,0,1) // default gateway is 192.168.0.1 and dns to 192.168.0.1 as well. |
RodColeman | 0:0791c1fece8e | 293 | * > 8080); // And port is on 8080. Default port is 80. |
RodColeman | 0:0791c1fece8e | 294 | */ |
RodColeman | 0:0791c1fece8e | 295 | |
RodColeman | 0:0791c1fece8e | 296 | HTTPServer(const char *hostname, struct ip_addr ip = ip_addr_any, struct ip_addr nm = ip_addr_any, struct ip_addr gw = ip_addr_any, struct ip_addr dns = ip_addr_any, unsigned short port = 80); |
RodColeman | 0:0791c1fece8e | 297 | HTTPServer(unsigned short port = 80); |
RodColeman | 0:0791c1fece8e | 298 | |
RodColeman | 0:0791c1fece8e | 299 | /* Destructor: ~HTTPServer |
RodColeman | 0:0791c1fece8e | 300 | * Destroys the HTTPServer and all open connections. |
RodColeman | 0:0791c1fece8e | 301 | */ |
RodColeman | 0:0791c1fece8e | 302 | virtual ~HTTPServer() { |
RodColeman | 0:0791c1fece8e | 303 | fields.clear(); |
RodColeman | 0:0791c1fece8e | 304 | _handler.clear(); |
RodColeman | 0:0791c1fece8e | 305 | } |
RodColeman | 0:0791c1fece8e | 306 | |
RodColeman | 0:0791c1fece8e | 307 | /* Function: addHandler |
RodColeman | 0:0791c1fece8e | 308 | * Add a new content handler to handle requests. |
RodColeman | 0:0791c1fece8e | 309 | * Content handler are URL prefix specific. |
RodColeman | 0:0791c1fece8e | 310 | * Have a look at HTTPRPC and HTTPFileSystemHandler for examples. |
RodColeman | 0:0791c1fece8e | 311 | */ |
RodColeman | 0:0791c1fece8e | 312 | virtual void addHandler(HTTPHandler *handler) { |
RodColeman | 0:0791c1fece8e | 313 | _handler.push_back(handler); |
RodColeman | 0:0791c1fece8e | 314 | handler->reg(this); |
RodColeman | 0:0791c1fece8e | 315 | } |
RodColeman | 0:0791c1fece8e | 316 | |
RodColeman | 0:0791c1fece8e | 317 | /* Function registerField |
RodColeman | 0:0791c1fece8e | 318 | * Register needed header fields to filter from a request header. |
RodColeman | 0:0791c1fece8e | 319 | * Should be called from HTTPHandler::reg() |
RodColeman | 0:0791c1fece8e | 320 | */ |
RodColeman | 0:0791c1fece8e | 321 | virtual void registerField(char *name) { |
RodColeman | 0:0791c1fece8e | 322 | fields.insert(hash((unsigned char *)name)); |
RodColeman | 0:0791c1fece8e | 323 | } |
RodColeman | 0:0791c1fece8e | 324 | |
RodColeman | 0:0791c1fece8e | 325 | /* Function isField |
RodColeman | 0:0791c1fece8e | 326 | * A short lookup if the headerfield is registerd. |
RodColeman | 0:0791c1fece8e | 327 | */ |
RodColeman | 0:0791c1fece8e | 328 | virtual bool isField(unsigned long h) const { |
RodColeman | 0:0791c1fece8e | 329 | return fields.find(h) != fields.end(); |
RodColeman | 0:0791c1fece8e | 330 | } |
RodColeman | 0:0791c1fece8e | 331 | |
RodColeman | 0:0791c1fece8e | 332 | /* Function: poll |
RodColeman | 0:0791c1fece8e | 333 | * You have to call this method at least every 250ms to let the http server run. |
RodColeman | 0:0791c1fece8e | 334 | * But I would recomend to call this function as fast as possible. |
RodColeman | 0:0791c1fece8e | 335 | * This function is directly coupled to the answere time of your HTTPServer instance. |
RodColeman | 0:0791c1fece8e | 336 | */ |
RodColeman | 0:0791c1fece8e | 337 | inline static void poll() { |
RodColeman | 0:0791c1fece8e | 338 | NetServer::poll(); |
RodColeman | 0:0791c1fece8e | 339 | } |
RodColeman | 0:0791c1fece8e | 340 | |
RodColeman | 0:0791c1fece8e | 341 | /* Function: timeout |
RodColeman | 0:0791c1fece8e | 342 | * Sets the timout for a HTTP request. |
RodColeman | 0:0791c1fece8e | 343 | * The timout is the time wich is allowed to spent between two incomming TCP packets. |
RodColeman | 0:0791c1fece8e | 344 | * If the time is passed the connection will be closed. |
RodColeman | 0:0791c1fece8e | 345 | */ |
RodColeman | 0:0791c1fece8e | 346 | void timeout(int value) { |
RodColeman | 0:0791c1fece8e | 347 | _timeout_max = value; |
RodColeman | 0:0791c1fece8e | 348 | } |
RodColeman | 0:0791c1fece8e | 349 | |
RodColeman | 0:0791c1fece8e | 350 | /* Function timeout |
RodColeman | 0:0791c1fece8e | 351 | * Returns the timout to use it in HTTPHandlers and HTTPConnections |
RodColeman | 0:0791c1fece8e | 352 | */ |
RodColeman | 0:0791c1fece8e | 353 | int timeout() { |
RodColeman | 0:0791c1fece8e | 354 | return _timeout_max; |
RodColeman | 0:0791c1fece8e | 355 | } |
RodColeman | 0:0791c1fece8e | 356 | private: |
RodColeman | 0:0791c1fece8e | 357 | /** |
RodColeman | 0:0791c1fece8e | 358 | * Pick up the right handler to deliver the response. |
RodColeman | 0:0791c1fece8e | 359 | */ |
RodColeman | 0:0791c1fece8e | 360 | virtual HTTPHandler *handle(HTTPConnection *con) const { |
RodColeman | 0:0791c1fece8e | 361 | for(list<HTTPHandler *>::const_iterator iter = _handler.begin(); |
RodColeman | 0:0791c1fece8e | 362 | iter != _handler.end(); iter++) { |
RodColeman | 0:0791c1fece8e | 363 | if(strncmp((*iter)->getPrefix(), con->getURL(), strlen((*iter)->getPrefix()))==0) { |
RodColeman | 0:0791c1fece8e | 364 | HTTPHandler *handler = *iter; |
RodColeman | 0:0791c1fece8e | 365 | if(handler->action(con)==HTTP_Deliver) { |
RodColeman | 0:0791c1fece8e | 366 | return *iter; |
RodColeman | 0:0791c1fece8e | 367 | } |
RodColeman | 0:0791c1fece8e | 368 | } |
RodColeman | 0:0791c1fece8e | 369 | } |
RodColeman | 0:0791c1fece8e | 370 | return NULL; |
RodColeman | 0:0791c1fece8e | 371 | } |
RodColeman | 0:0791c1fece8e | 372 | |
RodColeman | 0:0791c1fece8e | 373 | /** |
RodColeman | 0:0791c1fece8e | 374 | * Accept an incomming connection and fork a HTTPConnection if we have enought memory. |
RodColeman | 0:0791c1fece8e | 375 | */ |
RodColeman | 0:0791c1fece8e | 376 | virtual err_t accept(struct tcp_pcb *pcb, err_t err) { |
RodColeman | 0:0791c1fece8e | 377 | LWIP_UNUSED_ARG(err); |
RodColeman | 0:0791c1fece8e | 378 | HTTPConnection *con = new HTTPConnection(this, pcb); |
RodColeman | 0:0791c1fece8e | 379 | // printf("New Connection opend. Now are %u connections open\n", ++gconnections); |
RodColeman | 0:0791c1fece8e | 380 | if(con == NULL) { |
RodColeman | 0:0791c1fece8e | 381 | printf("http_accept: Out of memory\n"); |
RodColeman | 0:0791c1fece8e | 382 | return ERR_MEM; |
RodColeman | 0:0791c1fece8e | 383 | } |
RodColeman | 0:0791c1fece8e | 384 | con->set_poll_interval(1); |
RodColeman | 0:0791c1fece8e | 385 | tcp_setprio(pcb, TCP_PRIO_MIN); |
RodColeman | 0:0791c1fece8e | 386 | return ERR_OK; |
RodColeman | 0:0791c1fece8e | 387 | } |
RodColeman | 0:0791c1fece8e | 388 | |
RodColeman | 0:0791c1fece8e | 389 | /** The registerd request header fields */ |
RodColeman | 0:0791c1fece8e | 390 | set<unsigned int> fields; |
RodColeman | 0:0791c1fece8e | 391 | |
RodColeman | 0:0791c1fece8e | 392 | /** A List of all registered handler. */ |
RodColeman | 0:0791c1fece8e | 393 | list<HTTPHandler *> _handler; |
RodColeman | 0:0791c1fece8e | 394 | |
RodColeman | 0:0791c1fece8e | 395 | int _timeout_max; |
RodColeman | 0:0791c1fece8e | 396 | |
RodColeman | 0:0791c1fece8e | 397 | friend HTTPConnection; |
RodColeman | 0:0791c1fece8e | 398 | }; |
RodColeman | 0:0791c1fece8e | 399 | |
RodColeman | 0:0791c1fece8e | 400 | }; |
RodColeman | 0:0791c1fece8e | 401 | |
RodColeman | 0:0791c1fece8e | 402 | #include "HTTPRPC.h" |
RodColeman | 0:0791c1fece8e | 403 | #include "HTTPFS.h" |
RodColeman | 0:0791c1fece8e | 404 | #include "HTTPFields.h" |
RodColeman | 0:0791c1fece8e | 405 | |
RodColeman | 0:0791c1fece8e | 406 | #endif /* HTTP_H */ |