Embedded WebSockets Experiment

Dependencies:   mbed MD5

Committer:
nandgate
Date:
Tue Jul 26 05:30:53 2011 +0000
Revision:
0:6dee052a3fa4

        

Who changed what in which revision?

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