Small wrapper of TLS_cyassl

Dependents:   HTTPSClientExample2

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPSClient.h Source File

HTTPSClient.h

00001 #ifndef HTTPS_CLIENT_H
00002 #define HTTPS_CLIENT_H
00003 
00004 #include <string>
00005 #include "TLSConnection.h"
00006 #include "HTTPHeader.h"
00007 
00008 
00009 /** Simple wrapper of TLS library to send GET request to
00010     a server and receive some web pages.
00011 */
00012 class HTTPSClient
00013 {
00014 public :
00015 
00016     HTTPSClient();
00017 
00018     /** Connnect to the given host.
00019 
00020         \param host A valid hostname (e.g. "mbed.org")
00021         \return True if the connection was established with
00022         success, false otherwise.
00023 
00024         \note If the client is already connected, it returns false.
00025     */
00026     bool connect(const std::string& host);
00027 
00028     /** Send a GET request to the host.
00029 
00030         \param path
00031         \param hdr A pointer to an HTTPHeader object.
00032         \return Returns 0 if the request was sent with success,
00033         or -1 if an error occured.
00034     */
00035     int get(const std::string& path, HTTPHeader *hdr);
00036 
00037     /** Send a get request and reads (partially) the body
00038 
00039         \param path
00040         \param hdr      A pointer to an HTTPHeader object.
00041         \param data     A pointer to some buffer
00042         \param length   Maximum number of bytes to read
00043         \return Number of bytes stored in data in range 0..length, or -1
00044         if an error occured.
00045 
00046         \note To check whether this request is sent with success, you must check
00047         the status of the HTTPHeader and the value of the integer returned by this
00048         function.
00049 
00050         Example:
00051         @code
00052         // Assuming that an HTTPSClient object c is connected to a host
00053         HTTPHeader hdr;
00054         char buffer[256];
00055         int n = c.get("/", &hdr, buffer, 256);
00056         if(n > 0 && hdr.getStatus() == HTTP_OK) // GET request sent with success
00057         {
00058             // do some stuff here..
00059         }
00060         else
00061             printf("Failed to send get request\n");
00062         @endcode
00063 
00064 
00065         \note If hdr is null, this function does not send anything and directly writes
00066         bytes in data. This is particularly useful if you expect large answers (such as
00067         webpages) from the host.
00068     */
00069     int get(const std::string& path, HTTPHeader *hdr, char *data, int length);
00070 
00071     /** Disconnect from host
00072 
00073         \return True if the client disconnected with success, false otherwise.
00074     */
00075     bool disconnect();
00076 
00077 private :
00078 
00079     std::string readLine();
00080     HTTPHeader readHeader();
00081 
00082     TLSConnection _con;
00083 
00084 };
00085 
00086 #endif
00087