joey shelton / LED_Demo

Dependencies:   MAX44000 PWM_Tone_Library nexpaq_mdk

Fork of LED_Demo by Maxim nexpaq

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers HTTPSClient.cpp Source File

HTTPSClient.cpp

00001 #include "HTTPSClient.h"
00002 #include "HTTPHeader.h"
00003 #include <string>
00004 #include <cstring>
00005 #include "mbed.h"
00006 #include <stdlib.h>
00007 #include <stdio.h>
00008 
00009 using std::memset;
00010 using std::memcpy;    
00011 using std::string;
00012 
00013 const static int HTTPS_PORT = 443;
00014 char buf[256];
00015 
00016 HTTPSClient::HTTPSClient() :
00017         _is_connected(false),
00018         _ssl_ctx(),
00019         _ssl(),
00020         _host() {
00021 }
00022 
00023 HTTPSClient::~HTTPSClient() {
00024     close();
00025 }
00026 
00027 int HTTPSClient::connect(const char* host) {
00028     if (init_socket(SOCK_STREAM) < 0)
00029         return -1;
00030     
00031     if (set_address(host, HTTPS_PORT) != 0)
00032         return -1;
00033     
00034     if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
00035         close();
00036         return -1;
00037     }
00038 
00039     if(ssl_ctx_new(&_ssl_ctx, SSL_SERVER_VERIFY_LATER, SSL_DEFAULT_CLNT_SESS) != &_ssl_ctx)
00040         return -1;
00041 
00042     _ssl.ssl_ctx = &_ssl_ctx;
00043     
00044     if(ssl_client_new(&_ssl, _sock_fd, NULL, 0) == NULL)
00045     {
00046         close();
00047         return -1;
00048     }
00049     if(_ssl.hs_status != SSL_OK)
00050     {
00051         close();
00052         return -1;
00053     }
00054     
00055     _is_connected = true;
00056     _host = host;
00057     return 0;
00058 }
00059 
00060 bool HTTPSClient::is_connected(void) {
00061     return _is_connected;
00062 }
00063 
00064 int HTTPSClient::send(char* data, int length) {
00065     if ((_sock_fd < 0) || !_is_connected)
00066         return -1;
00067             
00068     return ssl_write(&_ssl, (uint8_t*)data, length);
00069 }
00070 
00071 
00072 
00073 HTTPHeader HTTPSClient::get(char *request)
00074 {
00075     if((_sock_fd < 0) || !_is_connected)
00076         return HTTPHeader();
00077         
00078     sprintf(buf, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", request, _host.c_str());
00079     printf("buf=%s\n", buf);
00080     if(send(buf, strlen(buf)) != strlen(buf))
00081         return HTTPHeader();
00082     printf("Finished sending request\n");
00083     return read_header();
00084 }
00085 
00086 
00087 HTTPHeader HTTPSClient::read_header()
00088 {
00089     _ssl.bm_read_index = 0;
00090     _ssl.bm_index = 0;
00091     HTTPHeader hdr;
00092     if(read_line())
00093         return hdr;
00094     
00095     int status;
00096     
00097     if(sscanf(buf, "HTTP/%*d.%*d %d %*s", &status) == -1)
00098         return hdr;
00099     
00100     if(status == 200)
00101         hdr._status = HTTP_OK;
00102    if(read_line())
00103         return hdr;
00104     do
00105     {
00106         string tmp(buf);
00107         std::size_t sep = tmp.find(':');
00108         string name = tmp.substr(0, sep);
00109         string value = tmp.substr(sep+2, tmp.size());
00110         hdr._fields[name] = value;
00111         if(read_line())
00112             return hdr;
00113     }while(strlen(buf));
00114     
00115     return hdr;
00116 }
00117 
00118 uint8_t HTTPSClient::read_line()
00119 {
00120     int index = 0;
00121     do
00122     {
00123         if(ssl_read(&_ssl, (uint8_t*)(&buf[index]), 1) != 1)
00124         {
00125             return 1;
00126         }
00127         index++;
00128     }while(buf[index-1] != '\r' && index < 256);
00129     ssl_read(&_ssl, (uint8_t*)(&buf[index-1]), 1);  // skip '\n'
00130     buf[index-1] = '\0';
00131     
00132     return 0;
00133 }
00134 
00135 // -1:error
00136 // otherwise return nb of characters read. Cannot be > than len
00137 int HTTPSClient::read(char *data, int len)
00138 {
00139     return ssl_read(&_ssl, (uint8_t*)data, len);
00140 }
00141 /*
00142     0    : must close connection
00143     -1   : error
00144     else : get data
00145 
00146 int HTTPSClient::receive(char* data, int length) {
00147     if ((_sock_fd < 0) || !_is_connected)
00148         return -1;
00149   
00150     if(read_record(&_ssl) < 0)
00151         return -1;
00152     return process_data(&_ssl, (uint8_t*)data, length);
00153 }
00154 */
00155 void HTTPSClient::close()
00156 {
00157     if(!_is_connected)
00158         return;
00159     ssl_ctx_free(_ssl.ssl_ctx);
00160     Socket::close();
00161     _is_connected = false;
00162     _host.clear();
00163 }