Some quick code to use UDP-only (no TCP) with mBed. Echos received packets and sends packets when a button is pressed

Dependencies:   mbed

Committer:
pehrhovey
Date:
Sun Mar 14 00:54:12 2010 +0000
Revision:
0:a548a085de55

        

Who changed what in which revision?

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