Example of HTTPServer with additional features: * SNTPClient, DST rules * Link status indication * Local or SDCard-based WebServer * RPC-able class * Static and Dynamic HTML page

Dependencies:   mbed

Committer:
iva2k
Date:
Tue Jan 12 07:41:55 2010 +0000
Revision:
2:360fda42fefd
Parent:
0:886e4b3119ad

        

Who changed what in which revision?

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