Example #1
0
 public static function createFromGlobals()
 {
     $uri = parse_url(isset($_SERVER['REQUEST_URI']) ? $_SERVER['REQUEST_URI'] : null);
     $request = new self(isset($_SERVER['HTTP_HOST']) ? $_SERVER['HTTP_HOST'] : null, $uri['path'], $_REQUEST);
     $request->setMethod(isset($_SERVER['REQUEST_METHOD']) ? $_SERVER['REQUEST_METHOD'] : null);
     return $request;
 }
 public static function createFromEnvironment()
 {
     $request = new self();
     $request->setProtocol(isset($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on');
     $request->setMethod($_SERVER['REQUEST_METHOD']);
     $request->setHttpProtocol($_SERVER['SERVER_PROTOCOL']);
     $request->setHostName($_SERVER['HTTP_HOST']);
     $queryStart = strpos($_SERVER['REQUEST_URI'], '?');
     if ($queryStart !== false) {
         $request->setUri(substr($_SERVER['REQUEST_URI'], 0, $queryStart));
     } else {
         $request->setUri($_SERVER['REQUEST_URI']);
     }
     $request->setQueryString($_SERVER['QUERY_STRING']);
     $request->_setPostParameters($_POST);
     $headers = array();
     foreach ($_SERVER as $name => $value) {
         if (substr($name, 0, 5) !== 'HTTP_') {
             continue;
         }
         $headerName = str_replace(' ', '-', ucwords(strtolower(str_replace('_', ' ', substr($name, 5)))));
         $headers[$headerName] = $value;
     }
     $request->setHeaders($headers);
     return $request;
 }
Example #3
0
 public static function post($url, $formData, $formEncoding = null)
 {
     $request = new self();
     $request->setMethod('POST');
     $request->setUrl($url);
     $request->setFormData($formData, $formEncoding);
     return $request->exec();
 }
 public static function create($target = null, $method = null, array $args = null, $type = null)
 {
     $request = new self();
     $request->setTarget($target);
     $request->setMethod($method);
     $request->setArgs($args);
     $request->setType($type);
     return $request;
 }
Example #5
0
 public static function factory(array $data)
 {
     $rpc = new self();
     $rpc->setId($data['tid']);
     $rpc->setAction($data['action']);
     $rpc->setMethod($data['method']);
     $rpc->setData($data['data'] ?: array());
     $rpc->setParameters($data);
     return $rpc;
 }
Example #6
0
 /**
  * @param string $url
  * @param array $post
  * @param wfWAFHTTP $request
  * @return wfWAFHTTPResponse|bool
  * @throws wfWAFHTTPTransportException
  */
 public static function post($url, $post = array(), $request = null)
 {
     if (!$request) {
         $request = new self();
     }
     $request->setUrl($url);
     $request->setMethod('POST');
     $request->setBody($post);
     $request->setTransport(wfWAFHTTPTransport::getInstance());
     return $request->send();
 }
Example #7
0
 /**
  * @param wfWAFRequest|null $request
  * @return wfWAFRequest
  */
 public static function createFromGlobals($request = null)
 {
     if ($request === null) {
         if (version_compare(phpversion(), '5.3.0') > 0) {
             $class = get_called_class();
             $request = new $class();
         } else {
             $request = new self();
         }
     }
     $request->setAuth(array());
     $request->setCookies(array());
     $request->setFileNames(array());
     $request->setFiles(array());
     $request->setHeaders(array());
     $request->setHost('');
     $request->setIP('');
     $request->setMethod('');
     $request->setPath('');
     $request->setProtocol('');
     $request->setTimestamp('');
     $request->setURI('');
     $request->setBody(wfWAFUtils::stripMagicQuotes($_POST));
     $request->setQueryString(wfWAFUtils::stripMagicQuotes($_GET));
     $request->setCookies(wfWAFUtils::stripMagicQuotes($_COOKIE));
     $request->setFiles(wfWAFUtils::stripMagicQuotes($_FILES));
     if (!empty($_FILES)) {
         $fileNames = array();
         foreach ($_FILES as $input => $file) {
             $fileNames[$input] = wfWAFUtils::stripMagicQuotes($file['name']);
         }
         $request->setFileNames($fileNames);
     }
     if (is_array($_SERVER)) {
         //All of these depend on $_SERVER being non-null and an array
         $auth = array();
         if (array_key_exists('PHP_AUTH_USER', $_SERVER)) {
             $auth['user'] = wfWAFUtils::stripMagicQuotes($_SERVER['PHP_AUTH_USER']);
         }
         if (array_key_exists('PHP_AUTH_PW', $_SERVER)) {
             $auth['password'] = wfWAFUtils::stripMagicQuotes($_SERVER['PHP_AUTH_PW']);
         }
         $request->setAuth($auth);
         if (array_key_exists('REQUEST_TIME_FLOAT', $_SERVER)) {
             $timestamp = $_SERVER['REQUEST_TIME_FLOAT'];
         } else {
             if (array_key_exists('REQUEST_TIME', $_SERVER)) {
                 $timestamp = $_SERVER['REQUEST_TIME'];
             } else {
                 $timestamp = time();
             }
         }
         $request->setTimestamp($timestamp);
         $headers = array();
         foreach ($_SERVER as $key => $value) {
             if (wfWAFUtils::strpos($key, 'HTTP_') === 0) {
                 $header = wfWAFUtils::substr($key, 5);
                 $header = str_replace(array(' ', '_'), array('', ' '), $header);
                 $header = ucwords(wfWAFUtils::strtolower($header));
                 $header = str_replace(' ', '-', $header);
                 $headers[$header] = wfWAFUtils::stripMagicQuotes($value);
             }
         }
         if (array_key_exists('CONTENT_TYPE', $_SERVER)) {
             $headers['Content-Type'] = wfWAFUtils::stripMagicQuotes($_SERVER['CONTENT_TYPE']);
         }
         if (array_key_exists('CONTENT_LENGTH', $_SERVER)) {
             $headers['Content-Length'] = wfWAFUtils::stripMagicQuotes($_SERVER['CONTENT_LENGTH']);
         }
         $request->setHeaders($headers);
         $host = '';
         if (array_key_exists('Host', $headers)) {
             $host = $headers['Host'];
         } else {
             if (array_key_exists('SERVER_NAME', $_SERVER)) {
                 $host = wfWAFUtils::stripMagicQuotes($_SERVER['SERVER_NAME']);
             }
         }
         $request->setHost($host);
         $request->setMethod(array_key_exists('REQUEST_METHOD', $_SERVER) ? wfWAFUtils::stripMagicQuotes($_SERVER['REQUEST_METHOD']) : 'GET');
         $request->setProtocol(array_key_exists('HTTPS', $_SERVER) && $_SERVER['HTTPS'] && $_SERVER['HTTPS'] !== 'off' ? 'https' : 'http');
         $request->setUri(array_key_exists('REQUEST_URI', $_SERVER) ? wfWAFUtils::stripMagicQuotes($_SERVER['REQUEST_URI']) : '');
         $uri = parse_url($request->getURI());
         if (is_array($uri) && array_key_exists('path', $uri)) {
             $path = $uri['path'];
         } else {
             $path = $request->getURI();
         }
         $request->setPath($path);
     }
     return $request;
 }
Example #8
0
 /**
  * Create an instance from a stringified request.
  * @param  string     $str the stringified request
  * @return \vakata\http\Request          the request instance
  * @codeCoverageIgnore
  */
 public static function fromString($str)
 {
     $req = new self();
     $break = strpos($str, "\r\n\r\n") === false ? "\n" : "\r\n";
     // just in case someone breaks RFC 2616
     list($headers, $message) = explode($break . $break, $str, 2);
     $headers = explode($break, preg_replace("(" . $break . "\\s+)", " ", $headers));
     if (isset($headers[0]) && strlen($headers[0])) {
         $temp = explode(' ', $headers[0]);
         if (in_array($temp[0], ['GET', 'POST', 'HEAD', 'PATCH', 'PUT', 'OPTIONS', 'TRACE', 'DELETE'])) {
             $req->setMethod($temp[0]);
             $req->setUrl($temp[1]);
             if (isset($temp[2])) {
                 $req->setProtocolVersion(substr($temp[2], 5));
             }
             unset($headers[0]);
             $headers = array_values($headers);
         }
     }
     foreach (array_filter($headers) as $v) {
         $v = explode(':', $v, 2);
         $req->setHeader(trim($v[0]), trim($v[1]));
     }
     if ($req->hasHeader('Host')) {
         $host = explode(':', $req->getHeader('Host'), 2);
         $req->getUrl()->setHost($host[0]);
         if (isset($host[1]) && (int) $host[1]) {
             $req->getUrl()->setPort($host[1]);
         }
     }
     if (strpos($req->getHeader('Content-Type'), 'multipart') !== false) {
         $bndr = trim(explode(' boundary=', $req->getHeader('Content-Type'))[1], '"');
         $parts = explode($break . '--' . $bndr, $break . $message);
         array_pop($parts);
         array_shift($parts);
         $post = [];
         foreach ($parts as $item) {
             list($head, $body) = explode($break . $break, $item, 2);
             $head = explode($break, preg_replace("(" . $break . "\\s+)", " ", $head));
             foreach ($head as $h) {
                 if (strpos(strtolower($h), 'content-disposition') === 0) {
                     $cd = explode(';', $h);
                     $name = '';
                     $file = '';
                     foreach ($cd as $p) {
                         if (strpos(trim($p), 'name=') === 0) {
                             $name = trim(explode('name=', $p)[1], ' "');
                         }
                         if (strpos(trim($p), 'filename=') === 0) {
                             $file = trim(explode('filename=', $p)[1], ' "');
                         }
                     }
                     if ($file) {
                         $req->addUpload($name, $body, $file);
                     } else {
                         $post[$name] = $body;
                     }
                 }
             }
         }
         $req->setBody(http_build_query($post));
     } elseif (strlen($message)) {
         $req->setBody($message);
     }
     $req->removeHeader('Content-Length');
     $req->removeHeader('Transfer-Encoding');
     return $req;
 }
Example #9
0
 /**
  * @param $json
  * @return Request
  */
 public static function fromJSON($json)
 {
     $model = new self();
     $data = json_decode($json, true);
     $model->setFromData($data);
     if (isset($data['method'])) {
         $model->setMethod($data['method']);
     }
     if (isset($data['parameters'])) {
         $model->setParameters($data['parameters']);
     }
     if (isset($data['segments'])) {
         $model->setSegments($data['segments']);
     }
     if (isset($data['environment'])) {
         $model->setEnvironment($data['environment']);
     }
     return $model;
 }
Example #10
0
 /**
  * A factory function to encapsulate the creation of form objects.
  * @param string $name
  * @param string $action
  * @param string $method
  * @return Staple_Form
  */
 public static function Create($name, $action = NULL, $method = "POST")
 {
     $inst = new self($name, $action);
     $inst->setMethod($method);
     return $inst;
 }
Example #11
0
 /**
  * Factory method for <i>HttpRequest</i> creation from server
  *
  * This method uses superglobal array <code>$_SERVER</code> to build single
  * Request-Line consisting of <i>METHOD</i>, <i>URI</i> and <i>PROTOCOL</i>.
  * Headers and content is created via functions <code>getallheaders()</code>
  * and <code>file_get_contents()</code>.
  *
  * @return HttpRequest request object
  */
 public static function createFromServer()
 {
     $request = new self();
     $request->setMethod($_SERVER['REQUEST_METHOD']);
     $request->setUri($_SERVER['REQUEST_URI']);
     $request->setVersion($_SERVER['SERVER_PROTOCOL']);
     $request->setHeaders(getallheaders());
     $request->setContent(file_get_contents('php://input'));
     return $request;
 }