Skip to content

Commit

Permalink
Keep original peer_name for HTTPS
Browse files Browse the repository at this point in the history
  • Loading branch information
hightman committed Dec 8, 2017
1 parent fdabe6e commit bced617
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 11 deletions.
9 changes: 6 additions & 3 deletions src/Connection.php
Original file line number Diff line number Diff line change
Expand Up @@ -90,6 +90,7 @@ public static function connect($conn, $arg = null)
Client::debug('create conn \'', $conn, '\'');
}
if ($obj !== null) {
$obj->arg = $arg;
if ($obj->flag & self::FLAG_OPENED) {
$obj->flag |= self::FLAG_REUSED;
} else {
Expand All @@ -100,7 +101,6 @@ public static function connect($conn, $arg = null)
$obj->flag |= self::FLAG_BUSY;
$obj->outBuf = null;
$obj->outLen = 0;
$obj->arg = $arg;
}
return $obj;
}
Expand Down Expand Up @@ -323,15 +323,18 @@ protected function openSock($repeat = false)
$this->flag |= self::FLAG_NEW2;
}
// async-connect
$ctx = ['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]];
if (isset(self::$_socks5['conn'])) {
$this->proxyState = 1;
$conn = self::$_socks5['conn'];
} else {
$this->proxyState = 0;
$conn = $this->conn;
if (!strncmp($conn, 'ssl:', 4) && $this->arg instanceof Processor) {
$ctx['ssl']['peer_name'] = $this->arg->req->getUrlParam('host');
}
}
$ctx = stream_context_create(['ssl' => ['verify_peer' => false, 'verify_peer_name' => false]]);
$this->sock = @stream_socket_client($conn, $errno, $error, 10, STREAM_CLIENT_ASYNC_CONNECT, $ctx);
$this->sock = @stream_socket_client($conn, $errno, $error, 10, STREAM_CLIENT_ASYNC_CONNECT, stream_context_create($ctx));
if ($this->sock === false) {
Client::debug($repeat ? 're' : '', 'open \'', $conn, '\' failed: ', $error);
self::$_lastError = $error;
Expand Down
29 changes: 21 additions & 8 deletions src/HeaderTrait.php
Original file line number Diff line number Diff line change
Expand Up @@ -25,15 +25,18 @@ trait HeaderTrait
* Set http header or headers
* @param mixed $key string key or key-value pairs to set multiple headers.
* @param string $value the header value when key is string, set null to remove header.
* @param boolean $toLower convert key to lowercase
*/
public function setHeader($key, $value = null)
public function setHeader($key, $value = null, $toLower = true)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->setHeader($k, $v);
}
} else {
$key = strtolower($key);
if ($toLower === true) {
$key = strtolower($key);
}
if ($value === null) {
unset($this->_headers[$key]);
} else {
Expand All @@ -46,16 +49,19 @@ public function setHeader($key, $value = null)
* Add http header or headers
* @param mixed $key string key or key-value pairs to be added.
* @param string $value the header value when key is string.
* @param boolean $toLower convert key to lowercase
*/
public function addHeader($key, $value = null)
public function addHeader($key, $value = null, $toLower = true)
{
if (is_array($key)) {
foreach ($key as $k => $v) {
$this->addHeader($k, $v);
}
} else {
if ($value !== null) {
$key = strtolower($key);
if ($toLower === true) {
$key = strtolower($key);
}
if (!isset($this->_headers[$key])) {
$this->_headers[$key] = $value;
} else {
Expand All @@ -80,25 +86,32 @@ public function clearHeader()
/**
* Get a http header or all http headers
* @param mixed $key the header key to be got, or null to get all headers
* @param boolean $toLower convert key to lowercase
* @return array|string the header value, or headers array when key is null.
*/
public function getHeader($key = null)
public function getHeader($key = null, $toLower = true)
{
if ($key === null) {
return $this->_headers;
}
$key = strtolower($key);
if ($toLower === true) {
$key = strtolower($key);
}
return isset($this->_headers[$key]) ? $this->_headers[$key] : null;
}

/**
* Check HTTP header is set or not
* @param string $key the header key to be check, not case sensitive
* @param boolean $toLower convert key to lowercase
* @return boolean if there is http header with the name.
*/
public function hasHeader($key)
public function hasHeader($key, $toLower = true)
{
return isset($this->_headers[strtolower($key)]);
if ($toLower === true) {
$key = strtolower($key);
}
return isset($this->_headers[$key]);
}

/**
Expand Down

0 comments on commit bced617

Please sign in to comment.