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 http_request.h Source File

http_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 _HTTP_REQUEST_
00019 #define _HTTP_REQUEST_
00020 
00021 #include <string>
00022 #include <vector>
00023 #include <map>
00024 #include "http_request_base.h"
00025 #include "http_parsed_url.h"
00026 #include "TCPSocket.h"
00027 
00028 /**
00029  * @todo:
00030  *      - Userinfo parameter is not handled
00031  */
00032 
00033 #ifndef HTTP_RECEIVE_BUFFER_SIZE
00034 #define HTTP_RECEIVE_BUFFER_SIZE 8 * 1024
00035 #endif
00036 
00037 /**
00038  * \brief HttpRequest implements the logic for interacting with HTTP servers.
00039  */
00040 class HttpRequest : public HttpRequestBase {
00041 public:
00042     friend class HttpRequestBase;
00043 
00044     /**
00045      * HttpRequest Constructor
00046      *
00047      * @param[in] network The network interface
00048      * @param[in] method HTTP method to use
00049      * @param[in] url URL to the resource
00050      * @param[in] bodyCallback Callback on which to retrieve chunks of the response body.
00051                                If not set, the complete body will be allocated on the HttpResponse object,
00052                                which might use lots of memory.
00053     */
00054     HttpRequest(NetworkInterface* network, http_method method, const char* url, Callback<void(const char *at, uint32_t length)> bodyCallback = 0)
00055         : HttpRequestBase(NULL, bodyCallback)
00056     {
00057         _error = 0;
00058         _response = NULL;
00059 
00060         _parsed_url = new ParsedUrl(url);
00061         _request_builder = new HttpRequestBuilder(method, _parsed_url);
00062 
00063         _socket = new TCPSocket();
00064         ((TCPSocket*)_socket)->open(network);
00065         _we_created_socket = true;
00066     }
00067 
00068     /**
00069      * HttpRequest Constructor
00070      *
00071      * @param[in] socket An open TCPSocket
00072      * @param[in] method HTTP method to use
00073      * @param[in] url URL to the resource
00074      * @param[in] bodyCallback Callback on which to retrieve chunks of the response body.
00075                                 If not set, the complete body will be allocated on the HttpResponse object,
00076                                 which might use lots of memory.
00077     */
00078     HttpRequest(TCPSocket* socket, http_method method, const char* url, Callback<void(const char *at, uint32_t length)> bodyCallback = 0)
00079         : HttpRequestBase(socket, bodyCallback)
00080     {
00081         _error = 0;
00082         _response = NULL;
00083 
00084         _parsed_url = new ParsedUrl(url);
00085         _request_builder = new HttpRequestBuilder(method, _parsed_url);
00086 
00087         _we_created_socket = false;
00088     }
00089 
00090     virtual ~HttpRequest() {
00091     }
00092 
00093 protected:
00094 
00095     virtual nsapi_error_t connect_socket(char *host, uint16_t port) {
00096         return ((TCPSocket*)_socket)->connect(host, port);
00097     }
00098 };
00099 
00100 #endif // _HTTP_REQUEST_