Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
HTTPClient.h
00001 #ifndef HTTPCLIENT_H 00002 #define HTTPCLIENT_H 00003 00004 #include "TCPConnection.h" 00005 #include "NetServer.h" 00006 #include "iputil.h" 00007 00008 /* Class: HTTPClient 00009 * A simple Class to fetch HTTP Pages. 00010 */ 00011 class HTTPClient : public TCPConnection { 00012 public: 00013 /* Constructor: HTTPClient 00014 * Creates an HTTPClient object. You might want to initialise the network server befor. 00015 * If you dont do it it will be happen by the first post or get request you make. 00016 * 00017 * To initialize the network server on creation of the HTTPClient object it's possible to parse some arguments. 00018 * 00019 * Variables: 00020 * hostname - A host name for the device. Might take a while to appear in the network, 00021 * depends on the network infrastructure. Furthermore in most cases you have 00022 * to add your domainname after the host name to address the device. 00023 * Default is NULL. 00024 * ip - The device ipaddress or ip_addr_any for dhcp. Default is ip_addr_any 00025 * nm - The device netmask or ip_addr_any for dhcp. Default is ip_addr_any. 00026 * gw - The device gateway or ip_addr_any for dhcp. Default is ip_addr_any. 00027 * dns - The device first dns server ip or ip_addr_any for dhcp. Default is ip_addr_any. 00028 * 00029 * Example: 00030 * > HTTPClient http; // Simple DHCP, brings up the TCP/IP stack on first get/post reguest. 00031 * 00032 * > HTTPClient http("worf"); // Brings up the device with DHCP and sets the host name "worf" 00033 * > // The device will be available under worf.<your local domain> 00034 * > // for example worf.1-2-3-4.dynamic.sky.com 00035 * 00036 * > HTTPClient http("wolf", // Brings up the device with static IP address and domain name. 00037 * > IPv4(192,168,0,44), // IPv4 is a helper function which allows to rtype ipaddresses direct 00038 * > IPv4(255,255,255,0), // as numbers in C++. 00039 * > IPv4(192,168,0,1), // the device address is set to 192.168.0.44, netmask 255.255.255.0 00040 * > IPv4(192,168,0,1)); // default gateway is 192.168.0.1 and dns to 192.168.0.1 as well. 00041 * > 00042 */ 00043 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); 00044 00045 /* Destructor: ~HTTPClient 00046 * Destroys the HTTPClient class. 00047 */ 00048 virtual ~HTTPClient() { 00049 if(_auth) { 00050 delete _auth; 00051 _auth = NULL; 00052 } 00053 } 00054 00055 /* Function: headers 00056 * Add header additional Information to the next post or get requests. 00057 * Somtimes it is useful to add further header information. For example an auth field. 00058 * Each time you call this function it will be replace the header fields given by an 00059 * prior call. 00060 * 00061 * It will not free your data. 00062 * Variables: 00063 * fields - A string containing all fields you want to add. Seperated by "\\r\\n". 00064 * 00065 */ 00066 void headers(const char *fields); 00067 00068 /* Function: auth 00069 * Enables basic authentication. Just enter username and password 00070 * and they will be used for all requests. 00071 * If you want to lean your username and passwort just insert NULL, NULL. 00072 * 00073 * Variables: 00074 * user - Username for ure auth or NULL. 00075 * password - Password for auth or NULL. 00076 */ 00077 void auth(const char *user, const char *password); 00078 00079 /* Function: get 00080 * A simple get-request just insert the url. 00081 * But if you want you can get the result back as a string. 00082 * Sometimes you want get a large result, more than 64 Bytes 00083 * than define your size. 00084 * 00085 * Variables: 00086 * url - The requested URL. 00087 * 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. 00088 * rsize - The maximum size of your result. By default 64 Bytes. 00089 * 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. 00090 */ 00091 unsigned int get(const char *url, char *result = NULL, int rsize = 64); 00092 00093 /* Function: get 00094 * A simple get-request just insert the url and a FILE Pointer. 00095 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data. 00096 * 00097 * Variables: 00098 * url - The requested URL. 00099 * result - The FILE Pointer in which you want to store the result. 00100 * 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. 00101 */ 00102 unsigned int get(const char *url, FILE *result); 00103 00104 /* Function: post 00105 * A simple post-request just insert the url. 00106 * You can send data if you want but they should be NULL-Terminated. 00107 * If you want you can get the result back as a string. 00108 * Sometimes you want get a large result, more than 64 Bytes 00109 * than define your size. 00110 * 00111 * Variables: 00112 * url - The requested URL. 00113 * data - A char array of the data you might want to send. 00114 * 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. 00115 * rsize - The maximum size of your result. By default 64 Bytes. 00116 * 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. 00117 */ 00118 unsigned int post(const char *url, const char *data = NULL, char *result = NULL, int rsize = 64); 00119 00120 /* Function: post 00121 * A simple get-request just insert the url and a FILE Pointer. 00122 * You can send data if you want but they should be NULL-Terminated. 00123 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data. 00124 * 00125 * Variables: 00126 * url - The requested URL. 00127 * data - A char array of the data you might want to send. 00128 * result - The FILE Pointer in which you want to store the result. 00129 * 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. 00130 */ 00131 unsigned int post(const char *url, const char *data, FILE *result); 00132 00133 /* Function: post 00134 * 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. 00135 * Your data to sent can come from a file. 00136 * This get request will save yor result to an file. Very helpful if you demat a big bunch of data. 00137 * 00138 * Variables: 00139 * url - The requested URL. 00140 * data - A FILE Pointer of a file you might want to send. 00141 * result - The FILE Pointer in which you want to store the result. 00142 * 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. 00143 */ 00144 unsigned int post(const char *url, FILE *data, FILE *result); 00145 00146 /* Function: post 00147 * 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. 00148 * Your data to sent can come from a file. 00149 * If you want you can get the result back as a string. 00150 * Sometimes you want get a large result, more than 64 Bytes 00151 * than define your size. 00152 * 00153 * url - The requested URL. 00154 * data - A FILE Pointer of a file you might want to send. 00155 * 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. 00156 * length - The maximum size of your result. By default 64 Bytes. 00157 * 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. 00158 */ 00159 unsigned int post(const char *url, FILE *data = NULL, char *result = NULL, int length = 64); 00160 00161 /* Function: timeout 00162 * Sets the timout for a HTTP request. 00163 * The timout is the time wich is allowed to spent between two incomming TCP packets. 00164 * If the time is passed the connection will be closed. 00165 */ 00166 void timeout(int value); 00167 00168 private: 00169 virtual void err(err_t err); 00170 virtual err_t poll(); 00171 virtual err_t sent(u16_t len) {return ERR_OK;}; 00172 virtual err_t connected(err_t err); 00173 virtual err_t recv(struct pbuf *q, err_t err); 00174 virtual void dnsreply(const char *hostname, struct ip_addr *ipaddr); 00175 unsigned int make(const char *); 00176 00177 char *_auth; 00178 bool _ready; 00179 char _mode; 00180 char _state; 00181 int _timeout; 00182 const char *_host; 00183 const char *_path; 00184 void *_result; 00185 void *_data; 00186 const char *_request; 00187 const char *_headerfields; 00188 unsigned int _hostlen; 00189 unsigned int _resultoff; 00190 unsigned int _resultleft; 00191 int _timeout_max; 00192 }; 00193 00194 #endif
Generated on Wed Jul 13 2022 07:13:43 by
1.7.2