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/HTTPClient/HTTPClient.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 HTTPCLIENT_H |
mitesh2patel | 0:e1a0471e5ffb | 2 | #define HTTPCLIENT_H |
mitesh2patel | 0:e1a0471e5ffb | 3 | |
mitesh2patel | 0:e1a0471e5ffb | 4 | #include "TCPConnection.h" |
mitesh2patel | 0:e1a0471e5ffb | 5 | #include "NetServer.h" |
mitesh2patel | 0:e1a0471e5ffb | 6 | #include "iputil.h" |
mitesh2patel | 0:e1a0471e5ffb | 7 | |
mitesh2patel | 0:e1a0471e5ffb | 8 | /* Class: HTTPClient |
mitesh2patel | 0:e1a0471e5ffb | 9 | * A simple Class to fetch HTTP Pages. |
mitesh2patel | 0:e1a0471e5ffb | 10 | */ |
mitesh2patel | 0:e1a0471e5ffb | 11 | class HTTPClient : public TCPConnection { |
mitesh2patel | 0:e1a0471e5ffb | 12 | public: |
mitesh2patel | 0:e1a0471e5ffb | 13 | /* Constructor: HTTPClient |
mitesh2patel | 0:e1a0471e5ffb | 14 | * Creates an HTTPClient object. You might want to initialise the network server befor. |
mitesh2patel | 0:e1a0471e5ffb | 15 | * If you dont do it it will be happen by the first post or get request you make. |
mitesh2patel | 0:e1a0471e5ffb | 16 | * |
mitesh2patel | 0:e1a0471e5ffb | 17 | * To initialize the network server on creation of the HTTPClient object it's possible to parse some arguments. |
mitesh2patel | 0:e1a0471e5ffb | 18 | * |
mitesh2patel | 0:e1a0471e5ffb | 19 | * Variables: |
mitesh2patel | 0:e1a0471e5ffb | 20 | * hostname - A host name for the device. Might take a while to appear in the network, |
mitesh2patel | 0:e1a0471e5ffb | 21 | * depends on the network infrastructure. Furthermore in most cases you have |
mitesh2patel | 0:e1a0471e5ffb | 22 | * to add your domainname after the host name to address the device. |
mitesh2patel | 0:e1a0471e5ffb | 23 | * Default is NULL. |
mitesh2patel | 0:e1a0471e5ffb | 24 | * ip - The device ipaddress or ip_addr_any for dhcp. Default is ip_addr_any |
mitesh2patel | 0:e1a0471e5ffb | 25 | * nm - The device netmask or ip_addr_any for dhcp. Default is ip_addr_any. |
mitesh2patel | 0:e1a0471e5ffb | 26 | * gw - The device gateway or ip_addr_any for dhcp. Default is ip_addr_any. |
mitesh2patel | 0:e1a0471e5ffb | 27 | * dns - The device first dns server ip or ip_addr_any for dhcp. Default is ip_addr_any. |
mitesh2patel | 0:e1a0471e5ffb | 28 | * |
mitesh2patel | 0:e1a0471e5ffb | 29 | * Example: |
mitesh2patel | 0:e1a0471e5ffb | 30 | * > HTTPClient http; // Simple DHCP, brings up the TCP/IP stack on first get/post reguest. |
mitesh2patel | 0:e1a0471e5ffb | 31 | * |
mitesh2patel | 0:e1a0471e5ffb | 32 | * > HTTPClient http("worf"); // Brings up the device with DHCP and sets the host name "worf" |
mitesh2patel | 0:e1a0471e5ffb | 33 | * > // The device will be available under worf.<your local domain> |
mitesh2patel | 0:e1a0471e5ffb | 34 | * > // for example worf.1-2-3-4.dynamic.sky.com |
mitesh2patel | 0:e1a0471e5ffb | 35 | * |
mitesh2patel | 0:e1a0471e5ffb | 36 | * > HTTPClient http("wolf", // Brings up the device with static IP address and domain name. |
mitesh2patel | 0:e1a0471e5ffb | 37 | * > IPv4(192,168,0,44), // IPv4 is a helper function which allows to rtype ipaddresses direct |
mitesh2patel | 0:e1a0471e5ffb | 38 | * > IPv4(255,255,255,0), // as numbers in C++. |
mitesh2patel | 0:e1a0471e5ffb | 39 | * > IPv4(192,168,0,1), // the device address is set to 192.168.0.44, netmask 255.255.255.0 |
mitesh2patel | 0:e1a0471e5ffb | 40 | * > IPv4(192,168,0,1)); // default gateway is 192.168.0.1 and dns to 192.168.0.1 as well. |
mitesh2patel | 0:e1a0471e5ffb | 41 | * > |
mitesh2patel | 0:e1a0471e5ffb | 42 | */ |
mitesh2patel | 0:e1a0471e5ffb | 43 | HTTPClient(const char *hostname = NULL, 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); |
mitesh2patel | 0:e1a0471e5ffb | 44 | |
mitesh2patel | 0:e1a0471e5ffb | 45 | /* Destructor: ~HTTPClient |
mitesh2patel | 0:e1a0471e5ffb | 46 | * Destroys the HTTPClient class. |
mitesh2patel | 0:e1a0471e5ffb | 47 | */ |
mitesh2patel | 0:e1a0471e5ffb | 48 | virtual ~HTTPClient() { |
mitesh2patel | 0:e1a0471e5ffb | 49 | if(_auth) { |
mitesh2patel | 0:e1a0471e5ffb | 50 | delete _auth; |
mitesh2patel | 0:e1a0471e5ffb | 51 | _auth = NULL; |
mitesh2patel | 0:e1a0471e5ffb | 52 | } |
mitesh2patel | 0:e1a0471e5ffb | 53 | } |
mitesh2patel | 0:e1a0471e5ffb | 54 | |
mitesh2patel | 0:e1a0471e5ffb | 55 | /* Function: headers |
mitesh2patel | 0:e1a0471e5ffb | 56 | * Add header additional Information to the next post or get requests. |
mitesh2patel | 0:e1a0471e5ffb | 57 | * Somtimes it is useful to add further header information. For example an auth field. |
mitesh2patel | 0:e1a0471e5ffb | 58 | * Each time you call this function it will be replace the header fields given by an |
mitesh2patel | 0:e1a0471e5ffb | 59 | * prior call. |
mitesh2patel | 0:e1a0471e5ffb | 60 | * |
mitesh2patel | 0:e1a0471e5ffb | 61 | * It will not free your data. |
mitesh2patel | 0:e1a0471e5ffb | 62 | * Variables: |
mitesh2patel | 0:e1a0471e5ffb | 63 | * fields - A string containing all fields you want to add. Seperated by "\\r\\n". |
mitesh2patel | 0:e1a0471e5ffb | 64 | * |
mitesh2patel | 0:e1a0471e5ffb | 65 | */ |
mitesh2patel | 0:e1a0471e5ffb | 66 | void headers(const char *fields); |
mitesh2patel | 0:e1a0471e5ffb | 67 | |
mitesh2patel | 0:e1a0471e5ffb | 68 | /* Function: auth |
mitesh2patel | 0:e1a0471e5ffb | 69 | * Enables basic authentication. Just enter username and password |
mitesh2patel | 0:e1a0471e5ffb | 70 | * and they will be used for all requests. |
mitesh2patel | 0:e1a0471e5ffb | 71 | * If you want to lean your username and passwort just insert NULL, NULL. |
mitesh2patel | 0:e1a0471e5ffb | 72 | * |
mitesh2patel | 0:e1a0471e5ffb | 73 | * Variables: |
mitesh2patel | 0:e1a0471e5ffb | 74 | * user - Username for ure auth or NULL. |
mitesh2patel | 0:e1a0471e5ffb | 75 | * password - Password for auth or NULL. |
mitesh2patel | 0:e1a0471e5ffb | 76 | */ |
mitesh2patel | 0:e1a0471e5ffb | 77 | void auth(const char *user, const char *password); |
mitesh2patel | 0:e1a0471e5ffb | 78 | |
mitesh2patel | 0:e1a0471e5ffb | 79 | /* Function: get |
mitesh2patel | 0:e1a0471e5ffb | 80 | * A simple get-request just insert the url. |
mitesh2patel | 0:e1a0471e5ffb | 81 | * But if you want you can get the result back as a string. |
mitesh2patel | 0:e1a0471e5ffb | 82 | * Sometimes you want get a large result, more than 64 Bytes |
mitesh2patel | 0:e1a0471e5ffb | 83 | * than define your size. |
mitesh2patel | 0:e1a0471e5ffb | 84 | * |
mitesh2patel | 0:e1a0471e5ffb | 85 | * Variables: |
mitesh2patel | 0:e1a0471e5ffb | 86 | * url - The requested URL. |
mitesh2patel | 0:e1a0471e5ffb | 87 | * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes. |
mitesh2patel | 0:e1a0471e5ffb | 88 | * rsize - The maximum size of your result. By default 64 Bytes. |
mitesh2patel | 0:e1a0471e5ffb | 89 | * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length. |
mitesh2patel | 0:e1a0471e5ffb | 90 | */ |
mitesh2patel | 0:e1a0471e5ffb | 91 | unsigned int get(const char *url, char *result = NULL, int rsize = 64); |
mitesh2patel | 0:e1a0471e5ffb | 92 | |
mitesh2patel | 0:e1a0471e5ffb | 93 | /* Function: get |
mitesh2patel | 0:e1a0471e5ffb | 94 | * A simple get-request just insert the url and a FILE Pointer. |
mitesh2patel | 0:e1a0471e5ffb | 95 | * This get request will save yor result to an file. Very helpful if you demat a big bunch of data. |
mitesh2patel | 0:e1a0471e5ffb | 96 | * |
mitesh2patel | 0:e1a0471e5ffb | 97 | * Variables: |
mitesh2patel | 0:e1a0471e5ffb | 98 | * url - The requested URL. |
mitesh2patel | 0:e1a0471e5ffb | 99 | * result - The FILE Pointer in which you want to store the result. |
mitesh2patel | 0:e1a0471e5ffb | 100 | * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length. |
mitesh2patel | 0:e1a0471e5ffb | 101 | */ |
mitesh2patel | 0:e1a0471e5ffb | 102 | unsigned int get(const char *url, FILE *result); |
mitesh2patel | 0:e1a0471e5ffb | 103 | |
mitesh2patel | 0:e1a0471e5ffb | 104 | /* Function: post |
mitesh2patel | 0:e1a0471e5ffb | 105 | * A simple post-request just insert the url. |
mitesh2patel | 0:e1a0471e5ffb | 106 | * You can send data if you want but they should be NULL-Terminated. |
mitesh2patel | 0:e1a0471e5ffb | 107 | * If you want you can get the result back as a string. |
mitesh2patel | 0:e1a0471e5ffb | 108 | * Sometimes you want get a large result, more than 64 Bytes |
mitesh2patel | 0:e1a0471e5ffb | 109 | * than define your size. |
mitesh2patel | 0:e1a0471e5ffb | 110 | * |
mitesh2patel | 0:e1a0471e5ffb | 111 | * Variables: |
mitesh2patel | 0:e1a0471e5ffb | 112 | * url - The requested URL. |
mitesh2patel | 0:e1a0471e5ffb | 113 | * data - A char array of the data you might want to send. |
mitesh2patel | 0:e1a0471e5ffb | 114 | * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes. |
mitesh2patel | 0:e1a0471e5ffb | 115 | * rsize - The maximum size of your result. By default 64 Bytes. |
mitesh2patel | 0:e1a0471e5ffb | 116 | * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length. |
mitesh2patel | 0:e1a0471e5ffb | 117 | */ |
mitesh2patel | 0:e1a0471e5ffb | 118 | unsigned int post(const char *url, const char *data = NULL, char *result = NULL, int rsize = 64); |
mitesh2patel | 0:e1a0471e5ffb | 119 | |
mitesh2patel | 0:e1a0471e5ffb | 120 | /* Function: post |
mitesh2patel | 0:e1a0471e5ffb | 121 | * A simple get-request just insert the url and a FILE Pointer. |
mitesh2patel | 0:e1a0471e5ffb | 122 | * You can send data if you want but they should be NULL-Terminated. |
mitesh2patel | 0:e1a0471e5ffb | 123 | * This get request will save yor result to an file. Very helpful if you demat a big bunch of data. |
mitesh2patel | 0:e1a0471e5ffb | 124 | * |
mitesh2patel | 0:e1a0471e5ffb | 125 | * Variables: |
mitesh2patel | 0:e1a0471e5ffb | 126 | * url - The requested URL. |
mitesh2patel | 0:e1a0471e5ffb | 127 | * data - A char array of the data you might want to send. |
mitesh2patel | 0:e1a0471e5ffb | 128 | * result - The FILE Pointer in which you want to store the result. |
mitesh2patel | 0:e1a0471e5ffb | 129 | * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length. |
mitesh2patel | 0:e1a0471e5ffb | 130 | */ |
mitesh2patel | 0:e1a0471e5ffb | 131 | unsigned int post(const char *url, const char *data, FILE *result); |
mitesh2patel | 0:e1a0471e5ffb | 132 | |
mitesh2patel | 0:e1a0471e5ffb | 133 | /* Function: post |
mitesh2patel | 0:e1a0471e5ffb | 134 | * A simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results. |
mitesh2patel | 0:e1a0471e5ffb | 135 | * Your data to sent can come from a file. |
mitesh2patel | 0:e1a0471e5ffb | 136 | * This get request will save yor result to an file. Very helpful if you demat a big bunch of data. |
mitesh2patel | 0:e1a0471e5ffb | 137 | * |
mitesh2patel | 0:e1a0471e5ffb | 138 | * Variables: |
mitesh2patel | 0:e1a0471e5ffb | 139 | * url - The requested URL. |
mitesh2patel | 0:e1a0471e5ffb | 140 | * data - A FILE Pointer of a file you might want to send. |
mitesh2patel | 0:e1a0471e5ffb | 141 | * result - The FILE Pointer in which you want to store the result. |
mitesh2patel | 0:e1a0471e5ffb | 142 | * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length. |
mitesh2patel | 0:e1a0471e5ffb | 143 | */ |
mitesh2patel | 0:e1a0471e5ffb | 144 | unsigned int post(const char *url, FILE *data, FILE *result); |
mitesh2patel | 0:e1a0471e5ffb | 145 | |
mitesh2patel | 0:e1a0471e5ffb | 146 | /* Function: post |
mitesh2patel | 0:e1a0471e5ffb | 147 | * A simple get-request just insert the url and a two FILE Pointers to send the content of the file out and store you results. |
mitesh2patel | 0:e1a0471e5ffb | 148 | * Your data to sent can come from a file. |
mitesh2patel | 0:e1a0471e5ffb | 149 | * If you want you can get the result back as a string. |
mitesh2patel | 0:e1a0471e5ffb | 150 | * Sometimes you want get a large result, more than 64 Bytes |
mitesh2patel | 0:e1a0471e5ffb | 151 | * than define your size. |
mitesh2patel | 0:e1a0471e5ffb | 152 | * |
mitesh2patel | 0:e1a0471e5ffb | 153 | * url - The requested URL. |
mitesh2patel | 0:e1a0471e5ffb | 154 | * data - A FILE Pointer of a file you might want to send. |
mitesh2patel | 0:e1a0471e5ffb | 155 | * result - The answere to your request, by default you have not to take it. But if you want it, it has a default length from 64 Bytes. |
mitesh2patel | 0:e1a0471e5ffb | 156 | * length - The maximum size of your result. By default 64 Bytes. |
mitesh2patel | 0:e1a0471e5ffb | 157 | * returns - The length of your demanted result or 1 if the request is succssesful or 0 if it failed. But it might be 0 too wether your result has 0 in length. |
mitesh2patel | 0:e1a0471e5ffb | 158 | */ |
mitesh2patel | 0:e1a0471e5ffb | 159 | unsigned int post(const char *url, FILE *data = NULL, char *result = NULL, int length = 64); |
mitesh2patel | 0:e1a0471e5ffb | 160 | |
mitesh2patel | 0:e1a0471e5ffb | 161 | /* Function: timeout |
mitesh2patel | 0:e1a0471e5ffb | 162 | * Sets the timout for a HTTP request. |
mitesh2patel | 0:e1a0471e5ffb | 163 | * The timout is the time wich is allowed to spent between two incomming TCP packets. |
mitesh2patel | 0:e1a0471e5ffb | 164 | * If the time is passed the connection will be closed. |
mitesh2patel | 0:e1a0471e5ffb | 165 | */ |
mitesh2patel | 0:e1a0471e5ffb | 166 | void timeout(int value); |
mitesh2patel | 0:e1a0471e5ffb | 167 | |
mitesh2patel | 0:e1a0471e5ffb | 168 | private: |
mitesh2patel | 0:e1a0471e5ffb | 169 | virtual void err(err_t err); |
mitesh2patel | 0:e1a0471e5ffb | 170 | virtual err_t poll(); |
mitesh2patel | 0:e1a0471e5ffb | 171 | virtual err_t sent(u16_t len) {return ERR_OK;}; |
mitesh2patel | 0:e1a0471e5ffb | 172 | virtual err_t connected(err_t err); |
mitesh2patel | 0:e1a0471e5ffb | 173 | virtual err_t recv(struct pbuf *q, err_t err); |
mitesh2patel | 0:e1a0471e5ffb | 174 | virtual void dnsreply(const char *hostname, struct ip_addr *ipaddr); |
mitesh2patel | 0:e1a0471e5ffb | 175 | unsigned int make(const char *); |
mitesh2patel | 0:e1a0471e5ffb | 176 | |
mitesh2patel | 0:e1a0471e5ffb | 177 | char *_auth; |
mitesh2patel | 0:e1a0471e5ffb | 178 | bool _ready; |
mitesh2patel | 0:e1a0471e5ffb | 179 | char _mode; |
mitesh2patel | 0:e1a0471e5ffb | 180 | char _state; |
mitesh2patel | 0:e1a0471e5ffb | 181 | int _timeout; |
mitesh2patel | 0:e1a0471e5ffb | 182 | const char *_host; |
mitesh2patel | 0:e1a0471e5ffb | 183 | const char *_path; |
mitesh2patel | 0:e1a0471e5ffb | 184 | void *_result; |
mitesh2patel | 0:e1a0471e5ffb | 185 | void *_data; |
mitesh2patel | 0:e1a0471e5ffb | 186 | const char *_request; |
mitesh2patel | 0:e1a0471e5ffb | 187 | const char *_headerfields; |
mitesh2patel | 0:e1a0471e5ffb | 188 | unsigned int _hostlen; |
mitesh2patel | 0:e1a0471e5ffb | 189 | unsigned int _resultoff; |
mitesh2patel | 0:e1a0471e5ffb | 190 | unsigned int _resultleft; |
mitesh2patel | 0:e1a0471e5ffb | 191 | int _timeout_max; |
mitesh2patel | 0:e1a0471e5ffb | 192 | }; |
mitesh2patel | 0:e1a0471e5ffb | 193 | |
mitesh2patel | 0:e1a0471e5ffb | 194 | #endif |