-
Notifications
You must be signed in to change notification settings - Fork 14
Expand file tree
/
Copy pathrange_parser.cpp
More file actions
191 lines (157 loc) · 4.03 KB
/
range_parser.cpp
File metadata and controls
191 lines (157 loc) · 4.03 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
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
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
//
// 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/range_parser.hpp>
#include <algorithm>
#include <cctype>
#include <charconv>
namespace boost {
namespace http {
namespace {
// Skip whitespace
void
skip_ws( core::string_view& s ) noexcept
{
while( ! s.empty() && std::isspace(
static_cast<unsigned char>( s.front() ) ) )
s.remove_prefix( 1 );
}
// Parse integer
bool
parse_int( core::string_view& s, std::int64_t& out ) noexcept
{
skip_ws( s );
if( s.empty() )
return false;
auto const* begin = s.data();
auto const* end = s.data() + s.size();
auto [ptr, ec] = std::from_chars( begin, end, out );
if( ec != std::errc() || ptr == begin )
return false;
s.remove_prefix( static_cast<std::size_t>( ptr - begin ) );
return true;
}
// Parse a single range spec: "start-end" or "-suffix" or "start-"
bool
parse_range_spec(
core::string_view& s,
std::int64_t size,
byte_range& out )
{
skip_ws( s );
if( s.empty() )
return false;
std::int64_t start = -1;
std::int64_t end = -1;
// Check for suffix range: "-suffix"
if( s.front() == '-' )
{
s.remove_prefix( 1 );
std::int64_t suffix;
if( ! parse_int( s, suffix ) || suffix < 0 )
return false;
// Last 'suffix' bytes
if( suffix == 0 )
return false;
if( suffix > size )
suffix = size;
out.start = size - suffix;
out.end = size - 1;
return true;
}
// Parse start
if( ! parse_int( s, start ) || start < 0 )
return false;
skip_ws( s );
if( s.empty() || s.front() != '-' )
return false;
s.remove_prefix( 1 ); // consume '-'
// Check for "start-" (open-ended)
skip_ws( s );
if( s.empty() || s.front() == ',' )
{
// Open-ended: start to end of file
out.start = start;
out.end = size - 1;
return start < size;
}
// Parse end
if( ! parse_int( s, end ) || end < 0 )
return false;
// Validate
if( start > end )
return false;
out.start = start;
out.end = ( std::min )( end, size - 1 );
return start < size;
}
} // (anon)
range_result
parse_range( std::int64_t size, core::string_view header )
{
range_result result;
result.type = range_result_type::malformed;
if( size <= 0 )
{
result.type = range_result_type::unsatisfiable;
return result;
}
// Must start with "bytes="
skip_ws( header );
if( header.size() < 6 )
return result;
// Case-insensitive "bytes=" check
auto prefix = header.substr( 0, 6 );
bool is_bytes = true;
for( std::size_t i = 0; i < 5; ++i )
{
char c = static_cast<char>( std::tolower(
static_cast<unsigned char>( prefix[i] ) ) );
if( c != "bytes"[i] )
{
is_bytes = false;
break;
}
}
if( ! is_bytes || prefix[5] != '=' )
return result;
header.remove_prefix( 6 );
// Parse range specs
bool any_satisfiable = false;
while( ! header.empty() )
{
skip_ws( header );
if( header.empty() )
break;
byte_range range;
if( parse_range_spec( header, size, range ) )
{
result.ranges.push_back( range );
any_satisfiable = true;
}
skip_ws( header );
if( ! header.empty() )
{
if( header.front() == ',' )
header.remove_prefix( 1 );
else
break; // Invalid
}
}
if( result.ranges.empty() )
{
result.type = range_result_type::unsatisfiable;
}
else if( any_satisfiable )
{
result.type = range_result_type::ok;
}
return result;
}
} // http
} // boost