Skip to content

Commit ad2b5db

Browse files
author
Dean Michael Berris
committed
Adding test and implementation for status message parsing.
1 parent ddc197f commit ad2b5db

File tree

2 files changed

+51
-1
lines changed

2 files changed

+51
-1
lines changed

boost/network/protocol/http/parser/incremental.hpp

Lines changed: 32 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,10 @@ namespace boost { namespace network { namespace http {
3131
http_version_minor,
3232
http_version_done,
3333
http_status_digit,
34-
http_status_done
34+
http_status_done,
35+
http_status_message_char,
36+
http_status_message_cr,
37+
http_status_message_done
3538
};
3639

3740
typedef typename string<Tag>::type::const_iterator iterator_type;
@@ -166,6 +169,34 @@ namespace boost { namespace network { namespace http {
166169
parsed_ok = false;
167170
}
168171
break;
172+
case http_status_done:
173+
// FIXME find a better way to use is_alnum, is_space
174+
if (algorithm::is_alnum()(*current)) {
175+
state_ = http_status_message_char;
176+
++current;
177+
} else {
178+
parsed_ok = false;
179+
}
180+
break;
181+
case http_status_message_char:
182+
// FIXME find a better way to use is_alnum, is_space
183+
if (algorithm::is_alnum()(*current) || algorithm::is_punct()(*current) || (*current == ' ')) {
184+
++current;
185+
} else if (*current == '\r') {
186+
state_ = http_status_message_cr;
187+
++current;
188+
} else {
189+
parsed_ok = false;
190+
}
191+
break;
192+
case http_status_message_cr:
193+
if (*current == '\n') {
194+
state_ = http_status_message_done;
195+
++current;
196+
} else {
197+
parsed_ok = false;
198+
}
199+
break;
169200
default:
170201
parsed_ok = false;
171202
}

libs/network/test/http_incremental_parser.cpp

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -132,3 +132,22 @@ BOOST_AUTO_TEST_CASE(incremental_parser_parse_status) {
132132
std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl;
133133
}
134134

135+
/** In this test then we get the rest of the first line of the HTTP
136+
* Response, and treat it as the status message.
137+
*/
138+
BOOST_AUTO_TEST_CASE(incremental_parser_parse_status_message) {
139+
typedef response_parser<tags::default_string> response_parser_type;
140+
typedef response_parser_type::range_type range_type;
141+
response_parser_type p(response_parser_type::http_status_done);
142+
143+
std::string valid_status_message = "OK\r\nServer: Foo";
144+
logic::tribool parsed_ok;
145+
range_type result_range;
146+
fusion::tie(parsed_ok, result_range) = p.parse_until(
147+
response_parser_type::http_status_message_done,
148+
valid_status_message);
149+
BOOST_CHECK_EQUAL(parsed_ok, true);
150+
std::string parsed = std::string(boost::begin(result_range), boost::end(result_range));
151+
std::cout << "PARSED: " << parsed << " state=" << p.state() << std::endl;
152+
}
153+

0 commit comments

Comments
 (0)