Exemple #1
0
 /**
  * Instantiate request from php _SERVER variable
  * @param array server
  */
 public static function createFromGlobals()
 {
     $server = $_SERVER;
     $uriParts = parse_url($server['REQUEST_URI']);
     $uriParts['host'] = $server['SERVER_NAME'];
     $uriParts['port'] = $server['SERVER_PORT'];
     $uriParts['scheme'] = isset($server['REQUEST_SCHEME']) ? $server['REQUEST_SCHEME'] : (isset($server['HTTPS']) && $server['HTTPS'] == 'on' ? 'https' : 'http');
     if (function_exists('getallheaders')) {
         // a correct case already
         $apacheHeaders = getallheaders();
         foreach ($apacheHeaders as $header => $value) {
             $headers[$header] = array_map('trim', explode(',', $value));
         }
     } else {
         $headers = array();
         // normalize the header key
         foreach ($server as $key => $value) {
             if (substr($key, 0, 5) != 'HTTP_') {
                 continue;
             }
             $name = str_replace(' ', '-', ucwords(str_replace('_', ' ', strtolower(substr($key, 5)))));
             $headers[$name] = array_map('trim', explode(',', $value));
         }
     }
     $request = new static($server['REQUEST_METHOD'], new Uri($uriParts), $headers, Stream::createFromContents(file_get_contents('php://input')), $server, $_COOKIE, UploadedFile::createFromGlobals($_FILES));
     if ($server['REQUEST_METHOD'] == 'POST' && in_array($request->getMediaType(), array('application/x-www-form-urlencoded', 'multipart/form-data'))) {
         $request->setParsedBody($_POST);
     }
     return $request;
 }
 public static function createFromEnvironment(Environment $environment, $jsonBody = null)
 {
     $method = $environment['REQUEST_METHOD'];
     $uri = Uri::createFromEnvironment($environment);
     $headers = Headers::createFromEnvironment($environment);
     $cookies = Cookies::parseHeader($headers->get('Cookie', []));
     $serverParams = $environment->all();
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromEnvironment($environment);
     $request = new static($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
     if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) {
         // parsed body must be $_POST
         $request = $request->withParsedBody($_POST);
     }
     if ($jsonBody !== null) {
         $request = $request->withParsedBody($jsonBody);
     }
     return $request;
 }
Exemple #3
0
 public static function createFromGlobals(array $globals)
 {
     $env = new Collection($globals);
     $method = $env->get('REQUEST_METHOD');
     $uri = Uri::createFromGlobals($globals);
     $headers = Headers::createFromGlobals($globals);
     $cookies = Cookies::parseHeader($headers->get('Cookie', []));
     $serverParams = $globals;
     $body = new RequestBody();
     $uploadedFiles = UploadedFile::createFromGlobals($globals);
     $request = new static($method, $uri, $headers, $cookies, $serverParams, $body, $uploadedFiles);
     if ($method === 'POST' && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) {
         // parsed body must be $_POST
         $request = $request->withParsedBody($_POST);
     }
     return $request;
 }
Exemple #4
0
 /**
  * Create new HTTP request with data extracted from the application
  * Environment object
  *
  * @param array $env
  * @return self
  */
 public static function createFromEnvironment(array $env = \null)
 {
     $env = $env ?: $_SERVER;
     // headers
     $headers = [];
     $special = ['CONTENT_TYPE' => 1, 'CONTENT_LENGTH' => 1, 'PHP_AUTH_USER' => 1, 'PHP_AUTH_PW' => 1, 'PHP_AUTH_DIGEST' => 1, 'AUTH_TYPE' => 1];
     foreach ($env as $key => $value) {
         $key = strtoupper($key);
         if (isset($special[$key]) || strpos($key, 'HTTP_') === 0) {
             if ($key !== 'HTTP_CONTENT_LENGTH') {
                 $headers[static::normalizeHeaderKey($key)] = [$value];
             }
         }
     }
     $request = new static($env['REQUEST_METHOD'], Uri::createFromEnvironment($env), $headers, $_COOKIE, $env, \null, UploadedFile::createFromEnvironment());
     if ($request->isPost() && in_array($request->getMediaType(), ['application/x-www-form-urlencoded', 'multipart/form-data'])) {
         // parsed body must be $_POST
         $request = $request->withParsedBody($_POST);
     }
     return $request;
 }