-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhttp_trace_context.h
More file actions
59 lines (49 loc) · 1.71 KB
/
Copy pathhttp_trace_context.h
File metadata and controls
59 lines (49 loc) · 1.71 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
#include <algorithm>
#include <string_view>
#include "httplib.h"
#include "pinpoint/tracer.h"
class HttpHeaderReader final : public pinpoint::HeaderReader {
public:
explicit HttpHeaderReader(const httplib::Headers& header) : headers_(header) {}
~HttpHeaderReader() override = default;
std::optional<std::string_view> Get(std::string_view key) const override {
auto it = headers_.find(std::string(key));
if (it == headers_.end()) {
return std::nullopt;
}
return std::string_view(it->second);
}
void ForEach(std::function<bool(std::string_view, std::string_view)> callback) const override {
for (const auto& pair : headers_) {
if (!callback(pair.first, pair.second)) {
break;
}
}
}
private:
const httplib::Headers& headers_;
};
class HttpHeaderReaderWriter final : public pinpoint::HeaderReaderWriter {
public:
explicit HttpHeaderReaderWriter(httplib::Headers& header) : headers_(header) {}
~HttpHeaderReaderWriter() override = default;
std::optional<std::string_view> Get(std::string_view key) const override {
auto it = headers_.find(std::string(key));
if (it == headers_.end()) {
return std::nullopt;
}
return std::string_view(it->second);
}
void ForEach(std::function<bool(std::string_view, std::string_view)> callback) const override {
for (const auto& pair : headers_) {
if (!callback(pair.first, pair.second)) {
break;
}
}
}
void Set(std::string_view key, std::string_view value) override {
headers_.emplace(key, value);
}
private:
httplib::Headers& headers_;
};