Skip to content

Commit fe4faba

Browse files
committed
[refactor] move SplitHostPort() into utilstrencodings
This moves SplitHostPort from libbitcoin_common to libbitcoin_util so it is available to bitcoin-cli.
1 parent ca4c545 commit fe4faba

File tree

6 files changed

+22
-20
lines changed

6 files changed

+22
-20
lines changed

src/httpserver.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@
77
#include "chainparamsbase.h"
88
#include "compat.h"
99
#include "util.h"
10+
#include "utilstrencodings.h"
1011
#include "netbase.h"
1112
#include "rpc/protocol.h" // For HTTP status codes
1213
#include "sync.h"

src/netbase.cpp

Lines changed: 0 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -58,25 +58,6 @@ std::string GetNetworkName(enum Network net) {
5858
}
5959
}
6060

61-
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
62-
size_t colon = in.find_last_of(':');
63-
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
64-
bool fHaveColon = colon != in.npos;
65-
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
66-
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
67-
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
68-
int32_t n;
69-
if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
70-
in = in.substr(0, colon);
71-
portOut = n;
72-
}
73-
}
74-
if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
75-
hostOut = in.substr(1, in.size()-2);
76-
else
77-
hostOut = in;
78-
}
79-
8061
bool static LookupIntern(const char *pszName, std::vector<CNetAddr>& vIP, unsigned int nMaxSolutions, bool fAllowLookup)
8162
{
8263
vIP.clear();

src/netbase.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -39,7 +39,6 @@ class proxyType
3939

4040
enum Network ParseNetwork(std::string net);
4141
std::string GetNetworkName(enum Network net);
42-
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
4342
bool SetProxy(enum Network net, const proxyType &addrProxy);
4443
bool GetProxy(enum Network net, proxyType &proxyInfoOut);
4544
bool IsProxy(const CNetAddr &addr);

src/test/netbase_tests.cpp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44

55
#include "netbase.h"
66
#include "test/test_bitcoin.h"
7+
#include "utilstrencodings.h"
78

89
#include <string>
910

src/utilstrencodings.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,25 @@ std::vector<unsigned char> ParseHex(const std::string& str)
9191
return ParseHex(str.c_str());
9292
}
9393

94+
void SplitHostPort(std::string in, int &portOut, std::string &hostOut) {
95+
size_t colon = in.find_last_of(':');
96+
// if a : is found, and it either follows a [...], or no other : is in the string, treat it as port separator
97+
bool fHaveColon = colon != in.npos;
98+
bool fBracketed = fHaveColon && (in[0]=='[' && in[colon-1]==']'); // if there is a colon, and in[0]=='[', colon is not 0, so in[colon-1] is safe
99+
bool fMultiColon = fHaveColon && (in.find_last_of(':',colon-1) != in.npos);
100+
if (fHaveColon && (colon==0 || fBracketed || !fMultiColon)) {
101+
int32_t n;
102+
if (ParseInt32(in.substr(colon + 1), &n) && n > 0 && n < 0x10000) {
103+
in = in.substr(0, colon);
104+
portOut = n;
105+
}
106+
}
107+
if (in.size()>0 && in[0] == '[' && in[in.size()-1] == ']')
108+
hostOut = in.substr(1, in.size()-2);
109+
else
110+
hostOut = in;
111+
}
112+
94113
std::string EncodeBase64(const unsigned char* pch, size_t len)
95114
{
96115
static const char *pbase64 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/";

src/utilstrencodings.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -48,6 +48,7 @@ std::string DecodeBase32(const std::string& str);
4848
std::string EncodeBase32(const unsigned char* pch, size_t len);
4949
std::string EncodeBase32(const std::string& str);
5050

51+
void SplitHostPort(std::string in, int &portOut, std::string &hostOut);
5152
std::string i64tostr(int64_t n);
5253
std::string itostr(int n);
5354
int64_t atoi64(const char* psz);

0 commit comments

Comments
 (0)