Skip to content

Commit b458435

Browse files
Make Date header parsing locale-independent (#1667)
* Make Date header parsing locale-independent Use boost::date_time to ensure cross platform compatibility. Supersedes the b877dc4 Relates-To: HERESUP-57013 Signed-off-by: Mykhailo Diachenko <ext-mykhailo.z.diachenko@here.com> * Fix Review comments - increased coverage - updated copyright headers - add leap seconds boundary Relates-To: HERESUP-57013 Signed-off-by: Mykhailo Diachenko <ext-mykhailo.z.diachenko@here.com> --------- Signed-off-by: Mykhailo Diachenko <ext-mykhailo.z.diachenko@here.com>
1 parent c08da4f commit b458435

File tree

6 files changed

+507
-46
lines changed

6 files changed

+507
-46
lines changed

external/boost/CMakeLists.txt.boost.in

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,11 +24,16 @@ include(ExternalProject)
2424
ExternalProject_Add(boost-download
2525
GIT_REPOSITORY @OLP_SDK_CPP_BOOST_URL@
2626
GIT_TAG @OLP_SDK_CPP_BOOST_TAG@
27-
GIT_SUBMODULES libs/any
27+
GIT_SUBMODULES libs/algorithm
28+
libs/any
29+
libs/array
2830
libs/assert
31+
libs/concept_check
2932
libs/config
33+
libs/container
3034
libs/container_hash
3135
libs/core
36+
libs/date_time
3237
libs/detail
3338
libs/describe
3439
libs/format
@@ -37,6 +42,7 @@ ExternalProject_Add(boost-download
3742
libs/integer
3843
libs/io
3944
libs/iterator
45+
libs/lexical_cast
4046
libs/move
4147
libs/mpl
4248
libs/mp11
@@ -45,10 +51,12 @@ ExternalProject_Add(boost-download
4551
libs/predef
4652
libs/preprocessor
4753
libs/random
54+
libs/range
4855
libs/serialization
4956
libs/smart_ptr
5057
libs/static_assert
5158
libs/throw_exception
59+
libs/tokenizer
5260
libs/tti
5361
libs/type_index
5462
libs/type_traits

olp-cpp-sdk-authentication/src/AuthenticationClientUtils.cpp

Lines changed: 3 additions & 45 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,5 @@
11
/*
2-
* Copyright (C) 2020-2024 HERE Europe B.V.
2+
* Copyright (C) 2020-2026 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.
@@ -32,6 +32,7 @@
3232
#include <rapidjson/writer.h>
3333
#include "Constants.h"
3434
#include "ResponseFromJsonBuilder.h"
35+
#include "Rfc1123Helper.h"
3536
#include "olp/core/http/NetworkResponse.h"
3637
#include "olp/core/http/NetworkUtils.h"
3738
#include "olp/core/logging/Log.h"
@@ -58,7 +59,6 @@ constexpr auto kOauthTimestamp = "oauth_timestamp";
5859
constexpr auto kOauthSignatureMethod = "oauth_signature_method";
5960
constexpr auto kVersion = "1.0";
6061
constexpr auto kHmac = "HMAC-SHA256";
61-
constexpr auto kLogTag = "AuthenticationClientUtils";
6262

6363
std::string Base64Encode(const Crypto::Sha256Digest& digest) {
6464
std::string ret = olp::utils::Base64Encode(digest.data(), digest.size());
@@ -98,52 +98,10 @@ namespace client = olp::client;
9898

9999
constexpr auto kDate = "date";
100100

101-
#ifdef _WIN32
102-
// Windows does not have ::strptime and ::timegm
103101
std::time_t ParseTime(const std::string& value) {
104-
std::tm tm = {};
105-
std::istringstream ss(value);
106-
ss >> std::get_time(&tm, "%a, %d %b %Y %H:%M:%S %z");
107-
return _mkgmtime(&tm);
102+
return internal::ParseRfc1123GmtNoExceptions(value);
108103
}
109104

110-
#else
111-
112-
std::string TrimDateHeaderValue(const std::string& value) {
113-
const auto begin = value.find_first_not_of(" \t\r\n");
114-
if (begin == std::string::npos) {
115-
return {};
116-
}
117-
const auto end = value.find_last_not_of(" \t\r\n");
118-
return value.substr(begin, end - begin + 1);
119-
}
120-
121-
std::time_t ParseTime(const std::string& value) {
122-
std::tm tm = {};
123-
const auto trimmed_value = TrimDateHeaderValue(value);
124-
125-
// Use a C locale to keep RFC1123 parsing locale-independent.
126-
// Literal "GMT" avoids platform-specific %Z behaviour.
127-
locale_t c_locale = newlocale(LC_ALL_MASK, "C", (locale_t)0);
128-
if (c_locale == (locale_t)0) {
129-
OLP_SDK_LOG_WARNING(kLogTag, "Failed to create C locale");
130-
return static_cast<std::time_t>(-1);
131-
}
132-
133-
const auto parsed_until = ::strptime_l(
134-
trimmed_value.c_str(), "%a, %d %b %Y %H:%M:%S GMT", &tm, c_locale);
135-
freelocale(c_locale);
136-
137-
if (parsed_until != trimmed_value.c_str() + trimmed_value.size()) {
138-
OLP_SDK_LOG_WARNING(kLogTag, "Timestamp is not fully parsed " << value);
139-
return static_cast<std::time_t>(-1);
140-
}
141-
142-
return timegm(&tm);
143-
}
144-
145-
#endif
146-
147105
porting::optional<std::time_t> GetTimestampFromHeaders(
148106
const olp::http::Headers& headers) {
149107
auto it =

0 commit comments

Comments
 (0)