friendship ended with social-app. php is my new best friend
at main 1.3 kB view raw
1<?php 2 3namespace React\Http\Middleware; 4 5use Psr\Http\Message\ServerRequestInterface; 6use React\Http\Io\MultipartParser; 7 8final class RequestBodyParserMiddleware 9{ 10 private $multipart; 11 12 /** 13 * @param int|string|null $uploadMaxFilesize 14 * @param int|null $maxFileUploads 15 */ 16 public function __construct($uploadMaxFilesize = null, $maxFileUploads = null) 17 { 18 $this->multipart = new MultipartParser($uploadMaxFilesize, $maxFileUploads); 19 } 20 21 public function __invoke(ServerRequestInterface $request, $next) 22 { 23 $type = \strtolower($request->getHeaderLine('Content-Type')); 24 list ($type) = \explode(';', $type); 25 26 if ($type === 'application/x-www-form-urlencoded') { 27 return $next($this->parseFormUrlencoded($request)); 28 } 29 30 if ($type === 'multipart/form-data') { 31 return $next($this->multipart->parse($request)); 32 } 33 34 return $next($request); 35 } 36 37 private function parseFormUrlencoded(ServerRequestInterface $request) 38 { 39 // parse string into array structure 40 // ignore warnings due to excessive data structures (max_input_vars and max_input_nesting_level) 41 $ret = array(); 42 @\parse_str((string)$request->getBody(), $ret); 43 44 return $request->withParsedBody($ret); 45 } 46}