Skip to content

Multipart stream docs #120

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 9 commits into from
Jul 19, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
63 changes: 63 additions & 0 deletions components/multipart-stream-builder.rst
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
Multipart Stream Builder
========================

A multipart stream is a special kind of stream that is used to transfer files over HTTP. There is currently no PSR-7 support for multipart streams as they are considered to be normal streams with a special content. A multipart stream HTTP request may look like this:

.. code-block:: none

POST / HTTP/1.1
Host: example.com
Content-Type: multipart/form-data; boundary=578de3b0e3c46

--578de3b0e3c46
Content-Disposition: form-data; name="foo"
Content-Length: 15

A normal stream
--578de3b0e3c46
Content-Disposition: form-data; name="bar"; filename="bar.png"
Content-Length: 71
Content-Type: image/png

?PNG

???
IHDR??? ??? ?????? ???? IDATxc???51?)?:??????IEND?B`?
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

are the question marks intentional ? i think it was something else before. but binary data in the doc file could be a problem when other contributors edit it to update doc. so maybe thats the right thing.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The question mars are HDR��������������� �����IDATx�c���� which does not make any sense either.. I just represent binary data..

--578de3b0e3c46
Content-Type: text/plain
Content-Disposition: form-data; name="baz"
Content-Length: 6

string
--578de3b0e3c46--


In the request above you see a set of HTTP headers and a body with two streams. The body starts and ends with a "boundary" and it is also this boundary that separates the streams. That boundary also needs to be specified in the ``Content-Type`` header.

Building a multipart stream
```````````````````````````

To build a multipart stream you may use the ``MultipartStreamBuilder``. It is not coupled to any stream implementation so it needs a ``StreamFactory`` to create the streams.

.. code-block:: php

$streamFactory = StreamFactoryDiscovery::find();
$builder = new MultipartStreamBuilder($streamFactory);
$builder
->addResource('foo', $stream)
->addResource('bar', fopen($filePath, 'r'), ['filename' => 'bar.png'])
->addResource('baz', 'string', ['headers' => ['Content-Type' => 'text/plain']]);

$multipartStream = $builder->build();
$boundary = $builder->getBoundary();

$request = MessageFactoryDiscovery::find()->createRequest(
'POST',
'http://example.com',
['Content-Type' => 'multipart/form-data; boundary='.$boundary],
$multipartStream
);

$response = HttpClientDiscovery::find()->sendRequest($request);

The second parameter of ``MultipartStreamBuilder::addResource()`` is the content of the stream. The supported input is the same as ``StreamFactory::createStream()``.
1 change: 1 addition & 0 deletions index.rst
Original file line number Diff line number Diff line change
Expand Up @@ -83,6 +83,7 @@ for discussion around a future HTTP client PSR.
components/adapter-integration-tests
components/promise
discovery
components/multipart-stream-builder

.. toctree::
:hidden:
Expand Down
1 change: 1 addition & 0 deletions spelling_word_list.txt
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@ namespace
plugin
plugins
matchers
multipart
param
params
Puli
Expand Down