A version of LWIP, provided for backwards compatibility.

Dependents:   AA_DemoBoard DemoBoard HelloServerDemo DemoBoard_RangeIndicator ... more

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

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