博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
php browserkit,HttpBrowser.php
阅读量:6361 次
发布时间:2019-06-23

本文共 3911 字,大约阅读时间需要 13 分钟。

/*

* This file is part of the Symfony package.

*

* (c) Fabien Potencier

*

* For the full copyright and license information, please view the LICENSE

* file that was distributed with this source code.

*/

namespace Symfony\Component\BrowserKit;

use Symfony\Component\HttpClient\HttpClient;

use Symfony\Component\Mime\Part\AbstractPart;

use Symfony\Component\Mime\Part\DataPart;

use Symfony\Component\Mime\Part\Multipart\FormDataPart;

use Symfony\Component\Mime\Part\TextPart;

use Symfony\Contracts\HttpClient\HttpClientInterface;

/**

* An implementation of a browser using the HttpClient component

* to make real HTTP requests.

*

* @author Fabien Potencier

*/

class HttpBrowser extends AbstractBrowser

{

private $client;

public function __construct(HttpClientInterface $client = null, History $history = null, CookieJar $cookieJar = null)

{

if (!$client && !class_exists(HttpClient::class)) {

throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));

}

$this->client = $client ?? HttpClient::create();

parent::__construct([], $history, $cookieJar);

}

/**

* @param Request $request

*/

protected function doRequest($request): Response

{

$headers = $this->getHeaders($request);

[$body, $extraHeaders] = $this->getBodyAndExtraHeaders($request, $headers);

$response = $this->client->request($request->getMethod(), $request->getUri(), [

'headers' => array_merge($headers, $extraHeaders),

'body' => $body,

'max_redirects' => 0,

]);

return new Response($response->getContent(false), $response->getStatusCode(), $response->getHeaders(false));

}

/**

* @return array [$body, $headers]

*/

private function getBodyAndExtraHeaders(Request $request, array $headers): array

{

if (\in_array($request->getMethod(), ['GET', 'HEAD'])) {

return ['', []];

}

if (!class_exists(AbstractPart::class)) {

throw new \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".');

}

if (null !== $content = $request->getContent()) {

if (isset($headers['content-type'])) {

return [$content, []];

}

$part = new TextPart($content, 'utf-8', 'plain', '8bit');

return [$part->bodyToString(), $part->getPreparedHeaders()->toArray()];

}

$fields = $request->getParameters();

if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) {

$part = new FormDataPart(array_merge($fields, $uploadedFiles));

return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()];

}

if (empty($fields)) {

return ['', []];

}

return [http_build_query($fields, '', '&', \PHP_QUERY_RFC1738), ['Content-Type' => 'application/x-www-form-urlencoded']];

}

protected function getHeaders(Request $request): array

{

$headers = [];

foreach ($request->getServer() as $key => $value) {

$key = strtolower(str_replace('_', '-', $key));

$contentHeaders = ['content-length' => true, 'content-md5' => true, 'content-type' => true];

if (0 === strpos($key, 'http-')) {

$headers[substr($key, 5)] = $value;

} elseif (isset($contentHeaders[$key])) {

// CONTENT_* are not prefixed with HTTP_

$headers[$key] = $value;

}

}

$cookies = [];

foreach ($this->getCookieJar()->allRawValues($request->getUri()) as $name => $value) {

$cookies[] = $name.'='.$value;

}

if ($cookies) {

$headers['cookie'] = implode('; ', $cookies);

}

return $headers;

}

/**

* Recursively go through the list. If the file has a tmp_name, convert it to a DataPart.

* Keep the original hierarchy.

*/

private function getUploadedFiles(array $files): array

{

$uploadedFiles = [];

foreach ($files as $name => $file) {

if (!\is_array($file)) {

return $uploadedFiles;

}

if (!isset($file['tmp_name'])) {

$uploadedFiles[$name] = $this->getUploadedFiles($file);

}

if (isset($file['tmp_name'])) {

$uploadedFiles[$name] = DataPart::fromPath($file['tmp_name'], $file['name']);

}

}

return $uploadedFiles;

}

}

一键复制

编辑

Web IDE

原始数据

按行查看

历史

转载地址:http://hjima.baihongyu.com/

你可能感兴趣的文章
java动态代理
查看>>
node.js原型继承
查看>>
揭露让Linux与Windows隔阂消失的奥秘(1)
查看>>
我的友情链接
查看>>
Mysql备份和恢复策略
查看>>
AS开发JNI步骤
查看>>
二分查找,php
查看>>
Python——eventlet.greenthread
查看>>
记大众点评之面试经历
查看>>
第三章:基本概念
查看>>
Jersey+mybatis实现web项目第一篇
查看>>
C++形参中const char * 与 char * 的区别
查看>>
espresso 2.0.4 Apple Xcode 4.4.1 coteditor 价格
查看>>
Object-C中emoji与json的问题
查看>>
linux 命令
查看>>
灾后重建
查看>>
Nothing 和 Is
查看>>
第一个sprint冲刺第三天
查看>>
r 选取从小到大的数据_r 选取数据库
查看>>
springMVC3学习(九)--redirect和forward跳转
查看>>