f

Dependencies:   mbed

Fork of lwip by mbed unsupported

Committer:
root@mbed.org
Date:
Tue May 08 15:32:10 2012 +0100
Revision:
0:5e1631496985
initial commit

Who changed what in which revision?

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