Skip to content

Commit 17f71bc

Browse files
committed
connection_pool: extract effective_port into its own header
1 parent be325cb commit 17f71bc

4 files changed

Lines changed: 133 additions & 18 deletions

File tree

src/detail/connection_pool.cpp

Lines changed: 1 addition & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111

1212
#include <boost/burl/error.hpp>
1313

14+
#include "effective_port.hpp"
1415
#include "http_tunnel.hpp"
1516
#include "socks5_tunnel.hpp"
1617

@@ -39,24 +40,6 @@ namespace detail
3940
namespace
4041
{
4142

42-
std::string_view
43-
effective_port(const urls::url_view& url)
44-
{
45-
if(url.has_port())
46-
return url.port();
47-
48-
if(url.scheme() == "https")
49-
return "443";
50-
51-
if(url.scheme() == "http")
52-
return "80";
53-
54-
if(url.scheme() == "socks5" || url.scheme() == "socks5h")
55-
return "1080";
56-
57-
return {};
58-
}
59-
6043
std::string
6144
origin(urls::url_view url)
6245
{

src/detail/effective_port.cpp

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
//
2+
// Copyright (c) 2026 Mohammad Nejati
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/burl
8+
//
9+
10+
#include "effective_port.hpp"
11+
12+
namespace boost
13+
{
14+
namespace burl
15+
{
16+
namespace detail
17+
{
18+
19+
std::string_view
20+
effective_port(const urls::url_view& url) noexcept
21+
{
22+
if(url.has_port())
23+
return url.port();
24+
25+
if(url.scheme() == "https")
26+
return "443";
27+
28+
if(url.scheme() == "http")
29+
return "80";
30+
31+
if(url.scheme() == "socks5" || url.scheme() == "socks5h")
32+
return "1080";
33+
34+
return {};
35+
}
36+
37+
} // namespace detail
38+
} // namespace burl
39+
} // namespace boost

src/detail/effective_port.hpp

Lines changed: 31 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,31 @@
1+
//
2+
// Copyright (c) 2026 Mohammad Nejati
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/burl
8+
//
9+
10+
#ifndef BOOST_BURL_SRC_DETAIL_EFFECTIVE_PORT_HPP
11+
#define BOOST_BURL_SRC_DETAIL_EFFECTIVE_PORT_HPP
12+
13+
#include <boost/url/url_view.hpp>
14+
15+
#include <string_view>
16+
17+
namespace boost
18+
{
19+
namespace burl
20+
{
21+
namespace detail
22+
{
23+
24+
std::string_view
25+
effective_port(const urls::url_view& url) noexcept;
26+
27+
} // namespace detail
28+
} // namespace burl
29+
} // namespace boost
30+
31+
#endif
Lines changed: 62 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// Copyright (c) 2026 Mohammad Nejati
3+
//
4+
// Distributed under the Boost Software License, Version 1.0. (See accompanying
5+
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
6+
//
7+
// Official repository: https://github.com/cppalliance/burl
8+
//
9+
10+
// Test that header file is self-contained.
11+
#include "src/detail/effective_port.hpp"
12+
13+
#include "test_suite.hpp"
14+
15+
namespace boost
16+
{
17+
namespace burl
18+
{
19+
namespace detail
20+
{
21+
22+
class effective_port_test
23+
{
24+
public:
25+
void
26+
testExplicitPort()
27+
{
28+
BOOST_TEST_EQ(effective_port("http://example.com:8080"), "8080");
29+
BOOST_TEST_EQ(effective_port("ftp://example.com:21"), "21");
30+
BOOST_TEST_EQ(effective_port("http://example.com:80"), "80");
31+
}
32+
33+
void
34+
testDefaultPort()
35+
{
36+
BOOST_TEST_EQ(effective_port("http://example.com"), "80");
37+
BOOST_TEST_EQ(effective_port("https://example.com"), "443");
38+
BOOST_TEST_EQ(effective_port("socks5://example.com"), "1080");
39+
BOOST_TEST_EQ(effective_port("socks5h://example.com"), "1080");
40+
}
41+
42+
void
43+
testUnknownScheme()
44+
{
45+
BOOST_TEST_EQ(effective_port("ftp://example.com"), "");
46+
BOOST_TEST_EQ(effective_port("ws://example.com"), "");
47+
}
48+
49+
void
50+
run()
51+
{
52+
testExplicitPort();
53+
testDefaultPort();
54+
testUnknownScheme();
55+
}
56+
};
57+
58+
TEST_SUITE(effective_port_test, "boost.burl.detail.effective_port");
59+
60+
} // namespace detail
61+
} // namespace burl
62+
} // namespace boost

0 commit comments

Comments
 (0)