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