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.
Revision 0:1abc65a0f50b, committed 2013-09-16
- Comitter:
- feb11
- Date:
- Mon Sep 16 10:39:01 2013 +0000
- Commit message:
- initial import
Changed in this revision
diff -r 000000000000 -r 1abc65a0f50b HTTPHeader.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPHeader.cpp Mon Sep 16 10:39:01 2013 +0000 @@ -0,0 +1,31 @@ +#include "HTTPHeader.h" + +HTTPHeader::HTTPHeader(HTTPStatus status): + _status(status), + _bodyLength(0) +{ +} + +std::string HTTPHeader::getRequest(const std::string &path, const std::string &host, const int port) +{ + + std::string request = "GET "; + request += path; + request += " HTTP/1.0\r\nHost: "; + request += host; + request += ":"; + request += port; + request += "\r\n\r\n"; + return request; +} + +HTTPStatus HTTPHeader::getStatus() const +{ + return _status; +} + +int HTTPHeader::getBodyLength() const +{ + return _bodyLength; +} +
diff -r 000000000000 -r 1abc65a0f50b HTTPHeader.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPHeader.h Mon Sep 16 10:39:01 2013 +0000 @@ -0,0 +1,28 @@ +#ifndef HTTP_HEADER_H +#define HTTP_HEADER_H + +#include <string> +#include "HTTPStatus.h" + +class HTTPSClient; + +class HTTPHeader +{ +public : + + friend class HTTPSClient; + + HTTPHeader(HTTPStatus status = HTTP_INVALID); + + static std::string getRequest(const std::string &path, const std::string &host, const int port); + + HTTPStatus getStatus() const; + int getBodyLength() const; +private : + + HTTPStatus _status; + int _bodyLength; +}; + +#endif +
diff -r 000000000000 -r 1abc65a0f50b HTTPSClient.cpp --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPSClient.cpp Mon Sep 16 10:39:01 2013 +0000 @@ -0,0 +1,88 @@ +#include "HTTPSClient.h" +#include "HTTPHeader.h" +#include <stdio.h> +#include <string.h> + +HTTPSClient::HTTPSClient(): + _con() +{ +} + +bool HTTPSClient::connect(const std::string& host) +{ + if(_con.is_connected()) + return false; + + return _con.connect(host.c_str()); +} + +std::string HTTPSClient::readLine() +{ + std::string line; + char c; + _con.receive(&c, 1); + while(c != '\r') { + line += c; + _con.receive(&c, 1); + } + _con.receive(&c, 1); // skip \n + return line; +} + +HTTPHeader HTTPSClient::readHeader() +{ + HTTPHeader hdr; + std::string line = readLine(); + sscanf(line.c_str(), "HTTP/1.%*d %d OK", &hdr._status); + 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()); + return hdr; +} + +int HTTPSClient::get(const std::string& path, HTTPHeader *hdr) +{ + 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; +} + +int HTTPSClient::get(const std::string& path, HTTPHeader *hdr, char *data, int length) +{ + if(!_con.is_connected()) + return -1; + + 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(data, hdr->_bodyLength > length ? length : hdr->_bodyLength); + + return 0; + } else + return _con.receive(data, length); +} + +bool HTTPSClient::disconnect() +{ + if(!_con.is_connected()) + return true; + + return _con.close() == 0; +}
diff -r 000000000000 -r 1abc65a0f50b HTTPSClient.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPSClient.h Mon Sep 16 10:39:01 2013 +0000 @@ -0,0 +1,87 @@ +#ifndef HTTPS_CLIENT_H +#define HTTPS_CLIENT_H + +#include <string> +#include "TLSConnection.h" +#include "HTTPHeader.h" + + +/** Simple wrapper of TLS library to send GET request to + a server and receive some web pages. +*/ +class HTTPSClient +{ +public : + + HTTPSClient(); + + /** Connnect to the given host. + + \param host A valid hostname (e.g. "mbed.org") + \return True if the connection was established with + success, false otherwise. + + \note If the client is already connected, it returns false. + */ + bool connect(const std::string& host); + + /** Send a GET request to the host. + + \param path + \param hdr A pointer to an HTTPHeader object. + \return Returns 0 if the request was sent with success, + or -1 if an error occured. + */ + int get(const std::string& path, HTTPHeader *hdr); + + /** Send a get request and reads (partially) the body + + \param path + \param hdr A pointer to an HTTPHeader object. + \param data A pointer to some buffer + \param length Maximum number of bytes to read + \return Number of bytes stored in data in range 0..length, or -1 + if an error occured. + + \note To check whether this request is sent with success, you must check + the status of the HTTPHeader and the value of the integer returned by this + function. + + Example: + @code + // Assuming that an HTTPSClient object c is connected to a host + HTTPHeader hdr; + char buffer[256]; + int n = c.get("/", &hdr, buffer, 256); + if(n > 0 && hdr.getStatus() == HTTP_OK) // GET request sent with success + { + // do some stuff here.. + } + else + printf("Failed to send get request\n"); + @endcode + + + \note If hdr is null, this function does not send anything and directly writes + bytes in data. This is particularly useful if you expect large answers (such as + webpages) from the host. + */ + int get(const std::string& path, HTTPHeader *hdr, char *data, int length); + + /** Disconnect from host + + \return True if the client disconnected with success, false otherwise. + */ + bool disconnect(); + +private : + + std::string readLine(); + HTTPHeader readHeader(); + + TLSConnection _con; + +}; + +#endif +
diff -r 000000000000 -r 1abc65a0f50b HTTPStatus.h --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPStatus.h Mon Sep 16 10:39:01 2013 +0000 @@ -0,0 +1,17 @@ +#ifndef HTTP_STATUS_H +#define HTTP_STATUS_H + + +enum HTTPStatus { + HTTP_OK = 200, + HTTP_BAD_REQUEST = 400, + HTTP_UNAUTHORIZED, + HTTP_FORBIDDEN = 403, + HTTP_NOT_FOUND, + HTTP_INTERNAL_ERROR = 500, + HTTP_INVALID = 0 +}; + + +#endif +