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