Skip to content

Commit 61f8626

Browse files
Add custom headers for network. (#716)
User can set default headers that will be added for each request made by network. Additionally, it concatenates the user agents set with default headers and user agents passed with network request. Relates-To: OLPEDGE-1669 Signed-off-by: Mykhailo Kuchma <[email protected]>
1 parent b3317e3 commit 61f8626

File tree

13 files changed

+403
-16
lines changed

13 files changed

+403
-16
lines changed

olp-cpp-sdk-core/CMakeLists.txt

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -218,6 +218,8 @@ set(OLP_SDK_CLIENT_SOURCES
218218
)
219219

220220
set(OLP_SDK_HTTP_SOURCES
221+
./src/http/DefaultNetwork.cpp
222+
./src/http/DefaultNetwork.h
221223
./src/http/Network.cpp
222224
./src/http/NetworkProxySettings.cpp
223225
./src/http/NetworkRequest.cpp

olp-cpp-sdk-core/include/olp/core/http/Network.h

Lines changed: 13 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -81,12 +81,23 @@ class CORE_API Network {
8181
* @param[in] id The unique RequestId of the request to be cancelled.
8282
*/
8383
virtual void Cancel(RequestId id) = 0;
84+
85+
/**
86+
* @brief Sets default network headers.
87+
*
88+
* Default headers are applied to each request passed to the `Send` method.
89+
* User agents are concatenated.
90+
*
91+
* @param headers The default headers.
92+
*/
93+
virtual void SetDefaultHeaders(Headers headers) {}
8494
};
8595

8696
/**
8797
* @brief Create default Network implementation.
8898
*/
89-
CORE_API std::shared_ptr<Network> CreateDefaultNetwork(size_t max_requests_count);
99+
CORE_API std::shared_ptr<Network> CreateDefaultNetwork(
100+
size_t max_requests_count);
90101

91102
} // namespace http
92103
} // namespace olp

olp-cpp-sdk-core/include/olp/core/http/NetworkRequest.h

Lines changed: 11 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -55,13 +55,20 @@ class CORE_API NetworkRequest final {
5555
explicit NetworkRequest(std::string url);
5656

5757
/**
58-
* @brief Get all HTTP headers.
59-
* @return vector of HTTP headers.
58+
* @brief Gets all HTTP headers.
59+
* @return The vector of the HTTP headers.
6060
*/
6161
const Headers& GetHeaders() const;
62+
63+
/**
64+
* @brief Gets the mutable reference to the HTTP headers.
65+
*
66+
* @return The mutable reference to vector of HTTP headers.
67+
*/
68+
Headers& GetMutableHeaders();
6269

6370
/**
64-
* @brief Add extra HTTP header.
71+
* @brief Adds an extra HTTP header.
6572
* @param[in] name Header name.
6673
* @param[in] value Header value.
6774
* @return reference to *this.

olp-cpp-sdk-core/include/olp/core/http/NetworkUtils.h

Lines changed: 13 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -21,6 +21,8 @@
2121

2222
#include <string>
2323

24+
#include <olp/core/http/NetworkTypes.h>
25+
2426
namespace olp {
2527
namespace http {
2628
/**
@@ -38,6 +40,16 @@ class NetworkUtils {
3840
static size_t CaseInsensitiveFind(const std::string& str1,
3941
const std::string& str2, size_t offset = 0);
4042

43+
/**
44+
* @brief Extract the user agent from the headers.
45+
*
46+
* User agent is removed from the headers.
47+
*
48+
* @param headers The input headers.
49+
*
50+
* @return The user agent or an empty string if there is no user agent.
51+
*/
52+
static std::string ExtractUserAgent(Headers& headers);
4153
}; // class NetworkUtils
4254

4355
std::string HttpErrorToString(int error);

olp-cpp-sdk-core/src/client/OlpClientSettingsFactory.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
Lines changed: 82 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,82 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#include <algorithm>
21+
22+
#include "DefaultNetwork.h"
23+
#include "olp/core/http/NetworkConstants.h"
24+
#include "olp/core/http/NetworkUtils.h"
25+
26+
namespace olp {
27+
namespace http {
28+
DefaultNetwork::DefaultNetwork(std::shared_ptr<Network> network)
29+
: network_{std::move(network)} {}
30+
31+
DefaultNetwork::~DefaultNetwork() = default;
32+
33+
SendOutcome DefaultNetwork::Send(NetworkRequest request, Payload payload,
34+
Callback callback,
35+
HeaderCallback header_callback,
36+
DataCallback data_callback) {
37+
{
38+
auto& request_headers = request.GetMutableHeaders();
39+
40+
std::unique_lock<std::mutex> lock(default_headers_mutex_);
41+
AppendUserAgent(request_headers);
42+
AppendDefaultHeaders(request_headers);
43+
}
44+
45+
return network_->Send(std::move(request), std::move(payload),
46+
std::move(callback), std::move(header_callback),
47+
std::move(data_callback));
48+
}
49+
50+
void DefaultNetwork::Cancel(RequestId id) { network_->Cancel(id); }
51+
52+
void DefaultNetwork::SetDefaultHeaders(Headers headers) {
53+
std::unique_lock<std::mutex> lock(default_headers_mutex_);
54+
default_headers_ = std::move(headers);
55+
user_agent_ = NetworkUtils::ExtractUserAgent(default_headers_);
56+
}
57+
58+
void DefaultNetwork::AppendUserAgent(Headers& request_headers) const {
59+
if (user_agent_.empty()) {
60+
return;
61+
}
62+
auto user_agent_it =
63+
std::find_if(std::begin(request_headers), std::end(request_headers),
64+
[](const Header& header) {
65+
return NetworkUtils::CaseInsensitiveCompare(
66+
header.first, kUserAgentHeader);
67+
});
68+
69+
if (user_agent_it != std::end(request_headers)) {
70+
user_agent_it->second.append(" ").append(user_agent_);
71+
} else {
72+
request_headers.emplace_back(kUserAgentHeader, user_agent_);
73+
}
74+
}
75+
76+
void DefaultNetwork::AppendDefaultHeaders(Headers& request_headers) const {
77+
request_headers.insert(request_headers.end(), default_headers_.begin(),
78+
default_headers_.end());
79+
}
80+
81+
} // namespace http
82+
} // namespace olp
Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/*
2+
* Copyright (C) 2020 HERE Europe B.V.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*
16+
* SPDX-License-Identifier: Apache-2.0
17+
* License-Filename: LICENSE
18+
*/
19+
20+
#pragma once
21+
22+
#include <mutex>
23+
24+
#include <olp/core/CoreApi.h>
25+
#include <olp/core/http/Network.h>
26+
27+
namespace olp {
28+
namespace http {
29+
30+
/**
31+
* @brief The default network class.
32+
*
33+
* Designed to provide default headers functionality on top of other `Network`
34+
* instances.
35+
*/
36+
class DefaultNetwork final : public Network {
37+
public:
38+
/**
39+
* @brief Creates the `DefaultNetwork` instance.
40+
*
41+
* @param network The `Network` instance that is used to handle `Send` and
42+
* `Cancel` methods.
43+
*/
44+
explicit DefaultNetwork(std::shared_ptr<Network> network);
45+
~DefaultNetwork() override;
46+
47+
/// Implements the `Send` method of the `Network` class.
48+
SendOutcome Send(NetworkRequest request, Payload payload, Callback callback,
49+
HeaderCallback header_callback = nullptr,
50+
DataCallback data_callback = nullptr) override;
51+
52+
/// Implements the `Cancel` method of the `Network` class.
53+
void Cancel(RequestId id) override;
54+
55+
/// Implements the `SetDefaultHeaders` method of the `Network` class.
56+
void SetDefaultHeaders(Headers headers) override;
57+
58+
private:
59+
void AppendUserAgent(Headers& request_headers) const;
60+
void AppendDefaultHeaders(Headers& request_headers) const;
61+
62+
std::shared_ptr<Network> network_;
63+
std::mutex default_headers_mutex_;
64+
Headers default_headers_;
65+
std::string user_agent_;
66+
};
67+
68+
} // namespace http
69+
} // namespace olp

olp-cpp-sdk-core/src/http/Network.cpp

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,6 +19,7 @@
1919

2020
#include "olp/core/http/Network.h"
2121

22+
#include "http/DefaultNetwork.h"
2223
#include "olp/core/utils/WarningWorkarounds.h"
2324

2425
#ifdef OLP_SDK_NETWORK_HAS_CURL
@@ -34,8 +35,8 @@
3435
namespace olp {
3536
namespace http {
3637

37-
CORE_API std::shared_ptr<Network> CreateDefaultNetwork(
38-
size_t max_requests_count) {
38+
namespace {
39+
std::shared_ptr<Network> CreateDefaultNetworkImpl(size_t max_requests_count) {
3940
CORE_UNUSED(max_requests_count);
4041
#ifdef OLP_SDK_NETWORK_HAS_CURL
4142
return std::make_shared<NetworkCurl>(max_requests_count);
@@ -49,6 +50,16 @@ CORE_API std::shared_ptr<Network> CreateDefaultNetwork(
4950
static_assert(false, "No default network implementation provided");
5051
#endif
5152
}
53+
} // namespace
54+
55+
CORE_API std::shared_ptr<Network> CreateDefaultNetwork(
56+
size_t max_requests_count) {
57+
auto network = CreateDefaultNetworkImpl(max_requests_count);
58+
if (network) {
59+
return std::make_shared<DefaultNetwork>(network);
60+
}
61+
return nullptr;
62+
}
5263

5364
} // namespace http
5465
} // namespace olp

olp-cpp-sdk-core/src/http/NetworkRequest.cpp

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -25,6 +25,8 @@ NetworkRequest::NetworkRequest(std::string url) : url_{std::move(url)} {}
2525

2626
const Headers& NetworkRequest::GetHeaders() const { return headers_; }
2727

28+
Headers& NetworkRequest::GetMutableHeaders() { return headers_; }
29+
2830
const std::string& NetworkRequest::GetUrl() const { return url_; }
2931

3032
NetworkRequest::HttpVerb NetworkRequest::GetVerb() const { return verb_; }

olp-cpp-sdk-core/src/http/NetworkUtils.cpp

Lines changed: 23 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2019 HERE Europe B.V.
2+
* Copyright (C) 2019-2020 HERE Europe B.V.
33
*
44
* Licensed under the Apache License, Version 2.0 (the "License");
55
* you may not use this file except in compliance with the License.
@@ -19,7 +19,9 @@
1919

2020
#include "olp/core/http/NetworkUtils.h"
2121

22-
#include <string>
22+
#include <algorithm>
23+
24+
#include "olp/core/http/NetworkConstants.h"
2325

2426
namespace olp {
2527
namespace http {
@@ -71,6 +73,25 @@ size_t NetworkUtils::CaseInsensitiveFind(const std::string& str1,
7173
return std::string::npos;
7274
}
7375

76+
std::string NetworkUtils::ExtractUserAgent(Headers& headers) {
77+
std::string user_agent;
78+
79+
auto user_agent_it =
80+
std::find_if(headers.begin(), headers.end(),
81+
[](const Header& header_pair) {
82+
return NetworkUtils::CaseInsensitiveCompare(
83+
header_pair.first, kUserAgentHeader);
84+
});
85+
86+
if (user_agent_it != headers.end()) {
87+
user_agent = std::move((*user_agent_it).second);
88+
headers.erase(user_agent_it);
89+
}
90+
91+
return user_agent;
92+
}
93+
94+
7495
std::string HttpErrorToString(int error) {
7596
switch (error) {
7697
case 100:

0 commit comments

Comments
 (0)