wrapper of TLS library to connect to HTTPS servers

Dependents:   HTTPSClientExample

This library provides a simple interface to send GET requests over HTTPS. Notice that this library uses the axTLS library for the implementation of TLS.

Import programHTTPSClientExample

Connect to twitter.com and copies this webpage to a file.

Committer:
feb11
Date:
Thu Sep 12 09:05:02 2013 +0000
Revision:
3:18af58231990
Parent:
2:6d7bc51cc77b
fixed scanf status line

Who changed what in which revision?

UserRevisionLine numberNew contents of line
feb11 0:ab9011f6ede5 1 #ifndef HTTPS_CLIENT_H
feb11 0:ab9011f6ede5 2 #define HTTPS_CLIENT_H
feb11 0:ab9011f6ede5 3
feb11 0:ab9011f6ede5 4 #include <string>
feb11 0:ab9011f6ede5 5 #include "TLSConnection.h"
feb11 0:ab9011f6ede5 6 #include "HTTPHeader.h"
feb11 0:ab9011f6ede5 7
feb11 2:6d7bc51cc77b 8
feb11 2:6d7bc51cc77b 9 /** Simple wrapper of TLS library to send GET request to
feb11 2:6d7bc51cc77b 10 a server and receive some web pages.
feb11 2:6d7bc51cc77b 11 */
feb11 0:ab9011f6ede5 12 class HTTPSClient
feb11 0:ab9011f6ede5 13 {
feb11 2:6d7bc51cc77b 14 public :
feb11 2:6d7bc51cc77b 15
feb11 2:6d7bc51cc77b 16 HTTPSClient();
feb11 2:6d7bc51cc77b 17
feb11 2:6d7bc51cc77b 18 /** Connnect to the given host.
feb11 2:6d7bc51cc77b 19
feb11 2:6d7bc51cc77b 20 \param host A valid hostname (e.g. "mbed.org")
feb11 2:6d7bc51cc77b 21 \return True if the connection was established with
feb11 2:6d7bc51cc77b 22 success, false otherwise.
feb11 2:6d7bc51cc77b 23
feb11 2:6d7bc51cc77b 24 \note If the client is already connected, it returns false.
feb11 2:6d7bc51cc77b 25 */
feb11 2:6d7bc51cc77b 26 bool connect(const std::string& host);
feb11 2:6d7bc51cc77b 27
feb11 2:6d7bc51cc77b 28 /** Send a GET request to the host.
feb11 2:6d7bc51cc77b 29
feb11 2:6d7bc51cc77b 30 \param path
feb11 2:6d7bc51cc77b 31 \param hdr A pointer to an HTTPHeader object.
feb11 2:6d7bc51cc77b 32 \return Returns 0 if the request was sent with success,
feb11 2:6d7bc51cc77b 33 or -1 if an error occured.
feb11 2:6d7bc51cc77b 34 */
feb11 2:6d7bc51cc77b 35 int get(const std::string& path, HTTPHeader *hdr);
feb11 2:6d7bc51cc77b 36
feb11 2:6d7bc51cc77b 37 /** Send a get request and reads (partially) the body
feb11 2:6d7bc51cc77b 38
feb11 2:6d7bc51cc77b 39 \param path
feb11 2:6d7bc51cc77b 40 \param hdr A pointer to an HTTPHeader object.
feb11 2:6d7bc51cc77b 41 \param data A pointer to some buffer
feb11 2:6d7bc51cc77b 42 \param length Maximum number of bytes to read
feb11 2:6d7bc51cc77b 43 \return Number of bytes stored in data in range 0..length, or -1
feb11 2:6d7bc51cc77b 44 if an error occured.
feb11 2:6d7bc51cc77b 45
feb11 2:6d7bc51cc77b 46 \note To check whether this request is sent with success, you must check
feb11 2:6d7bc51cc77b 47 the status of the HTTPHeader and the value of the integer returned by this
feb11 2:6d7bc51cc77b 48 function.
feb11 2:6d7bc51cc77b 49
feb11 2:6d7bc51cc77b 50 Example:
feb11 2:6d7bc51cc77b 51 @code
feb11 2:6d7bc51cc77b 52 // Assuming that an HTTPSClient object c is connected to a host
feb11 2:6d7bc51cc77b 53 HTTPHeader hdr;
feb11 2:6d7bc51cc77b 54 char buffer[256];
feb11 2:6d7bc51cc77b 55 int n = c.get("/", &hdr, buffer, 256);
feb11 2:6d7bc51cc77b 56 if(n > 0 && hdr.getStatus() == HTTP_OK) // GET request sent with success
feb11 2:6d7bc51cc77b 57 {
feb11 2:6d7bc51cc77b 58 // do some stuff here..
feb11 2:6d7bc51cc77b 59 }
feb11 2:6d7bc51cc77b 60 else
feb11 2:6d7bc51cc77b 61 printf("Failed to send get request\n");
feb11 2:6d7bc51cc77b 62 @endcode
feb11 2:6d7bc51cc77b 63
feb11 2:6d7bc51cc77b 64
feb11 2:6d7bc51cc77b 65 \note If hdr is null, this function does not send anything and directly writes
feb11 2:6d7bc51cc77b 66 bytes in data. This is particularly useful if you expect large answers (such as
feb11 2:6d7bc51cc77b 67 webpages) from the host.
feb11 2:6d7bc51cc77b 68 */
feb11 2:6d7bc51cc77b 69 int get(const std::string& path, HTTPHeader *hdr, char *data, int length);
feb11 2:6d7bc51cc77b 70
feb11 2:6d7bc51cc77b 71 /** Disconnect from host
feb11 2:6d7bc51cc77b 72
feb11 2:6d7bc51cc77b 73 \return True if the client disconnected with success, false otherwise.
feb11 2:6d7bc51cc77b 74 */
feb11 2:6d7bc51cc77b 75 bool disconnect();
feb11 2:6d7bc51cc77b 76
feb11 2:6d7bc51cc77b 77 private :
feb11 2:6d7bc51cc77b 78
feb11 2:6d7bc51cc77b 79 std::string readLine();
feb11 2:6d7bc51cc77b 80 HTTPHeader readHeader();
feb11 2:6d7bc51cc77b 81
feb11 2:6d7bc51cc77b 82 TLSConnection _con;
feb11 0:ab9011f6ede5 83
feb11 0:ab9011f6ede5 84 };
feb11 0:ab9011f6ede5 85
feb11 0:ab9011f6ede5 86 #endif
feb11 0:ab9011f6ede5 87