Chris@0
|
1 <?php
|
Chris@0
|
2 namespace GuzzleHttp\Psr7;
|
Chris@0
|
3
|
Chris@0
|
4 use Psr\Http\Message\StreamInterface;
|
Chris@0
|
5
|
Chris@0
|
6 /**
|
Chris@0
|
7 * Stream that when read returns bytes for a streaming multipart or
|
Chris@0
|
8 * multipart/form-data stream.
|
Chris@0
|
9 */
|
Chris@0
|
10 class MultipartStream implements StreamInterface
|
Chris@0
|
11 {
|
Chris@0
|
12 use StreamDecoratorTrait;
|
Chris@0
|
13
|
Chris@0
|
14 private $boundary;
|
Chris@0
|
15
|
Chris@0
|
16 /**
|
Chris@0
|
17 * @param array $elements Array of associative arrays, each containing a
|
Chris@0
|
18 * required "name" key mapping to the form field,
|
Chris@0
|
19 * name, a required "contents" key mapping to a
|
Chris@0
|
20 * StreamInterface/resource/string, an optional
|
Chris@0
|
21 * "headers" associative array of custom headers,
|
Chris@0
|
22 * and an optional "filename" key mapping to a
|
Chris@0
|
23 * string to send as the filename in the part.
|
Chris@0
|
24 * @param string $boundary You can optionally provide a specific boundary
|
Chris@0
|
25 *
|
Chris@0
|
26 * @throws \InvalidArgumentException
|
Chris@0
|
27 */
|
Chris@0
|
28 public function __construct(array $elements = [], $boundary = null)
|
Chris@0
|
29 {
|
Chris@0
|
30 $this->boundary = $boundary ?: sha1(uniqid('', true));
|
Chris@0
|
31 $this->stream = $this->createStream($elements);
|
Chris@0
|
32 }
|
Chris@0
|
33
|
Chris@0
|
34 /**
|
Chris@0
|
35 * Get the boundary
|
Chris@0
|
36 *
|
Chris@0
|
37 * @return string
|
Chris@0
|
38 */
|
Chris@0
|
39 public function getBoundary()
|
Chris@0
|
40 {
|
Chris@0
|
41 return $this->boundary;
|
Chris@0
|
42 }
|
Chris@0
|
43
|
Chris@0
|
44 public function isWritable()
|
Chris@0
|
45 {
|
Chris@0
|
46 return false;
|
Chris@0
|
47 }
|
Chris@0
|
48
|
Chris@0
|
49 /**
|
Chris@0
|
50 * Get the headers needed before transferring the content of a POST file
|
Chris@0
|
51 */
|
Chris@0
|
52 private function getHeaders(array $headers)
|
Chris@0
|
53 {
|
Chris@0
|
54 $str = '';
|
Chris@0
|
55 foreach ($headers as $key => $value) {
|
Chris@0
|
56 $str .= "{$key}: {$value}\r\n";
|
Chris@0
|
57 }
|
Chris@0
|
58
|
Chris@0
|
59 return "--{$this->boundary}\r\n" . trim($str) . "\r\n\r\n";
|
Chris@0
|
60 }
|
Chris@0
|
61
|
Chris@0
|
62 /**
|
Chris@0
|
63 * Create the aggregate stream that will be used to upload the POST data
|
Chris@0
|
64 */
|
Chris@0
|
65 protected function createStream(array $elements)
|
Chris@0
|
66 {
|
Chris@0
|
67 $stream = new AppendStream();
|
Chris@0
|
68
|
Chris@0
|
69 foreach ($elements as $element) {
|
Chris@0
|
70 $this->addElement($stream, $element);
|
Chris@0
|
71 }
|
Chris@0
|
72
|
Chris@0
|
73 // Add the trailing boundary with CRLF
|
Chris@0
|
74 $stream->addStream(stream_for("--{$this->boundary}--\r\n"));
|
Chris@0
|
75
|
Chris@0
|
76 return $stream;
|
Chris@0
|
77 }
|
Chris@0
|
78
|
Chris@0
|
79 private function addElement(AppendStream $stream, array $element)
|
Chris@0
|
80 {
|
Chris@0
|
81 foreach (['contents', 'name'] as $key) {
|
Chris@0
|
82 if (!array_key_exists($key, $element)) {
|
Chris@0
|
83 throw new \InvalidArgumentException("A '{$key}' key is required");
|
Chris@0
|
84 }
|
Chris@0
|
85 }
|
Chris@0
|
86
|
Chris@0
|
87 $element['contents'] = stream_for($element['contents']);
|
Chris@0
|
88
|
Chris@0
|
89 if (empty($element['filename'])) {
|
Chris@0
|
90 $uri = $element['contents']->getMetadata('uri');
|
Chris@0
|
91 if (substr($uri, 0, 6) !== 'php://') {
|
Chris@0
|
92 $element['filename'] = $uri;
|
Chris@0
|
93 }
|
Chris@0
|
94 }
|
Chris@0
|
95
|
Chris@0
|
96 list($body, $headers) = $this->createElement(
|
Chris@0
|
97 $element['name'],
|
Chris@0
|
98 $element['contents'],
|
Chris@0
|
99 isset($element['filename']) ? $element['filename'] : null,
|
Chris@0
|
100 isset($element['headers']) ? $element['headers'] : []
|
Chris@0
|
101 );
|
Chris@0
|
102
|
Chris@0
|
103 $stream->addStream(stream_for($this->getHeaders($headers)));
|
Chris@0
|
104 $stream->addStream($body);
|
Chris@0
|
105 $stream->addStream(stream_for("\r\n"));
|
Chris@0
|
106 }
|
Chris@0
|
107
|
Chris@0
|
108 /**
|
Chris@0
|
109 * @return array
|
Chris@0
|
110 */
|
Chris@0
|
111 private function createElement($name, StreamInterface $stream, $filename, array $headers)
|
Chris@0
|
112 {
|
Chris@0
|
113 // Set a default content-disposition header if one was no provided
|
Chris@0
|
114 $disposition = $this->getHeader($headers, 'content-disposition');
|
Chris@0
|
115 if (!$disposition) {
|
Chris@0
|
116 $headers['Content-Disposition'] = ($filename === '0' || $filename)
|
Chris@0
|
117 ? sprintf('form-data; name="%s"; filename="%s"',
|
Chris@0
|
118 $name,
|
Chris@0
|
119 basename($filename))
|
Chris@0
|
120 : "form-data; name=\"{$name}\"";
|
Chris@0
|
121 }
|
Chris@0
|
122
|
Chris@0
|
123 // Set a default content-length header if one was no provided
|
Chris@0
|
124 $length = $this->getHeader($headers, 'content-length');
|
Chris@0
|
125 if (!$length) {
|
Chris@0
|
126 if ($length = $stream->getSize()) {
|
Chris@0
|
127 $headers['Content-Length'] = (string) $length;
|
Chris@0
|
128 }
|
Chris@0
|
129 }
|
Chris@0
|
130
|
Chris@0
|
131 // Set a default Content-Type if one was not supplied
|
Chris@0
|
132 $type = $this->getHeader($headers, 'content-type');
|
Chris@0
|
133 if (!$type && ($filename === '0' || $filename)) {
|
Chris@0
|
134 if ($type = mimetype_from_filename($filename)) {
|
Chris@0
|
135 $headers['Content-Type'] = $type;
|
Chris@0
|
136 }
|
Chris@0
|
137 }
|
Chris@0
|
138
|
Chris@0
|
139 return [$stream, $headers];
|
Chris@0
|
140 }
|
Chris@0
|
141
|
Chris@0
|
142 private function getHeader(array $headers, $key)
|
Chris@0
|
143 {
|
Chris@0
|
144 $lowercaseHeader = strtolower($key);
|
Chris@0
|
145 foreach ($headers as $k => $v) {
|
Chris@0
|
146 if (strtolower($k) === $lowercaseHeader) {
|
Chris@0
|
147 return $v;
|
Chris@0
|
148 }
|
Chris@0
|
149 }
|
Chris@0
|
150
|
Chris@0
|
151 return null;
|
Chris@0
|
152 }
|
Chris@0
|
153 }
|