Joe Danlloh / lwip

Dependencies:   mbed

Fork of lwip by mbed unsupported

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPClient.h Source File

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