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 #include "HTTPSClient.h"
feb11 0:1abc65a0f50b 2 #include "HTTPHeader.h"
feb11 0:1abc65a0f50b 3 #include <stdio.h>
feb11 0:1abc65a0f50b 4 #include <string.h>
feb11 0:1abc65a0f50b 5
feb11 0:1abc65a0f50b 6 HTTPSClient::HTTPSClient():
feb11 0:1abc65a0f50b 7 _con()
feb11 0:1abc65a0f50b 8 {
feb11 0:1abc65a0f50b 9 }
feb11 0:1abc65a0f50b 10
feb11 0:1abc65a0f50b 11 bool HTTPSClient::connect(const std::string& host)
feb11 0:1abc65a0f50b 12 {
feb11 0:1abc65a0f50b 13 if(_con.is_connected())
feb11 0:1abc65a0f50b 14 return false;
feb11 0:1abc65a0f50b 15
feb11 0:1abc65a0f50b 16 return _con.connect(host.c_str());
feb11 0:1abc65a0f50b 17 }
feb11 0:1abc65a0f50b 18
feb11 0:1abc65a0f50b 19 std::string HTTPSClient::readLine()
feb11 0:1abc65a0f50b 20 {
feb11 0:1abc65a0f50b 21 std::string line;
feb11 0:1abc65a0f50b 22 char c;
feb11 0:1abc65a0f50b 23 _con.receive(&c, 1);
feb11 0:1abc65a0f50b 24 while(c != '\r') {
feb11 0:1abc65a0f50b 25 line += c;
feb11 0:1abc65a0f50b 26 _con.receive(&c, 1);
feb11 0:1abc65a0f50b 27 }
feb11 0:1abc65a0f50b 28 _con.receive(&c, 1); // skip \n
feb11 0:1abc65a0f50b 29 return line;
feb11 0:1abc65a0f50b 30 }
feb11 0:1abc65a0f50b 31
feb11 0:1abc65a0f50b 32 HTTPHeader HTTPSClient::readHeader()
feb11 0:1abc65a0f50b 33 {
feb11 0:1abc65a0f50b 34 HTTPHeader hdr;
feb11 0:1abc65a0f50b 35 std::string line = readLine();
feb11 0:1abc65a0f50b 36 sscanf(line.c_str(), "HTTP/1.%*d %d OK", &hdr._status);
feb11 0:1abc65a0f50b 37 do {
feb11 0:1abc65a0f50b 38 if(!line.compare(0,strlen("Content-Length"), "Content-Length"))
feb11 0:1abc65a0f50b 39 sscanf(line.c_str(), "Content-Length: %d", &hdr._bodyLength);
feb11 0:1abc65a0f50b 40 else if(!line.compare(0,strlen("content-length"), "content-length"))
feb11 0:1abc65a0f50b 41 sscanf(line.c_str(), "content-length: %d", &hdr._bodyLength);
feb11 0:1abc65a0f50b 42 line = readLine();
feb11 0:1abc65a0f50b 43 } while(line.size());
feb11 0:1abc65a0f50b 44 return hdr;
feb11 0:1abc65a0f50b 45 }
feb11 0:1abc65a0f50b 46
feb11 0:1abc65a0f50b 47 int HTTPSClient::get(const std::string& path, HTTPHeader *hdr)
feb11 0:1abc65a0f50b 48 {
feb11 0:1abc65a0f50b 49 if(!_con.is_connected())
feb11 0:1abc65a0f50b 50 return -1;
feb11 0:1abc65a0f50b 51
feb11 0:1abc65a0f50b 52 const std::string &request = HTTPHeader::getRequest(path, _con.get_address(), 443);
feb11 0:1abc65a0f50b 53
feb11 0:1abc65a0f50b 54 if(_con.send_all((char*)request.c_str(), request.size()+1) != request.size()+1)
feb11 0:1abc65a0f50b 55 return -1;
feb11 0:1abc65a0f50b 56
feb11 0:1abc65a0f50b 57 *hdr = readHeader();
feb11 0:1abc65a0f50b 58 return hdr->_status == HTTP_OK ? 0 : -1;
feb11 0:1abc65a0f50b 59 }
feb11 0:1abc65a0f50b 60
feb11 0:1abc65a0f50b 61 int HTTPSClient::get(const std::string& path, HTTPHeader *hdr, char *data, int length)
feb11 0:1abc65a0f50b 62 {
feb11 0:1abc65a0f50b 63 if(!_con.is_connected())
feb11 0:1abc65a0f50b 64 return -1;
feb11 0:1abc65a0f50b 65
feb11 0:1abc65a0f50b 66 if(hdr != NULL) {
feb11 0:1abc65a0f50b 67 const std::string &request = HTTPHeader::getRequest(path, _con.get_address(), 443);
feb11 0:1abc65a0f50b 68 if(_con.send_all((char*)request.c_str(), request.size()+1) != request.size()+1)
feb11 0:1abc65a0f50b 69 return -1;
feb11 0:1abc65a0f50b 70 *hdr = readHeader();
feb11 0:1abc65a0f50b 71 if(hdr->_status != HTTP_OK)
feb11 0:1abc65a0f50b 72 return -1;
feb11 0:1abc65a0f50b 73
feb11 0:1abc65a0f50b 74 if(hdr->_bodyLength > 0)
feb11 0:1abc65a0f50b 75 return _con.receive(data, hdr->_bodyLength > length ? length : hdr->_bodyLength);
feb11 0:1abc65a0f50b 76
feb11 0:1abc65a0f50b 77 return 0;
feb11 0:1abc65a0f50b 78 } else
feb11 0:1abc65a0f50b 79 return _con.receive(data, length);
feb11 0:1abc65a0f50b 80 }
feb11 0:1abc65a0f50b 81
feb11 0:1abc65a0f50b 82 bool HTTPSClient::disconnect()
feb11 0:1abc65a0f50b 83 {
feb11 0:1abc65a0f50b 84 if(!_con.is_connected())
feb11 0:1abc65a0f50b 85 return true;
feb11 0:1abc65a0f50b 86
feb11 0:1abc65a0f50b 87 return _con.close() == 0;
feb11 0:1abc65a0f50b 88 }