summaryrefslogtreecommitdiff
path: root/tool/mbed/mbed-sdk/libraries/net/https/HTTPSClient.cpp
blob: 76eae5b4a9747300d840809fc489b87de77f9eb3 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
#include "HTTPSClient.h"
#include "HTTPHeader.h"
#include <string>
#include <cstring>
#include "mbed.h"
#include <stdlib.h>
#include <stdio.h>

using std::memset;
using std::memcpy;    
using std::string;

const static int HTTPS_PORT = 443;
char buf[256];

HTTPSClient::HTTPSClient() :
        _is_connected(false),
        _ssl_ctx(),
        _ssl(),
        _host() {
}

HTTPSClient::~HTTPSClient() {
    close();
}

int HTTPSClient::connect(const char* host) {
    if (init_socket(SOCK_STREAM) < 0)
        return -1;
    
    if (set_address(host, HTTPS_PORT) != 0)
        return -1;
    
    if (lwip_connect(_sock_fd, (const struct sockaddr *) &_remoteHost, sizeof(_remoteHost)) < 0) {
        close();
        return -1;
    }

    if(ssl_ctx_new(&_ssl_ctx, SSL_SERVER_VERIFY_LATER, SSL_DEFAULT_CLNT_SESS) != &_ssl_ctx)
        return -1;

    _ssl.ssl_ctx = &_ssl_ctx;
    
    if(ssl_client_new(&_ssl, _sock_fd, NULL, 0) == NULL)
    {
        close();
        return -1;
    }
    if(_ssl.hs_status != SSL_OK)
    {
        close();
        return -1;
    }
    
    _is_connected = true;
    _host = host;
    return 0;
}

bool HTTPSClient::is_connected(void) {
    return _is_connected;
}

int HTTPSClient::send(char* data, int length) {
    if ((_sock_fd < 0) || !_is_connected)
        return -1;
            
    return ssl_write(&_ssl, (uint8_t*)data, length);
}



HTTPHeader HTTPSClient::get(char *request)
{
    if((_sock_fd < 0) || !_is_connected)
        return HTTPHeader();
        
    sprintf(buf, "GET %s HTTP/1.1\r\nHost: %s\r\n\r\n", request, _host.c_str());
    printf("buf=%s\n", buf);
    if(send(buf, strlen(buf)) != strlen(buf))
        return HTTPHeader();
    printf("Finished sending request\n");
    return read_header();
}


HTTPHeader HTTPSClient::read_header()
{
    _ssl.bm_read_index = 0;
    _ssl.bm_index = 0;
    HTTPHeader hdr;
    if(read_line())
        return hdr;
    
    int status;
    
    if(sscanf(buf, "HTTP/%*d.%*d %d %*s", &status) == -1)
        return hdr;
    
    if(status == 200)
        hdr._status = HTTP_OK;
   if(read_line())
        return hdr;
    do
    {
        string tmp(buf);
        std::size_t sep = tmp.find(':');
        string name = tmp.substr(0, sep);
        string value = tmp.substr(sep+2, tmp.size());
        hdr._fields[name] = value;
        if(read_line())
            return hdr;
    }while(strlen(buf));
    
    return hdr;
}

uint8_t HTTPSClient::read_line()
{
    int index = 0;
    do
    {
        if(ssl_read(&_ssl, (uint8_t*)(&buf[index]), 1) != 1)
        {
            return 1;
        }
        index++;
    }while(buf[index-1] != '\r' && index < 256);
    ssl_read(&_ssl, (uint8_t*)(&buf[index-1]), 1);  // skip '\n'
    buf[index-1] = '\0';
    
    return 0;
}

// -1:error
// otherwise return nb of characters read. Cannot be > than len
int HTTPSClient::read(char *data, int len)
{
    return ssl_read(&_ssl, (uint8_t*)data, len);
}
/*
    0    : must close connection
    -1   : error
    else : get data

int HTTPSClient::receive(char* data, int length) {
    if ((_sock_fd < 0) || !_is_connected)
        return -1;
  
    if(read_record(&_ssl) < 0)
        return -1;
    return process_data(&_ssl, (uint8_t*)data, length);
}
*/
void HTTPSClient::close()
{
    if(!_is_connected)
        return;
    ssl_ctx_free(_ssl.ssl_ctx);
    Socket::close();
    _is_connected = false;
    _host.clear();
}