OpenVPN 3 Core Library
Loading...
Searching...
No Matches
header.hpp
Go to the documentation of this file.
1// OpenVPN -- An application to securely tunnel IP networks
2// over a single port, with support for SSL/TLS-based
3// session authentication and key exchange,
4// packet encryption, packet authentication, and
5// packet compression.
6//
7// Copyright (C) 2012- OpenVPN Inc.
8//
9// SPDX-License-Identifier: MPL-2.0 OR AGPL-3.0-only WITH openvpn3-openssl-exception
10//
11
12// Denote the data in an HTTP header
13
14#ifndef OPENVPN_HTTP_HEADER_H
15#define OPENVPN_HTTP_HEADER_H
16
17#include <string>
18#include <sstream>
19#include <utility>
20
24
25namespace openvpn::HTTP {
26
27struct Header
28{
29 Header() = default;
30
31 Header(std::string name_arg, std::string value_arg)
32 : name(std::move(name_arg)), value(std::move(value_arg))
33 {
34 }
35
36 bool name_match(const std::string &n) const
37 {
38 return string::strcasecmp(n, name) == 0;
39 }
40
41 std::string to_string() const
42 {
43 std::ostringstream out;
44 out << name << '=' << value;
45 return out.str();
46 }
47
48 std::string name;
49 std::string value;
50};
51
52struct HeaderList : public std::vector<Header>
53{
54 const Header *get(const std::string &key) const
55 {
56 for (auto &h : *this)
57 {
58 if (h.name_match(key))
59 return &h;
60 }
61 return nullptr;
62 }
63
64 Header *get(const std::string &key)
65 {
66 for (auto &h : *this)
67 {
68 if (h.name_match(key))
69 return &h;
70 }
71 return nullptr;
72 }
73
74 std::string get_value(const std::string &key) const
75 {
76 const Header *h = get(key);
77 if (h)
78 return h->value;
79 return std::string();
80 }
81
82 std::string get_value_trim(const std::string &key) const
83 {
84 return string::trim_copy(get_value(key));
85 }
86
87 std::string get_value_trim_lower(const std::string &key) const
88 {
90 }
91
92 std::string to_string() const
93 {
94 std::ostringstream out;
95 for (size_t i = 0; i < size(); ++i)
96 out << '[' << i << "] " << (*this)[i].to_string() << '\n';
97 return out.str();
98 }
99};
100
101} // namespace openvpn::HTTP
102
103#endif
int strcasecmp(const char *s1, const char *s2)
Definition string.hpp:33
std::string trim_copy(const std::string &str)
Definition string.hpp:507
std::string to_lower_copy(const std::string &str)
Definition string.hpp:535
std::string get_value(const std::string &key) const
Definition header.hpp:74
std::string get_value_trim_lower(const std::string &key) const
Definition header.hpp:87
std::string to_string() const
Definition header.hpp:92
Header * get(const std::string &key)
Definition header.hpp:64
const Header * get(const std::string &key) const
Definition header.hpp:54
std::string get_value_trim(const std::string &key) const
Definition header.hpp:82
std::string name
Definition header.hpp:48
bool name_match(const std::string &n) const
Definition header.hpp:36
std::string value
Definition header.hpp:49
Header(std::string name_arg, std::string value_arg)
Definition header.hpp:31
std::string to_string() const
Definition header.hpp:41
static std::stringstream out
Definition test_path.cpp:10