forked from apache/arrow
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathcookie_internal.h
More file actions
126 lines (107 loc) · 4.13 KB
/
Copy pathcookie_internal.h
File metadata and controls
126 lines (107 loc) · 4.13 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
// Licensed to the Apache Software Foundation (ASF) under one
// or more contributor license agreements. See the NOTICE file
// distributed with this work for additional information
// regarding copyright ownership. The ASF licenses this file
// to you under the Apache License, Version 2.0 (the
// "License"); you may not use this file except in compliance
// with the License. You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing,
// software distributed under the License is distributed on an
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
// KIND, either express or implied. See the License for the
// specific language governing permissions and limitations
// under the License.
// Utilities for working with HTTP cookies.
#pragma once
#include <chrono>
#include <mutex>
#include <optional>
#include <string>
#include <string_view>
#include <unordered_map>
#include <utility>
#include "arrow/flight/client_middleware.h"
#include "arrow/result.h"
namespace arrow {
namespace flight {
namespace internal {
/// \brief Case insensitive comparator for use by cookie caching map. Cookies are not
/// case sensitive.
class ARROW_FLIGHT_EXPORT CaseInsensitiveComparator {
public:
bool operator()(const std::string& t1, const std::string& t2) const;
};
/// \brief Case insensitive hasher for use by cookie caching map. Cookies are not
/// case sensitive.
class ARROW_FLIGHT_EXPORT CaseInsensitiveHash {
public:
size_t operator()(const std::string& key) const;
};
/// \brief Class to represent a cookie.
class ARROW_FLIGHT_EXPORT Cookie {
public:
/// \brief Parse function to parse a cookie header value and return a Cookie object.
///
/// \return Cookie object based on cookie header value.
static Cookie Parse(std::string_view cookie_header_value);
/// \brief Parse a cookie header string beginning at the given start_pos and identify
/// the name and value of an attribute.
///
/// \param cookie_header_value The value of the Set-Cookie header.
/// \param[out] start_pos An input/output parameter indicating the starting position
/// of the attribute. It will store the position of the next attribute when the
/// function returns.
///
/// \return Optional cookie key value pair.
static std::optional<std::pair<std::string, std::string>> ParseCookieAttribute(
const std::string& cookie_header_value, std::string::size_type* start_pos);
/// \brief Function to fix cookie format date string so it is accepted by Windows
///
/// parsers.
/// \param date Date to fix.
static void ConvertCookieDate(std::string* date);
/// \brief Function to check if the cookie has expired.
///
/// \return Returns true if the cookie has expired.
bool IsExpired() const;
/// \brief Function to get cookie as a string.
///
/// \return Cookie as a string.
std::string AsCookieString() const;
/// \brief Function to get name of the cookie as a string.
///
/// \return Name of the cookie as a string.
std::string GetName() const;
private:
std::string cookie_name_;
std::string cookie_value_;
std::chrono::time_point<std::chrono::system_clock> expiration_time_;
bool has_expiry_;
};
/// \brief Class to handle updating a cookie cache.
class ARROW_FLIGHT_EXPORT CookieCache {
public:
/// \brief Updates the cache of cookies with new Set-Cookie header values.
///
/// \param incoming_headers The range representing header values.
void UpdateCachedCookies(const CallHeaders& incoming_headers);
/// \brief Retrieve the cached cookie values as a string. This function discards
/// cookies that have expired.
///
/// \return a string that can be used in a Cookie header representing the cookies that
/// have been cached.
std::string GetValidCookiesAsString();
private:
/// \brief Removes cookies that are marked as expired from the cache.
void DiscardExpiredCookies();
// Mutex must be used to protect cookie cache.
std::mutex mutex_;
std::unordered_map<std::string, Cookie, CaseInsensitiveHash, CaseInsensitiveComparator>
cookies;
};
} // namespace internal
} // namespace flight
} // namespace arrow