Last active
February 15, 2023 12:18
-
-
Save jimmygle/f1ee377ecb75812f3c39e9fc945c3f56 to your computer and use it in GitHub Desktop.
Basic Laravel 5.2 HTTP Proxy Server (with file upload support)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<?php | |
/** | |
* This is a really quick and dirty HTTP proxy server that supports GET and POST (with file upload) requests. | |
* | |
* Setup instructions: | |
* 1) Install Laravel 5.2 | |
* 2) `composer require guzzlehttp/guzzle` | |
* 3) Add this to your laravel routes.php | |
* 4) `php artisan serve --host=[server_ip] --port=80` | |
* | |
* Note: this is really sloppy and can very easily be refactored, but it works. Also could use some better commenting. | |
*/ | |
/* | |
|-------------------------------------------------------------------------- | |
| Application Routes | |
|-------------------------------------------------------------------------- | |
| | |
| Here is where you can register all of the routes for an application. | |
| It's a breeze. Simply tell Laravel the URIs it should respond to | |
| and give it the controller to call when that URI is requested. | |
| | |
*/ | |
use GuzzleHttp\Client; | |
use Illuminate\Http\Request; | |
use Symfony\Component\HttpKernel\Exception\HttpException; | |
use GuzzleHttp\Exception\ServerException; | |
use Symfony\Component\HttpFoundation\File\UploadedFile; | |
Route::any('{any?}', function (Request $request) { | |
$requestedUrl = "http://{$_SERVER['HTTP_HOST']}{$_SERVER['REQUEST_URI']}"; | |
try { | |
$client = new Client([]); | |
// Handle post requests with file uploads (as multipart/form-data; no streaming) | |
if ($request->isMethod('post')) { | |
$formParams = $request->all(); | |
$fileUploads = []; | |
foreach ($formParams as $key => $param) { | |
if ($param instanceof UploadedFile) { | |
$fileUploads[$key] = $param; | |
unset($formParams[$key]); | |
} | |
} | |
if (count($fileUploads) > 0) { | |
$multipartParams = []; | |
foreach ($formParams as $key => $value) { | |
$multipartParams[] = [ | |
'name' => $key, | |
'contents' => $value | |
]; | |
} | |
foreach ($fileUploads as $key => $value) { | |
$multipartParams[] = [ | |
'name' => $key, | |
'contents' => fopen($value->getRealPath(), 'r'), | |
'filename' => $value->getClientOriginalName(), | |
'headers' => [ | |
'Content-Type' => $value->getMimeType() | |
] | |
]; | |
} | |
$response = $client->request('POST', $requestedUrl, ['multipart' => $multipartParams]); | |
} else { | |
$response = $client->request('POST', $requestedUrl, [ | |
'form_params' => $request->all() | |
]); | |
} | |
} elseif ($request->isMethod('get')) { | |
$response = $client->get($requestedUrl, []); | |
} | |
$mimeType = $response->getHeader('Content-Type') ?: 'text/html'; | |
return response((string) $response->getBody())->header('Content-Type', $mimeType); | |
} catch (HttpException $e) { | |
return abort($e->getCode(), $e->getMessage()); | |
} catch (ServerException $e) { | |
return abort($e->getCode(), $e->getMessage()); | |
} | |
})->where('any', '.*'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
I've made a slightly more complex version for Level 7, I hope it helps someone
https://github.com/juanal98/Laravel-Proxy-Helper