Small wrapper of TLS_cyassl

Dependents:   HTTPSClientExample2

This is a small wrapper of TLS_cyassl to easily sends GET requests over HTTPS. This library is used in the same way as HTTPSClient_axTLS.

Import programHTTPSClientExample2

This example shows how to use the TLS_cyassl library. It connects to twitter.com and downloads a webpage.

Committer:
feb11
Date:
Mon Sep 16 10:39:01 2013 +0000
Revision:
0:1abc65a0f50b
initial import

Who changed what in which revision?

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