Skip to content

Commit

Permalink
Improve host handler, fix swlib#15.
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed Nov 2, 2018
1 parent 89113e4 commit 955a8d3
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 9 deletions.
8 changes: 1 addition & 7 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -112,13 +112,7 @@ private function getConnectionTarget(): array
{
$host = $this->uri->getHost();
if (empty($host)) {
$host = explode('/', ($uri_string = (string)$this->uri))[0] ?? '';
if (empty($host) || !preg_match('/\.\w+$/', $host)) {
throw new \InvalidArgumentException('Host should not be empty!');
}
$uri_string = 'http://' . rtrim($uri_string, '/');
$this->uri = new Uri($uri_string);
$host = $this->uri->getHost();
throw new \InvalidArgumentException('Host should not be empty!');
}
$port = $this->uri->getRealPort();
$ssl = $this->getSSL();
Expand Down
11 changes: 9 additions & 2 deletions src/Saber.php
Original file line number Diff line number Diff line change
Expand Up @@ -368,13 +368,20 @@ public function setOptions(array $options = [], ?Request $request = null): self
$request = $this->raw;
$this->options += self::mergeOptions($options, $this->options);
}

// special options
$options += [
'base_uri' => $this->options['base_uri'] ?? null,
'uri' => $this->options['uri'] ?? null
];
if (empty($options['base_uri']) && !empty($options['uri'] && strpos($options['uri'], '://') === false)) {
// fix uri like localhost
$options['uri'] = "http://{$options['uri']}";
}
if ($this->session) {
$options['session'] = $this->raw->cookies;
}

self::transOptionsToRequest($options, $request);

return $this;
Expand Down Expand Up @@ -452,8 +459,8 @@ private static function transOptionsToRequest(array $options, Request $request)

if (isset($options['base_uri']) || isset($options['uri'])) {
$request->withUri(
Uri::resolve($options['base_uri'] ?? null, $options['uri'] ?? null),
!!$request->getHeader('Host')
Uri::resolve($options['base_uri'], $options['uri']),
$request->hasHeader('Host')
);
}

Expand Down

0 comments on commit 955a8d3

Please sign in to comment.