Refactor chunked-encoding serialization:

New buffer sequence classes are provided to allow full
control over the serialization of chunk-encoded message
payloads:

* chunk_header

    A ConstBufferSequence representing the chunk header.
    It includes a hexadecimal-encoded size, an optional
    set of chunk extensions, and the trailing CRLF
    required to denote the end of the chunk header.

    This allows the caller to manually serialize the chunk
    body in one or more calls to a stream output function.
    The caller must also output an object of type `chunk_crlf`
    to the stream to indicate the end of the chunk body.

* chunk_crlf

    A small ConstBufferSequence that simply represents
    the two character sequence "\r\n" (CRLF). This is needed
    for the case where the caller wants to output a chunk
    body as a series of buffers (i.e. "chunking a chunk").

* chunk_body

    A ConstBufferSequence representing a complete chunk.
    This includes the size, an optional set of chunk extensions,
    a caller provided buffer containing the body, and the
    required CRLF that follows.

* chunk_final

    A ConstBufferSequence representing a final chunk. It
    includes an optional set of caller-provided field trailers

* chunk_extensions

    A container for building a set of chunk extensions to use
    during serialization. The use of the container is optional,
    callers may provide their own buffer containing a correctly
    formatted set of chunk extensions, or they may use their
    own convenience container which meets the requirements.

The basic_fields container is modified to allow construction
outside the context of a message. The container can be used
to provide trailers to `chunk_final`.

Actions Required:

* Remove references to ChunkDecorators. Use the new chunk-encoding
  buffer sequences to manually produce a chunked payload body in
  the case where control over the chunk-extensions and/or trailers
  is required.
This commit is contained in:
Vinnie Falco
2017-07-09 20:09:30 -07:00
parent 02de2e5cbb
commit ff15cf8688
69 changed files with 2312 additions and 686 deletions

View File

@ -6,8 +6,22 @@ Version 80:
* Remove unused file_path
* Add basic_file_body.hpp
* buffers_ref is Assignable
HTTP
* Shrink chunk header buffer sequence size
API Changes:
* Refactor chunked-encoding serialization
Actions Required:
* Remove references to ChunkDecorators. Use the new chunk-encoding
buffer sequences to manually produce a chunked payload body in
the case where control over the chunk-extensions and/or trailers
is required.
--------------------------------------------------------------------------------
Version 79:

View File

@ -1,22 +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)
]
[section:concept Concepts]
This section describes all of the concepts defined by the library.
[include concept/Body.qbk]
[include concept/BodyReader.qbk]
[include concept/BodyWriter.qbk]
[include concept/BufferSequence.qbk]
[include concept/DynamicBuffer.qbk]
[include concept/Fields.qbk]
[include concept/FieldsReader.qbk]
[include concept/File.qbk]
[include concept/Streams.qbk]
[endsect]

View File

@ -50,7 +50,7 @@ explicit callout ;
xml doc
:
0_main.qbk
qbk/00_main.qbk
:
<location>temp
<include>$(broot)/tools/boostbook/dtd

View File

@ -53,16 +53,16 @@
[def __async_initfn__ [@http://www.boost.org/doc/html/boost_asio/reference/asynchronous_operations.html initiating function]]
[def __AsyncStream__ [link beast.concept.streams.AsyncStream [*AsyncStream]]]
[def __Body__ [link beast.concept.Body [*Body]]]
[def __BodyReader__ [link beast.concept.BodyReader [*BodyReader]]]
[def __BodyWriter__ [link beast.concept.BodyWriter [*BodyWriter]]]
[def __DynamicBuffer__ [link beast.concept.DynamicBuffer [*DynamicBuffer]]]
[def __Fields__ [link beast.concept.Fields [*Fields]]]
[def __FieldsReader__ [link beast.concept.FieldsReader [*FieldsReader]]]
[def __File__ [link beast.concept.File [*File]]]
[def __Stream__ [link beast.concept.streams [*Stream]]]
[def __SyncStream__ [link beast.concept.streams.SyncStream [*SyncStream]]]
[def __AsyncStream__ [link beast.concepts.streams.AsyncStream [*AsyncStream]]]
[def __Body__ [link beast.concepts.Body [*Body]]]
[def __BodyReader__ [link beast.concepts.BodyReader [*BodyReader]]]
[def __BodyWriter__ [link beast.concepts.BodyWriter [*BodyWriter]]]
[def __DynamicBuffer__ [link beast.concepts.DynamicBuffer [*DynamicBuffer]]]
[def __Fields__ [link beast.concepts.Fields [*Fields]]]
[def __FieldsReader__ [link beast.concepts.FieldsReader [*FieldsReader]]]
[def __File__ [link beast.concepts.File [*File]]]
[def __Stream__ [link beast.concepts.streams [*Stream]]]
[def __SyncStream__ [link beast.concepts.streams.SyncStream [*SyncStream]]]
[def __basic_fields__ [link beast.ref.beast__http__basic_fields `basic_fields`]]
[def __basic_multi_buffer__ [link beast.ref.beast__basic_multi_buffer `basic_multi_buffer`]]
@ -78,27 +78,27 @@
[def __static_buffer__ [link beast.ref.beast__static_buffer `static_buffer`]]
[def __static_buffer_n__ [link beast.ref.beast__static_buffer_n `static_buffer_n`]]
[import ../example/common/detect_ssl.hpp]
[import ../example/doc/http_examples.hpp]
[import ../example/echo-op/echo_op.cpp]
[import ../example/http-client/http_client.cpp]
[import ../example/websocket-client/websocket_client.cpp]
[import ../../example/common/detect_ssl.hpp]
[import ../../example/doc/http_examples.hpp]
[import ../../example/echo-op/echo_op.cpp]
[import ../../example/http-client/http_client.cpp]
[import ../../example/websocket-client/websocket_client.cpp]
[import ../include/beast/http/basic_file_body.hpp]
[import ../../include/beast/http/basic_file_body.hpp]
[import ../test/exemplars.cpp]
[import ../test/core/doc_snippets.cpp]
[import ../test/http/doc_snippets.cpp]
[import ../test/websocket/doc_snippets.cpp]
[import ../../test/exemplars.cpp]
[import ../../test/core/doc_snippets.cpp]
[import ../../test/http/doc_snippets.cpp]
[import ../../test/websocket/doc_snippets.cpp]
[include 1_intro.qbk]
[include 2_examples.qbk]
[include 3_0_core.qbk]
[include 5_00_http.qbk]
[include 6_0_http_examples.qbk]
[include 7_0_websocket.qbk]
[include 8_concepts.qbk]
[include 9_0_design.qbk]
[include 01_intro.qbk]
[include 02_examples.qbk]
[include 03_core.qbk]
[include 04_http.qbk]
[include 05_http_examples.qbk]
[include 06_websocket.qbk]
[include 07_concepts.qbk]
[include 08_design.qbk]
[section:quickref Reference]
[xinclude quickref.xml]
@ -106,7 +106,7 @@
[block'''<reference id="hidden"><title>This Page Intentionally Left Blank 1/2</title>''']
[section:ref This Page Intentionally Left Blank 2/2]
[include reference.qbk]
[include ../reference.qbk]
[endsect]
[block'''</reference>''']

View File

@ -5,7 +5,7 @@
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
]
[section:intro Introduction]
[section Introduction]
Beast is a C++ header-only library serving as a foundation for writing
interoperable networking libraries by providing [*low-level HTTP/1,

View File

@ -5,13 +5,15 @@
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
]
[section:quickstart Quick Start]
[section Quick Start]
[block'''<?dbhtml stop-chunking?>''']
These complete programs are intended to quickly impress upon readers
the flavor of the library. Source code and build scripts for them are
located in the example/ directory.
[section HTTP Client]
Use HTTP to make a GET request to a website and print the response:
@ -36,7 +38,7 @@ File: [repo_file example/websocket-client/websocket_client.cpp]
[section:examples Examples]
[section Examples]
[block'''<?dbhtml stop-chunking?>''']
Source code and build scripts for these programs are located

View File

@ -5,7 +5,7 @@
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
]
[section:using_io Using I/O]
[section Using I/O]
This library makes I/O primitives used by the implementation publicly
available so users can take advantage of them in their own libraries.
@ -23,11 +23,11 @@ by group, with descriptions.
[snippet_core_1b]
]
[include 3_1_asio.qbk]
[include 3_2_streams.qbk]
[include 3_3_buffers.qbk]
[include 3_4_files.qbk]
[include 3_5_composed.qbk]
[include 3_6_detect_ssl.qbk]
[include 03_core/1_asio.qbk]
[include 03_core/2_streams.qbk]
[include 03_core/3_buffers.qbk]
[include 03_core/4_files.qbk]
[include 03_core/5_composed.qbk]
[include 03_core/6_detect_ssl.qbk]
[endsect]

View File

@ -5,7 +5,7 @@
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
]
[section:files Files]
[section Files]
Often when implementing network algorithms such as servers, it is necessary
to interact with files on the system. Beast defines the __File__ concept

View File

@ -38,7 +38,7 @@ format using __Asio__. Specifically, the library provides:
[link beast.ref.beast__http__async_read_header `async_read_header`], and
[link beast.ref.beast__http__async_read_some `async_read_some`]
read HTTP/1 message data from a
[link beast.concept.streams stream].
[link beast.concepts.streams stream].
]
][
[Stream Writing]
@ -51,7 +51,7 @@ format using __Asio__. Specifically, the library provides:
[link beast.ref.beast__http__async_write_header `async_write_header`], and
[link beast.ref.beast__http__async_write_some `async_write_some`]
write HTTP/1 message data to a
[link beast.concept.streams stream].
[link beast.concepts.streams stream].
]
][
[Serialization]
@ -78,14 +78,15 @@ format using __Asio__. Specifically, the library provides:
[http_snippet_1]
]
[include 5_01_primer.qbk]
[include 5_02_message.qbk]
[include 5_03_streams.qbk]
[include 5_04_serializer_streams.qbk]
[include 5_05_parser_streams.qbk]
[include 5_06_serializer_buffers.qbk]
[include 5_07_parser_buffers.qbk]
[include 5_08_custom_body.qbk]
[include 5_09_custom_parsers.qbk]
[include 04_http/01_primer.qbk]
[include 04_http/02_message.qbk]
[include 04_http/03_streams.qbk]
[include 04_http/04_serializer_streams.qbk]
[include 04_http/05_parser_streams.qbk]
[include 04_http/06_serializer_buffers.qbk]
[include 04_http/07_parser_buffers.qbk]
[include 04_http/08_chunked_encoding.qbk]
[include 04_http/09_custom_body.qbk]
[include 04_http/10_custom_parsers.qbk]
[endsect]

View File

@ -32,8 +32,7 @@ the message to be sent:
template<
bool isRequest,
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator
class Fields = fields
>
class serializer;
```
@ -45,9 +44,9 @@ the message to be sent:
/// A serializer for HTTP/1 requests
template<
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator>
using request_serializer = serializer<true, Body, Fields, ChunkDecorator>;
class Fields = fields
>
using request_serializer = serializer<true, Body, Fields>;
```
]]
[[
@ -57,9 +56,9 @@ the message to be sent:
/// A serializer for HTTP/1 responses
template<
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator>
using response_serializer = serializer<false, Body, Fields, ChunkDecorator>;
class Fields = fields
>
using response_serializer = serializer<false, Body, Fields>;
```
]]
]
@ -110,41 +109,4 @@ synchronously. This performs the same operation as calling `write(stream, m)`:
[http_snippet_12]
[heading Chunk Decorators]
When the message used to construct the serializer indicates the chunked
transfer encoding, the serializer will automatically generate the proper
encoding in the output buffers. __rfc7230__ defines additional fields
called the
[@https://tools.ietf.org/html/rfc7230#section-4.1.1 chunk extensions]
in chunks with body octets, and the
[@https://tools.ietf.org/html/rfc7230#section-4.1.2 chunked trailer part]
for the final chunk. Applications that wish to emit chunk extensions
and trailers may instantiate the serializer with a "chunk decorator" type,
and pass an instance of the type upon construction. This decorator is
a function object which, when invoked with a __ConstBufferSequence__,
returns a
[link beast.ref.beast__string_view `string_view`] containing either the extensions
or the trailer. For chunks containing body data, the passed buffer will
contain one or more corresponding body octets. The decorator may use this
information as needed. For example, to compute a digest on the data and
store it as a chunk extension. For the trailers, the serializer will
invoke the decorator with a buffer sequence of size zero. Or more
specifically, with an object of type
[@http://www.boost.org/doc/html/boost_asio/reference/null_buffers.html `boost::asio::null_buffers`].
For body chunks the string returned by the decorator must follow the
[@https://tools.ietf.org/html/rfc7230#section-4.1.1 correct syntax]
for the entire chunk extension. For the trailer, the returned string
should consist of zero or more lines ending in a CRLF and containing
a field name/value pair in the format prescribed by __rfc7230__. It
is the responsibility of the decorator to manage returned string buffers.
The implementation guarantees it will not reference previous strings
after subsequent calls.
This defines a decorator which sets an extension variable `x` equal
to the size of the chunk in bytes, and returns a single trailer field:
[http_snippet_17]
[endsect]

View File

@ -0,0 +1,147 @@
[/
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)
]
[section Chunked Encoding]
For message payloads whose size is not known ahead of time, HTTP
version 1.1 defines the
[@https://tools.ietf.org/html/rfc7230#section-4.1 ['chunked]]
transfer coding. This coding consists of zero or more
[@https://tools.ietf.org/html/rfc7230#section-4.1 ['chunked bodies]],
followed by a
[@https://tools.ietf.org/html/rfc7230#section-4.1 ['last chunk]].
Each chunked body may contain optional application-defined, connection-specific
[@https://tools.ietf.org/html/rfc7230#section-4.1.1 ['chunk-extensions]].
The last chunk may contain additional HTTP field values in a section
of the last chunk called a
[@https://tools.ietf.org/html/rfc7230#section-4.1.2 ['chunk-trailer]].
The field values are "promised" in the header as a comma delimited list
of field names in the
[@https://tools.ietf.org/html/rfc7230#section-4.4 [*Trailer]]
field value. Clients indicate their willingness to accept trailers
by including the "trailers" token in the
[@https://tools.ietf.org/html/rfc7230#section-4.3 [*TE]]
field value.
[heading Serializing Chunks]
The __serializer__ automatically applies the chunked tranfer encoding
when a message returns `true` from
[link beast.ref.beast__http__message.chunked.overload1 `message::chunked`].
The boundaries between chunks emitted by the serializer are implementation
defined. Chunk extensions and trailers are omitted. Applications which
need precise control over the chunk boundaries, extensions, and trailers
may use a set of helper classes which enable manual emission of message
payloads using chunk encoding.
To use these helper classes, first serialize the header portion of the
message using the standard interface. Then prepare the buffers, chunk
extensions, and desired trailers, and use them with these helpers:
[table Chunking Helpers
[[Name][Description]]
[
[[link beast.ref.beast__http__chunk_body `chunk_body`]]
[
A buffer sequence representing a complete chunk body.
]
][
[[link beast.ref.beast__http__chunk_crlf `chunk_crlf`]]
[
A buffer sequence representing the CRLF (`"\r\n"`) delimiter.
This class is used when the caller desires to emit the
chunk body in two or more individual stream operations.
]
][
[
[link beast.ref.beast__http__chunk_extensions `chunk_extensions`]
[link beast.ref.beast__http__basic_chunk_extensions `basic_chunk_extensions`]
]
[
This is a simple, allocating container which lets callers
easily build up a set of chunk extensions.
]
][
[[link beast.ref.beast__http__chunk_header `chunk_header`]]
[
A buffer sequence representing a hex-encoded chunk size,
followed by an optional set of chunk extensions, including
the terminating CRLF (`"\r\n"`) delimiter which precedes
the chunk body. This class is used when the caller desires
to emit the chunk body in two or more individual stream
operations.
]
][
[[link beast.ref.beast__http__chunk_last `chunk_last`]]
[
A buffer sequence representing a last chunk. The last chunk
indicates the end of the chunked message payload, and may
contain optional trailer fields.
]
][
[
[link beast.ref.beast__http__make_chunk `make_chunk`]
[link beast.ref.beast__http__make_chunk_last `make_chunk_last`]
]
[
These helper functions are used to construct a chunk
or last chunk directly at call sites.
]
]]
We demonstrate the use of these objects first by declaring a function
which returns the next buffer sequence to use as a chunk body:
[http_snippet_17]
This example demonstrates sending a complete chunked message payload
manually. No chunk extensions or trailers are emitted:
[http_snippet_18]
The following code sends additional chunks, and sets chunk extensions
using the helper container. The container automatically quotes values
in the serialized output when necessary:
[http_snippet_19]
Callers can take over the generation and management of the extensions
buffer by passing a non-owning string. Note that this requires the
string contents to adhere to the correct syntax for chunk extensions,
including the needed double quotes for values which contain spaces:
[http_snippet_20]
The next code sample emits a chunked response which promises two
trailer fields and delivers them in the last chunk. The implementation
allocates memory using the default or a passed-in allocator to hold
the state information required to serialize the trailer:
[http_snippet_21]
Using a custom allocator to serialize the last chunk:
[http_snippet_22]
Alternatively, callers can take over the generation and lifetime
management of the serialized trailer fields by passing in a non-owning
string:
[http_snippet_23]
For the ultimate level of control, a caller can manually compose the
chunk itself by first emitting a header with the correct chunk body
size, and then by emitting the chunk body in multiple calls to the
stream write function. In this case the caller is responsible for
also emitting the terminating CRLF (`"\r\n"`):
[http_snippet_24]
[endsect]

View File

@ -27,12 +27,12 @@ Boost.Asio with a consistent asynchronous model using a modern C++ approach.
[ws_snippet_1]
]
[include 7_1_streams.qbk]
[include 7_2_connect.qbk]
[include 7_3_client.qbk]
[include 7_4_server.qbk]
[include 7_5_messages.qbk]
[include 7_6_control.qbk]
[include 7_7_notes.qbk]
[include 06_websocket/1_streams.qbk]
[include 06_websocket/2_connect.qbk]
[include 06_websocket/3_client.qbk]
[include 06_websocket/4_server.qbk]
[include 06_websocket/5_messages.qbk]
[include 06_websocket/6_control.qbk]
[include 06_websocket/7_notes.qbk]
[endsect]

22
doc/qbk/07_concepts.qbk Normal file
View File

@ -0,0 +1,22 @@
[/
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)
]
[section:concepts Concepts]
This section describes all of the concepts defined by the library.
[include 07_concepts/Body.qbk]
[include 07_concepts/BodyReader.qbk]
[include 07_concepts/BodyWriter.qbk]
[include 07_concepts/BufferSequence.qbk]
[include 07_concepts/DynamicBuffer.qbk]
[include 07_concepts/Fields.qbk]
[include 07_concepts/FieldsReader.qbk]
[include 07_concepts/File.qbk]
[include 07_concepts/Streams.qbk]
[endsect]

View File

@ -5,7 +5,7 @@
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
]
[section:streams Stream]
[section:streams Streams]
Stream types represent objects capable of performing synchronous or
asynchronous I/O. They are based on concepts from `boost::asio`.

View File

@ -50,9 +50,9 @@ start. Other design goals:
* Allow for customizations, if the user needs it.
[include 9_1_http_message.qbk]
[include 9_2_http_comparison.qbk]
[include 9_3_websocket_zaphoyd.qbk]
[include 9_4_faq.qbk]
[include 08_design/1_http_message.qbk]
[include 08_design/2_http_comparison.qbk]
[include 08_design/3_websocket_zaphoyd.qbk]
[include 08_design/4_faq.qbk]
[endsect]

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" "boostbook.dtd">
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" "../boostbook.dtd">
<!--
Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)

View File

@ -1,5 +1,5 @@
<?xml version="1.0" encoding="utf-8"?>
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" "boostbook.dtd">
<!DOCTYPE library PUBLIC "-//Boost//DTD BoostBook XML V1.0//EN" "../boostbook.dtd">
<!--
Copyright (c) 2013-2017 Vinnie Falco (vinnie dot falco at gmail dot com)
@ -29,12 +29,18 @@
<entry valign="top">
<bridgehead renderas="sect3">Classes</bridgehead>
<simplelist type="vert" columns="1">
<member><link linkend="beast.ref.beast__http__basic_chunk_extensions">basic_chunk_extensions</link></member>
<member><link linkend="beast.ref.beast__http__basic_dynamic_body">basic_dynamic_body</link></member>
<member><link linkend="beast.ref.beast__http__basic_fields">basic_fields</link></member>
<member><link linkend="beast.ref.beast__http__basic_file_body">basic_file_body</link></member>
<member><link linkend="beast.ref.beast__http__basic_parser">basic_parser</link></member>
<member><link linkend="beast.ref.beast__http__basic_string_body">basic_string_body</link></member>
<member><link linkend="beast.ref.beast__http__buffer_body">buffer_body</link></member>
<member><link linkend="beast.ref.beast__http__chunk_body">chunk_body</link></member>
<member><link linkend="beast.ref.beast__http__chunk_crlf">chunk_crlf</link></member>
<member><link linkend="beast.ref.beast__http__chunk_extensions">chunk_extensions</link></member>
<member><link linkend="beast.ref.beast__http__chunk_header">chunk_header</link></member>
<member><link linkend="beast.ref.beast__http__chunk_last">chunk_last</link></member>
<member><link linkend="beast.ref.beast__http__dynamic_body">dynamic_body</link></member>
<member><link linkend="beast.ref.beast__http__empty_body">empty_body</link></member>
<member><link linkend="beast.ref.beast__http__fields">fields</link></member>
@ -42,7 +48,6 @@
<member><link linkend="beast.ref.beast__http__header">header</link></member>
<member><link linkend="beast.ref.beast__http__message">message</link></member>
<member><link linkend="beast.ref.beast__http__parser">parser</link></member>
<member><link linkend="beast.ref.beast__http__no_chunk_decorator">no_chunk_decorator</link></member>
<member><link linkend="beast.ref.beast__http__request">request</link></member>
<member><link linkend="beast.ref.beast__http__request_header">request_header</link></member>
<member><link linkend="beast.ref.beast__http__request_parser">request_parser</link></member>
@ -67,6 +72,8 @@
<member><link linkend="beast.ref.beast__http__async_write_header">async_write_header</link></member>
<member><link linkend="beast.ref.beast__http__async_write_some">async_write_some</link></member>
<member><link linkend="beast.ref.beast__http__int_to_status">int_to_status</link></member>
<member><link linkend="beast.ref.beast__http__make_chunk">make_chunk</link></member>
<member><link linkend="beast.ref.beast__http__make_chunk_last">make_chunk_last</link></member>
<member><link linkend="beast.ref.beast__http__obsolete_reason">obsolete_reason</link></member>
<member><link linkend="beast.ref.beast__http__operator_lt__lt_">operator&lt;&lt;</link></member>
<member><link linkend="beast.ref.beast__http__read">read</link></member>
@ -107,11 +114,11 @@
</simplelist>
<bridgehead renderas="sect3">Concepts</bridgehead>
<simplelist type="vert" columns="1">
<member><link linkend="beast.concept.Body">Body</link></member>
<member><link linkend="beast.concept.BodyReader">BodyReader</link></member>
<member><link linkend="beast.concept.BodyWriter">BodyWriter</link></member>
<member><link linkend="beast.concept.Fields">Fields</link></member>
<member><link linkend="beast.concept.FieldsReader">FieldsReader</link></member>
<member><link linkend="beast.concepts.Body">Body</link></member>
<member><link linkend="beast.concepts.BodyReader">BodyReader</link></member>
<member><link linkend="beast.concepts.BodyWriter">BodyWriter</link></member>
<member><link linkend="beast.concepts.Fields">Fields</link></member>
<member><link linkend="beast.concepts.FieldsReader">FieldsReader</link></member>
</simplelist>
</entry>
<entry valign="top">
@ -245,12 +252,12 @@
<entry valign="top">
<bridgehead renderas="sect3">Concepts</bridgehead>
<simplelist type="vert" columns="1">
<member><link linkend="beast.concept.streams.AsyncStream">AsyncStream</link></member>
<member><link linkend="beast.concept.BufferSequence">BufferSequence</link></member>
<member><link linkend="beast.concept.DynamicBuffer">DynamicBuffer</link></member>
<member><link linkend="beast.concept.File">File</link></member>
<member><link linkend="beast.concept.streams.Stream">Stream</link></member>
<member><link linkend="beast.concept.streams.SyncStream">SyncStream</link></member>
<member><link linkend="beast.concepts.streams.AsyncStream">AsyncStream</link></member>
<member><link linkend="beast.concepts.BufferSequence">BufferSequence</link></member>
<member><link linkend="beast.concepts.DynamicBuffer">DynamicBuffer</link></member>
<member><link linkend="beast.concepts.File">File</link></member>
<member><link linkend="beast.concepts.streams.Stream">Stream</link></member>
<member><link linkend="beast.concepts.streams.SyncStream">SyncStream</link></member>
</simplelist>
</entry>
<entry valign="top">

View File

@ -1,6 +1,6 @@
<!-- CLASS_DETAIL_TEMPLATE BEGIN -->
<xsl:when test="type = 'class AsyncStream'">
<xsl:text>class ``[link beast.concept.streams.AsyncStream [*AsyncStream]]``</xsl:text>
<xsl:text>class ``[link beast.concepts.streams.AsyncStream [*AsyncStream]]``</xsl:text>
</xsl:when>
<xsl:when test="type = 'class AsyncReadStream'">
<xsl:text>class __AsyncReadStream__</xsl:text>
@ -9,14 +9,14 @@
<xsl:text>class __AsyncWriteStream__</xsl:text>
</xsl:when>
<xsl:when test="type = 'class Body'">
<xsl:text>class ``[link beast.concept.Body [*Body]]``</xsl:text>
<xsl:text>class ``[link beast.concepts.Body [*Body]]``</xsl:text>
</xsl:when>
<xsl:when test="type = 'class BufferSequence'">
<xsl:text>class ``[link beast.concept.BufferSequence [*BufferSequence]]``</xsl:text>
<xsl:text>class ``[link beast.concepts.BufferSequence [*BufferSequence]]``</xsl:text>
</xsl:when>
<xsl:when test="(type = 'class' or type = 'class...') and declname = 'BufferSequence'">
<xsl:value-of select="type"/>
<xsl:text> ``[link beast.concept.BufferSequence [*BufferSequence]]``</xsl:text>
<xsl:text> ``[link beast.concepts.BufferSequence [*BufferSequence]]``</xsl:text>
</xsl:when>
<xsl:when test="declname = 'CompletionHandler' or type = 'class CompletionHandler'">
<xsl:text>class __CompletionHandler__</xsl:text>
@ -25,10 +25,10 @@
<xsl:text>class __ConstBufferSequence__</xsl:text>
</xsl:when>
<xsl:when test="declname = 'DynamicBuffer' or type = 'class DynamicBuffer'">
<xsl:text>class ``[link beast.concept.DynamicBuffer [*DynamicBuffer]]``</xsl:text>
<xsl:text>class ``[link beast.concepts.DynamicBuffer [*DynamicBuffer]]``</xsl:text>
</xsl:when>
<xsl:when test="type = 'class Fields' or substring(type, 1, 13) = 'class Fields '">
<xsl:text>class ``[link beast.concept.Fields [*Fields]]``</xsl:text>
<xsl:text>class ``[link beast.concepts.Fields [*Fields]]``</xsl:text>
</xsl:when>
<xsl:when test="declname = 'Handler' or type = 'class Handler'">
<xsl:text>class __Handler__</xsl:text>
@ -37,10 +37,10 @@
<xsl:text>class __MutableBufferSequence__</xsl:text>
</xsl:when>
<xsl:when test="declname = 'Stream' or type = 'class Stream'">
<xsl:text>class ``[link beast.concept.streams.Stream [*Stream]]``</xsl:text>
<xsl:text>class ``[link beast.concepts.streams.Stream [*Stream]]``</xsl:text>
</xsl:when>
<xsl:when test="type = 'class SyncStream'">
<xsl:text>class ``[link beast.concept.streams.SyncStream [*SyncStream]]``</xsl:text>
<xsl:text>class ``[link beast.concepts.streams.SyncStream [*SyncStream]]``</xsl:text>
</xsl:when>
<xsl:when test="declname = 'SyncReadStream' or type = 'class SyncReadStream'">
<xsl:text>class __SyncReadStream__</xsl:text>

View File

@ -170,9 +170,9 @@ public:
`async_write_some` function, and is known as a <em>composed operation</em>.
The program must ensure that the stream performs no other write operations
until this operation completes. The algorithm will use a temporary
@ref serializer with an empty chunk decorator to produce buffers. If
the semantics of the message indicate that the connection should be
closed after the message is sent, the error delivered by this function
@ref serializer to produce buffers. If the semantics of the message
indicate that the connection should be closed after the message is sent,
the error delivered by this function
will be @ref error::end_of_stream
@param stream The stream to which the data is to be written.

View File

@ -13,6 +13,7 @@
#include <beast/http/basic_dynamic_body.hpp>
#include <beast/http/basic_parser.hpp>
#include <beast/http/buffer_body.hpp>
#include <beast/http/chunk_encode.hpp>
#include <beast/http/dynamic_body.hpp>
#include <beast/http/empty_body.hpp>
#include <beast/http/error.hpp>

View File

@ -0,0 +1,698 @@
//
// 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_CHUNK_ENCODE_HPP
#define BEAST_HTTP_CHUNK_ENCODE_HPP
#include <beast/config.hpp>
#include <beast/core/buffer_cat.hpp>
#include <beast/core/string.hpp>
#include <beast/http/type_traits.hpp>
#include <beast/http/detail/chunk_encode.hpp>
#include <boost/asio/buffer.hpp>
#include <memory>
#include <type_traits>
namespace beast {
namespace http {
/** A chunked encoding crlf
This implements a @b ConstBufferSequence holding the CRLF
(`"\r\n"`) used as a delimiter in a @em chunk.
To use this class, pass an instance of it to a
stream algorithm as the buffer sequence:
@code
// writes "\r\n"
boost::asio::write(stream, chunk_crlf{});
@endcode
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
struct chunk_crlf
{
/// Constructor
chunk_crlf() = default;
//-----
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using value_type = implementation_defined;
#else
using value_type = detail::chunk_crlf_iter::value_type;
#endif
/// Required for @b ConstBufferSequence
using const_iterator = value_type const*;
/// Required for @b ConstBufferSequence
chunk_crlf(chunk_crlf const&) = default;
/// Required for @b ConstBufferSequence
const_iterator
begin() const
{
return &detail::chunk_crlf_iter::value;
}
/// Required for @b ConstBufferSequence
const_iterator
end() const
{
return begin() + 1;
}
};
//------------------------------------------------------------------------------
/** A @em chunk header
This implements a @b ConstBufferSequence representing the
header of a @em chunk. The serialized format is as follows:
@code
chunk-header = 1*HEXDIG chunk-ext CRLF
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
@endcode
The chunk extension is optional. After the header and
chunk body have been serialized, it is the callers
responsibility to also serialize the final CRLF (`"\r\n"`).
This class allows the caller to emit piecewise chunk bodies,
by first serializing the chunk header using this class and then
serializing the chunk body in a series of one or more calls to
a stream write operation.
To use this class, pass an instance of it to a
stream algorithm as the buffer sequence:
@code
// writes "400;x\r\n"
boost::asio::write(stream, chunk_header{1024, "x"});
@endcode
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
class chunk_header
{
using view_type = buffer_cat_view<
detail::chunk_size, // chunk-size
boost::asio::const_buffers_1, // chunk-extensions
chunk_crlf>; // CRLF
std::shared_ptr<
detail::chunk_extensions> exts_;
view_type view_;
public:
/** Constructor
This constructs a buffer sequence representing a
@em chunked-body size and terminating CRLF (`"\r\n"`)
with no chunk extensions.
@param size The size of the chunk body that follows.
The value must be greater than zero.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
explicit
chunk_header(std::size_t size);
/** Constructor
This constructs a buffer sequence representing a
@em chunked-body size and terminating CRLF (`"\r\n"`)
with provided chunk extensions.
@param size The size of the chunk body that follows.
The value must be greater than zero.
@param extensions The chunk extensions string. This
string must be formatted correctly as per rfc7230,
using this BNF syntax:
@code
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
@endcode
The data pointed to by this string view must remain
valid for the lifetime of any operations performed on
the object.
@see https://tools.ietf.org/html/rfc7230#section-4.1.1
*/
chunk_header(
std::size_t size,
string_view extensions);
/** Constructor
This constructs a buffer sequence representing a
@em chunked-body size and terminating CRLF (`"\r\n"`)
with provided chunk extensions.
The default allocator is used to provide storage for the
extensions object.
@param size The size of the chunk body that follows.
The value must be greater than zero.
@param extensions The chunk extensions object. The expression
`extensions.str()` must be valid, and the return type must
be convertible to @ref string_view. This object will be copied
or moved as needed to ensure that the chunk header object retains
ownership of the buffers provided by the chunk extensions object.
@note This function participates in overload resolution only
if @b ChunkExtensions meets the requirements stated above.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ChunkExtensions
#if ! BEAST_DOXYGEN
, class = typename std::enable_if<
detail::is_chunk_extensions<
ChunkExtensions>::value>::type
#endif
>
chunk_header(
std::size_t size,
ChunkExtensions&& extensions);
/** Constructor
This constructs a buffer sequence representing a
@em chunked-body size and terminating CRLF (`"\r\n"`)
with provided chunk extensions.
The specified allocator is used to provide storage for the
extensions object.
@param size The size of the chunk body that follows.
The value be greater than zero.
@param extensions The chunk extensions object. The expression
`extensions.str()` must be valid, and the return type must
be convertible to @ref string_view. This object will be copied
or moved as needed to ensure that the chunk header object retains
ownership of the buffers provided by the chunk extensions object.
@param allocator The allocator to provide storage for the moved
or copied extensions object.
@note This function participates in overload resolution only
if @b ChunkExtensions meets the requirements stated above.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ChunkExtensions, class Allocator
#if ! BEAST_DOXYGEN
, class = typename std::enable_if<
detail::is_chunk_extensions<
ChunkExtensions>::value>::type
#endif
>
chunk_header(
std::size_t size,
ChunkExtensions&& extensions,
Allocator const& allocator);
//-----
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using value_type = implementation_defined;
#else
using value_type = typename view_type::value_type;
#endif
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using const_iterator = implementation_defined;
#else
using const_iterator = typename view_type::const_iterator;
#endif
/// Required for @b ConstBufferSequence
chunk_header(chunk_header const&) = default;
/// Required for @b ConstBufferSequence
const_iterator
begin() const
{
return view_.begin();
}
/// Required for @b ConstBufferSequence
const_iterator
end() const
{
return view_.end();
}
};
//------------------------------------------------------------------------------
/** A @em chunk
This implements a @b ConstBufferSequence representing
a @em chunk. The serialized format is as follows:
@code
chunk = chunk-size [ chunk-ext ] CRLF chunk-data CRLF
chunk-size = 1*HEXDIG
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
chunk-data = 1*OCTET ; a sequence of chunk-size octets
@endcode
The chunk extension is optional.
To use this class, pass an instance of it to a
stream algorithm as the buffer sequence.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ConstBufferSequence>
class chunk_body
{
using view_type = buffer_cat_view<
detail::chunk_size, // chunk-size
boost::asio::const_buffers_1, // chunk-extensions
chunk_crlf, // CRLF
ConstBufferSequence, // chunk-body
chunk_crlf>; // CRLF
std::shared_ptr<
detail::chunk_extensions> exts_;
view_type view_;
public:
/** Constructor
This constructs buffers representing a complete @em chunk
with no chunk extensions and having the size and contents
of the specified buffer sequence.
@param buffers A buffer sequence representing the chunk
body. Although the buffers object may be copied as necessary,
ownership of the underlying memory blocks is retained by the
caller, which must guarantee that they remain valid while this
object is in use.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
explicit
chunk_body(
ConstBufferSequence const& buffers);
/** Constructor
This constructs buffers representing a complete @em chunk
with the passed chunk extensions and having the size and
contents of the specified buffer sequence.
@param buffers A buffer sequence representing the chunk
body. Although the buffers object may be copied as necessary,
ownership of the underlying memory blocks is retained by the
caller, which must guarantee that they remain valid while this
object is in use.
@param extensions The chunk extensions string. This
string must be formatted correctly as per rfc7230,
using this BNF syntax:
@code
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
@endcode
The data pointed to by this string view must remain
valid for the lifetime of any operations performed on
the object.
@see https://tools.ietf.org/html/rfc7230#section-4.1.1
*/
chunk_body(
ConstBufferSequence const& buffers,
string_view extensions);
/** Constructor
This constructs buffers representing a complete @em chunk
with the passed chunk extensions and having the size and
contents of the specified buffer sequence.
The default allocator is used to provide storage for the
extensions object.
@param buffers A buffer sequence representing the chunk
body. Although the buffers object may be copied as necessary,
ownership of the underlying memory blocks is retained by the
caller, which must guarantee that they remain valid while this
object is in use.
@param extensions The chunk extensions object. The expression
`extensions.str()` must be valid, and the return type must
be convertible to @ref string_view. This object will be copied
or moved as needed to ensure that the chunk header object retains
ownership of the buffers provided by the chunk extensions object.
@note This function participates in overload resolution only
if @b ChunkExtensions meets the requirements stated above.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ChunkExtensions
#if ! BEAST_DOXYGEN
, class = typename std::enable_if<
! std::is_convertible<typename std::decay<
ChunkExtensions>::type, string_view>::value>::type
#endif
>
chunk_body(
ConstBufferSequence const& buffers,
ChunkExtensions&& extensions);
/** Constructor
This constructs buffers representing a complete @em chunk
with the passed chunk extensions and having the size and
contents of the specified buffer sequence.
The specified allocator is used to provide storage for the
extensions object.
@param buffers A buffer sequence representing the chunk
body. Although the buffers object may be copied as necessary,
ownership of the underlying memory blocks is retained by the
caller, which must guarantee that they remain valid while this
object is in use.
@param extensions The chunk extensions object. The expression
`extensions.str()` must be valid, and the return type must
be convertible to @ref string_view. This object will be copied
or moved as needed to ensure that the chunk header object retains
ownership of the buffers provided by the chunk extensions object.
@param allocator The allocator to provide storage for the moved
or copied extensions object.
@note This function participates in overload resolution only
if @b ChunkExtensions meets the requirements stated above.
@see https://tools.ietf.org/html/rfc7230#section-4.1
*/
template<class ChunkExtensions, class Allocator
#if ! BEAST_DOXYGEN
, class = typename std::enable_if<
! std::is_convertible<typename std::decay<
ChunkExtensions>::type, string_view>::value>::type
#endif
>
chunk_body(
ConstBufferSequence const& buffers,
ChunkExtensions&& extensions,
Allocator const& allocator);
//-----
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using value_type = implementation_defined;
#else
using value_type = typename view_type::value_type;
#endif
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using const_iterator = implementation_defined;
#else
using const_iterator = typename view_type::const_iterator;
#endif
/// Required for @b ConstBufferSequence
const_iterator
begin() const
{
return view_.begin();
}
/// Required for @b ConstBufferSequence
const_iterator
end() const
{
return view_.end();
}
};
//------------------------------------------------------------------------------
/** A chunked-encoding last chunk
*/
template<class Trailer = chunk_crlf>
class chunk_last
{
static_assert(
is_fields<Trailer>::value ||
is_const_buffer_sequence<Trailer>::value,
"Trailer requirements not met");
using buffers_type = typename
detail::buffers_or_fields<Trailer>::type;
using view_type =
buffer_cat_view<
detail::chunk_size0, // "0\r\n"
buffers_type>; // Trailer (includes CRLF)
template<class Allocator>
buffers_type
prepare(Trailer const& trailer, Allocator const& alloc);
buffers_type
prepare(Trailer const& trailer, std::true_type);
buffers_type
prepare(Trailer const& trailer, std::false_type);
std::shared_ptr<void> sp_;
view_type view_;
public:
/** Constructor
The last chunk will have an empty trailer
*/
chunk_last();
/** Constructor
@param trailer The trailer to use. This may be
a type meeting the requirements of either Fields
or ConstBufferSequence. If it is a ConstBufferSequence,
the trailer must be formatted correctly as per rfc7230
including a CRLF on its own line to denote the end
of the trailer.
*/
explicit
chunk_last(Trailer const& trailer);
/** Constructor
@param trailer The trailer to use. This type must
meet the requirements of Fields.
@param allocator The allocator to use for storing temporary
data associated with the serialized trailer buffers.
*/
#if BEAST_DOXYGEN
template<class Allocator>
chunk_last(Trailer const& trailer, Allocator const& allocator);
#else
template<class DeducedTrailer, class Allocator,
class = typename std::enable_if<
is_fields<DeducedTrailer>::value>::type>
chunk_last(
DeducedTrailer const& trailer, Allocator const& allocator);
#endif
//-----
/// Required for @b ConstBufferSequence
chunk_last(chunk_last const&) = default;
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using value_type = implementation_defined;
#else
using value_type =
typename view_type::value_type;
#endif
/// Required for @b ConstBufferSequence
#if BEAST_DOXYGEN
using const_iterator = implementation_defined;
#else
using const_iterator =
typename view_type::const_iterator;
#endif
/// Required for @b ConstBufferSequence
const_iterator
begin() const
{
return view_.begin();
}
/// Required for @b ConstBufferSequence
const_iterator
end() const
{
return view_.end();
}
};
//------------------------------------------------------------------------------
/** A set of chunk extensions
This container stores a set of chunk extensions suited
for use with @ref chunk_header and @ref chunk_body.
Meets the requirements of ChunkExtensions
*/
template<class Allocator>
class basic_chunk_extensions
{
std::basic_string<char,
std::char_traits<char>, Allocator> s_;
public:
/// Constructor
basic_chunk_extensions() = default;
/// Constructor
basic_chunk_extensions(basic_chunk_extensions&&) = default;
/// Constructor
basic_chunk_extensions(basic_chunk_extensions const&) = default;
/** Constructor
@param allocator The allocator to use for storing the serialized extension
*/
explicit
basic_chunk_extensions(Allocator const& allocator)
: s_(allocator)
{
}
/** Clear the chunk extensions
This preserves the capacity of the internal string
used to hold the serialized representation.
*/
void
clear()
{
s_.clear();
}
/** Insert an extension name with an empty value
@param name The name of the extension
*/
void
insert(string_view name);
/** Insert an extension value
@param name The name of the extension
@param value The value to insert. Depending on the
contents, the serialized extension may use a quoted string.
*/
void
insert(string_view name, string_view value);
/// Return the serialized representation of the chunk extension
string_view
str() const
{
return s_;
}
};
//------------------------------------------------------------------------------
/// A set of chunk extensions
using chunk_extensions =
basic_chunk_extensions<std::allocator<char>>;
/** Returns a @ref chunk_body
This functions constructs and returns a complete
@ref chunk_body for a chunk body represented by the
specified buffer sequence.
@param buffers The buffers representing the chunk body.
@param args Optional arguments passed to the @ref chunk_body constructor.
@note This function is provided as a notational convenience
to omit specification of the class template arguments.
*/
template<class ConstBufferSequence, class... Args>
auto
make_chunk(
ConstBufferSequence const& buffers,
Args&&... args) ->
chunk_body<ConstBufferSequence>
{
return chunk_body<ConstBufferSequence>(
buffers, std::forward<Args>(args)...);
}
/** Returns a @ref chunk_last
@note This function is provided as a notational convenience
to omit specification of the class template arguments.
*/
inline
chunk_last<chunk_crlf>
make_chunk_last()
{
return chunk_last<chunk_crlf>{};
}
/** Returns a @ref chunk_last
This function construct and returns a complete
@ref chunk_last for a last chunk containing the
specified trailers.
@param trailer A ConstBufferSequence or
@note This function is provided as a notational convenience
to omit specification of the class template arguments.
@param args Optional arguments passed to the @ref chunk_last
constructor.
*/
template<class Trailer, class... Args>
chunk_last<Trailer>
make_chunk_last(
Trailer const& trailer,
Args&&... args)
{
return chunk_last<Trailer>{
trailer, std::forward<Args>(args)...};
}
} // http
} // beast
#include <beast/http/impl/chunk_encode.ipp>
#endif

View File

@ -8,6 +8,8 @@
#ifndef BEAST_HTTP_DETAIL_CHUNK_ENCODE_HPP
#define BEAST_HTTP_DETAIL_CHUNK_ENCODE_HPP
#include <beast/core/type_traits.hpp>
#include <beast/http/type_traits.hpp>
#include <boost/asio/buffer.hpp>
#include <algorithm>
#include <array>
@ -17,15 +19,56 @@ namespace beast {
namespace http {
namespace detail {
struct chunk_extensions
{
virtual ~chunk_extensions() = default;
virtual boost::asio::const_buffers_1 str() = 0;
};
template<class ChunkExtensions>
struct chunk_extensions_impl : chunk_extensions
{
ChunkExtensions ext_;
chunk_extensions_impl(ChunkExtensions&& ext)
: ext_(std::move(ext))
{
}
chunk_extensions_impl(ChunkExtensions const& ext)
: ext_(ext)
{
}
boost::asio::const_buffers_1
str() override
{
auto const s = ext_.str();
return {s.data(), s.size()};
}
};
template<class T, class = void>
struct is_chunk_extensions : std::false_type {};
template<class T>
struct is_chunk_extensions<T, beast::detail::void_t<decltype(
std::declval<string_view&>() = std::declval<T&>().str(),
(void)0)>> : std::true_type
{
};
//------------------------------------------------------------------------------
/** A buffer sequence containing a chunk-encoding header
*/
class chunk_header
class chunk_size
{
public:
// Storage for the longest hex string we might need
class value_type
{
friend class chunk_header;
friend class chunk_size;
// First byte holds the length
char buf_[1 + 2 * sizeof(std::size_t)];
@ -63,14 +106,13 @@ public:
using const_iterator = value_type const*;
chunk_header(chunk_header const& other) = default;
chunk_size(chunk_size const& other) = default;
/** Construct a chunk header
@param n The number of octets in this chunk.
*/
explicit
chunk_header(std::size_t n)
chunk_size(std::size_t n)
{
value_.prepare(n);
}
@ -93,7 +135,7 @@ private:
template<class>
void
chunk_header::
chunk_size::
value_type::
prepare(std::size_t n)
{
@ -102,6 +144,8 @@ prepare(std::size_t n)
buf_[0] = static_cast<char>(last - it);
}
//------------------------------------------------------------------------------
/// Returns a buffer sequence holding a CRLF for chunk encoding
inline
boost::asio::const_buffers_1
@ -113,11 +157,100 @@ chunk_crlf()
/// Returns a buffer sequence holding a final chunk header
inline
boost::asio::const_buffers_1
chunk_final()
chunk_last()
{
return {"0\r\n", 3};
}
//------------------------------------------------------------------------------
template<class = void>
struct chunk_crlf_iter_type
{
class value_type
{
char const s[2] = {'\r', '\n'};
public:
value_type() = default;
operator
boost::asio::const_buffer() const
{
return {s, sizeof(s)};
}
};
static value_type value;
};
template<class T>
typename chunk_crlf_iter_type<T>::value_type
chunk_crlf_iter_type<T>::value;
using chunk_crlf_iter = chunk_crlf_iter_type<void>;
//------------------------------------------------------------------------------
template<class = void>
struct chunk_size0_iter_type
{
class value_type
{
char const s[3] = {'0', '\r', '\n'};
public:
value_type() = default;
operator
boost::asio::const_buffer() const
{
return {s, sizeof(s)};
}
};
static value_type value;
};
template<class T>
typename chunk_size0_iter_type<T>::value_type
chunk_size0_iter_type<T>::value;
using chunk_size0_iter = chunk_size0_iter_type<void>;
struct chunk_size0
{
using value_type = chunk_size0_iter::value_type;
using const_iterator = value_type const*;
const_iterator
begin() const
{
return &chunk_size0_iter::value;
}
const_iterator
end() const
{
return begin() + 1;
}
};
//------------------------------------------------------------------------------
template<class T,
bool = is_fields<T>::value>
struct buffers_or_fields
{
using type = typename
T::reader::const_buffers_type;
};
template<class T>
struct buffers_or_fields<T, false>
{
using type = T;
};
} // detail
} // http
} // beast

View File

@ -48,6 +48,8 @@ namespace http {
template<class Allocator>
class basic_fields
{
friend class fields_test; // for `header`
static std::size_t constexpr max_static_buffer = 4096;
using off_t = std::uint16_t;
@ -162,9 +164,7 @@ private:
boost::intrusive::compare<key_compare>>::type;
protected:
friend class fields_test; // for `header`
public:
/// Destructor
~basic_fields();
@ -481,6 +481,16 @@ public:
std::size_t
erase(string_view name);
/** Return a buffer sequence representing the trailers.
This function returns a buffer sequence holding the
serialized representation of the trailer fields promised
in the Accept field. Before calling this function the
Accept field must contain the exact trailer fields
desired. Each field must also exist.
*/
/// Swap this container with another
void
swap(basic_fields& other);

View File

@ -0,0 +1,283 @@
//
// 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_IMPL_CHUNK_ENCODE_IPP
#define BEAST_HTTP_IMPL_CHUNK_ENCODE_IPP
#include <beast/http/detail/rfc7230.hpp>
namespace beast {
namespace http {
inline
chunk_header::
chunk_header(std::size_t size)
: view_(
size,
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{})
{
BOOST_ASSERT(size > 0);
}
inline
chunk_header::
chunk_header(
std::size_t size,
string_view extensions)
: view_(
size,
boost::asio::const_buffers_1{
extensions.data(), extensions.size()},
chunk_crlf{})
{
BOOST_ASSERT(size > 0);
}
template<class ChunkExtensions, class>
chunk_header::
chunk_header(
std::size_t size,
ChunkExtensions&& extensions)
: exts_(std::make_shared<detail::chunk_extensions_impl<
typename std::decay<ChunkExtensions>::type>>(
std::forward<ChunkExtensions>(extensions)))
, view_(
size,
exts_->str(),
chunk_crlf{})
{
static_assert(
detail::is_chunk_extensions<ChunkExtensions>::value,
"ChunkExtensions requirements not met");
BOOST_ASSERT(size > 0);
}
template<class ChunkExtensions, class Allocator, class>
chunk_header::
chunk_header(
std::size_t size,
ChunkExtensions&& extensions,
Allocator const& allocator)
: exts_(std::allocate_shared<detail::chunk_extensions_impl<
typename std::decay<ChunkExtensions>::type>>(allocator,
std::forward<ChunkExtensions>(extensions)))
, view_(
size,
exts_->str(),
chunk_crlf{})
{
static_assert(
detail::is_chunk_extensions<ChunkExtensions>::value,
"ChunkExtensions requirements not met");
BOOST_ASSERT(size > 0);
}
//------------------------------------------------------------------------------
template<class ConstBufferSequence>
chunk_body<ConstBufferSequence>::
chunk_body(ConstBufferSequence const& buffers)
: view_(
boost::asio::buffer_size(buffers),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
buffers,
chunk_crlf{})
{
}
template<class ConstBufferSequence>
chunk_body<ConstBufferSequence>::
chunk_body(
ConstBufferSequence const& buffers,
string_view extensions)
: view_(
boost::asio::buffer_size(buffers),
boost::asio::const_buffers_1{
extensions.data(), extensions.size()},
chunk_crlf{},
buffers,
chunk_crlf{})
{
}
template<class ConstBufferSequence>
template<class ChunkExtensions, class>
chunk_body<ConstBufferSequence>::
chunk_body(
ConstBufferSequence const& buffers,
ChunkExtensions&& extensions)
: exts_(std::make_shared<detail::chunk_extensions_impl<
typename std::decay<ChunkExtensions>::type>>(
std::forward<ChunkExtensions>(extensions)))
, view_(
boost::asio::buffer_size(buffers),
exts_->str(),
chunk_crlf{},
buffers,
chunk_crlf{})
{
}
template<class ConstBufferSequence>
template<class ChunkExtensions, class Allocator, class>
chunk_body<ConstBufferSequence>::
chunk_body(
ConstBufferSequence const& buffers,
ChunkExtensions&& extensions,
Allocator const& allocator)
: exts_(std::allocate_shared<detail::chunk_extensions_impl<
typename std::decay<ChunkExtensions>::type>>(allocator,
std::forward<ChunkExtensions>(extensions)))
, view_(
boost::asio::buffer_size(buffers),
exts_->str(),
chunk_crlf{},
buffers,
chunk_crlf{})
{
}
//------------------------------------------------------------------------------
template<class Trailer>
template<class Allocator>
auto
chunk_last<Trailer>::
prepare(Trailer const& trailer, Allocator const& allocator) ->
buffers_type
{
auto sp = std::allocate_shared<typename
Trailer::reader>(allocator, trailer);
sp_ = sp;
return sp->get();
}
template<class Trailer>
auto
chunk_last<Trailer>::
prepare(Trailer const& trailer, std::true_type) ->
buffers_type
{
auto sp = std::make_shared<
typename Trailer::reader>(trailer);
sp_ = sp;
return sp->get();
}
template<class Trailer>
auto
chunk_last<Trailer>::
prepare(Trailer const& trailer, std::false_type) ->
buffers_type
{
return trailer;
}
template<class Trailer>
chunk_last<Trailer>::
chunk_last()
: view_(
detail::chunk_size0{},
Trailer{})
{
}
template<class Trailer>
chunk_last<Trailer>::
chunk_last(Trailer const& trailer)
: view_(
detail::chunk_size0{},
prepare(trailer, is_fields<Trailer>{}))
{
}
template<class Trailer>
template<class DeducedTrailer, class Allocator, class>
chunk_last<Trailer>::
chunk_last(
DeducedTrailer const& trailer, Allocator const& allocator)
: view_(
detail::chunk_size0{},
prepare(trailer, allocator))
{
}
//------------------------------------------------------------------------------
template<class Allocator>
void
basic_chunk_extensions<Allocator>::
insert(string_view name)
{
/*
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
*/
s_.reserve(1 + name.size());
s_.push_back(';');
s_.append(name.data(), name.size());
}
template<class Allocator>
void
basic_chunk_extensions<Allocator>::
insert(string_view name, string_view value)
{
/*
chunk-ext = *( ";" chunk-ext-name [ "=" chunk-ext-val ] )
chunk-ext-name = token
chunk-ext-val = token / quoted-string
token = 1*tchar
quoted-string = DQUOTE *( qdtext / quoted-pair ) DQUOTE
qdtext = HTAB / SP / "!" / %x23-5B ; '#'-'[' / %x5D-7E ; ']'-'~' / obs-text
quoted-pair = "\" ( HTAB / SP / VCHAR / obs-text )
obs-text = %x80-FF
*/
bool is_token = true;
for(auto const c : value)
{
if(! detail::is_tchar(c))
{
is_token = false;
break;
}
}
if(is_token)
{
s_.reserve(1 + name.size() + 1 + value.size());
s_.push_back(';');
s_.append(name.data(), name.size());
s_.push_back('=');
s_.append(value.data(), value.size());
return;
}
// quoted-string
s_.reserve(
1 + name.size() + 1 +
1 + value.size() + 20 + 1);
s_.push_back(';');
s_.append(name.data(), name.size());
s_.append("=\"", 2);
for(auto const c : value)
{
if(c == '\\')
s_.append(R"(\\)", 2);
else if(c == '\"')
s_.append(R"(\")", 2);
else
s_.push_back(c);
}
s_.push_back('"');
}
} // http
} // beast
#endif

View File

@ -11,10 +11,11 @@
#include <beast/core/buffer_cat.hpp>
#include <beast/core/string.hpp>
#include <beast/core/static_string.hpp>
#include <beast/core/detail/buffers_ref.hpp>
#include <beast/http/verb.hpp>
#include <beast/http/rfc7230.hpp>
#include <beast/http/status.hpp>
#include <beast/http/detail/chunk_encode.hpp>
#include <beast/http/chunk_encode.hpp>
#include <boost/throw_exception.hpp>
#include <stdexcept>
#include <string>
@ -140,18 +141,20 @@ public:
}
};
basic_fields const& f_;
boost::asio::const_buffer cb_[3];
char buf_[13];
public:
using const_buffers_type =
buffer_cat_view<
using view_type = buffer_cat_view<
boost::asio::const_buffers_1,
boost::asio::const_buffers_1,
boost::asio::const_buffers_1,
field_range,
boost::asio::const_buffers_1>;
chunk_crlf>;
basic_fields const& f_;
boost::optional<view_type> view_;
char buf_[13];
public:
using const_buffers_type =
beast::detail::buffers_ref<view_type>;
reader(basic_fields const& f,
unsigned version, verb v);
@ -159,18 +162,28 @@ public:
reader(basic_fields const& f,
unsigned version, unsigned code);
reader(basic_fields const& f);
const_buffers_type
get() const
{
return buffer_cat(
boost::asio::const_buffers_1{cb_[0]},
boost::asio::const_buffers_1{cb_[1]},
boost::asio::const_buffers_1{cb_[2]},
field_range(f_.list_.begin(), f_.list_.end()),
detail::chunk_crlf());
return const_buffers_type(*view_);
}
};
template<class Allocator>
basic_fields<Allocator>::reader::
reader(basic_fields const& f)
: f_(f)
{
view_.emplace(
boost::asio::const_buffers_1{nullptr, 0},
boost::asio::const_buffers_1{nullptr, 0},
boost::asio::const_buffers_1{nullptr, 0},
field_range(f_.list_.begin(), f_.list_.end()),
chunk_crlf());
}
template<class Allocator>
basic_fields<Allocator>::reader::
reader(basic_fields const& f,
@ -188,12 +201,8 @@ reader(basic_fields const& f,
sv = f_.get_method_impl();
else
sv = to_string(v);
cb_[0] = {sv.data(), sv.size()};
// target_or_reason_ has a leading SP
cb_[1] = {
f_.target_or_reason_.data(),
f_.target_or_reason_.size()};
buf_[0] = ' ';
buf_[1] = 'H';
@ -206,7 +215,15 @@ reader(basic_fields const& f,
buf_[8] = '0' + static_cast<char>(version % 10);
buf_[9] = '\r';
buf_[10]= '\n';
cb_[2] = {buf_, 11};
view_.emplace(
boost::asio::const_buffers_1{sv.data(), sv.size()},
boost::asio::const_buffers_1{
f_.target_or_reason_.data(),
f_.target_or_reason_.size()},
boost::asio::const_buffers_1{buf_, 11},
field_range(f_.list_.begin(), f_.list_.end()),
chunk_crlf());
}
template<class Allocator>
@ -234,16 +251,19 @@ reader(basic_fields const& f,
buf_[10]= '0' + static_cast<char>((code / 10) % 10);
buf_[11]= '0' + static_cast<char>(code % 10);
buf_[12]= ' ';
cb_[0] = {buf_, 13};
string_view sv;
if(! f_.target_or_reason_.empty())
sv = f_.target_or_reason_;
else
sv = obsolete_reason(static_cast<status>(code));
cb_[1] = {sv.data(), sv.size()};
cb_[2] = detail::chunk_crlf();
view_.emplace(
boost::asio::const_buffers_1{buf_, 13},
boost::asio::const_buffers_1{sv.data(), sv.size()},
boost::asio::const_buffers_1{"\r\n", 2},
field_range(f_.list_.begin(), f_.list_.end()),
chunk_crlf{});
}
//------------------------------------------------------------------------------

View File

@ -30,7 +30,7 @@ namespace beast {
namespace http {
namespace detail {
template<class, class, bool, class, class>
template<class, class, bool, class>
class write_some_win32_op;
} // detail
@ -50,16 +50,16 @@ struct basic_file_body<file_win32>
friend class writer;
friend struct basic_file_body<file_win32>;
template<class, class, bool, class, class>
template<class, class, bool, class>
friend class detail::write_some_win32_op;
template<class Protocol, bool isRequest,
class Fields, class Decorator>
template<
class Protocol, bool isRequest, class Fields>
friend
void
write_some(
boost::asio::basic_stream_socket<Protocol>& sock,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr,
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr,
error_code& ec);
file_win32 file_;
@ -99,16 +99,16 @@ struct basic_file_body<file_win32>
class reader
{
template<class, class, bool, class, class>
template<class, class, bool, class>
friend class detail::write_some_win32_op;
template<class Protocol, bool isRequest,
class Fields, class Decorator>
template<
class Protocol, bool isRequest, class Fields>
friend
void
write_some(
boost::asio::basic_stream_socket<Protocol>& sock,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr,
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr,
error_code& ec);
value_type& body_; // The body we are reading from
@ -329,13 +329,14 @@ public:
#if BOOST_ASIO_HAS_WINDOWS_OVERLAPPED_PTR
template<class Protocol, class Handler,
bool isRequest, class Fields, class Decorator>
template<
class Protocol, class Handler,
bool isRequest, class Fields>
class write_some_win32_op
{
boost::asio::basic_stream_socket<Protocol>& sock_;
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr_;
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr_;
bool header_ = false;
Handler h_;
@ -347,8 +348,8 @@ public:
write_some_win32_op(
DeducedHandler&& h,
boost::asio::basic_stream_socket<Protocol>& s,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr)
serializer<isRequest,
basic_file_body<file_win32>,Fields>& sr)
: sock_(s)
, sr_(sr)
, h_(std::forward<DeducedHandler>(h))
@ -398,11 +399,12 @@ public:
}
};
template<class Protocol, class Handler,
bool isRequest, class Fields, class Decorator>
template<
class Protocol, class Handler,
bool isRequest, class Fields>
void
write_some_win32_op<
Protocol, Handler, isRequest, Fields, Decorator>::
Protocol, Handler, isRequest, Fields>::
operator()()
{
if(! sr_.is_header_done())
@ -449,11 +451,12 @@ operator()()
overlapped.release();
}
template<class Protocol, class Handler,
bool isRequest, class Fields, class Decorator>
template<
class Protocol, class Handler,
bool isRequest, class Fields>
void
write_some_win32_op<
Protocol, Handler,isRequest, Fields, Decorator>::
Protocol, Handler, isRequest, Fields>::
operator()(error_code ec, std::size_t bytes_transferred)
{
if(! ec)
@ -484,13 +487,12 @@ operator()(error_code ec, std::size_t bytes_transferred)
//------------------------------------------------------------------------------
template<class Protocol,
bool isRequest, class Fields, class Decorator>
template<class Protocol, bool isRequest, class Fields>
void
write_some(
boost::asio::basic_stream_socket<Protocol>& sock,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr,
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr,
error_code& ec)
{
if(! sr.is_header_done())
@ -552,20 +554,20 @@ write_some(
template<
class Protocol,
bool isRequest, class Fields, class Decorator,
bool isRequest, class Fields,
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_some(
boost::asio::basic_stream_socket<Protocol>& sock,
serializer<isRequest, basic_file_body<file_win32>,
Fields, Decorator>& sr,
serializer<isRequest,
basic_file_body<file_win32>, Fields>& sr,
WriteHandler&& handler)
{
async_completion<WriteHandler,
void(error_code)> init{handler};
detail::write_some_win32_op<Protocol, handler_type<
WriteHandler, void(error_code)>, isRequest, Fields,
Decorator>{init.completion_handler, sock, sr}();
WriteHandler, void(error_code)>, isRequest, Fields>{
init.completion_handler, sock, sr}();
return init.result.get();
}

View File

@ -18,30 +18,30 @@
namespace beast {
namespace http {
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
frdinit(std::true_type)
{
frd_.emplace(m_, m_.version, m_.method());
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
frdinit(std::false_type)
{
frd_.emplace(m_, m_.version, m_.result_int());
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
template<class T1, class T2, class Visit>
inline
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
do_visit(error_code& ec, Visit& visit)
{
// VFALCO work-around for missing variant::emplace
@ -54,30 +54,20 @@ do_visit(error_code& ec, Visit& visit)
//------------------------------------------------------------------------------
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
serializer<isRequest, Body, Fields, ChunkDecorator>::
template<
bool isRequest, class Body, class Fields>
serializer<isRequest, Body, Fields>::
serializer(value_type& m)
: m_(m)
, rd_(m_)
{
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer(value_type& m, ChunkDecorator const& d)
: m_(m)
, rd_(m_)
, d_(d)
{
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
template<class Visit>
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
next(error_code& ec, Visit&& visit)
{
using boost::asio::buffer_size;
@ -177,46 +167,25 @@ next(error_code& ec, Visit&& visit)
v_ = cb7_t{
boost::in_place_init,
frd_->get(),
detail::chunk_header{
buffer_size(result->first)},
[&]()
{
auto sv = d_(result->first);
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf(),
buffer_size(result->first),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
result->first,
detail::chunk_crlf(),
detail::chunk_final(),
[&]()
{
auto sv = d_(
boost::asio::null_buffers{});
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf()};
chunk_crlf{},
detail::chunk_last(),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{}};
goto go_all_c;
}
#endif
v_ = cb4_t{
boost::in_place_init,
frd_->get(),
detail::chunk_header{
buffer_size(result->first)},
[&]()
{
auto sv = d_(result->first);
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf(),
buffer_size(result->first),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
result->first,
detail::chunk_crlf()};
chunk_crlf{}};
s_ = do_header_c;
BEAST_FALLTHROUGH;
}
@ -250,45 +219,24 @@ next(error_code& ec, Visit&& visit)
// do it all in one buffer
v_ = cb6_t{
boost::in_place_init,
detail::chunk_header{
buffer_size(result->first)},
[&]()
{
auto sv = d_(result->first);
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf(),
buffer_size(result->first),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
result->first,
detail::chunk_crlf(),
detail::chunk_final(),
[&]()
{
auto sv = d_(
boost::asio::null_buffers{});
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf()};
chunk_crlf{},
detail::chunk_last(),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{}};
goto go_body_final_c;
}
#endif
v_ = cb5_t{
boost::in_place_init,
detail::chunk_header{
buffer_size(result->first)},
[&]()
{
auto sv = d_(result->first);
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf(),
buffer_size(result->first),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{},
result->first,
detail::chunk_crlf()};
chunk_crlf{}};
s_ = do_body_c + 2;
BEAST_FALLTHROUGH;
}
@ -315,16 +263,9 @@ next(error_code& ec, Visit&& visit)
case do_final_c:
v_ = cb8_t{
boost::in_place_init,
detail::chunk_final(),
[&]()
{
auto sv = d_(
boost::asio::null_buffers{});
return boost::asio::const_buffers_1{
sv.data(), sv.size()};
}(),
detail::chunk_crlf()};
detail::chunk_last(),
boost::asio::const_buffers_1{nullptr, 0},
chunk_crlf{}};
s_ = do_final_c + 1;
BEAST_FALLTHROUGH;
@ -345,10 +286,10 @@ next(error_code& ec, Visit&& visit)
}
}
template<bool isRequest, class Body,
class Fields, class ChunkDecorator>
template<
bool isRequest, class Body, class Fields>
void
serializer<isRequest, Body, Fields, ChunkDecorator>::
serializer<isRequest, Body, Fields>::
consume(std::size_t n)
{
using boost::asio::buffer_size;

View File

@ -28,13 +28,13 @@ namespace beast {
namespace http {
namespace detail {
template<class Stream, class Handler,
bool isRequest, class Body,
class Fields, class Decorator>
template<
class Stream, class Handler,
bool isRequest, class Body, class Fields>
class write_some_op
{
Stream& s_;
serializer<isRequest,Body, Fields, Decorator>& sr_;
serializer<isRequest,Body, Fields>& sr_;
Handler h_;
class lambda
@ -67,9 +67,8 @@ public:
write_some_op(write_some_op const&) = default;
template<class DeducedHandler>
write_some_op(DeducedHandler&& h,
Stream& s, serializer<isRequest, Body,
Fields, Decorator>& sr)
write_some_op(DeducedHandler&& h, Stream& s,
serializer<isRequest, Body, Fields>& sr)
: s_(s)
, sr_(sr)
, h_(std::forward<DeducedHandler>(h))
@ -118,12 +117,12 @@ public:
}
};
template<class Stream, class Handler,
bool isRequest, class Body,
class Fields, class Decorator>
template<
class Stream, class Handler,
bool isRequest, class Body, class Fields>
void
write_some_op<Stream, Handler,
isRequest, Body, Fields, Decorator>::
write_some_op<
Stream, Handler, isRequest, Body, Fields>::
operator()()
{
error_code ec;
@ -150,12 +149,12 @@ operator()()
bind_handler(std::move(*this), ec, 0));
}
template<class Stream, class Handler,
bool isRequest, class Body,
class Fields, class Decorator>
template<
class Stream, class Handler,
bool isRequest, class Body, class Fields>
void
write_some_op<Stream, Handler,
isRequest, Body, Fields, Decorator>::
write_some_op<
Stream, Handler, isRequest, Body, Fields>::
operator()(
error_code ec, std::size_t bytes_transferred)
{
@ -173,11 +172,11 @@ operator()(
struct serializer_is_header_done
{
template<bool isRequest, class Body,
class Fields, class Decorator>
template<
bool isRequest, class Body, class Fields>
bool
operator()(serializer<isRequest, Body,
Fields, Decorator>& sr) const
operator()(
serializer<isRequest, Body, Fields>& sr) const
{
return sr.is_header_done();
}
@ -185,11 +184,11 @@ struct serializer_is_header_done
struct serializer_is_done
{
template<bool isRequest, class Body,
class Fields, class Decorator>
template<
bool isRequest, class Body, class Fields>
bool
operator()(serializer<isRequest, Body,
Fields, Decorator>& sr) const
operator()(
serializer<isRequest, Body, Fields>& sr) const
{
return sr.is_done();
}
@ -199,14 +198,12 @@ struct serializer_is_done
template<
class Stream, class Handler, class Predicate,
bool isRequest, class Body,
class Fields, class Decorator>
bool isRequest, class Body, class Fields>
class write_op
{
int state_ = 0;
Stream& s_;
serializer<isRequest,
Body, Fields, Decorator>& sr_;
serializer<isRequest, Body, Fields>& sr_;
Handler h_;
public:
@ -215,8 +212,7 @@ public:
template<class DeducedHandler>
write_op(DeducedHandler&& h, Stream& s,
serializer<isRequest, Body, Fields,
Decorator>& sr)
serializer<isRequest, Body, Fields>& sr)
: s_(s)
, sr_(sr)
, h_(std::forward<DeducedHandler>(h))
@ -265,11 +261,10 @@ public:
template<
class Stream, class Handler, class Predicate,
bool isRequest, class Body,
class Fields, class Decorator>
bool isRequest, class Body, class Fields>
void
write_op<Stream, Handler, Predicate,
isRequest, Body, Fields, Decorator>::
isRequest, Body, Fields>::
operator()(error_code ec)
{
if(ec)
@ -317,13 +312,12 @@ class write_msg_op
struct data
{
Stream& s;
serializer<isRequest,
Body, Fields, no_chunk_decorator> sr;
serializer<isRequest, Body, Fields> sr;
data(Handler&, Stream& s_, message<
isRequest, Body, Fields>& m_)
: s(s_)
, sr(m_, no_chunk_decorator{})
, sr(m_)
{
}
};
@ -466,11 +460,11 @@ namespace detail {
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields, class Decorator>
bool isRequest, class Body, class Fields>
void
write_some(
SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec)
{
if(! sr.is_done())
@ -492,30 +486,34 @@ write_some(
ec.assign(0, ec.category());
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_some(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
async_write_some(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler)
{
async_completion<WriteHandler,
void(error_code)> init{handler};
detail::write_some_op<AsyncWriteStream,
handler_type<WriteHandler, void(error_code)>,
isRequest, Body, Fields, Decorator>{
isRequest, Body, Fields>{
init.completion_handler, stream, sr}();
return init.result.get();
}
} // detail
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr)
write_some(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@ -529,12 +527,13 @@ write_some(SyncWriteStream& stream, serializer<
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class SyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
write_some(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
@ -546,12 +545,14 @@ write_some(SyncWriteStream& stream, serializer<
detail::write_some(stream, sr, ec);
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_some(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
async_write_some(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler)
{
static_assert(is_async_write_stream<
@ -567,12 +568,12 @@ async_write_some(AsyncWriteStream& stream, serializer<
//------------------------------------------------------------------------------
template<class SyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_header(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr)
write_header(SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@ -586,12 +587,13 @@ write_header(SyncWriteStream& stream, serializer<
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class SyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_header(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
write_header(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
@ -620,12 +622,14 @@ write_header(SyncWriteStream& stream, serializer<
}
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write_header(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
async_write_header(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler)
{
static_assert(is_async_write_stream<
@ -641,7 +645,7 @@ async_write_header(AsyncWriteStream& stream, serializer<
detail::write_op<AsyncWriteStream, handler_type<
WriteHandler, void(error_code)>,
detail::serializer_is_header_done,
isRequest, Body, Fields, Decorator>{
isRequest, Body, Fields>{
init.completion_handler, stream, sr}(
error_code{}, 0);
return init.result.get();
@ -651,12 +655,11 @@ async_write_header(AsyncWriteStream& stream, serializer<
template<
class SyncWriteStream,
bool isRequest, class Body,
class Fields, class Decorator>
bool isRequest, class Body, class Fields>
void
write(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields, Decorator>& sr)
serializer<isRequest, Body, Fields>& sr)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
"SyncWriteStream requirements not met");
@ -668,12 +671,11 @@ write(
template<
class SyncWriteStream,
bool isRequest, class Body,
class Fields, class Decorator>
bool isRequest, class Body, class Fields>
void
write(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields, Decorator>& sr,
serializer<isRequest, Body, Fields>& sr,
error_code& ec)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
@ -689,12 +691,14 @@ write(
}
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
async_return_type<WriteHandler, void(error_code)>
async_write(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
async_write(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler)
{
static_assert(is_async_write_stream<
@ -709,19 +713,19 @@ async_write(AsyncWriteStream& stream, serializer<
void(error_code)> init{handler};
detail::write_op<AsyncWriteStream, handler_type<
WriteHandler, void(error_code)>,
detail::serializer_is_done,
isRequest, Body, Fields, Decorator>{
init.completion_handler, stream, sr}(
error_code{});
detail::serializer_is_done, isRequest, Body, Fields>{
init.completion_handler, stream, sr}(error_code{});
return init.result.get();
}
//------------------------------------------------------------------------------
template<class SyncWriteStream,
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
write(
SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg)
{
static_assert(is_sync_write_stream<SyncWriteStream>::value,
@ -736,10 +740,12 @@ write(SyncWriteStream& stream,
BOOST_THROW_EXCEPTION(system_error{ec});
}
template<class SyncWriteStream,
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
write(
SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg,
error_code& ec)
{
@ -753,12 +759,14 @@ write(SyncWriteStream& stream,
write(stream, sr, ec);
}
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class WriteHandler>
async_return_type<
WriteHandler, void(error_code)>
async_write(AsyncWriteStream& stream,
async_write(
AsyncWriteStream& stream,
message<isRequest, Body, Fields>& msg,
WriteHandler&& handler)
{

View File

@ -15,7 +15,7 @@
#include <beast/core/string.hpp>
#include <beast/core/type_traits.hpp>
#include <beast/http/message.hpp>
#include <beast/http/detail/chunk_encode.hpp>
#include <beast/http/chunk_encode.hpp>
#include <boost/asio/buffer.hpp>
#include <boost/optional.hpp>
#include <boost/variant.hpp>
@ -29,34 +29,6 @@
namespace beast {
namespace http {
/** A chunk decorator which does nothing.
When selected as a chunk decorator, objects of this type
affect the output of messages specifying chunked
transfer encodings as follows:
@li chunk headers will have empty chunk extensions, and
@li final chunks will have an empty set of trailers.
@see @ref serializer
*/
struct no_chunk_decorator
{
template<class ConstBufferSequence>
string_view
operator()(ConstBufferSequence const&) const
{
return {};
}
string_view
operator()(boost::asio::null_buffers) const
{
return {};
}
};
/** Provides buffer oriented HTTP message serialization functionality.
An object of this type is used to serialize a complete
@ -67,64 +39,27 @@ struct no_chunk_decorator
if the contents of the message indicate that chunk encoding
is required. If the semantics of the message indicate that
the connection should be closed after the message is sent, the
function @ref keep_alive will return `true`.
function @ref keep_alive will return `false`.
Upon construction, an optional chunk decorator may be
specified. This decorator is a function object called with
each buffer sequence of the body when the chunked transfer
encoding is indicate in the message header. The decorator
will be called with an empty buffer sequence (actually
the type `boost::asio::null_buffers`) to indicate the
final chunk. The decorator may return a string which forms
the chunk extension for chunks, and the field trailers
for the final chunk.
In C++11 the decorator must be declared as a class or
struct with a templated operator() thusly:
@code
// The implementation guarantees that operator()
// will be called only after the view returned by
// any previous calls to operator() are no longer
// needed. The decorator instance is intended to
// manage the lifetime of the storage for all returned
// views.
//
struct decorator
{
// Returns the chunk-extension for each chunk,
// or an empty string for no chunk extension. The
// buffer must include the leading semicolon (";")
// and follow the format for chunk extensions defined
// in rfc7230.
//
template<class ConstBufferSequence>
string_view
operator()(ConstBufferSequence const&) const;
// Returns a set of field trailers for the final chunk.
// Each field should be formatted according to rfc7230
// including the trailing "\r\n" for each field. If
// no trailers are indicated, an empty string is returned.
//
string_view
operator()(boost::asio::null_buffers) const;
};
@endcode
Chunked output produced by the serializer never contains chunk
extensions or trailers, and the location of chunk boundaries
is not specified. If callers require chunk extensions, trailers,
or control over the exact contents of each chunk they should
use the serializer to write just the message header, and then
assume control over serializing the chunked payload by using
the chunk buffer sequence types @ref chunk_body, @ref chunk_crlf,
@ref chunk_header, and @ref chunk_last.
@tparam isRequest `true` if the message is a request.
@tparam Body The body type of the message.
@tparam Fields The type of fields in the message.
@tparam ChunkDecorator The type of chunk decorator to use.
*/
template<
bool isRequest,
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator>
class Fields = fields>
class serializer
{
public:
@ -199,50 +134,50 @@ private:
using cb4_t = consuming_buffers<buffer_cat_view<
typename Fields::reader::const_buffers_type,// header
detail::chunk_header, // chunk-header
detail::chunk_size, // chunk-size
boost::asio::const_buffers_1, // chunk-ext
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
typename reader::const_buffers_type, // body
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb4_t = buffer_prefix_view<cb4_t const&>;
using cb5_t = consuming_buffers<buffer_cat_view<
detail::chunk_header, // chunk-header
detail::chunk_size, // chunk-header
boost::asio::const_buffers_1, // chunk-ext
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
typename reader::const_buffers_type, // body
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb5_t = buffer_prefix_view<cb5_t const&>;
#ifndef BEAST_NO_BIG_VARIANTS
using cb6_t = consuming_buffers<buffer_cat_view<
detail::chunk_header, // chunk-header
boost::asio::const_buffers_1, // chunk-ext
boost::asio::const_buffers_1, // crlf
detail::chunk_size, // chunk-header
boost::asio::const_buffers_1, // chunk-size
chunk_crlf, // crlf
typename reader::const_buffers_type, // body
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
boost::asio::const_buffers_1, // chunk-final
boost::asio::const_buffers_1, // trailers
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb6_t = buffer_prefix_view<cb6_t const&>;
using cb7_t = consuming_buffers<buffer_cat_view<
typename Fields::reader::const_buffers_type,// header
detail::chunk_header, // chunk-header
detail::chunk_size, // chunk-size
boost::asio::const_buffers_1, // chunk-ext
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
typename reader::const_buffers_type, // body
boost::asio::const_buffers_1, // crlf
chunk_crlf, // crlf
boost::asio::const_buffers_1, // chunk-final
boost::asio::const_buffers_1, // trailers
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb7_t = buffer_prefix_view<cb7_t const&>;
#endif
using cb8_t = consuming_buffers<buffer_cat_view<
boost::asio::const_buffers_1, // chunk-final
boost::asio::const_buffers_1, // trailers
boost::asio::const_buffers_1>>; // crlf
chunk_crlf>>; // crlf
using pcb8_t = buffer_prefix_view<cb8_t const&>;
value_type& m_;
@ -268,7 +203,6 @@ private:
bool chunked_;
bool keep_alive_;
bool more_;
ChunkDecorator d_;
public:
/** Constructor
@ -288,25 +222,6 @@ public:
explicit
serializer(value_type& msg);
/** Constructor
The implementation guarantees that the message passed on
construction will not be accessed until the first call to
@ref next. This allows the message to be lazily created.
For example, if the header is filled in before serialization.
@param msg A reference to the message to serialize, which must
remain valid for the lifetime of the serializer. Depending on
the type of Body used, this may or may not be a `const` reference.
@param decorator The decorator to use.
@note This function participates in overload resolution only if
Body::reader is constructible from a `const` message reference.
*/
explicit
serializer(value_type& msg, ChunkDecorator const& decorator);
/// Returns the message being serialized
value_type&
get()
@ -314,22 +229,6 @@ public:
return m_;
}
/** Provides access to the associated @b BodyReader
This function provides access to the instance of the reader
associated with the body and created by the serializer
upon construction. The behavior of accessing this object
is defined by the specification of the particular reader
and its associated body.
@return A reference to the reader.
*/
reader&
reader_impl()
{
return rd_;
}
/// Returns the serialized buffer size limit
std::size_t
limit()
@ -478,21 +377,31 @@ public:
*/
void
consume(std::size_t n);
/** Provides low-level access to the associated @b BodyReader
This function provides access to the instance of the reader
associated with the body and created by the serializer
upon construction. The behavior of accessing this object
is defined by the specification of the particular reader
and its associated body.
@return A reference to the reader.
*/
reader&
reader_impl()
{
return rd_;
}
};
/// A serializer for HTTP/1 requests
template<
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator>
using request_serializer = serializer<true, Body, Fields, ChunkDecorator>;
template<class Body, class Fields = fields>
using request_serializer = serializer<true, Body, Fields>;
/// A serializer for HTTP/1 responses
template<
class Body,
class Fields = fields,
class ChunkDecorator = no_chunk_decorator>
using response_serializer = serializer<false, Body, Fields, ChunkDecorator>;
template<class Body, class Fields = fields>
using response_serializer = serializer<false, Body, Fields>;
} // http
} // beast

View File

@ -60,11 +60,13 @@ namespace http {
@see serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr);
write_some(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr);
/** Write part of a message to a stream using a serializer.
@ -98,11 +100,13 @@ write_some(SyncWriteStream& stream, serializer<
@see @ref async_write_some, @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_some(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
write_some(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec);
/** Write part of a message to a stream asynchronously using a serializer.
@ -151,16 +155,18 @@ write_some(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<WriteHandler, void(error_code)>
#endif
async_write_some(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
async_write_some(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler);
//------------------------------------------------------------------------------
@ -190,11 +196,13 @@ async_write_some(AsyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_header(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr);
write_header(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr);
/** Write a header to a stream using a serializer.
@ -221,11 +229,13 @@ write_header(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write_header(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
write_header(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec);
/** Write a header to a stream asynchronously using a serializer.
@ -267,16 +277,18 @@ write_header(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<WriteHandler, void(error_code)>
#endif
async_write_header(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
async_write_header(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler);
//------------------------------------------------------------------------------
@ -303,11 +315,13 @@ async_write_header(AsyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr);
write(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr);
/** Write a complete message to a stream using a serializer.
@ -331,11 +345,13 @@ write(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class SyncWriteStream, bool isRequest,
class Body, class Fields, class Decorator>
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
write(
SyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
error_code& ec);
/** Write a complete message to a stream asynchronously using a serializer.
@ -374,16 +390,18 @@ write(SyncWriteStream& stream, serializer<
@see @ref serializer
*/
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class Decorator, class WriteHandler>
class WriteHandler>
#if BEAST_DOXYGEN
void_or_deduced
#else
async_return_type<WriteHandler, void(error_code)>
#endif
async_write(AsyncWriteStream& stream, serializer<
isRequest, Body, Fields, Decorator>& sr,
async_write(
AsyncWriteStream& stream,
serializer<isRequest, Body, Fields>& sr,
WriteHandler&& handler);
//------------------------------------------------------------------------------
@ -412,10 +430,12 @@ async_write(AsyncWriteStream& stream, serializer<
@see @ref message
*/
template<class SyncWriteStream,
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
write(
SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg);
/** Write a complete message to a stream.
@ -442,10 +462,12 @@ write(SyncWriteStream& stream,
@see @ref message
*/
template<class SyncWriteStream,
template<
class SyncWriteStream,
bool isRequest, class Body, class Fields>
void
write(SyncWriteStream& stream,
write(
SyncWriteStream& stream,
message<isRequest, Body, Fields> const& msg,
error_code& ec);
@ -488,12 +510,13 @@ write(SyncWriteStream& stream,
@see @ref message
*/
template<class AsyncWriteStream,
template<
class AsyncWriteStream,
bool isRequest, class Body, class Fields,
class WriteHandler>
async_return_type<
WriteHandler, void(error_code)>
async_write(AsyncWriteStream& stream,
async_return_type<WriteHandler, void(error_code)>
async_write(
AsyncWriteStream& stream,
message<isRequest, Body, Fields>& msg,
WriteHandler&& handler);

View File

@ -17,6 +17,7 @@ add_executable (http-tests
basic_file_body.cpp
basic_parser.cpp
buffer_body.cpp
chunk_encode.cpp
doc_examples.cpp
doc_snippets.cpp
dynamic_body.cpp

View File

@ -11,6 +11,7 @@ unit-test http-tests :
basic_file_body.cpp
basic_parser.cpp
buffer_body.cpp
chunk_encode.cpp
doc_examples.cpp
doc_snippets.cpp
dynamic_body.cpp

225
test/http/chunk_encode.cpp Normal file
View File

@ -0,0 +1,225 @@
//
// 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/http/fields.hpp>
#include <beast/unit_test/suite.hpp>
#include <boost/optional.hpp>
namespace beast {
namespace http {
class chunk_encode_test
: public beast::unit_test::suite
{
public:
struct not_chunk_extensions {};
BOOST_STATIC_ASSERT(
detail::is_chunk_extensions<chunk_extensions>::value);
BOOST_STATIC_ASSERT(
! detail::is_chunk_extensions<not_chunk_extensions>::value);
template<class ConstBufferSequence>
static
std::string
to_string(ConstBufferSequence const& buffers)
{
std::string s;
s.reserve(boost::asio::buffer_size(buffers));
for(boost::asio::const_buffer buffer : buffers)
s.append(
boost::asio::buffer_cast<char const*>(buffer),
boost::asio::buffer_size(buffer));
return s;
}
template<class T, class... Args>
void
check(string_view match, Args&&... args)
{
T t{std::forward<Args>(args)...};
BEAST_EXPECT(to_string(t) == match);
T t2{t};
BEAST_EXPECT(to_string(t2) == match);
T t3{std::move(t2)};
BEAST_EXPECT(to_string(t3) == match);
}
template<class T, class... Args>
void
check_fwd(string_view match, Args&&... args)
{
T t{std::forward<Args>(args)...};
BEAST_EXPECT(to_string(t) == match);
T t2{t};
BEAST_EXPECT(to_string(t2) == match);
T t3{std::move(t2)};
BEAST_EXPECT(to_string(t3) == match);
}
using cb_t = boost::asio::const_buffers_1;
static
cb_t
cb(string_view s)
{
return {s.data(), s.size()};
}
void
testChunkCRLF()
{
#if ! defined(BOOST_GCC) || BOOST_GCC >= 50000
check<chunk_crlf>("\r\n");
#endif
}
void
testChunkHeader()
{
check<chunk_header>("10\r\n", 16u);
check<chunk_header>("20;x\r\n", 32u, ";x");
chunk_extensions exts;
exts.insert("y");
exts.insert("z");
check<chunk_header>("30;y;z\r\n", 48u, exts);
{
auto exts2 = exts;
check_fwd<chunk_header>(
"30;y;z\r\n", 48u, std::move(exts2));
}
check<chunk_header>("30;y;z\r\n", 48u, exts,
std::allocator<double>{});
{
auto exts2 = exts;
check<chunk_header>(
"30;y;z\r\n", 48u, std::move(exts2),
std::allocator<double>{});
}
}
void
testChunkBody()
{
check<chunk_body<cb_t>>(
"3\r\n***\r\n", cb("***"));
check<chunk_body<cb_t>>(
"3;x\r\n***\r\n", cb("***"), ";x");
chunk_extensions exts;
exts.insert("y");
exts.insert("z");
check<chunk_body<cb_t>>(
"3;y;z\r\n***\r\n",
cb("***"), exts);
{
auto exts2 = exts;
check_fwd<chunk_body<cb_t>>(
"3;y;z\r\n***\r\n",
cb("***"), std::move(exts2));
}
check<chunk_body<cb_t>>(
"3;y;z\r\n***\r\n",
cb("***"), exts, std::allocator<double>{});
{
auto exts2 = exts;
check_fwd<chunk_body<cb_t>>(
"3;y;z\r\n***\r\n",
cb("***"), std::move(exts2),
std::allocator<double>{});
}
}
void
testChunkFinal()
{
check<chunk_last<>>(
"0\r\n\r\n");
check<chunk_last<cb_t>>(
"0\r\nMD5:ou812\r\n\r\n",
cb("MD5:ou812\r\n\r\n"));
fields trailers;
trailers.set(field::content_md5, "ou812");
check<chunk_last<fields>>(
"0\r\nContent-MD5: ou812\r\n\r\n",
trailers);
{
auto trailers2 = trailers;
check_fwd<chunk_last<fields>>(
"0\r\nContent-MD5: ou812\r\n\r\n",
std::move(trailers2));
}
check<chunk_last<fields>>(
"0\r\nContent-MD5: ou812\r\n\r\n",
trailers, std::allocator<double>{});
{
auto trailers2 = trailers;
check<chunk_last<fields>>(
"0\r\nContent-MD5: ou812\r\n\r\n",
std::move(trailers2), std::allocator<double>{});
}
}
void
testChunkExtensions()
{
chunk_extensions ce;
ce.insert("x");
BEAST_EXPECT(ce.str() == ";x");
ce.insert("y", "z");
BEAST_EXPECT(ce.str() == ";x;y=z");
ce.insert("z", R"(")");
BEAST_EXPECT(ce.str() == R"(;x;y=z;z="\"")");
ce.insert("p", R"(\)");
BEAST_EXPECT(ce.str() == R"(;x;y=z;z="\"";p="\\")");
ce.insert("q", R"(1"2\)");
BEAST_EXPECT(ce.str() == R"(;x;y=z;z="\"";p="\\";q="1\"2\\")");
}
void
run() override
{
testChunkCRLF();
testChunkHeader();
testChunkBody();
testChunkFinal();
testChunkExtensions();
}
};
BEAST_DEFINE_TESTSUITE(chunk_encode,http,beast);
} // http
} // beast

View File

@ -7,8 +7,14 @@
#include "example/doc/http_examples.hpp"
#include <beast/core/flat_buffer.hpp>
#include <beast/core/read_size.hpp>
#include <beast/core/ostream.hpp>
#include <beast/core/detail/clamp.hpp>
#include <beast/http/chunk_encode.hpp>
#include <beast/http/parser.hpp>
#include <beast/http/read.hpp>
#include <beast/http/write.hpp>
#include <beast/test/pipe_stream.hpp>
#include <beast/test/string_istream.hpp>
#include <beast/test/string_ostream.hpp>
@ -282,6 +288,118 @@ public:
//--------------------------------------------------------------------------
void
doExplicitChunkSerialize()
{
auto const buf =
[](string_view s)
{
return boost::asio::const_buffers_1{
s.data(), s.size()};
};
test::pipe p{ios_};
response<empty_body> res{status::ok, 11};
res.set(field::server, "test");
res.set(field::accept, "Expires, Content-MD5");
res.chunked(true);
error_code ec;
response_serializer<empty_body> sr{res};
write_header(p.client, sr, ec);
chunk_extensions exts;
boost::asio::write(p.client,
make_chunk(buf("First")), ec);
exts.insert("quality", "1.0");
boost::asio::write(p.client,
make_chunk(buf("Hello, world!"), exts), ec);
exts.clear();
exts.insert("file", "abc.txt");
exts.insert("quality", "0.7");
boost::asio::write(p.client,
make_chunk(buf("The Next Chunk"), std::move(exts)), ec);
exts.clear();
exts.insert("last");
boost::asio::write(p.client,
make_chunk(buf("Last one"), std::move(exts),
std::allocator<double>{}), ec);
fields trailers;
trailers.set(field::expires, "never");
trailers.set(field::content_md5, "f4a5c16584f03d90");
boost::asio::write(p.client,
make_chunk_last(
trailers,
std::allocator<double>{}
), ec);
BEAST_EXPECT(
boost::lexical_cast<std::string>(
buffers(p.server.buffer.data())) ==
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Accept: Expires, Content-MD5\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"5\r\n"
"First\r\n"
"d;quality=1.0\r\n"
"Hello, world!\r\n"
"e;file=abc.txt;quality=0.7\r\n"
"The Next Chunk\r\n"
"8;last\r\n"
"Last one\r\n"
"0\r\n"
"Expires: never\r\n"
"Content-MD5: f4a5c16584f03d90\r\n"
"\r\n");
}
//--------------------------------------------------------------------------
void
doExplicitChunkParse()
{
test::pipe c{ios_};
ostream(c.client.buffer) <<
"HTTP/1.1 200 OK\r\n"
"Server: test\r\n"
"Accept: Expires, Content-MD5\r\n"
"Transfer-Encoding: chunked\r\n"
"\r\n"
"5\r\n"
"First\r\n"
"d;quality=1.0\r\n"
"Hello, world!\r\n"
"e;file=abc.txt;quality=0.7\r\n"
"The Next Chunk\r\n"
"8;last\r\n"
"Last one\r\n"
"0\r\n"
"Expires: never\r\n"
"Content-MD5: f4a5c16584f03d90\r\n"
"\r\n";
flat_buffer b;
response_parser<empty_body> p;
read_header(c.client, b, p);
BOOST_ASSERT(p.is_chunked());
//while(! p.is_done())
{
// read the chunk header?
// read the next chunk?
}
}
//--------------------------------------------------------------------------
void
run()
{
@ -294,6 +412,8 @@ public:
doHEAD();
doDeferredBody();
doIncrementalRead();
doExplicitChunkSerialize();
doExplicitChunkParse();
}
};

View File

@ -22,6 +22,16 @@ using namespace beast::http;
namespace doc_http_snippets {
//[http_snippet_17
// This function returns the buffer containing the next chunk body
boost::asio::const_buffers_1 get_next_chunk_body();
//]
boost::asio::const_buffers_1 get_next_chunk_body()
{
return {nullptr, 0};
}
void fxx() {
boost::asio::io_service ios;
@ -130,8 +140,170 @@ void fxx() {
//]
}
{
//[http_snippet_18
// Prepare an HTTP/1.1 response with a chunked body
response<empty_body> res{status::ok, 11};
res.set(field::server, "Beast");
// Set Transfer-Encoding to "chunked".
// If a Content-Length was present, it is removed.
res.chunked(true);
// Set up the serializer
response_serializer<empty_body> sr{res};
// Write the header first
write_header(sock, sr);
// Now manually emit three chunks:
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
// We are responsible for sending the last chunk:
boost::asio::write(sock, make_chunk_last());
//]
}
{
//[http_snippet_19
// Prepare a set of chunk extension to emit with the body
chunk_extensions ext;
ext.insert("mp3");
ext.insert("title", "Beale Street Blues");
ext.insert("artist", "W.C. Handy");
// Write the next chunk with the chunk extensions
// The implementation will make a copy of the extensions object,
// so the caller does not need to manage lifetime issues.
boost::asio::async_write(sock, make_chunk(get_next_chunk_body(), ext),
[](error_code ec, std::size_t)
{
if(ec)
std::cout << "Error: " << ec.message() << std::endl;
});
// Write the next chunk with the chunk extensions
// The implementation will make a copy of the extensions object, storing the copy
// using the custom allocator, so the caller does not need to manage lifetime issues.
boost::asio::async_write(sock,
make_chunk(get_next_chunk_body(), ext, std::allocator<char>{}),
[](error_code ec, std::size_t)
{
if(ec)
std::cout << "Error: " << ec.message() << std::endl;
});
// Write the next chunk with the chunk extensions
// The implementation allocates memory using the default allocator and takes ownership
// of the extensions object, so the caller does not need to manage lifetime issues.
// Note: ext is moved
boost::asio::async_write(sock, make_chunk(get_next_chunk_body(), std::move(ext)),
[](error_code ec, std::size_t)
{
if(ec)
std::cout << "Error: " << ec.message() << std::endl;
});
//]
}
{
//[http_snippet_20
// Manually specify the chunk extensions.
// Some of the strings contain spaces and a period and must be quoted
boost::asio::write(sock, make_chunk(get_next_chunk_body(),
";mp3"
";title=\"Danny Boy\""
";artist=\"Fred E. Weatherly\""
));
//]
}
{
//[http_snippet_21
// Prepare a chunked HTTP/1.1 response with some trailer fields
response<empty_body> res{status::ok, 11};
res.set(field::server, "Beast");
// Inform the client of the trailer fields we will send
res.set(field::trailer, "Content-MD5, Expires");
res.chunked(true);
// Serialize the header and two chunks
response_serializer<empty_body> sr{res};
write_header(sock, sr);
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
boost::asio::write(sock, make_chunk(get_next_chunk_body()));
// Prepare the trailer
fields trailer;
trailer.set(field::content_md5, "f4a5c16584f03d90");
trailer.set(field::expires, "never");
// Emit the trailer in the last chunk.
// The implementation will use the default allocator to create the storage for holding
// the serialized fields.
boost::asio::write(sock, make_chunk_last(trailer));
//]
}
{
//[http_snippet_22
// Use a custom allocator for serializing the last chunk
fields trailer;
trailer.set(field::approved, "yes");
boost::asio::write(sock, make_chunk_last(trailer, std::allocator<char>{}));
//]
}
{
//[http_snippet_23
// Manually emit a trailer.
// We are responsible for ensuring that the trailer format adheres to the specification.
string_view ext =
"Content-MD5: f4a5c16584f03d90\r\n"
"Expires: never\r\n"
"\r\n";
boost::asio::write(sock, make_chunk_last(boost::asio::const_buffers_1{ext.data(), ext.size()}));
//]
}
{
//[http_snippet_24
// Prepare a chunked HTTP/1.1 response and send the header
response<empty_body> res{status::ok, 11};
res.set(field::server, "Beast");
res.chunked(true);
response_serializer<empty_body> sr{res};
write_header(sock, sr);
// Obtain three body buffers up front
auto const cb1 = get_next_chunk_body();
auto const cb2 = get_next_chunk_body();
auto const cb3 = get_next_chunk_body();
// Manually emit a chunk by first writing the chunk-size header with the correct size
boost::asio::write(sock, chunk_header{
boost::asio::buffer_size(cb1) +
boost::asio::buffer_size(cb2) +
boost::asio::buffer_size(cb3)});
// And then output the chunk body in three pieces ("chunk the chunk")
boost::asio::write(sock, cb1);
boost::asio::write(sock, cb2);
boost::asio::write(sock, cb3);
// When we go this deep, we are also responsible for the terminating CRLF
boost::asio::write(sock, chunk_crlf{});
//]
}
} // fxx()
//[http_snippet_12
/** Send a message to a stream synchronously.
@ -302,27 +474,6 @@ split_print_cxx14(message<isRequest, Body, Fields> const& m)
//]
#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";
}
};
//]
// Highest snippet:
} // doc_http_snippets

View File

@ -629,15 +629,16 @@ public:
}
}
template<class Stream,
bool isRequest, class Body, class Fields,
class Decorator = no_chunk_decorator>
template<
class Stream,
bool isRequest, class Body, class Fields>
void
do_write(Stream& stream, message<
isRequest, Body, Fields> const& m, error_code& ec,
Decorator const& decorator = Decorator{})
do_write(
Stream& stream,
message<isRequest, Body, Fields> const& m,
error_code& ec)
{
serializer<isRequest, Body, Fields, Decorator> sr{m, decorator};
serializer<isRequest, Body, Fields> sr{m};
for(;;)
{
stream.nwrite = 0;
@ -650,16 +651,17 @@ public:
}
}
template<class Stream,
bool isRequest, class Body, class Fields,
class Decorator = no_chunk_decorator>
template<
class Stream,
bool isRequest, class Body, class Fields>
void
do_async_write(Stream& stream,
do_async_write(
Stream& stream,
message<isRequest, Body, Fields> const& m,
error_code& ec, yield_context yield,
Decorator const& decorator = Decorator{})
error_code& ec,
yield_context yield)
{
serializer<isRequest, Body, Fields, Decorator> sr{m, decorator};
serializer<isRequest, Body, Fields> sr{m};
for(;;)
{
stream.nwrite = 0;
@ -672,25 +674,6 @@ public:
}
}
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)
@ -768,14 +751,6 @@ public:
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;
@ -784,14 +759,6 @@ public:
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;