Important changes to repositories hosted on mbed.com
Mbed hosted mercurial repositories are deprecated and are due to be permanently deleted in July 2026.
To keep a copy of this software download the repository Zip archive or clone locally using Mercurial.
It is also possible to export all your personal repositories from the account settings page.
Dependencies: EthernetInterface7 HTTPSClient_cyassl TLS_cyassl mbed-rtos mbed
Fork of HTTPSClientExample2 by
Revision 2:659d693a1a21, committed 2013-09-18
- Comitter:
- feb11
- Date:
- Wed Sep 18 12:41:31 2013 +0000
- Parent:
- 1:db560c045c62
- Commit message:
- moved HTTPS files to new library
Changed in this revision
diff -r db560c045c62 -r 659d693a1a21 HTTPHeader.cpp
--- a/HTTPHeader.cpp Mon Sep 16 09:59:26 2013 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,31 +0,0 @@
-#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 db560c045c62 -r 659d693a1a21 HTTPHeader.h
--- a/HTTPHeader.h Mon Sep 16 09:59:26 2013 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,28 +0,0 @@
-#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 db560c045c62 -r 659d693a1a21 HTTPSClient.cpp
--- a/HTTPSClient.cpp Mon Sep 16 09:59:26 2013 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,88 +0,0 @@
-#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 db560c045c62 -r 659d693a1a21 HTTPSClient.h
--- a/HTTPSClient.h Mon Sep 16 09:59:26 2013 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,87 +0,0 @@
-#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 db560c045c62 -r 659d693a1a21 HTTPSClient_cyassl.lib --- /dev/null Thu Jan 01 00:00:00 1970 +0000 +++ b/HTTPSClient_cyassl.lib Wed Sep 18 12:41:31 2013 +0000 @@ -0,0 +1,1 @@ +http://mbed.org/users/feb11/code/HTTPSClient_cyassl/#1abc65a0f50b
diff -r db560c045c62 -r 659d693a1a21 HTTPStatus.h
--- a/HTTPStatus.h Mon Sep 16 09:59:26 2013 +0000
+++ /dev/null Thu Jan 01 00:00:00 1970 +0000
@@ -1,17 +0,0 @@
-#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
-
diff -r db560c045c62 -r 659d693a1a21 TLS_cyassl.lib --- a/TLS_cyassl.lib Mon Sep 16 09:59:26 2013 +0000 +++ b/TLS_cyassl.lib Wed Sep 18 12:41:31 2013 +0000 @@ -1,1 +1,1 @@ -http://mbed.org/users/feb11/code/TLS_cyassl/#63ad554f6ca4 +https://mbed.org/users/feb11/code/TLS_cyassl/#29069dd9abfc
