PluginProbe
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell / 2.8.16
WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell v2.8.16
3.13.3 3.13.2 3.13.1 3.13.0 3.12.13 3.12.12 3.12.11 3.12.10 3.12.9 3.12.8 3.12.7 3.12.6 3.12.5 3.12.4 3.12.3 3.12.1 3.12.2 3.12.0 3.11.1 3.11.0 3.10.9 3.10.8 3.10.7 3.10.6 2.8.16 All 261 releases
wpfunnels / vendor / symfony / browser-kit / HttpBrowser.php

HttpBrowser.php in WPFunnels – Funnel Builder for WooCommerce with Checkout & One Click Upsell 2.8.16, at vendor/symfony/browser-kit/HttpBrowser.php

154 lines 5.2 KB
No matching file
Up and down to move Enter to open Esc to close
Raw Download Zip
1 <?php
2
3 /*
4 * This file is part of the Symfony package.
5 *
6 * (c) Fabien Potencier <[email protected]>
7 *
8 * For the full copyright and license information, please view the LICENSE
9 * file that was distributed with this source code.
10 */
11
12 namespace Symfony\Component\BrowserKit;
13
14 use Symfony\Component\HttpClient\HttpClient;
15 use Symfony\Component\Mime\Part\AbstractPart;
16 use Symfony\Component\Mime\Part\DataPart;
17 use Symfony\Component\Mime\Part\Multipart\FormDataPart;
18 use Symfony\Component\Mime\Part\TextPart;
19 use Symfony\Contracts\HttpClient\HttpClientInterface;
20
21 /**
22 * An implementation of a browser using the HttpClient component
23 * to make real HTTP requests.
24 *
25 * @author Fabien Potencier <[email protected]>
26 */
27 class HttpBrowser extends AbstractBrowser
28 {
29 private $client;
30
31 public function __construct(HttpClientInterface $client = null, History $history = null, CookieJar $cookieJar = null)
32 {
33 if (!$client && !class_exists(HttpClient::class)) {
34 throw new \LogicException(sprintf('You cannot use "%s" as the HttpClient component is not installed. Try running "composer require symfony/http-client".', __CLASS__));
35 }
36
37 $this->client = $client ?? HttpClient::create();
38
39 parent::__construct([], $history, $cookieJar);
40 }
41
42 /**
43 * @param Request $request
44 */
45 protected function doRequest(object $request): Response
46 {
47 $headers = $this->getHeaders($request);
48 [$body, $extraHeaders] = $this->getBodyAndExtraHeaders($request, $headers);
49
50 $response = $this->client->request($request->getMethod(), $request->getUri(), [
51 'headers' => array_merge($headers, $extraHeaders),
52 'body' => $body,
53 'max_redirects' => 0,
54 ]);
55
56 return new Response($response->getContent(false), $response->getStatusCode(), $response->getHeaders(false));
57 }
58
59 /**
60 * @return array [$body, $headers]
61 */
62 private function getBodyAndExtraHeaders(Request $request, array $headers): array
63 {
64 if (\in_array($request->getMethod(), ['GET', 'HEAD']) && !isset($headers['content-type'])) {
65 return ['', []];
66 }
67
68 if (!class_exists(AbstractPart::class)) {
69 throw new \LogicException('You cannot pass non-empty bodies as the Mime component is not installed. Try running "composer require symfony/mime".');
70 }
71
72 if (null !== $content = $request->getContent()) {
73 if (isset($headers['content-type'])) {
74 return [$content, []];
75 }
76
77 $part = new TextPart($content, 'utf-8', 'plain', '8bit');
78
79 return [$part->bodyToString(), $part->getPreparedHeaders()->toArray()];
80 }
81
82 $fields = $request->getParameters();
83
84 if ($uploadedFiles = $this->getUploadedFiles($request->getFiles())) {
85 $part = new FormDataPart(array_replace_recursive($fields, $uploadedFiles));
86
87 return [$part->bodyToIterable(), $part->getPreparedHeaders()->toArray()];
88 }
89
90 if (empty($fields)) {
91 return ['', []];
92 }
93
94 array_walk_recursive($fields, $caster = static function (&$v) use (&$caster) {
95 if (\is_object($v)) {
96 if ($vars = get_object_vars($v)) {
97 array_walk_recursive($vars, $caster);
98 $v = $vars;
99 } elseif (method_exists($v, '__toString')) {
100 $v = (string) $v;
101 }
102 }
103 });
104
105 return [http_build_query($fields, '', '&'), ['Content-Type' => 'application/x-www-form-urlencoded']];
106 }
107
108 protected function getHeaders(Request $request): array
109 {
110 $headers = [];
111 foreach ($request->getServer() as $key => $value) {
112 $key = strtolower(str_replace('_', '-', $key));
113 $contentHeaders = ['content-length' => true, 'content-md5' => true, 'content-type' => true];
114 if (str_starts_with($key, 'http-')) {
115 $headers[substr($key, 5)] = $value;
116 } elseif (isset($contentHeaders[$key])) {
117 // CONTENT_* are not prefixed with HTTP_
118 $headers[$key] = $value;
119 }
120 }
121 $cookies = [];
122 foreach ($this->getCookieJar()->allRawValues($request->getUri()) as $name => $value) {
123 $cookies[] = $name.'='.$value;
124 }
125 if ($cookies) {
126 $headers['cookie'] = implode('; ', $cookies);
127 }
128
129 return $headers;
130 }
131
132 /**
133 * Recursively go through the list. If the file has a tmp_name, convert it to a DataPart.
134 * Keep the original hierarchy.
135 */
136 private function getUploadedFiles(array $files): array
137 {
138 $uploadedFiles = [];
139 foreach ($files as $name => $file) {
140 if (!\is_array($file)) {
141 return $uploadedFiles;
142 }
143 if (!isset($file['tmp_name'])) {
144 $uploadedFiles[$name] = $this->getUploadedFiles($file);
145 }
146 if (isset($file['tmp_name'])) {
147 $uploadedFiles[$name] = DataPart::fromPath($file['tmp_name'], $file['name']);
148 }
149 }
150
151 return $uploadedFiles;
152 }
153 }
154