]>
git.gir.st - tmk_keyboard.git/blob - tmk_core/tool/mbed/mbed-sdk/libraries/net/https/HTTPSClient.cpp
1 #include "HTTPSClient.h"
2 #include "HTTPHeader.h"
13 const static int HTTPS_PORT
= 443;
16 HTTPSClient::HTTPSClient() :
23 HTTPSClient::~HTTPSClient() {
27 int HTTPSClient::connect(const char* host
) {
28 if (init_socket(SOCK_STREAM
) < 0)
31 if (set_address(host
, HTTPS_PORT
) != 0)
34 if (lwip_connect(_sock_fd
, (const struct sockaddr
*) &_remoteHost
, sizeof(_remoteHost
)) < 0) {
39 if(ssl_ctx_new(&_ssl_ctx
, SSL_SERVER_VERIFY_LATER
, SSL_DEFAULT_CLNT_SESS
) != &_ssl_ctx
)
42 _ssl
.ssl_ctx
= &_ssl_ctx
;
44 if(ssl_client_new(&_ssl
, _sock_fd
, NULL
, 0) == NULL
)
49 if(_ssl
.hs_status
!= SSL_OK
)
60 bool HTTPSClient::is_connected(void) {
64 int HTTPSClient::send(char* data
, int length
) {
65 if ((_sock_fd
< 0) || !_is_connected
)
68 return ssl_write(&_ssl
, (uint8_t*)data
, length
);
73 HTTPHeader
HTTPSClient::get(char *request
)
75 if((_sock_fd
< 0) || !_is_connected
)
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
))
82 printf("Finished sending request\n");
87 HTTPHeader
HTTPSClient::read_header()
89 _ssl
.bm_read_index
= 0;
97 if(sscanf(buf
, "HTTP/%*d.%*d %d %*s", &status
) == -1)
101 hdr
._status
= HTTP_OK
;
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
;
118 uint8_t HTTPSClient::read_line()
123 if(ssl_read(&_ssl
, (uint8_t*)(&buf
[index
]), 1) != 1)
128 }while(buf
[index
-1] != '\r' && index
< 256);
129 ssl_read(&_ssl
, (uint8_t*)(&buf
[index
-1]), 1); // skip '\n'
136 // otherwise return nb of characters read. Cannot be > than len
137 int HTTPSClient::read(char *data
, int len
)
139 return ssl_read(&_ssl
, (uint8_t*)data
, len
);
142 0 : must close connection
146 int HTTPSClient::receive(char* data, int length) {
147 if ((_sock_fd < 0) || !_is_connected)
150 if(read_record(&_ssl) < 0)
152 return process_data(&_ssl, (uint8_t*)data, length);
155 void HTTPSClient::close()
159 ssl_ctx_free(_ssl
.ssl_ctx
);
161 _is_connected
= false;