dhgdh
Dependencies: MAX44000 PWM_Tone_Library nexpaq_mdk
Fork of LED_Demo by
mbd_os/libraries/net/https/HTTPSClient.cpp@1:55a6170b404f, 2016-09-17 (annotated)
- Committer:
- nexpaq
- Date:
- Sat Sep 17 16:32:05 2016 +0000
- Revision:
- 1:55a6170b404f
checking in for sharing
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
nexpaq | 1:55a6170b404f | 1 | #include "HTTPSClient.h" |
nexpaq | 1:55a6170b404f | 2 | #include "HTTPHeader.h" |
nexpaq | 1:55a6170b404f | 3 | #include <string> |
nexpaq | 1:55a6170b404f | 4 | #include <cstring> |
nexpaq | 1:55a6170b404f | 5 | #include "mbed.h" |
nexpaq | 1:55a6170b404f | 6 | #include <stdlib.h> |
nexpaq | 1:55a6170b404f | 7 | #include <stdio.h> |
nexpaq | 1:55a6170b404f | 8 | |
nexpaq | 1:55a6170b404f | 9 | using std::memset; |
nexpaq | 1:55a6170b404f | 10 | using std::memcpy; |
nexpaq | 1:55a6170b404f | 11 | using std::string; |
nexpaq | 1:55a6170b404f | 12 | |
nexpaq | 1:55a6170b404f | 13 | const static int HTTPS_PORT = 443; |
nexpaq | 1:55a6170b404f | 14 | char buf[256]; |
nexpaq | 1:55a6170b404f | 15 | |
nexpaq | 1:55a6170b404f | 16 | HTTPSClient::HTTPSClient() : |
nexpaq | 1:55a6170b404f | 17 | _is_connected(false), |
nexpaq | 1:55a6170b404f | 18 | _ssl_ctx(), |
nexpaq | 1:55a6170b404f | 19 | _ssl(), |
nexpaq | 1:55a6170b404f | 20 | _host() { |
nexpaq | 1:55a6170b404f | 21 | } |
nexpaq | 1:55a6170b404f | 22 | |
nexpaq | 1:55a6170b404f | 23 | HTTPSClient::~HTTPSClient() { |
nexpaq | 1:55a6170b404f | 24 | close(); |
nexpaq | 1:55a6170b404f | 25 | } |
nexpaq | 1:55a6170b404f | 26 | |
nexpaq | 1:55a6170b404f | 27 | int HTTPSClient::connect(const char* host) { |
nexpaq | 1:55a6170b404f | 28 | if (init_socket(SOCK_STREAM) < 0) |
nexpaq | 1:55a6170b404f | 29 | return -1; |
nexpaq | 1:55a6170b404f | 30 | |
nexpaq | 1:55a6170b404f | 31 | if (set_address(host, HTTPS_PORT) != 0) |
nexpaq | 1:55a6170b404f | 32 | return -1; |
nexpaq | 1:55a6170b404f | 33 | |
nexpaq | 1:55a6170b404f | 34 | if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) { |
nexpaq | 1:55a6170b404f | 35 | close(); |
nexpaq | 1:55a6170b404f | 36 | return -1; |
nexpaq | 1:55a6170b404f | 37 | } |
nexpaq | 1:55a6170b404f | 38 | |
nexpaq | 1:55a6170b404f | 39 | if(ssl_ctx_new(&_ssl_ctx, SSL_SERVER_VERIFY_LATER, SSL_DEFAULT_CLNT_SESS) != &_ssl_ctx) |
nexpaq | 1:55a6170b404f | 40 | return -1; |
nexpaq | 1:55a6170b404f | 41 | |
nexpaq | 1:55a6170b404f | 42 | _ssl.ssl_ctx = &_ssl_ctx; |
nexpaq | 1:55a6170b404f | 43 | |
nexpaq | 1:55a6170b404f | 44 | if(ssl_client_new(&_ssl, _sock_fd, NULL, 0) == NULL) |
nexpaq | 1:55a6170b404f | 45 | { |
nexpaq | 1:55a6170b404f | 46 | close(); |
nexpaq | 1:55a6170b404f | 47 | return -1; |
nexpaq | 1:55a6170b404f | 48 | } |
nexpaq | 1:55a6170b404f | 49 | if(_ssl.hs_status != SSL_OK) |
nexpaq | 1:55a6170b404f | 50 | { |
nexpaq | 1:55a6170b404f | 51 | close(); |
nexpaq | 1:55a6170b404f | 52 | return -1; |
nexpaq | 1:55a6170b404f | 53 | } |
nexpaq | 1:55a6170b404f | 54 | |
nexpaq | 1:55a6170b404f | 55 | _is_connected = true; |
nexpaq | 1:55a6170b404f | 56 | _host = host; |
nexpaq | 1:55a6170b404f | 57 | return 0; |
nexpaq | 1:55a6170b404f | 58 | } |
nexpaq | 1:55a6170b404f | 59 | |
nexpaq | 1:55a6170b404f | 60 | bool HTTPSClient::is_connected(void) { |
nexpaq | 1:55a6170b404f | 61 | return _is_connected; |
nexpaq | 1:55a6170b404f | 62 | } |
nexpaq | 1:55a6170b404f | 63 | |
nexpaq | 1:55a6170b404f | 64 | int HTTPSClient::send(char* data, int length) { |
nexpaq | 1:55a6170b404f | 65 | if ((_sock_fd < 0) || !_is_connected) |
nexpaq | 1:55a6170b404f | 66 | return -1; |
nexpaq | 1:55a6170b404f | 67 | |
nexpaq | 1:55a6170b404f | 68 | return ssl_write(&_ssl, (uint8_t*)data, length); |
nexpaq | 1:55a6170b404f | 69 | } |
nexpaq | 1:55a6170b404f | 70 | |
nexpaq | 1:55a6170b404f | 71 | |
nexpaq | 1:55a6170b404f | 72 | |
nexpaq | 1:55a6170b404f | 73 | HTTPHeader HTTPSClient::get(char *request) |
nexpaq | 1:55a6170b404f | 74 | { |
nexpaq | 1:55a6170b404f | 75 | if((_sock_fd < 0) || !_is_connected) |
nexpaq | 1:55a6170b404f | 76 | return HTTPHeader(); |
nexpaq | 1:55a6170b404f | 77 | |
nexpaq | 1:55a6170b404f | 78 | sprintf(buf, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", request, _host.c_str()); |
nexpaq | 1:55a6170b404f | 79 | printf("buf=%s\n", buf); |
nexpaq | 1:55a6170b404f | 80 | if(send(buf, strlen(buf)) != strlen(buf)) |
nexpaq | 1:55a6170b404f | 81 | return HTTPHeader(); |
nexpaq | 1:55a6170b404f | 82 | printf("Finished sending request\n"); |
nexpaq | 1:55a6170b404f | 83 | return read_header(); |
nexpaq | 1:55a6170b404f | 84 | } |
nexpaq | 1:55a6170b404f | 85 | |
nexpaq | 1:55a6170b404f | 86 | |
nexpaq | 1:55a6170b404f | 87 | HTTPHeader HTTPSClient::read_header() |
nexpaq | 1:55a6170b404f | 88 | { |
nexpaq | 1:55a6170b404f | 89 | _ssl.bm_read_index = 0; |
nexpaq | 1:55a6170b404f | 90 | _ssl.bm_index = 0; |
nexpaq | 1:55a6170b404f | 91 | HTTPHeader hdr; |
nexpaq | 1:55a6170b404f | 92 | if(read_line()) |
nexpaq | 1:55a6170b404f | 93 | return hdr; |
nexpaq | 1:55a6170b404f | 94 | |
nexpaq | 1:55a6170b404f | 95 | int status; |
nexpaq | 1:55a6170b404f | 96 | |
nexpaq | 1:55a6170b404f | 97 | if(sscanf(buf, "HTTP/%*d.%*d %d %*s", &status) == -1) |
nexpaq | 1:55a6170b404f | 98 | return hdr; |
nexpaq | 1:55a6170b404f | 99 | |
nexpaq | 1:55a6170b404f | 100 | if(status == 200) |
nexpaq | 1:55a6170b404f | 101 | hdr._status = HTTP_OK; |
nexpaq | 1:55a6170b404f | 102 | if(read_line()) |
nexpaq | 1:55a6170b404f | 103 | return hdr; |
nexpaq | 1:55a6170b404f | 104 | do |
nexpaq | 1:55a6170b404f | 105 | { |
nexpaq | 1:55a6170b404f | 106 | string tmp(buf); |
nexpaq | 1:55a6170b404f | 107 | std::size_t sep = tmp.find(':'); |
nexpaq | 1:55a6170b404f | 108 | string name = tmp.substr(0, sep); |
nexpaq | 1:55a6170b404f | 109 | string value = tmp.substr(sep+2, tmp.size()); |
nexpaq | 1:55a6170b404f | 110 | hdr._fields[name] = value; |
nexpaq | 1:55a6170b404f | 111 | if(read_line()) |
nexpaq | 1:55a6170b404f | 112 | return hdr; |
nexpaq | 1:55a6170b404f | 113 | }while(strlen(buf)); |
nexpaq | 1:55a6170b404f | 114 | |
nexpaq | 1:55a6170b404f | 115 | return hdr; |
nexpaq | 1:55a6170b404f | 116 | } |
nexpaq | 1:55a6170b404f | 117 | |
nexpaq | 1:55a6170b404f | 118 | uint8_t HTTPSClient::read_line() |
nexpaq | 1:55a6170b404f | 119 | { |
nexpaq | 1:55a6170b404f | 120 | int index = 0; |
nexpaq | 1:55a6170b404f | 121 | do |
nexpaq | 1:55a6170b404f | 122 | { |
nexpaq | 1:55a6170b404f | 123 | if(ssl_read(&_ssl, (uint8_t*)(&buf[index]), 1) != 1) |
nexpaq | 1:55a6170b404f | 124 | { |
nexpaq | 1:55a6170b404f | 125 | return 1; |
nexpaq | 1:55a6170b404f | 126 | } |
nexpaq | 1:55a6170b404f | 127 | index++; |
nexpaq | 1:55a6170b404f | 128 | }while(buf[index-1] != '\r' && index < 256); |
nexpaq | 1:55a6170b404f | 129 | ssl_read(&_ssl, (uint8_t*)(&buf[index-1]), 1); // skip '\n' |
nexpaq | 1:55a6170b404f | 130 | buf[index-1] = '\0'; |
nexpaq | 1:55a6170b404f | 131 | |
nexpaq | 1:55a6170b404f | 132 | return 0; |
nexpaq | 1:55a6170b404f | 133 | } |
nexpaq | 1:55a6170b404f | 134 | |
nexpaq | 1:55a6170b404f | 135 | // -1:error |
nexpaq | 1:55a6170b404f | 136 | // otherwise return nb of characters read. Cannot be > than len |
nexpaq | 1:55a6170b404f | 137 | int HTTPSClient::read(char *data, int len) |
nexpaq | 1:55a6170b404f | 138 | { |
nexpaq | 1:55a6170b404f | 139 | return ssl_read(&_ssl, (uint8_t*)data, len); |
nexpaq | 1:55a6170b404f | 140 | } |
nexpaq | 1:55a6170b404f | 141 | /* |
nexpaq | 1:55a6170b404f | 142 | 0 : must close connection |
nexpaq | 1:55a6170b404f | 143 | -1 : error |
nexpaq | 1:55a6170b404f | 144 | else : get data |
nexpaq | 1:55a6170b404f | 145 | |
nexpaq | 1:55a6170b404f | 146 | int HTTPSClient::receive(char* data, int length) { |
nexpaq | 1:55a6170b404f | 147 | if ((_sock_fd < 0) || !_is_connected) |
nexpaq | 1:55a6170b404f | 148 | return -1; |
nexpaq | 1:55a6170b404f | 149 | |
nexpaq | 1:55a6170b404f | 150 | if(read_record(&_ssl) < 0) |
nexpaq | 1:55a6170b404f | 151 | return -1; |
nexpaq | 1:55a6170b404f | 152 | return process_data(&_ssl, (uint8_t*)data, length); |
nexpaq | 1:55a6170b404f | 153 | } |
nexpaq | 1:55a6170b404f | 154 | */ |
nexpaq | 1:55a6170b404f | 155 | void HTTPSClient::close() |
nexpaq | 1:55a6170b404f | 156 | { |
nexpaq | 1:55a6170b404f | 157 | if(!_is_connected) |
nexpaq | 1:55a6170b404f | 158 | return; |
nexpaq | 1:55a6170b404f | 159 | ssl_ctx_free(_ssl.ssl_ctx); |
nexpaq | 1:55a6170b404f | 160 | Socket::close(); |
nexpaq | 1:55a6170b404f | 161 | _is_connected = false; |
nexpaq | 1:55a6170b404f | 162 | _host.clear(); |
nexpaq | 1:55a6170b404f | 163 | } |