my fork
Fork of HTTPClient by
HTTPClient.cpp@18:d89df40b4cf3, 2014-04-07 (annotated)
- Committer:
- wolfSSL
- Date:
- Mon Apr 07 23:41:06 2014 +0000
- Revision:
- 18:d89df40b4cf3
- Parent:
- 17:c73d8e61d391
- Child:
- 19:1e2f05809eb1
just Indentation
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
donatien | 0:2ccb9960a044 | 1 | /* HTTPClient.cpp */ |
donatien | 10:e1351de84c16 | 2 | /* Copyright (C) 2012 mbed.org, MIT License |
donatien | 10:e1351de84c16 | 3 | * |
donatien | 10:e1351de84c16 | 4 | * Permission is hereby granted, free of charge, to any person obtaining a copy of this software |
donatien | 10:e1351de84c16 | 5 | * and associated documentation files (the "Software"), to deal in the Software without restriction, |
donatien | 10:e1351de84c16 | 6 | * including without limitation the rights to use, copy, modify, merge, publish, distribute, |
donatien | 10:e1351de84c16 | 7 | * sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is |
donatien | 10:e1351de84c16 | 8 | * furnished to do so, subject to the following conditions: |
donatien | 10:e1351de84c16 | 9 | * |
donatien | 10:e1351de84c16 | 10 | * The above copyright notice and this permission notice shall be included in all copies or |
donatien | 10:e1351de84c16 | 11 | * substantial portions of the Software. |
donatien | 10:e1351de84c16 | 12 | * |
donatien | 10:e1351de84c16 | 13 | * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING |
donatien | 10:e1351de84c16 | 14 | * BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND |
donatien | 10:e1351de84c16 | 15 | * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, |
donatien | 10:e1351de84c16 | 16 | * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, |
donatien | 10:e1351de84c16 | 17 | * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. |
donatien | 10:e1351de84c16 | 18 | */ |
donatien | 0:2ccb9960a044 | 19 | |
donatien | 7:4e39864f7b15 | 20 | //Debug is disabled by default |
donatien | 16:1f743885e7de | 21 | #if 0 |
donatien | 12:89d09a6db00a | 22 | //Enable debug |
donatien | 11:390362de8c3f | 23 | #include <cstdio> |
wolfSSL | 18:d89df40b4cf3 | 24 | #define DBG(x, ...) std::printf("[HTTPClient : DBG]"x"\r\n", ##__VA_ARGS__); |
wolfSSL | 18:d89df40b4cf3 | 25 | #define WARN(x, ...) std::printf("[HTTPClient : WARN]"x"\r\n", ##__VA_ARGS__); |
wolfSSL | 18:d89df40b4cf3 | 26 | #define ERR(x, ...) std::printf("[HTTPClient : ERR]"x"\r\n", ##__VA_ARGS__); |
donatien | 12:89d09a6db00a | 27 | |
donatien | 12:89d09a6db00a | 28 | #else |
donatien | 12:89d09a6db00a | 29 | //Disable debug |
wolfSSL | 18:d89df40b4cf3 | 30 | #define DBG(x, ...) |
donatien | 12:89d09a6db00a | 31 | #define WARN(x, ...) |
wolfSSL | 18:d89df40b4cf3 | 32 | #define ERR(x, ...) |
donatien | 12:89d09a6db00a | 33 | |
donatien | 7:4e39864f7b15 | 34 | #endif |
donatien | 0:2ccb9960a044 | 35 | |
donatien | 0:2ccb9960a044 | 36 | #define HTTP_PORT 80 |
wolfSSL | 17:c73d8e61d391 | 37 | #define HTTPS_PORT 443 |
donatien | 0:2ccb9960a044 | 38 | |
donatien | 11:390362de8c3f | 39 | #define OK 0 |
donatien | 11:390362de8c3f | 40 | |
donatien | 11:390362de8c3f | 41 | #define MIN(x,y) (((x)<(y))?(x):(y)) |
donatien | 11:390362de8c3f | 42 | #define MAX(x,y) (((x)>(y))?(x):(y)) |
donatien | 11:390362de8c3f | 43 | |
wolfSSL | 17:c73d8e61d391 | 44 | #include <cstring> |
donatien | 0:2ccb9960a044 | 45 | |
wolfSSL | 17:c73d8e61d391 | 46 | #include <../CyaSSL/cyassl/ctaocrypt/settings.h> |
wolfSSL | 17:c73d8e61d391 | 47 | #include <../CyaSSL/cyassl/ctaocrypt/types.h> |
wolfSSL | 17:c73d8e61d391 | 48 | #include <../CyaSSL/cyassl/internal.h> |
wolfSSL | 17:c73d8e61d391 | 49 | #include <../CyaSSL/cyassl/ssl.h> |
donatien | 0:2ccb9960a044 | 50 | |
donatien | 11:390362de8c3f | 51 | #include "HTTPClient.h" |
wolfSSL | 17:c73d8e61d391 | 52 | #include "TCPSocketConnection.h" |
wolfSSL | 17:c73d8e61d391 | 53 | |
wolfSSL | 18:d89df40b4cf3 | 54 | class TCPSocketConnection_fd: public TCPSocketConnection |
wolfSSL | 17:c73d8e61d391 | 55 | { |
wolfSSL | 17:c73d8e61d391 | 56 | public: |
wolfSSL | 17:c73d8e61d391 | 57 | int get_fd() { |
wolfSSL | 17:c73d8e61d391 | 58 | return _sock_fd ; |
wolfSSL | 17:c73d8e61d391 | 59 | } |
wolfSSL | 17:c73d8e61d391 | 60 | } ; |
wolfSSL | 17:c73d8e61d391 | 61 | |
wolfSSL | 17:c73d8e61d391 | 62 | static TCPSocketConnection_fd m_sock; |
wolfSSL | 17:c73d8e61d391 | 63 | |
wolfSSL | 17:c73d8e61d391 | 64 | #define CHUNK_SIZE 256 |
wolfSSL | 17:c73d8e61d391 | 65 | #define SEND_BUF_SIZE 512 |
wolfSSL | 17:c73d8e61d391 | 66 | static char send_buf[SEND_BUF_SIZE] ; |
wolfSSL | 17:c73d8e61d391 | 67 | static char *send_buf_p ; |
wolfSSL | 17:c73d8e61d391 | 68 | |
wolfSSL | 17:c73d8e61d391 | 69 | static int SocketReceive(CYASSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 17:c73d8e61d391 | 70 | { |
wolfSSL | 17:c73d8e61d391 | 71 | int n ; |
wolfSSL | 17:c73d8e61d391 | 72 | int i ; |
wolfSSL | 18:d89df40b4cf3 | 73 | #define RECV_RETRY 3 |
wolfSSL | 17:c73d8e61d391 | 74 | for(i=0; i<RECV_RETRY; i++) { |
wolfSSL | 17:c73d8e61d391 | 75 | n = m_sock.receive(buf, sz) ; |
wolfSSL | 17:c73d8e61d391 | 76 | if(n >= 0)return n ; |
wolfSSL | 17:c73d8e61d391 | 77 | } |
wolfSSL | 17:c73d8e61d391 | 78 | ERR("SocketReceive:%d/%d\n", n, sz) ; |
wolfSSL | 17:c73d8e61d391 | 79 | return n ; |
wolfSSL | 17:c73d8e61d391 | 80 | } |
wolfSSL | 17:c73d8e61d391 | 81 | |
wolfSSL | 17:c73d8e61d391 | 82 | static int SocketSend(CYASSL* ssl, char *buf, int sz, void *ctx) |
wolfSSL | 17:c73d8e61d391 | 83 | { |
wolfSSL | 17:c73d8e61d391 | 84 | int n ; |
wolfSSL | 17:c73d8e61d391 | 85 | |
wolfSSL | 17:c73d8e61d391 | 86 | n = m_sock.send(buf, sz); |
wolfSSL | 17:c73d8e61d391 | 87 | if(n > 0) { |
wolfSSL | 17:c73d8e61d391 | 88 | return n ; |
wolfSSL | 17:c73d8e61d391 | 89 | } else ERR("SocketSend:%d/%d\n", n, sz); |
wolfSSL | 17:c73d8e61d391 | 90 | return n ; |
wolfSSL | 17:c73d8e61d391 | 91 | } |
donatien | 11:390362de8c3f | 92 | |
donatien | 0:2ccb9960a044 | 93 | HTTPClient::HTTPClient() : |
wolfSSL | 18:d89df40b4cf3 | 94 | m_basicAuthUser(NULL), m_basicAuthPassword(NULL), m_httpResponseCode(0) |
donatien | 0:2ccb9960a044 | 95 | { |
wolfSSL | 18:d89df40b4cf3 | 96 | //CyaSSL_Debugging_ON() ; |
wolfSSL | 18:d89df40b4cf3 | 97 | ctx = 0 ; |
wolfSSL | 18:d89df40b4cf3 | 98 | ssl = 0 ; |
donatien | 0:2ccb9960a044 | 99 | } |
donatien | 0:2ccb9960a044 | 100 | |
donatien | 0:2ccb9960a044 | 101 | HTTPClient::~HTTPClient() |
donatien | 0:2ccb9960a044 | 102 | { |
donatien | 0:2ccb9960a044 | 103 | |
donatien | 0:2ccb9960a044 | 104 | } |
donatien | 0:2ccb9960a044 | 105 | |
donatien | 0:2ccb9960a044 | 106 | #if 0 |
donatien | 0:2ccb9960a044 | 107 | void HTTPClient::basicAuth(const char* user, const char* password) //Basic Authentification |
donatien | 0:2ccb9960a044 | 108 | { |
wolfSSL | 18:d89df40b4cf3 | 109 | m_basicAuthUser = user; |
wolfSSL | 18:d89df40b4cf3 | 110 | m_basicAuthPassword = password; |
donatien | 0:2ccb9960a044 | 111 | } |
donatien | 0:2ccb9960a044 | 112 | #endif |
donatien | 0:2ccb9960a044 | 113 | |
donatien | 12:89d09a6db00a | 114 | HTTPResult HTTPClient::get(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 115 | { |
wolfSSL | 18:d89df40b4cf3 | 116 | return connect(url, HTTP_GET, NULL, pDataIn, timeout); |
donatien | 0:2ccb9960a044 | 117 | } |
donatien | 0:2ccb9960a044 | 118 | |
donatien | 12:89d09a6db00a | 119 | HTTPResult HTTPClient::get(const char* url, char* result, size_t maxResultLen, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 120 | { |
wolfSSL | 18:d89df40b4cf3 | 121 | HTTPText str(result, maxResultLen); |
wolfSSL | 18:d89df40b4cf3 | 122 | return get(url, &str, timeout); |
donatien | 0:2ccb9960a044 | 123 | } |
donatien | 0:2ccb9960a044 | 124 | |
donatien | 12:89d09a6db00a | 125 | HTTPResult HTTPClient::post(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 0:2ccb9960a044 | 126 | { |
wolfSSL | 18:d89df40b4cf3 | 127 | return connect(url, HTTP_POST, (IHTTPDataOut*)&dataOut, pDataIn, timeout); |
donatien | 0:2ccb9960a044 | 128 | } |
donatien | 0:2ccb9960a044 | 129 | |
donatien | 16:1f743885e7de | 130 | HTTPResult HTTPClient::put(const char* url, const IHTTPDataOut& dataOut, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 16:1f743885e7de | 131 | { |
wolfSSL | 18:d89df40b4cf3 | 132 | return connect(url, HTTP_PUT, (IHTTPDataOut*)&dataOut, pDataIn, timeout); |
donatien | 16:1f743885e7de | 133 | } |
donatien | 16:1f743885e7de | 134 | |
donatien | 16:1f743885e7de | 135 | HTTPResult HTTPClient::del(const char* url, IHTTPDataIn* pDataIn, int timeout /*= HTTP_CLIENT_DEFAULT_TIMEOUT*/) //Blocking |
donatien | 16:1f743885e7de | 136 | { |
wolfSSL | 18:d89df40b4cf3 | 137 | return connect(url, HTTP_DELETE, NULL, pDataIn, timeout); |
donatien | 16:1f743885e7de | 138 | } |
donatien | 16:1f743885e7de | 139 | |
donatien | 16:1f743885e7de | 140 | |
donatien | 0:2ccb9960a044 | 141 | int HTTPClient::getHTTPResponseCode() |
donatien | 0:2ccb9960a044 | 142 | { |
wolfSSL | 18:d89df40b4cf3 | 143 | return m_httpResponseCode; |
donatien | 0:2ccb9960a044 | 144 | } |
donatien | 0:2ccb9960a044 | 145 | |
wolfSSL | 17:c73d8e61d391 | 146 | void HTTPClient::setHeader(char * h) |
wolfSSL | 17:c73d8e61d391 | 147 | { |
wolfSSL | 17:c73d8e61d391 | 148 | header = h ; |
wolfSSL | 17:c73d8e61d391 | 149 | } |
wolfSSL | 17:c73d8e61d391 | 150 | |
wolfSSL | 17:c73d8e61d391 | 151 | |
donatien | 5:791fc3dcb6c4 | 152 | #define CHECK_CONN_ERR(ret) \ |
donatien | 5:791fc3dcb6c4 | 153 | do{ \ |
donatien | 7:4e39864f7b15 | 154 | if(ret) { \ |
wolfSSL | 17:c73d8e61d391 | 155 | cyassl_free() ;\ |
donatien | 7:4e39864f7b15 | 156 | m_sock.close(); \ |
donatien | 5:791fc3dcb6c4 | 157 | ERR("Connection error (%d)", ret); \ |
donatien | 11:390362de8c3f | 158 | return HTTP_CONN; \ |
donatien | 5:791fc3dcb6c4 | 159 | } \ |
donatien | 5:791fc3dcb6c4 | 160 | } while(0) |
donatien | 5:791fc3dcb6c4 | 161 | |
donatien | 5:791fc3dcb6c4 | 162 | #define PRTCL_ERR() \ |
donatien | 5:791fc3dcb6c4 | 163 | do{ \ |
wolfSSL | 17:c73d8e61d391 | 164 | cyassl_free() ;\ |
donatien | 7:4e39864f7b15 | 165 | m_sock.close(); \ |
donatien | 5:791fc3dcb6c4 | 166 | ERR("Protocol error"); \ |
donatien | 11:390362de8c3f | 167 | return HTTP_PRTCL; \ |
donatien | 5:791fc3dcb6c4 | 168 | } while(0) |
donatien | 0:2ccb9960a044 | 169 | |
wolfSSL | 17:c73d8e61d391 | 170 | void HTTPClient::cyassl_free(void) |
wolfSSL | 17:c73d8e61d391 | 171 | { |
wolfSSL | 17:c73d8e61d391 | 172 | if(ssl) |
wolfSSL | 17:c73d8e61d391 | 173 | CyaSSL_free(ssl) ; |
wolfSSL | 17:c73d8e61d391 | 174 | if(ctx) |
wolfSSL | 17:c73d8e61d391 | 175 | CyaSSL_CTX_free(ctx) ; |
wolfSSL | 17:c73d8e61d391 | 176 | } |
wolfSSL | 17:c73d8e61d391 | 177 | |
wolfSSL | 17:c73d8e61d391 | 178 | |
donatien | 12:89d09a6db00a | 179 | HTTPResult HTTPClient::connect(const char* url, HTTP_METH method, IHTTPDataOut* pDataOut, IHTTPDataIn* pDataIn, int timeout) //Execute request |
wolfSSL | 18:d89df40b4cf3 | 180 | { |
wolfSSL | 18:d89df40b4cf3 | 181 | m_httpResponseCode = 0; //Invalidate code |
wolfSSL | 18:d89df40b4cf3 | 182 | m_timeout = timeout; |
donatien | 0:2ccb9960a044 | 183 | |
wolfSSL | 18:d89df40b4cf3 | 184 | pDataIn->writeReset(); |
wolfSSL | 18:d89df40b4cf3 | 185 | if( pDataOut ) { |
wolfSSL | 18:d89df40b4cf3 | 186 | pDataOut->readReset(); |
wolfSSL | 18:d89df40b4cf3 | 187 | } |
wolfSSL | 17:c73d8e61d391 | 188 | |
wolfSSL | 18:d89df40b4cf3 | 189 | char scheme[8]; |
wolfSSL | 18:d89df40b4cf3 | 190 | char host[32]; |
wolfSSL | 18:d89df40b4cf3 | 191 | char path[64]; |
wolfSSL | 18:d89df40b4cf3 | 192 | |
wolfSSL | 18:d89df40b4cf3 | 193 | int ret ; |
donatien | 0:2ccb9960a044 | 194 | |
wolfSSL | 18:d89df40b4cf3 | 195 | //First we need to parse the url (http[s]://host[:port][/[path]]) |
wolfSSL | 18:d89df40b4cf3 | 196 | HTTPResult res = parseURL(url, scheme, sizeof(scheme), host, sizeof(host), &port, path, sizeof(path)); |
wolfSSL | 18:d89df40b4cf3 | 197 | if(res != HTTP_OK) { |
wolfSSL | 18:d89df40b4cf3 | 198 | ERR("parseURL returned %d", res); |
wolfSSL | 18:d89df40b4cf3 | 199 | return res; |
wolfSSL | 18:d89df40b4cf3 | 200 | } |
donatien | 0:2ccb9960a044 | 201 | |
wolfSSL | 18:d89df40b4cf3 | 202 | if(port == 0) { //TODO do handle HTTPS->443 |
wolfSSL | 18:d89df40b4cf3 | 203 | if(strcmp(scheme, "http") == 0) |
wolfSSL | 18:d89df40b4cf3 | 204 | port = HTTP_PORT ; |
wolfSSL | 18:d89df40b4cf3 | 205 | else if(strcmp(scheme, "https") == 0) |
wolfSSL | 18:d89df40b4cf3 | 206 | port = HTTPS_PORT ; |
wolfSSL | 18:d89df40b4cf3 | 207 | } |
donatien | 0:2ccb9960a044 | 208 | |
wolfSSL | 18:d89df40b4cf3 | 209 | DBG("Scheme: %s", scheme); |
wolfSSL | 18:d89df40b4cf3 | 210 | DBG("Host: %s", host); |
wolfSSL | 18:d89df40b4cf3 | 211 | DBG("Port: %d", port); |
wolfSSL | 18:d89df40b4cf3 | 212 | DBG("Path: %s", path); |
wolfSSL | 17:c73d8e61d391 | 213 | |
wolfSSL | 18:d89df40b4cf3 | 214 | //Connect |
wolfSSL | 18:d89df40b4cf3 | 215 | DBG("Connecting socket to server"); |
wolfSSL | 18:d89df40b4cf3 | 216 | sockfd = m_sock.get_fd() ; |
wolfSSL | 18:d89df40b4cf3 | 217 | |
wolfSSL | 18:d89df40b4cf3 | 218 | #define MAX_RETRY 5 |
wolfSSL | 18:d89df40b4cf3 | 219 | int retry ; |
donatien | 0:2ccb9960a044 | 220 | |
wolfSSL | 18:d89df40b4cf3 | 221 | for(retry=0; retry<MAX_RETRY; retry++) { |
wolfSSL | 18:d89df40b4cf3 | 222 | int ret = m_sock.connect(host, port); |
wolfSSL | 18:d89df40b4cf3 | 223 | if(ret == 0)break ; |
wolfSSL | 17:c73d8e61d391 | 224 | } |
wolfSSL | 18:d89df40b4cf3 | 225 | if(retry == MAX_RETRY) { |
wolfSSL | 18:d89df40b4cf3 | 226 | m_sock.close(); |
wolfSSL | 18:d89df40b4cf3 | 227 | ERR("Could not connect"); |
wolfSSL | 18:d89df40b4cf3 | 228 | return HTTP_CONN; |
wolfSSL | 17:c73d8e61d391 | 229 | } |
wolfSSL | 17:c73d8e61d391 | 230 | |
wolfSSL | 18:d89df40b4cf3 | 231 | if(port == HTTPS_PORT) { |
wolfSSL | 18:d89df40b4cf3 | 232 | /* Start SSL connect */ |
wolfSSL | 18:d89df40b4cf3 | 233 | ctx = CyaSSL_CTX_new( |
wolfSSL | 18:d89df40b4cf3 | 234 | CyaTLSv1_2_client_method |
wolfSSL | 18:d89df40b4cf3 | 235 | //CyaSSLv3_client_method |
wolfSSL | 18:d89df40b4cf3 | 236 | ()); |
wolfSSL | 18:d89df40b4cf3 | 237 | if (ctx == NULL) { |
wolfSSL | 18:d89df40b4cf3 | 238 | ERR("unable to get ctx"); |
wolfSSL | 18:d89df40b4cf3 | 239 | return HTTP_CONN; |
wolfSSL | 18:d89df40b4cf3 | 240 | } |
wolfSSL | 18:d89df40b4cf3 | 241 | CyaSSL_CTX_set_verify(ctx, SSL_VERIFY_NONE, 0); |
wolfSSL | 18:d89df40b4cf3 | 242 | |
wolfSSL | 18:d89df40b4cf3 | 243 | ssl = CyaSSL_new(ctx); |
wolfSSL | 18:d89df40b4cf3 | 244 | if (ssl == NULL) { |
wolfSSL | 18:d89df40b4cf3 | 245 | ERR("unable to get SSL object"); |
wolfSSL | 18:d89df40b4cf3 | 246 | cyassl_free() ; |
wolfSSL | 18:d89df40b4cf3 | 247 | return HTTP_CONN; |
wolfSSL | 18:d89df40b4cf3 | 248 | } |
donatien | 0:2ccb9960a044 | 249 | |
wolfSSL | 18:d89df40b4cf3 | 250 | CyaSSL_SetVersion(ssl, CYASSL_TLSV1_2) ; |
wolfSSL | 18:d89df40b4cf3 | 251 | CyaSSL_set_fd(ssl, sockfd); |
wolfSSL | 18:d89df40b4cf3 | 252 | CyaSSL_SetIORecv(ctx, SocketReceive) ; |
wolfSSL | 18:d89df40b4cf3 | 253 | CyaSSL_SetIOSend(ctx, SocketSend) ; |
wolfSSL | 18:d89df40b4cf3 | 254 | DBG("ctx=%x, ssl=%x, ssl->ctx->CBIORecv, CBIOSend=%x, %x\n", |
wolfSSL | 18:d89df40b4cf3 | 255 | ctx, ssl, SocketReceive, SocketSend ) ; |
wolfSSL | 18:d89df40b4cf3 | 256 | if (CyaSSL_connect(ssl) != SSL_SUCCESS) { |
wolfSSL | 18:d89df40b4cf3 | 257 | ERR("SSL_connect failed"); |
wolfSSL | 18:d89df40b4cf3 | 258 | cyassl_free() ; |
wolfSSL | 18:d89df40b4cf3 | 259 | return HTTP_CONN; |
wolfSSL | 18:d89df40b4cf3 | 260 | } |
wolfSSL | 18:d89df40b4cf3 | 261 | } /* SSL connect complete */ |
donatien | 0:2ccb9960a044 | 262 | |
wolfSSL | 18:d89df40b4cf3 | 263 | //Send request |
wolfSSL | 18:d89df40b4cf3 | 264 | DBG("Sending request"); |
wolfSSL | 18:d89df40b4cf3 | 265 | char buf[CHUNK_SIZE]; |
wolfSSL | 18:d89df40b4cf3 | 266 | send_buf_p = send_buf ; // Reset send buffer ; |
wolfSSL | 18:d89df40b4cf3 | 267 | |
wolfSSL | 18:d89df40b4cf3 | 268 | const char* meth = (method==HTTP_GET)?"GET":(method==HTTP_POST)?"POST":(method==HTTP_PUT)?"PUT":(method==HTTP_DELETE)?"DELETE":""; |
wolfSSL | 18:d89df40b4cf3 | 269 | snprintf(buf, sizeof(buf), "%s %s HTTP/1.1\r\nHost: %s\r\n", meth, path, host); //Write request |
wolfSSL | 18:d89df40b4cf3 | 270 | ret = send(buf); |
wolfSSL | 18:d89df40b4cf3 | 271 | if(ret) { |
wolfSSL | 18:d89df40b4cf3 | 272 | m_sock.close(); |
wolfSSL | 18:d89df40b4cf3 | 273 | ERR("Could not write request"); |
wolfSSL | 18:d89df40b4cf3 | 274 | return HTTP_CONN; |
donatien | 0:2ccb9960a044 | 275 | } |
wolfSSL | 17:c73d8e61d391 | 276 | |
wolfSSL | 18:d89df40b4cf3 | 277 | //Send all headers |
donatien | 0:2ccb9960a044 | 278 | |
wolfSSL | 18:d89df40b4cf3 | 279 | //Send default headers |
wolfSSL | 18:d89df40b4cf3 | 280 | DBG("Sending headers"); |
wolfSSL | 18:d89df40b4cf3 | 281 | if( pDataOut != NULL ) { |
wolfSSL | 18:d89df40b4cf3 | 282 | if( pDataOut->getIsChunked() ) { |
wolfSSL | 18:d89df40b4cf3 | 283 | ret = send("Transfer-Encoding: chunked\r\n"); |
wolfSSL | 18:d89df40b4cf3 | 284 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 285 | } else { |
wolfSSL | 18:d89df40b4cf3 | 286 | snprintf(buf, sizeof(buf), "Content-Length: %d\r\n", pDataOut->getDataLen()); |
wolfSSL | 18:d89df40b4cf3 | 287 | ret = send(buf); |
wolfSSL | 18:d89df40b4cf3 | 288 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 289 | } |
wolfSSL | 18:d89df40b4cf3 | 290 | char type[48]; |
wolfSSL | 18:d89df40b4cf3 | 291 | if( pDataOut->getDataType(type, 48) == HTTP_OK ) { |
wolfSSL | 18:d89df40b4cf3 | 292 | snprintf(buf, sizeof(buf), "Content-Type: %s\r\n", type); |
wolfSSL | 18:d89df40b4cf3 | 293 | ret = send(buf); |
wolfSSL | 18:d89df40b4cf3 | 294 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 295 | } |
wolfSSL | 18:d89df40b4cf3 | 296 | } |
wolfSSL | 18:d89df40b4cf3 | 297 | |
wolfSSL | 18:d89df40b4cf3 | 298 | //Add user headers |
wolfSSL | 18:d89df40b4cf3 | 299 | if(header) { |
wolfSSL | 18:d89df40b4cf3 | 300 | ret = send(header); |
donatien | 5:791fc3dcb6c4 | 301 | CHECK_CONN_ERR(ret); |
donatien | 0:2ccb9960a044 | 302 | } |
donatien | 0:2ccb9960a044 | 303 | |
wolfSSL | 18:d89df40b4cf3 | 304 | //Close headers |
wolfSSL | 18:d89df40b4cf3 | 305 | DBG("Headers sent"); |
wolfSSL | 18:d89df40b4cf3 | 306 | ret = send("\r\n"); |
wolfSSL | 18:d89df40b4cf3 | 307 | CHECK_CONN_ERR(ret); |
wolfSSL | 17:c73d8e61d391 | 308 | |
wolfSSL | 18:d89df40b4cf3 | 309 | size_t trfLen; |
donatien | 0:2ccb9960a044 | 310 | |
wolfSSL | 18:d89df40b4cf3 | 311 | //Send data (if available) |
wolfSSL | 18:d89df40b4cf3 | 312 | if( pDataOut != NULL ) { |
wolfSSL | 18:d89df40b4cf3 | 313 | DBG("Sending data"); |
wolfSSL | 18:d89df40b4cf3 | 314 | while(true) { |
wolfSSL | 18:d89df40b4cf3 | 315 | size_t writtenLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 316 | pDataOut->read(buf, CHUNK_SIZE, &trfLen); |
wolfSSL | 18:d89df40b4cf3 | 317 | buf[trfLen] = 0x0 ; |
wolfSSL | 18:d89df40b4cf3 | 318 | DBG("buf:%s", buf) ; |
wolfSSL | 18:d89df40b4cf3 | 319 | if( pDataOut->getIsChunked() ) { |
wolfSSL | 18:d89df40b4cf3 | 320 | //Write chunk header |
wolfSSL | 18:d89df40b4cf3 | 321 | char chunkHeader[16]; |
wolfSSL | 18:d89df40b4cf3 | 322 | snprintf(chunkHeader, sizeof(chunkHeader), "%X\r\n", trfLen); //In hex encoding |
wolfSSL | 18:d89df40b4cf3 | 323 | ret = send(chunkHeader); |
wolfSSL | 18:d89df40b4cf3 | 324 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 325 | } else if( trfLen == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 326 | break; |
wolfSSL | 18:d89df40b4cf3 | 327 | } |
wolfSSL | 18:d89df40b4cf3 | 328 | if( trfLen != 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 329 | ret = send(buf, trfLen); |
wolfSSL | 18:d89df40b4cf3 | 330 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 331 | } |
donatien | 0:2ccb9960a044 | 332 | |
wolfSSL | 18:d89df40b4cf3 | 333 | if( pDataOut->getIsChunked() ) { |
wolfSSL | 18:d89df40b4cf3 | 334 | ret = send("\r\n"); //Chunk-terminating CRLF |
wolfSSL | 18:d89df40b4cf3 | 335 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 336 | } else { |
wolfSSL | 18:d89df40b4cf3 | 337 | writtenLen += trfLen; |
wolfSSL | 18:d89df40b4cf3 | 338 | if( writtenLen >= pDataOut->getDataLen() ) { |
wolfSSL | 18:d89df40b4cf3 | 339 | break; |
wolfSSL | 18:d89df40b4cf3 | 340 | } |
wolfSSL | 18:d89df40b4cf3 | 341 | } |
donatien | 0:2ccb9960a044 | 342 | |
wolfSSL | 18:d89df40b4cf3 | 343 | if( trfLen == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 344 | break; |
wolfSSL | 18:d89df40b4cf3 | 345 | } |
wolfSSL | 18:d89df40b4cf3 | 346 | } |
donatien | 0:2ccb9960a044 | 347 | |
wolfSSL | 18:d89df40b4cf3 | 348 | } |
wolfSSL | 18:d89df40b4cf3 | 349 | ret = flush() ; // flush the send buffer ; |
wolfSSL | 18:d89df40b4cf3 | 350 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 351 | |
wolfSSL | 18:d89df40b4cf3 | 352 | //Receive response |
wolfSSL | 18:d89df40b4cf3 | 353 | DBG("Receiving response"); |
wolfSSL | 18:d89df40b4cf3 | 354 | |
wolfSSL | 18:d89df40b4cf3 | 355 | ret = recv(buf, CHUNK_SIZE - 1, CHUNK_SIZE - 1, &trfLen); //Read n bytes |
wolfSSL | 18:d89df40b4cf3 | 356 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 357 | |
wolfSSL | 18:d89df40b4cf3 | 358 | buf[trfLen] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 359 | |
wolfSSL | 18:d89df40b4cf3 | 360 | char* crlfPtr = strstr(buf, "\r\n"); |
wolfSSL | 18:d89df40b4cf3 | 361 | if(crlfPtr == NULL) { |
donatien | 5:791fc3dcb6c4 | 362 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 363 | } |
donatien | 0:2ccb9960a044 | 364 | |
wolfSSL | 18:d89df40b4cf3 | 365 | int crlfPos = crlfPtr - buf; |
donatien | 0:2ccb9960a044 | 366 | buf[crlfPos] = '\0'; |
donatien | 0:2ccb9960a044 | 367 | |
wolfSSL | 18:d89df40b4cf3 | 368 | //Parse HTTP response |
wolfSSL | 18:d89df40b4cf3 | 369 | if( sscanf(buf, "HTTP/%*d.%*d %d %*[^\r\n]", &m_httpResponseCode) != 1 ) { |
wolfSSL | 18:d89df40b4cf3 | 370 | //Cannot match string, error |
wolfSSL | 18:d89df40b4cf3 | 371 | ERR("Not a correct HTTP answer : %s\n", buf); |
wolfSSL | 18:d89df40b4cf3 | 372 | PRTCL_ERR(); |
wolfSSL | 18:d89df40b4cf3 | 373 | } |
donatien | 4:c071b05ac026 | 374 | |
wolfSSL | 18:d89df40b4cf3 | 375 | if( (m_httpResponseCode < 200) || (m_httpResponseCode >= 300) ) { |
wolfSSL | 18:d89df40b4cf3 | 376 | //Did not return a 2xx code; TODO fetch headers/(&data?) anyway and implement a mean of writing/reading headers |
wolfSSL | 18:d89df40b4cf3 | 377 | WARN("Response code %d", m_httpResponseCode); |
wolfSSL | 18:d89df40b4cf3 | 378 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 379 | } |
donatien | 0:2ccb9960a044 | 380 | |
wolfSSL | 18:d89df40b4cf3 | 381 | DBG("Reading headers"); |
donatien | 0:2ccb9960a044 | 382 | |
wolfSSL | 18:d89df40b4cf3 | 383 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
wolfSSL | 18:d89df40b4cf3 | 384 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 385 | |
wolfSSL | 18:d89df40b4cf3 | 386 | size_t recvContentLength = 0; |
wolfSSL | 18:d89df40b4cf3 | 387 | bool recvChunked = false; |
wolfSSL | 18:d89df40b4cf3 | 388 | //Now get headers |
wolfSSL | 18:d89df40b4cf3 | 389 | while( true ) { |
wolfSSL | 18:d89df40b4cf3 | 390 | crlfPtr = strstr(buf, "\r\n"); |
wolfSSL | 18:d89df40b4cf3 | 391 | if(crlfPtr == NULL) { |
wolfSSL | 18:d89df40b4cf3 | 392 | if( trfLen < CHUNK_SIZE - 1 ) { |
wolfSSL | 18:d89df40b4cf3 | 393 | size_t newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 394 | ret = recv(buf + trfLen, 1, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
wolfSSL | 18:d89df40b4cf3 | 395 | trfLen += newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 396 | buf[trfLen] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 397 | DBG("Read %d chars; In buf: [%s]", newTrfLen, buf); |
wolfSSL | 18:d89df40b4cf3 | 398 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 399 | continue; |
wolfSSL | 18:d89df40b4cf3 | 400 | } else { |
wolfSSL | 18:d89df40b4cf3 | 401 | PRTCL_ERR(); |
donatien | 14:2744e0c0e527 | 402 | } |
wolfSSL | 18:d89df40b4cf3 | 403 | } |
wolfSSL | 18:d89df40b4cf3 | 404 | |
wolfSSL | 18:d89df40b4cf3 | 405 | crlfPos = crlfPtr - buf; |
wolfSSL | 18:d89df40b4cf3 | 406 | |
wolfSSL | 18:d89df40b4cf3 | 407 | if(crlfPos == 0) { //End of headers |
wolfSSL | 18:d89df40b4cf3 | 408 | DBG("Headers read"); |
wolfSSL | 18:d89df40b4cf3 | 409 | memmove(buf, &buf[2], trfLen - 2 + 1); //Be sure to move NULL-terminating char as well |
wolfSSL | 18:d89df40b4cf3 | 410 | trfLen -= 2; |
wolfSSL | 18:d89df40b4cf3 | 411 | break; |
donatien | 0:2ccb9960a044 | 412 | } |
wolfSSL | 18:d89df40b4cf3 | 413 | |
wolfSSL | 18:d89df40b4cf3 | 414 | buf[crlfPos] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 415 | |
wolfSSL | 18:d89df40b4cf3 | 416 | char key[32]; |
wolfSSL | 18:d89df40b4cf3 | 417 | char value[32]; |
wolfSSL | 18:d89df40b4cf3 | 418 | |
wolfSSL | 18:d89df40b4cf3 | 419 | key[31] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 420 | value[31] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 421 | |
wolfSSL | 18:d89df40b4cf3 | 422 | int n = sscanf(buf, "%31[^:]: %31[^\r\n]", key, value); |
wolfSSL | 18:d89df40b4cf3 | 423 | if ( n == 2 ) { |
wolfSSL | 18:d89df40b4cf3 | 424 | DBG("Read header : %s: %s\n", key, value); |
wolfSSL | 18:d89df40b4cf3 | 425 | if( !strcmp(key, "Content-Length") ) { |
wolfSSL | 18:d89df40b4cf3 | 426 | sscanf(value, "%d", &recvContentLength); |
wolfSSL | 18:d89df40b4cf3 | 427 | pDataIn->setDataLen(recvContentLength); |
wolfSSL | 18:d89df40b4cf3 | 428 | } else if( !strcmp(key, "Transfer-Encoding") ) { |
wolfSSL | 18:d89df40b4cf3 | 429 | if( !strcmp(value, "Chunked") || !strcmp(value, "chunked") ) { |
wolfSSL | 18:d89df40b4cf3 | 430 | recvChunked = true; |
wolfSSL | 18:d89df40b4cf3 | 431 | pDataIn->setIsChunked(true); |
wolfSSL | 18:d89df40b4cf3 | 432 | } |
wolfSSL | 18:d89df40b4cf3 | 433 | } else if( !strcmp(key, "Content-Type") ) { |
wolfSSL | 18:d89df40b4cf3 | 434 | pDataIn->setDataType(value); |
wolfSSL | 18:d89df40b4cf3 | 435 | } |
wolfSSL | 18:d89df40b4cf3 | 436 | |
wolfSSL | 18:d89df40b4cf3 | 437 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2) + 1); //Be sure to move NULL-terminating char as well |
wolfSSL | 18:d89df40b4cf3 | 438 | trfLen -= (crlfPos + 2); |
wolfSSL | 18:d89df40b4cf3 | 439 | |
wolfSSL | 18:d89df40b4cf3 | 440 | } else { |
wolfSSL | 18:d89df40b4cf3 | 441 | ERR("Could not parse header"); |
donatien | 14:2744e0c0e527 | 442 | PRTCL_ERR(); |
donatien | 0:2ccb9960a044 | 443 | } |
donatien | 0:2ccb9960a044 | 444 | |
donatien | 0:2ccb9960a044 | 445 | } |
donatien | 0:2ccb9960a044 | 446 | |
wolfSSL | 18:d89df40b4cf3 | 447 | //Receive data |
wolfSSL | 18:d89df40b4cf3 | 448 | DBG("Receiving data"); |
wolfSSL | 18:d89df40b4cf3 | 449 | |
wolfSSL | 18:d89df40b4cf3 | 450 | while(true) { |
wolfSSL | 18:d89df40b4cf3 | 451 | size_t readLen = 0; |
donatien | 0:2ccb9960a044 | 452 | |
wolfSSL | 18:d89df40b4cf3 | 453 | if( recvChunked ) { |
wolfSSL | 18:d89df40b4cf3 | 454 | //Read chunk header |
wolfSSL | 18:d89df40b4cf3 | 455 | bool foundCrlf; |
wolfSSL | 18:d89df40b4cf3 | 456 | do { |
wolfSSL | 18:d89df40b4cf3 | 457 | foundCrlf = false; |
wolfSSL | 18:d89df40b4cf3 | 458 | crlfPos=0; |
wolfSSL | 18:d89df40b4cf3 | 459 | buf[trfLen]=0; |
wolfSSL | 18:d89df40b4cf3 | 460 | if(trfLen >= 2) { |
wolfSSL | 18:d89df40b4cf3 | 461 | for(; crlfPos < trfLen - 2; crlfPos++) { |
wolfSSL | 18:d89df40b4cf3 | 462 | if( buf[crlfPos] == '\r' && buf[crlfPos + 1] == '\n' ) { |
wolfSSL | 18:d89df40b4cf3 | 463 | foundCrlf = true; |
wolfSSL | 18:d89df40b4cf3 | 464 | break; |
wolfSSL | 18:d89df40b4cf3 | 465 | } |
wolfSSL | 18:d89df40b4cf3 | 466 | } |
wolfSSL | 18:d89df40b4cf3 | 467 | } |
wolfSSL | 18:d89df40b4cf3 | 468 | if(!foundCrlf) { //Try to read more |
wolfSSL | 18:d89df40b4cf3 | 469 | if( trfLen < CHUNK_SIZE ) { |
wolfSSL | 18:d89df40b4cf3 | 470 | size_t newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 471 | ret = recv(buf + trfLen, 0, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
wolfSSL | 18:d89df40b4cf3 | 472 | trfLen += newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 473 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 474 | continue; |
wolfSSL | 18:d89df40b4cf3 | 475 | } else { |
wolfSSL | 18:d89df40b4cf3 | 476 | PRTCL_ERR(); |
wolfSSL | 18:d89df40b4cf3 | 477 | } |
wolfSSL | 18:d89df40b4cf3 | 478 | } |
wolfSSL | 18:d89df40b4cf3 | 479 | } while(!foundCrlf); |
wolfSSL | 18:d89df40b4cf3 | 480 | buf[crlfPos] = '\0'; |
wolfSSL | 18:d89df40b4cf3 | 481 | int n = sscanf(buf, "%x", &readLen); |
wolfSSL | 18:d89df40b4cf3 | 482 | if(n!=1) { |
wolfSSL | 18:d89df40b4cf3 | 483 | ERR("Could not read chunk length"); |
wolfSSL | 18:d89df40b4cf3 | 484 | PRTCL_ERR(); |
wolfSSL | 18:d89df40b4cf3 | 485 | } |
wolfSSL | 18:d89df40b4cf3 | 486 | |
wolfSSL | 18:d89df40b4cf3 | 487 | memmove(buf, &buf[crlfPos+2], trfLen - (crlfPos + 2)); //Not need to move NULL-terminating char any more |
wolfSSL | 18:d89df40b4cf3 | 488 | trfLen -= (crlfPos + 2); |
donatien | 0:2ccb9960a044 | 489 | |
wolfSSL | 18:d89df40b4cf3 | 490 | if( readLen == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 491 | //Last chunk |
wolfSSL | 18:d89df40b4cf3 | 492 | break; |
wolfSSL | 18:d89df40b4cf3 | 493 | } |
wolfSSL | 18:d89df40b4cf3 | 494 | } else { |
wolfSSL | 18:d89df40b4cf3 | 495 | readLen = recvContentLength; |
wolfSSL | 18:d89df40b4cf3 | 496 | } |
wolfSSL | 18:d89df40b4cf3 | 497 | |
wolfSSL | 18:d89df40b4cf3 | 498 | DBG("Retrieving %d bytes", readLen); |
wolfSSL | 18:d89df40b4cf3 | 499 | |
wolfSSL | 18:d89df40b4cf3 | 500 | do { |
wolfSSL | 18:d89df40b4cf3 | 501 | pDataIn->write(buf, MIN(trfLen, readLen)); |
wolfSSL | 18:d89df40b4cf3 | 502 | if( trfLen > readLen ) { |
wolfSSL | 18:d89df40b4cf3 | 503 | memmove(buf, &buf[readLen], trfLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 504 | trfLen -= readLen; |
wolfSSL | 18:d89df40b4cf3 | 505 | readLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 506 | } else { |
wolfSSL | 18:d89df40b4cf3 | 507 | readLen -= trfLen; |
wolfSSL | 18:d89df40b4cf3 | 508 | } |
donatien | 0:2ccb9960a044 | 509 | |
wolfSSL | 18:d89df40b4cf3 | 510 | if(readLen) { |
wolfSSL | 18:d89df40b4cf3 | 511 | ret = recv(buf, 1, CHUNK_SIZE - trfLen - 1, &trfLen); |
wolfSSL | 18:d89df40b4cf3 | 512 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 513 | } |
wolfSSL | 18:d89df40b4cf3 | 514 | } while(readLen); |
wolfSSL | 18:d89df40b4cf3 | 515 | |
wolfSSL | 18:d89df40b4cf3 | 516 | if( recvChunked ) { |
wolfSSL | 18:d89df40b4cf3 | 517 | if(trfLen < 2) { |
wolfSSL | 18:d89df40b4cf3 | 518 | size_t newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 519 | //Read missing chars to find end of chunk |
wolfSSL | 18:d89df40b4cf3 | 520 | ret = recv(buf + trfLen, 2 - trfLen, CHUNK_SIZE - trfLen - 1, &newTrfLen); |
wolfSSL | 18:d89df40b4cf3 | 521 | CHECK_CONN_ERR(ret); |
wolfSSL | 18:d89df40b4cf3 | 522 | trfLen += newTrfLen; |
wolfSSL | 18:d89df40b4cf3 | 523 | } |
wolfSSL | 18:d89df40b4cf3 | 524 | if( (buf[0] != '\r') || (buf[1] != '\n') ) { |
wolfSSL | 18:d89df40b4cf3 | 525 | ERR("Format error"); |
wolfSSL | 18:d89df40b4cf3 | 526 | PRTCL_ERR(); |
wolfSSL | 18:d89df40b4cf3 | 527 | } |
wolfSSL | 18:d89df40b4cf3 | 528 | memmove(buf, &buf[2], trfLen - 2); |
wolfSSL | 18:d89df40b4cf3 | 529 | trfLen -= 2; |
wolfSSL | 18:d89df40b4cf3 | 530 | } else { |
wolfSSL | 18:d89df40b4cf3 | 531 | break; |
wolfSSL | 18:d89df40b4cf3 | 532 | } |
wolfSSL | 18:d89df40b4cf3 | 533 | |
donatien | 0:2ccb9960a044 | 534 | } |
donatien | 0:2ccb9960a044 | 535 | |
wolfSSL | 18:d89df40b4cf3 | 536 | cyassl_free() ; |
wolfSSL | 18:d89df40b4cf3 | 537 | m_sock.close(); |
wolfSSL | 18:d89df40b4cf3 | 538 | DBG("Completed HTTP transaction"); |
donatien | 0:2ccb9960a044 | 539 | |
wolfSSL | 18:d89df40b4cf3 | 540 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 541 | } |
donatien | 0:2ccb9960a044 | 542 | |
donatien | 11:390362de8c3f | 543 | HTTPResult HTTPClient::recv(char* buf, size_t minLen, size_t maxLen, size_t* pReadLen) //0 on success, err code on failure |
donatien | 0:2ccb9960a044 | 544 | { |
wolfSSL | 18:d89df40b4cf3 | 545 | DBG("Trying to read between %d and %d bytes", minLen, maxLen); |
wolfSSL | 18:d89df40b4cf3 | 546 | size_t readLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 547 | |
wolfSSL | 18:d89df40b4cf3 | 548 | if(!m_sock.is_connected()) { |
wolfSSL | 18:d89df40b4cf3 | 549 | WARN("Connection was closed by server"); |
wolfSSL | 18:d89df40b4cf3 | 550 | return HTTP_CLOSED; //Connection was closed by server |
wolfSSL | 18:d89df40b4cf3 | 551 | } |
wolfSSL | 18:d89df40b4cf3 | 552 | |
wolfSSL | 18:d89df40b4cf3 | 553 | int ret; |
wolfSSL | 18:d89df40b4cf3 | 554 | |
wolfSSL | 18:d89df40b4cf3 | 555 | if(port == HTTPS_PORT) { |
wolfSSL | 18:d89df40b4cf3 | 556 | DBG("Enter CyaSSL_read") ; |
wolfSSL | 18:d89df40b4cf3 | 557 | |
wolfSSL | 18:d89df40b4cf3 | 558 | m_sock.set_blocking(false, m_timeout); |
wolfSSL | 18:d89df40b4cf3 | 559 | readLen = CyaSSL_read(ssl, buf, maxLen); |
wolfSSL | 18:d89df40b4cf3 | 560 | if (readLen > 0) { |
wolfSSL | 18:d89df40b4cf3 | 561 | buf[readLen] = 0; |
wolfSSL | 18:d89df40b4cf3 | 562 | DBG("CyaSSL_read:%s\n", buf); |
wolfSSL | 18:d89df40b4cf3 | 563 | } else { |
wolfSSL | 18:d89df40b4cf3 | 564 | ERR("CyaSSL_read, ret = %d", readLen) ; |
wolfSSL | 18:d89df40b4cf3 | 565 | return HTTP_ERROR ; |
wolfSSL | 18:d89df40b4cf3 | 566 | } |
wolfSSL | 18:d89df40b4cf3 | 567 | DBG("Read %d bytes", readLen); |
wolfSSL | 18:d89df40b4cf3 | 568 | *pReadLen = readLen; |
wolfSSL | 18:d89df40b4cf3 | 569 | return HTTP_OK; |
wolfSSL | 18:d89df40b4cf3 | 570 | } |
wolfSSL | 18:d89df40b4cf3 | 571 | |
wolfSSL | 18:d89df40b4cf3 | 572 | while(readLen < maxLen) { |
wolfSSL | 18:d89df40b4cf3 | 573 | if(readLen < minLen) { |
wolfSSL | 18:d89df40b4cf3 | 574 | DBG("Trying to read at most %d bytes [Blocking]", minLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 575 | m_sock.set_blocking(false, m_timeout); |
wolfSSL | 18:d89df40b4cf3 | 576 | ret = m_sock.receive_all(buf + readLen, minLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 577 | } else { |
wolfSSL | 18:d89df40b4cf3 | 578 | DBG("Trying to read at most %d bytes [Not blocking]", maxLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 579 | m_sock.set_blocking(false, 0); |
wolfSSL | 18:d89df40b4cf3 | 580 | ret = m_sock.receive(buf + readLen, maxLen - readLen); |
wolfSSL | 18:d89df40b4cf3 | 581 | } |
wolfSSL | 18:d89df40b4cf3 | 582 | |
wolfSSL | 18:d89df40b4cf3 | 583 | if( ret > 0) { |
wolfSSL | 18:d89df40b4cf3 | 584 | readLen += ret; |
wolfSSL | 18:d89df40b4cf3 | 585 | } else if( ret == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 586 | break; |
wolfSSL | 18:d89df40b4cf3 | 587 | } else { |
wolfSSL | 18:d89df40b4cf3 | 588 | if(!m_sock.is_connected()) { |
wolfSSL | 18:d89df40b4cf3 | 589 | ERR("Connection error (recv returned %d)", ret); |
wolfSSL | 18:d89df40b4cf3 | 590 | *pReadLen = readLen; |
wolfSSL | 18:d89df40b4cf3 | 591 | return HTTP_CONN; |
wolfSSL | 18:d89df40b4cf3 | 592 | } else { |
wolfSSL | 18:d89df40b4cf3 | 593 | break; |
wolfSSL | 18:d89df40b4cf3 | 594 | } |
wolfSSL | 18:d89df40b4cf3 | 595 | } |
wolfSSL | 18:d89df40b4cf3 | 596 | |
wolfSSL | 18:d89df40b4cf3 | 597 | if(!m_sock.is_connected()) { |
wolfSSL | 18:d89df40b4cf3 | 598 | break; |
wolfSSL | 18:d89df40b4cf3 | 599 | } |
wolfSSL | 17:c73d8e61d391 | 600 | } |
wolfSSL | 17:c73d8e61d391 | 601 | DBG("Read %d bytes", readLen); |
wolfSSL | 17:c73d8e61d391 | 602 | *pReadLen = readLen; |
wolfSSL | 17:c73d8e61d391 | 603 | return HTTP_OK; |
donatien | 7:4e39864f7b15 | 604 | } |
donatien | 7:4e39864f7b15 | 605 | |
donatien | 11:390362de8c3f | 606 | HTTPResult HTTPClient::send(char* buf, size_t len) //0 on success, err code on failure |
donatien | 7:4e39864f7b15 | 607 | { |
wolfSSL | 18:d89df40b4cf3 | 608 | HTTPResult ret ; |
wolfSSL | 18:d89df40b4cf3 | 609 | int cp_len ; |
wolfSSL | 18:d89df40b4cf3 | 610 | |
wolfSSL | 18:d89df40b4cf3 | 611 | if(len == 0) { |
wolfSSL | 18:d89df40b4cf3 | 612 | len = strlen(buf); |
wolfSSL | 17:c73d8e61d391 | 613 | } |
wolfSSL | 17:c73d8e61d391 | 614 | |
wolfSSL | 18:d89df40b4cf3 | 615 | do { |
wolfSSL | 18:d89df40b4cf3 | 616 | if((SEND_BUF_SIZE - (send_buf_p - send_buf)) >= len) { |
wolfSSL | 18:d89df40b4cf3 | 617 | cp_len = len ; |
wolfSSL | 18:d89df40b4cf3 | 618 | } else { |
wolfSSL | 18:d89df40b4cf3 | 619 | cp_len = send_buf_p - send_buf ; |
wolfSSL | 18:d89df40b4cf3 | 620 | } |
wolfSSL | 18:d89df40b4cf3 | 621 | memcpy(send_buf_p, buf, cp_len) ; |
wolfSSL | 18:d89df40b4cf3 | 622 | send_buf_p += cp_len ; |
wolfSSL | 18:d89df40b4cf3 | 623 | len -= cp_len ; |
wolfSSL | 18:d89df40b4cf3 | 624 | |
wolfSSL | 18:d89df40b4cf3 | 625 | if(send_buf_p == send_buf + SEND_BUF_SIZE) { |
wolfSSL | 18:d89df40b4cf3 | 626 | ret = flush() ; |
wolfSSL | 18:d89df40b4cf3 | 627 | if(ret)return(ret) ; |
wolfSSL | 18:d89df40b4cf3 | 628 | } |
wolfSSL | 18:d89df40b4cf3 | 629 | } while(len) ; |
wolfSSL | 18:d89df40b4cf3 | 630 | return HTTP_OK ; |
wolfSSL | 17:c73d8e61d391 | 631 | } |
wolfSSL | 17:c73d8e61d391 | 632 | |
wolfSSL | 17:c73d8e61d391 | 633 | HTTPResult HTTPClient::flush() //0 on success, err code on failure |
wolfSSL | 17:c73d8e61d391 | 634 | { |
wolfSSL | 18:d89df40b4cf3 | 635 | int len ; |
wolfSSL | 18:d89df40b4cf3 | 636 | char * buf ; |
wolfSSL | 18:d89df40b4cf3 | 637 | |
wolfSSL | 18:d89df40b4cf3 | 638 | buf = send_buf ; |
wolfSSL | 18:d89df40b4cf3 | 639 | len = send_buf_p - send_buf ; |
wolfSSL | 18:d89df40b4cf3 | 640 | send_buf_p = send_buf ; // reset send buffer |
wolfSSL | 18:d89df40b4cf3 | 641 | |
wolfSSL | 18:d89df40b4cf3 | 642 | DBG("Trying to write %d bytes:%s\n", len, buf); |
wolfSSL | 18:d89df40b4cf3 | 643 | size_t writtenLen = 0; |
wolfSSL | 18:d89df40b4cf3 | 644 | |
wolfSSL | 18:d89df40b4cf3 | 645 | if(!m_sock.is_connected()) { |
wolfSSL | 18:d89df40b4cf3 | 646 | WARN("Connection was closed by server"); |
wolfSSL | 18:d89df40b4cf3 | 647 | return HTTP_CLOSED; //Connection was closed by server |
wolfSSL | 17:c73d8e61d391 | 648 | } |
wolfSSL | 18:d89df40b4cf3 | 649 | |
wolfSSL | 18:d89df40b4cf3 | 650 | if(port == HTTPS_PORT) { |
wolfSSL | 18:d89df40b4cf3 | 651 | DBG("Enter CyaSSL_write") ; |
wolfSSL | 18:d89df40b4cf3 | 652 | if (CyaSSL_write(ssl, buf, len) != len) { |
wolfSSL | 18:d89df40b4cf3 | 653 | ERR("SSL_write failed"); |
wolfSSL | 18:d89df40b4cf3 | 654 | return HTTP_ERROR ; |
wolfSSL | 18:d89df40b4cf3 | 655 | } |
wolfSSL | 18:d89df40b4cf3 | 656 | DBG("Written %d bytes", writtenLen); |
wolfSSL | 18:d89df40b4cf3 | 657 | return HTTP_OK; |
wolfSSL | 18:d89df40b4cf3 | 658 | } |
wolfSSL | 18:d89df40b4cf3 | 659 | m_sock.set_blocking(false, m_timeout); |
wolfSSL | 18:d89df40b4cf3 | 660 | int ret = m_sock.send_all(buf, len); |
wolfSSL | 18:d89df40b4cf3 | 661 | if(ret > 0) { |
wolfSSL | 18:d89df40b4cf3 | 662 | writtenLen += ret; |
wolfSSL | 18:d89df40b4cf3 | 663 | } else if( ret == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 664 | WARN("Connection was closed by server"); |
wolfSSL | 18:d89df40b4cf3 | 665 | return HTTP_CLOSED; //Connection was closed by server |
wolfSSL | 18:d89df40b4cf3 | 666 | } else { |
wolfSSL | 18:d89df40b4cf3 | 667 | ERR("Connection error (send returned %d)", ret); |
wolfSSL | 18:d89df40b4cf3 | 668 | return HTTP_CONN; |
wolfSSL | 18:d89df40b4cf3 | 669 | } |
wolfSSL | 18:d89df40b4cf3 | 670 | |
wolfSSL | 17:c73d8e61d391 | 671 | DBG("Written %d bytes", writtenLen); |
wolfSSL | 17:c73d8e61d391 | 672 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 673 | } |
donatien | 0:2ccb9960a044 | 674 | |
donatien | 11:390362de8c3f | 675 | HTTPResult HTTPClient::parseURL(const char* url, char* scheme, size_t maxSchemeLen, char* host, size_t maxHostLen, uint16_t* port, char* path, size_t maxPathLen) //Parse URL |
donatien | 0:2ccb9960a044 | 676 | { |
wolfSSL | 18:d89df40b4cf3 | 677 | char* schemePtr = (char*) url; |
wolfSSL | 18:d89df40b4cf3 | 678 | char* hostPtr = (char*) strstr(url, "://"); |
wolfSSL | 18:d89df40b4cf3 | 679 | if(hostPtr == NULL) { |
wolfSSL | 18:d89df40b4cf3 | 680 | WARN("Could not find host"); |
wolfSSL | 18:d89df40b4cf3 | 681 | return HTTP_PARSE; //URL is invalid |
wolfSSL | 18:d89df40b4cf3 | 682 | } |
wolfSSL | 18:d89df40b4cf3 | 683 | |
wolfSSL | 18:d89df40b4cf3 | 684 | if( maxSchemeLen < hostPtr - schemePtr + 1 ) { //including NULL-terminating char |
wolfSSL | 18:d89df40b4cf3 | 685 | WARN("Scheme str is too small (%d >= %d)", maxSchemeLen, hostPtr - schemePtr + 1); |
wolfSSL | 18:d89df40b4cf3 | 686 | return HTTP_PARSE; |
wolfSSL | 18:d89df40b4cf3 | 687 | } |
wolfSSL | 18:d89df40b4cf3 | 688 | memcpy(scheme, schemePtr, hostPtr - schemePtr); |
wolfSSL | 18:d89df40b4cf3 | 689 | scheme[hostPtr - schemePtr] = '\0'; |
donatien | 0:2ccb9960a044 | 690 | |
wolfSSL | 18:d89df40b4cf3 | 691 | hostPtr+=3; |
donatien | 0:2ccb9960a044 | 692 | |
wolfSSL | 18:d89df40b4cf3 | 693 | size_t hostLen = 0; |
donatien | 0:2ccb9960a044 | 694 | |
wolfSSL | 18:d89df40b4cf3 | 695 | char* portPtr = strchr(hostPtr, ':'); |
wolfSSL | 18:d89df40b4cf3 | 696 | if( portPtr != NULL ) { |
wolfSSL | 18:d89df40b4cf3 | 697 | hostLen = portPtr - hostPtr; |
wolfSSL | 18:d89df40b4cf3 | 698 | portPtr++; |
wolfSSL | 18:d89df40b4cf3 | 699 | if( sscanf(portPtr, "%hu", port) != 1) { |
wolfSSL | 18:d89df40b4cf3 | 700 | WARN("Could not find port"); |
wolfSSL | 18:d89df40b4cf3 | 701 | return HTTP_PARSE; |
wolfSSL | 18:d89df40b4cf3 | 702 | } |
wolfSSL | 18:d89df40b4cf3 | 703 | } else { |
wolfSSL | 18:d89df40b4cf3 | 704 | *port=0; |
donatien | 0:2ccb9960a044 | 705 | } |
wolfSSL | 18:d89df40b4cf3 | 706 | char* pathPtr = strchr(hostPtr, '/'); |
wolfSSL | 18:d89df40b4cf3 | 707 | if( hostLen == 0 ) { |
wolfSSL | 18:d89df40b4cf3 | 708 | hostLen = pathPtr - hostPtr; |
wolfSSL | 18:d89df40b4cf3 | 709 | } |
donatien | 0:2ccb9960a044 | 710 | |
wolfSSL | 18:d89df40b4cf3 | 711 | if( maxHostLen < hostLen + 1 ) { //including NULL-terminating char |
wolfSSL | 18:d89df40b4cf3 | 712 | WARN("Host str is too small (%d >= %d)", maxHostLen, hostLen + 1); |
wolfSSL | 18:d89df40b4cf3 | 713 | return HTTP_PARSE; |
wolfSSL | 18:d89df40b4cf3 | 714 | } |
wolfSSL | 18:d89df40b4cf3 | 715 | memcpy(host, hostPtr, hostLen); |
wolfSSL | 18:d89df40b4cf3 | 716 | host[hostLen] = '\0'; |
donatien | 0:2ccb9960a044 | 717 | |
wolfSSL | 18:d89df40b4cf3 | 718 | size_t pathLen; |
wolfSSL | 18:d89df40b4cf3 | 719 | char* fragmentPtr = strchr(hostPtr, '#'); |
wolfSSL | 18:d89df40b4cf3 | 720 | if(fragmentPtr != NULL) { |
wolfSSL | 18:d89df40b4cf3 | 721 | pathLen = fragmentPtr - pathPtr; |
wolfSSL | 18:d89df40b4cf3 | 722 | } else { |
wolfSSL | 18:d89df40b4cf3 | 723 | pathLen = strlen(pathPtr); |
wolfSSL | 18:d89df40b4cf3 | 724 | } |
donatien | 0:2ccb9960a044 | 725 | |
wolfSSL | 18:d89df40b4cf3 | 726 | if( maxPathLen < pathLen + 1 ) { //including NULL-terminating char |
wolfSSL | 18:d89df40b4cf3 | 727 | WARN("Path str is too small (%d >= %d)", maxPathLen, pathLen + 1); |
wolfSSL | 18:d89df40b4cf3 | 728 | return HTTP_PARSE; |
wolfSSL | 18:d89df40b4cf3 | 729 | } |
wolfSSL | 18:d89df40b4cf3 | 730 | memcpy(path, pathPtr, pathLen); |
wolfSSL | 18:d89df40b4cf3 | 731 | path[pathLen] = '\0'; |
donatien | 0:2ccb9960a044 | 732 | |
wolfSSL | 18:d89df40b4cf3 | 733 | return HTTP_OK; |
donatien | 0:2ccb9960a044 | 734 | } |