HTTP and HTTPS library for Mbed OS 5

Dependents:   MQTTGateway2 MQTTGatewayK64 http-example-wnc GuardRoom ... more

Embed: (wiki syntax)

« Back to documentation index

Show/hide line numbers https_request.h Source File

https_request.h

00001 /*
00002  * PackageLicenseDeclared: Apache-2.0
00003  * Copyright (c) 2017 ARM Limited
00004  *
00005  * Licensed under the Apache License, Version 2.0 (the "License");
00006  * you may not use this file except in compliance with the License.
00007  * You may obtain a copy of the License at
00008  *
00009  *     http://www.apache.org/licenses/LICENSE-2.0
00010  *
00011  * Unless required by applicable law or agreed to in writing, software
00012  * distributed under the License is distributed on an "AS IS" BASIS,
00013  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
00014  * See the License for the specific language governing permissions and
00015  * limitations under the License.
00016  */
00017 
00018 #ifndef _MBED_HTTPS_REQUEST_H_
00019 #define _MBED_HTTPS_REQUEST_H_
00020 
00021 #include <string>
00022 #include <vector>
00023 #include <map>
00024 #include "http_request_base.h"
00025 #include "TLSSocket.h"
00026 
00027 #ifndef HTTP_RECEIVE_BUFFER_SIZE
00028 #define HTTP_RECEIVE_BUFFER_SIZE 8 * 1024
00029 #endif
00030 
00031 /**
00032  * \brief HttpsRequest implements the logic for interacting with HTTPS servers.
00033  */
00034 class HttpsRequest : public HttpRequestBase {
00035 public:
00036     /**
00037      * HttpsRequest Constructor
00038      * Initializes the TCP socket, sets up event handlers and flags.
00039      *
00040      * @param[in] network The network interface
00041      * @param[in] ssl_ca_pem String containing the trusted CAs
00042      * @param[in] method HTTP method to use
00043      * @param[in] url URL to the resource
00044      * @param[in] body_callback Callback on which to retrieve chunks of the response body.
00045                                 If not set, the complete body will be allocated on the HttpResponse object,
00046                                 which might use lots of memory.
00047      */
00048     HttpsRequest(NetworkInterface* network,
00049                  const char* ssl_ca_pem,
00050                  http_method method,
00051                  const char* url,
00052                  Callback<void(const char *at, uint32_t length)> body_callback = 0)
00053         : HttpRequestBase(NULL, body_callback)
00054     {
00055         _parsed_url = new ParsedUrl(url);
00056         _request_builder = new HttpRequestBuilder(method, _parsed_url);
00057         _response = NULL;
00058 
00059         _socket = new TLSSocket();
00060         ((TLSSocket*)_socket)->open(network);
00061         ((TLSSocket*)_socket)->set_root_ca_cert(ssl_ca_pem);
00062         _we_created_socket = true;
00063     }
00064 
00065     /**
00066      * HttpsRequest Constructor
00067      * Sets up event handlers and flags.
00068      *
00069      * @param[in] socket A connected TLSSocket
00070      * @param[in] method HTTP method to use
00071      * @param[in] url URL to the resource
00072      * @param[in] body_callback Callback on which to retrieve chunks of the response body.
00073                                 If not set, the complete body will be allocated on the HttpResponse object,
00074                                 which might use lots of memory.
00075      */
00076     HttpsRequest(TLSSocket* socket,
00077                  http_method method,
00078                  const char* url,
00079                  Callback<void(const char *at, uint32_t length)> body_callback = 0)
00080         : HttpRequestBase(socket, body_callback)
00081     {
00082         _parsed_url = new ParsedUrl(url);
00083         _body_callback = body_callback;
00084         _request_builder = new HttpRequestBuilder(method, _parsed_url);
00085         _response = NULL;
00086 
00087         _we_created_socket = false;
00088     }
00089 
00090     virtual ~HttpsRequest() {}
00091 
00092 protected:
00093     virtual nsapi_error_t connect_socket(char *host, uint16_t port) {
00094         return ((TLSSocket*)_socket)->connect(host, port);
00095     }
00096 };
00097 
00098 #endif // _MBED_HTTPS_REQUEST_H_