Open Journal Systems  3.3.0
PrepareBodyMiddleware.php
1 <?php
2 namespace GuzzleHttp;
3 
7 
13 {
15  private $nextHandler;
16 
20  public function __construct(callable $nextHandler)
21  {
22  $this->nextHandler = $nextHandler;
23  }
24 
31  public function __invoke(RequestInterface $request, array $options)
32  {
33  $fn = $this->nextHandler;
34 
35  // Don't do anything if the request has no body.
36  if ($request->getBody()->getSize() === 0) {
37  return $fn($request, $options);
38  }
39 
40  $modify = [];
41 
42  // Add a default content-type if possible.
43  if (!$request->hasHeader('Content-Type')) {
44  if ($uri = $request->getBody()->getMetadata('uri')) {
45  if ($type = Psr7\mimetype_from_filename($uri)) {
46  $modify['set_headers']['Content-Type'] = $type;
47  }
48  }
49  }
50 
51  // Add a default content-length or transfer-encoding header.
52  if (!$request->hasHeader('Content-Length')
53  && !$request->hasHeader('Transfer-Encoding')
54  ) {
55  $size = $request->getBody()->getSize();
56  if ($size !== null) {
57  $modify['set_headers']['Content-Length'] = $size;
58  } else {
59  $modify['set_headers']['Transfer-Encoding'] = 'chunked';
60  }
61  }
62 
63  // Add the expect header if needed.
64  $this->addExpectHeader($request, $options, $modify);
65 
66  return $fn(Psr7\modify_request($request, $modify), $options);
67  }
68 
74  private function addExpectHeader(
75  RequestInterface $request,
76  array $options,
77  array &$modify
78  ) {
79  // Determine if the Expect header should be used
80  if ($request->hasHeader('Expect')) {
81  return;
82  }
83 
84  $expect = isset($options['expect']) ? $options['expect'] : null;
85 
86  // Return if disabled or if you're not using HTTP/1.1 or HTTP/2.0
87  if ($expect === false || $request->getProtocolVersion() < 1.1) {
88  return;
89  }
90 
91  // The expect header is unconditionally enabled
92  if ($expect === true) {
93  $modify['set_headers']['Expect'] = '100-Continue';
94  return;
95  }
96 
97  // By default, send the expect header when the payload is > 1mb
98  if ($expect === null) {
99  $expect = 1048576;
100  }
101 
102  // Always add if the body cannot be rewound, the size cannot be
103  // determined, or the size is greater than the cutoff threshold
104  $body = $request->getBody();
105  $size = $body->getSize();
106 
107  if ($size === null || $size >= (int) $expect || !$body->isSeekable()) {
108  $modify['set_headers']['Expect'] = '100-Continue';
109  }
110  }
111 }
GuzzleHttp\PrepareBodyMiddleware\__invoke
__invoke(RequestInterface $request, array $options)
Definition: PrepareBodyMiddleware.php:34
GuzzleHttp
Definition: vendor/guzzlehttp/guzzle/src/Client.php:2
GuzzleHttp\Promise\PromiseInterface
Definition: PromiseInterface.php:13
Psr\Http\Message\RequestInterface
Definition: vendor/psr/http-message/src/RequestInterface.php:24
Psr\Http\Message\MessageInterface\getBody
getBody()
GuzzleHttp\Psr7\modify_request
modify_request(RequestInterface $request, array $changes)
Definition: guzzlehttp/psr7/src/functions.php:201
GuzzleHttp\Psr7\mimetype_from_filename
mimetype_from_filename($filename)
Definition: guzzlehttp/psr7/src/functions.php:624
GuzzleHttp\Psr7
Definition: AppendStream.php:2
GuzzleHttp\PrepareBodyMiddleware
Definition: PrepareBodyMiddleware.php:12
Psr\Http\Message\MessageInterface\hasHeader
hasHeader($name)
Psr\Http\Message\MessageInterface\getProtocolVersion
getProtocolVersion()
GuzzleHttp\PrepareBodyMiddleware\__construct
__construct(callable $nextHandler)
Definition: PrepareBodyMiddleware.php:23