Skip to content

Commit e2d4d3a

Browse files
committed
added php 8.1 support, added multipart request handler in guzzle
1 parent 4ef375a commit e2d4d3a

15 files changed

+127
-30
lines changed

README.md

+7-6
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
# PHP Proxy
1+
# PHP Request Proxy
2+
3+
> This package is forked from `jenssegers/php-proxy` and modified. It is not compatible with the original package. Basically the original package is not maintained anymore thats why I forked it.
24
3-
[![Build Status](http://img.shields.io/travis/jenssegers/php-proxy.svg)](https://travis-ci.org/jenssegers/php-proxy) [![Coverage Status](http://img.shields.io/coveralls/jenssegers/php-proxy.svg)](https://coveralls.io/r/jenssegers/php-proxy?branch=master)
45

56
This is a HTTP/HTTPS proxy script that forwards requests to a different server and returns the response. The Proxy class uses PSR7 request/response objects as input/output, and uses Guzzle to do the actual HTTP request.
67

@@ -9,17 +10,17 @@ This is a HTTP/HTTPS proxy script that forwards requests to a different server a
910
Install using composer:
1011

1112
```
12-
composer require jenssegers/proxy
13+
composer require nahid/request-proxy
1314
```
1415

1516
## Example
1617

1718
The following example creates a request object, based on the current browser request, and forwards it to `example.com`. The `RemoveEncodingFilter` removes the encoding headers from the original response so that the current webserver can set these correctly.
1819

1920
```php
20-
use Proxy\Proxy;
21-
use Proxy\Adapter\Guzzle\GuzzleAdapter;
22-
use Proxy\Filter\RemoveEncodingFilter;
21+
use Nahid\RequestProxy\Proxy;
22+
use Nahid\RequestProxy\Adapter\Guzzle\GuzzleAdapter;
23+
use Nahid\RequestProxy\Filter\RemoveEncodingFilter;
2324
use Laminas\Diactoros\ServerRequestFactory;
2425

2526
// Create a PSR7 request based on the current browser request.

composer.json

+11-7
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
{
2-
"name": "jenssegers/proxy",
3-
"description": "Proxy library that forwards requests to the desired url and returns the response.",
4-
"keywords": ["proxy"],
5-
"homepage": "https://github.com/jenssegers/php-proxy",
2+
"name": "nahid/request-proxy",
3+
"description": "Request proxy library helps you to proxy your request to another server",
4+
"keywords": ["proxy", "php", "gateway", "http", "https"],
5+
"homepage": "https://github.com/nahid/php-request-proxy",
66
"license": "MIT",
77
"authors": [
88
{
@@ -13,10 +13,14 @@
1313
"name": "Ota Mares",
1414
"email": "[email protected]",
1515
"homepage": "http://www.rebuy.de"
16+
},
17+
{
18+
"name": "Nahid Bin Azhar",
19+
"email": "[email protected]"
1620
}
1721
],
1822
"require": {
19-
"php": "^5.6 || ^7.0",
23+
"php": "^5.6 || ^7.0 || ^8.0",
2024
"psr/http-message": "^1.0",
2125
"guzzlehttp/guzzle": "^6.0",
2226
"laminas/laminas-diactoros": "^2.0",
@@ -30,12 +34,12 @@
3034
},
3135
"autoload": {
3236
"psr-4": {
33-
"Proxy\\": "src"
37+
"Nahid\\RequestProxy\\": "src"
3438
}
3539
},
3640
"autoload-dev": {
3741
"psr-4": {
38-
"Proxy\\": "tests"
42+
"Nahid\\RequestProxy\\": "tests"
3943
}
4044
}
4145
}

src/Adapter/AdapterInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Proxy\Adapter;
3+
namespace Nahid\RequestProxy\Adapter;
44

55
use Psr\Http\Message\RequestInterface;
66
use Psr\Http\Message\ResponseInterface;

src/Adapter/Dummy/DummyAdapter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,8 @@
11
<?php
22

3-
namespace Proxy\Adapter\Dummy;
3+
namespace Nahid\RequestProxy\Adapter\Dummy;
44

5-
use Proxy\Adapter\AdapterInterface;
5+
use Nahid\RequestProxy\Adapter\AdapterInterface;
66
use Psr\Http\Message\RequestInterface;
77
use Laminas\Diactoros\Response;
88

src/Adapter/Guzzle/GuzzleAdapter.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
<?php
22

3-
namespace Proxy\Adapter\Guzzle;
3+
namespace Nahid\RequestProxy\Adapter\Guzzle;
44

55
use GuzzleHttp\Client;
6-
use Proxy\Adapter\AdapterInterface;
6+
use Nahid\RequestProxy\Adapter\AdapterInterface;
77
use Psr\Http\Message\RequestInterface;
88

99
class GuzzleAdapter implements AdapterInterface
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
<?php
2+
3+
namespace Nahid\RequestProxy\Adapter\Guzzle;
4+
5+
use GuzzleHttp\Psr7\MultipartStream;
6+
use Laminas\Diactoros\UploadedFile;
7+
use Psr\Http\Message\RequestInterface;
8+
9+
class MultipartStreamHandler
10+
{
11+
public function __invoke(callable $handler)
12+
{
13+
return function(RequestInterface $request, array $options) use ($handler){
14+
$contentType = $request->getHeader('Content-Type')[0];
15+
if(!preg_match('#^multipart/form-data; boundary=(.*)#',$contentType,$matches)){
16+
return $handler($request,$options);
17+
}
18+
19+
20+
$boundary = $matches[1];
21+
$files = $request->getUploadedFiles();
22+
$postFields = $request->getParsedBody();
23+
24+
$fields = $this->postFields($postFields, '', []);
25+
$files = $this->files($files, '', []);
26+
27+
$elements = [ ... $files, ... $fields];
28+
29+
$multiStream = new MultipartStream($elements,$boundary);
30+
$request = $request->withBody($multiStream);
31+
32+
return $handler($request,$options);
33+
};
34+
}
35+
36+
37+
38+
protected function files(array $files, $fileName = '', $elements = []) {
39+
foreach($files as $name => $file){
40+
if(empty($fileName)){
41+
$fileName .= $name;
42+
} else {
43+
$fileName .= '[' . $name . ']';
44+
}
45+
46+
if (is_array($file)) {
47+
$elements = $this->files($file, $fileName, $elements);
48+
}
49+
50+
/** @var UploadedFile $file */
51+
52+
if($file instanceof UploadedFile) {
53+
$elements[] = [
54+
'name' => $fileName,
55+
'contents' => $file->getStream(),
56+
'filename' => $file->getClientFilename(),
57+
];
58+
59+
$fileName = '';
60+
}
61+
62+
}
63+
64+
return $elements;
65+
}
66+
67+
protected function postFields(array $postFields, $fieldName = '', $elements = []) {
68+
foreach($postFields as $name => $postField){
69+
if(empty($fieldName)){
70+
$fieldName .= $name;
71+
} else {
72+
$fieldName .= '[' . $name . ']';
73+
}
74+
75+
if (is_array($postField)) {
76+
$elements = $this->postFields($postField, $fieldName, $elements);
77+
}
78+
79+
if(is_string($postField)) {
80+
$elements[] = [
81+
'name' => $fieldName,
82+
'contents' => $postField,
83+
];
84+
85+
$fieldName = '';
86+
}
87+
}
88+
89+
return $elements;
90+
}
91+
92+
}

src/Exception/InvalidArgumentException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Proxy\Exception;
3+
namespace Nahid\RequestProxy\Exception;
44

55
class InvalidArgumentException extends \InvalidArgumentException
66
{

src/Exception/UnexpectedValueException.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Proxy\Exception;
3+
namespace Nahid\RequestProxy\Exception;
44

55
class UnexpectedValueException extends \UnexpectedValueException
66
{

src/Filter/FilterInterface.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Proxy\Filter;
3+
namespace Nahid\RequestProxy\Filter;
44

55
use Psr\Http\Message\RequestInterface;
66
use Psr\Http\Message\ResponseInterface;

src/Filter/RemoveEncodingFilter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Proxy\Filter;
3+
namespace Nahid\RequestProxy\Filter;
44

55
use Psr\Http\Message\RequestInterface;
66
use Psr\Http\Message\ResponseInterface;

src/Filter/RemoveLocationFilter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Proxy\Filter;
3+
namespace Nahid\RequestProxy\Filter;
44

55
use Psr\Http\Message\RequestInterface;
66
use Psr\Http\Message\ResponseInterface;

src/Filter/RewriteLocationFilter.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
<?php
22

3-
namespace Proxy\Filter;
3+
namespace Nahid\RequestProxy\Filter;
44

55
use Psr\Http\Message\RequestInterface;
66
use Psr\Http\Message\ResponseInterface;

src/Proxy.php

+3-3
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,10 @@
11
<?php
22

3-
namespace Proxy;
3+
namespace Nahid\RequestProxy;
44

55
use GuzzleHttp\Exception\ClientException;
6-
use Proxy\Adapter\AdapterInterface;
7-
use Proxy\Exception\UnexpectedValueException;
6+
use Nahid\RequestProxy\Adapter\AdapterInterface;
7+
use Nahid\RequestProxy\Exception\UnexpectedValueException;
88
use Psr\Http\Message\RequestInterface;
99
use Psr\Http\Message\ResponseInterface;
1010
use Relay\RelayBuilder;

tests/Proxy/Adapter/Guzzle/GuzzleAdapterTest.php

+1-1
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
use GuzzleHttp\Handler\MockHandler;
77
use GuzzleHttp\Psr7\Response as GuzzleResponse;
88
use PHPUnit\Framework\TestCase;
9-
use Proxy\Adapter\Guzzle\GuzzleAdapter;
9+
use Nahid\RequestProxy\Adapter\Guzzle\GuzzleAdapter;
1010
use Psr\Http\Message\ResponseInterface;
1111
use Laminas\Diactoros\Request;
1212

tests/Proxy/ProxyTest.php

+2-2
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,8 @@
33
namespace Proxy;
44

55
use PHPUnit\Framework\TestCase;
6-
use Proxy\Adapter\Dummy\DummyAdapter;
7-
use Proxy\Exception\UnexpectedValueException;
6+
use Nahid\RequestProxy\Adapter\Dummy\DummyAdapter;
7+
use Nahid\RequestProxy\Exception\UnexpectedValueException;
88
use Psr\Http\Message\RequestInterface;
99
use Laminas\Diactoros\Request;
1010
use Laminas\Diactoros\Response;

0 commit comments

Comments
 (0)