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{
30 {
31 }
32 Header(std::string name_arg, std::string value_arg)
33 : name(std::move(name_arg)), value(std::move(value_arg))
34 {
35 }
36
37 bool name_match(const std::string &n) const
38 {
39 return string::strcasecmp(n, name) == 0;
40 }
41
42 std::string to_string() const
43 {
44 std::ostringstream out;
45 out << name << '=' << value;
46 return out.str();
47 }
48
49 std::string name;
50 std::string value;
51};
52
53struct HeaderList : public std::vector<Header>
54{
55 const Header *get(const std::string &key) const
56 {
57 for (auto &h : *this)
58 {
59 if (h.name_match(key))
60 return &h;
61 }
62 return nullptr;
63 }
64
65 Header *get(const std::string &key)
66 {
67 for (auto &h : *this)
68 {
69 if (h.name_match(key))
70 return &h;
71 }
72 return nullptr;
73 }
74
75 std::string get_value(const std::string &key) const
76 {
77 const Header *h = get(key);
78 if (h)
79 return h->value;
80 else
81 return std::string();
82 }
83
84 std::string get_value_trim(const std::string &key) const
85 {
86 return string::trim_copy(get_value(key));
87 }
88
89 std::string get_value_trim_lower(const std::string &key) const
90 {
92 }
93
94 std::string to_string() const
95 {
96 std::ostringstream out;
97 for (size_t i = 0; i < size(); ++i)
98 out << '[' << i << "] " << (*this)[i].to_string() << std::endl;
99 return out.str();
100 }
101};
102
103} // namespace openvpn::HTTP
104
105#endif
int strcasecmp(const char *s1, const char *s2)
Definition string.hpp:29
std::string trim_copy(const std::string &str)
Definition string.hpp:559
std::string to_lower_copy(const std::string &str)
Definition string.hpp:587
std::string get_value(const std::string &key) const
Definition header.hpp:75
std::string get_value_trim_lower(const std::string &key) const
Definition header.hpp:89
std::string to_string() const
Definition header.hpp:94
Header * get(const std::string &key)
Definition header.hpp:65
const Header * get(const std::string &key) const
Definition header.hpp:55
std::string get_value_trim(const std::string &key) const
Definition header.hpp:84
std::string name
Definition header.hpp:49
bool name_match(const std::string &n) const
Definition header.hpp:37
std::string value
Definition header.hpp:50
Header(std::string name_arg, std::string value_arg)
Definition header.hpp:32
std::string to_string() const
Definition header.hpp:42
static std::stringstream out
Definition test_path.cpp:10