Skip to content

Commit

Permalink
Fix #124
Browse files Browse the repository at this point in the history
  • Loading branch information
twose committed May 20, 2021
1 parent 5c240e7 commit 6b8843e
Showing 1 changed file with 24 additions and 16 deletions.
40 changes: 24 additions & 16 deletions src/Request.php
Original file line number Diff line number Diff line change
Expand Up @@ -662,30 +662,16 @@ public function exec()
}
$this->client = $client ?? $client_pool->createEx($connectionInfo, !$this->use_pool);
}

/** Clear useless cookies property */
$this->client->cookies = null;

/** Set request headers */
$cookie = $this->cookies->toRequestString($this->uri);

// Ensure Host is the first header.
// See: http://tools.ietf.org/html/rfc7230#section-5.4
$headers = ['Host' => $this->getHeaderLine('Host') ?: $this->uri->getHost()] +
$this->getHeaders(true, true);
if (!empty($cookie) && empty($headers['Cookie'])) {
$headers['Cookie'] = $cookie;
}
$this->client->setHeaders($headers);

/** Set method */
$this->client->setMethod($this->getMethod());
/** Get body as string */
$body = (string) ($this->getBody() ?? '');
/** Set Upload file */
$files = $this->getUploadedFiles();
if (!empty($files)) {
/** @var $file SwUploadFile */
$fileAdded = false;
foreach ($files as $key => $file) {
$file_options = [
$file->getFilePath(),
Expand All @@ -703,7 +689,11 @@ public function exec()
if ($file_size = $file->getSize()) {
$file_options[] = $file_size;
}
$this->client->addFile(...$file_options);
$fileAdded = $fileAdded || $this->client->addFile(...$file_options);
}
if ($fileAdded) {
/** it will be set by Swoole kernel */
$this->withoutHeader('content-type');
}
if ($body !== '') {
parse_str($body, $body);
Expand All @@ -717,6 +707,24 @@ public function exec()
}
}

/** Code of set headers must be placed here (after parse body),
* because they may changed during parse body */

/** Set method */
$this->client->setMethod($this->getMethod());

/** Set request headers */
$cookie = $this->cookies->toRequestString($this->uri);

// Ensure Host is the first header.
// See: http://tools.ietf.org/html/rfc7230#section-5.4
$headers = ['Host' => $this->getHeaderLine('Host') ?: $this->uri->getHost()] +
$this->getHeaders(true, true);
if (!empty($cookie) && empty($headers['Cookie'])) {
$headers['Cookie'] = $cookie;
}
$this->client->setHeaders($headers);

/** calc timeout value */
if ($this->_redirect_times > 0) {
$timeout = $this->getTimeout() - (microtime(true) - $this->_start_time);
Expand Down

0 comments on commit 6b8843e

Please sign in to comment.