LRSD stephane / Mbed 2 deprecated WEBserverv1

Dependencies:   mbed

Committer:
geiineuville
Date:
Sat Sep 03 09:42:32 2011 +0000
Revision:
0:4570f87afab6
v1

Who changed what in which revision?

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