80 $logUri->convertToUnicode();
82 $this->
log(
"***CONNECT to {address} for URI {uri}\n",
Web\
HttpDebug::CONNECT, [
'address' => $this->socket->getAddress(),
'uri' => $logUri]);
87 $this->socket->connect();
89 catch (\RuntimeException $e)
94 $this->connectTimer?->stop();
96 $this->state = self::CONNECTED;
100 case self::CONNECTED:
101 case self::CONNECT_RECEIVED:
102 if ($this->state === self::CONNECTED && $this->useProxy &&
$uri->getScheme() ===
'https')
107 $this->state = self::CONNECT_SENT;
112 if (
$uri->getScheme() ===
'https')
116 $this->socket->setBlocking();
119 if ($this->socket->enableCrypto() ===
false)
124 $this->handshakeTimer?->stop();
132 if ($body->isSeekable())
137 $this->state = self::HEADERS_SENT;
142 case self::CONNECT_SENT:
152 $this->responseHeaders =
'';
154 $this->state = self::CONNECT_RECEIVED;
158 throw new Http\NetworkException(
$request,
'Error receiving the CONNECT response from the proxy: ' . $headers->getStatus() .
' ' . $headers->getReasonPhrase());
164 case self::HEADERS_SENT:
169 $this->requestTimer?->stop();
171 $this->state = self::BODY_SENT;
176 case self::BODY_SENT:
184 $this->response = $this->responseBuilder->createFromString($this->responseHeaders);
195 $this->state = self::HEADERS_RECEIVED;
200 $this->state = self::BODY_RECEIVED;
206 case self::HEADERS_RECEIVED:
211 $this->state = self::BODY_RECEIVED;
217 if ($this->state == self::BODY_RECEIVED)
219 $this->socket->close();
229 $this->response->adjustHeaders();
232 $this->totalTimer?->stop();
237 $promise?->fulfill($this->response);
242 $this->socket->close();
244 $promise?->reject($exception);
246 $this->
getLogger()?->error($exception->getMessage() .
"\n");
248 if ($promise ===
null)
344 if (!$body->eof() || $this->requestBodyPart !==
'')
346 if (!$body->eof() && strlen($this->requestBodyPart) < self::BUF_BODY_LEN)
348 $part = $body->read(self::BUF_BODY_LEN);
349 $this->requestBodyPart .= $part;
353 $result = $this->
write($this->requestBodyPart,
'Error sending the message body.');
355 $this->requestBodyPart = substr($this->requestBodyPart,
$result);
358 return ($body->eof() && $this->requestBodyPart ===
'');
400 $headers = $this->response->getHeadersCollection();
401 $body = $this->response->getBody();
403 $length = $headers->get(
'Content-Length');
405 if ($length !==
null && $length <= 0)
411 while (!$this->socket->eof())
413 if ($length !==
null)
415 $bufLength = min($length, self::BUF_READ_LEN);
419 $bufLength = self::BUF_READ_LEN;
424 $buf = $this->socket->read($bufLength);
426 catch (\RuntimeException)
441 catch (\RuntimeException)
446 if ($this->bodyLengthMax > 0 && $body->getSize() > $this->bodyLengthMax)
451 if ($length !==
null)
453 $length -= strlen($buf);
545 'connect' => $this->connectTimer?->get() ?? 0.0,
546 'handshake' => $this->handshakeTimer?->get() ?? 0.0,
547 'request' => $this->requestTimer?->get() ?? 0.0,
548 'total' => $this->totalTimer?->get() ?? 0.0,