LRSD stephane / Mbed 2 deprecated WEBserver0-lrsd

Dependencies:   mbed

Committer:
geiineuville
Date:
Fri Sep 02 08:36:24 2011 +0000
Revision:
0:441400ffd086
V0

Who changed what in which revision?

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