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