Skip to content

Commit f742d7c

Browse files
committed
field does not have unkown value
1 parent c77b2c7 commit f742d7c

14 files changed

Lines changed: 86 additions & 137 deletions

include/boost/http_proto/detail/header.hpp

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -63,6 +63,10 @@ struct header
6363
std::numeric_limits<
6464
offset_type>::max();
6565

66+
static constexpr
67+
field unknown_field =
68+
static_cast<field>(0);
69+
6670
struct entry
6771
{
6872
offset_type np; // name pos
@@ -212,7 +216,6 @@ struct header
212216
static std::size_t
213217
count_crlf(core::string_view s) noexcept;
214218

215-
BOOST_HTTP_PROTO_DECL
216219
void parse(
217220
std::size_t,
218221
header_limits const&,

include/boost/http_proto/field.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//
22
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3+
// Copyright (c) 2025 Mohammad Nejati
34
//
45
// Distributed under the Boost Software License, Version 1.0. (See accompanying
56
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -12,6 +13,7 @@
1213

1314
#include <boost/http_proto/detail/config.hpp>
1415
#include <boost/core/detail/string_view.hpp>
16+
#include <boost/optional.hpp>
1517
#include <cstdint>
1618
#include <iosfwd>
1719
#include <type_traits>
@@ -21,9 +23,7 @@ namespace http_proto {
2123

2224
enum class field : unsigned short
2325
{
24-
unknown = 0, // must be zero
25-
26-
a_im,
26+
a_im = 1,
2727
accept,
2828
accept_additions,
2929
accept_charset,
@@ -399,7 +399,7 @@ to_string(field f);
399399
@ref field::unknown if there is no match.
400400
*/
401401
BOOST_HTTP_PROTO_DECL
402-
field
402+
boost::optional<field>
403403
string_to_field(
404404
core::string_view s) noexcept;
405405

include/boost/http_proto/fields_base.hpp

Lines changed: 13 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -203,8 +203,7 @@ class fields_base
203203
@par Exception Safety
204204
Strong guarantee.
205205
206-
@param id The field name constant,
207-
which may not be @ref field::unknown.
206+
@param id The field name constant.
208207
209208
@param value A value, which must be semantically
210209
valid for the message.
@@ -252,8 +251,7 @@ class fields_base
252251
@par Exception Safety
253252
Strong guarantee.
254253
255-
@param id The field name constant,
256-
which may not be @ref field::unknown.
254+
@param id The field name constant.
257255
258256
@param value A value, which must be semantically
259257
valid for the message.
@@ -271,10 +269,6 @@ class fields_base
271269
core::string_view value,
272270
system::error_code& ec)
273271
{
274-
// Precondition violation
275-
if(id == field::unknown)
276-
detail::throw_logic_error();
277-
278272
insert_impl(
279273
id,
280274
to_string(id),
@@ -411,8 +405,7 @@ class fields_base
411405
412406
@param before Position to insert before.
413407
414-
@param id The field name constant,
415-
which may not be @ref field::unknown.
408+
@param id The field name constant.
416409
417410
@param value A value, which must be semantically
418411
valid for the message.
@@ -468,8 +461,7 @@ class fields_base
468461
469462
@param before Position to insert before.
470463
471-
@param id The field name constant,
472-
which may not be @ref field::unknown.
464+
@param id The field name constant.
473465
474466
@param value A value, which must be semantically
475467
valid for the message.
@@ -488,10 +480,6 @@ class fields_base
488480
core::string_view value,
489481
system::error_code& ec)
490482
{
491-
// Precondition violation
492-
if(id == field::unknown)
493-
detail::throw_logic_error();
494-
495483
insert_impl(
496484
id,
497485
to_string(id),
@@ -630,18 +618,15 @@ class fields_base
630618
@par Exception Safety
631619
Throws nothing.
632620
633-
@return An iterator to the inserted
634-
element.
621+
@return An iterator to one past the
622+
removed element.
635623
636624
@param it An iterator to the element
637625
to erase.
638626
*/
627+
BOOST_HTTP_PROTO_DECL
639628
iterator
640-
erase(iterator it) noexcept
641-
{
642-
erase_impl(it.i_, it->id);
643-
return it;
644-
}
629+
erase(iterator it) noexcept;
645630

646631
/** Erase headers
647632
@@ -661,12 +646,11 @@ class fields_base
661646
662647
@return The number of headers erased.
663648
664-
@param id The field name constant,
665-
which may not be @ref field::unknown.
649+
@param id The field name constant.
666650
*/
667651
BOOST_HTTP_PROTO_DECL
668652
std::size_t
669-
erase(field id);
653+
erase(field id) noexcept;
670654

671655
/** Erase all matching fields
672656
@@ -823,8 +807,7 @@ class fields_base
823807
824808
@par Complexity
825809
826-
@param id The field name constant,
827-
which may not be @ref field::unknown.
810+
@param id The field name constant.
828811
829812
@param value A value, which must be semantically
830813
valid for the message.
@@ -924,7 +907,7 @@ class fields_base
924907

925908
void
926909
insert_unchecked_impl(
927-
field id,
910+
optional<field> id,
928911
core::string_view name,
929912
core::string_view value,
930913
std::size_t before,
@@ -933,7 +916,7 @@ class fields_base
933916
BOOST_HTTP_PROTO_DECL
934917
void
935918
insert_impl(
936-
field id,
919+
optional<field> id,
937920
core::string_view name,
938921
core::string_view value,
939922
std::size_t before,

include/boost/http_proto/fields_view_base.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//
22
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3+
// Copyright (c) 2025 Mohammad Nejati
34
//
45
// Distributed under the Boost Software License, Version 1.0. (See accompanying
56
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -15,8 +16,6 @@
1516
#include <boost/url/grammar/recycled.hpp>
1617
#include <boost/url/grammar/type_traits.hpp>
1718
#include <boost/core/detail/string_view.hpp>
18-
#include <iterator>
19-
#include <memory>
2019
#include <string>
2120

2221
namespace boost {
@@ -74,7 +73,7 @@ class fields_view_base
7473
/**@{*/
7574
struct reference
7675
{
77-
field const id;
76+
boost::optional<field> const id;
7877
core::string_view const name;
7978
core::string_view const value;
8079

@@ -98,7 +97,7 @@ class fields_view_base
9897
*/
9998
struct value_type
10099
{
101-
field id;
100+
boost::optional<field> id;
102101
std::string name;
103102
std::string value;
104103

src/detail/header.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -71,6 +71,10 @@ operator-(
7171

7272
//------------------------------------------------
7373

74+
constexpr field header::unknown_field;
75+
76+
//------------------------------------------------
77+
7478
constexpr
7579
header::
7680
header(fields_tag) noexcept
@@ -1250,7 +1254,8 @@ parse_field(
12501254
BOOST_ASSERT(h.buf != nullptr);
12511255
remove_obs_fold(h.buf + h.size, it);
12521256
}
1253-
auto id = string_to_field(rv->name);
1257+
auto id = string_to_field(rv->name)
1258+
.value_or(header::unknown_field);
12541259
h.size = static_cast<header::offset_type>(it - h.cbuf);
12551260

12561261
// add field table entry

src/field.cpp

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
//
22
// Copyright (c) 2021 Vinnie Falco (vinnie.falco@gmail.com)
3+
// Copyright (c) 2025 Mohammad Nejati
34
//
45
// Distributed under the Boost Software License, Version 1.0. (See accompanying
56
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
@@ -493,7 +494,7 @@ struct field_table
493494
}
494495
}
495496

496-
field
497+
optional<field>
497498
string_to_field(
498499
core::string_view s) const noexcept
499500
{
@@ -505,13 +506,13 @@ struct field_table
505506
return static_cast<field>(i);
506507
i = map_[j][1];
507508
if(i == 0)
508-
return field::unknown;
509+
return boost::none;
509510
i += 255;
510511
s2 = by_name_[i];
511512

512513
if(equals(s, s2))
513514
return static_cast<field>(i);
514-
return field::unknown;
515+
return boost::none;
515516
}
516517

517518
//
@@ -558,7 +559,7 @@ to_string(field f)
558559
return v.begin()[static_cast<unsigned>(f)];
559560
}
560561

561-
field
562+
boost::optional<field>
562563
string_to_field(
563564
core::string_view s) noexcept
564565
{

0 commit comments

Comments
 (0)