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