mirror of
https://github.com/Xahau/xahaud.git
synced 2025-12-06 17:27:52 +00:00
Squashed 'src/beast/' changes from 6d5547a..3bcd986
3bcd986 Set version to 79 4f42f8c Remove spurious fallthrough guidance fa1ac16 Set version to 78 0cb9b63 Fix warning in root ca declaration f78c73a Tidy up file_posix unused variable 72ce9ef Tidy up dstream for existing Boost versions efe8e58 Add Boost.Locale utf8 benchmark comparison f7c745e Remove string_view_body 7a79efa Tidy up FieldsReader doc e51aefd Header file tidying 69898f4 Fix warning in zlib 53723c0 Add message keep_alive, chunked, content_length members d7af73b Fix spurious uninitialized warning ca42cc0 Tidy up invalid characters in test vector 48d3e60 Use make_unique_noinit a1ff804 span, string, vector bodies are public feab6a0 Documentation work 116c0b0 Add span_body 7fb901d Tidy up includes and javadocs f0f58be Add span 7a8982b Add vector_body 524f73a Tidy up basic_string_body a8ad67b Set version to 77 d555859 file_posix works without large file support 1bc30cb Set version to 76 9a1e7a8 Disable SSE4.2 optimizations 09af312 Fix parse illegal characters in obs-fold 7dd684c Add file_body_win32: 1bbc71c serializing file_body is not const 9a4b55e BodyReader, BodyWriter use two-phase init (API Change): dc400ce Serializer members are not const 1a33c37 Rename to serializer::keep_alive (API Change): 55935c5 Add serializer::chunked 63ace37 Add serializer::get 2c4047b BodyReader may construct from a non-const message 0a0a225 Use Boost.Config 6f83d07 Always go through write_some: 0e23066 Set version to 75 28f3ece Doc tidying 3495331 Using SSE4.2 intrinsics in basic_parser if available bc1f0ac file_body tests 4e03d7e Add serializer::limit 85e3ee8 Shrink serializer buffers using buffers_ref 78bcdb1 Tidy up BEAST_NO_BIG_VARIANTS 3ea6cf2 Construct buffer_prefix_view in-place 69f9f7a Use file_body for valid requests, string_body otherwise. 6f88f01 Set version to 74 93fed8e remove redundant flush() from example: e0f56da Fix Beast include directories for cmake targets 5ff9e0a Add file_posix 1bb5705 Remove common/file_body.hpp 5c89d87 Add file_body 67a55c8 Add file_win32 647d3b0 Add file_stdio and File concept 89c416c Set version to 73 0efc32f Fixes for gcc-4.8 c8910ab Initialize local variable in basic_parser 8a28193 Adjust benchmarks 81e51d8 Verify certificates in SSL clients a43f6d4 Jamroot tweak 8c85ee8 Put more... links on overload reference pages ff1104e Documentation tidy 826ff0e serializer::next replaces serializer::get (API Change): 8d67775 Refactor header and message constructors: 6c79f19 Add basic_parser tests 25127d9 basic_parser optimizations: 9d082fd Set version to 72 c88e2b9 Various improvements to http_server_fast.cpp: 20b0fdb Documentation tidying afd1fa7 Add websocket-server-async example 954b597 Add http-server-threaded example df8f253 Refactor file_body for best practices 11c1037 Newly constructed responses have a 200 OK result a648817 Refine Body::size specification 40aad37 Tidy up set payload in http-server-fast 52cefbc Set version to 71 8c51c77 Tidy up Jamroot /permissive- 5efecea Update README.md 5a47acd Tidy up http_sync_port error check a2af2b5 Concept check in basic_dynamic_body 8b80a6f Fix buffer overflow handling for string_body and mutable_body ec3b4fd Return std::size_t from Body::writer::put (API Change) effbb37 Check trailers in test f5368cf Call prepare_payload in HTTP example a3e5e01 Fix spurious on_chunk invocation 96d94eb Add options for building examples and tests. Move zlib test sources to test/zlib e0efdc7 Allow close, ping, and write to happen concurrently 9c1a419 Refactor websocket composed ops d5659a4 Fine tune websocket asserts b8e8943 std::pair "last" -> "first" in http_message.qbk c691bf4 Fix can/cannot thinko in FAQ. 6dd006b Documentation revision 6d2e315 Fix extra ; warning 78a065b Set version to 70 00c7e9d Fix HEAD response in file_service 67d70d2 Fix BEAST_FALLTHROUGH in config 4f33655 Add parser::on_header to set a callback 9c16b21 Add basic_parser header and body limits: b64d1f7 Rename to message::base (API Change): 436c66a Serialize in one step when possible 3e1061b Set version to 69 f709273 Add /permissive- to msvc toolchain 0dae464 Use BEAST_FALLTHROUGH to silence warnings a70d386 basic_parser optimizations 4269f35 Set version to 68 544327f Link statically on cmake MSVC e213ffe Add const_body, mutable_body to examples 0568763 Optimize field lookups 8fc3001 Use string_ref in older Boost versions 8af77da bad_target replaces bad_path (API Change): 325dd62 Adjust buffer size in fast server be59785 Doc erratum d9b44f3 Small speed up in fields comparisons 3e6ce38 Use Boost master on Appveyor 09f3d64 Split common tests to a new project adfd22a Remove BodyReader::is_deferred (API Change): 582d28d Change BodyReader, BodyWriter requirements (API Change): 8982e14 Set version to 67 daa58a2 Group common example headers afd8f1a Rename to http-server-fast 07cb9f7 control_callback replaces ping_callback (API Change): 91e83ed Use boost::string_view 067db87 Merge stream_base to stream and tidy d61241a Add http-server-small example eb08e92 Fix doc example link 7fb75d0 Set version to 66 df86723 Respect debug flag for marked output c08565a Squelch spurious warning on gcc 188ef7c Documentation work 1c62d3a Add http-server example 3f54582 basic_fields optimizations a8b05b8 Add header aliases b94eac3 Tidy up message piecewise ctors 9c48b52 Handle bad_alloc in parser 1b57c54 Fix costly potential value-init in parser 1edc41e Make consuming_buffers smaller 72ac918 Add serializer request/response aliases 18f7606 string_param optimizations c675252 Set version to 65 c398cdd Enable msvc warnings in Jamfile 380cceb Fix unused variable warnings 4172e7e Enable unused variable warning on msvc cmake f04d227 Fix integer warnings ca975b3 Fix narrowing in deflate_stream 2fab796 Fix narrowing in inflate_stream cff87f6 Fix narrowing in ostream 1956886 Fix narrowing in static_ostream 69cdc4b Fix integer types in deflate_stream::bi_reverse 3e3dd21 Enable narrowing warning on msvc cmake e11a294 Set version to 64 a00e070 Remove make_serializer (API Change): 8449697 Add link_directories to cmake 7b2b066 Doc tidying 158d3e8 async_write requires a non-const message: d13328b Better User-Agent in examples ebcb2c0 Exemplars are compiled code b9054d3 Simplify websocket write_op c2571fe Simplify ssl teardown composed op d8ad3d1 Simplify buffered_read_stream composed op f68dc34 Set version to 63 a99f7ef Control running with valgrind explicitly 4eb7af4 Tidy up Jamfiles 49b42a5 Tidy up CMakeLists.txt dadb54f Only run the tests under ubasan d1c7696 Move benchmarks to a separate project a4aada8 Only build and run tests in variant=coverage f835b9a Don't use cached Boost a0edd82 Put num_jobs back up on Travis a8d5823 Use std::to_string instead of lexical_cast 45d8b81 Set version to 62 09e07ce Put slow tests back for coverage builds 295b1d7 Doc tidy f58425c Squelch harmless not_connected errors 9b537f7 Add http::is_fields trait d43701b message::prepare_payload replaces message::prepare (API Change): 42ba289 Refine FieldsReader concept (API Change) bde90a1 Narrow the use of Fields parameters: 5f47526 parser requires basic_fields (API Change): 60f58e4 Avoid explicit operator bool for error 352f8de Clear the error faster 34befd8 Tidy up namespaces in examples 9e0b4b5 Doc fixes and tidy c003a2a Tidy up test build scripts and projects b929130 Add server-framework tests 03d4301 Increase detail::static_ostream coverage 80d7cbc Remove libssl-dev from a Travis matrix item 4c15db4 Set version to 61 1dfbd0b Don't run slow tests on certain targets 6bb1109 Use one job less on CI 6f58342 Tidy up resolver calls 530b044 Add multi_port to server-framework bfef5d1 Tidy up http-crawl example e2f2f33 Reorganize SSL examples adc301b Fix shadowing warnings c60185e Add server-framework SSL HTTP and WebSocket ports 7912fb8 Refactor WebSocket, HTTP examples: cd4b9e0 Flush the output stream in the example d046b20 Tidy up names in error categories 9d4a422 status-codes is unsigned (API Change) e3599b0 header::version is unsigned (API Change) a26b043 Add message::header_part() fc8d2e9 Tidy up some integer conversion warnings c91732e Reorganize SSL examples 9907b31 Documentation work 4b2e78e Use generic_cateogry for errno 38c46cd Remove Spirit dependency c111d6f Set version to 60 d78dc12 Documentation work 141a524 New server-framework, full featured server example: 3f7ffd9 Fix response message type in async websocket accept 13f3750 String comparisons are public interfaces 4e4bcf8 Set version to 59 5015cdb Remove obsolete doc/README.md 71c3f0a Fix base64 alphabet aa2b843 Change Body::size signature (API Change): 80a599a Documentation work 9c19449 Integrated Beast interface. 3f8097b Set version to 58 4581777 Better message formal parameter names 5879cd8 Fix parsing chunk size with leading zeroes 56bd228 Remove redundant code 534ca63 Use static string in basic_fields::reader a7b3810 basic_parser::put doc 1e4413f basic_fields::set optimization: 9b244c1 Fix basic_fields insert ordering 4f854d0 Avoid std::string in websocket dc8f146 Renamed to basic_fields::set (API Change): 660c465 Specification for http read 981285b Documentation tidy 983d676 Reorganize examples: a2a5c57 Qualify size_t in message template d86769c Fix unaligned reads in utf8-checker 8ba182c Set version to 57 42e2791 Update doc/ for docca 1ee0afd Merge commit '101d7dbfb9725674cb9ce5a4196f19aa1d4bb801' as 'doc/docca' 101d7db Squashed 'doc/docca/' content from commit c50b3ba5 900c04e Documentation work 8eee932 Fix warning in basic_parser.cpp 437a616 Fix message.hpp javadocs 18c68ce Set version to 56 b058e90 Convert buffer in range loops cbc9212 Add Beast INTERFACE CMake target 2914b59 More basic_parser tests ed5c317 Reset error codes ba14251 Test error code handling e45e50b Tidy up README.md build instructions 16efb9b Try harder to find Boost (cmake) e281d91 HTTP/1 is the default version 916fe4a Call on_chunk when the extension is empty 9855598 Add string_view_body 19d4520 Tidy up 6e59f9b Add provisional IANA header field names 84722f2 Revert "Add a Beast CMake interface target:" fde6929 Set version to 55 01f6cc4 Documentation work a7a388c read_size replaces read_size_helper: ed8f0bb Tidy up type_traits c2f6268 Avoid a parser allocation using non-flat buffer: 906db45 Add a Beast CMake interface target: 47f2541 Don't allocate memory to handle obs-fold 6a8912a Set version to 54 296ef3b Documentation work e3e9b61 Fix incorrect use of [[fallthrough]] 3c44398 Retain ownership when reading using a message a71bb2b basic_fields refactor (API Change): d8d3562 Add string_param 83b2558 basic_fields members and coverage c4f5fa5 consuming_buffers members and coverage e10507c multi_buffer members and coverage 0e6bd3f flat_buffer coverage 7351d6e static_buffer coverage 18a8ef5 Set version to 53 452df59 Remove extraneous doc file 3ef0359 Fix read_size_helper usage: b64e6d3 Fix basic_parser::maybe_flatten (#462) 76402f7 Set version to 52 b0ceb2a Add drain_buffer class 4c6735a flat_buffer is an AllocatorAwareContainer 9d5d4d5 Documentation work d4ec693 finish(error_code&) is a BodyReader requirement (API Change) 7b24cad opcode is private (API Change): 068c2ac Documentation work a1ff89b Disable std::future snippet for libstdc++ bug b5ef664 read_frame returns `bool` fin (API Change): 7911847 Remove opcode from read, async_read (API Change): c72d70f ping_callback is a member of stream (API Change): 720a309 write_buffer_size is a member of stream (API Change): 7ff0178 read_message_max is a member of stream (API Change): cd40964 read_buffer_size is a member of stream (API Change): a58e5e1 binary, text are members of stream (API Change): ad35846 auto_fragment is a member of stream (API Change): ccee139 Documentation work 13c64e3 Set version to 51 cafc8e2 Fix infinite loop in basic_parser dc4b69d Add construct, destroy to handler_alloc 58c2739 multi_buffer implementation change (API Change): dd7f5c0 DynamicBuffer benchmarks 1c4811b Use BOOST_STRINGIZE 31051ac Use BOOST_FALLTHROUGH 8f2430b Documentation work eb35b92 Fix file_body::get() not setting the more flag correctly 566244a Tidy up file_body 53cbeea Tune up static_buffer (API Change): 20c59b7 Fix operator<< for header a2c1117 Set version to 50 6045b74 http read_some, async_read_some don't return bytes (API Change): 4df6885 Fix chunk header parsing 36bf32b Fix test::pipe read_size bf69ce1 Fix chunk delimiter parsing 0c6b6b1 Add missing handler_alloc nested types a06b8f9 Tidy up message javadoc 3bd8260 Remove obsolete serializer allocator (API Change) 001c979 Remove message free functions (API Change) 745876b Remove message connection settings (API Change) bcf2c33 Body documentation work 1e303b0 Fields concept work 9d0464a Tidy up basic_fields, header, and concepts 3ba81b5 Use field in basic_parser b5f6cc1 Use field in basic_fields and call sites cfd6d14 Documentation reference tidy 2adc80a Protect basic_fields special members (API Change) d55b079 Fix basic_fields allocator awareness d8febda Documentation work 485a6e5 Refactor prepare (API Change) 9fd3071 Derive header from Fields (API Change) 8ad26b8 Use allocator more in basic_fields 0071039 Add verb to on_request for parsers (API Change) 74f6cbb Add field enumeration be0d74f Documentation fixes 054ac40 Remove header_parser (API Change) a007eba parser is constructible from other body types d89809f Documentation work (buffer_body) ac5bc4f Set version to 49 af47128 Documentation work a1848f0 Add HEAD request example ddfbfbf Use <iosfwd> instead of <ostream> e67c0ab Refactor header status, reason, and target (API Change): 60f044a Tidy up empty_body writer error 7d267f4 Canonicalize string_view parameter types ac175cb Refactor method and verb (API Change): e18efed Documentation work d77e423 Set version to 48 d3a5a05 Documentation work acf18fb Tidy up traits 6cb188e Remove detail::sync_ostream d6092bc Documentation work 4707b21 Rename to parser (API Change): 3cb385d Consolidate parsers to parser.hpp 290bdf1 Documentation work 7cb442c Make buffer_prefix_view public ef0b121 Rename to buffer_cat_view (API Change) b9df187 Tidy up chunk decorator (API Change): 458fa7e Set version to 47 fc83a03 Documentation work 1ee3013 Fix leak in basic_flat_buffer 55fbf76 Fix undefined behavior in pausation fe75a7c Refactor HTTP serialization and parsing (API Change): 50cba32 buffer_size overload for basic_multi_buffer::const_buffers_type d977bf2 Disable operator<< for buffer_body 5db707a Refactor treatment of status code and obsolete reason (API Change): 9a585a8 Refactor treatment of request-method (API Change): 3ae76d0 Set version to 46 6004712 Documentation work 34ea0b3 Refactor serialization algorithms: 407b046 Rename to make_serializer c29451a Refactor type_traits (API Change): 8578419 Refactor HTTP serialization (API Change): f8612aa Remove HTTP header aliases (API Change): b0054e3 Add test::pipe dfba72b Set version to 45 6ba3697 Disable reverse_iterator buffer_view test 266ebac buffer_view skips empty buffer sequences 96b9892 Documentation work c23f1e2 Fix header::reason 9796106 Better test::enable_yield_to 9a8bcb7 Fix message doc image 7a5e87e Workaround for boost::asio::basic_streambuf type check 663c275 Set version to 44 f205976 Make buffers_adapter meet requirements 8e39c60 Tidy up is_dynamic_buffer traits test 0088f7c Add buffers_adapter regression test 8a23de1 Fix README websocket example 949504a Fix async return values in docs cd9f41b Use BOOST_STATIC_ASSERT 1b616fa Tidy up and make get_lowest_layer public 612e616 Require Boost 1.58 or later 1b1daa7 Tidy up read_size_helper and dynamic buffers bf0145d Use BOOST_THROW_EXCEPTION e762818 Add GitHub issue template dab679c Set version to 43 386b817 Reformat README.md QR code 50e5123 Additional constructors for consuming_buffers f7289b9 Add write limit to test::string_ostream 3aa87e0 Tidy up buffer_prefix overloads and test bee583c Fix strict aliasing warnings in buffers_view 6b54d3a Require Boost 1.64.0 76f1084 Set version to 42 0bdb148 Make buffers_view a public interface 338fc81 Add formal review notes 784f965 Fix javadoc typo 823aee2 Set version to v41 88adbdd Remove handler helpers, tidy up hook invocations (API Change) 4974af2 Rename prepare_buffer(s) to buffer_prefix (API Change) ebd459b Tidy up websocket::close_code enum and constructors c3fd6f9 Tidy up formal parameter names 210cd70 Remove coveralls integration d811962 Concept revision and documentation (API Change): bdae92a Replace asynchronous helper macros with template aliases (API Change) df95a09 Move prepare_buffers to prepare_buffer.hpp (API Change) 787de21 Remove placeholders (API Change) c59b544 Trim Appveyor matrix rows b7184f3 Return http::error::end_of_stream on HTTP read eof (API Change) f2d8255 Set version to 40 40b9194 Tidy up .travis.yml: 9b240c7 Fix basic_streambuf movable trait 76a2617 Consolidate get_lowest_layer in type_traits.hpp 6d00321 Add to_static_string: f888136 Set version to 39 47c82b5 Better travis deps 4ed7865 Squelch openssl spurious leak and memory errors b6bc26f Fixed braced-init error with older gcc 59b2f8f ostream workaround for gcc 4.8.4 8363d86 Increase ostream test coverage 5631936 Tidy up HTTP reason_string (API Change): 2bf5150 Harmonize concepts and identifiers with net-ts (API Change): 728e9d8 Tidy up basic_parser javadocs 1c9067b Use beast::string_view alias 771c5ca Doc fixes and tidying e2b5c31 Rename to buffered_read_stream (API Change): a753f1c Rename to static_buffer, static_buffer_n (API Change): 24b6686 Rename to flat_buffer, basic_flat_buffer (API Change): 69259ef Rename to multi_buffer, basic_multi_buffer (API Change): bef9ae1 New buffers() replaces to_string() (API Change): a7ef4f5 New ostream() returns dynamic buffer output stream (API Change): 87fd60c Fix eof error on ssl::stream shutdown 606fc9d Add websocket async echo ssl server test: ff5e659 Refactor http::header contents (API Change): dd02097 Set version to 1.0.0-b38 5596e97 Prevent basic_fields operator[] assignment c2b32dc Remove websocket::keep_alive option (API Change): 32dbfb2 Refactor WebSocket error codes (API Change): dd6b500 WebSocket doc work 0b4d87c More flat_streambuf tests aacefb4 Add test_allocator to extras/test 931a5fb Simplify get_lowest_layer test ba4228a Use static_string for WebSocket handshakes: 6df3ff3 Refactor base64: 19b124d Refactor static_string: 30e8d16 Set version to 1.0.0-b37 b141020 Fix narrowing warning in table constants d554b81 Add -funsigned-char to asan build target bcc6ad8 Add ub sanitizer blacklist e1f08e9 Fix flat_streambuf: 7d08f59 Fix typo in documentation example 21ef97d Rename to http::dynamic_body, consolidate header: 45a2d73 Rename project to http-bench c86fee9 Move everything in basic_fields.hpp to fields.hpp (API Change) a14a5d6 Rename to detail::is_invocable 540d037 Rename to websocket::detail::pausation 84e1739 Document websocket::stream thread safety dc274af Add is_upgrade() free function: 2c17d04 Refactor websocket decorators (API Change): 235fe68 Provide websocket::stream accept() overloads (API Change): a715825 CMake hide command lines in .vcxproj Output windows 32024d8 Set version to 1.0.0-b36 f48b95f Update README.md d8db5f1 Set version to 1.0.0-b35 dd2a514 Tidy up doc declarations 2c50aba Fix README.md CMake instructions 4ffdce2 Update .gitignore for VS2017 403011f Remove http::empty_body (API Change) f47b661 New HTTP interfaces (API Change): f6835b8 Rename to BEAST_DOXYGEN 7e37723 Add flat_streambuf: 5b68faa Doc XSL support for list and table markdown 3de46de Make websocket::close_code a proper enum: 0128743 Tidy up MSVC CMake configuration ccd188e Add appveyor build script git-subtree-dir: src/beast git-subtree-split: 3bcd9865f80f12ba5faad35c564918f85b02e271
This commit is contained in:
@@ -1,52 +1,47 @@
|
||||
# Part of Beast
|
||||
|
||||
GroupSources(example example)
|
||||
GroupSources(extras/beast extras)
|
||||
GroupSources(include/beast beast)
|
||||
|
||||
GroupSources(test/http "/")
|
||||
|
||||
add_executable (http-tests
|
||||
${BEAST_INCLUDES}
|
||||
${EXAMPLE_INCLUDES}
|
||||
${EXTRAS_INCLUDES}
|
||||
message_fuzz.hpp
|
||||
fail_parser.hpp
|
||||
test_parser.hpp
|
||||
../../extras/beast/unit_test/main.cpp
|
||||
basic_dynabuf_body.cpp
|
||||
basic_fields.cpp
|
||||
basic_parser_v1.cpp
|
||||
concepts.cpp
|
||||
basic_parser.cpp
|
||||
buffer_body.cpp
|
||||
doc_examples.cpp
|
||||
doc_snippets.cpp
|
||||
dynamic_body.cpp
|
||||
empty_body.cpp
|
||||
error.cpp
|
||||
field.cpp
|
||||
fields.cpp
|
||||
header_parser_v1.cpp
|
||||
file_body.cpp
|
||||
message.cpp
|
||||
parse.cpp
|
||||
parse_error.cpp
|
||||
parser_v1.cpp
|
||||
parser.cpp
|
||||
read.cpp
|
||||
reason.cpp
|
||||
rfc7230.cpp
|
||||
streambuf_body.cpp
|
||||
serializer.cpp
|
||||
span_body.cpp
|
||||
status.cpp
|
||||
string_body.cpp
|
||||
type_traits.cpp
|
||||
vector_body.cpp
|
||||
verb.cpp
|
||||
write.cpp
|
||||
chunk_encode.cpp
|
||||
)
|
||||
|
||||
if (NOT WIN32)
|
||||
target_link_libraries(http-tests ${Boost_LIBRARIES} Threads::Threads)
|
||||
else()
|
||||
target_link_libraries(http-tests ${Boost_LIBRARIES})
|
||||
endif()
|
||||
|
||||
add_executable (bench-tests
|
||||
${BEAST_INCLUDES}
|
||||
${EXTRAS_INCLUDES}
|
||||
nodejs_parser.hpp
|
||||
../../extras/beast/unit_test/main.cpp
|
||||
nodejs_parser.cpp
|
||||
parser_bench.cpp
|
||||
)
|
||||
|
||||
if (NOT WIN32)
|
||||
target_link_libraries(bench-tests ${Boost_LIBRARIES} Threads::Threads)
|
||||
else()
|
||||
target_link_libraries(bench-tests ${Boost_LIBRARIES})
|
||||
endif()
|
||||
target_link_libraries(http-tests
|
||||
Beast
|
||||
${Boost_PROGRAM_OPTIONS_LIBRARY}
|
||||
${Boost_FILESYSTEM_LIBRARY}
|
||||
${Boost_COROUTINE_LIBRARY}
|
||||
${Boost_THREAD_LIBRARY}
|
||||
${Boost_CONTEXT_LIBRARY}
|
||||
)
|
||||
|
||||
31
test/http/Jamfile
Normal file
31
test/http/Jamfile
Normal file
@@ -0,0 +1,31 @@
|
||||
#
|
||||
# Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
#
|
||||
|
||||
unit-test http-tests :
|
||||
../../extras/beast/unit_test/main.cpp
|
||||
basic_parser.cpp
|
||||
buffer_body.cpp
|
||||
doc_examples.cpp
|
||||
doc_snippets.cpp
|
||||
dynamic_body.cpp
|
||||
error.cpp
|
||||
field.cpp
|
||||
fields.cpp
|
||||
file_body.cpp
|
||||
message.cpp
|
||||
parser.cpp
|
||||
read.cpp
|
||||
rfc7230.cpp
|
||||
serializer.cpp
|
||||
span_body.cpp
|
||||
status.cpp
|
||||
string_body.cpp
|
||||
type_traits.cpp
|
||||
vector_body.cpp
|
||||
verb.cpp
|
||||
write.cpp
|
||||
;
|
||||
@@ -1,9 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/basic_dynabuf_body.hpp>
|
||||
@@ -1,96 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/basic_fields.hpp>
|
||||
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class basic_fields_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
template<class Allocator>
|
||||
using bha = basic_fields<Allocator>;
|
||||
|
||||
using bh = basic_fields<std::allocator<char>>;
|
||||
|
||||
template<class Allocator>
|
||||
static
|
||||
void
|
||||
fill(std::size_t n, basic_fields<Allocator>& h)
|
||||
{
|
||||
for(std::size_t i = 1; i<= n; ++i)
|
||||
h.insert(boost::lexical_cast<std::string>(i), i);
|
||||
}
|
||||
|
||||
template<class U, class V>
|
||||
static
|
||||
void
|
||||
self_assign(U& u, V&& v)
|
||||
{
|
||||
u = std::forward<V>(v);
|
||||
}
|
||||
|
||||
void testHeaders()
|
||||
{
|
||||
bh h1;
|
||||
BEAST_EXPECT(h1.empty());
|
||||
fill(1, h1);
|
||||
BEAST_EXPECT(h1.size() == 1);
|
||||
bh h2;
|
||||
h2 = h1;
|
||||
BEAST_EXPECT(h2.size() == 1);
|
||||
h2.insert("2", "2");
|
||||
BEAST_EXPECT(std::distance(h2.begin(), h2.end()) == 2);
|
||||
h1 = std::move(h2);
|
||||
BEAST_EXPECT(h1.size() == 2);
|
||||
BEAST_EXPECT(h2.size() == 0);
|
||||
bh h3(std::move(h1));
|
||||
BEAST_EXPECT(h3.size() == 2);
|
||||
BEAST_EXPECT(h1.size() == 0);
|
||||
self_assign(h3, std::move(h3));
|
||||
BEAST_EXPECT(h3.size() == 2);
|
||||
BEAST_EXPECT(h2.erase("Not-Present") == 0);
|
||||
}
|
||||
|
||||
void testRFC2616()
|
||||
{
|
||||
bh h;
|
||||
h.insert("a", "w");
|
||||
h.insert("a", "x");
|
||||
h.insert("aa", "y");
|
||||
h.insert("b", "z");
|
||||
BEAST_EXPECT(h.count("a") == 2);
|
||||
}
|
||||
|
||||
void testErase()
|
||||
{
|
||||
bh h;
|
||||
h.insert("a", "w");
|
||||
h.insert("a", "x");
|
||||
h.insert("aa", "y");
|
||||
h.insert("b", "z");
|
||||
BEAST_EXPECT(h.size() == 4);
|
||||
h.erase("a");
|
||||
BEAST_EXPECT(h.size() == 2);
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
testHeaders();
|
||||
testRFC2616();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(basic_fields,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
1164
test/http/basic_parser.cpp
Normal file
1164
test/http/basic_parser.cpp
Normal file
File diff suppressed because it is too large
Load Diff
File diff suppressed because it is too large
Load Diff
@@ -6,4 +6,4 @@
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/concepts.hpp>
|
||||
#include <beast/http/buffer_body.hpp>
|
||||
@@ -1,136 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/chunk_encode.hpp>
|
||||
|
||||
#include <beast/core/to_string.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class chunk_encode_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
struct final_chunk
|
||||
{
|
||||
std::string s;
|
||||
|
||||
final_chunk() = default;
|
||||
|
||||
explicit
|
||||
final_chunk(std::string s_)
|
||||
: s(std::move(s_))
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
static
|
||||
void
|
||||
encode1(std::string& s, final_chunk const& fc)
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
if(! fc.s.empty())
|
||||
s.append(to_string(chunk_encode(
|
||||
false, buffer(fc.s.data(), fc.s.size()))));
|
||||
s.append(to_string(chunk_encode_final()));
|
||||
}
|
||||
|
||||
static
|
||||
void
|
||||
encode1(std::string& s, std::string const& piece)
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
s.append(to_string(chunk_encode(
|
||||
false, buffer(piece.data(), piece.size()))));
|
||||
}
|
||||
|
||||
static
|
||||
inline
|
||||
void
|
||||
encode(std::string&)
|
||||
{
|
||||
}
|
||||
|
||||
template<class Arg, class... Args>
|
||||
static
|
||||
void
|
||||
encode(std::string& s, Arg const& arg, Args const&... args)
|
||||
{
|
||||
encode1(s, arg);
|
||||
encode(s, args...);
|
||||
}
|
||||
|
||||
template<class... Args>
|
||||
void
|
||||
check(std::string const& answer, Args const&... args)
|
||||
{
|
||||
std::string s;
|
||||
encode(s, args...);
|
||||
BEAST_EXPECT(s == answer);
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
check(
|
||||
"0\r\n\r\n"
|
||||
"0\r\n\r\n",
|
||||
"", final_chunk{});
|
||||
|
||||
check(
|
||||
"1\r\n"
|
||||
"*\r\n"
|
||||
"0\r\n\r\n",
|
||||
final_chunk("*"));
|
||||
|
||||
check(
|
||||
"2\r\n"
|
||||
"**\r\n"
|
||||
"0\r\n\r\n",
|
||||
final_chunk("**"));
|
||||
|
||||
check(
|
||||
"1\r\n"
|
||||
"*\r\n"
|
||||
"1\r\n"
|
||||
"*\r\n"
|
||||
"0\r\n\r\n",
|
||||
"*", final_chunk("*"));
|
||||
|
||||
check(
|
||||
"5\r\n"
|
||||
"*****\r\n"
|
||||
"7\r\n"
|
||||
"*******\r\n"
|
||||
"0\r\n\r\n",
|
||||
"*****", final_chunk("*******"));
|
||||
|
||||
check(
|
||||
"1\r\n"
|
||||
"*\r\n"
|
||||
"1\r\n"
|
||||
"*\r\n"
|
||||
"0\r\n\r\n",
|
||||
"*", "*", final_chunk{});
|
||||
|
||||
check(
|
||||
"4\r\n"
|
||||
"****\r\n"
|
||||
"0\r\n\r\n",
|
||||
"****", final_chunk{});
|
||||
|
||||
BEAST_EXPECT(to_string(chunk_encode(true,
|
||||
boost::asio::buffer("****", 4))) ==
|
||||
"4\r\n****\r\n0\r\n\r\n");
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(chunk_encode,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
303
test/http/doc_examples.cpp
Normal file
303
test/http/doc_examples.cpp
Normal file
@@ -0,0 +1,303 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
#include "example/doc/http_examples.hpp"
|
||||
|
||||
#include <beast/core/read_size.hpp>
|
||||
#include <beast/core/detail/clamp.hpp>
|
||||
#include <beast/test/pipe_stream.hpp>
|
||||
#include <beast/test/string_istream.hpp>
|
||||
#include <beast/test/string_ostream.hpp>
|
||||
#include <beast/test/yield_to.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <sstream>
|
||||
#include <array>
|
||||
#include <vector>
|
||||
#include <list>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class doc_examples_test
|
||||
: public beast::unit_test::suite
|
||||
, public beast::test::enable_yield_to
|
||||
{
|
||||
public:
|
||||
// two threads, for some examples using a pipe
|
||||
doc_examples_test()
|
||||
: enable_yield_to(2)
|
||||
{
|
||||
}
|
||||
|
||||
template<bool isRequest>
|
||||
bool
|
||||
equal_body(string_view sv, string_view body)
|
||||
{
|
||||
test::string_istream si{
|
||||
get_io_service(), sv.to_string()};
|
||||
message<isRequest, string_body, fields> m;
|
||||
multi_buffer b;
|
||||
try
|
||||
{
|
||||
read(si, b, m);
|
||||
return m.body == body;
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
log << "equal_body: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
doExpect100Continue()
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
yield_to(
|
||||
[&](yield_context)
|
||||
{
|
||||
error_code ec;
|
||||
flat_buffer buffer;
|
||||
receive_expect_100_continue(
|
||||
p.server, buffer, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
},
|
||||
[&](yield_context)
|
||||
{
|
||||
flat_buffer buffer;
|
||||
request<string_body> req;
|
||||
req.version = 11;
|
||||
req.method_string("POST");
|
||||
req.target("/");
|
||||
req.insert(field::user_agent, "test");
|
||||
req.body = "Hello, world!";
|
||||
req.prepare_payload();
|
||||
|
||||
error_code ec;
|
||||
send_expect_100_continue(
|
||||
p.client, buffer, req, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
doCgiResponse()
|
||||
{
|
||||
std::string const s = "Hello, world!";
|
||||
test::pipe child{ios_};
|
||||
child.server.read_size(3);
|
||||
ostream(child.server.buffer) << s;
|
||||
child.client.close();
|
||||
test::pipe p{ios_};
|
||||
error_code ec;
|
||||
send_cgi_response(child.server, p.client, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(equal_body<false>(p.server.str(), s));
|
||||
}
|
||||
|
||||
void
|
||||
doRelay()
|
||||
{
|
||||
request<string_body> req;
|
||||
req.version = 11;
|
||||
req.method_string("POST");
|
||||
req.target("/");
|
||||
req.insert(field::user_agent, "test");
|
||||
req.body = "Hello, world!";
|
||||
req.prepare_payload();
|
||||
|
||||
test::pipe downstream{ios_};
|
||||
downstream.server.read_size(3);
|
||||
test::pipe upstream{ios_};
|
||||
upstream.client.write_size(3);
|
||||
|
||||
error_code ec;
|
||||
write(downstream.client, req);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
downstream.client.close();
|
||||
|
||||
flat_buffer buffer;
|
||||
relay<true>(upstream.client, downstream.server, buffer, ec,
|
||||
[&](header<true, fields>& h, error_code& ev)
|
||||
{
|
||||
ev = {};
|
||||
h.erase("Content-Length");
|
||||
h.set("Transfer-Encoding", "chunked");
|
||||
});
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(equal_body<true>(
|
||||
upstream.server.str(), req.body));
|
||||
}
|
||||
|
||||
void
|
||||
doReadStdStream()
|
||||
{
|
||||
std::string const s =
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"\r\n"
|
||||
"Hello, world!";
|
||||
std::istringstream is(s);
|
||||
error_code ec;
|
||||
flat_buffer buffer;
|
||||
response<string_body> res;
|
||||
read_istream(is, buffer, res, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(boost::lexical_cast<
|
||||
std::string>(res) == s);
|
||||
}
|
||||
|
||||
void
|
||||
doWriteStdStream()
|
||||
{
|
||||
std::ostringstream os;
|
||||
request<string_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::get);
|
||||
req.target("/");
|
||||
req.insert(field::user_agent, "test");
|
||||
error_code ec;
|
||||
write_ostream(os, req, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(boost::lexical_cast<
|
||||
std::string>(req) == os.str());
|
||||
}
|
||||
|
||||
void
|
||||
doCustomParser()
|
||||
{
|
||||
{
|
||||
string_view s{
|
||||
"POST / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 13\r\n"
|
||||
"\r\n"
|
||||
"Hello, world!"
|
||||
};
|
||||
error_code ec;
|
||||
custom_parser<true> p;
|
||||
p.put(boost::asio::buffer(
|
||||
s.data(), s.size()), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
{
|
||||
string_view s{
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"d\r\n"
|
||||
"Hello, world!"
|
||||
"\r\n"
|
||||
"0\r\n\r\n"
|
||||
};
|
||||
error_code ec;
|
||||
custom_parser<false> p;
|
||||
p.put(boost::asio::buffer(
|
||||
s.data(), s.size()), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
doHEAD()
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
yield_to(
|
||||
[&](yield_context)
|
||||
{
|
||||
error_code ec;
|
||||
flat_buffer buffer;
|
||||
do_server_head(p.server, buffer, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
},
|
||||
[&](yield_context)
|
||||
{
|
||||
error_code ec;
|
||||
flat_buffer buffer;
|
||||
auto res = do_head_request(p.client, buffer, "/", ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
});
|
||||
}
|
||||
|
||||
struct handler
|
||||
{
|
||||
std::string body;
|
||||
|
||||
template<class Body>
|
||||
void
|
||||
operator()(request<Body>&&)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
operator()(request<string_body>&& req)
|
||||
{
|
||||
body = req.body;
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
doDeferredBody()
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
ostream(p.server.buffer) <<
|
||||
"POST / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Type: multipart/form-data\r\n"
|
||||
"Content-Length: 13\r\n"
|
||||
"\r\n"
|
||||
"Hello, world!";
|
||||
|
||||
handler h;
|
||||
flat_buffer buffer;
|
||||
do_form_request(p.server, buffer, h);
|
||||
BEAST_EXPECT(h.body == "Hello, world!");
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
doIncrementalRead()
|
||||
{
|
||||
test::pipe c{ios_};
|
||||
std::string s(2048, '*');
|
||||
ostream(c.server.buffer) <<
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Length: 2048\r\n"
|
||||
"Server: test\r\n"
|
||||
"\r\n" <<
|
||||
s;
|
||||
error_code ec;
|
||||
flat_buffer b;
|
||||
std::stringstream ss;
|
||||
read_and_print_body<false>(ss, c.server, b, ec);
|
||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||
BEAST_EXPECT(ss.str() == s);
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
void
|
||||
run()
|
||||
{
|
||||
doExpect100Continue();
|
||||
doCgiResponse();
|
||||
doRelay();
|
||||
doReadStdStream();
|
||||
doWriteStdStream();
|
||||
doCustomParser();
|
||||
doHEAD();
|
||||
doDeferredBody();
|
||||
doIncrementalRead();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(doc_examples,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
328
test/http/doc_snippets.cpp
Normal file
328
test/http/doc_snippets.cpp
Normal file
@@ -0,0 +1,328 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
#include <beast/core.hpp>
|
||||
#include <boost/asio.hpp>
|
||||
#include <boost/config.hpp>
|
||||
#include <iostream>
|
||||
#include <thread>
|
||||
|
||||
using namespace beast;
|
||||
|
||||
//[http_snippet_1
|
||||
|
||||
#include <beast/http.hpp>
|
||||
using namespace beast::http;
|
||||
|
||||
//]
|
||||
|
||||
namespace doc_http_snippets {
|
||||
|
||||
void fxx() {
|
||||
|
||||
boost::asio::io_service ios;
|
||||
boost::asio::io_service::work work{ios};
|
||||
std::thread t{[&](){ ios.run(); }};
|
||||
boost::asio::ip::tcp::socket sock{ios};
|
||||
|
||||
{
|
||||
//[http_snippet_2
|
||||
|
||||
request<empty_body> req;
|
||||
req.version = 11; // HTTP/1.1
|
||||
req.method(verb::get);
|
||||
req.target("/index.htm");
|
||||
req.set(field::accept, "text/html");
|
||||
req.set(field::user_agent, "Beast");
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_3
|
||||
|
||||
response<string_body> res;
|
||||
res.version = 11; // HTTP/1.1
|
||||
res.result(status::ok);
|
||||
res.set(field::server, "Beast");
|
||||
res.body = "Hello, world!";
|
||||
res.prepare_payload();
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_4
|
||||
|
||||
flat_buffer buffer; // (The parser is optimized for flat buffers)
|
||||
request<string_body> req;
|
||||
read(sock, buffer, req);
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_5
|
||||
|
||||
flat_buffer buffer;
|
||||
response<string_body> res;
|
||||
async_read(sock, buffer, res,
|
||||
[&](error_code ec)
|
||||
{
|
||||
std::cerr << ec.message() << std::endl;
|
||||
});
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_6
|
||||
|
||||
// This buffer's max size is too small for much of anything
|
||||
flat_buffer buffer{10};
|
||||
|
||||
// Try to read a request
|
||||
error_code ec;
|
||||
request<string_body> req;
|
||||
read(sock, buffer, req, ec);
|
||||
if(ec == error::buffer_overflow)
|
||||
std::cerr << "Buffer limit exceeded!" << std::endl;
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_7
|
||||
|
||||
response<string_body> res;
|
||||
res.version = 11;
|
||||
res.result(status::ok);
|
||||
res.set(field::server, "Beast");
|
||||
res.body = "Hello, world!";
|
||||
|
||||
error_code ec;
|
||||
write(sock, res, ec);
|
||||
if(ec == error::end_of_stream)
|
||||
sock.close();
|
||||
//]
|
||||
|
||||
//[http_snippet_8
|
||||
async_write(sock, res,
|
||||
[&](error_code)
|
||||
{
|
||||
if(ec)
|
||||
std::cerr << ec.message() << std::endl;
|
||||
});
|
||||
//]
|
||||
}
|
||||
|
||||
{
|
||||
//[http_snippet_10
|
||||
|
||||
response<string_body> res;
|
||||
|
||||
response_serializer<string_body> sr{res};
|
||||
|
||||
//]
|
||||
}
|
||||
|
||||
} // fxx()
|
||||
|
||||
//[http_snippet_12
|
||||
|
||||
/** Send a message to a stream synchronously.
|
||||
|
||||
@param stream The stream to write to. This type must support
|
||||
the @b SyncWriteStream concept.
|
||||
|
||||
@param m The message to send. The Body type must support
|
||||
the @b BodyReader concept.
|
||||
*/
|
||||
template<
|
||||
class SyncWriteStream,
|
||||
bool isRequest, class Body, class Fields>
|
||||
void
|
||||
send(
|
||||
SyncWriteStream& stream,
|
||||
message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
// Check the template types
|
||||
static_assert(is_sync_write_stream<SyncWriteStream>::value,
|
||||
"SyncWriteStream requirements not met");
|
||||
static_assert(is_body_reader<Body>::value,
|
||||
"BodyReader requirements not met");
|
||||
|
||||
// Create the instance of serializer for the message
|
||||
serializer<isRequest, Body, Fields> sr{m};
|
||||
|
||||
// Loop until the serializer is finished
|
||||
do
|
||||
{
|
||||
// This call guarantees it will make some
|
||||
// forward progress, or otherwise return an error.
|
||||
write_some(stream, sr);
|
||||
}
|
||||
while(! sr.is_done());
|
||||
}
|
||||
|
||||
//]
|
||||
|
||||
//[http_snippet_13
|
||||
|
||||
template<class SyncReadStream>
|
||||
void
|
||||
print_response(SyncReadStream& stream)
|
||||
{
|
||||
static_assert(is_sync_read_stream<SyncReadStream>::value,
|
||||
"SyncReadStream requirements not met");
|
||||
|
||||
// Declare a parser for an HTTP response
|
||||
response_parser<string_body> parser;
|
||||
|
||||
// Read the entire message
|
||||
read(stream, parser);
|
||||
|
||||
// Now print the message
|
||||
std::cout << parser.get() << std::endl;
|
||||
}
|
||||
|
||||
//]
|
||||
|
||||
#ifdef BOOST_MSVC
|
||||
//[http_snippet_14
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
print_cxx14(message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
error_code ec;
|
||||
serializer<isRequest, Body, Fields> sr{m};
|
||||
do
|
||||
{
|
||||
sr.next(ec,
|
||||
[&sr](error_code& ec, auto const& buffer)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
std::cout << buffers(buffer);
|
||||
sr.consume(boost::asio::buffer_size(buffer));
|
||||
});
|
||||
}
|
||||
while(! ec && ! sr.is_done());
|
||||
if(! ec)
|
||||
std::cout << std::endl;
|
||||
else
|
||||
std::cerr << ec.message() << std::endl;
|
||||
}
|
||||
|
||||
//]
|
||||
#endif
|
||||
|
||||
//[http_snippet_15
|
||||
|
||||
template<class Serializer>
|
||||
struct lambda
|
||||
{
|
||||
Serializer& sr;
|
||||
|
||||
lambda(Serializer& sr_) : sr(sr_) {}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
void operator()(error_code& ec, ConstBufferSequence const& buffer) const
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
std::cout << buffers(buffer);
|
||||
sr.consume(boost::asio::buffer_size(buffer));
|
||||
}
|
||||
};
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
print(message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
error_code ec;
|
||||
serializer<isRequest, Body, Fields> sr{m};
|
||||
do
|
||||
{
|
||||
sr.next(ec, lambda<decltype(sr)>{sr});
|
||||
}
|
||||
while(! ec && ! sr.is_done());
|
||||
if(! ec)
|
||||
std::cout << std::endl;
|
||||
else
|
||||
std::cerr << ec.message() << std::endl;
|
||||
}
|
||||
|
||||
//]
|
||||
|
||||
#if BOOST_MSVC
|
||||
//[http_snippet_16
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
void
|
||||
split_print_cxx14(message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
error_code ec;
|
||||
serializer<isRequest, Body, Fields> sr{m};
|
||||
sr.split(true);
|
||||
std::cout << "Header:" << std::endl;
|
||||
do
|
||||
{
|
||||
sr.next(ec,
|
||||
[&sr](error_code& ec, auto const& buffer)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
std::cout << buffers(buffer);
|
||||
sr.consume(boost::asio::buffer_size(buffer));
|
||||
});
|
||||
}
|
||||
while(! sr.is_header_done());
|
||||
if(! ec && ! sr.is_done())
|
||||
{
|
||||
std::cout << "Body:" << std::endl;
|
||||
do
|
||||
{
|
||||
sr.next(ec,
|
||||
[&sr](error_code& ec, auto const& buffer)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
std::cout << buffers(buffer);
|
||||
sr.consume(boost::asio::buffer_size(buffer));
|
||||
});
|
||||
}
|
||||
while(! ec && ! sr.is_done());
|
||||
}
|
||||
if(ec)
|
||||
std::cerr << ec.message() << std::endl;
|
||||
}
|
||||
|
||||
//]
|
||||
#endif
|
||||
|
||||
//[http_snippet_17
|
||||
|
||||
struct decorator
|
||||
{
|
||||
std::string s;
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
string_view
|
||||
operator()(ConstBufferSequence const& buffers)
|
||||
{
|
||||
s = ";x=" + std::to_string(boost::asio::buffer_size(buffers));
|
||||
return s;
|
||||
}
|
||||
|
||||
string_view
|
||||
operator()(boost::asio::null_buffers)
|
||||
{
|
||||
return "Result: OK\r\n";
|
||||
}
|
||||
};
|
||||
|
||||
//]
|
||||
|
||||
} // doc_http_snippets
|
||||
@@ -6,11 +6,11 @@
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/streambuf_body.hpp>
|
||||
#include <beast/http/dynamic_body.hpp>
|
||||
|
||||
#include <beast/core/to_string.hpp>
|
||||
#include <beast/core/ostream.hpp>
|
||||
#include <beast/http/fields.hpp>
|
||||
#include <beast/http/parser_v1.hpp>
|
||||
#include <beast/http/parser.hpp>
|
||||
#include <beast/http/read.hpp>
|
||||
#include <beast/http/write.hpp>
|
||||
#include <beast/test/string_istream.hpp>
|
||||
@@ -20,12 +20,13 @@
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class streambuf_body_test : public beast::unit_test::suite
|
||||
class dynamic_body_test : public beast::unit_test::suite
|
||||
{
|
||||
boost::asio::io_service ios_;
|
||||
|
||||
public:
|
||||
void run() override
|
||||
void
|
||||
run() override
|
||||
{
|
||||
std::string const s =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
@@ -34,15 +35,17 @@ public:
|
||||
"\r\n"
|
||||
"xyz";
|
||||
test::string_istream ss(ios_, s);
|
||||
parser_v1<false, streambuf_body, fields> p;
|
||||
streambuf sb;
|
||||
parse(ss, sb, p);
|
||||
BEAST_EXPECT(to_string(p.get().body.data()) == "xyz");
|
||||
BEAST_EXPECT(boost::lexical_cast<std::string>(p.get()) == s);
|
||||
response_parser<dynamic_body> p;
|
||||
multi_buffer b;
|
||||
read(ss, b, p);
|
||||
auto const& m = p.get();
|
||||
BEAST_EXPECT(boost::lexical_cast<std::string>(
|
||||
buffers(m.body.data())) == "xyz");
|
||||
BEAST_EXPECT(boost::lexical_cast<std::string>(m) == s);
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(streambuf_body,http,beast);
|
||||
BEAST_DEFINE_TESTSUITE(dynamic_body,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
@@ -7,3 +7,13 @@
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/empty_body.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
BOOST_STATIC_ASSERT(is_body<empty_body>::value);
|
||||
BOOST_STATIC_ASSERT(is_body_reader<empty_body>::value);
|
||||
BOOST_STATIC_ASSERT(is_body_writer<empty_body>::value);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
66
test/http/error.cpp
Normal file
66
test/http/error.cpp
Normal file
@@ -0,0 +1,66 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/error.hpp>
|
||||
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class error_test : public unit_test::suite
|
||||
{
|
||||
public:
|
||||
void
|
||||
check(char const* name, error ev)
|
||||
{
|
||||
auto const ec = make_error_code(ev);
|
||||
BEAST_EXPECT(std::string(ec.category().name()) == name);
|
||||
BEAST_EXPECT(! ec.message().empty());
|
||||
BEAST_EXPECT(std::addressof(ec.category()) ==
|
||||
std::addressof(detail::get_http_error_category()));
|
||||
BEAST_EXPECT(detail::get_http_error_category().equivalent(
|
||||
static_cast<std::underlying_type<error>::type>(ev),
|
||||
ec.category().default_error_condition(
|
||||
static_cast<std::underlying_type<error>::type>(ev))));
|
||||
BEAST_EXPECT(detail::get_http_error_category().equivalent(
|
||||
ec, static_cast<std::underlying_type<error>::type>(ev)));
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
check("beast.http", error::end_of_stream);
|
||||
check("beast.http", error::partial_message);
|
||||
check("beast.http", error::need_more);
|
||||
check("beast.http", error::unexpected_body);
|
||||
check("beast.http", error::need_buffer);
|
||||
check("beast.http", error::buffer_overflow);
|
||||
check("beast.http", error::body_limit);
|
||||
check("beast.http", error::bad_alloc);
|
||||
|
||||
check("beast.http", error::bad_line_ending);
|
||||
check("beast.http", error::bad_method);
|
||||
check("beast.http", error::bad_target);
|
||||
check("beast.http", error::bad_version);
|
||||
check("beast.http", error::bad_status);
|
||||
check("beast.http", error::bad_reason);
|
||||
check("beast.http", error::bad_field);
|
||||
check("beast.http", error::bad_value);
|
||||
check("beast.http", error::bad_content_length);
|
||||
check("beast.http", error::bad_transfer_encoding);
|
||||
check("beast.http", error::bad_chunk);
|
||||
check("beast.http", error::bad_obs_fold);
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(error,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
@@ -1,120 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
#ifndef BEAST_HTTP_TEST_FAIL_PARSER_HPP
|
||||
#define BEAST_HTTP_TEST_FAIL_PARSER_HPP
|
||||
|
||||
#include <beast/http/basic_parser_v1.hpp>
|
||||
#include <beast/test/fail_counter.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
template<bool isRequest>
|
||||
class fail_parser
|
||||
: public basic_parser_v1<isRequest, fail_parser<isRequest>>
|
||||
{
|
||||
test::fail_counter& fc_;
|
||||
std::uint64_t content_length_ = no_content_length;
|
||||
body_what body_rv_ = body_what::normal;
|
||||
|
||||
public:
|
||||
std::string body;
|
||||
|
||||
template<class... Args>
|
||||
explicit
|
||||
fail_parser(test::fail_counter& fc, Args&&... args)
|
||||
: fc_(fc)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
on_body_rv(body_what rv)
|
||||
{
|
||||
body_rv_ = rv;
|
||||
}
|
||||
|
||||
// valid on successful parse
|
||||
std::uint64_t
|
||||
content_length() const
|
||||
{
|
||||
return content_length_;
|
||||
}
|
||||
|
||||
void on_start(error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
|
||||
void on_method(boost::string_ref const&, error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
|
||||
void on_uri(boost::string_ref const&, error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
|
||||
void on_reason(boost::string_ref const&, error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
|
||||
void on_request(error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
|
||||
void on_response(error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
|
||||
void on_field(boost::string_ref const&, error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
|
||||
void on_value(boost::string_ref const&, error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
|
||||
void
|
||||
on_header(std::uint64_t content_length, error_code& ec)
|
||||
{
|
||||
if(fc_.fail(ec))
|
||||
return;
|
||||
}
|
||||
|
||||
body_what
|
||||
on_body_what(std::uint64_t content_length, error_code& ec)
|
||||
{
|
||||
if(fc_.fail(ec))
|
||||
return body_what::normal;
|
||||
content_length_ = content_length;
|
||||
return body_rv_;
|
||||
}
|
||||
|
||||
void on_body(boost::string_ref const& s, error_code& ec)
|
||||
{
|
||||
if(fc_.fail(ec))
|
||||
return;
|
||||
body.append(s.data(), s.size());
|
||||
}
|
||||
|
||||
void on_complete(error_code& ec)
|
||||
{
|
||||
fc_.fail(ec);
|
||||
}
|
||||
};
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
#endif
|
||||
405
test/http/field.cpp
Normal file
405
test/http/field.cpp
Normal file
@@ -0,0 +1,405 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/field.hpp>
|
||||
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class field_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void
|
||||
testField()
|
||||
{
|
||||
auto const match =
|
||||
[&](field f, string_view s)
|
||||
{
|
||||
BEAST_EXPECT(iequals(to_string(f), s));
|
||||
BEAST_EXPECT(string_to_field(s) == f);
|
||||
};
|
||||
|
||||
match(field::accept, "accept");
|
||||
match(field::accept, "aCcept");
|
||||
match(field::accept, "ACCEPT");
|
||||
|
||||
|
||||
match(field::a_im, "A-IM");
|
||||
match(field::accept, "Accept");
|
||||
match(field::accept_additions, "Accept-Additions");
|
||||
match(field::accept_charset, "Accept-Charset");
|
||||
match(field::accept_datetime, "Accept-Datetime");
|
||||
match(field::accept_encoding, "Accept-Encoding");
|
||||
match(field::accept_features, "Accept-Features");
|
||||
match(field::accept_language, "Accept-Language");
|
||||
match(field::accept_patch, "Accept-Patch");
|
||||
match(field::accept_post, "Accept-Post");
|
||||
match(field::accept_ranges, "Accept-Ranges");
|
||||
match(field::access_control, "Access-Control");
|
||||
match(field::access_control_allow_credentials, "Access-Control-Allow-Credentials");
|
||||
match(field::access_control_allow_headers, "Access-Control-Allow-Headers");
|
||||
match(field::access_control_allow_methods, "Access-Control-Allow-Methods");
|
||||
match(field::access_control_allow_origin, "Access-Control-Allow-Origin");
|
||||
match(field::access_control_max_age, "Access-Control-Max-Age");
|
||||
match(field::access_control_request_headers, "Access-Control-Request-Headers");
|
||||
match(field::access_control_request_method, "Access-Control-Request-Method");
|
||||
match(field::age, "Age");
|
||||
match(field::allow, "Allow");
|
||||
match(field::alpn, "ALPN");
|
||||
match(field::also_control, "Also-Control");
|
||||
match(field::alt_svc, "Alt-Svc");
|
||||
match(field::alt_used, "Alt-Used");
|
||||
match(field::alternate_recipient, "Alternate-Recipient");
|
||||
match(field::alternates, "Alternates");
|
||||
match(field::apparently_to, "Apparently-To");
|
||||
match(field::apply_to_redirect_ref, "Apply-To-Redirect-Ref");
|
||||
match(field::approved, "Approved");
|
||||
match(field::archive, "Archive");
|
||||
match(field::archived_at, "Archived-At");
|
||||
match(field::article_names, "Article-Names");
|
||||
match(field::article_updates, "Article-Updates");
|
||||
match(field::authentication_control, "Authentication-Control");
|
||||
match(field::authentication_info, "Authentication-Info");
|
||||
match(field::authentication_results, "Authentication-Results");
|
||||
match(field::authorization, "Authorization");
|
||||
match(field::auto_submitted, "Auto-Submitted");
|
||||
match(field::autoforwarded, "Autoforwarded");
|
||||
match(field::autosubmitted, "Autosubmitted");
|
||||
match(field::base, "Base");
|
||||
match(field::bcc, "Bcc");
|
||||
match(field::body, "Body");
|
||||
match(field::c_ext, "C-Ext");
|
||||
match(field::c_man, "C-Man");
|
||||
match(field::c_opt, "C-Opt");
|
||||
match(field::c_pep, "C-PEP");
|
||||
match(field::c_pep_info, "C-PEP-Info");
|
||||
match(field::cache_control, "Cache-Control");
|
||||
match(field::caldav_timezones, "CalDAV-Timezones");
|
||||
match(field::cancel_key, "Cancel-Key");
|
||||
match(field::cancel_lock, "Cancel-Lock");
|
||||
match(field::cc, "Cc");
|
||||
match(field::close, "Close");
|
||||
match(field::comments, "Comments");
|
||||
match(field::compliance, "Compliance");
|
||||
match(field::connection, "Connection");
|
||||
match(field::content_alternative, "Content-Alternative");
|
||||
match(field::content_base, "Content-Base");
|
||||
match(field::content_description, "Content-Description");
|
||||
match(field::content_disposition, "Content-Disposition");
|
||||
match(field::content_duration, "Content-Duration");
|
||||
match(field::content_encoding, "Content-Encoding");
|
||||
match(field::content_features, "Content-features");
|
||||
match(field::content_id, "Content-ID");
|
||||
match(field::content_identifier, "Content-Identifier");
|
||||
match(field::content_language, "Content-Language");
|
||||
match(field::content_length, "Content-Length");
|
||||
match(field::content_location, "Content-Location");
|
||||
match(field::content_md5, "Content-MD5");
|
||||
match(field::content_range, "Content-Range");
|
||||
match(field::content_return, "Content-Return");
|
||||
match(field::content_script_type, "Content-Script-Type");
|
||||
match(field::content_style_type, "Content-Style-Type");
|
||||
match(field::content_transfer_encoding, "Content-Transfer-Encoding");
|
||||
match(field::content_type, "Content-Type");
|
||||
match(field::content_version, "Content-Version");
|
||||
match(field::control, "Control");
|
||||
match(field::conversion, "Conversion");
|
||||
match(field::conversion_with_loss, "Conversion-With-Loss");
|
||||
match(field::cookie, "Cookie");
|
||||
match(field::cookie2, "Cookie2");
|
||||
match(field::cost, "Cost");
|
||||
match(field::dasl, "DASL");
|
||||
match(field::date, "Date");
|
||||
match(field::date_received, "Date-Received");
|
||||
match(field::dav, "DAV");
|
||||
match(field::default_style, "Default-Style");
|
||||
match(field::deferred_delivery, "Deferred-Delivery");
|
||||
match(field::delivery_date, "Delivery-Date");
|
||||
match(field::delta_base, "Delta-Base");
|
||||
match(field::depth, "Depth");
|
||||
match(field::derived_from, "Derived-From");
|
||||
match(field::destination, "Destination");
|
||||
match(field::differential_id, "Differential-ID");
|
||||
match(field::digest, "Digest");
|
||||
match(field::discarded_x400_ipms_extensions, "Discarded-X400-IPMS-Extensions");
|
||||
match(field::discarded_x400_mts_extensions, "Discarded-X400-MTS-Extensions");
|
||||
match(field::disclose_recipients, "Disclose-Recipients");
|
||||
match(field::disposition_notification_options, "Disposition-Notification-Options");
|
||||
match(field::disposition_notification_to, "Disposition-Notification-To");
|
||||
match(field::distribution, "Distribution");
|
||||
match(field::dkim_signature, "DKIM-Signature");
|
||||
match(field::dl_expansion_history, "DL-Expansion-History");
|
||||
match(field::downgraded_bcc, "Downgraded-Bcc");
|
||||
match(field::downgraded_cc, "Downgraded-Cc");
|
||||
match(field::downgraded_disposition_notification_to, "Downgraded-Disposition-Notification-To");
|
||||
match(field::downgraded_final_recipient, "Downgraded-Final-Recipient");
|
||||
match(field::downgraded_from, "Downgraded-From");
|
||||
match(field::downgraded_in_reply_to, "Downgraded-In-Reply-To");
|
||||
match(field::downgraded_mail_from, "Downgraded-Mail-From");
|
||||
match(field::downgraded_message_id, "Downgraded-Message-Id");
|
||||
match(field::downgraded_original_recipient, "Downgraded-Original-Recipient");
|
||||
match(field::downgraded_rcpt_to, "Downgraded-Rcpt-To");
|
||||
match(field::downgraded_references, "Downgraded-References");
|
||||
match(field::downgraded_reply_to, "Downgraded-Reply-To");
|
||||
match(field::downgraded_resent_bcc, "Downgraded-Resent-Bcc");
|
||||
match(field::downgraded_resent_cc, "Downgraded-Resent-Cc");
|
||||
match(field::downgraded_resent_from, "Downgraded-Resent-From");
|
||||
match(field::downgraded_resent_reply_to, "Downgraded-Resent-Reply-To");
|
||||
match(field::downgraded_resent_sender, "Downgraded-Resent-Sender");
|
||||
match(field::downgraded_resent_to, "Downgraded-Resent-To");
|
||||
match(field::downgraded_return_path, "Downgraded-Return-Path");
|
||||
match(field::downgraded_sender, "Downgraded-Sender");
|
||||
match(field::downgraded_to, "Downgraded-To");
|
||||
match(field::ediint_features, "EDIINT-Features");
|
||||
match(field::eesst_version, "Eesst-Version");
|
||||
match(field::encoding, "Encoding");
|
||||
match(field::encrypted, "Encrypted");
|
||||
match(field::errors_to, "Errors-To");
|
||||
match(field::etag, "ETag");
|
||||
match(field::expect, "Expect");
|
||||
match(field::expires, "Expires");
|
||||
match(field::expiry_date, "Expiry-Date");
|
||||
match(field::ext, "Ext");
|
||||
match(field::followup_to, "Followup-To");
|
||||
match(field::forwarded, "Forwarded");
|
||||
match(field::from, "From");
|
||||
match(field::generate_delivery_report, "Generate-Delivery-Report");
|
||||
match(field::getprofile, "GetProfile");
|
||||
match(field::hobareg, "Hobareg");
|
||||
match(field::host, "Host");
|
||||
match(field::http2_settings, "HTTP2-Settings");
|
||||
match(field::if_, "If");
|
||||
match(field::if_match, "If-Match");
|
||||
match(field::if_modified_since, "If-Modified-Since");
|
||||
match(field::if_none_match, "If-None-Match");
|
||||
match(field::if_range, "If-Range");
|
||||
match(field::if_schedule_tag_match, "If-Schedule-Tag-Match");
|
||||
match(field::if_unmodified_since, "If-Unmodified-Since");
|
||||
match(field::im, "IM");
|
||||
match(field::importance, "Importance");
|
||||
match(field::in_reply_to, "In-Reply-To");
|
||||
match(field::incomplete_copy, "Incomplete-Copy");
|
||||
match(field::injection_date, "Injection-Date");
|
||||
match(field::injection_info, "Injection-Info");
|
||||
match(field::jabber_id, "Jabber-ID");
|
||||
match(field::keep_alive, "Keep-Alive");
|
||||
match(field::keywords, "Keywords");
|
||||
match(field::label, "Label");
|
||||
match(field::language, "Language");
|
||||
match(field::last_modified, "Last-Modified");
|
||||
match(field::latest_delivery_time, "Latest-Delivery-Time");
|
||||
match(field::lines, "Lines");
|
||||
match(field::link, "Link");
|
||||
match(field::list_archive, "List-Archive");
|
||||
match(field::list_help, "List-Help");
|
||||
match(field::list_id, "List-ID");
|
||||
match(field::list_owner, "List-Owner");
|
||||
match(field::list_post, "List-Post");
|
||||
match(field::list_subscribe, "List-Subscribe");
|
||||
match(field::list_unsubscribe, "List-Unsubscribe");
|
||||
match(field::list_unsubscribe_post, "List-Unsubscribe-Post");
|
||||
match(field::location, "Location");
|
||||
match(field::lock_token, "Lock-Token");
|
||||
match(field::man, "Man");
|
||||
match(field::max_forwards, "Max-Forwards");
|
||||
match(field::memento_datetime, "Memento-Datetime");
|
||||
match(field::message_context, "Message-Context");
|
||||
match(field::message_id, "Message-ID");
|
||||
match(field::message_type, "Message-Type");
|
||||
match(field::meter, "Meter");
|
||||
match(field::method_check, "Method-Check");
|
||||
match(field::method_check_expires, "Method-Check-Expires");
|
||||
match(field::mime_version, "MIME-Version");
|
||||
match(field::mmhs_acp127_message_identifier, "MMHS-Acp127-Message-Identifier");
|
||||
match(field::mmhs_authorizing_users, "MMHS-Authorizing-Users");
|
||||
match(field::mmhs_codress_message_indicator, "MMHS-Codress-Message-Indicator");
|
||||
match(field::mmhs_copy_precedence, "MMHS-Copy-Precedence");
|
||||
match(field::mmhs_exempted_address, "MMHS-Exempted-Address");
|
||||
match(field::mmhs_extended_authorisation_info, "MMHS-Extended-Authorisation-Info");
|
||||
match(field::mmhs_handling_instructions, "MMHS-Handling-Instructions");
|
||||
match(field::mmhs_message_instructions, "MMHS-Message-Instructions");
|
||||
match(field::mmhs_message_type, "MMHS-Message-Type");
|
||||
match(field::mmhs_originator_plad, "MMHS-Originator-PLAD");
|
||||
match(field::mmhs_originator_reference, "MMHS-Originator-Reference");
|
||||
match(field::mmhs_other_recipients_indicator_cc, "MMHS-Other-Recipients-Indicator-CC");
|
||||
match(field::mmhs_other_recipients_indicator_to, "MMHS-Other-Recipients-Indicator-To");
|
||||
match(field::mmhs_primary_precedence, "MMHS-Primary-Precedence");
|
||||
match(field::mmhs_subject_indicator_codes, "MMHS-Subject-Indicator-Codes");
|
||||
match(field::mt_priority, "MT-Priority");
|
||||
match(field::negotiate, "Negotiate");
|
||||
match(field::newsgroups, "Newsgroups");
|
||||
match(field::nntp_posting_date, "NNTP-Posting-Date");
|
||||
match(field::nntp_posting_host, "NNTP-Posting-Host");
|
||||
match(field::non_compliance, "Non-Compliance");
|
||||
match(field::obsoletes, "Obsoletes");
|
||||
match(field::opt, "Opt");
|
||||
match(field::optional, "Optional");
|
||||
match(field::optional_www_authenticate, "Optional-WWW-Authenticate");
|
||||
match(field::ordering_type, "Ordering-Type");
|
||||
match(field::organization, "Organization");
|
||||
match(field::origin, "Origin");
|
||||
match(field::original_encoded_information_types, "Original-Encoded-Information-Types");
|
||||
match(field::original_from, "Original-From");
|
||||
match(field::original_message_id, "Original-Message-ID");
|
||||
match(field::original_recipient, "Original-Recipient");
|
||||
match(field::original_sender, "Original-Sender");
|
||||
match(field::original_subject, "Original-Subject");
|
||||
match(field::originator_return_address, "Originator-Return-Address");
|
||||
match(field::overwrite, "Overwrite");
|
||||
match(field::p3p, "P3P");
|
||||
match(field::path, "Path");
|
||||
match(field::pep, "PEP");
|
||||
match(field::pep_info, "Pep-Info");
|
||||
match(field::pics_label, "PICS-Label");
|
||||
match(field::position, "Position");
|
||||
match(field::posting_version, "Posting-Version");
|
||||
match(field::pragma, "Pragma");
|
||||
match(field::prefer, "Prefer");
|
||||
match(field::preference_applied, "Preference-Applied");
|
||||
match(field::prevent_nondelivery_report, "Prevent-NonDelivery-Report");
|
||||
match(field::priority, "Priority");
|
||||
match(field::privicon, "Privicon");
|
||||
match(field::profileobject, "ProfileObject");
|
||||
match(field::protocol, "Protocol");
|
||||
match(field::protocol_info, "Protocol-Info");
|
||||
match(field::protocol_query, "Protocol-Query");
|
||||
match(field::protocol_request, "Protocol-Request");
|
||||
match(field::proxy_authenticate, "Proxy-Authenticate");
|
||||
match(field::proxy_authentication_info, "Proxy-Authentication-Info");
|
||||
match(field::proxy_authorization, "Proxy-Authorization");
|
||||
match(field::proxy_connection, "Proxy-Connection");
|
||||
match(field::proxy_features, "Proxy-Features");
|
||||
match(field::proxy_instruction, "Proxy-Instruction");
|
||||
match(field::public_, "Public");
|
||||
match(field::public_key_pins, "Public-Key-Pins");
|
||||
match(field::public_key_pins_report_only, "Public-Key-Pins-Report-Only");
|
||||
match(field::range, "Range");
|
||||
match(field::received, "Received");
|
||||
match(field::received_spf, "Received-SPF");
|
||||
match(field::redirect_ref, "Redirect-Ref");
|
||||
match(field::references, "References");
|
||||
match(field::referer, "Referer");
|
||||
match(field::referer_root, "Referer-Root");
|
||||
match(field::relay_version, "Relay-Version");
|
||||
match(field::reply_by, "Reply-By");
|
||||
match(field::reply_to, "Reply-To");
|
||||
match(field::require_recipient_valid_since, "Require-Recipient-Valid-Since");
|
||||
match(field::resent_bcc, "Resent-Bcc");
|
||||
match(field::resent_cc, "Resent-Cc");
|
||||
match(field::resent_date, "Resent-Date");
|
||||
match(field::resent_from, "Resent-From");
|
||||
match(field::resent_message_id, "Resent-Message-ID");
|
||||
match(field::resent_reply_to, "Resent-Reply-To");
|
||||
match(field::resent_sender, "Resent-Sender");
|
||||
match(field::resent_to, "Resent-To");
|
||||
match(field::resolution_hint, "Resolution-Hint");
|
||||
match(field::resolver_location, "Resolver-Location");
|
||||
match(field::retry_after, "Retry-After");
|
||||
match(field::return_path, "Return-Path");
|
||||
match(field::safe, "Safe");
|
||||
match(field::schedule_reply, "Schedule-Reply");
|
||||
match(field::schedule_tag, "Schedule-Tag");
|
||||
match(field::sec_websocket_accept, "Sec-WebSocket-Accept");
|
||||
match(field::sec_websocket_extensions, "Sec-WebSocket-Extensions");
|
||||
match(field::sec_websocket_key, "Sec-WebSocket-Key");
|
||||
match(field::sec_websocket_protocol, "Sec-WebSocket-Protocol");
|
||||
match(field::sec_websocket_version, "Sec-WebSocket-Version");
|
||||
match(field::security_scheme, "Security-Scheme");
|
||||
match(field::see_also, "See-Also");
|
||||
match(field::sender, "Sender");
|
||||
match(field::sensitivity, "Sensitivity");
|
||||
match(field::server, "Server");
|
||||
match(field::set_cookie, "Set-Cookie");
|
||||
match(field::set_cookie2, "Set-Cookie2");
|
||||
match(field::setprofile, "SetProfile");
|
||||
match(field::sio_label, "SIO-Label");
|
||||
match(field::sio_label_history, "SIO-Label-History");
|
||||
match(field::slug, "SLUG");
|
||||
match(field::soapaction, "SoapAction");
|
||||
match(field::solicitation, "Solicitation");
|
||||
match(field::status_uri, "Status-URI");
|
||||
match(field::strict_transport_security, "Strict-Transport-Security");
|
||||
match(field::subject, "Subject");
|
||||
match(field::subok, "SubOK");
|
||||
match(field::subst, "Subst");
|
||||
match(field::summary, "Summary");
|
||||
match(field::supersedes, "Supersedes");
|
||||
match(field::surrogate_capability, "Surrogate-Capability");
|
||||
match(field::surrogate_control, "Surrogate-Control");
|
||||
match(field::tcn, "TCN");
|
||||
match(field::te, "TE");
|
||||
match(field::timeout, "Timeout");
|
||||
match(field::title, "Title");
|
||||
match(field::to, "To");
|
||||
match(field::topic, "Topic");
|
||||
match(field::trailer, "Trailer");
|
||||
match(field::transfer_encoding, "Transfer-Encoding");
|
||||
match(field::ttl, "TTL");
|
||||
match(field::ua_color, "UA-Color");
|
||||
match(field::ua_media, "UA-Media");
|
||||
match(field::ua_pixels, "UA-Pixels");
|
||||
match(field::ua_resolution, "UA-Resolution");
|
||||
match(field::ua_windowpixels, "UA-Windowpixels");
|
||||
match(field::upgrade, "Upgrade");
|
||||
match(field::urgency, "Urgency");
|
||||
match(field::uri, "URI");
|
||||
match(field::user_agent, "User-Agent");
|
||||
match(field::variant_vary, "Variant-Vary");
|
||||
match(field::vary, "Vary");
|
||||
match(field::vbr_info, "VBR-Info");
|
||||
match(field::version, "Version");
|
||||
match(field::via, "Via");
|
||||
match(field::want_digest, "Want-Digest");
|
||||
match(field::warning, "Warning");
|
||||
match(field::www_authenticate, "WWW-Authenticate");
|
||||
match(field::x_archived_at, "X-Archived-At");
|
||||
match(field::x_device_accept, "X-Device-Accept");
|
||||
match(field::x_device_accept_charset, "X-Device-Accept-Charset");
|
||||
match(field::x_device_accept_encoding, "X-Device-Accept-Encoding");
|
||||
match(field::x_device_accept_language, "X-Device-Accept-Language");
|
||||
match(field::x_device_user_agent, "X-Device-User-Agent");
|
||||
match(field::x_frame_options, "X-Frame-Options");
|
||||
match(field::x_mittente, "X-Mittente");
|
||||
match(field::x_pgp_sig, "X-PGP-Sig");
|
||||
match(field::x_ricevuta, "X-Ricevuta");
|
||||
match(field::x_riferimento_message_id, "X-Riferimento-Message-ID");
|
||||
match(field::x_tiporicevuta, "X-TipoRicevuta");
|
||||
match(field::x_trasporto, "X-Trasporto");
|
||||
match(field::x_verificasicurezza, "X-VerificaSicurezza");
|
||||
match(field::x400_content_identifier, "X400-Content-Identifier");
|
||||
match(field::x400_content_return, "X400-Content-Return");
|
||||
match(field::x400_content_type, "X400-Content-Type");
|
||||
match(field::x400_mts_identifier, "X400-MTS-Identifier");
|
||||
match(field::x400_originator, "X400-Originator");
|
||||
match(field::x400_received, "X400-Received");
|
||||
match(field::x400_recipients, "X400-Recipients");
|
||||
match(field::x400_trace, "X400-Trace");
|
||||
match(field::xref, "Xref");
|
||||
|
||||
auto const unknown =
|
||||
[&](string_view s)
|
||||
{
|
||||
BEAST_EXPECT(string_to_field(s) == field::unknown);
|
||||
};
|
||||
unknown("");
|
||||
unknown("x");
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
testField();
|
||||
pass();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(field,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
@@ -7,3 +7,915 @@
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/fields.hpp>
|
||||
|
||||
#include <beast/http/empty_body.hpp>
|
||||
#include <beast/http/message.hpp>
|
||||
#include <beast/http/type_traits.hpp>
|
||||
#include <beast/test/test_allocator.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <boost/lexical_cast.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
BOOST_STATIC_ASSERT(is_fields<fields>::value);
|
||||
|
||||
class fields_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
template<class Allocator>
|
||||
using fa_t = basic_fields<Allocator>;
|
||||
|
||||
using f_t = fa_t<std::allocator<char>>;
|
||||
|
||||
template<class Allocator>
|
||||
static
|
||||
void
|
||||
fill(std::size_t n, basic_fields<Allocator>& f)
|
||||
{
|
||||
for(std::size_t i = 1; i<= n; ++i)
|
||||
f.insert(boost::lexical_cast<std::string>(i), i);
|
||||
}
|
||||
|
||||
template<class U, class V>
|
||||
static
|
||||
void
|
||||
self_assign(U& u, V&& v)
|
||||
{
|
||||
u = std::forward<V>(v);
|
||||
}
|
||||
|
||||
template<class Alloc>
|
||||
static
|
||||
bool
|
||||
empty(basic_fields<Alloc> const& f)
|
||||
{
|
||||
return f.begin() == f.end();
|
||||
}
|
||||
|
||||
template<class Alloc>
|
||||
static
|
||||
std::size_t
|
||||
size(basic_fields<Alloc> const& f)
|
||||
{
|
||||
return std::distance(f.begin(), f.end());
|
||||
}
|
||||
|
||||
void
|
||||
testMembers()
|
||||
{
|
||||
using namespace test;
|
||||
|
||||
// compare equal
|
||||
using equal_t = test::test_allocator<char,
|
||||
true, true, true, true, true>;
|
||||
|
||||
// compare not equal
|
||||
using unequal_t = test::test_allocator<char,
|
||||
false, true, true, true, true>;
|
||||
|
||||
// construction
|
||||
{
|
||||
{
|
||||
fields f;
|
||||
BEAST_EXPECT(f.begin() == f.end());
|
||||
}
|
||||
{
|
||||
unequal_t a1;
|
||||
basic_fields<unequal_t> f{a1};
|
||||
BEAST_EXPECT(f.get_allocator() == a1);
|
||||
BEAST_EXPECT(f.get_allocator() != unequal_t{});
|
||||
}
|
||||
}
|
||||
|
||||
// move construction
|
||||
{
|
||||
{
|
||||
basic_fields<equal_t> f1;
|
||||
BEAST_EXPECT(f1.get_allocator()->nmove == 0);
|
||||
f1.insert("1", "1");
|
||||
BEAST_EXPECT(f1["1"] == "1");
|
||||
basic_fields<equal_t> f2{std::move(f1)};
|
||||
BEAST_EXPECT(f2.get_allocator()->nmove == 1);
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
BEAST_EXPECT(f1["1"] == "");
|
||||
}
|
||||
// allocators equal
|
||||
{
|
||||
basic_fields<equal_t> f1;
|
||||
f1.insert("1", "1");
|
||||
equal_t a;
|
||||
basic_fields<equal_t> f2{std::move(f1), a};
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
BEAST_EXPECT(f1["1"] == "");
|
||||
}
|
||||
{
|
||||
// allocators unequal
|
||||
basic_fields<unequal_t> f1;
|
||||
f1.insert("1", "1");
|
||||
unequal_t a;
|
||||
basic_fields<unequal_t> f2{std::move(f1), a};
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
}
|
||||
|
||||
// copy construction
|
||||
{
|
||||
{
|
||||
basic_fields<equal_t> f1;
|
||||
f1.insert("1", "1");
|
||||
basic_fields<equal_t> f2{f1};
|
||||
BEAST_EXPECT(f1.get_allocator() == f2.get_allocator());
|
||||
BEAST_EXPECT(f1["1"] == "1");
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
{
|
||||
basic_fields<unequal_t> f1;
|
||||
f1.insert("1", "1");
|
||||
unequal_t a;
|
||||
basic_fields<unequal_t> f2(f1, a);
|
||||
BEAST_EXPECT(f1.get_allocator() != f2.get_allocator());
|
||||
BEAST_EXPECT(f1["1"] == "1");
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
{
|
||||
basic_fields<equal_t> f1;
|
||||
f1.insert("1", "1");
|
||||
basic_fields<unequal_t> f2(f1);
|
||||
BEAST_EXPECT(f1["1"] == "1");
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
{
|
||||
basic_fields<unequal_t> f1;
|
||||
f1.insert("1", "1");
|
||||
equal_t a;
|
||||
basic_fields<equal_t> f2(f1, a);
|
||||
BEAST_EXPECT(f2.get_allocator() == a);
|
||||
BEAST_EXPECT(f1["1"] == "1");
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
}
|
||||
|
||||
// move assignment
|
||||
{
|
||||
{
|
||||
fields f1;
|
||||
f1.insert("1", "1");
|
||||
fields f2;
|
||||
f2 = std::move(f1);
|
||||
BEAST_EXPECT(f1.begin() == f1.end());
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
{
|
||||
// propagate_on_container_move_assignment : true
|
||||
using pocma_t = test::test_allocator<char,
|
||||
true, true, true, true, true>;
|
||||
basic_fields<pocma_t> f1;
|
||||
f1.insert("1", "1");
|
||||
basic_fields<pocma_t> f2;
|
||||
f2 = std::move(f1);
|
||||
BEAST_EXPECT(f1.begin() == f1.end());
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
{
|
||||
// propagate_on_container_move_assignment : false
|
||||
using pocma_t = test::test_allocator<char,
|
||||
true, true, false, true, true>;
|
||||
basic_fields<pocma_t> f1;
|
||||
f1.insert("1", "1");
|
||||
basic_fields<pocma_t> f2;
|
||||
f2 = std::move(f1);
|
||||
BEAST_EXPECT(f1.begin() == f1.end());
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
}
|
||||
|
||||
// copy assignment
|
||||
{
|
||||
{
|
||||
fields f1;
|
||||
f1.insert("1", "1");
|
||||
fields f2;
|
||||
f2 = f1;
|
||||
BEAST_EXPECT(f1["1"] == "1");
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
basic_fields<equal_t> f3;
|
||||
f3 = f2;
|
||||
BEAST_EXPECT(f3["1"] == "1");
|
||||
}
|
||||
{
|
||||
// propagate_on_container_copy_assignment : true
|
||||
using pocca_t = test::test_allocator<char,
|
||||
true, true, true, true, true>;
|
||||
basic_fields<pocca_t> f1;
|
||||
f1.insert("1", "1");
|
||||
basic_fields<pocca_t> f2;
|
||||
f2 = f1;
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
{
|
||||
// propagate_on_container_copy_assignment : false
|
||||
using pocca_t = test::test_allocator<char,
|
||||
true, false, true, true, true>;
|
||||
basic_fields<pocca_t> f1;
|
||||
f1.insert("1", "1");
|
||||
basic_fields<pocca_t> f2;
|
||||
f2 = f1;
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
}
|
||||
}
|
||||
|
||||
// swap
|
||||
{
|
||||
{
|
||||
// propagate_on_container_swap : true
|
||||
using pocs_t = test::test_allocator<char,
|
||||
false, true, true, true, true>;
|
||||
pocs_t a1, a2;
|
||||
BEAST_EXPECT(a1 != a2);
|
||||
basic_fields<pocs_t> f1{a1};
|
||||
f1.insert("1", "1");
|
||||
basic_fields<pocs_t> f2{a2};
|
||||
BEAST_EXPECT(f1.get_allocator() == a1);
|
||||
BEAST_EXPECT(f2.get_allocator() == a2);
|
||||
swap(f1, f2);
|
||||
BEAST_EXPECT(f1.get_allocator() == a2);
|
||||
BEAST_EXPECT(f2.get_allocator() == a1);
|
||||
BEAST_EXPECT(f1.begin() == f1.end());
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
swap(f1, f2);
|
||||
BEAST_EXPECT(f1.get_allocator() == a1);
|
||||
BEAST_EXPECT(f2.get_allocator() == a2);
|
||||
BEAST_EXPECT(f1["1"] == "1");
|
||||
BEAST_EXPECT(f2.begin() == f2.end());
|
||||
}
|
||||
{
|
||||
// propagate_on_container_swap : false
|
||||
using pocs_t = test::test_allocator<char,
|
||||
true, true, true, false, true>;
|
||||
pocs_t a1, a2;
|
||||
BEAST_EXPECT(a1 == a2);
|
||||
BEAST_EXPECT(a1.id() != a2.id());
|
||||
basic_fields<pocs_t> f1{a1};
|
||||
f1.insert("1", "1");
|
||||
basic_fields<pocs_t> f2{a2};
|
||||
BEAST_EXPECT(f1.get_allocator() == a1);
|
||||
BEAST_EXPECT(f2.get_allocator() == a2);
|
||||
swap(f1, f2);
|
||||
BEAST_EXPECT(f1.get_allocator().id() == a1.id());
|
||||
BEAST_EXPECT(f2.get_allocator().id() == a2.id());
|
||||
BEAST_EXPECT(f1.begin() == f1.end());
|
||||
BEAST_EXPECT(f2["1"] == "1");
|
||||
swap(f1, f2);
|
||||
BEAST_EXPECT(f1.get_allocator().id() == a1.id());
|
||||
BEAST_EXPECT(f2.get_allocator().id() == a2.id());
|
||||
BEAST_EXPECT(f1["1"] == "1");
|
||||
BEAST_EXPECT(f2.begin() == f2.end());
|
||||
}
|
||||
}
|
||||
|
||||
// operations
|
||||
{
|
||||
fields f;
|
||||
f.insert(field::user_agent, "x");
|
||||
BEAST_EXPECT(f.count(field::user_agent));
|
||||
BEAST_EXPECT(f.count(to_string(field::user_agent)));
|
||||
BEAST_EXPECT(f.count(field::user_agent) == 1);
|
||||
BEAST_EXPECT(f.count(to_string(field::user_agent)) == 1);
|
||||
f.insert(field::user_agent, "y");
|
||||
BEAST_EXPECT(f.count(field::user_agent) == 2);
|
||||
}
|
||||
}
|
||||
|
||||
void testHeaders()
|
||||
{
|
||||
f_t f1;
|
||||
BEAST_EXPECT(empty(f1));
|
||||
fill(1, f1);
|
||||
BEAST_EXPECT(size(f1) == 1);
|
||||
f_t f2;
|
||||
f2 = f1;
|
||||
BEAST_EXPECT(size(f2) == 1);
|
||||
f2.insert("2", "2");
|
||||
BEAST_EXPECT(std::distance(f2.begin(), f2.end()) == 2);
|
||||
f1 = std::move(f2);
|
||||
BEAST_EXPECT(size(f1) == 2);
|
||||
BEAST_EXPECT(size(f2) == 0);
|
||||
f_t f3(std::move(f1));
|
||||
BEAST_EXPECT(size(f3) == 2);
|
||||
BEAST_EXPECT(size(f1) == 0);
|
||||
self_assign(f3, std::move(f3));
|
||||
BEAST_EXPECT(size(f3) == 2);
|
||||
BEAST_EXPECT(f2.erase("Not-Present") == 0);
|
||||
}
|
||||
|
||||
void testRFC2616()
|
||||
{
|
||||
f_t f;
|
||||
f.insert("a", "w");
|
||||
f.insert("a", "x");
|
||||
f.insert("aa", "y");
|
||||
f.insert("f", "z");
|
||||
BEAST_EXPECT(f.count("a") == 2);
|
||||
}
|
||||
|
||||
void testErase()
|
||||
{
|
||||
f_t f;
|
||||
f.insert("a", "w");
|
||||
f.insert("a", "x");
|
||||
f.insert("aa", "y");
|
||||
f.insert("f", "z");
|
||||
BEAST_EXPECT(size(f) == 4);
|
||||
f.erase("a");
|
||||
BEAST_EXPECT(size(f) == 2);
|
||||
}
|
||||
|
||||
void
|
||||
testContainer()
|
||||
{
|
||||
{
|
||||
// group fields
|
||||
fields f;
|
||||
f.insert(field::age, 1);
|
||||
f.insert(field::body, 2);
|
||||
f.insert(field::close, 3);
|
||||
f.insert(field::body, 4);
|
||||
BEAST_EXPECT(std::next(f.begin(), 0)->name() == field::age);
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->name() == field::body);
|
||||
BEAST_EXPECT(std::next(f.begin(), 2)->name() == field::body);
|
||||
BEAST_EXPECT(std::next(f.begin(), 3)->name() == field::close);
|
||||
BEAST_EXPECT(std::next(f.begin(), 0)->name_string() == "Age");
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "Body");
|
||||
BEAST_EXPECT(std::next(f.begin(), 2)->name_string() == "Body");
|
||||
BEAST_EXPECT(std::next(f.begin(), 3)->name_string() == "Close");
|
||||
BEAST_EXPECT(std::next(f.begin(), 0)->value() == "1");
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->value() == "2");
|
||||
BEAST_EXPECT(std::next(f.begin(), 2)->value() == "4");
|
||||
BEAST_EXPECT(std::next(f.begin(), 3)->value() == "3");
|
||||
BEAST_EXPECT(f.erase(field::body) == 2);
|
||||
BEAST_EXPECT(std::next(f.begin(), 0)->name_string() == "Age");
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "Close");
|
||||
}
|
||||
{
|
||||
// group fields, case insensitive
|
||||
fields f;
|
||||
f.insert("a", 1);
|
||||
f.insert("ab", 2);
|
||||
f.insert("b", 3);
|
||||
f.insert("AB", 4);
|
||||
BEAST_EXPECT(std::next(f.begin(), 0)->name() == field::unknown);
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->name() == field::unknown);
|
||||
BEAST_EXPECT(std::next(f.begin(), 2)->name() == field::unknown);
|
||||
BEAST_EXPECT(std::next(f.begin(), 3)->name() == field::unknown);
|
||||
BEAST_EXPECT(std::next(f.begin(), 0)->name_string() == "a");
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "ab");
|
||||
BEAST_EXPECT(std::next(f.begin(), 2)->name_string() == "AB");
|
||||
BEAST_EXPECT(std::next(f.begin(), 3)->name_string() == "b");
|
||||
BEAST_EXPECT(std::next(f.begin(), 0)->value() == "1");
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->value() == "2");
|
||||
BEAST_EXPECT(std::next(f.begin(), 2)->value() == "4");
|
||||
BEAST_EXPECT(std::next(f.begin(), 3)->value() == "3");
|
||||
BEAST_EXPECT(f.erase("Ab") == 2);
|
||||
BEAST_EXPECT(std::next(f.begin(), 0)->name_string() == "a");
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "b");
|
||||
}
|
||||
{
|
||||
// verify insertion orde
|
||||
fields f;
|
||||
f.insert( "a", 1);
|
||||
f.insert("dd", 2);
|
||||
f.insert("b", 3);
|
||||
f.insert("dD", 4);
|
||||
f.insert("c", 5);
|
||||
f.insert("Dd", 6);
|
||||
f.insert("DD", 7);
|
||||
f.insert( "e", 8);
|
||||
BEAST_EXPECT(f.count("dd") == 4);
|
||||
BEAST_EXPECT(std::next(f.begin(), 1)->name_string() == "dd");
|
||||
BEAST_EXPECT(std::next(f.begin(), 2)->name_string() == "dD");
|
||||
BEAST_EXPECT(std::next(f.begin(), 3)->name_string() == "Dd");
|
||||
BEAST_EXPECT(std::next(f.begin(), 4)->name_string() == "DD");
|
||||
f.set("dd", "-");
|
||||
BEAST_EXPECT(f.count("dd") == 1);
|
||||
BEAST_EXPECT(f["dd"] == "-");
|
||||
}
|
||||
}
|
||||
|
||||
struct sized_body
|
||||
{
|
||||
using value_type = std::uint64_t;
|
||||
|
||||
static
|
||||
std::uint64_t
|
||||
size(value_type const& v)
|
||||
{
|
||||
return v;
|
||||
}
|
||||
};
|
||||
|
||||
struct unsized_body
|
||||
{
|
||||
struct value_type {};
|
||||
};
|
||||
|
||||
void
|
||||
testPreparePayload()
|
||||
{
|
||||
// GET, empty
|
||||
{
|
||||
request<empty_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::get);
|
||||
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(req.count(field::transfer_encoding) == 0);
|
||||
|
||||
req.set(field::content_length, "0");
|
||||
req.set(field::transfer_encoding, "chunked");
|
||||
req.prepare_payload();
|
||||
|
||||
BEAST_EXPECT(req.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(req.count(field::transfer_encoding) == 0);
|
||||
|
||||
req.set(field::transfer_encoding, "deflate");
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "deflate");
|
||||
|
||||
req.set(field::transfer_encoding, "deflate, chunked");
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "deflate");
|
||||
}
|
||||
|
||||
// GET, sized
|
||||
{
|
||||
request<sized_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::get);
|
||||
req.body = 50;
|
||||
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req[field::content_length] == "50");
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "");
|
||||
|
||||
req.set(field::content_length, "0");
|
||||
req.set(field::transfer_encoding, "chunked");
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req[field::content_length] == "50");
|
||||
BEAST_EXPECT(req.count(field::transfer_encoding) == 0);
|
||||
|
||||
req.set(field::transfer_encoding, "deflate, chunked");
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req[field::content_length] == "50");
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "deflate");
|
||||
}
|
||||
|
||||
// PUT, empty
|
||||
{
|
||||
request<empty_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::put);
|
||||
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req[field::content_length] == "0");
|
||||
BEAST_EXPECT(req.count(field::transfer_encoding) == 0);
|
||||
|
||||
req.set(field::content_length, "50");
|
||||
req.set(field::transfer_encoding, "deflate, chunked");
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req[field::content_length] == "0");
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "deflate");
|
||||
}
|
||||
|
||||
// PUT, sized
|
||||
{
|
||||
request<sized_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::put);
|
||||
req.body = 50;
|
||||
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req[field::content_length] == "50");
|
||||
BEAST_EXPECT(req.count(field::transfer_encoding) == 0);
|
||||
|
||||
req.set(field::content_length, "25");
|
||||
req.set(field::transfer_encoding, "deflate, chunked");
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req[field::content_length] == "50");
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "deflate");
|
||||
}
|
||||
|
||||
// POST, unsized
|
||||
{
|
||||
request<unsized_body> req;
|
||||
req.version = 11;
|
||||
req.method(verb::post);
|
||||
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "chunked");
|
||||
|
||||
req.set(field::transfer_encoding, "deflate");
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "deflate, chunked");
|
||||
}
|
||||
|
||||
// POST, unsized HTTP/1.0
|
||||
{
|
||||
request<unsized_body> req;
|
||||
req.version = 10;
|
||||
req.method(verb::post);
|
||||
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(req.count(field::transfer_encoding) == 0);
|
||||
|
||||
req.set(field::transfer_encoding, "deflate");
|
||||
req.prepare_payload();
|
||||
BEAST_EXPECT(req.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(req[field::transfer_encoding] == "deflate");
|
||||
}
|
||||
|
||||
// OK, empty
|
||||
{
|
||||
response<empty_body> res;
|
||||
res.version = 11;
|
||||
|
||||
res.prepare_payload();
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.erase(field::content_length);
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
res.prepare_payload();
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
}
|
||||
|
||||
// OK, sized
|
||||
{
|
||||
response<sized_body> res;
|
||||
res.version = 11;
|
||||
res.body = 50;
|
||||
|
||||
res.prepare_payload();
|
||||
BEAST_EXPECT(res[field::content_length] == "50");
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.erase(field::content_length);
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
res.prepare_payload();
|
||||
BEAST_EXPECT(res[field::content_length] == "50");
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
}
|
||||
|
||||
// OK, unsized
|
||||
{
|
||||
response<unsized_body> res;
|
||||
res.version = 11;
|
||||
|
||||
res.prepare_payload();
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testKeepAlive()
|
||||
{
|
||||
response<empty_body> res;
|
||||
auto const keep_alive =
|
||||
[&](bool v)
|
||||
{
|
||||
res.keep_alive(v);
|
||||
BEAST_EXPECT(
|
||||
(res.keep_alive() && v) ||
|
||||
(! res.keep_alive() && ! v));
|
||||
};
|
||||
|
||||
BOOST_STATIC_ASSERT(fields::max_static_buffer == 4096);
|
||||
std::string const big(4096 + 1, 'a');
|
||||
|
||||
// HTTP/1.0
|
||||
res.version = 10;
|
||||
res.erase(field::connection);
|
||||
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "keep-alive");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "keep-alive, close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.erase(field::connection);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive");
|
||||
|
||||
res.set(field::connection, "close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive");
|
||||
|
||||
res.set(field::connection, "keep-alive");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive");
|
||||
|
||||
res.set(field::connection, "keep-alive, close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive");
|
||||
|
||||
auto const test10 =
|
||||
[&](std::string s)
|
||||
{
|
||||
res.set(field::connection, s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, s + ", close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s + ", close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, s);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s + ", keep-alive");
|
||||
|
||||
res.set(field::connection, s + ", close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s + ", keep-alive");
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive, " + s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s+ ", close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == "keep-alive, " + s);
|
||||
};
|
||||
|
||||
test10("foo");
|
||||
test10(big);
|
||||
|
||||
// HTTP/1.1
|
||||
res.version = 11;
|
||||
|
||||
res.erase(field::connection);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "keep-alive");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.set(field::connection, "keep-alive, close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res.count(field::connection) == 0);
|
||||
|
||||
res.erase(field::connection);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close");
|
||||
|
||||
res.set(field::connection, "close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close");
|
||||
|
||||
res.set(field::connection, "keep-alive");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close");
|
||||
|
||||
res.set(field::connection, "keep-alive, close");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close");
|
||||
|
||||
auto const test11 =
|
||||
[&](std::string s)
|
||||
{
|
||||
res.set(field::connection, s);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, s + ", close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s);
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s + ", close");
|
||||
keep_alive(true);
|
||||
BEAST_EXPECT(res[field::connection] == s);
|
||||
|
||||
res.set(field::connection, s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s + ", close");
|
||||
|
||||
res.set(field::connection, "close, " + s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close, " + s);
|
||||
|
||||
res.set(field::connection, "keep-alive, " + s);
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == s + ", close");
|
||||
|
||||
res.set(field::connection, "close, " + s + ", keep-alive");
|
||||
keep_alive(false);
|
||||
BEAST_EXPECT(res[field::connection] == "close, " + s);
|
||||
};
|
||||
|
||||
test11("foo");
|
||||
test11(big);
|
||||
}
|
||||
|
||||
void
|
||||
testContentLength()
|
||||
{
|
||||
response<empty_body> res{status::ok, 11};
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
auto const check = [&](std::string s)
|
||||
{
|
||||
res.set(field::transfer_encoding, s);
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s);
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s);
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s + ", chunked");
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s + ", chunked");
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, s + ", chunked");
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == s);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, " + s);
|
||||
res.content_length(0);
|
||||
BEAST_EXPECT(res[field::content_length] == "0");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, " + s);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, " + s);
|
||||
res.content_length(100);
|
||||
BEAST_EXPECT(res[field::content_length] == "100");
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, " + s);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, " + s);
|
||||
res.content_length(boost::none);
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, " + s);
|
||||
};
|
||||
|
||||
check("foo");
|
||||
|
||||
BOOST_STATIC_ASSERT(fields::max_static_buffer == 4096);
|
||||
std::string const big(4096 + 1, 'a');
|
||||
|
||||
check(big);
|
||||
}
|
||||
|
||||
void
|
||||
testChunked()
|
||||
{
|
||||
response<empty_body> res{status::ok, 11};
|
||||
BEAST_EXPECT(res.count(field::content_length) == 0);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
auto const chunked =
|
||||
[&](bool v)
|
||||
{
|
||||
res.chunked(v);
|
||||
BEAST_EXPECT(
|
||||
(res.chunked() && v) ||
|
||||
(! res.chunked() && ! v));
|
||||
BEAST_EXPECT(res.count(
|
||||
field::content_length) == 0);
|
||||
};
|
||||
|
||||
res.erase(field::transfer_encoding);
|
||||
res.set(field::content_length, 32);
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked");
|
||||
|
||||
res.erase(field::transfer_encoding);
|
||||
res.set(field::content_length, 32);
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
res.set(field::transfer_encoding, "chunked");
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res.count(field::transfer_encoding) == 0);
|
||||
|
||||
|
||||
|
||||
res.set(field::transfer_encoding, "foo");
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "foo, chunked");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, foo");
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, foo, chunked");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, foo, chunked");
|
||||
chunked(true);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, foo, chunked");
|
||||
|
||||
res.set(field::transfer_encoding, "foo, chunked");
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "foo");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, foo");
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, foo");
|
||||
|
||||
res.set(field::transfer_encoding, "chunked, foo, chunked");
|
||||
chunked(false);
|
||||
BEAST_EXPECT(res[field::transfer_encoding] == "chunked, foo");
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testMembers();
|
||||
testHeaders();
|
||||
testRFC2616();
|
||||
testErase();
|
||||
testContainer();
|
||||
testPreparePayload();
|
||||
|
||||
testKeepAlive();
|
||||
testContentLength();
|
||||
testChunked();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(fields,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
112
test/http/file_body.cpp
Normal file
112
test/http/file_body.cpp
Normal file
@@ -0,0 +1,112 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/file_body.hpp>
|
||||
|
||||
#include <beast/core/file_stdio.hpp>
|
||||
#include <beast/core/flat_buffer.hpp>
|
||||
#include <beast/http/parser.hpp>
|
||||
#include <beast/http/serializer.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <boost/filesystem.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class file_body_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
struct lambda
|
||||
{
|
||||
flat_buffer buffer;
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
void
|
||||
operator()(error_code&, ConstBufferSequence const& buffers)
|
||||
{
|
||||
buffer.commit(boost::asio::buffer_copy(
|
||||
buffer.prepare(boost::asio::buffer_size(buffers)),
|
||||
buffers));
|
||||
}
|
||||
};
|
||||
|
||||
template<class File>
|
||||
void
|
||||
doTestFileBody()
|
||||
{
|
||||
error_code ec;
|
||||
string_view const s =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Content-Length: 3\r\n"
|
||||
"\r\n"
|
||||
"xyz";
|
||||
auto const temp = boost::filesystem::unique_path();
|
||||
{
|
||||
response_parser<basic_file_body<File>> p;
|
||||
p.eager(true);
|
||||
|
||||
p.get().body.open(
|
||||
temp.string<std::string>().c_str(), file_mode::write, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
|
||||
p.put(boost::asio::buffer(s.data(), s.size()), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
{
|
||||
File f;
|
||||
f.open(temp.string<std::string>().c_str(), file_mode::read, ec);
|
||||
auto size = f.size(ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(size == 3);
|
||||
std::string s1;
|
||||
s1.resize(3);
|
||||
f.read(&s1[0], s1.size(), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECTS(s1 == "xyz", s);
|
||||
}
|
||||
{
|
||||
lambda visit;
|
||||
{
|
||||
response<basic_file_body<File>> res{status::ok, 11};
|
||||
res.set(field::server, "test");
|
||||
res.body.open(temp.string<std::string>().c_str(),
|
||||
file_mode::scan, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
res.prepare_payload();
|
||||
|
||||
serializer<false, basic_file_body<File>, fields> sr{res};
|
||||
sr.next(ec, visit);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
auto const cb = *visit.buffer.data().begin();
|
||||
string_view const s1{
|
||||
boost::asio::buffer_cast<char const*>(cb),
|
||||
boost::asio::buffer_size(cb)};
|
||||
BEAST_EXPECTS(s1 == s, s1);
|
||||
}
|
||||
}
|
||||
boost::filesystem::remove(temp, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
void
|
||||
run() override
|
||||
{
|
||||
doTestFileBody<file_stdio>();
|
||||
#if BEAST_USE_WIN32_FILE
|
||||
doTestFileBody<file_win32>();
|
||||
#endif
|
||||
#if BEAST_USE_POSIX_FILE
|
||||
doTestFileBody<file_posix>();
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(file_body,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
@@ -1,90 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/header_parser_v1.hpp>
|
||||
|
||||
#include <beast/http/fields.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <boost/asio/buffer.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class header_parser_v1_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void testParser()
|
||||
{
|
||||
{
|
||||
error_code ec;
|
||||
header_parser_v1<true, fields> p;
|
||||
BEAST_EXPECT(! p.complete());
|
||||
auto const n = p.write(boost::asio::buffer(
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"\r\n"
|
||||
), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(p.complete());
|
||||
BEAST_EXPECT(n == 36);
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
header_parser_v1<true, fields> p;
|
||||
BEAST_EXPECT(! p.complete());
|
||||
auto const n = p.write(boost::asio::buffer(
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n"
|
||||
"*****"
|
||||
), ec);
|
||||
BEAST_EXPECT(n == 55);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(p.complete());
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
header_parser_v1<false, fields> p;
|
||||
BEAST_EXPECT(! p.complete());
|
||||
auto const n = p.write(boost::asio::buffer(
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"\r\n"
|
||||
), ec);
|
||||
BEAST_EXPECT(n == 33);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(p.complete());
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
header_parser_v1<false, fields> p;
|
||||
BEAST_EXPECT(! p.complete());
|
||||
auto const n = p.write(boost::asio::buffer(
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n"
|
||||
"*****"
|
||||
), ec);
|
||||
BEAST_EXPECT(n == 52);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(p.complete());
|
||||
}
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
testParser();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(header_parser_v1,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
@@ -9,6 +9,7 @@
|
||||
#include <beast/http/message.hpp>
|
||||
|
||||
#include <beast/http/empty_body.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/http/fields.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
@@ -70,212 +71,223 @@ public:
|
||||
};
|
||||
};
|
||||
|
||||
void testMessage()
|
||||
// 0-arg
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
request<default_body>>::value);
|
||||
|
||||
// 1-arg
|
||||
BOOST_STATIC_ASSERT(! std::is_constructible<request<one_arg_body>
|
||||
>::value);
|
||||
|
||||
//BOOST_STATIC_ASSERT(! std::is_constructible<request<one_arg_body>,
|
||||
// verb, string_view, unsigned>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1&&>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1 const>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1 const&>::value);
|
||||
|
||||
// 1-arg + fields
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>,
|
||||
verb, string_view, unsigned, Arg1, fields::allocator_type>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<one_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1>>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<two_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1, Arg2>>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<request<two_arg_body>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1, Arg2>, std::tuple<fields::allocator_type>>::value);
|
||||
|
||||
// special members
|
||||
BOOST_STATIC_ASSERT(std::is_copy_constructible<header<true>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_constructible<header<true>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_copy_assignable<header<true>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_assignable<header<true>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_copy_constructible<header<false>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_constructible<header<false>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_copy_assignable<header<false>>::value);
|
||||
BOOST_STATIC_ASSERT(std::is_move_assignable<header<false>>::value);
|
||||
|
||||
void
|
||||
testMessage()
|
||||
{
|
||||
static_assert(std::is_constructible<
|
||||
message<true, default_body, fields>>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<
|
||||
message<true, one_arg_body, fields>, Arg1>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<
|
||||
message<true, one_arg_body, fields>, Arg1 const>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<
|
||||
message<true, one_arg_body, fields>, Arg1 const&>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<
|
||||
message<true, one_arg_body, fields>, Arg1&&>::value, "");
|
||||
|
||||
static_assert(! std::is_constructible<
|
||||
message<true, one_arg_body, fields>>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<
|
||||
message<true, one_arg_body, fields>,
|
||||
Arg1, fields::allocator_type>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<
|
||||
message<true, one_arg_body, fields>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1>>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<
|
||||
message<true, two_arg_body, fields>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1, Arg2>>::value, "");
|
||||
|
||||
static_assert(std::is_constructible<
|
||||
message<true, two_arg_body, fields>, std::piecewise_construct_t,
|
||||
std::tuple<Arg1, Arg2>, std::tuple<fields::allocator_type>>::value, "");
|
||||
|
||||
{
|
||||
Arg1 arg1;
|
||||
message<true, one_arg_body, fields>{std::move(arg1)};
|
||||
request<one_arg_body>{verb::get, "/", 11, std::move(arg1)};
|
||||
BEAST_EXPECT(arg1.moved);
|
||||
}
|
||||
|
||||
{
|
||||
fields h;
|
||||
h.insert("User-Agent", "test");
|
||||
message<true, one_arg_body, fields> m{Arg1{}, h};
|
||||
BEAST_EXPECT(h["User-Agent"] == "test");
|
||||
BEAST_EXPECT(m.fields["User-Agent"] == "test");
|
||||
header<true> h;
|
||||
h.set(field::user_agent, "test");
|
||||
BEAST_EXPECT(h[field::user_agent] == "test");
|
||||
request<default_body> m{std::move(h)};
|
||||
BEAST_EXPECT(m[field::user_agent] == "test");
|
||||
BEAST_EXPECT(h.count(field::user_agent) == 0);
|
||||
}
|
||||
{
|
||||
fields h;
|
||||
h.insert("User-Agent", "test");
|
||||
message<true, one_arg_body, fields> m{Arg1{}, std::move(h)};
|
||||
BEAST_EXPECT(! h.exists("User-Agent"));
|
||||
BEAST_EXPECT(m.fields["User-Agent"] == "test");
|
||||
request<empty_body> h{verb::get, "/", 10};
|
||||
h.set(field::user_agent, "test");
|
||||
request<one_arg_body> m{std::move(h.base()), Arg1{}};
|
||||
BEAST_EXPECT(m["User-Agent"] == "test");
|
||||
BEAST_EXPECT(h.count(http::field::user_agent) == 0);
|
||||
BEAST_EXPECT(m.method() == verb::get);
|
||||
BEAST_EXPECT(m.target() == "/");
|
||||
BEAST_EXPECT(m.version == 10);
|
||||
}
|
||||
|
||||
// swap
|
||||
message<true, string_body, fields> m1;
|
||||
message<true, string_body, fields> m2;
|
||||
m1.url = "u";
|
||||
request<string_body> m1;
|
||||
request<string_body> m2;
|
||||
m1.target("u");
|
||||
m1.body = "1";
|
||||
m1.fields.insert("h", "v");
|
||||
m2.method = "G";
|
||||
m1.insert("h", "v");
|
||||
m2.method_string("G");
|
||||
m2.body = "2";
|
||||
swap(m1, m2);
|
||||
BEAST_EXPECT(m1.method == "G");
|
||||
BEAST_EXPECT(m2.method.empty());
|
||||
BEAST_EXPECT(m1.url.empty());
|
||||
BEAST_EXPECT(m2.url == "u");
|
||||
BEAST_EXPECT(m1.method_string() == "G");
|
||||
BEAST_EXPECT(m2.method_string().empty());
|
||||
BEAST_EXPECT(m1.target().empty());
|
||||
BEAST_EXPECT(m2.target() == "u");
|
||||
BEAST_EXPECT(m1.body == "2");
|
||||
BEAST_EXPECT(m2.body == "1");
|
||||
BEAST_EXPECT(! m1.fields.exists("h"));
|
||||
BEAST_EXPECT(m2.fields.exists("h"));
|
||||
BEAST_EXPECT(! m1.count("h"));
|
||||
BEAST_EXPECT(m2.count("h"));
|
||||
}
|
||||
|
||||
struct MoveHeaders
|
||||
struct MoveFields : fields
|
||||
{
|
||||
bool moved_to = false;
|
||||
bool moved_from = false;
|
||||
|
||||
MoveHeaders() = default;
|
||||
MoveFields() = default;
|
||||
|
||||
MoveHeaders(MoveHeaders&& other)
|
||||
MoveFields(MoveFields&& other)
|
||||
: moved_to(true)
|
||||
{
|
||||
other.moved_from = true;
|
||||
}
|
||||
|
||||
MoveHeaders& operator=(MoveHeaders&& other)
|
||||
MoveFields& operator=(MoveFields&&)
|
||||
{
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
void testHeaders()
|
||||
struct token {};
|
||||
|
||||
struct test_fields
|
||||
{
|
||||
std::string target;
|
||||
|
||||
test_fields() = delete;
|
||||
test_fields(token) {}
|
||||
string_view get_method_impl() const { return {}; }
|
||||
string_view get_target_impl() const { return target; }
|
||||
string_view get_reason_impl() const { return {}; }
|
||||
bool get_chunked_impl() const { return false; }
|
||||
bool get_keep_alive_impl(unsigned) const { return true; }
|
||||
void set_method_impl(string_view) {}
|
||||
void set_target_impl(string_view s) { target = s.to_string(); }
|
||||
void set_reason_impl(string_view) {}
|
||||
void set_chunked_impl(bool) {}
|
||||
void set_content_length_impl(boost::optional<std::uint64_t>) {}
|
||||
void set_keep_alive_impl(unsigned, bool) {}
|
||||
};
|
||||
|
||||
void
|
||||
testMessageCtors()
|
||||
{
|
||||
{
|
||||
using req_type = request_header;
|
||||
static_assert(std::is_copy_constructible<req_type>::value, "");
|
||||
static_assert(std::is_move_constructible<req_type>::value, "");
|
||||
static_assert(std::is_copy_assignable<req_type>::value, "");
|
||||
static_assert(std::is_move_assignable<req_type>::value, "");
|
||||
|
||||
using res_type = response_header;
|
||||
static_assert(std::is_copy_constructible<res_type>::value, "");
|
||||
static_assert(std::is_move_constructible<res_type>::value, "");
|
||||
static_assert(std::is_copy_assignable<res_type>::value, "");
|
||||
static_assert(std::is_move_assignable<res_type>::value, "");
|
||||
request<empty_body> req;
|
||||
BEAST_EXPECT(req.version == 11);
|
||||
BEAST_EXPECT(req.method() == verb::unknown);
|
||||
BEAST_EXPECT(req.target() == "");
|
||||
}
|
||||
|
||||
{
|
||||
MoveHeaders h;
|
||||
header<true, MoveHeaders> r{std::move(h)};
|
||||
BEAST_EXPECT(h.moved_from);
|
||||
BEAST_EXPECT(r.fields.moved_to);
|
||||
request<string_body, MoveHeaders> m{std::move(r)};
|
||||
BEAST_EXPECT(r.fields.moved_from);
|
||||
BEAST_EXPECT(m.fields.moved_to);
|
||||
request<empty_body> req{verb::get, "/", 11};
|
||||
BEAST_EXPECT(req.version == 11);
|
||||
BEAST_EXPECT(req.method() == verb::get);
|
||||
BEAST_EXPECT(req.target() == "/");
|
||||
}
|
||||
{
|
||||
request<string_body> req{verb::get, "/", 11, "Hello"};
|
||||
BEAST_EXPECT(req.version == 11);
|
||||
BEAST_EXPECT(req.method() == verb::get);
|
||||
BEAST_EXPECT(req.target() == "/");
|
||||
BEAST_EXPECT(req.body == "Hello");
|
||||
}
|
||||
{
|
||||
request<string_body, test_fields> req{
|
||||
verb::get, "/", 11, "Hello", token{}};
|
||||
BEAST_EXPECT(req.version == 11);
|
||||
BEAST_EXPECT(req.method() == verb::get);
|
||||
BEAST_EXPECT(req.target() == "/");
|
||||
BEAST_EXPECT(req.body == "Hello");
|
||||
}
|
||||
{
|
||||
response<string_body> res;
|
||||
BEAST_EXPECT(res.version == 11);
|
||||
BEAST_EXPECT(res.result() == status::ok);
|
||||
BEAST_EXPECT(res.reason() == "OK");
|
||||
}
|
||||
{
|
||||
response<string_body> res{status::bad_request, 10};
|
||||
BEAST_EXPECT(res.version == 10);
|
||||
BEAST_EXPECT(res.result() == status::bad_request);
|
||||
BEAST_EXPECT(res.reason() == "Bad Request");
|
||||
}
|
||||
{
|
||||
response<string_body> res{status::bad_request, 10, "Hello"};
|
||||
BEAST_EXPECT(res.version == 10);
|
||||
BEAST_EXPECT(res.result() == status::bad_request);
|
||||
BEAST_EXPECT(res.reason() == "Bad Request");
|
||||
BEAST_EXPECT(res.body == "Hello");
|
||||
}
|
||||
{
|
||||
response<string_body, test_fields> res{
|
||||
status::bad_request, 10, "Hello", token{}};
|
||||
BEAST_EXPECT(res.version == 10);
|
||||
BEAST_EXPECT(res.result() == status::bad_request);
|
||||
BEAST_EXPECT(res.reason() == "Bad Request");
|
||||
BEAST_EXPECT(res.body == "Hello");
|
||||
}
|
||||
}
|
||||
|
||||
void testFreeFunctions()
|
||||
void
|
||||
testSwap()
|
||||
{
|
||||
{
|
||||
request<empty_body> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 11;
|
||||
m.fields.insert("Upgrade", "test");
|
||||
BEAST_EXPECT(! is_upgrade(m));
|
||||
|
||||
prepare(m, connection::upgrade);
|
||||
BEAST_EXPECT(is_upgrade(m));
|
||||
BEAST_EXPECT(m.fields["Connection"] == "upgrade");
|
||||
|
||||
m.version = 10;
|
||||
BEAST_EXPECT(! is_upgrade(m));
|
||||
}
|
||||
}
|
||||
|
||||
void testPrepare()
|
||||
{
|
||||
request<empty_body> m;
|
||||
m.version = 10;
|
||||
BEAST_EXPECT(! is_upgrade(m));
|
||||
m.fields.insert("Transfer-Encoding", "chunked");
|
||||
try
|
||||
{
|
||||
prepare(m);
|
||||
fail();
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
}
|
||||
m.fields.erase("Transfer-Encoding");
|
||||
m.fields.insert("Content-Length", "0");
|
||||
try
|
||||
{
|
||||
prepare(m);
|
||||
fail();
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
pass();
|
||||
}
|
||||
m.fields.erase("Content-Length");
|
||||
m.fields.insert("Connection", "keep-alive");
|
||||
try
|
||||
{
|
||||
prepare(m);
|
||||
fail();
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
pass();
|
||||
}
|
||||
m.version = 11;
|
||||
m.fields.erase("Connection");
|
||||
m.fields.insert("Connection", "close");
|
||||
BEAST_EXPECT(! is_keep_alive(m));
|
||||
}
|
||||
|
||||
void testSwap()
|
||||
{
|
||||
message<false, string_body, fields> m1;
|
||||
message<false, string_body, fields> m2;
|
||||
m1.status = 200;
|
||||
response<string_body> m1;
|
||||
response<string_body> m2;
|
||||
m1.result(status::ok);
|
||||
m1.version = 10;
|
||||
m1.body = "1";
|
||||
m1.fields.insert("h", "v");
|
||||
m2.status = 404;
|
||||
m2.reason = "OK";
|
||||
m1.insert("h", "v");
|
||||
m2.result(status::not_found);
|
||||
m2.body = "2";
|
||||
m2.version = 11;
|
||||
swap(m1, m2);
|
||||
BEAST_EXPECT(m1.status == 404);
|
||||
BEAST_EXPECT(m2.status == 200);
|
||||
BEAST_EXPECT(m1.reason == "OK");
|
||||
BEAST_EXPECT(m2.reason.empty());
|
||||
BEAST_EXPECT(m1.result() == status::not_found);
|
||||
BEAST_EXPECT(m1.result_int() == 404);
|
||||
BEAST_EXPECT(m2.result() == status::ok);
|
||||
BEAST_EXPECT(m2.result_int() == 200);
|
||||
BEAST_EXPECT(m1.reason() == "Not Found");
|
||||
BEAST_EXPECT(m2.reason() == "OK");
|
||||
BEAST_EXPECT(m1.version == 11);
|
||||
BEAST_EXPECT(m2.version == 10);
|
||||
BEAST_EXPECT(m1.body == "2");
|
||||
BEAST_EXPECT(m2.body == "1");
|
||||
BEAST_EXPECT(! m1.fields.exists("h"));
|
||||
BEAST_EXPECT(m2.fields.exists("h"));
|
||||
BEAST_EXPECT(! m1.count("h"));
|
||||
BEAST_EXPECT(m2.count("h"));
|
||||
}
|
||||
|
||||
void
|
||||
@@ -291,14 +303,70 @@ public:
|
||||
}();
|
||||
}
|
||||
|
||||
void run() override
|
||||
void
|
||||
testMethod()
|
||||
{
|
||||
header<true> h;
|
||||
auto const vcheck =
|
||||
[&](verb v)
|
||||
{
|
||||
h.method(v);
|
||||
BEAST_EXPECT(h.method() == v);
|
||||
BEAST_EXPECT(h.method_string() == to_string(v));
|
||||
};
|
||||
auto const scheck =
|
||||
[&](string_view s)
|
||||
{
|
||||
h.method_string(s);
|
||||
BEAST_EXPECT(h.method() == string_to_verb(s));
|
||||
BEAST_EXPECT(h.method_string() == s);
|
||||
};
|
||||
vcheck(verb::get);
|
||||
vcheck(verb::head);
|
||||
scheck("GET");
|
||||
scheck("HEAD");
|
||||
scheck("XYZ");
|
||||
}
|
||||
|
||||
void
|
||||
testStatus()
|
||||
{
|
||||
header<false> h;
|
||||
h.result(200);
|
||||
BEAST_EXPECT(h.result_int() == 200);
|
||||
BEAST_EXPECT(h.result() == status::ok);
|
||||
h.result(status::switching_protocols);
|
||||
BEAST_EXPECT(h.result_int() == 101);
|
||||
BEAST_EXPECT(h.result() == status::switching_protocols);
|
||||
h.result(1);
|
||||
BEAST_EXPECT(h.result_int() == 1);
|
||||
BEAST_EXPECT(h.result() == status::unknown);
|
||||
}
|
||||
|
||||
void
|
||||
testReason()
|
||||
{
|
||||
header<false> h;
|
||||
h.result(status::ok);
|
||||
BEAST_EXPECT(h.reason() == "OK");
|
||||
h.reason("Pepe");
|
||||
BEAST_EXPECT(h.reason() == "Pepe");
|
||||
h.result(status::not_found);
|
||||
BEAST_EXPECT(h.reason() == "Pepe");
|
||||
h.reason({});
|
||||
BEAST_EXPECT(h.reason() == "Not Found");
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testMessage();
|
||||
testHeaders();
|
||||
testFreeFunctions();
|
||||
testPrepare();
|
||||
testMessageCtors();
|
||||
testSwap();
|
||||
testSpecialMembers();
|
||||
testMethod();
|
||||
testStatus();
|
||||
testReason();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -8,8 +8,7 @@
|
||||
#ifndef BEAST_HTTP_TEST_MESSAGE_FUZZ_HPP
|
||||
#define BEAST_HTTP_TEST_MESSAGE_FUZZ_HPP
|
||||
|
||||
#include <beast/core/write_dynabuf.hpp>
|
||||
#include <beast/http/detail/basic_parser_v1.hpp>
|
||||
#include <beast/core/ostream.hpp>
|
||||
#include <beast/http/detail/rfc7230.hpp>
|
||||
#include <cstdint>
|
||||
#include <random>
|
||||
@@ -20,7 +19,7 @@ namespace http {
|
||||
|
||||
template<class = void>
|
||||
std::string
|
||||
escaped_string(boost::string_ref const& s)
|
||||
escaped_string(string_view s)
|
||||
{
|
||||
std::string out;
|
||||
out.reserve(s.size());
|
||||
@@ -132,7 +131,7 @@ public:
|
||||
"msrp", "msrps", "mtqp", "mumble", "mupdate", "mvn", "news", "nfs", "ni",
|
||||
"nih", "nntp", "notes", "oid", "opaquelocktoken", "pack", "palm", "paparazzi",
|
||||
"pkcs11", "platform", "pop", "pres", "prospero", "proxy", "psyc", "query",
|
||||
"redis", "rediss", "reload", "res", "resource", "rmi", "rsync", "rtmfp",
|
||||
"redis", "rediss", "reload", "res", "target", "rmi", "rsync", "rtmfp",
|
||||
"rtmp", "rtsp", "rtsps", "rtspu", "secondlife", "service", "session", "sftp",
|
||||
"sgn", "shttp", "sieve", "sip", "sips", "skype", "smb", "sms", "smtp",
|
||||
"snews", "snmp", "soap.beep", "soap.beeps", "soldat", "spotify", "ssh",
|
||||
@@ -320,7 +319,7 @@ public:
|
||||
}
|
||||
|
||||
std::string
|
||||
uri()
|
||||
target()
|
||||
{
|
||||
//switch(rand(4))
|
||||
switch(1)
|
||||
@@ -349,7 +348,7 @@ public:
|
||||
|
||||
#if 0
|
||||
std::string
|
||||
uri()
|
||||
target()
|
||||
{
|
||||
static char constexpr alpha[63] =
|
||||
"0123456789" "ABCDEFGHIJ" "KLMNOPQRST"
|
||||
@@ -477,13 +476,13 @@ public:
|
||||
void
|
||||
fields(DynamicBuffer& db)
|
||||
{
|
||||
auto os = ostream(db);
|
||||
while(rand(6))
|
||||
{
|
||||
write(db, field());
|
||||
write(db, rand(4) ? ": " : ":");
|
||||
write(db, value());
|
||||
write(db, "\r\n");
|
||||
}
|
||||
os <<
|
||||
field() <<
|
||||
(rand(4) ? ": " : ":") <<
|
||||
value() <<
|
||||
"\r\n";
|
||||
}
|
||||
|
||||
template<class DynamicBuffer>
|
||||
@@ -492,14 +491,16 @@ public:
|
||||
{
|
||||
if(! rand(4))
|
||||
{
|
||||
write(db, "Content-Length: 0\r\n\r\n");
|
||||
ostream(db) <<
|
||||
"Content-Length: 0\r\n\r\n";
|
||||
return;
|
||||
}
|
||||
if(rand(2))
|
||||
{
|
||||
auto const len = rand(500);
|
||||
write(db, "Content-Length: ", len, "\r\n\r\n");
|
||||
for(auto const& b : db.prepare(len))
|
||||
ostream(db) <<
|
||||
"Content-Length: " << len << "\r\n\r\n";
|
||||
for(boost::asio::mutable_buffer b : db.prepare(len))
|
||||
{
|
||||
auto p = boost::asio::buffer_cast<char*>(b);
|
||||
auto n = boost::asio::buffer_size(b);
|
||||
@@ -511,13 +512,15 @@ public:
|
||||
else
|
||||
{
|
||||
auto len = rand(500);
|
||||
write(db, "Transfer-Encoding: chunked\r\n\r\n");
|
||||
ostream(db) <<
|
||||
"Transfer-Encoding: chunked\r\n\r\n";
|
||||
while(len > 0)
|
||||
{
|
||||
auto n = (std::min)(1 + rand(300), len);
|
||||
len -= n;
|
||||
write(db, to_hex(n), "\r\n");
|
||||
for(auto const& b : db.prepare(n))
|
||||
ostream(db) <<
|
||||
to_hex(n) << "\r\n";
|
||||
for(boost::asio::mutable_buffer b : db.prepare(n))
|
||||
{
|
||||
auto p = boost::asio::buffer_cast<char*>(b);
|
||||
auto m = boost::asio::buffer_size(b);
|
||||
@@ -525,9 +528,9 @@ public:
|
||||
*p++ = static_cast<char>(32 + rand(26+26+10+6));
|
||||
}
|
||||
db.commit(n);
|
||||
write(db, "\r\n");
|
||||
ostream(db) << "\r\n";
|
||||
}
|
||||
write(db, "0\r\n\r\n");
|
||||
ostream(db) << "0\r\n\r\n";
|
||||
}
|
||||
}
|
||||
|
||||
@@ -535,7 +538,8 @@ public:
|
||||
void
|
||||
request(DynamicBuffer& db)
|
||||
{
|
||||
write(db, method(), " ", uri(), " HTTP/1.1\r\n");
|
||||
ostream(db) <<
|
||||
method() << " " << target() << " HTTP/1.1\r\n";
|
||||
fields(db);
|
||||
body(db);
|
||||
}
|
||||
@@ -544,14 +548,15 @@ public:
|
||||
void
|
||||
response(DynamicBuffer& db)
|
||||
{
|
||||
write(db, "HTTP/1.");
|
||||
write(db, rand(2) ? "0" : "1");
|
||||
write(db, " ", 100 + rand(401), " ");
|
||||
write(db, token());
|
||||
write(db, "\r\n");
|
||||
ostream(db) <<
|
||||
"HTTP/1." <<
|
||||
(rand(2) ? "0" : "1") << " " <<
|
||||
(100 + rand(401)) << " " <<
|
||||
token() <<
|
||||
"\r\n";
|
||||
fields(db);
|
||||
body(db);
|
||||
write(db, "\r\n");
|
||||
ostream(db) << "\r\n";
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -1,68 +0,0 @@
|
||||
# Authors ordered by first contribution.
|
||||
Ryan Dahl <ry@tinyclouds.org>
|
||||
Jeremy Hinegardner <jeremy@hinegardner.org>
|
||||
Sergey Shepelev <temotor@gmail.com>
|
||||
Joe Damato <ice799@gmail.com>
|
||||
tomika <tomika_nospam@freemail.hu>
|
||||
Phoenix Sol <phoenix@burninglabs.com>
|
||||
Cliff Frey <cliff@meraki.com>
|
||||
Ewen Cheslack-Postava <ewencp@cs.stanford.edu>
|
||||
Santiago Gala <sgala@apache.org>
|
||||
Tim Becker <tim.becker@syngenio.de>
|
||||
Jeff Terrace <jterrace@gmail.com>
|
||||
Ben Noordhuis <info@bnoordhuis.nl>
|
||||
Nathan Rajlich <nathan@tootallnate.net>
|
||||
Mark Nottingham <mnot@mnot.net>
|
||||
Aman Gupta <aman@tmm1.net>
|
||||
Tim Becker <tim.becker@kuriositaet.de>
|
||||
Sean Cunningham <sean.cunningham@mandiant.com>
|
||||
Peter Griess <pg@std.in>
|
||||
Salman Haq <salman.haq@asti-usa.com>
|
||||
Cliff Frey <clifffrey@gmail.com>
|
||||
Jon Kolb <jon@b0g.us>
|
||||
Fouad Mardini <f.mardini@gmail.com>
|
||||
Paul Querna <pquerna@apache.org>
|
||||
Felix Geisendörfer <felix@debuggable.com>
|
||||
koichik <koichik@improvement.jp>
|
||||
Andre Caron <andre.l.caron@gmail.com>
|
||||
Ivo Raisr <ivosh@ivosh.net>
|
||||
James McLaughlin <jamie@lacewing-project.org>
|
||||
David Gwynne <loki@animata.net>
|
||||
Thomas LE ROUX <thomas@november-eleven.fr>
|
||||
Randy Rizun <rrizun@ortivawireless.com>
|
||||
Andre Louis Caron <andre.louis.caron@usherbrooke.ca>
|
||||
Simon Zimmermann <simonz05@gmail.com>
|
||||
Erik Dubbelboer <erik@dubbelboer.com>
|
||||
Martell Malone <martellmalone@gmail.com>
|
||||
Bertrand Paquet <bpaquet@octo.com>
|
||||
BogDan Vatra <bogdan@kde.org>
|
||||
Peter Faiman <peter@thepicard.org>
|
||||
Corey Richardson <corey@octayn.net>
|
||||
Tóth Tamás <tomika_nospam@freemail.hu>
|
||||
Cam Swords <cam.swords@gmail.com>
|
||||
Chris Dickinson <christopher.s.dickinson@gmail.com>
|
||||
Uli Köhler <ukoehler@btronik.de>
|
||||
Charlie Somerville <charlie@charliesomerville.com>
|
||||
Patrik Stutz <patrik.stutz@gmail.com>
|
||||
Fedor Indutny <fedor.indutny@gmail.com>
|
||||
runner <runner.mei@gmail.com>
|
||||
Alexis Campailla <alexis@janeasystems.com>
|
||||
David Wragg <david@wragg.org>
|
||||
Vinnie Falco <vinnie.falco@gmail.com>
|
||||
Alex Butum <alexbutum@linux.com>
|
||||
Rex Feng <rexfeng@gmail.com>
|
||||
Alex Kocharin <alex@kocharin.ru>
|
||||
Mark Koopman <markmontymark@yahoo.com>
|
||||
Helge Heß <me@helgehess.eu>
|
||||
Alexis La Goutte <alexis.lagoutte@gmail.com>
|
||||
George Miroshnykov <george.miroshnykov@gmail.com>
|
||||
Maciej Małecki <me@mmalecki.com>
|
||||
Marc O'Morain <github.com@marcomorain.com>
|
||||
Jeff Pinner <jpinner@twitter.com>
|
||||
Timothy J Fontaine <tjfontaine@gmail.com>
|
||||
Akagi201 <akagi201@gmail.com>
|
||||
Romain Giraud <giraud.romain@gmail.com>
|
||||
Jay Satiro <raysatiro@yahoo.com>
|
||||
Arne Steen <Arne.Steen@gmx.de>
|
||||
Kjell Schubert <kjell.schubert@gmail.com>
|
||||
Olivier Mengué <dolmen@cpan.org>
|
||||
@@ -1,23 +0,0 @@
|
||||
http_parser.c is based on src/http/ngx_http_parse.c from NGINX copyright
|
||||
Igor Sysoev.
|
||||
|
||||
Additional changes are licensed under the same terms as NGINX and
|
||||
copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to
|
||||
deal in the Software without restriction, including without limitation the
|
||||
rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
sell copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in
|
||||
all copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
IN THE SOFTWARE.
|
||||
@@ -1,246 +0,0 @@
|
||||
HTTP Parser
|
||||
===========
|
||||
|
||||
[](https://travis-ci.org/nodejs/http-parser)
|
||||
|
||||
This is a parser for HTTP messages written in C. It parses both requests and
|
||||
responses. The parser is designed to be used in performance HTTP
|
||||
applications. It does not make any syscalls nor allocations, it does not
|
||||
buffer data, it can be interrupted at anytime. Depending on your
|
||||
architecture, it only requires about 40 bytes of data per message
|
||||
stream (in a web server that is per connection).
|
||||
|
||||
Features:
|
||||
|
||||
* No dependencies
|
||||
* Handles persistent streams (keep-alive).
|
||||
* Decodes chunked encoding.
|
||||
* Upgrade support
|
||||
* Defends against buffer overflow attacks.
|
||||
|
||||
The parser extracts the following information from HTTP messages:
|
||||
|
||||
* Header fields and values
|
||||
* Content-Length
|
||||
* Request method
|
||||
* Response status code
|
||||
* Transfer-Encoding
|
||||
* HTTP version
|
||||
* Request URL
|
||||
* Message body
|
||||
|
||||
|
||||
Usage
|
||||
-----
|
||||
|
||||
One `http_parser` object is used per TCP connection. Initialize the struct
|
||||
using `http_parser_init()` and set the callbacks. That might look something
|
||||
like this for a request parser:
|
||||
```c
|
||||
http_parser_settings settings;
|
||||
settings.on_url = my_url_callback;
|
||||
settings.on_header_field = my_header_field_callback;
|
||||
/* ... */
|
||||
|
||||
http_parser *parser = malloc(sizeof(http_parser));
|
||||
http_parser_init(parser, HTTP_REQUEST);
|
||||
parser->data = my_socket;
|
||||
```
|
||||
|
||||
When data is received on the socket execute the parser and check for errors.
|
||||
|
||||
```c
|
||||
size_t len = 80*1024, nparsed;
|
||||
char buf[len];
|
||||
ssize_t recved;
|
||||
|
||||
recved = recv(fd, buf, len, 0);
|
||||
|
||||
if (recved < 0) {
|
||||
/* Handle error. */
|
||||
}
|
||||
|
||||
/* Start up / continue the parser.
|
||||
* Note we pass recved==0 to signal that EOF has been received.
|
||||
*/
|
||||
nparsed = http_parser_execute(parser, &settings, buf, recved);
|
||||
|
||||
if (parser->upgrade) {
|
||||
/* handle new protocol */
|
||||
} else if (nparsed != recved) {
|
||||
/* Handle error. Usually just close the connection. */
|
||||
}
|
||||
```
|
||||
|
||||
HTTP needs to know where the end of the stream is. For example, sometimes
|
||||
servers send responses without Content-Length and expect the client to
|
||||
consume input (for the body) until EOF. To tell http_parser about EOF, give
|
||||
`0` as the fourth parameter to `http_parser_execute()`. Callbacks and errors
|
||||
can still be encountered during an EOF, so one must still be prepared
|
||||
to receive them.
|
||||
|
||||
Scalar valued message information such as `status_code`, `method`, and the
|
||||
HTTP version are stored in the parser structure. This data is only
|
||||
temporally stored in `http_parser` and gets reset on each new message. If
|
||||
this information is needed later, copy it out of the structure during the
|
||||
`headers_complete` callback.
|
||||
|
||||
The parser decodes the transfer-encoding for both requests and responses
|
||||
transparently. That is, a chunked encoding is decoded before being sent to
|
||||
the on_body callback.
|
||||
|
||||
|
||||
The Special Problem of Upgrade
|
||||
------------------------------
|
||||
|
||||
HTTP supports upgrading the connection to a different protocol. An
|
||||
increasingly common example of this is the WebSocket protocol which sends
|
||||
a request like
|
||||
|
||||
GET /demo HTTP/1.1
|
||||
Upgrade: WebSocket
|
||||
Connection: Upgrade
|
||||
Host: example.com
|
||||
Origin: http://example.com
|
||||
WebSocket-Protocol: sample
|
||||
|
||||
followed by non-HTTP data.
|
||||
|
||||
(See [RFC6455](https://tools.ietf.org/html/rfc6455) for more information the
|
||||
WebSocket protocol.)
|
||||
|
||||
To support this, the parser will treat this as a normal HTTP message without a
|
||||
body, issuing both on_headers_complete and on_message_complete callbacks. However
|
||||
http_parser_execute() will stop parsing at the end of the headers and return.
|
||||
|
||||
The user is expected to check if `parser->upgrade` has been set to 1 after
|
||||
`http_parser_execute()` returns. Non-HTTP data begins at the buffer supplied
|
||||
offset by the return value of `http_parser_execute()`.
|
||||
|
||||
|
||||
Callbacks
|
||||
---------
|
||||
|
||||
During the `http_parser_execute()` call, the callbacks set in
|
||||
`http_parser_settings` will be executed. The parser maintains state and
|
||||
never looks behind, so buffering the data is not necessary. If you need to
|
||||
save certain data for later usage, you can do that from the callbacks.
|
||||
|
||||
There are two types of callbacks:
|
||||
|
||||
* notification `typedef int (*http_cb) (http_parser*);`
|
||||
Callbacks: on_message_begin, on_headers_complete, on_message_complete.
|
||||
* data `typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);`
|
||||
Callbacks: (requests only) on_url,
|
||||
(common) on_header_field, on_header_value, on_body;
|
||||
|
||||
Callbacks must return 0 on success. Returning a non-zero value indicates
|
||||
error to the parser, making it exit immediately.
|
||||
|
||||
For cases where it is necessary to pass local information to/from a callback,
|
||||
the `http_parser` object's `data` field can be used.
|
||||
An example of such a case is when using threads to handle a socket connection,
|
||||
parse a request, and then give a response over that socket. By instantiation
|
||||
of a thread-local struct containing relevant data (e.g. accepted socket,
|
||||
allocated memory for callbacks to write into, etc), a parser's callbacks are
|
||||
able to communicate data between the scope of the thread and the scope of the
|
||||
callback in a threadsafe manner. This allows http-parser to be used in
|
||||
multi-threaded contexts.
|
||||
|
||||
Example:
|
||||
```
|
||||
typedef struct {
|
||||
socket_t sock;
|
||||
void* buffer;
|
||||
int buf_len;
|
||||
} custom_data_t;
|
||||
|
||||
|
||||
int my_url_callback(http_parser* parser, const char *at, size_t length) {
|
||||
/* access to thread local custom_data_t struct.
|
||||
Use this access save parsed data for later use into thread local
|
||||
buffer, or communicate over socket
|
||||
*/
|
||||
parser->data;
|
||||
...
|
||||
return 0;
|
||||
}
|
||||
|
||||
...
|
||||
|
||||
void http_parser_thread(socket_t sock) {
|
||||
int nparsed = 0;
|
||||
/* allocate memory for user data */
|
||||
custom_data_t *my_data = malloc(sizeof(custom_data_t));
|
||||
|
||||
/* some information for use by callbacks.
|
||||
* achieves thread -> callback information flow */
|
||||
my_data->sock = sock;
|
||||
|
||||
/* instantiate a thread-local parser */
|
||||
http_parser *parser = malloc(sizeof(http_parser));
|
||||
http_parser_init(parser, HTTP_REQUEST); /* initialise parser */
|
||||
/* this custom data reference is accessible through the reference to the
|
||||
parser supplied to callback functions */
|
||||
parser->data = my_data;
|
||||
|
||||
http_parser_settings settings; / * set up callbacks */
|
||||
settings.on_url = my_url_callback;
|
||||
|
||||
/* execute parser */
|
||||
nparsed = http_parser_execute(parser, &settings, buf, recved);
|
||||
|
||||
...
|
||||
/* parsed information copied from callback.
|
||||
can now perform action on data copied into thread-local memory from callbacks.
|
||||
achieves callback -> thread information flow */
|
||||
my_data->buffer;
|
||||
...
|
||||
}
|
||||
|
||||
```
|
||||
|
||||
In case you parse HTTP message in chunks (i.e. `read()` request line
|
||||
from socket, parse, read half headers, parse, etc) your data callbacks
|
||||
may be called more than once. Http-parser guarantees that data pointer is only
|
||||
valid for the lifetime of callback. You can also `read()` into a heap allocated
|
||||
buffer to avoid copying memory around if this fits your application.
|
||||
|
||||
Reading headers may be a tricky task if you read/parse headers partially.
|
||||
Basically, you need to remember whether last header callback was field or value
|
||||
and apply the following logic:
|
||||
|
||||
(on_header_field and on_header_value shortened to on_h_*)
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| State (prev. callback) | Callback | Description/action |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| nothing (first call) | on_h_field | Allocate new buffer and copy callback data |
|
||||
| | | into it |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| value | on_h_field | New header started. |
|
||||
| | | Copy current name,value buffers to headers |
|
||||
| | | list and allocate new buffer for new name |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| field | on_h_field | Previous name continues. Reallocate name |
|
||||
| | | buffer and append callback data to it |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| field | on_h_value | Value for current header started. Allocate |
|
||||
| | | new buffer and copy callback data to it |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
| value | on_h_value | Value continues. Reallocate value buffer |
|
||||
| | | and append callback data to it |
|
||||
------------------------ ------------ --------------------------------------------
|
||||
|
||||
|
||||
Parsing URLs
|
||||
------------
|
||||
|
||||
A simplistic zero-copy URL parser is provided as `http_parser_parse_url()`.
|
||||
Users of this library may wish to use it to parse URLs constructed from
|
||||
consecutive `on_url` callbacks.
|
||||
|
||||
See examples of reading in headers:
|
||||
|
||||
* [partial example](http://gist.github.com/155877) in C
|
||||
* [from http-parser tests](http://github.com/joyent/http-parser/blob/37a0ff8/test.c#L403) in C
|
||||
* [from Node library](http://github.com/joyent/node/blob/842eaf4/src/http.js#L284) in Javascript
|
||||
File diff suppressed because it is too large
Load Diff
@@ -1,362 +0,0 @@
|
||||
/* Copyright Joyent, Inc. and other Node contributors. All rights reserved.
|
||||
*
|
||||
* Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
* of this software and associated documentation files (the "Software"), to
|
||||
* deal in the Software without restriction, including without limitation the
|
||||
* rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
|
||||
* sell copies of the Software, and to permit persons to whom the Software is
|
||||
* furnished to do so, subject to the following conditions:
|
||||
*
|
||||
* The above copyright notice and this permission notice shall be included in
|
||||
* all copies or substantial portions of the Software.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
|
||||
* FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS
|
||||
* IN THE SOFTWARE.
|
||||
*/
|
||||
#ifndef http_parser_h
|
||||
#define http_parser_h
|
||||
#ifdef __cplusplus
|
||||
extern "C" {
|
||||
#endif
|
||||
|
||||
/* Also update SONAME in the Makefile whenever you change these. */
|
||||
#define HTTP_PARSER_VERSION_MAJOR 2
|
||||
#define HTTP_PARSER_VERSION_MINOR 7
|
||||
#define HTTP_PARSER_VERSION_PATCH 0
|
||||
|
||||
#include <sys/types.h>
|
||||
#if defined(_WIN32) && !defined(__MINGW32__) && \
|
||||
(!defined(_MSC_VER) || _MSC_VER<1600) && !defined(__WINE__)
|
||||
#include <BaseTsd.h>
|
||||
#include <stddef.h>
|
||||
typedef __int8 int8_t;
|
||||
typedef unsigned __int8 uint8_t;
|
||||
typedef __int16 int16_t;
|
||||
typedef unsigned __int16 uint16_t;
|
||||
typedef __int32 int32_t;
|
||||
typedef unsigned __int32 uint32_t;
|
||||
typedef __int64 int64_t;
|
||||
typedef unsigned __int64 uint64_t;
|
||||
#else
|
||||
#include <stdint.h>
|
||||
#endif
|
||||
|
||||
/* Compile with -DHTTP_PARSER_STRICT=0 to make less checks, but run
|
||||
* faster
|
||||
*/
|
||||
#ifndef HTTP_PARSER_STRICT
|
||||
# define HTTP_PARSER_STRICT 1
|
||||
#endif
|
||||
|
||||
/* Maximium header size allowed. If the macro is not defined
|
||||
* before including this header then the default is used. To
|
||||
* change the maximum header size, define the macro in the build
|
||||
* environment (e.g. -DHTTP_MAX_HEADER_SIZE=<value>). To remove
|
||||
* the effective limit on the size of the header, define the macro
|
||||
* to a very large number (e.g. -DHTTP_MAX_HEADER_SIZE=0x7fffffff)
|
||||
*/
|
||||
#ifndef HTTP_MAX_HEADER_SIZE
|
||||
# define HTTP_MAX_HEADER_SIZE (80*1024)
|
||||
#endif
|
||||
|
||||
typedef struct http_parser http_parser;
|
||||
typedef struct http_parser_settings http_parser_settings;
|
||||
|
||||
|
||||
/* Callbacks should return non-zero to indicate an error. The parser will
|
||||
* then halt execution.
|
||||
*
|
||||
* The one exception is on_headers_complete. In a HTTP_RESPONSE parser
|
||||
* returning '1' from on_headers_complete will tell the parser that it
|
||||
* should not expect a body. This is used when receiving a response to a
|
||||
* HEAD request which may contain 'Content-Length' or 'Transfer-Encoding:
|
||||
* chunked' headers that indicate the presence of a body.
|
||||
*
|
||||
* Returning `2` from on_headers_complete will tell parser that it should not
|
||||
* expect neither a body nor any futher responses on this connection. This is
|
||||
* useful for handling responses to a CONNECT request which may not contain
|
||||
* `Upgrade` or `Connection: upgrade` headers.
|
||||
*
|
||||
* http_data_cb does not return data chunks. It will be called arbitrarily
|
||||
* many times for each string. E.G. you might get 10 callbacks for "on_url"
|
||||
* each providing just a few characters more data.
|
||||
*/
|
||||
typedef int (*http_data_cb) (http_parser*, const char *at, size_t length);
|
||||
typedef int (*http_cb) (http_parser*);
|
||||
|
||||
|
||||
/* Request Methods */
|
||||
#define HTTP_METHOD_MAP(XX) \
|
||||
XX(0, DELETE, DELETE) \
|
||||
XX(1, GET, GET) \
|
||||
XX(2, HEAD, HEAD) \
|
||||
XX(3, POST, POST) \
|
||||
XX(4, PUT, PUT) \
|
||||
/* pathological */ \
|
||||
XX(5, CONNECT, CONNECT) \
|
||||
XX(6, OPTIONS, OPTIONS) \
|
||||
XX(7, TRACE, TRACE) \
|
||||
/* WebDAV */ \
|
||||
XX(8, COPY, COPY) \
|
||||
XX(9, LOCK, LOCK) \
|
||||
XX(10, MKCOL, MKCOL) \
|
||||
XX(11, MOVE, MOVE) \
|
||||
XX(12, PROPFIND, PROPFIND) \
|
||||
XX(13, PROPPATCH, PROPPATCH) \
|
||||
XX(14, SEARCH, SEARCH) \
|
||||
XX(15, UNLOCK, UNLOCK) \
|
||||
XX(16, BIND, BIND) \
|
||||
XX(17, REBIND, REBIND) \
|
||||
XX(18, UNBIND, UNBIND) \
|
||||
XX(19, ACL, ACL) \
|
||||
/* subversion */ \
|
||||
XX(20, REPORT, REPORT) \
|
||||
XX(21, MKACTIVITY, MKACTIVITY) \
|
||||
XX(22, CHECKOUT, CHECKOUT) \
|
||||
XX(23, MERGE, MERGE) \
|
||||
/* upnp */ \
|
||||
XX(24, MSEARCH, M-SEARCH) \
|
||||
XX(25, NOTIFY, NOTIFY) \
|
||||
XX(26, SUBSCRIBE, SUBSCRIBE) \
|
||||
XX(27, UNSUBSCRIBE, UNSUBSCRIBE) \
|
||||
/* RFC-5789 */ \
|
||||
XX(28, PATCH, PATCH) \
|
||||
XX(29, PURGE, PURGE) \
|
||||
/* CalDAV */ \
|
||||
XX(30, MKCALENDAR, MKCALENDAR) \
|
||||
/* RFC-2068, section 19.6.1.2 */ \
|
||||
XX(31, LINK, LINK) \
|
||||
XX(32, UNLINK, UNLINK) \
|
||||
|
||||
enum http_method
|
||||
{
|
||||
#define XX(num, name, string) HTTP_##name = num,
|
||||
HTTP_METHOD_MAP(XX)
|
||||
#undef XX
|
||||
};
|
||||
|
||||
|
||||
enum http_parser_type { HTTP_REQUEST, HTTP_RESPONSE, HTTP_BOTH };
|
||||
|
||||
|
||||
/* Flag values for http_parser.flags field */
|
||||
enum flags
|
||||
{ F_CHUNKED = 1 << 0
|
||||
, F_CONNECTION_KEEP_ALIVE = 1 << 1
|
||||
, F_CONNECTION_CLOSE = 1 << 2
|
||||
, F_CONNECTION_UPGRADE = 1 << 3
|
||||
, F_TRAILING = 1 << 4
|
||||
, F_UPGRADE = 1 << 5
|
||||
, F_SKIPBODY = 1 << 6
|
||||
, F_CONTENTLENGTH = 1 << 7
|
||||
};
|
||||
|
||||
|
||||
/* Map for errno-related constants
|
||||
*
|
||||
* The provided argument should be a macro that takes 2 arguments.
|
||||
*/
|
||||
#define HTTP_ERRNO_MAP(XX) \
|
||||
/* No error */ \
|
||||
XX(OK, "success") \
|
||||
\
|
||||
/* Callback-related errors */ \
|
||||
XX(CB_message_begin, "the on_message_begin callback failed") \
|
||||
XX(CB_url, "the on_url callback failed") \
|
||||
XX(CB_header_field, "the on_header_field callback failed") \
|
||||
XX(CB_header_value, "the on_header_value callback failed") \
|
||||
XX(CB_headers_complete, "the on_headers_complete callback failed") \
|
||||
XX(CB_body, "the on_body callback failed") \
|
||||
XX(CB_message_complete, "the on_message_complete callback failed") \
|
||||
XX(CB_status, "the on_status callback failed") \
|
||||
XX(CB_chunk_header, "the on_chunk_header callback failed") \
|
||||
XX(CB_chunk_complete, "the on_chunk_complete callback failed") \
|
||||
\
|
||||
/* Parsing-related errors */ \
|
||||
XX(INVALID_EOF_STATE, "stream ended at an unexpected time") \
|
||||
XX(HEADER_OVERFLOW, \
|
||||
"too many header bytes seen; overflow detected") \
|
||||
XX(CLOSED_CONNECTION, \
|
||||
"data received after completed connection: close message") \
|
||||
XX(INVALID_VERSION, "invalid HTTP version") \
|
||||
XX(INVALID_STATUS, "invalid HTTP status code") \
|
||||
XX(INVALID_METHOD, "invalid HTTP method") \
|
||||
XX(INVALID_URL, "invalid URL") \
|
||||
XX(INVALID_HOST, "invalid host") \
|
||||
XX(INVALID_PORT, "invalid port") \
|
||||
XX(INVALID_PATH, "invalid path") \
|
||||
XX(INVALID_QUERY_STRING, "invalid query string") \
|
||||
XX(INVALID_FRAGMENT, "invalid fragment") \
|
||||
XX(LF_EXPECTED, "LF character expected") \
|
||||
XX(INVALID_HEADER_TOKEN, "invalid character in header") \
|
||||
XX(INVALID_CONTENT_LENGTH, \
|
||||
"invalid character in content-length header") \
|
||||
XX(UNEXPECTED_CONTENT_LENGTH, \
|
||||
"unexpected content-length header") \
|
||||
XX(INVALID_CHUNK_SIZE, \
|
||||
"invalid character in chunk size header") \
|
||||
XX(INVALID_CONSTANT, "invalid constant string") \
|
||||
XX(INVALID_INTERNAL_STATE, "encountered unexpected internal state")\
|
||||
XX(STRICT, "strict mode assertion failed") \
|
||||
XX(PAUSED, "parser is paused") \
|
||||
XX(UNKNOWN, "an unknown error occurred")
|
||||
|
||||
|
||||
/* Define HPE_* values for each errno value above */
|
||||
#define HTTP_ERRNO_GEN(n, s) HPE_##n,
|
||||
enum http_errno {
|
||||
HTTP_ERRNO_MAP(HTTP_ERRNO_GEN)
|
||||
};
|
||||
#undef HTTP_ERRNO_GEN
|
||||
|
||||
|
||||
/* Get an http_errno value from an http_parser */
|
||||
#define HTTP_PARSER_ERRNO(p) ((enum http_errno) (p)->http_errno)
|
||||
|
||||
|
||||
struct http_parser {
|
||||
/** PRIVATE **/
|
||||
unsigned int type : 2; /* enum http_parser_type */
|
||||
unsigned int flags : 8; /* F_* values from 'flags' enum; semi-public */
|
||||
unsigned int state : 7; /* enum state from http_parser.c */
|
||||
unsigned int header_state : 7; /* enum header_state from http_parser.c */
|
||||
unsigned int index : 7; /* index into current matcher */
|
||||
unsigned int lenient_http_headers : 1;
|
||||
|
||||
uint32_t nread; /* # bytes read in various scenarios */
|
||||
uint64_t content_length; /* # bytes in body (0 if no Content-Length header) */
|
||||
|
||||
/** READ-ONLY **/
|
||||
unsigned short http_major;
|
||||
unsigned short http_minor;
|
||||
unsigned int status_code : 16; /* responses only */
|
||||
unsigned int method : 8; /* requests only */
|
||||
unsigned int http_errno : 7;
|
||||
|
||||
/* 1 = Upgrade header was present and the parser has exited because of that.
|
||||
* 0 = No upgrade header present.
|
||||
* Should be checked when http_parser_execute() returns in addition to
|
||||
* error checking.
|
||||
*/
|
||||
unsigned int upgrade : 1;
|
||||
|
||||
/** PUBLIC **/
|
||||
void *data; /* A pointer to get hook to the "connection" or "socket" object */
|
||||
};
|
||||
|
||||
|
||||
struct http_parser_settings {
|
||||
http_cb on_message_begin;
|
||||
http_data_cb on_url;
|
||||
http_data_cb on_status;
|
||||
http_data_cb on_header_field;
|
||||
http_data_cb on_header_value;
|
||||
http_cb on_headers_complete;
|
||||
http_data_cb on_body;
|
||||
http_cb on_message_complete;
|
||||
/* When on_chunk_header is called, the current chunk length is stored
|
||||
* in parser->content_length.
|
||||
*/
|
||||
http_cb on_chunk_header;
|
||||
http_cb on_chunk_complete;
|
||||
};
|
||||
|
||||
|
||||
enum http_parser_url_fields
|
||||
{ UF_SCHEMA = 0
|
||||
, UF_HOST = 1
|
||||
, UF_PORT = 2
|
||||
, UF_PATH = 3
|
||||
, UF_QUERY = 4
|
||||
, UF_FRAGMENT = 5
|
||||
, UF_USERINFO = 6
|
||||
, UF_MAX = 7
|
||||
};
|
||||
|
||||
|
||||
/* Result structure for http_parser_parse_url().
|
||||
*
|
||||
* Callers should index into field_data[] with UF_* values iff field_set
|
||||
* has the relevant (1 << UF_*) bit set. As a courtesy to clients (and
|
||||
* because we probably have padding left over), we convert any port to
|
||||
* a uint16_t.
|
||||
*/
|
||||
struct http_parser_url {
|
||||
uint16_t field_set; /* Bitmask of (1 << UF_*) values */
|
||||
uint16_t port; /* Converted UF_PORT string */
|
||||
|
||||
struct {
|
||||
uint16_t off; /* Offset into buffer in which field starts */
|
||||
uint16_t len; /* Length of run in buffer */
|
||||
} field_data[UF_MAX];
|
||||
};
|
||||
|
||||
|
||||
/* Returns the library version. Bits 16-23 contain the major version number,
|
||||
* bits 8-15 the minor version number and bits 0-7 the patch level.
|
||||
* Usage example:
|
||||
*
|
||||
* unsigned long version = http_parser_version();
|
||||
* unsigned major = (version >> 16) & 255;
|
||||
* unsigned minor = (version >> 8) & 255;
|
||||
* unsigned patch = version & 255;
|
||||
* printf("http_parser v%u.%u.%u\n", major, minor, patch);
|
||||
*/
|
||||
unsigned long http_parser_version(void);
|
||||
|
||||
void http_parser_init(http_parser *parser, enum http_parser_type type);
|
||||
|
||||
|
||||
/* Initialize http_parser_settings members to 0
|
||||
*/
|
||||
void http_parser_settings_init(http_parser_settings *settings);
|
||||
|
||||
|
||||
/* Executes the parser. Returns number of parsed bytes. Sets
|
||||
* `parser->http_errno` on error. */
|
||||
size_t http_parser_execute(http_parser *parser,
|
||||
const http_parser_settings *settings,
|
||||
const char *data,
|
||||
size_t len);
|
||||
|
||||
|
||||
/* If http_should_keep_alive() in the on_headers_complete or
|
||||
* on_message_complete callback returns 0, then this should be
|
||||
* the last message on the connection.
|
||||
* If you are the server, respond with the "Connection: close" header.
|
||||
* If you are the client, close the connection.
|
||||
*/
|
||||
int http_should_keep_alive(const http_parser *parser);
|
||||
|
||||
/* Returns a string version of the HTTP method. */
|
||||
const char *http_method_str(enum http_method m);
|
||||
|
||||
/* Return a string name of the given error */
|
||||
const char *http_errno_name(enum http_errno err);
|
||||
|
||||
/* Return a string description of the given error */
|
||||
const char *http_errno_description(enum http_errno err);
|
||||
|
||||
/* Initialize all http_parser_url members to 0 */
|
||||
void http_parser_url_init(struct http_parser_url *u);
|
||||
|
||||
/* Parse a URL; return nonzero on failure */
|
||||
int http_parser_parse_url(const char *buf, size_t buflen,
|
||||
int is_connect,
|
||||
struct http_parser_url *u);
|
||||
|
||||
/* Pause or un-pause the parser; a nonzero value pauses */
|
||||
void http_parser_pause(http_parser *parser, int paused);
|
||||
|
||||
/* Checks if this is the final chunk of the body. */
|
||||
int http_body_is_final(const http_parser *parser);
|
||||
|
||||
#ifdef __cplusplus
|
||||
}
|
||||
#endif
|
||||
#endif
|
||||
@@ -1,17 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning (push)
|
||||
# pragma warning (disable: 4127) // conditional expression is constant
|
||||
# pragma warning (disable: 4244) // integer conversion, possible loss of data
|
||||
#endif
|
||||
#include "nodejs-parser/http_parser.c"
|
||||
#ifdef _MSC_VER
|
||||
# pragma warning (pop)
|
||||
#endif
|
||||
|
||||
@@ -1,870 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
#ifndef BEAST_HTTP_NODEJS_PARSER_HPP
|
||||
#define BEAST_HTTP_NODEJS_PARSER_HPP
|
||||
|
||||
#include "nodejs-parser/http_parser.h"
|
||||
|
||||
#include <beast/http/message.hpp>
|
||||
#include <beast/http/rfc7230.hpp>
|
||||
#include <beast/core/buffer_concepts.hpp>
|
||||
#include <beast/core/error.hpp>
|
||||
#include <boost/asio/buffer.hpp>
|
||||
#include <boost/system/error_code.hpp>
|
||||
#include <cstdint>
|
||||
#include <string>
|
||||
#include <type_traits>
|
||||
#include <utility>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
namespace detail {
|
||||
|
||||
class nodejs_message_category
|
||||
: public boost::system::error_category
|
||||
{
|
||||
public:
|
||||
const char*
|
||||
name() const noexcept override
|
||||
{
|
||||
return "nodejs-http-error";
|
||||
}
|
||||
|
||||
std::string
|
||||
message(int ev) const override
|
||||
{
|
||||
return http_errno_description(
|
||||
static_cast<http_errno>(ev));
|
||||
}
|
||||
|
||||
boost::system::error_condition
|
||||
default_error_condition(int ev) const noexcept override
|
||||
{
|
||||
return boost::system::error_condition{ev, *this};
|
||||
}
|
||||
|
||||
bool
|
||||
equivalent(int ev,
|
||||
boost::system::error_condition const& condition
|
||||
) const noexcept override
|
||||
{
|
||||
return condition.value() == ev &&
|
||||
&condition.category() == this;
|
||||
}
|
||||
|
||||
bool
|
||||
equivalent(boost::system::error_code const& error,
|
||||
int ev) const noexcept override
|
||||
{
|
||||
return error.value() == ev &&
|
||||
&error.category() == this;
|
||||
}
|
||||
};
|
||||
|
||||
template<class = void>
|
||||
boost::system::error_code
|
||||
make_nodejs_error(int http_errno)
|
||||
{
|
||||
static nodejs_message_category const mc{};
|
||||
return boost::system::error_code{http_errno, mc};
|
||||
}
|
||||
|
||||
inline
|
||||
char const*
|
||||
method_to_string(unsigned method)
|
||||
{
|
||||
using namespace beast;
|
||||
switch(static_cast<http_method>(method))
|
||||
{
|
||||
case HTTP_DELETE: return "DELETE";
|
||||
case HTTP_GET: return "GET";
|
||||
case HTTP_HEAD: return "HEAD";
|
||||
case HTTP_POST: return "POST";
|
||||
case HTTP_PUT: return "PUT";
|
||||
|
||||
// pathological
|
||||
case HTTP_CONNECT: return "CONNECT";
|
||||
case HTTP_OPTIONS: return "OPTIONS";
|
||||
case HTTP_TRACE: return "TRACE";
|
||||
|
||||
// webdav
|
||||
case HTTP_COPY: return "COPY";
|
||||
case HTTP_LOCK: return "LOCK";
|
||||
case HTTP_MKCOL: return "MKCOL";
|
||||
case HTTP_MOVE: return "MOVE";
|
||||
case HTTP_PROPFIND: return "PROPFIND";
|
||||
case HTTP_PROPPATCH: return "PROPPATCH";
|
||||
case HTTP_SEARCH: return "SEARCH";
|
||||
case HTTP_UNLOCK: return "UNLOCK";
|
||||
case HTTP_BIND: return "BIND";
|
||||
case HTTP_REBIND: return "REBIND";
|
||||
case HTTP_UNBIND: return "UNBIND";
|
||||
case HTTP_ACL: return "ACL";
|
||||
|
||||
// subversion
|
||||
case HTTP_REPORT: return "REPORT";
|
||||
case HTTP_MKACTIVITY: return "MKACTIVITY";
|
||||
case HTTP_CHECKOUT: return "CHECKOUT";
|
||||
case HTTP_MERGE: return "MERGE";
|
||||
|
||||
// upnp
|
||||
case HTTP_MSEARCH: return "MSEARCH";
|
||||
case HTTP_NOTIFY: return "NOTIFY";
|
||||
case HTTP_SUBSCRIBE: return "SUBSCRIBE";
|
||||
case HTTP_UNSUBSCRIBE: return "UNSUBSCRIBE";
|
||||
|
||||
// RFC-5789
|
||||
case HTTP_PATCH: return "PATCH";
|
||||
case HTTP_PURGE: return "PURGE";
|
||||
|
||||
// CalDav
|
||||
case HTTP_MKCALENDAR: return "MKCALENDAR";
|
||||
|
||||
// RFC-2068, section 19.6.1.2
|
||||
case HTTP_LINK: return "LINK";
|
||||
case HTTP_UNLINK: return "UNLINK";
|
||||
};
|
||||
|
||||
return "<unknown>";
|
||||
}
|
||||
|
||||
} // detail
|
||||
|
||||
template<class Derived>
|
||||
class nodejs_basic_parser
|
||||
{
|
||||
http_parser state_;
|
||||
boost::system::error_code* ec_;
|
||||
bool complete_ = false;
|
||||
std::string url_;
|
||||
std::string status_;
|
||||
std::string field_;
|
||||
std::string value_;
|
||||
|
||||
public:
|
||||
using error_code = boost::system::error_code;
|
||||
|
||||
nodejs_basic_parser(nodejs_basic_parser&& other);
|
||||
|
||||
nodejs_basic_parser&
|
||||
operator=(nodejs_basic_parser&& other);
|
||||
|
||||
nodejs_basic_parser(nodejs_basic_parser const& other);
|
||||
|
||||
nodejs_basic_parser& operator=(nodejs_basic_parser const& other);
|
||||
|
||||
explicit
|
||||
nodejs_basic_parser(bool request) noexcept;
|
||||
|
||||
bool
|
||||
complete() const noexcept
|
||||
{
|
||||
return complete_;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
write(void const* data, std::size_t size)
|
||||
{
|
||||
error_code ec;
|
||||
auto const used = write(data, size, ec);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
return used;
|
||||
}
|
||||
|
||||
std::size_t
|
||||
write(void const* data, std::size_t size,
|
||||
error_code& ec);
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t
|
||||
write(ConstBufferSequence const& buffers)
|
||||
{
|
||||
error_code ec;
|
||||
auto const used = write(buffers, ec);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
return used;
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t
|
||||
write(ConstBufferSequence const& buffers,
|
||||
error_code& ec);
|
||||
|
||||
void
|
||||
write_eof()
|
||||
{
|
||||
error_code ec;
|
||||
write_eof(ec);
|
||||
if(ec)
|
||||
throw system_error{ec};
|
||||
}
|
||||
|
||||
void
|
||||
write_eof(error_code& ec);
|
||||
|
||||
private:
|
||||
Derived&
|
||||
impl()
|
||||
{
|
||||
return *static_cast<Derived*>(this);
|
||||
}
|
||||
|
||||
template<class C>
|
||||
class has_on_start_t
|
||||
{
|
||||
template<class T, class R =
|
||||
decltype(std::declval<T>().on_start(), std::true_type{})>
|
||||
static R check(int);
|
||||
template<class>
|
||||
static std::false_type check(...);
|
||||
using type = decltype(check<C>(0));
|
||||
public:
|
||||
static bool const value = type::value;
|
||||
};
|
||||
template<class C>
|
||||
using has_on_start =
|
||||
std::integral_constant<bool, has_on_start_t<C>::value>;
|
||||
|
||||
void
|
||||
call_on_start(std::true_type)
|
||||
{
|
||||
impl().on_start();
|
||||
}
|
||||
|
||||
void
|
||||
call_on_start(std::false_type)
|
||||
{
|
||||
}
|
||||
|
||||
template<class C>
|
||||
class has_on_field_t
|
||||
{
|
||||
template<class T, class R =
|
||||
decltype(std::declval<T>().on_field(
|
||||
std::declval<std::string const&>(),
|
||||
std::declval<std::string const&>()),
|
||||
std::true_type{})>
|
||||
static R check(int);
|
||||
template<class>
|
||||
static std::false_type check(...);
|
||||
using type = decltype(check<C>(0));
|
||||
public:
|
||||
static bool const value = type::value;
|
||||
};
|
||||
template<class C>
|
||||
using has_on_field =
|
||||
std::integral_constant<bool, has_on_field_t<C>::value>;
|
||||
|
||||
void
|
||||
call_on_field(std::string const& field,
|
||||
std::string const& value, std::true_type)
|
||||
{
|
||||
impl().on_field(field, value);
|
||||
}
|
||||
|
||||
void
|
||||
call_on_field(std::string const&, std::string const&,
|
||||
std::false_type)
|
||||
{
|
||||
}
|
||||
|
||||
template<class C>
|
||||
class has_on_headers_complete_t
|
||||
{
|
||||
template<class T, class R =
|
||||
decltype(std::declval<T>().on_headers_complete(
|
||||
std::declval<error_code&>()), std::true_type{})>
|
||||
static R check(int);
|
||||
template<class>
|
||||
static std::false_type check(...);
|
||||
using type = decltype(check<C>(0));
|
||||
public:
|
||||
static bool const value = type::value;
|
||||
};
|
||||
template<class C>
|
||||
using has_on_headers_complete =
|
||||
std::integral_constant<bool, has_on_headers_complete_t<C>::value>;
|
||||
|
||||
void
|
||||
call_on_headers_complete(error_code& ec, std::true_type)
|
||||
{
|
||||
impl().on_headers_complete(ec);
|
||||
}
|
||||
|
||||
void
|
||||
call_on_headers_complete(error_code&, std::false_type)
|
||||
{
|
||||
}
|
||||
|
||||
template<class C>
|
||||
class has_on_request_t
|
||||
{
|
||||
template<class T, class R =
|
||||
decltype(std::declval<T>().on_request(
|
||||
std::declval<unsigned>(), std::declval<std::string>(),
|
||||
std::declval<int>(), std::declval<int>(),
|
||||
std::declval<bool>(), std::declval<bool>()),
|
||||
std::true_type{})>
|
||||
static R check(int);
|
||||
template<class>
|
||||
static std::false_type check(...);
|
||||
using type = decltype(check<C>(0));
|
||||
public:
|
||||
static bool const value = type::value;
|
||||
};
|
||||
template<class C>
|
||||
using has_on_request =
|
||||
std::integral_constant<bool, has_on_request_t<C>::value>;
|
||||
|
||||
void
|
||||
call_on_request(unsigned method, std::string url,
|
||||
int major, int minor, bool keep_alive, bool upgrade,
|
||||
std::true_type)
|
||||
{
|
||||
impl().on_request(
|
||||
method, url, major, minor, keep_alive, upgrade);
|
||||
}
|
||||
|
||||
void
|
||||
call_on_request(unsigned, std::string, int, int, bool, bool,
|
||||
std::false_type)
|
||||
{
|
||||
}
|
||||
|
||||
template<class C>
|
||||
class has_on_response_t
|
||||
{
|
||||
template<class T, class R =
|
||||
decltype(std::declval<T>().on_response(
|
||||
std::declval<int>(), std::declval<std::string>,
|
||||
std::declval<int>(), std::declval<int>(),
|
||||
std::declval<bool>(), std::declval<bool>()),
|
||||
std::true_type{})>
|
||||
static R check(int);
|
||||
template<class>
|
||||
static std::false_type check(...);
|
||||
#if 0
|
||||
using type = decltype(check<C>(0));
|
||||
#else
|
||||
// VFALCO Trait seems broken for http::parser
|
||||
using type = std::true_type;
|
||||
#endif
|
||||
public:
|
||||
static bool const value = type::value;
|
||||
};
|
||||
template<class C>
|
||||
using has_on_response =
|
||||
std::integral_constant<bool, has_on_response_t<C>::value>;
|
||||
|
||||
bool
|
||||
call_on_response(int status, std::string text,
|
||||
int major, int minor, bool keep_alive, bool upgrade,
|
||||
std::true_type)
|
||||
{
|
||||
return impl().on_response(
|
||||
status, text, major, minor, keep_alive, upgrade);
|
||||
}
|
||||
|
||||
bool
|
||||
call_on_response(int, std::string, int, int, bool, bool,
|
||||
std::false_type)
|
||||
{
|
||||
// VFALCO Certainly incorrect
|
||||
return true;
|
||||
}
|
||||
|
||||
template<class C>
|
||||
class has_on_body_t
|
||||
{
|
||||
template<class T, class R =
|
||||
decltype(std::declval<T>().on_body(
|
||||
std::declval<void const*>(), std::declval<std::size_t>(),
|
||||
std::declval<error_code&>()), std::true_type{})>
|
||||
static R check(int);
|
||||
template<class>
|
||||
static std::false_type check(...);
|
||||
using type = decltype(check<C>(0));
|
||||
public:
|
||||
static bool const value = type::value;
|
||||
};
|
||||
template<class C>
|
||||
using has_on_body =
|
||||
std::integral_constant<bool, has_on_body_t<C>::value>;
|
||||
|
||||
void
|
||||
call_on_body(void const* data, std::size_t bytes,
|
||||
error_code& ec, std::true_type)
|
||||
{
|
||||
impl().on_body(data, bytes, ec);
|
||||
}
|
||||
|
||||
void
|
||||
call_on_body(void const*, std::size_t,
|
||||
error_code&, std::false_type)
|
||||
{
|
||||
}
|
||||
|
||||
template<class C>
|
||||
class has_on_complete_t
|
||||
{
|
||||
template<class T, class R =
|
||||
decltype(std::declval<T>().on_complete(), std::true_type{})>
|
||||
static R check(int);
|
||||
template<class>
|
||||
static std::false_type check(...);
|
||||
using type = decltype(check<C>(0));
|
||||
public:
|
||||
static bool const value = type::value;
|
||||
};
|
||||
template<class C>
|
||||
using has_on_complete =
|
||||
std::integral_constant<bool, has_on_complete_t<C>::value>;
|
||||
|
||||
void
|
||||
call_on_complete(std::true_type)
|
||||
{
|
||||
impl().on_complete();
|
||||
}
|
||||
|
||||
void
|
||||
call_on_complete(std::false_type)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
check_header();
|
||||
|
||||
static int cb_message_start(http_parser*);
|
||||
static int cb_url(http_parser*, char const*, std::size_t);
|
||||
static int cb_status(http_parser*, char const*, std::size_t);
|
||||
static int cb_header_field(http_parser*, char const*, std::size_t);
|
||||
static int cb_header_value(http_parser*, char const*, std::size_t);
|
||||
static int cb_headers_complete(http_parser*);
|
||||
static int cb_body(http_parser*, char const*, std::size_t);
|
||||
static int cb_message_complete(http_parser*);
|
||||
static int cb_chunk_header(http_parser*);
|
||||
static int cb_chunk_complete(http_parser*);
|
||||
|
||||
struct hooks_t : http_parser_settings
|
||||
{
|
||||
hooks_t()
|
||||
{
|
||||
http_parser_settings_init(this);
|
||||
on_message_begin = &nodejs_basic_parser::cb_message_start;
|
||||
on_url = &nodejs_basic_parser::cb_url;
|
||||
on_status = &nodejs_basic_parser::cb_status;
|
||||
on_header_field = &nodejs_basic_parser::cb_header_field;
|
||||
on_header_value = &nodejs_basic_parser::cb_header_value;
|
||||
on_headers_complete = &nodejs_basic_parser::cb_headers_complete;
|
||||
on_body = &nodejs_basic_parser::cb_body;
|
||||
on_message_complete = &nodejs_basic_parser::cb_message_complete;
|
||||
on_chunk_header = &nodejs_basic_parser::cb_chunk_header;
|
||||
on_chunk_complete = &nodejs_basic_parser::cb_chunk_complete;
|
||||
}
|
||||
};
|
||||
|
||||
static
|
||||
http_parser_settings const*
|
||||
hooks();
|
||||
};
|
||||
|
||||
template<class Derived>
|
||||
template<class ConstBufferSequence>
|
||||
std::size_t
|
||||
nodejs_basic_parser<Derived>::write(
|
||||
ConstBufferSequence const& buffers, error_code& ec)
|
||||
{
|
||||
static_assert(beast::is_ConstBufferSequence<
|
||||
ConstBufferSequence>::value,
|
||||
"ConstBufferSequence requirements not met");
|
||||
using boost::asio::buffer_cast;
|
||||
using boost::asio::buffer_size;
|
||||
std::size_t bytes_used = 0;
|
||||
for(auto const& buffer : buffers)
|
||||
{
|
||||
auto const n = write(
|
||||
buffer_cast<void const*>(buffer),
|
||||
buffer_size(buffer), ec);
|
||||
if(ec)
|
||||
return 0;
|
||||
bytes_used += n;
|
||||
if(complete())
|
||||
break;
|
||||
}
|
||||
return bytes_used;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
http_parser_settings const*
|
||||
nodejs_basic_parser<Derived>::hooks()
|
||||
{
|
||||
static hooks_t const h;
|
||||
return &h;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
nodejs_basic_parser<Derived>::
|
||||
nodejs_basic_parser(nodejs_basic_parser&& other)
|
||||
{
|
||||
state_ = other.state_;
|
||||
state_.data = this;
|
||||
complete_ = other.complete_;
|
||||
url_ = std::move(other.url_);
|
||||
status_ = std::move(other.status_);
|
||||
field_ = std::move(other.field_);
|
||||
value_ = std::move(other.value_);
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
auto
|
||||
nodejs_basic_parser<Derived>::operator=(nodejs_basic_parser&& other) ->
|
||||
nodejs_basic_parser&
|
||||
{
|
||||
state_ = other.state_;
|
||||
state_.data = this;
|
||||
complete_ = other.complete_;
|
||||
url_ = std::move(other.url_);
|
||||
status_ = std::move(other.status_);
|
||||
field_ = std::move(other.field_);
|
||||
value_ = std::move(other.value_);
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
nodejs_basic_parser<Derived>::
|
||||
nodejs_basic_parser(nodejs_basic_parser const& other)
|
||||
{
|
||||
state_ = other.state_;
|
||||
state_.data = this;
|
||||
complete_ = other.complete_;
|
||||
url_ = other.url_;
|
||||
status_ = other.status_;
|
||||
field_ = other.field_;
|
||||
value_ = other.value_;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
auto
|
||||
nodejs_basic_parser<Derived>::
|
||||
operator=(nodejs_basic_parser const& other) ->
|
||||
nodejs_basic_parser&
|
||||
{
|
||||
state_ = other.state_;
|
||||
state_.data = this;
|
||||
complete_ = other.complete_;
|
||||
url_ = other.url_;
|
||||
status_ = other.status_;
|
||||
field_ = other.field_;
|
||||
value_ = other.value_;
|
||||
return *this;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
nodejs_basic_parser<Derived>::nodejs_basic_parser(bool request) noexcept
|
||||
{
|
||||
state_.data = this;
|
||||
http_parser_init(&state_, request
|
||||
? http_parser_type::HTTP_REQUEST
|
||||
: http_parser_type::HTTP_RESPONSE);
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
std::size_t
|
||||
nodejs_basic_parser<Derived>::write(void const* data,
|
||||
std::size_t size, error_code& ec)
|
||||
{
|
||||
ec_ = &ec;
|
||||
auto const n = http_parser_execute(
|
||||
&state_, hooks(),
|
||||
static_cast<const char*>(data), size);
|
||||
if(! ec)
|
||||
ec = detail::make_nodejs_error(
|
||||
static_cast<int>(state_.http_errno));
|
||||
if(ec)
|
||||
return 0;
|
||||
return n;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
void
|
||||
nodejs_basic_parser<Derived>::write_eof(error_code& ec)
|
||||
{
|
||||
ec_ = &ec;
|
||||
http_parser_execute(
|
||||
&state_, hooks(), nullptr, 0);
|
||||
if(! ec)
|
||||
ec = detail::make_nodejs_error(
|
||||
static_cast<int>(state_.http_errno));
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
void
|
||||
nodejs_basic_parser<Derived>::check_header()
|
||||
{
|
||||
if(! value_.empty())
|
||||
{
|
||||
//detail::trim(value_);
|
||||
call_on_field(field_, value_,
|
||||
has_on_field<Derived>{});
|
||||
field_.clear();
|
||||
value_.clear();
|
||||
}
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_message_start(http_parser* p)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.complete_ = false;
|
||||
t.url_.clear();
|
||||
t.status_.clear();
|
||||
t.field_.clear();
|
||||
t.value_.clear();
|
||||
t.call_on_start(has_on_start<Derived>{});
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_url(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.url_.append(in, bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_status(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.status_.append(in, bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_header_field(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.check_header();
|
||||
t.field_.append(in, bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_header_value(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.value_.append(in, bytes);
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_headers_complete(http_parser* p)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.check_header();
|
||||
t.call_on_headers_complete(*t.ec_,
|
||||
has_on_headers_complete<Derived>{});
|
||||
if(*t.ec_)
|
||||
return 1;
|
||||
bool const keep_alive =
|
||||
http_should_keep_alive(p) != 0;
|
||||
if(p->type == http_parser_type::HTTP_REQUEST)
|
||||
{
|
||||
t.call_on_request(p->method, t.url_,
|
||||
p->http_major, p->http_minor, keep_alive,
|
||||
p->upgrade, has_on_request<Derived>{});
|
||||
return 0;
|
||||
}
|
||||
return t.call_on_response(p->status_code, t.status_,
|
||||
p->http_major, p->http_minor, keep_alive,
|
||||
p->upgrade, has_on_response<Derived>{}) ? 0 : 1;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_body(http_parser* p,
|
||||
char const* in, std::size_t bytes)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.call_on_body(in, bytes, *t.ec_, has_on_body<Derived>{});
|
||||
return *t.ec_ ? 1 : 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_message_complete(http_parser* p)
|
||||
{
|
||||
auto& t = *reinterpret_cast<nodejs_basic_parser*>(p->data);
|
||||
t.complete_ = true;
|
||||
t.call_on_complete(has_on_complete<Derived>{});
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_chunk_header(http_parser*)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
template<class Derived>
|
||||
int
|
||||
nodejs_basic_parser<Derived>::cb_chunk_complete(http_parser*)
|
||||
{
|
||||
return 0;
|
||||
}
|
||||
|
||||
/** A HTTP parser.
|
||||
|
||||
The parser may only be used once.
|
||||
*/
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
class nodejs_parser
|
||||
: public nodejs_basic_parser<nodejs_parser<isRequest, Body, Fields>>
|
||||
{
|
||||
using message_type =
|
||||
message<isRequest, Body, Fields>;
|
||||
|
||||
message_type m_;
|
||||
typename message_type::body_type::reader r_;
|
||||
bool started_ = false;
|
||||
|
||||
public:
|
||||
nodejs_parser(nodejs_parser&&) = default;
|
||||
|
||||
nodejs_parser()
|
||||
: http::nodejs_basic_parser<nodejs_parser>(isRequest)
|
||||
, r_(m_)
|
||||
{
|
||||
}
|
||||
|
||||
/// Returns `true` if at least one byte has been processed
|
||||
bool
|
||||
started()
|
||||
{
|
||||
return started_;
|
||||
}
|
||||
|
||||
message_type
|
||||
release()
|
||||
{
|
||||
return std::move(m_);
|
||||
}
|
||||
|
||||
private:
|
||||
friend class http::nodejs_basic_parser<nodejs_parser>;
|
||||
|
||||
void
|
||||
on_start()
|
||||
{
|
||||
started_ = true;
|
||||
}
|
||||
|
||||
void
|
||||
on_field(std::string const& field, std::string const& value)
|
||||
{
|
||||
m_.fields.insert(field, value);
|
||||
}
|
||||
|
||||
void
|
||||
on_headers_complete(error_code&)
|
||||
{
|
||||
// vFALCO TODO Decode the Content-Length and
|
||||
// Transfer-Encoding, see if we can reserve the buffer.
|
||||
//
|
||||
// r_.reserve(content_length)
|
||||
}
|
||||
|
||||
bool
|
||||
on_request(unsigned method, std::string const& url,
|
||||
int major, int minor, bool /*keep_alive*/, bool /*upgrade*/,
|
||||
std::true_type)
|
||||
{
|
||||
m_.method = detail::method_to_string(method);
|
||||
m_.url = url;
|
||||
m_.version = major * 10 + minor;
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
on_request(unsigned, std::string const&,
|
||||
int, int, bool, bool,
|
||||
std::false_type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
on_request(unsigned method, std::string const& url,
|
||||
int major, int minor, bool keep_alive, bool upgrade)
|
||||
{
|
||||
return on_request(method, url,
|
||||
major, minor, keep_alive, upgrade,
|
||||
std::integral_constant<
|
||||
bool, message_type::is_request>{});
|
||||
}
|
||||
|
||||
bool
|
||||
on_response(int status, std::string const& reason,
|
||||
int major, int minor, bool keep_alive, bool upgrade,
|
||||
std::true_type)
|
||||
{
|
||||
beast::detail::ignore_unused(keep_alive, upgrade);
|
||||
m_.status = status;
|
||||
m_.reason = reason;
|
||||
m_.version = major * 10 + minor;
|
||||
// VFALCO TODO return expect_body_
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
on_response(int, std::string const&, int, int, bool, bool,
|
||||
std::false_type)
|
||||
{
|
||||
return true;
|
||||
}
|
||||
|
||||
bool
|
||||
on_response(int status, std::string const& reason,
|
||||
int major, int minor, bool keep_alive, bool upgrade)
|
||||
{
|
||||
return on_response(
|
||||
status, reason, major, minor, keep_alive, upgrade,
|
||||
std::integral_constant<bool, ! message_type::is_request>{});
|
||||
}
|
||||
|
||||
void
|
||||
on_body(void const* data,
|
||||
std::size_t size, error_code& ec)
|
||||
{
|
||||
r_.write(data, size, ec);
|
||||
}
|
||||
|
||||
void
|
||||
on_complete()
|
||||
{
|
||||
}
|
||||
};
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
#endif
|
||||
@@ -1,9 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/parse.hpp>
|
||||
@@ -1,60 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/parse_error.hpp>
|
||||
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <memory>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class parse_error_test : public unit_test::suite
|
||||
{
|
||||
public:
|
||||
void check(char const* name, parse_error ev)
|
||||
{
|
||||
auto const ec = make_error_code(ev);
|
||||
BEAST_EXPECT(std::string{ec.category().name()} == name);
|
||||
BEAST_EXPECT(! ec.message().empty());
|
||||
BEAST_EXPECT(std::addressof(ec.category()) ==
|
||||
std::addressof(detail::get_parse_error_category()));
|
||||
BEAST_EXPECT(detail::get_parse_error_category().equivalent(
|
||||
static_cast<std::underlying_type<parse_error>::type>(ev),
|
||||
ec.category().default_error_condition(
|
||||
static_cast<std::underlying_type<parse_error>::type>(ev))));
|
||||
BEAST_EXPECT(detail::get_parse_error_category().equivalent(
|
||||
ec, static_cast<std::underlying_type<parse_error>::type>(ev)));
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
check("http", parse_error::connection_closed);
|
||||
check("http", parse_error::bad_method);
|
||||
check("http", parse_error::bad_uri);
|
||||
check("http", parse_error::bad_version);
|
||||
check("http", parse_error::bad_crlf);
|
||||
check("http", parse_error::bad_status);
|
||||
check("http", parse_error::bad_reason);
|
||||
check("http", parse_error::bad_field);
|
||||
check("http", parse_error::bad_value);
|
||||
check("http", parse_error::bad_content_length);
|
||||
check("http", parse_error::illegal_content_length);
|
||||
check("http", parse_error::invalid_chunk_size);
|
||||
check("http", parse_error::invalid_ext_name);
|
||||
check("http", parse_error::invalid_ext_val);
|
||||
check("http", parse_error::header_too_big);
|
||||
check("http", parse_error::body_too_big);
|
||||
check("http", parse_error::short_read);
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(parse_error,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
389
test/http/parser.cpp
Normal file
389
test/http/parser.cpp
Normal file
@@ -0,0 +1,389 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/parser.hpp>
|
||||
|
||||
#include "test_parser.hpp"
|
||||
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <beast/test/string_istream.hpp>
|
||||
#include <beast/test/string_ostream.hpp>
|
||||
#include <beast/test/yield_to.hpp>
|
||||
#include <beast/core/consuming_buffers.hpp>
|
||||
#include <beast/core/flat_buffer.hpp>
|
||||
#include <beast/core/multi_buffer.hpp>
|
||||
#include <beast/core/ostream.hpp>
|
||||
#include <beast/http/read.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <boost/system/system_error.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class parser_test
|
||||
: public beast::unit_test::suite
|
||||
, public beast::test::enable_yield_to
|
||||
{
|
||||
public:
|
||||
template<bool isRequest>
|
||||
using parser_type =
|
||||
parser<isRequest, string_body>;
|
||||
|
||||
static
|
||||
boost::asio::const_buffers_1
|
||||
buf(string_view s)
|
||||
{
|
||||
return {s.data(), s.size()};
|
||||
}
|
||||
|
||||
template<class ConstBufferSequence,
|
||||
bool isRequest, class Derived>
|
||||
static
|
||||
void
|
||||
put(ConstBufferSequence const& buffers,
|
||||
basic_parser<isRequest, Derived>& p,
|
||||
error_code& ec)
|
||||
{
|
||||
using boost::asio::buffer_size;
|
||||
consuming_buffers<ConstBufferSequence> cb{buffers};
|
||||
for(;;)
|
||||
{
|
||||
auto const used = p.put(cb, ec);
|
||||
cb.consume(used);
|
||||
if(ec)
|
||||
return;
|
||||
if(p.need_eof() &&
|
||||
buffer_size(cb) == 0)
|
||||
{
|
||||
p.put_eof(ec);
|
||||
if(ec)
|
||||
return;
|
||||
}
|
||||
if(p.is_done())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template<bool isRequest, class F>
|
||||
void
|
||||
doMatrix(string_view s0, F const& f)
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
// parse a single buffer
|
||||
{
|
||||
auto s = s0;
|
||||
error_code ec;
|
||||
parser_type<isRequest> p;
|
||||
put(buffer(s.data(), s.size()), p, ec);
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
return;
|
||||
f(p);
|
||||
}
|
||||
// parse two buffers
|
||||
for(auto n = s0.size() - 1; n >= 1; --n)
|
||||
{
|
||||
auto s = s0;
|
||||
error_code ec;
|
||||
parser_type<isRequest> p;
|
||||
p.eager(true);
|
||||
auto used =
|
||||
p.put(buffer(s.data(), n), ec);
|
||||
s.remove_prefix(used);
|
||||
if(ec == error::need_more)
|
||||
ec.assign(0, ec.category());
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
continue;
|
||||
BEAST_EXPECT(! p.is_done());
|
||||
used = p.put(
|
||||
buffer(s.data(), s.size()), ec);
|
||||
s.remove_prefix(used);
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
continue;
|
||||
BEAST_EXPECT(s.empty());
|
||||
if(p.need_eof())
|
||||
{
|
||||
p.put_eof(ec);
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
continue;
|
||||
}
|
||||
if(BEAST_EXPECT(p.is_done()))
|
||||
f(p);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testParse()
|
||||
{
|
||||
doMatrix<false>(
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"\r\n"
|
||||
"Hello, world!",
|
||||
[&](parser_type<false> const& p)
|
||||
{
|
||||
auto const& m = p.get();
|
||||
BEAST_EXPECT(! p.is_chunked());
|
||||
BEAST_EXPECT(p.need_eof());
|
||||
BEAST_EXPECT(p.content_length() == boost::none);
|
||||
BEAST_EXPECT(m.version == 10);
|
||||
BEAST_EXPECT(m.result() == status::ok);
|
||||
BEAST_EXPECT(m.reason() == "OK");
|
||||
BEAST_EXPECT(m["Server"] == "test");
|
||||
BEAST_EXPECT(m.body == "Hello, world!");
|
||||
}
|
||||
);
|
||||
doMatrix<false>(
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Expect: Expires, MD5-Fingerprint\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"5\r\n"
|
||||
"*****\r\n"
|
||||
"2;a;b=1;c=\"2\"\r\n"
|
||||
"--\r\n"
|
||||
"0;d;e=3;f=\"4\"\r\n"
|
||||
"Expires: never\r\n"
|
||||
"MD5-Fingerprint: -\r\n"
|
||||
"\r\n",
|
||||
[&](parser_type<false> const& p)
|
||||
{
|
||||
auto const& m = p.get();
|
||||
BEAST_EXPECT(! p.need_eof());
|
||||
BEAST_EXPECT(p.is_chunked());
|
||||
BEAST_EXPECT(p.content_length() == boost::none);
|
||||
BEAST_EXPECT(m.version == 11);
|
||||
BEAST_EXPECT(m.result() == status::ok);
|
||||
BEAST_EXPECT(m.reason() == "OK");
|
||||
BEAST_EXPECT(m["Server"] == "test");
|
||||
BEAST_EXPECT(m["Transfer-Encoding"] == "chunked");
|
||||
BEAST_EXPECT(m["Expires"] == "never");
|
||||
BEAST_EXPECT(m["MD5-Fingerprint"] == "-");
|
||||
BEAST_EXPECT(m.body == "*****--");
|
||||
}
|
||||
);
|
||||
doMatrix<false>(
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n"
|
||||
"*****",
|
||||
[&](parser_type<false> const& p)
|
||||
{
|
||||
auto const& m = p.get();
|
||||
BEAST_EXPECT(m.body == "*****");
|
||||
}
|
||||
);
|
||||
doMatrix<true>(
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"\r\n",
|
||||
[&](parser_type<true> const& p)
|
||||
{
|
||||
auto const& m = p.get();
|
||||
BEAST_EXPECT(m.method() == verb::get);
|
||||
BEAST_EXPECT(m.target() == "/");
|
||||
BEAST_EXPECT(m.version == 11);
|
||||
BEAST_EXPECT(! p.need_eof());
|
||||
BEAST_EXPECT(! p.is_chunked());
|
||||
BEAST_EXPECT(p.content_length() == boost::none);
|
||||
}
|
||||
);
|
||||
doMatrix<true>(
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"X: \t x \t \r\n"
|
||||
"\r\n",
|
||||
[&](parser_type<true> const& p)
|
||||
{
|
||||
auto const& m = p.get();
|
||||
BEAST_EXPECT(m["X"] == "x");
|
||||
}
|
||||
);
|
||||
|
||||
// test eager(true)
|
||||
{
|
||||
error_code ec;
|
||||
parser_type<true> p;
|
||||
p.eager(true);
|
||||
p.put(buf(
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"\r\n"
|
||||
"*")
|
||||
, ec);
|
||||
auto const& m = p.get();
|
||||
BEAST_EXPECT(! ec);
|
||||
BEAST_EXPECT(p.is_done());
|
||||
BEAST_EXPECT(p.is_header_done());
|
||||
BEAST_EXPECT(! p.need_eof());
|
||||
BEAST_EXPECT(m.method() == verb::get);
|
||||
BEAST_EXPECT(m.target() == "/");
|
||||
BEAST_EXPECT(m.version == 11);
|
||||
BEAST_EXPECT(m["User-Agent"] == "test");
|
||||
BEAST_EXPECT(m.body == "*");
|
||||
}
|
||||
{
|
||||
// test partial parsing of final chunk
|
||||
// parse through the chunk body
|
||||
error_code ec;
|
||||
flat_buffer b;
|
||||
parser_type<true> p;
|
||||
p.eager(true);
|
||||
ostream(b) <<
|
||||
"PUT / HTTP/1.1\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"1\r\n"
|
||||
"*";
|
||||
auto used = p.put(b.data(), ec);
|
||||
b.consume(used);
|
||||
BEAST_EXPECT(! ec);
|
||||
BEAST_EXPECT(! p.is_done());
|
||||
BEAST_EXPECT(p.get().body == "*");
|
||||
ostream(b) <<
|
||||
"\r\n"
|
||||
"0;d;e=3;f=\"4\"\r\n"
|
||||
"Expires: never\r\n"
|
||||
"MD5-Fingerprint: -\r\n";
|
||||
// incomplete parse, missing the final crlf
|
||||
used = p.put(b.data(), ec);
|
||||
b.consume(used);
|
||||
BEAST_EXPECT(ec == error::need_more);
|
||||
ec.assign(0, ec.category());
|
||||
BEAST_EXPECT(! p.is_done());
|
||||
ostream(b) <<
|
||||
"\r\n"; // final crlf to end message
|
||||
used = p.put(b.data(), ec);
|
||||
b.consume(used);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(p.is_done());
|
||||
}
|
||||
// skip body
|
||||
{
|
||||
error_code ec;
|
||||
response_parser<string_body> p;
|
||||
p.skip(true);
|
||||
p.put(buf(
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n"
|
||||
"*****")
|
||||
, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(p.is_done());
|
||||
BEAST_EXPECT(p.is_header_done());
|
||||
BEAST_EXPECT(p.content_length() &&
|
||||
*p.content_length() == 5);
|
||||
}
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
template<class DynamicBuffer>
|
||||
void
|
||||
testNeedMore()
|
||||
{
|
||||
error_code ec;
|
||||
std::size_t used;
|
||||
{
|
||||
DynamicBuffer b;
|
||||
parser_type<true> p;
|
||||
ostream(b) <<
|
||||
"GET / HTTP/1.1\r\n";
|
||||
used = p.put(b.data(), ec);
|
||||
BEAST_EXPECTS(ec == error::need_more, ec.message());
|
||||
b.consume(used);
|
||||
ec.assign(0, ec.category());
|
||||
ostream(b) <<
|
||||
"User-Agent: test\r\n"
|
||||
"\r\n";
|
||||
used = p.put(b.data(), ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
b.consume(used);
|
||||
BEAST_EXPECT(p.is_done());
|
||||
BEAST_EXPECT(p.is_header_done());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testGotSome()
|
||||
{
|
||||
error_code ec;
|
||||
parser_type<true> p;
|
||||
auto used = p.put(buf(""), ec);
|
||||
BEAST_EXPECT(ec == error::need_more);
|
||||
BEAST_EXPECT(! p.got_some());
|
||||
BEAST_EXPECT(used == 0);
|
||||
ec.assign(0, ec.category());
|
||||
used = p.put(buf("G"), ec);
|
||||
BEAST_EXPECT(ec == error::need_more);
|
||||
BEAST_EXPECT(p.got_some());
|
||||
BEAST_EXPECT(used == 0);
|
||||
}
|
||||
|
||||
void
|
||||
testCallback()
|
||||
{
|
||||
{
|
||||
multi_buffer b;
|
||||
ostream(b) <<
|
||||
"POST / HTTP/1.1\r\n"
|
||||
"Content-Length: 2\r\n"
|
||||
"\r\n"
|
||||
"**";
|
||||
error_code ec;
|
||||
parser<true, string_body> p;
|
||||
p.eager(true);
|
||||
p.put(b.data(), ec);
|
||||
p.on_header(
|
||||
[this](parser<true, string_body>& p, error_code& ec)
|
||||
{
|
||||
BEAST_EXPECT(p.is_header_done());
|
||||
ec.assign(0, ec.category());
|
||||
});
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
{
|
||||
multi_buffer b;
|
||||
ostream(b) <<
|
||||
"POST / HTTP/1.1\r\n"
|
||||
"Content-Length: 2\r\n"
|
||||
"\r\n"
|
||||
"**";
|
||||
error_code ec;
|
||||
parser<true, string_body> p;
|
||||
p.eager(true);
|
||||
p.put(b.data(), ec);
|
||||
p.on_header(
|
||||
[this](parser<true, string_body>&, error_code& ec)
|
||||
{
|
||||
ec.assign(errc::bad_message,
|
||||
generic_category());
|
||||
});
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testParse();
|
||||
testNeedMore<flat_buffer>();
|
||||
testNeedMore<multi_buffer>();
|
||||
testGotSome();
|
||||
testCallback();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(parser,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
@@ -1,155 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
#include "nodejs_parser.hpp"
|
||||
#include "message_fuzz.hpp"
|
||||
|
||||
#include <beast/http.hpp>
|
||||
#include <beast/core/streambuf.hpp>
|
||||
#include <beast/core/to_string.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <chrono>
|
||||
#include <iostream>
|
||||
#include <vector>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class parser_bench_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
static std::size_t constexpr N = 2000;
|
||||
|
||||
using corpus = std::vector<streambuf>;
|
||||
|
||||
corpus creq_;
|
||||
corpus cres_;
|
||||
std::size_t size_ = 0;
|
||||
|
||||
parser_bench_test()
|
||||
{
|
||||
creq_ = build_corpus(N/2, std::true_type{});
|
||||
cres_ = build_corpus(N/2, std::false_type{});
|
||||
}
|
||||
|
||||
corpus
|
||||
build_corpus(std::size_t n, std::true_type)
|
||||
{
|
||||
corpus v;
|
||||
v.resize(N);
|
||||
message_fuzz mg;
|
||||
for(std::size_t i = 0; i < n; ++i)
|
||||
{
|
||||
mg.request(v[i]);
|
||||
size_ += v[i].size();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
corpus
|
||||
build_corpus(std::size_t n, std::false_type)
|
||||
{
|
||||
corpus v;
|
||||
v.resize(N);
|
||||
message_fuzz mg;
|
||||
for(std::size_t i = 0; i < n; ++i)
|
||||
{
|
||||
mg.response(v[i]);
|
||||
size_ += v[i].size();
|
||||
}
|
||||
return v;
|
||||
}
|
||||
|
||||
template<class Parser>
|
||||
void
|
||||
testParser(std::size_t repeat, corpus const& v)
|
||||
{
|
||||
while(repeat--)
|
||||
for(auto const& sb : v)
|
||||
{
|
||||
Parser p;
|
||||
error_code ec;
|
||||
p.write(sb.data(), ec);
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
log << to_string(sb.data()) << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<class Function>
|
||||
void
|
||||
timedTest(std::size_t repeat, std::string const& name, Function&& f)
|
||||
{
|
||||
using namespace std::chrono;
|
||||
using clock_type = std::chrono::high_resolution_clock;
|
||||
log << name << std::endl;
|
||||
for(std::size_t trial = 1; trial <= repeat; ++trial)
|
||||
{
|
||||
auto const t0 = clock_type::now();
|
||||
f();
|
||||
auto const elapsed = clock_type::now() - t0;
|
||||
log <<
|
||||
"Trial " << trial << ": " <<
|
||||
duration_cast<milliseconds>(elapsed).count() << " ms" << std::endl;
|
||||
}
|
||||
}
|
||||
|
||||
template<bool isRequest>
|
||||
struct null_parser : basic_parser_v1<isRequest, null_parser<isRequest>>
|
||||
{
|
||||
};
|
||||
|
||||
void
|
||||
testSpeed()
|
||||
{
|
||||
static std::size_t constexpr Trials = 3;
|
||||
static std::size_t constexpr Repeat = 50;
|
||||
|
||||
log << "sizeof(request parser) == " <<
|
||||
sizeof(basic_parser_v1<true, null_parser<true>>) << '\n';
|
||||
|
||||
log << "sizeof(response parser) == " <<
|
||||
sizeof(basic_parser_v1<false, null_parser<true>>)<< '\n';
|
||||
|
||||
testcase << "Parser speed test, " <<
|
||||
((Repeat * size_ + 512) / 1024) << "KB in " <<
|
||||
(Repeat * (creq_.size() + cres_.size())) << " messages";
|
||||
|
||||
timedTest(Trials, "nodejs_parser",
|
||||
[&]
|
||||
{
|
||||
testParser<nodejs_parser<
|
||||
true, streambuf_body, fields>>(
|
||||
Repeat, creq_);
|
||||
testParser<nodejs_parser<
|
||||
false, streambuf_body, fields>>(
|
||||
Repeat, cres_);
|
||||
});
|
||||
timedTest(Trials, "http::basic_parser_v1",
|
||||
[&]
|
||||
{
|
||||
testParser<parser_v1<
|
||||
true, streambuf_body, fields>>(
|
||||
Repeat, creq_);
|
||||
testParser<parser_v1<
|
||||
false, streambuf_body, fields>>(
|
||||
Repeat, cres_);
|
||||
});
|
||||
pass();
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
pass();
|
||||
testSpeed();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(parser_bench,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
@@ -1,160 +0,0 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/parser_v1.hpp>
|
||||
|
||||
#include <beast/core/streambuf.hpp>
|
||||
#include <beast/http/fields.hpp>
|
||||
#include <beast/http/header_parser_v1.hpp>
|
||||
#include <beast/http/parse.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/test/string_istream.hpp>
|
||||
#include <beast/test/yield_to.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class parser_v1_test
|
||||
: public beast::unit_test::suite
|
||||
, public test::enable_yield_to
|
||||
{
|
||||
public:
|
||||
void
|
||||
testParse()
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
{
|
||||
error_code ec;
|
||||
parser_v1<true, string_body,
|
||||
basic_fields<std::allocator<char>>> p;
|
||||
std::string const s =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"\r\n"
|
||||
"*";
|
||||
p.write(buffer(s), ec);
|
||||
BEAST_EXPECT(! ec);
|
||||
BEAST_EXPECT(p.complete());
|
||||
auto m = p.release();
|
||||
BEAST_EXPECT(m.method == "GET");
|
||||
BEAST_EXPECT(m.url == "/");
|
||||
BEAST_EXPECT(m.version == 11);
|
||||
BEAST_EXPECT(m.fields["User-Agent"] == "test");
|
||||
BEAST_EXPECT(m.body == "*");
|
||||
}
|
||||
{
|
||||
error_code ec;
|
||||
parser_v1<false, string_body,
|
||||
basic_fields<std::allocator<char>>> p;
|
||||
std::string const s =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"\r\n"
|
||||
"*";
|
||||
p.write(buffer(s), ec);
|
||||
BEAST_EXPECT(! ec);
|
||||
BEAST_EXPECT(p.complete());
|
||||
auto m = p.release();
|
||||
BEAST_EXPECT(m.status == 200);
|
||||
BEAST_EXPECT(m.reason == "OK");
|
||||
BEAST_EXPECT(m.version == 11);
|
||||
BEAST_EXPECT(m.fields["Server"] == "test");
|
||||
BEAST_EXPECT(m.body == "*");
|
||||
}
|
||||
// skip body
|
||||
{
|
||||
error_code ec;
|
||||
parser_v1<false, string_body, fields> p;
|
||||
std::string const s =
|
||||
"HTTP/1.1 200 Connection Established\r\n"
|
||||
"Proxy-Agent: Zscaler/5.1\r\n"
|
||||
"\r\n";
|
||||
p.set_option(skip_body{true});
|
||||
p.write(buffer(s), ec);
|
||||
BEAST_EXPECT(! ec);
|
||||
BEAST_EXPECT(p.complete());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testWithBody()
|
||||
{
|
||||
std::string const raw =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"\r\n"
|
||||
"*";
|
||||
test::string_istream ss{
|
||||
ios_, raw, raw.size() - 1};
|
||||
|
||||
streambuf rb;
|
||||
header_parser_v1<true, fields> p0;
|
||||
parse(ss, rb, p0);
|
||||
request_header const& reqh = p0.get();
|
||||
BEAST_EXPECT(reqh.method == "GET");
|
||||
BEAST_EXPECT(reqh.url == "/");
|
||||
BEAST_EXPECT(reqh.version == 11);
|
||||
BEAST_EXPECT(reqh.fields["User-Agent"] == "test");
|
||||
BEAST_EXPECT(reqh.fields["Content-Length"] == "1");
|
||||
parser_v1<true, string_body, fields> p =
|
||||
with_body<string_body>(p0);
|
||||
BEAST_EXPECT(p.get().method == "GET");
|
||||
BEAST_EXPECT(p.get().url == "/");
|
||||
BEAST_EXPECT(p.get().version == 11);
|
||||
BEAST_EXPECT(p.get().fields["User-Agent"] == "test");
|
||||
BEAST_EXPECT(p.get().fields["Content-Length"] == "1");
|
||||
parse(ss, rb, p);
|
||||
request<string_body, fields> req = p.release();
|
||||
BEAST_EXPECT(req.body == "*");
|
||||
}
|
||||
|
||||
void
|
||||
testRegressions()
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
|
||||
// consecutive empty header values
|
||||
{
|
||||
error_code ec;
|
||||
parser_v1<true, string_body, fields> p;
|
||||
std::string const s =
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"X1:\r\n"
|
||||
"X2:\r\n"
|
||||
"X3:x\r\n"
|
||||
"\r\n";
|
||||
p.write(buffer(s), ec);
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
return;
|
||||
BEAST_EXPECT(p.complete());
|
||||
auto const msg = p.release();
|
||||
BEAST_EXPECT(msg.fields.exists("X1"));
|
||||
BEAST_EXPECT(msg.fields["X1"] == "");
|
||||
BEAST_EXPECT(msg.fields.exists("X2"));
|
||||
BEAST_EXPECT(msg.fields["X2"] == "");
|
||||
BEAST_EXPECT(msg.fields.exists("X3"));
|
||||
BEAST_EXPECT(msg.fields["X3"] == "x");
|
||||
}
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
testParse();
|
||||
testWithBody();
|
||||
testRegressions();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(parser_v1,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
@@ -8,11 +8,16 @@
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/read.hpp>
|
||||
|
||||
#include "fail_parser.hpp"
|
||||
#include "test_parser.hpp"
|
||||
|
||||
#include <beast/core/ostream.hpp>
|
||||
#include <beast/core/static_buffer.hpp>
|
||||
#include <beast/http/fields.hpp>
|
||||
#include <beast/http/streambuf_body.hpp>
|
||||
#include <beast/http/dynamic_body.hpp>
|
||||
#include <beast/http/parser.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/test/fail_stream.hpp>
|
||||
#include <beast/test/pipe_stream.hpp>
|
||||
#include <beast/test/string_istream.hpp>
|
||||
#include <beast/test/yield_to.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
@@ -27,62 +32,9 @@ class read_test
|
||||
, public test::enable_yield_to
|
||||
{
|
||||
public:
|
||||
struct fail_body
|
||||
{
|
||||
class reader;
|
||||
|
||||
class value_type
|
||||
{
|
||||
friend class reader;
|
||||
|
||||
std::string s_;
|
||||
test::fail_counter& fc_;
|
||||
|
||||
public:
|
||||
explicit
|
||||
value_type(test::fail_counter& fc)
|
||||
: fc_(fc)
|
||||
{
|
||||
}
|
||||
|
||||
value_type&
|
||||
operator=(std::string s)
|
||||
{
|
||||
s_ = std::move(s);
|
||||
return *this;
|
||||
}
|
||||
};
|
||||
|
||||
class reader
|
||||
{
|
||||
value_type& body_;
|
||||
|
||||
public:
|
||||
template<bool isRequest, class Allocator>
|
||||
explicit
|
||||
reader(message<isRequest, fail_body, Allocator>& msg) noexcept
|
||||
: body_(msg.body)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
init(error_code& ec) noexcept
|
||||
{
|
||||
body_.fc_.fail(ec);
|
||||
}
|
||||
|
||||
void
|
||||
write(void const* data,
|
||||
std::size_t size, error_code& ec) noexcept
|
||||
{
|
||||
if(body_.fc_.fail(ec))
|
||||
return;
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template<bool isRequest>
|
||||
void failMatrix(char const* s, yield_context do_yield)
|
||||
void
|
||||
failMatrix(char const* s, yield_context do_yield)
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
using boost::asio::buffer_copy;
|
||||
@@ -91,15 +43,15 @@ public:
|
||||
auto const len = strlen(s);
|
||||
for(n = 0; n < limit; ++n)
|
||||
{
|
||||
streambuf sb;
|
||||
sb.commit(buffer_copy(
|
||||
sb.prepare(len), buffer(s, len)));
|
||||
multi_buffer b;
|
||||
b.commit(buffer_copy(
|
||||
b.prepare(len), buffer(s, len)));
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_istream> fs{fc, ios_, ""};
|
||||
fail_parser<isRequest> p(fc);
|
||||
error_code ec;
|
||||
parse(fs, sb, p, ec);
|
||||
test_parser<isRequest> p(fc);
|
||||
error_code ec = test::error::fail_error;
|
||||
read(fs, b, p, ec);
|
||||
if(! ec)
|
||||
break;
|
||||
}
|
||||
@@ -107,30 +59,30 @@ public:
|
||||
for(n = 0; n < limit; ++n)
|
||||
{
|
||||
static std::size_t constexpr pre = 10;
|
||||
streambuf sb;
|
||||
sb.commit(buffer_copy(
|
||||
sb.prepare(pre), buffer(s, pre)));
|
||||
multi_buffer b;
|
||||
b.commit(buffer_copy(
|
||||
b.prepare(pre), buffer(s, pre)));
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<test::string_istream> fs{
|
||||
fc, ios_, std::string{s + pre, len - pre}};
|
||||
fail_parser<isRequest> p(fc);
|
||||
error_code ec;
|
||||
parse(fs, sb, p, ec);
|
||||
test_parser<isRequest> p(fc);
|
||||
error_code ec = test::error::fail_error;
|
||||
read(fs, b, p, ec);
|
||||
if(! ec)
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(n < limit);
|
||||
for(n = 0; n < limit; ++n)
|
||||
{
|
||||
streambuf sb;
|
||||
sb.commit(buffer_copy(
|
||||
sb.prepare(len), buffer(s, len)));
|
||||
multi_buffer b;
|
||||
b.commit(buffer_copy(
|
||||
b.prepare(len), buffer(s, len)));
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_istream> fs{fc, ios_, ""};
|
||||
fail_parser<isRequest> p(fc);
|
||||
error_code ec;
|
||||
async_parse(fs, sb, p, do_yield[ec]);
|
||||
test_parser<isRequest> p(fc);
|
||||
error_code ec = test::error::fail_error;
|
||||
async_read(fs, b, p, do_yield[ec]);
|
||||
if(! ec)
|
||||
break;
|
||||
}
|
||||
@@ -138,29 +90,15 @@ public:
|
||||
for(n = 0; n < limit; ++n)
|
||||
{
|
||||
static std::size_t constexpr pre = 10;
|
||||
streambuf sb;
|
||||
sb.commit(buffer_copy(
|
||||
sb.prepare(pre), buffer(s, pre)));
|
||||
multi_buffer b;
|
||||
b.commit(buffer_copy(
|
||||
b.prepare(pre), buffer(s, pre)));
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<test::string_istream> fs{
|
||||
fc, ios_, std::string{s + pre, len - pre}};
|
||||
fail_parser<isRequest> p(fc);
|
||||
error_code ec;
|
||||
async_parse(fs, sb, p, do_yield[ec]);
|
||||
if(! ec)
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(n < limit);
|
||||
for(n = 0; n < limit; ++n)
|
||||
{
|
||||
streambuf sb;
|
||||
sb.commit(buffer_copy(
|
||||
sb.prepare(len), buffer(s, len)));
|
||||
test::fail_counter fc{n};
|
||||
test::string_istream ss{ios_, s};
|
||||
parser_v1<isRequest, fail_body, fields> p{fc};
|
||||
error_code ec;
|
||||
parse(ss, sb, p, ec);
|
||||
test_parser<isRequest> p(fc);
|
||||
error_code ec = test::error::fail_error;
|
||||
async_read(fs, b, p, do_yield[ec]);
|
||||
if(! ec)
|
||||
break;
|
||||
}
|
||||
@@ -171,10 +109,10 @@ public:
|
||||
{
|
||||
try
|
||||
{
|
||||
streambuf sb;
|
||||
multi_buffer b;
|
||||
test::string_istream ss(ios_, "GET / X");
|
||||
parser_v1<true, streambuf_body, fields> p;
|
||||
parse(ss, sb, p);
|
||||
request_parser<dynamic_body> p;
|
||||
read(ss, b, p);
|
||||
fail();
|
||||
}
|
||||
catch(std::exception const&)
|
||||
@@ -183,6 +121,52 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testBufferOverflow()
|
||||
{
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
ostream(p.server.buffer) <<
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Host: localhost\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"10\r\n"
|
||||
"****************\r\n"
|
||||
"0\r\n\r\n";
|
||||
static_buffer_n<1024> b;
|
||||
request<string_body> req;
|
||||
try
|
||||
{
|
||||
read(p.server, b, req);
|
||||
pass();
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
fail(e.what(), __FILE__, __LINE__);
|
||||
}
|
||||
}
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
ostream(p.server.buffer) <<
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Host: localhost\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"10\r\n"
|
||||
"****************\r\n"
|
||||
"0\r\n\r\n";
|
||||
error_code ec = test::error::fail_error;
|
||||
static_buffer_n<10> b;
|
||||
request<string_body> req;
|
||||
read(p.server, b, req, ec);
|
||||
BEAST_EXPECTS(ec == error::buffer_overflow,
|
||||
ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
void testFailures(yield_context do_yield)
|
||||
{
|
||||
char const* req[] = {
|
||||
@@ -245,52 +229,6 @@ public:
|
||||
failMatrix<false>(res[i], do_yield);
|
||||
}
|
||||
|
||||
void testReadHeaders(yield_context do_yield)
|
||||
{
|
||||
static std::size_t constexpr limit = 100;
|
||||
std::size_t n;
|
||||
|
||||
for(n = 0; n < limit; ++n)
|
||||
{
|
||||
test::fail_stream<test::string_istream> fs{n, ios_,
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Host: localhost\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n"
|
||||
};
|
||||
request_header m;
|
||||
try
|
||||
{
|
||||
streambuf sb;
|
||||
read(fs, sb, m);
|
||||
break;
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
}
|
||||
}
|
||||
BEAST_EXPECT(n < limit);
|
||||
|
||||
for(n = 0; n < limit; ++n)
|
||||
{
|
||||
test::fail_stream<test::string_istream> fs(n, ios_,
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"Host: localhost\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
request_header m;
|
||||
error_code ec;
|
||||
streambuf sb;
|
||||
async_read(fs, sb, m, do_yield[ec]);
|
||||
if(! ec)
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(n < limit);
|
||||
}
|
||||
|
||||
void testRead(yield_context do_yield)
|
||||
{
|
||||
static std::size_t constexpr limit = 100;
|
||||
@@ -305,11 +243,11 @@ public:
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
request<streambuf_body> m;
|
||||
request<dynamic_body> m;
|
||||
try
|
||||
{
|
||||
streambuf sb;
|
||||
read(fs, sb, m);
|
||||
multi_buffer b;
|
||||
read(fs, b, m);
|
||||
break;
|
||||
}
|
||||
catch(std::exception const&)
|
||||
@@ -327,10 +265,10 @@ public:
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
request<streambuf_body> m;
|
||||
error_code ec;
|
||||
streambuf sb;
|
||||
read(fs, sb, m, ec);
|
||||
request<dynamic_body> m;
|
||||
error_code ec = test::error::fail_error;
|
||||
multi_buffer b;
|
||||
read(fs, b, m, ec);
|
||||
if(! ec)
|
||||
break;
|
||||
}
|
||||
@@ -345,33 +283,34 @@ public:
|
||||
"Content-Length: 0\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
request<streambuf_body> m;
|
||||
error_code ec;
|
||||
streambuf sb;
|
||||
async_read(fs, sb, m, do_yield[ec]);
|
||||
request<dynamic_body> m;
|
||||
error_code ec = test::error::fail_error;
|
||||
multi_buffer b;
|
||||
async_read(fs, b, m, do_yield[ec]);
|
||||
if(! ec)
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(n < limit);
|
||||
}
|
||||
|
||||
void testEof(yield_context do_yield)
|
||||
void
|
||||
testEof(yield_context do_yield)
|
||||
{
|
||||
{
|
||||
streambuf sb;
|
||||
multi_buffer b;
|
||||
test::string_istream ss(ios_, "");
|
||||
parser_v1<true, streambuf_body, fields> p;
|
||||
request_parser<dynamic_body> p;
|
||||
error_code ec;
|
||||
parse(ss, sb, p, ec);
|
||||
BEAST_EXPECT(ec == boost::asio::error::eof);
|
||||
read(ss, b, p, ec);
|
||||
BEAST_EXPECT(ec == http::error::end_of_stream);
|
||||
}
|
||||
{
|
||||
streambuf sb;
|
||||
multi_buffer b;
|
||||
test::string_istream ss(ios_, "");
|
||||
parser_v1<true, streambuf_body, fields> p;
|
||||
request_parser<dynamic_body> p;
|
||||
error_code ec;
|
||||
async_parse(ss, sb, p, do_yield[ec]);
|
||||
BEAST_EXPECT(ec == boost::asio::error::eof);
|
||||
async_read(ss, b, p, do_yield[ec]);
|
||||
BEAST_EXPECT(ec == http::error::end_of_stream);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -396,9 +335,9 @@ public:
|
||||
test::string_istream is{ios,
|
||||
"GET / HTTP/1.1\r\n\r\n"};
|
||||
BEAST_EXPECT(handler::count() == 0);
|
||||
streambuf sb;
|
||||
message<true, streambuf_body, fields> m;
|
||||
async_read(is, sb, m, handler{});
|
||||
multi_buffer b;
|
||||
request<dynamic_body> m;
|
||||
async_read(is, b, m, handler{});
|
||||
BEAST_EXPECT(handler::count() > 0);
|
||||
ios.stop();
|
||||
BEAST_EXPECT(handler::count() > 0);
|
||||
@@ -415,25 +354,107 @@ public:
|
||||
test::string_istream is{ios,
|
||||
"GET / HTTP/1.1\r\n\r\n"};
|
||||
BEAST_EXPECT(handler::count() == 0);
|
||||
streambuf sb;
|
||||
message<true, streambuf_body, fields> m;
|
||||
async_read(is, sb, m, handler{});
|
||||
multi_buffer b;
|
||||
request<dynamic_body> m;
|
||||
async_read(is, b, m, handler{});
|
||||
BEAST_EXPECT(handler::count() > 0);
|
||||
}
|
||||
BEAST_EXPECT(handler::count() == 0);
|
||||
}
|
||||
}
|
||||
|
||||
void run() override
|
||||
// https://github.com/vinniefalco/Beast/issues/430
|
||||
void
|
||||
testRegression430()
|
||||
{
|
||||
test::pipe c{ios_};
|
||||
c.server.read_size(1);
|
||||
ostream(c.server.buffer) <<
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"Content-Type: application/octet-stream\r\n"
|
||||
"\r\n"
|
||||
"4\r\nabcd\r\n"
|
||||
"0\r\n\r\n";
|
||||
error_code ec;
|
||||
flat_buffer fb;
|
||||
parser<false, dynamic_body> p;
|
||||
read(c.server, fb, p, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
}
|
||||
|
||||
//--------------------------------------------------------------------------
|
||||
|
||||
template<class Parser, class Pred>
|
||||
void
|
||||
readgrind(string_view s, Pred&& pred)
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
for(std::size_t n = 1; n < s.size() - 1; ++n)
|
||||
{
|
||||
Parser p;
|
||||
error_code ec = test::error::fail_error;
|
||||
flat_buffer b;
|
||||
test::pipe c{ios_};
|
||||
ostream(c.server.buffer) << s;
|
||||
c.server.read_size(n);
|
||||
read(c.server, b, p, ec);
|
||||
if(! BEAST_EXPECTS(! ec, ec.message()))
|
||||
continue;
|
||||
pred(p);
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testReadGrind()
|
||||
{
|
||||
readgrind<test_parser<false>>(
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"Content-Type: application/octet-stream\r\n"
|
||||
"\r\n"
|
||||
"4\r\nabcd\r\n"
|
||||
"0\r\n\r\n"
|
||||
,[&](test_parser<false> const& p)
|
||||
{
|
||||
BEAST_EXPECT(p.body == "abcd");
|
||||
});
|
||||
readgrind<test_parser<false>>(
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Expect: Expires, MD5-Fingerprint\r\n"
|
||||
"Transfer-Encoding: chunked\r\n"
|
||||
"\r\n"
|
||||
"5\r\n"
|
||||
"*****\r\n"
|
||||
"2;a;b=1;c=\"2\"\r\n"
|
||||
"--\r\n"
|
||||
"0;d;e=3;f=\"4\"\r\n"
|
||||
"Expires: never\r\n"
|
||||
"MD5-Fingerprint: -\r\n"
|
||||
"\r\n"
|
||||
,[&](test_parser<false> const& p)
|
||||
{
|
||||
BEAST_EXPECT(p.body == "*****--");
|
||||
});
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testThrow();
|
||||
testBufferOverflow();
|
||||
|
||||
yield_to(&read_test::testFailures, this);
|
||||
yield_to(&read_test::testReadHeaders, this);
|
||||
yield_to(&read_test::testRead, this);
|
||||
yield_to(&read_test::testEof, this);
|
||||
yield_to([&](yield_context yield){
|
||||
testFailures(yield); });
|
||||
yield_to([&](yield_context yield){
|
||||
testRead(yield); });
|
||||
yield_to([&](yield_context yield){
|
||||
testEof(yield); });
|
||||
|
||||
testIoService();
|
||||
testRegression430();
|
||||
testReadGrind();
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
@@ -13,9 +13,10 @@
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include <beast/core/detail/empty_base_optimization.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
namespace test {
|
||||
|
||||
class rfc7230_test : public beast::unit_test::suite
|
||||
{
|
||||
@@ -29,7 +30,7 @@ public:
|
||||
|
||||
static
|
||||
std::string
|
||||
str(boost::string_ref const& s)
|
||||
str(string_view s)
|
||||
{
|
||||
return std::string(s.data(), s.size());
|
||||
}
|
||||
@@ -62,18 +63,18 @@ public:
|
||||
BEAST_EXPECTS(got == s, fmt(got));
|
||||
};
|
||||
auto const cs =
|
||||
[&](std::string const& s, std::string const& good)
|
||||
[&](std::string const& s, std::string const& answer)
|
||||
{
|
||||
ce(good);
|
||||
ce(answer);
|
||||
auto const got = str(param_list{s});
|
||||
ce(got);
|
||||
BEAST_EXPECTS(got == good, fmt(got));
|
||||
BEAST_EXPECTS(got == answer, fmt(got));
|
||||
};
|
||||
auto const cq =
|
||||
[&](std::string const& s, std::string const& good)
|
||||
[&](std::string const& s, std::string const& answer)
|
||||
{
|
||||
auto const got = str(param_list{s});
|
||||
BEAST_EXPECTS(got == good, fmt(got));
|
||||
BEAST_EXPECTS(got == answer, fmt(got));
|
||||
};
|
||||
|
||||
ce("");
|
||||
@@ -135,9 +136,9 @@ public:
|
||||
BEAST_EXPECTS(got == good, fmt(got));
|
||||
};
|
||||
/*
|
||||
ext-list = *( "," OWS ) ext *( OWS "," [ OWS ext ] )
|
||||
ext = token param-list
|
||||
param-list = *( OWS ";" OWS param )
|
||||
ext-basic_parsed_list = *( "," OWS ) ext *( OWS "," [ OWS ext ] )
|
||||
ext = token param-basic_parsed_list
|
||||
param-basic_parsed_list = *( OWS ";" OWS param )
|
||||
param = token OWS "=" OWS ( token / quoted-string )
|
||||
*/
|
||||
cs(",", "");
|
||||
@@ -237,17 +238,120 @@ public:
|
||||
cs("x y", "x");
|
||||
}
|
||||
|
||||
template<class Policy>
|
||||
static
|
||||
std::vector<std::string>
|
||||
to_vector(string_view in)
|
||||
{
|
||||
std::vector<std::string> v;
|
||||
detail::basic_parsed_list<Policy> list{in};
|
||||
for(auto const& s :
|
||||
detail::basic_parsed_list<Policy>{in})
|
||||
v.emplace_back(s.data(), s.size());
|
||||
return v;
|
||||
}
|
||||
|
||||
template<class Policy>
|
||||
void
|
||||
validate(string_view in,
|
||||
std::vector<std::string> const& v)
|
||||
{
|
||||
BEAST_EXPECT(to_vector<Policy>(in) == v);
|
||||
}
|
||||
|
||||
template<class Policy>
|
||||
void
|
||||
good(string_view in)
|
||||
{
|
||||
BEAST_EXPECT(validate_list(
|
||||
detail::basic_parsed_list<Policy>{in}));
|
||||
}
|
||||
|
||||
template<class Policy>
|
||||
void
|
||||
good(string_view in,
|
||||
std::vector<std::string> const& v)
|
||||
{
|
||||
BEAST_EXPECT(validate_list(
|
||||
detail::basic_parsed_list<Policy>{in}));
|
||||
validate<Policy>(in, v);
|
||||
}
|
||||
|
||||
template<class Policy>
|
||||
void
|
||||
bad(string_view in)
|
||||
{
|
||||
BEAST_EXPECT(! validate_list(
|
||||
detail::basic_parsed_list<Policy>{in}));
|
||||
}
|
||||
|
||||
void
|
||||
testOptTokenList()
|
||||
{
|
||||
/*
|
||||
#token = [ ( "," / token ) *( OWS "," [ OWS token ] ) ]
|
||||
*/
|
||||
using type = detail::opt_token_list_policy;
|
||||
|
||||
good<type>("", {});
|
||||
good<type>(" ", {});
|
||||
good<type>("\t", {});
|
||||
good<type>(" \t", {});
|
||||
good<type>(",", {});
|
||||
good<type>(",,", {});
|
||||
good<type>(", ,", {});
|
||||
good<type>(",\t,", {});
|
||||
good<type>(", \t,", {});
|
||||
good<type>(", \t, ", {});
|
||||
good<type>(", \t,\t", {});
|
||||
good<type>(", \t, \t", {});
|
||||
|
||||
good<type>("x", {"x"});
|
||||
good<type>(" x", {"x"});
|
||||
good<type>("x,,", {"x"});
|
||||
good<type>("x, ,", {"x"});
|
||||
good<type>("x,, ", {"x"});
|
||||
good<type>("x,,,", {"x"});
|
||||
|
||||
good<type>("x,y", {"x","y"});
|
||||
good<type>("x ,y", {"x","y"});
|
||||
good<type>("x\t,y", {"x","y"});
|
||||
good<type>("x \t,y", {"x","y"});
|
||||
good<type>(" x,y", {"x","y"});
|
||||
good<type>(" x,y ", {"x","y"});
|
||||
good<type>(",x,y", {"x","y"});
|
||||
good<type>("x,y,", {"x","y"});
|
||||
good<type>(",,x,y", {"x","y"});
|
||||
good<type>(",x,,y", {"x","y"});
|
||||
good<type>(",x,y,", {"x","y"});
|
||||
good<type>("x ,, y", {"x","y"});
|
||||
good<type>("x , ,y", {"x","y"});
|
||||
|
||||
good<type>("x,y,z", {"x","y","z"});
|
||||
|
||||
bad<type>("(");
|
||||
bad<type>("x(");
|
||||
bad<type>("(x");
|
||||
bad<type>(",(");
|
||||
bad<type>("(,");
|
||||
bad<type>("x,(");
|
||||
bad<type>("(,x");
|
||||
bad<type>("x y");
|
||||
}
|
||||
|
||||
void
|
||||
run()
|
||||
{
|
||||
testOptTokenList();
|
||||
#if 0
|
||||
testParamList();
|
||||
testExtList();
|
||||
testTokenList();
|
||||
#endif
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(rfc7230,http,beast);
|
||||
|
||||
} // test
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
125
test/http/serializer.cpp
Normal file
125
test/http/serializer.cpp
Normal file
@@ -0,0 +1,125 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/serializer.hpp>
|
||||
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class serializer_test : public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
struct const_body
|
||||
{
|
||||
struct value_type{};
|
||||
|
||||
struct reader
|
||||
{
|
||||
using const_buffers_type =
|
||||
boost::asio::const_buffers_1;
|
||||
|
||||
template<bool isRequest, class Fields>
|
||||
reader(message<isRequest, const_body, Fields> const&);
|
||||
|
||||
void
|
||||
init(error_code& ec);
|
||||
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(error_code&);
|
||||
};
|
||||
};
|
||||
|
||||
struct mutable_body
|
||||
{
|
||||
struct value_type{};
|
||||
|
||||
struct reader
|
||||
{
|
||||
using const_buffers_type =
|
||||
boost::asio::const_buffers_1;
|
||||
|
||||
template<bool isRequest, class Fields>
|
||||
reader(message<isRequest, mutable_body, Fields>&);
|
||||
|
||||
void
|
||||
init(error_code& ec);
|
||||
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(error_code&);
|
||||
};
|
||||
};
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_const< serializer<
|
||||
true, const_body>::value_type>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(! std::is_const<serializer<
|
||||
true, mutable_body>::value_type>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
serializer<true, const_body>,
|
||||
message <true, const_body>&>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
serializer<true, const_body>,
|
||||
message <true, const_body> const&>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(std::is_constructible<
|
||||
serializer<true, mutable_body>,
|
||||
message <true, mutable_body>&>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(! std::is_constructible<
|
||||
serializer<true, mutable_body>,
|
||||
message <true, mutable_body> const&>::value);
|
||||
|
||||
struct lambda
|
||||
{
|
||||
std::size_t size;
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
void
|
||||
operator()(error_code&,
|
||||
ConstBufferSequence const& buffers)
|
||||
{
|
||||
size = boost::asio::buffer_size(buffers);
|
||||
}
|
||||
};
|
||||
|
||||
void
|
||||
testWriteLimit()
|
||||
{
|
||||
auto const limit = 30;
|
||||
lambda visit;
|
||||
error_code ec;
|
||||
response<string_body> res;
|
||||
res.body.append(1000, '*');
|
||||
serializer<false, string_body> sr{res};
|
||||
sr.limit(limit);
|
||||
for(;;)
|
||||
{
|
||||
sr.next(ec, visit);
|
||||
BEAST_EXPECT(visit.size <= limit);
|
||||
sr.consume(visit.size);
|
||||
if(sr.is_done())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testWriteLimit();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(serializer,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
76
test/http/span_body.cpp
Normal file
76
test/http/span_body.cpp
Normal file
@@ -0,0 +1,76 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/span_body.hpp>
|
||||
|
||||
#include <beast/http/message.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
struct span_body_test
|
||||
: public beast::unit_test::suite
|
||||
{
|
||||
void
|
||||
testSpanBody()
|
||||
{
|
||||
{
|
||||
using B = span_body<char const>;
|
||||
request<B> req;
|
||||
|
||||
BEAST_EXPECT(req.body.size() == 0);
|
||||
BEAST_EXPECT(B::size(req.body) == 0);
|
||||
|
||||
req.body = B::value_type("xyz", 3);
|
||||
BEAST_EXPECT(req.body.size() == 3);
|
||||
BEAST_EXPECT(B::size(req.body) == 3);
|
||||
|
||||
B::reader r{req};
|
||||
error_code ec;
|
||||
r.init(ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
auto const buf = r.get(ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
if(! BEAST_EXPECT(buf != boost::none))
|
||||
return;
|
||||
BEAST_EXPECT(boost::asio::buffer_size(buf->first) == 3);
|
||||
BEAST_EXPECT(! buf->second);
|
||||
}
|
||||
{
|
||||
char buf[5];
|
||||
using B = span_body<char>;
|
||||
request<B> req;
|
||||
req.body = span<char>{buf, sizeof(buf)};
|
||||
B::writer w{req};
|
||||
error_code ec;
|
||||
w.init(boost::none, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
w.put(boost::asio::const_buffers_1{
|
||||
"123", 3}, ec);
|
||||
BEAST_EXPECTS(! ec, ec.message());
|
||||
BEAST_EXPECT(buf[0] == '1');
|
||||
BEAST_EXPECT(buf[1] == '2');
|
||||
BEAST_EXPECT(buf[2] == '3');
|
||||
w.put(boost::asio::const_buffers_1{
|
||||
"456", 3}, ec);
|
||||
BEAST_EXPECTS(ec == error::buffer_overflow, ec.message());
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
run() override
|
||||
{
|
||||
testSpanBody();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(span_body,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
176
test/http/status.cpp
Normal file
176
test/http/status.cpp
Normal file
@@ -0,0 +1,176 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/status.hpp>
|
||||
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class status_test
|
||||
: public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void
|
||||
testStatus()
|
||||
{
|
||||
auto const check = [&](status s, int i, status_class sc)
|
||||
{
|
||||
BEAST_EXPECT(int_to_status(i) == s);
|
||||
BEAST_EXPECT(to_status_class(i) == sc);
|
||||
BEAST_EXPECT(to_status_class(int_to_status(i)) == sc);
|
||||
};
|
||||
check(status::continue_ ,100, status_class::informational);
|
||||
check(status::switching_protocols ,101, status_class::informational);
|
||||
check(status::processing ,102, status_class::informational);
|
||||
|
||||
check(status::ok ,200, status_class::successful);
|
||||
check(status::created ,201, status_class::successful);
|
||||
check(status::accepted ,202, status_class::successful);
|
||||
check(status::non_authoritative_information ,203, status_class::successful);
|
||||
check(status::no_content ,204, status_class::successful);
|
||||
check(status::reset_content ,205, status_class::successful);
|
||||
check(status::partial_content ,206, status_class::successful);
|
||||
check(status::multi_status ,207, status_class::successful);
|
||||
check(status::already_reported ,208, status_class::successful);
|
||||
check(status::im_used ,226, status_class::successful);
|
||||
|
||||
check(status::multiple_choices ,300, status_class::redirection);
|
||||
check(status::moved_permanently ,301, status_class::redirection);
|
||||
check(status::found ,302, status_class::redirection);
|
||||
check(status::see_other ,303, status_class::redirection);
|
||||
check(status::not_modified ,304, status_class::redirection);
|
||||
check(status::use_proxy ,305, status_class::redirection);
|
||||
check(status::temporary_redirect ,307, status_class::redirection);
|
||||
check(status::permanent_redirect ,308, status_class::redirection);
|
||||
|
||||
check(status::bad_request ,400, status_class::client_error);
|
||||
check(status::unauthorized ,401, status_class::client_error);
|
||||
check(status::payment_required ,402, status_class::client_error);
|
||||
check(status::forbidden ,403, status_class::client_error);
|
||||
check(status::not_found ,404, status_class::client_error);
|
||||
check(status::method_not_allowed ,405, status_class::client_error);
|
||||
check(status::not_acceptable ,406, status_class::client_error);
|
||||
check(status::proxy_authentication_required ,407, status_class::client_error);
|
||||
check(status::request_timeout ,408, status_class::client_error);
|
||||
check(status::conflict ,409, status_class::client_error);
|
||||
check(status::gone ,410, status_class::client_error);
|
||||
check(status::length_required ,411, status_class::client_error);
|
||||
check(status::precondition_failed ,412, status_class::client_error);
|
||||
check(status::payload_too_large ,413, status_class::client_error);
|
||||
check(status::uri_too_long ,414, status_class::client_error);
|
||||
check(status::unsupported_media_type ,415, status_class::client_error);
|
||||
check(status::range_not_satisfiable ,416, status_class::client_error);
|
||||
check(status::expectation_failed ,417, status_class::client_error);
|
||||
check(status::misdirected_request ,421, status_class::client_error);
|
||||
check(status::unprocessable_entity ,422, status_class::client_error);
|
||||
check(status::locked ,423, status_class::client_error);
|
||||
check(status::failed_dependency ,424, status_class::client_error);
|
||||
check(status::upgrade_required ,426, status_class::client_error);
|
||||
check(status::precondition_required ,428, status_class::client_error);
|
||||
check(status::too_many_requests ,429, status_class::client_error);
|
||||
check(status::request_header_fields_too_large ,431, status_class::client_error);
|
||||
check(status::connection_closed_without_response ,444, status_class::client_error);
|
||||
check(status::unavailable_for_legal_reasons ,451, status_class::client_error);
|
||||
check(status::client_closed_request ,499, status_class::client_error);
|
||||
|
||||
check(status::internal_server_error ,500, status_class::server_error);
|
||||
check(status::not_implemented ,501, status_class::server_error);
|
||||
check(status::bad_gateway ,502, status_class::server_error);
|
||||
check(status::service_unavailable ,503, status_class::server_error);
|
||||
check(status::gateway_timeout ,504, status_class::server_error);
|
||||
check(status::http_version_not_supported ,505, status_class::server_error);
|
||||
check(status::variant_also_negotiates ,506, status_class::server_error);
|
||||
check(status::insufficient_storage ,507, status_class::server_error);
|
||||
check(status::loop_detected ,508, status_class::server_error);
|
||||
check(status::not_extended ,510, status_class::server_error);
|
||||
check(status::network_authentication_required ,511, status_class::server_error);
|
||||
check(status::network_connect_timeout_error ,599, status_class::server_error);
|
||||
|
||||
BEAST_EXPECT(to_status_class(1) == status_class::unknown);
|
||||
BEAST_EXPECT(to_status_class(status::unknown) == status_class::unknown);
|
||||
|
||||
auto const good =
|
||||
[&](status v)
|
||||
{
|
||||
BEAST_EXPECT(obsolete_reason(v) != "Unknown Status");
|
||||
};
|
||||
good(status::continue_);
|
||||
good(status::switching_protocols);
|
||||
good(status::processing);
|
||||
good(status::ok);
|
||||
good(status::created);
|
||||
good(status::accepted);
|
||||
good(status::non_authoritative_information);
|
||||
good(status::no_content);
|
||||
good(status::reset_content);
|
||||
good(status::partial_content);
|
||||
good(status::multi_status);
|
||||
good(status::already_reported);
|
||||
good(status::im_used);
|
||||
good(status::multiple_choices);
|
||||
good(status::moved_permanently);
|
||||
good(status::found);
|
||||
good(status::see_other);
|
||||
good(status::not_modified);
|
||||
good(status::use_proxy);
|
||||
good(status::temporary_redirect);
|
||||
good(status::permanent_redirect);
|
||||
good(status::bad_request);
|
||||
good(status::unauthorized);
|
||||
good(status::payment_required);
|
||||
good(status::forbidden);
|
||||
good(status::not_found);
|
||||
good(status::method_not_allowed);
|
||||
good(status::not_acceptable);
|
||||
good(status::proxy_authentication_required);
|
||||
good(status::request_timeout);
|
||||
good(status::conflict);
|
||||
good(status::gone);
|
||||
good(status::length_required);
|
||||
good(status::precondition_failed);
|
||||
good(status::payload_too_large);
|
||||
good(status::uri_too_long);
|
||||
good(status::unsupported_media_type);
|
||||
good(status::range_not_satisfiable);
|
||||
good(status::expectation_failed);
|
||||
good(status::misdirected_request);
|
||||
good(status::unprocessable_entity);
|
||||
good(status::locked);
|
||||
good(status::failed_dependency);
|
||||
good(status::upgrade_required);
|
||||
good(status::precondition_required);
|
||||
good(status::too_many_requests);
|
||||
good(status::request_header_fields_too_large);
|
||||
good(status::unavailable_for_legal_reasons);
|
||||
good(status::internal_server_error);
|
||||
good(status::not_implemented);
|
||||
good(status::bad_gateway);
|
||||
good(status::service_unavailable);
|
||||
good(status::gateway_timeout);
|
||||
good(status::http_version_not_supported);
|
||||
good(status::variant_also_negotiates);
|
||||
good(status::insufficient_storage);
|
||||
good(status::loop_detected);
|
||||
good(status::not_extended);
|
||||
good(status::network_authentication_required);
|
||||
}
|
||||
|
||||
void
|
||||
run()
|
||||
{
|
||||
testStatus();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(status,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
@@ -7,3 +7,13 @@
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/string_body.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
BOOST_STATIC_ASSERT(is_body<string_body>::value);
|
||||
BOOST_STATIC_ASSERT(is_body_reader<string_body>::value);
|
||||
BOOST_STATIC_ASSERT(is_body_writer<string_body>::value);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
158
test/http/test_parser.hpp
Normal file
158
test/http/test_parser.hpp
Normal file
@@ -0,0 +1,158 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
#ifndef BEAST_HTTP_TEST_PARSER_HPP
|
||||
#define BEAST_HTTP_TEST_PARSER_HPP
|
||||
|
||||
#include <beast/http/basic_parser.hpp>
|
||||
#include <beast/test/fail_counter.hpp>
|
||||
#include <string>
|
||||
#include <unordered_map>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
template<bool isRequest>
|
||||
class test_parser
|
||||
: public basic_parser<isRequest, test_parser<isRequest>>
|
||||
{
|
||||
test::fail_counter* fc_ = nullptr;
|
||||
|
||||
public:
|
||||
using mutable_buffers_type =
|
||||
boost::asio::mutable_buffers_1;
|
||||
|
||||
int status = 0;
|
||||
int version = 0;
|
||||
std::string method;
|
||||
std::string path;
|
||||
std::string reason;
|
||||
std::string body;
|
||||
int got_on_begin = 0;
|
||||
int got_on_field = 0;
|
||||
int got_on_header = 0;
|
||||
int got_on_body = 0;
|
||||
int got_content_length = 0;
|
||||
int got_on_chunk = 0;
|
||||
int got_on_complete = 0;
|
||||
std::unordered_map<
|
||||
std::string, std::string> fields;
|
||||
|
||||
test_parser() = default;
|
||||
|
||||
explicit
|
||||
test_parser(test::fail_counter& fc)
|
||||
: fc_(&fc)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
on_request(verb, string_view method_str_,
|
||||
string_view path_, int version_, error_code& ec)
|
||||
{
|
||||
method = std::string(
|
||||
method_str_.data(), method_str_.size());
|
||||
path = std::string(
|
||||
path_.data(), path_.size());
|
||||
version = version_;
|
||||
++got_on_begin;
|
||||
if(fc_)
|
||||
fc_->fail(ec);
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_response(int code,
|
||||
string_view reason_,
|
||||
int version_, error_code& ec)
|
||||
{
|
||||
status = code;
|
||||
reason = std::string(
|
||||
reason_.data(), reason_.size());
|
||||
version = version_;
|
||||
++got_on_begin;
|
||||
if(fc_)
|
||||
fc_->fail(ec);
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_field(field, string_view name,
|
||||
string_view value, error_code& ec)
|
||||
{
|
||||
++got_on_field;
|
||||
if(fc_)
|
||||
fc_->fail(ec);
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
fields[name.to_string()] = value.to_string();
|
||||
}
|
||||
|
||||
void
|
||||
on_header(error_code& ec)
|
||||
{
|
||||
++got_on_header;
|
||||
if(fc_)
|
||||
fc_->fail(ec);
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_body(boost::optional<
|
||||
std::uint64_t> const& content_length_,
|
||||
error_code& ec)
|
||||
{
|
||||
++got_on_body;
|
||||
got_content_length =
|
||||
static_cast<bool>(content_length_);
|
||||
if(fc_)
|
||||
fc_->fail(ec);
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
std::size_t
|
||||
on_data(string_view s,
|
||||
error_code& ec)
|
||||
{
|
||||
body.append(s.data(), s.size());
|
||||
if(fc_)
|
||||
fc_->fail(ec);
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
return s.size();
|
||||
}
|
||||
|
||||
void
|
||||
on_chunk(std::uint64_t,
|
||||
string_view, error_code& ec)
|
||||
{
|
||||
++got_on_chunk;
|
||||
if(fc_)
|
||||
fc_->fail(ec);
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
void
|
||||
on_complete(error_code& ec)
|
||||
{
|
||||
++got_on_complete;
|
||||
if(fc_)
|
||||
fc_->fail(ec);
|
||||
else
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
};
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
#endif
|
||||
32
test/http/type_traits.cpp
Normal file
32
test/http/type_traits.cpp
Normal file
@@ -0,0 +1,32 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/type_traits.hpp>
|
||||
|
||||
#include <beast/http/empty_body.hpp>
|
||||
#include <string>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
BOOST_STATIC_ASSERT(! is_body_reader<int>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(is_body_reader<empty_body>::value);
|
||||
|
||||
BOOST_STATIC_ASSERT(! is_body_writer<std::string>::value);
|
||||
|
||||
namespace {
|
||||
|
||||
struct not_fields {};
|
||||
|
||||
} // (anonymous)
|
||||
|
||||
BOOST_STATIC_ASSERT(! is_fields<not_fields>::value);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
@@ -6,24 +6,14 @@
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/reason.hpp>
|
||||
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
#include <beast/http/vector_body.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class reason_test : public unit_test::suite
|
||||
{
|
||||
public:
|
||||
void run() override
|
||||
{
|
||||
for(int i = 1; i <= 999; ++i)
|
||||
BEAST_EXPECT(reason_string(i) != nullptr);
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(reason,http,beast);
|
||||
BOOST_STATIC_ASSERT(is_body<vector_body<char>>::value);
|
||||
BOOST_STATIC_ASSERT(is_body_reader<vector_body<char>>::value);
|
||||
BOOST_STATIC_ASSERT(is_body_writer<vector_body<char>>::value);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
128
test/http/verb.cpp
Normal file
128
test/http/verb.cpp
Normal file
@@ -0,0 +1,128 @@
|
||||
//
|
||||
// Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot 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)
|
||||
//
|
||||
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/verb.hpp>
|
||||
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
|
||||
namespace beast {
|
||||
namespace http {
|
||||
|
||||
class verb_test
|
||||
: public beast::unit_test::suite
|
||||
{
|
||||
public:
|
||||
void
|
||||
testVerb()
|
||||
{
|
||||
auto const good =
|
||||
[&](verb v)
|
||||
{
|
||||
BEAST_EXPECT(string_to_verb(to_string(v)) == v);
|
||||
};
|
||||
|
||||
good(verb::unknown);
|
||||
|
||||
good(verb::delete_);
|
||||
good(verb::get);
|
||||
good(verb::head);
|
||||
good(verb::post);
|
||||
good(verb::put);
|
||||
good(verb::connect);
|
||||
good(verb::options);
|
||||
good(verb::trace);
|
||||
good(verb::copy);
|
||||
good(verb::lock);
|
||||
good(verb::mkcol);
|
||||
good(verb::move);
|
||||
good(verb::propfind);
|
||||
good(verb::proppatch);
|
||||
good(verb::search);
|
||||
good(verb::unlock);
|
||||
good(verb::bind);
|
||||
good(verb::rebind);
|
||||
good(verb::unbind);
|
||||
good(verb::acl);
|
||||
good(verb::report);
|
||||
good(verb::mkactivity);
|
||||
good(verb::checkout);
|
||||
good(verb::merge);
|
||||
good(verb::msearch);
|
||||
good(verb::notify);
|
||||
good(verb::subscribe);
|
||||
good(verb::unsubscribe);
|
||||
good(verb::patch);
|
||||
good(verb::purge);
|
||||
good(verb::mkcalendar);
|
||||
good(verb::link);
|
||||
good(verb::unlink);
|
||||
|
||||
auto const bad =
|
||||
[&](string_view s)
|
||||
{
|
||||
auto const v = string_to_verb(s);
|
||||
BEAST_EXPECTS(v == verb::unknown, to_string(v));
|
||||
};
|
||||
|
||||
bad("AC_");
|
||||
bad("BIN_");
|
||||
bad("CHECKOU_");
|
||||
bad("CONNEC_");
|
||||
bad("COP_");
|
||||
bad("DELET_");
|
||||
bad("GE_");
|
||||
bad("HEA_");
|
||||
bad("LIN_");
|
||||
bad("LOC_");
|
||||
bad("M-SEARC_");
|
||||
bad("MERG_");
|
||||
bad("MKACTIVIT_");
|
||||
bad("MKCALENDA_");
|
||||
bad("MKCO_");
|
||||
bad("MOV_");
|
||||
bad("NOTIF_");
|
||||
bad("OPTION_");
|
||||
bad("PATC_");
|
||||
bad("POS_");
|
||||
bad("PROPFIN_");
|
||||
bad("PROPPATC_");
|
||||
bad("PURG_");
|
||||
bad("PU_");
|
||||
bad("REBIN_");
|
||||
bad("REPOR_");
|
||||
bad("SEARC_");
|
||||
bad("SUBSCRIB_");
|
||||
bad("TRAC_");
|
||||
bad("UNBIN_");
|
||||
bad("UNLIN_");
|
||||
bad("UNLOC_");
|
||||
bad("UNSUBSCRIB_");
|
||||
|
||||
try
|
||||
{
|
||||
to_string(static_cast<verb>(-1));
|
||||
fail("", __FILE__, __LINE__);
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
pass();
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
run()
|
||||
{
|
||||
testVerb();
|
||||
}
|
||||
};
|
||||
|
||||
BEAST_DEFINE_TESTSUITE(verb,http,beast);
|
||||
|
||||
} // http
|
||||
} // beast
|
||||
|
||||
@@ -8,15 +8,16 @@
|
||||
// Test that header file is self-contained.
|
||||
#include <beast/http/write.hpp>
|
||||
|
||||
#include <beast/http/buffer_body.hpp>
|
||||
#include <beast/http/fields.hpp>
|
||||
#include <beast/http/message.hpp>
|
||||
#include <beast/http/empty_body.hpp>
|
||||
#include <beast/http/read.hpp>
|
||||
#include <beast/http/string_body.hpp>
|
||||
#include <beast/http/write.hpp>
|
||||
#include <beast/core/error.hpp>
|
||||
#include <beast/core/streambuf.hpp>
|
||||
#include <beast/core/to_string.hpp>
|
||||
#include <beast/core/multi_buffer.hpp>
|
||||
#include <beast/test/fail_stream.hpp>
|
||||
#include <beast/test/pipe_stream.hpp>
|
||||
#include <beast/test/string_istream.hpp>
|
||||
#include <beast/test/string_ostream.hpp>
|
||||
#include <beast/test/yield_to.hpp>
|
||||
#include <beast/unit_test/suite.hpp>
|
||||
@@ -36,60 +37,179 @@ public:
|
||||
{
|
||||
using value_type = std::string;
|
||||
|
||||
class writer
|
||||
class reader
|
||||
{
|
||||
value_type const& body_;
|
||||
|
||||
public:
|
||||
using const_buffers_type =
|
||||
boost::asio::const_buffers_1;
|
||||
|
||||
template<bool isRequest, class Allocator>
|
||||
explicit
|
||||
writer(message<isRequest, unsized_body, Allocator> const& msg) noexcept
|
||||
reader(message<isRequest,
|
||||
unsized_body, Allocator> const& msg)
|
||||
: body_(msg.body)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
init(error_code& ec) noexcept
|
||||
init(error_code& ec)
|
||||
{
|
||||
beast::detail::ignore_unused(ec);
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
template<class WriteFunction>
|
||||
bool
|
||||
write(error_code&, WriteFunction&& wf) noexcept
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(error_code& ec)
|
||||
{
|
||||
wf(boost::asio::buffer(body_));
|
||||
return true;
|
||||
ec.assign(0, ec.category());
|
||||
return {{const_buffers_type{
|
||||
body_.data(), body_.size()}, false}};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template<
|
||||
bool isSplit,
|
||||
bool isFinalEmpty
|
||||
>
|
||||
struct test_body
|
||||
{
|
||||
struct value_type
|
||||
{
|
||||
std::string s;
|
||||
bool mutable read = false;
|
||||
};
|
||||
|
||||
class reader
|
||||
{
|
||||
int step_ = 0;
|
||||
value_type const& body_;
|
||||
|
||||
public:
|
||||
using const_buffers_type =
|
||||
boost::asio::const_buffers_1;
|
||||
|
||||
template<bool isRequest, class Fields>
|
||||
explicit
|
||||
reader(message<isRequest,
|
||||
test_body, Fields> const& msg)
|
||||
: body_(msg.body)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
init(error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
}
|
||||
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(error_code& ec)
|
||||
{
|
||||
ec.assign(0, ec.category());
|
||||
body_.read = true;
|
||||
return get(
|
||||
std::integral_constant<bool, isSplit>{},
|
||||
std::integral_constant<bool, isFinalEmpty>{});
|
||||
}
|
||||
|
||||
private:
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(
|
||||
std::false_type, // isSplit
|
||||
std::false_type) // isFinalEmpty
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
if(body_.s.empty())
|
||||
return boost::none;
|
||||
return {{buffer(body_.s.data(), body_.s.size()), false}};
|
||||
}
|
||||
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(
|
||||
std::false_type, // isSplit
|
||||
std::true_type) // isFinalEmpty
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
if(body_.s.empty())
|
||||
return boost::none;
|
||||
switch(step_)
|
||||
{
|
||||
case 0:
|
||||
step_ = 1;
|
||||
return {{buffer(
|
||||
body_.s.data(), body_.s.size()), true}};
|
||||
default:
|
||||
return boost::none;
|
||||
}
|
||||
}
|
||||
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(
|
||||
std::true_type, // isSplit
|
||||
std::false_type) // isFinalEmpty
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
auto const n = (body_.s.size() + 1) / 2;
|
||||
switch(step_)
|
||||
{
|
||||
case 0:
|
||||
if(n == 0)
|
||||
return boost::none;
|
||||
step_ = 1;
|
||||
return {{buffer(body_.s.data(), n),
|
||||
body_.s.size() > 1}};
|
||||
default:
|
||||
return {{buffer(body_.s.data() + n,
|
||||
body_.s.size() - n), false}};
|
||||
}
|
||||
}
|
||||
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(
|
||||
std::true_type, // isSplit
|
||||
std::true_type) // isFinalEmpty
|
||||
{
|
||||
using boost::asio::buffer;
|
||||
auto const n = (body_.s.size() + 1) / 2;
|
||||
switch(step_)
|
||||
{
|
||||
case 0:
|
||||
if(n == 0)
|
||||
return boost::none;
|
||||
step_ = body_.s.size() > 1 ? 1 : 2;
|
||||
return {{buffer(body_.s.data(), n), true}};
|
||||
case 1:
|
||||
BOOST_ASSERT(body_.s.size() > 1);
|
||||
step_ = 2;
|
||||
return {{buffer(body_.s.data() + n,
|
||||
body_.s.size() - n), true}};
|
||||
default:
|
||||
return boost::none;
|
||||
}
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
struct fail_body
|
||||
{
|
||||
class writer;
|
||||
class reader;
|
||||
|
||||
class value_type
|
||||
{
|
||||
friend class writer;
|
||||
friend class reader;
|
||||
|
||||
std::string s_;
|
||||
test::fail_counter& fc_;
|
||||
boost::asio::io_service& ios_;
|
||||
|
||||
public:
|
||||
value_type(test::fail_counter& fc,
|
||||
boost::asio::io_service& ios)
|
||||
explicit
|
||||
value_type(test::fail_counter& fc)
|
||||
: fc_(fc)
|
||||
, ios_(ios)
|
||||
{
|
||||
}
|
||||
|
||||
boost::asio::io_service&
|
||||
get_io_service() const
|
||||
{
|
||||
return ios_;
|
||||
}
|
||||
|
||||
value_type&
|
||||
operator=(std::string s)
|
||||
{
|
||||
@@ -98,101 +218,88 @@ public:
|
||||
}
|
||||
};
|
||||
|
||||
class writer
|
||||
class reader
|
||||
{
|
||||
std::size_t n_ = 0;
|
||||
value_type const& body_;
|
||||
|
||||
public:
|
||||
using const_buffers_type =
|
||||
boost::asio::const_buffers_1;
|
||||
|
||||
template<bool isRequest, class Allocator>
|
||||
explicit
|
||||
writer(message<isRequest, fail_body, Allocator> const& msg) noexcept
|
||||
reader(message<isRequest,
|
||||
fail_body, Allocator> const& msg)
|
||||
: body_(msg.body)
|
||||
{
|
||||
}
|
||||
|
||||
void
|
||||
init(error_code& ec) noexcept
|
||||
init(error_code& ec)
|
||||
{
|
||||
body_.fc_.fail(ec);
|
||||
}
|
||||
|
||||
template<class WriteFunction>
|
||||
bool
|
||||
write(error_code& ec, WriteFunction&& wf) noexcept
|
||||
boost::optional<std::pair<const_buffers_type, bool>>
|
||||
get(error_code& ec)
|
||||
{
|
||||
if(body_.fc_.fail(ec))
|
||||
return false;
|
||||
return boost::none;
|
||||
if(n_ >= body_.s_.size())
|
||||
return true;
|
||||
wf(boost::asio::buffer(body_.s_.data() + n_, 1));
|
||||
++n_;
|
||||
return n_ == body_.s_.size();
|
||||
return boost::none;
|
||||
return {{const_buffers_type{
|
||||
body_.s_.data() + n_++, 1}, true}};
|
||||
}
|
||||
};
|
||||
};
|
||||
|
||||
template<bool isRequest>
|
||||
bool
|
||||
equal_body(string_view sv, string_view body)
|
||||
{
|
||||
test::string_istream si{
|
||||
get_io_service(), sv.to_string()};
|
||||
message<isRequest, string_body, fields> m;
|
||||
multi_buffer b;
|
||||
try
|
||||
{
|
||||
read(si, b, m);
|
||||
return m.body == body;
|
||||
}
|
||||
catch(std::exception const& e)
|
||||
{
|
||||
log << "equal_body: " << e.what() << std::endl;
|
||||
return false;
|
||||
}
|
||||
}
|
||||
|
||||
template<bool isRequest, class Body, class Fields>
|
||||
std::string
|
||||
str(message<isRequest, Body, Fields> const& m)
|
||||
{
|
||||
test::string_ostream ss(ios_);
|
||||
write(ss, m);
|
||||
error_code ec;
|
||||
write(ss, m, ec);
|
||||
if(ec && ec != error::end_of_stream)
|
||||
BOOST_THROW_EXCEPTION(system_error{ec});
|
||||
return ss.str;
|
||||
}
|
||||
|
||||
void
|
||||
testAsyncWriteHeaders(yield_context do_yield)
|
||||
{
|
||||
{
|
||||
header<true, fields> m;
|
||||
m.version = 11;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.fields.insert("User-Agent", "test");
|
||||
error_code ec;
|
||||
test::string_ostream ss{ios_};
|
||||
async_write(ss, m, do_yield[ec]);
|
||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||
BEAST_EXPECT(ss.str ==
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"\r\n");
|
||||
}
|
||||
{
|
||||
header<false, fields> m;
|
||||
m.version = 10;
|
||||
m.status = 200;
|
||||
m.reason = "OK";
|
||||
m.fields.insert("Server", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
error_code ec;
|
||||
test::string_ostream ss{ios_};
|
||||
async_write(ss, m, do_yield[ec]);
|
||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||
BEAST_EXPECT(ss.str ==
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n");
|
||||
}
|
||||
}
|
||||
|
||||
void
|
||||
testAsyncWrite(yield_context do_yield)
|
||||
{
|
||||
{
|
||||
message<false, string_body, fields> m;
|
||||
response<string_body> m;
|
||||
m.version = 10;
|
||||
m.status = 200;
|
||||
m.reason = "OK";
|
||||
m.fields.insert("Server", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
m.result(status::ok);
|
||||
m.set(field::server, "test");
|
||||
m.set(field::content_length, "5");
|
||||
m.body = "*****";
|
||||
error_code ec;
|
||||
test::string_ostream ss{ios_};
|
||||
async_write(ss, m, do_yield[ec]);
|
||||
if(BEAST_EXPECTS(! ec, ec.message()))
|
||||
if(BEAST_EXPECTS(ec == error::end_of_stream, ec.message()))
|
||||
BEAST_EXPECT(ss.str ==
|
||||
"HTTP/1.0 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
@@ -201,12 +308,11 @@ public:
|
||||
"*****");
|
||||
}
|
||||
{
|
||||
message<false, string_body, fields> m;
|
||||
response<string_body> m;
|
||||
m.version = 11;
|
||||
m.status = 200;
|
||||
m.reason = "OK";
|
||||
m.fields.insert("Server", "test");
|
||||
m.fields.insert("Transfer-Encoding", "chunked");
|
||||
m.result(status::ok);
|
||||
m.set(field::server, "test");
|
||||
m.set(field::transfer_encoding, "chunked");
|
||||
m.body = "*****";
|
||||
error_code ec;
|
||||
test::string_ostream ss(ios_);
|
||||
@@ -234,14 +340,10 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
request<fail_body> m(verb::get, "/", 10, fc);
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::connection, "keep-alive");
|
||||
m.set(field::content_length, "5");
|
||||
m.body = "*****";
|
||||
try
|
||||
{
|
||||
@@ -249,6 +351,7 @@ public:
|
||||
BEAST_EXPECT(fs.next_layer().str ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Connection: keep-alive\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n"
|
||||
"*****"
|
||||
@@ -267,18 +370,13 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Transfer-Encoding", "chunked");
|
||||
request<fail_body> m{verb::get, "/", 10, fc};
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::transfer_encoding, "chunked");
|
||||
m.body = "*****";
|
||||
error_code ec;
|
||||
error_code ec = test::error::fail_error;
|
||||
write(fs, m, ec);
|
||||
if(ec == boost::asio::error::eof)
|
||||
if(ec == error::end_of_stream)
|
||||
{
|
||||
BEAST_EXPECT(fs.next_layer().str ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
@@ -302,18 +400,13 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Transfer-Encoding", "chunked");
|
||||
request<fail_body> m{verb::get, "/", 10, fc};
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::transfer_encoding, "chunked");
|
||||
m.body = "*****";
|
||||
error_code ec;
|
||||
error_code ec = test::error::fail_error;
|
||||
async_write(fs, m, do_yield[ec]);
|
||||
if(ec == boost::asio::error::eof)
|
||||
if(ec == error::end_of_stream)
|
||||
{
|
||||
BEAST_EXPECT(fs.next_layer().str ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
@@ -337,22 +430,19 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
request<fail_body> m{verb::get, "/", 10, fc};
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::connection, "keep-alive");
|
||||
m.set(field::content_length, "5");
|
||||
m.body = "*****";
|
||||
error_code ec;
|
||||
error_code ec = test::error::fail_error;
|
||||
write(fs, m, ec);
|
||||
if(! ec)
|
||||
{
|
||||
BEAST_EXPECT(fs.next_layer().str ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Connection: keep-alive\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n"
|
||||
"*****"
|
||||
@@ -367,22 +457,19 @@ public:
|
||||
test::fail_counter fc(n);
|
||||
test::fail_stream<
|
||||
test::string_ostream> fs(fc, ios_);
|
||||
message<true, fail_body, fields> m(
|
||||
std::piecewise_construct,
|
||||
std::forward_as_tuple(fc, ios_));
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.fields.insert("Content-Length", "5");
|
||||
request<fail_body> m{verb::get, "/", 10, fc};
|
||||
m.set(field::user_agent, "test");
|
||||
m.set(field::connection, "keep-alive");
|
||||
m.set(field::content_length, "5");
|
||||
m.body = "*****";
|
||||
error_code ec;
|
||||
error_code ec = test::error::fail_error;
|
||||
async_write(fs, m, do_yield[ec]);
|
||||
if(! ec)
|
||||
{
|
||||
BEAST_EXPECT(fs.next_layer().str ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Connection: keep-alive\r\n"
|
||||
"Content-Length: 5\r\n"
|
||||
"\r\n"
|
||||
"*****"
|
||||
@@ -398,13 +485,13 @@ public:
|
||||
{
|
||||
// auto content-length HTTP/1.0
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
request<string_body> m;
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
m.prepare_payload();
|
||||
BEAST_EXPECT(str(m) ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
@@ -413,55 +500,19 @@ public:
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// keep-alive HTTP/1.0
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m, connection::keep_alive);
|
||||
BEAST_EXPECT(str(m) ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"Connection: keep-alive\r\n"
|
||||
"\r\n"
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// upgrade HTTP/1.0
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
try
|
||||
{
|
||||
prepare(m, connection::upgrade);
|
||||
fail();
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
pass();
|
||||
}
|
||||
}
|
||||
// no content-length HTTP/1.0
|
||||
{
|
||||
message<true, unsized_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
request<unsized_body> m;
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 10;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
m.prepare_payload();
|
||||
test::string_ostream ss(ios_);
|
||||
error_code ec;
|
||||
write(ss, m, ec);
|
||||
BEAST_EXPECT(ec == boost::asio::error::eof);
|
||||
BEAST_EXPECT(ec == error::end_of_stream);
|
||||
BEAST_EXPECT(ss.str ==
|
||||
"GET / HTTP/1.0\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
@@ -471,13 +522,13 @@ public:
|
||||
}
|
||||
// auto content-length HTTP/1.1
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
request<string_body> m;
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
m.prepare_payload();
|
||||
BEAST_EXPECT(str(m) ==
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
@@ -486,52 +537,15 @@ public:
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// close HTTP/1.1
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m, connection::close);
|
||||
test::string_ostream ss(ios_);
|
||||
error_code ec;
|
||||
write(ss, m, ec);
|
||||
BEAST_EXPECT(ec == boost::asio::error::eof);
|
||||
BEAST_EXPECT(ss.str ==
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Content-Length: 1\r\n"
|
||||
"Connection: close\r\n"
|
||||
"\r\n"
|
||||
"*"
|
||||
);
|
||||
}
|
||||
// upgrade HTTP/1.1
|
||||
{
|
||||
message<true, empty_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
prepare(m, connection::upgrade);
|
||||
BEAST_EXPECT(str(m) ==
|
||||
"GET / HTTP/1.1\r\n"
|
||||
"User-Agent: test\r\n"
|
||||
"Connection: upgrade\r\n"
|
||||
"\r\n"
|
||||
);
|
||||
}
|
||||
// no content-length HTTP/1.1
|
||||
{
|
||||
message<true, unsized_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
request<unsized_body> m;
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
m.prepare_payload();
|
||||
test::string_ostream ss(ios_);
|
||||
error_code ec;
|
||||
write(ss, m, ec);
|
||||
@@ -550,65 +564,14 @@ public:
|
||||
void test_std_ostream()
|
||||
{
|
||||
// Conversion to std::string via operator<<
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
request<string_body> m;
|
||||
m.method(verb::get);
|
||||
m.target("/");
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.set(field::user_agent, "test");
|
||||
m.body = "*";
|
||||
BEAST_EXPECT(boost::lexical_cast<std::string>(m) ==
|
||||
"GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n*");
|
||||
BEAST_EXPECT(boost::lexical_cast<std::string>(m.base()) ==
|
||||
"GET / HTTP/1.1\r\nUser-Agent: test\r\n\r\n");
|
||||
// Cause exceptions in operator<<
|
||||
{
|
||||
std::stringstream ss;
|
||||
ss.setstate(ss.rdstate() |
|
||||
std::stringstream::failbit);
|
||||
try
|
||||
{
|
||||
// header
|
||||
ss << m.base();
|
||||
fail("", __FILE__, __LINE__);
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
pass();
|
||||
}
|
||||
try
|
||||
{
|
||||
// message
|
||||
ss << m;
|
||||
fail("", __FILE__, __LINE__);
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
pass();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void testOstream()
|
||||
{
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
m.url = "/";
|
||||
m.version = 11;
|
||||
m.fields.insert("User-Agent", "test");
|
||||
m.body = "*";
|
||||
prepare(m);
|
||||
std::stringstream ss;
|
||||
ss.setstate(ss.rdstate() |
|
||||
std::stringstream::failbit);
|
||||
try
|
||||
{
|
||||
ss << m;
|
||||
fail();
|
||||
}
|
||||
catch(std::exception const&)
|
||||
{
|
||||
pass();
|
||||
}
|
||||
}
|
||||
|
||||
// Ensure completion handlers are not leaked
|
||||
@@ -631,11 +594,11 @@ public:
|
||||
boost::asio::io_service ios;
|
||||
test::string_ostream os{ios};
|
||||
BEAST_EXPECT(handler::count() == 0);
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
request<string_body> m;
|
||||
m.method(verb::get);
|
||||
m.version = 11;
|
||||
m.url = "/";
|
||||
m.fields["Content-Length"] = "5";
|
||||
m.target("/");
|
||||
m.set("Content-Length", 5);
|
||||
m.body = "*****";
|
||||
async_write(os, m, handler{});
|
||||
BEAST_EXPECT(handler::count() > 0);
|
||||
@@ -653,11 +616,11 @@ public:
|
||||
boost::asio::io_service ios;
|
||||
test::string_ostream is{ios};
|
||||
BEAST_EXPECT(handler::count() == 0);
|
||||
message<true, string_body, fields> m;
|
||||
m.method = "GET";
|
||||
request<string_body> m;
|
||||
m.method(verb::get);
|
||||
m.version = 11;
|
||||
m.url = "/";
|
||||
m.fields["Content-Length"] = "5";
|
||||
m.target("/");
|
||||
m.set("Content-Length", 5);
|
||||
m.body = "*****";
|
||||
async_write(is, m, handler{});
|
||||
BEAST_EXPECT(handler::count() > 0);
|
||||
@@ -666,15 +629,224 @@ public:
|
||||
}
|
||||
}
|
||||
|
||||
template<class Stream,
|
||||
bool isRequest, class Body, class Fields,
|
||||
class Decorator = no_chunk_decorator>
|
||||
void
|
||||
do_write(Stream& stream, message<
|
||||
isRequest, Body, Fields> const& m, error_code& ec,
|
||||
Decorator const& decorator = Decorator{})
|
||||
{
|
||||
serializer<isRequest, Body, Fields, Decorator> sr{m, decorator};
|
||||
for(;;)
|
||||
{
|
||||
stream.nwrite = 0;
|
||||
write_some(stream, sr, ec);
|
||||
if(ec)
|
||||
return;
|
||||
BEAST_EXPECT(stream.nwrite <= 1);
|
||||
if(sr.is_done())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
template<class Stream,
|
||||
bool isRequest, class Body, class Fields,
|
||||
class Decorator = no_chunk_decorator>
|
||||
void
|
||||
do_async_write(Stream& stream,
|
||||
message<isRequest, Body, Fields> const& m,
|
||||
error_code& ec, yield_context yield,
|
||||
Decorator const& decorator = Decorator{})
|
||||
{
|
||||
serializer<isRequest, Body, Fields, Decorator> sr{m, decorator};
|
||||
for(;;)
|
||||
{
|
||||
stream.nwrite = 0;
|
||||
async_write_some(stream, sr, yield[ec]);
|
||||
if(ec)
|
||||
return;
|
||||
BEAST_EXPECT(stream.nwrite <= 1);
|
||||
if(sr.is_done())
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
struct test_decorator
|
||||
{
|
||||
std::string s;
|
||||
|
||||
template<class ConstBufferSequence>
|
||||
string_view
|
||||
operator()(ConstBufferSequence const& buffers)
|
||||
{
|
||||
s = ";x=" + std::to_string(boost::asio::buffer_size(buffers));
|
||||
return s;
|
||||
}
|
||||
|
||||
string_view
|
||||
operator()(boost::asio::null_buffers)
|
||||
{
|
||||
return "Result: OK\r\n";
|
||||
}
|
||||
};
|
||||
|
||||
template<class Body>
|
||||
void
|
||||
testWriteStream(boost::asio::yield_context yield)
|
||||
{
|
||||
test::pipe p{ios_};
|
||||
p.client.write_size(3);
|
||||
|
||||
response<Body> m0;
|
||||
m0.version = 11;
|
||||
m0.result(status::ok);
|
||||
m0.reason("OK");
|
||||
m0.set(field::server, "test");
|
||||
m0.body.s = "Hello, world!\n";
|
||||
|
||||
{
|
||||
std::string const result =
|
||||
"HTTP/1.1 200 OK\r\n"
|
||||
"Server: test\r\n"
|
||||
"\r\n"
|
||||
"Hello, world!\n";
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
do_write(p.client, m, ec);
|
||||
BEAST_EXPECT(p.server.str() == result);
|
||||
BEAST_EXPECT(equal_body<false>(
|
||||
p.server.str(), m.body.s));
|
||||
p.server.clear();
|
||||
}
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
do_async_write(p.client, m, ec, yield);
|
||||
BEAST_EXPECT(p.server.str() == result);
|
||||
BEAST_EXPECT(equal_body<false>(
|
||||
p.server.str(), m.body.s));
|
||||
p.server.clear();
|
||||
}
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
response_serializer<Body, fields> sr{m};
|
||||
sr.split(true);
|
||||
for(;;)
|
||||
{
|
||||
write_some(p.client, sr);
|
||||
if(sr.is_header_done())
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(! m.body.read);
|
||||
p.server.clear();
|
||||
}
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
response_serializer<Body, fields> sr{m};
|
||||
sr.split(true);
|
||||
for(;;)
|
||||
{
|
||||
async_write_some(p.client, sr, yield);
|
||||
if(sr.is_header_done())
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(! m.body.read);
|
||||
p.server.clear();
|
||||
}
|
||||
}
|
||||
{
|
||||
m0.set("Transfer-Encoding", "chunked");
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
do_write(p.client, m, ec);
|
||||
BEAST_EXPECT(equal_body<false>(
|
||||
p.server.str(), m.body.s));
|
||||
p.server.clear();
|
||||
}
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
do_write(p.client, m, ec, test_decorator{});
|
||||
BEAST_EXPECT(equal_body<false>(
|
||||
p.server.str(), m.body.s));
|
||||
p.server.clear();
|
||||
}
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
do_async_write(p.client, m, ec, yield);
|
||||
BEAST_EXPECT(equal_body<false>(
|
||||
p.server.str(), m.body.s));
|
||||
p.server.clear();
|
||||
}
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
do_async_write(p.client, m, ec, yield, test_decorator{});
|
||||
BEAST_EXPECT(equal_body<false>(
|
||||
p.server.str(), m.body.s));
|
||||
p.server.clear();
|
||||
}
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
test::string_ostream so{get_io_service(), 3};
|
||||
response_serializer<Body, fields> sr{m};
|
||||
sr.split(true);
|
||||
for(;;)
|
||||
{
|
||||
write_some(p.client, sr);
|
||||
if(sr.is_header_done())
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(! m.body.read);
|
||||
p.server.clear();
|
||||
}
|
||||
{
|
||||
auto m = m0;
|
||||
error_code ec;
|
||||
response_serializer<Body, fields> sr{m};
|
||||
sr.split(true);
|
||||
for(;;)
|
||||
{
|
||||
async_write_some(p.client, sr, yield);
|
||||
if(sr.is_header_done())
|
||||
break;
|
||||
}
|
||||
BEAST_EXPECT(! m.body.read);
|
||||
p.server.clear();
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
void run() override
|
||||
{
|
||||
yield_to(&write_test::testAsyncWriteHeaders, this);
|
||||
yield_to(&write_test::testAsyncWrite, this);
|
||||
yield_to(&write_test::testFailures, this);
|
||||
yield_to(
|
||||
[&](yield_context yield)
|
||||
{
|
||||
testAsyncWrite(yield);
|
||||
});
|
||||
yield_to(
|
||||
[&](yield_context yield)
|
||||
{
|
||||
testFailures(yield);
|
||||
});
|
||||
testOutput();
|
||||
test_std_ostream();
|
||||
testOstream();
|
||||
testIoService();
|
||||
yield_to(
|
||||
[&](yield_context yield)
|
||||
{
|
||||
testWriteStream<test_body<false, false>>(yield);
|
||||
testWriteStream<test_body<false, true>>(yield);
|
||||
testWriteStream<test_body< true, false>>(yield);
|
||||
testWriteStream<test_body< true, true>>(yield);
|
||||
});
|
||||
}
|
||||
};
|
||||
|
||||
|
||||
Reference in New Issue
Block a user