2 * This file is open source software, licensed to you under the terms
3 * of the Apache License, Version 2.0 (the "License"). See the NOTICE file
4 * distributed with this work for additional information regarding copyright
5 * ownership. You may not use this file except in compliance with the License.
7 * You may obtain a copy of the License at
9 * http://www.apache.org/licenses/LICENSE-2.0
11 * Unless required by applicable law or agreed to in writing,
12 * software distributed under the License is distributed on an
13 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14 * KIND, either express or implied. See the License for the
15 * specific language governing permissions and limitations
20 * Copyright (C) 2020 ScyllaDB.
23 #include <seastar/core/ragel.hh>
24 #include <seastar/core/sstring.hh>
25 #include <seastar/core/thread.hh>
26 #include <seastar/core/temporary_buffer.hh>
27 #include <seastar/http/chunk_parsers.hh>
28 #include <seastar/http/internal/content_source.hh>
29 #include <seastar/testing/test_case.hh>
34 using namespace seastar
;
36 SEASTAR_TEST_CASE(test_size_and_extensions_parsing
) {
41 std::vector
<std::pair
<sstring
, sstring
>> extensions
;
43 temporary_buffer
<char> buf() {
44 return temporary_buffer
<char>(msg
.c_str(), msg
.size());
48 std::vector
<test_set
> tests
= {
49 { "14;name=value\r\n", true, "14", { {"name", "value"} } },
50 { "abcdef;name=value;name2=\"value2\"\r\n", true, "abcdef" },
51 { "1efg;name=value\r\n", false },
52 { "aa;tchars.^_`|123=t1!#$%&'*+-.~\r\n", true, "aa", { {"tchars.^_`|123", "t1!#$%&'*+-.~"} } },
53 { "1;quoted=\"hello world\";quoted-pair=\"\\a\\b\\cd\\\\ef\"\r\n", true, "1", { {"quoted", "hello world"}, {"quoted-pair", "abcd\\ef"} } },
54 { "2;bad-quoted-pair=\"abc\\\"\r\n", false },
55 { "3;quoted-pair-outside-quoted-string=\\q\\p\r\n", false },
56 { "4;whitespace-outside-quoted-string=quoted string\r\n", false },
57 { "5;quotation-mark-inside-quoted-string=\"quoted\"mark\"\r\n", false },
58 { "6; bad=space\r\n", false },
59 { "7;sole-name\r\n", true, "7", { { "sole-name", ""} } },
60 { "8;empty_value=\"\"\r\n", true, "8", { { "empty_value", ""} } },
61 { "0\r\n", true, "0" }
64 http_chunk_size_and_ext_parser parser
;
65 for (auto& tset
: tests
) {
67 BOOST_REQUIRE(parser(tset
.buf()).get0().has_value());
68 BOOST_REQUIRE_NE(parser
.failed(), tset
.parsable
);
70 BOOST_REQUIRE_EQUAL(parser
.get_size(), std::move(tset
.size
));
71 auto exts
= parser
.get_parsed_extensions();
72 for (auto& ext
: tset
.extensions
) {
73 BOOST_REQUIRE_EQUAL(exts
[ext
.first
], ext
.second
);
77 return make_ready_future
<>();
80 SEASTAR_TEST_CASE(test_trailer_headers_parsing
) {
84 sstring header_name
= "";
85 sstring header_value
= "";
87 temporary_buffer
<char> buf() {
88 return temporary_buffer
<char>(msg
.c_str(), msg
.size());
92 std::vector
<test_set
> tests
= {
93 // the headers follow the same rules as in the request parser
94 { "Host: test\r\n\r\n", true, "Host", "test" },
95 { "Header: Field\r\n\r\n", true, "Header", "Field" },
96 { "Header: \r\n\r\n", true, "Header", "" },
97 { "Header: f i e l d \r\n\r\n", true, "Header", "f i e l d" },
98 { "Header: fiel\r\n d\r\n\r\n", true, "Header", "fiel d" },
99 { "tchars.^_`|123: printable!@#%^&*()obs_text\x80\x81\xff\r\n\r\n", true,
100 "tchars.^_`|123", "printable!@#%^&*()obs_text\x80\x81\xff" },
101 { "Header: Field\r\nHeader: Field2\r\n\r\n", true, "Header", "Field,Field2" },
102 { "Header : Field\r\n\r\n", false },
103 { "Header Field\r\n\r\n", false },
104 { "Header@: Field\r\n\r\n", false },
105 { "Header: fiel\r\nd \r\n\r\n", false },
109 http_chunk_trailer_parser parser
;
110 for (auto& tset
: tests
) {
112 BOOST_REQUIRE(parser(tset
.buf()).get0().has_value());
113 BOOST_REQUIRE_NE(parser
.failed(), tset
.parsable
);
115 auto heads
= parser
.get_parsed_headers();
116 BOOST_REQUIRE_EQUAL(heads
[std::move(tset
.header_name
)], std::move(tset
.header_value
));
119 return make_ready_future
<>();