scratch – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
87 office 1 <?php
2  
3 namespace GuzzleHttp\Adapter\Curl;
4  
5 use GuzzleHttp\Adapter\TransactionInterface;
6 use GuzzleHttp\Event\RequestEvents;
7 use GuzzleHttp\Message\MessageFactoryInterface;
8 use GuzzleHttp\Stream\Stream;
9 use GuzzleHttp\Stream\StreamInterface;
10  
11 /**
12 * Mediator between curl handles and request objects
13 */
14 class RequestMediator
15 {
16 /** @var TransactionInterface */
17 private $transaction;
18 /** @var MessageFactoryInterface */
19 private $messageFactory;
20 private $statusCode;
21 private $reasonPhrase;
22 private $body;
23 private $protocolVersion;
24 private $headers;
25  
26 /**
27 * @param TransactionInterface $transaction Transaction to populate
28 * @param MessageFactoryInterface $messageFactory Creates responses
29 */
30 public function __construct(
31 TransactionInterface $transaction,
32 MessageFactoryInterface $messageFactory
33 ) {
34 $this->transaction = $transaction;
35 $this->messageFactory = $messageFactory;
36 }
37  
38 /**
39 * Set the body that will hold the response body
40 *
41 * @param StreamInterface $body Response body
42 */
43 public function setResponseBody(StreamInterface $body = null)
44 {
45 $this->body = $body;
46 }
47  
48 /**
49 * Receive a response header from curl
50 *
51 * @param resource $curl Curl handle
52 * @param string $header Received header
53 *
54 * @return int
55 */
56 public function receiveResponseHeader($curl, $header)
57 {
58 static $normalize = ["\r", "\n"];
59 $length = strlen($header);
60 $header = str_replace($normalize, '', $header);
61  
62 if (strpos($header, 'HTTP/') === 0) {
63 $startLine = explode(' ', $header, 3);
64 // Only download the body to a target body when a successful
65 // response is received.
66 if ($startLine[1][0] != '2') {
67 $this->body = null;
68 }
69 $this->statusCode = $startLine[1];
70 $this->reasonPhrase = isset($startLine[2]) ? $startLine[2] : null;
71 $this->protocolVersion = substr($startLine[0], -3);
72 $this->headers = [];
73 } elseif ($pos = strpos($header, ':')) {
74 $this->headers[substr($header, 0, $pos)][] = substr($header, $pos + 1);
75 } elseif ($header == '' && $this->statusCode >= 200) {
76 $response = $this->messageFactory->createResponse(
77 $this->statusCode,
78 $this->headers,
79 $this->body,
80 [
81 'protocol_version' => $this->protocolVersion,
82 'reason_phrase' => $this->reasonPhrase
83 ]
84 );
85 $this->headers = $this->body = null;
86 $this->transaction->setResponse($response);
87 // Allows events to react before downloading any of the body
88 RequestEvents::emitHeaders($this->transaction);
89 }
90  
91 return $length;
92 }
93  
94 /**
95 * Write data to the response body of a request
96 *
97 * @param resource $curl
98 * @param string $write
99 *
100 * @return int
101 */
102 public function writeResponseBody($curl, $write)
103 {
104 if (!($response = $this->transaction->getResponse())) {
105 return 0;
106 }
107  
108 // Add a default body on the response if one was not found
109 if (!($body = $response->getBody())) {
110 $body = new Stream(fopen('php://temp', 'r+'));
111 $response->setBody($body);
112 }
113  
114 return $body->write($write);
115 }
116  
117 /**
118 * Read data from the request body and send it to curl
119 *
120 * @param resource $ch Curl handle
121 * @param resource $fd File descriptor
122 * @param int $length Amount of data to read
123 *
124 * @return string
125 */
126 public function readRequestBody($ch, $fd, $length)
127 {
128 return (string) $this->transaction->getRequest()->getBody()->read($length);
129 }
130 }