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.
Diff: HTTPSClient.cpp
- Revision:
- 2:6d7bc51cc77b
- Parent:
- 1:95f92eed4e09
- Child:
- 3:18af58231990
--- a/HTTPSClient.cpp Thu Sep 05 10:33:20 2013 +0000 +++ b/HTTPSClient.cpp Thu Sep 05 14:28:29 2013 +0000 @@ -4,7 +4,7 @@ #include <string.h> HTTPSClient::HTTPSClient(): -_con() + _con() { } @@ -13,20 +13,19 @@ if(_con.is_connected()) return false; - return _con.connect(host.c_str()) == 0; + return _con.connect(host.c_str()); } std::string HTTPSClient::readLine() { std::string line; char c; - _con.receive_all(&c, 1); - while(c != '\r') - { + _con.receive(&c, 1); + while(c != '\r') { line += c; - _con.receive_all(&c, 1); + _con.receive(&c, 1); } - _con.receive_all(&c, 1); // skip \n + _con.receive(&c, 1); // skip \n return line; } @@ -35,14 +34,13 @@ HTTPHeader hdr; std::string line = readLine(); sscanf(line.c_str(), "HTTP/1.1 %d OK", &hdr._status); - do - { + do { if(!line.compare(0,strlen("Content-Length"), "Content-Length")) sscanf(line.c_str(), "Content-Length: %d", &hdr._bodyLength); else if(!line.compare(0,strlen("content-length"), "content-length")) sscanf(line.c_str(), "content-length: %d", &hdr._bodyLength); line = readLine(); - }while(line.size()); + } while(line.size()); return hdr; } @@ -50,12 +48,12 @@ { if(!_con.is_connected()) return -1; - + const std::string &request = HTTPHeader::getRequest(path, _con.get_address(), 443); - + if(_con.send_all((char*)request.c_str(), request.size()+1) != request.size()+1) return -1; - + *hdr = readHeader(); return hdr->_status == HTTP_OK ? 0 : -1; } @@ -64,29 +62,27 @@ { if(!_con.is_connected()) return -1; - - if(hdr != NULL) - { + + if(hdr != NULL) { const std::string &request = HTTPHeader::getRequest(path, _con.get_address(), 443); if(_con.send_all((char*)request.c_str(), request.size()+1) != request.size()+1) return -1; *hdr = readHeader(); if(hdr->_status != HTTP_OK) return -1; - + if(hdr->_bodyLength > 0) - return _con.receive_all(data, hdr->_bodyLength > length ? length : hdr->_bodyLength); + return _con.receive(data, hdr->_bodyLength > length ? length : hdr->_bodyLength); return 0; - } - else - return _con.receive_all(data, length); + } else + return _con.receive(data, length); } bool HTTPSClient::disconnect() { if(!_con.is_connected()) return true; - + return _con.close() == 0; }