-
Notifications
You must be signed in to change notification settings - Fork 775
/
Copy pathNetUtil.hpp
142 lines (112 loc) · 4.27 KB
/
NetUtil.hpp
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
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
/* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4; fill-column: 100 -*- */
/*
* Copyright the Collabora Online contributors.
*
* SPDX-License-Identifier: MPL-2.0
*
* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/.
*/
#pragma once
#include <chrono>
#include <functional>
#include <string>
#include <memory>
#include <vector>
// This file hosts network related common functionality
// and helper/utility functions and classes.
// HTTP-specific helpers are in HttpHelper.hpp.
class StreamSocket;
class ProtocolHandlerInterface;
struct addrinfo;
struct sockaddr;
namespace net
{
class DefaultValues
{
public:
/// StreamSocket inactivity timeout in us (3600s default). Zero disables instrument.
std::chrono::microseconds inactivityTimeout;
/// WebSocketHandler average ping timeout in us (12s default). Zero disables instrument.
std::chrono::microseconds wsPingAvgTimeout;
/// WebSocketHandler ping interval in us (18s default), i.e. duration until next ping. Zero disables instrument.
std::chrono::microseconds wsPingInterval;
/// Maximum number of concurrent TCP connections. Zero disables instrument.
size_t maxTCPConnections;
};
extern DefaultValues Defaults;
class HostEntry
{
std::string _requestName;
std::string _canonicalName;
std::vector<std::string> _ipAddresses;
std::shared_ptr<addrinfo> _ainfo;
int _errno;
int _eaino;
void setEAI(int eaino);
std::string makeIPAddress(const sockaddr* ai_addr);
public:
HostEntry(const std::string& desc, const char* port);
~HostEntry();
bool good() const { return _errno == 0 && _eaino == 0; }
std::string errorMessage() const;
const std::string& getCanonicalName() const { return _canonicalName; }
const std::vector<std::string>& getAddresses() const { return _ipAddresses; }
const addrinfo* getAddrInfo() const { return _ainfo.get(); }
std::string resolveHostAddress() const;
bool isLocalhost() const;
};
#if !MOBILEAPP
/// Resolves the IP of the given hostname. On failure, returns @targetHost.
std::string resolveHostAddress(const std::string& targetHost);
/// Returns true if @targetHost is on the same host.
bool isLocalhost(const std::string& targetHost);
/// Returns the canonical host name of the given IP address or host name.
std::string canonicalHostName(const std::string& addressToCheck);
/// Returns a vector containing the IPAddresses for the host.
std::vector<std::string> resolveAddresses(const std::string& addressToCheck);
#endif
/// Connect to an end-point at the given host and port and return StreamSocket.
std::shared_ptr<StreamSocket>
connect(const std::string& host, const std::string& port, const bool isSSL,
const std::shared_ptr<ProtocolHandlerInterface>& protocolHandler);
typedef std::function<void(std::shared_ptr<StreamSocket>)> asyncConnectCB;
void
asyncConnect(const std::string& host, const std::string& port, const bool isSSL,
const std::shared_ptr<ProtocolHandlerInterface>& protocolHandler,
const asyncConnectCB& asyncCb);
/// Connect to an end-point at the given @uri and return StreamSocket.
std::shared_ptr<StreamSocket>
connect(std::string uri, const std::shared_ptr<ProtocolHandlerInterface>& protocolHandler);
/// Decomposes a URI into its components.
/// Returns true if parsing was successful.
bool parseUri(std::string uri, std::string& scheme, std::string& host, std::string& port,
std::string& url);
/// Decomposes a URI into its components.
/// Returns true if parsing was successful.
inline bool parseUri(std::string uri, std::string& scheme, std::string& host, std::string& port)
{
std::string url;
return parseUri(std::move(uri), scheme, host, port, url);
}
/// Return the locator given a URI.
inline std::string parseUrl(const std::string& uri)
{
auto itScheme = uri.find("://");
if (itScheme != uri.npos)
{
itScheme += 3; // Skip it.
}
else
{
itScheme = 0;
}
const auto itUrl = uri.find('/', itScheme);
if (itUrl != uri.npos)
{
return uri.substr(itUrl); // Including the first foreslash.
}
return std::string();
}
} // namespace net