Library for Firebase, PUT, PATCH, POST, GET, DELETE operations supported, (others are available, todo). Based on Mbed's https-example. Tested on STM32F767 using ETHERNET and ESP8266 WIFI interfaces and STM32F446 using ESP8266 WIFI interface.
source/https_request.h@0:768ae9838086, 2020-01-23 (annotated)
- Committer:
- star297
- Date:
- Thu Jan 23 22:18:11 2020 +0000
- Revision:
- 0:768ae9838086
- Child:
- 1:4f2c1fcc6fb6
Initial version.
Who changed what in which revision?
User | Revision | Line number | New contents of line |
---|---|---|---|
star297 | 0:768ae9838086 | 1 | /* |
star297 | 0:768ae9838086 | 2 | * PackageLicenseDeclared: Apache-2.0 |
star297 | 0:768ae9838086 | 3 | * Copyright (c) 2017 ARM Limited |
star297 | 0:768ae9838086 | 4 | * |
star297 | 0:768ae9838086 | 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
star297 | 0:768ae9838086 | 6 | * you may not use this file except in compliance with the License. |
star297 | 0:768ae9838086 | 7 | * You may obtain a copy of the License at |
star297 | 0:768ae9838086 | 8 | * |
star297 | 0:768ae9838086 | 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
star297 | 0:768ae9838086 | 10 | * |
star297 | 0:768ae9838086 | 11 | * Unless required by applicable law or agreed to in writing, software |
star297 | 0:768ae9838086 | 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
star297 | 0:768ae9838086 | 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
star297 | 0:768ae9838086 | 14 | * See the License for the specific language governing permissions and |
star297 | 0:768ae9838086 | 15 | * limitations under the License. |
star297 | 0:768ae9838086 | 16 | */ |
star297 | 0:768ae9838086 | 17 | |
star297 | 0:768ae9838086 | 18 | #ifndef _MBED_HTTPS_REQUEST_H_ |
star297 | 0:768ae9838086 | 19 | #define _MBED_HTTPS_REQUEST_H_ |
star297 | 0:768ae9838086 | 20 | |
star297 | 0:768ae9838086 | 21 | #include <string> |
star297 | 0:768ae9838086 | 22 | #include <vector> |
star297 | 0:768ae9838086 | 23 | #include <map> |
star297 | 0:768ae9838086 | 24 | #include "http_request_base.h" |
star297 | 0:768ae9838086 | 25 | #include "TLSSocket.h" |
star297 | 0:768ae9838086 | 26 | |
star297 | 0:768ae9838086 | 27 | #ifndef HTTP_RECEIVE_BUFFER_SIZE |
star297 | 0:768ae9838086 | 28 | #define HTTP_RECEIVE_BUFFER_SIZE 8 * 1024 |
star297 | 0:768ae9838086 | 29 | #endif |
star297 | 0:768ae9838086 | 30 | |
star297 | 0:768ae9838086 | 31 | /** |
star297 | 0:768ae9838086 | 32 | * \brief HttpsRequest implements the logic for interacting with HTTPS servers. |
star297 | 0:768ae9838086 | 33 | */ |
star297 | 0:768ae9838086 | 34 | class HttpsRequest : public HttpRequestBase { |
star297 | 0:768ae9838086 | 35 | public: |
star297 | 0:768ae9838086 | 36 | /** |
star297 | 0:768ae9838086 | 37 | * HttpsRequest Constructor |
star297 | 0:768ae9838086 | 38 | * Initializes the TCP socket, sets up event handlers and flags. |
star297 | 0:768ae9838086 | 39 | * |
star297 | 0:768ae9838086 | 40 | * @param[in] network The network interface |
star297 | 0:768ae9838086 | 41 | * @param[in] ssl_ca_pem String containing the trusted CAs |
star297 | 0:768ae9838086 | 42 | * @param[in] method HTTP method to use |
star297 | 0:768ae9838086 | 43 | * @param[in] url URL to the resource |
star297 | 0:768ae9838086 | 44 | * @param[in] body_callback Callback on which to retrieve chunks of the response body. |
star297 | 0:768ae9838086 | 45 | If not set, the complete body will be allocated on the HttpResponse object, |
star297 | 0:768ae9838086 | 46 | which might use lots of memory. |
star297 | 0:768ae9838086 | 47 | */ |
star297 | 0:768ae9838086 | 48 | HttpsRequest(NetworkInterface* network, |
star297 | 0:768ae9838086 | 49 | const char* ssl_ca_pem, |
star297 | 0:768ae9838086 | 50 | http_method method, |
star297 | 0:768ae9838086 | 51 | const char* url, |
star297 | 0:768ae9838086 | 52 | Callback<void(const char *at, uint32_t length)> body_callback = 0) |
star297 | 0:768ae9838086 | 53 | : HttpRequestBase(NULL, body_callback) |
star297 | 0:768ae9838086 | 54 | { |
star297 | 0:768ae9838086 | 55 | _parsed_url = new ParsedUrl(url); |
star297 | 0:768ae9838086 | 56 | _request_builder = new HttpRequestBuilder(method, _parsed_url); |
star297 | 0:768ae9838086 | 57 | _response = NULL; |
star297 | 0:768ae9838086 | 58 | |
star297 | 0:768ae9838086 | 59 | _socket = new TLSSocket(); |
star297 | 0:768ae9838086 | 60 | ((TLSSocket*)_socket)->open(network); |
star297 | 0:768ae9838086 | 61 | ((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem); |
star297 | 0:768ae9838086 | 62 | _we_created_socket = true; |
star297 | 0:768ae9838086 | 63 | } |
star297 | 0:768ae9838086 | 64 | |
star297 | 0:768ae9838086 | 65 | /** |
star297 | 0:768ae9838086 | 66 | * HttpsRequest Constructor |
star297 | 0:768ae9838086 | 67 | * Sets up event handlers and flags. |
star297 | 0:768ae9838086 | 68 | * |
star297 | 0:768ae9838086 | 69 | * @param[in] socket A connected TLSSocket |
star297 | 0:768ae9838086 | 70 | * @param[in] method HTTP method to use |
star297 | 0:768ae9838086 | 71 | * @param[in] url URL to the resource |
star297 | 0:768ae9838086 | 72 | * @param[in] body_callback Callback on which to retrieve chunks of the response body. |
star297 | 0:768ae9838086 | 73 | If not set, the complete body will be allocated on the HttpResponse object, |
star297 | 0:768ae9838086 | 74 | which might use lots of memory. |
star297 | 0:768ae9838086 | 75 | */ |
star297 | 0:768ae9838086 | 76 | HttpsRequest(TLSSocket* socket, |
star297 | 0:768ae9838086 | 77 | http_method method, |
star297 | 0:768ae9838086 | 78 | const char* url, |
star297 | 0:768ae9838086 | 79 | Callback<void(const char *at, uint32_t length)> body_callback = 0) |
star297 | 0:768ae9838086 | 80 | : HttpRequestBase(socket, body_callback) |
star297 | 0:768ae9838086 | 81 | { |
star297 | 0:768ae9838086 | 82 | _parsed_url = new ParsedUrl(url); |
star297 | 0:768ae9838086 | 83 | _body_callback = body_callback; |
star297 | 0:768ae9838086 | 84 | _request_builder = new HttpRequestBuilder(method, _parsed_url); |
star297 | 0:768ae9838086 | 85 | _response = NULL; |
star297 | 0:768ae9838086 | 86 | |
star297 | 0:768ae9838086 | 87 | _we_created_socket = false; |
star297 | 0:768ae9838086 | 88 | } |
star297 | 0:768ae9838086 | 89 | |
star297 | 0:768ae9838086 | 90 | virtual ~HttpsRequest() {} |
star297 | 0:768ae9838086 | 91 | |
star297 | 0:768ae9838086 | 92 | protected: |
star297 | 0:768ae9838086 | 93 | virtual nsapi_error_t connect_socket(char *host, uint16_t port) { |
star297 | 0:768ae9838086 | 94 | return ((TLSSocket*)_socket)->connect(host, port); |
star297 | 0:768ae9838086 | 95 | } |
star297 | 0:768ae9838086 | 96 | }; |
star297 | 0:768ae9838086 | 97 | |
star297 | 0:768ae9838086 | 98 | #endif // _MBED_HTTPS_REQUEST_H_ |