|
| 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 |
0 commit comments