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.
Dependents: HTTPSClientExample
Revision 0:ab9011f6ede5, committed 2013-09-04
- Comitter:
- feb11
- Date:
- Wed Sep 04 13:24:29 2013 +0000
- Child:
- 1:95f92eed4e09
- Commit message:
- initial import
Changed in this revision
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPHeader.cpp Wed Sep 04 13:24:29 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.1\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;
+}
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPHeader.h Wed Sep 04 13:24:29 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
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPSClient.cpp Wed Sep 04 13:24:29 2013 +0000
@@ -0,0 +1,90 @@
+#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()) == 0;
+}
+
+std::string HTTPSClient::readLine()
+{
+ std::string line;
+ char c;
+ _con.receive_all(&c, 1);
+ while(c != '\r')
+ {
+ line += c;
+ _con.receive_all(&c, 1);
+ }
+ _con.receive_all(&c, 1); // skip \n
+ return line;
+}
+
+HTTPHeader HTTPSClient::readHeader()
+{
+ HTTPHeader hdr;
+ std::string line = readLine();
+ sscanf(line.c_str(), "HTTP/1.1 %d OK", &hdr._status);
+ do
+ {
+ 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_all(data, hdr->_bodyLength > length ? length : hdr->_bodyLength);
+
+ return 0;
+ }
+ else
+ return _con.receive_all(data, length);
+}
+
+bool HTTPSClient::disconnect()
+{
+ if(!_con.is_connected())
+ return true;
+
+ return _con.close() == 0;
+}
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPSClient.h Wed Sep 04 13:24:29 2013 +0000
@@ -0,0 +1,30 @@
+#ifndef HTTPS_CLIENT_H
+#define HTTPS_CLIENT_H
+
+#include <string>
+#include "TLSConnection.h"
+#include "HTTPStatus.h"
+#include "HTTPHeader.h"
+
+class HTTPSClient
+{
+ public :
+
+ HTTPSClient();
+
+ bool connect(const std::string& host);
+ int get(const std::string& path, HTTPHeader *hdr);
+ int get(const std::string& path, HTTPHeader *hdr, char *data, int length);
+ bool disconnect();
+
+ private :
+
+ std::string readLine();
+ HTTPHeader readHeader();
+
+ TLSConnection _con;
+
+};
+
+#endif
+
--- /dev/null Thu Jan 01 00:00:00 1970 +0000
+++ b/HTTPStatus.h Wed Sep 04 13:24:29 2013 +0000
@@ -0,0 +1,18 @@
+#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
+