-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathencode_url.cpp
More file actions
82 lines (71 loc) · 1.63 KB
/
encode_url.cpp
File metadata and controls
82 lines (71 loc) · 1.63 KB
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
//
// Copyright (c) 2025 Vinnie Falco (vinnie.falco@gmail.com)
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// Official repository: https://github.com/cppalliance/http
//
#include <boost/http/server/encode_url.hpp>
namespace boost {
namespace http {
namespace {
// Check if character needs encoding
// Unreserved + reserved chars that are allowed in URLs
bool
is_safe( char c ) noexcept
{
// Unreserved: A-Z a-z 0-9 - _ . ~
if( ( c >= 'A' && c <= 'Z' ) ||
( c >= 'a' && c <= 'z' ) ||
( c >= '0' && c <= '9' ) ||
c == '-' || c == '_' || c == '.' || c == '~' )
return true;
// Reserved chars allowed in URLs: ! # $ & ' ( ) * + , / : ; = ? @
switch( c )
{
case '!':
case '#':
case '$':
case '&':
case '\'':
case '(':
case ')':
case '*':
case '+':
case ',':
case '/':
case ':':
case ';':
case '=':
case '?':
case '@':
return true;
default:
return false;
}
}
constexpr char hex_chars[] = "0123456789ABCDEF";
} // (anon)
std::string
encode_url( core::string_view url )
{
std::string result;
result.reserve( url.size() );
for( unsigned char c : url )
{
if( is_safe( static_cast<char>( c ) ) )
{
result.push_back( static_cast<char>( c ) );
}
else
{
result.push_back( '%' );
result.push_back( hex_chars[c >> 4] );
result.push_back( hex_chars[c & 0x0F] );
}
}
return result;
}
} // http
} // boost