Skip to content

Commit 0773ca1

Browse files
committed
field does not have unkown value
1 parent 3ad3377 commit 0773ca1

File tree

14 files changed

+76
-133
lines changed

14 files changed

+76
-133
lines changed

include/boost/http_proto/detail/header.hpp

Lines changed: 4 additions & 0 deletions
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

include/boost/http_proto/field.hpp

Lines changed: 3 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@
1212

1313
#include <boost/http_proto/detail/config.hpp>
1414
#include <boost/core/detail/string_view.hpp>
15+
#include <boost/optional.hpp>
1516
#include <cstdint>
1617
#include <iosfwd>
1718
#include <type_traits>
@@ -21,9 +22,7 @@ namespace http_proto {
2122

2223
enum class field : unsigned short
2324
{
24-
unknown = 0, // must be zero
25-
26-
a_im,
25+
a_im = 1,
2726
accept,
2827
accept_additions,
2928
accept_charset,
@@ -399,7 +398,7 @@ to_string(field f);
399398
@ref field::unknown if there is no match.
400399
*/
401400
BOOST_HTTP_PROTO_DECL
402-
field
401+
boost::optional<field>
403402
string_to_field(
404403
core::string_view s) noexcept;
405404

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: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,7 @@ class fields_view_base
7474
/**@{*/
7575
struct reference
7676
{
77-
field const id;
77+
boost::optional<field> const id;
7878
core::string_view const name;
7979
core::string_view const value;
8080

@@ -98,7 +98,7 @@ class fields_view_base
9898
*/
9999
struct value_type
100100
{
101-
field id;
101+
boost::optional<field> id;
102102
std::string name;
103103
std::string value;
104104

src/detail/header.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1250,7 +1250,8 @@ parse_field(
12501250
BOOST_ASSERT(h.buf != nullptr);
12511251
remove_obs_fold(h.buf + h.size, it);
12521252
}
1253-
auto id = string_to_field(rv->name);
1253+
auto id = string_to_field(rv->name)
1254+
.value_or(header::unknown_field);
12541255
h.size = static_cast<header::offset_type>(it - h.cbuf);
12551256

12561257
// add field table entry

src/field.cpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -493,7 +493,7 @@ struct field_table
493493
}
494494
}
495495

496-
field
496+
optional<field>
497497
string_to_field(
498498
core::string_view s) const noexcept
499499
{
@@ -505,13 +505,13 @@ struct field_table
505505
return static_cast<field>(i);
506506
i = map_[j][1];
507507
if(i == 0)
508-
return field::unknown;
508+
return boost::none;
509509
i += 255;
510510
s2 = by_name_[i];
511511

512512
if(equals(s, s2))
513513
return static_cast<field>(i);
514-
return field::unknown;
514+
return boost::none;
515515
}
516516

517517
//
@@ -558,7 +558,7 @@ to_string(field f)
558558
return v.begin()[static_cast<unsigned>(f)];
559559
}
560560

561-
field
561+
optional<field>
562562
string_to_field(
563563
core::string_view s) noexcept
564564
{

src/fields_base.cpp

Lines changed: 25 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -562,15 +562,23 @@ shrink_to_fit() noexcept
562562
//
563563
//------------------------------------------------
564564

565-
std::size_t
565+
auto
566566
fields_base::
567567
erase(
568-
field id)
568+
iterator it) noexcept -> iterator
569569
{
570-
// Precondition violation
571-
if(id == field::unknown)
572-
detail::throw_logic_error();
570+
auto const id = it->id.value_or(
571+
detail::header::unknown_field);
572+
raw_erase(it.i_);
573+
h_.on_erase(id);
574+
return it;
575+
}
573576

577+
std::size_t
578+
fields_base::
579+
erase(
580+
field id) noexcept
581+
{
574582
auto const i0 = h_.find(id);
575583
if(i0 == h_.count)
576584
return 0;
@@ -587,7 +595,7 @@ erase(
587595
return 0;
588596
auto const ft = h_.tab();
589597
auto const id = ft[i0].id;
590-
if(id == field::unknown)
598+
if(id == detail::header::unknown_field)
591599
return erase_all_impl(i0, name);
592600
return erase_all_impl(i0, id);
593601
}
@@ -701,14 +709,15 @@ set(
701709
h_.size = static_cast<
702710
offset_type>(h_.size + dn);
703711
}
704-
auto const id = it->id;
712+
auto const id = it->id.value_or(
713+
detail::header::unknown_field);
705714
if(h_.is_special(id))
706715
{
707716
// replace first char of name
708717
// with null to hide metadata
709718
char saved = h_.buf[pos0];
710719
auto& e = h_.tab()[i];
711-
e.id = field::unknown;
720+
e.id = detail::header::unknown_field;
712721
h_.buf[pos0] = '\0';
713722
h_.on_erase(id);
714723
h_.buf[pos0] = saved; // restore
@@ -726,10 +735,6 @@ set(
726735
core::string_view value,
727736
system::error_code& ec)
728737
{
729-
// Precondition violation
730-
if(id == field::unknown)
731-
detail::throw_logic_error();
732-
733738
auto rv = verify_field_value(value);
734739
if(rv.has_error())
735740
{
@@ -801,7 +806,7 @@ set(
801806
}
802807
// VFALCO simple algorithm but
803808
// costs one extra memmove
804-
if(id != field::unknown)
809+
if(id != detail::header::unknown_field)
805810
erase_all_impl(i0, id);
806811
else
807812
erase_all_impl(i0, name);
@@ -865,7 +870,7 @@ copy_impl(
865870
void
866871
fields_base::
867872
insert_unchecked_impl(
868-
field id,
873+
optional<field> id,
869874
core::string_view name,
870875
core::string_view value,
871876
std::size_t before,
@@ -954,20 +959,20 @@ insert_unchecked_impl(
954959
! value.empty());
955960
e.vn = static_cast<
956961
offset_type>(value.size());
957-
e.id = id;
962+
e.id = id.value_or(
963+
detail::header::unknown_field);
958964

959965
// update container
960966
h_.count++;
961967
h_.size = static_cast<
962968
offset_type>(h_.size + n);
963-
if( id != field::unknown)
964-
h_.on_insert(id, value);
969+
h_.on_insert(e.id, value);
965970
}
966971

967972
void
968973
fields_base::
969974
insert_impl(
970-
field id,
975+
optional<field> id,
971976
core::string_view name,
972977
core::string_view value,
973978
std::size_t before,
@@ -992,17 +997,6 @@ insert_impl(
992997
rv->has_obs_fold);
993998
}
994999

995-
// erase i and update metadata
996-
void
997-
fields_base::
998-
erase_impl(
999-
std::size_t i,
1000-
field id) noexcept
1001-
{
1002-
raw_erase(i);
1003-
h_.on_erase(id);
1004-
}
1005-
10061000
//------------------------------------------------
10071001

10081002
void
@@ -1038,7 +1032,7 @@ erase_all_impl(
10381032
field id) noexcept
10391033
{
10401034
BOOST_ASSERT(
1041-
id != field::unknown);
1035+
id != detail::header::unknown_field);
10421036
std::size_t n = 1;
10431037
std::size_t i = h_.count - 1;
10441038
auto const ft = h_.tab();
@@ -1059,7 +1053,7 @@ erase_all_impl(
10591053
}
10601054

10611055
// erase all fields with name
1062-
// when id == field::unknown
1056+
// when id == detail::header::unknown_field
10631057
std::size_t
10641058
fields_base::
10651059
erase_all_impl(

0 commit comments

Comments
 (0)