From 6b77c9f3da2c69cd2e11d7897a23ef37fc8f5a90 Mon Sep 17 00:00:00 2001 From: xdrm-brackets Date: Sat, 17 Feb 2018 18:49:56 +0100 Subject: [PATCH] +add build.http (required by build.api) +fixed router.core.router (allow dot '.' in url) -removed require autoloader in view.homepage (already loaded from autoloader in parent execution tree) --- build/http/core/HttpRequest.php | 198 ++++++++++++++++++++++++++++++++ build/router/core/Router.php | 2 +- public_html/view/homepage.php | 2 +- 3 files changed, 200 insertions(+), 2 deletions(-) create mode 100644 build/http/core/HttpRequest.php diff --git a/build/http/core/HttpRequest.php b/build/http/core/HttpRequest.php new file mode 100644 index 0000000..de3a294 --- /dev/null +++ b/build/http/core/HttpRequest.php @@ -0,0 +1,198 @@ + auto-filled HTTP Request + * + =========================================================*/ + public function __construct(){ + /* [1] Define URI & Status Code & method + =========================================================*/ + $this->uri = $_SERVER['REQUEST_URI']; + $this->method = $_SERVER['REQUEST_METHOD']; + + + /* [2] Define headers + =========================================================*/ + $this->headers = \getallheaders(); + + + /* [3] Define default datasets (GET, POST) + =========================================================*/ + $this->getdata = $_GET; + $this->postdata = $_POST; + + + /* [4] Define BODY & its type + =========================================================*/ + /* (1) Default: set plain/text body */ + $this->body = \file_get_contents('php://input'); + + /* (2) Fetch content type */ + $this->type = self::getContentType($this->headers['Content-Type']); + + + /* [5] Parse BODY data -> POST + =========================================================*/ + $this->parseBody(); + } + + + + /* GET CONSTANT CT_* FROM `Content-Type` HEADER + * + * @pContentType `Content-Type` header value + * + * @return type Constant value + * + */ + private static function getContentType($pContentType=null){ + /* [1] Checks argv + =========================================================*/ + if( is_null($pContentType) ) + $pContentType = $_SERVER['CONTENT_TYPE']; + + + /* [2] Checks types + =========================================================*/ + /* (1) Form Data Types + ---------------------------------------------------------*/ + /* (1) multipart/form-data */ + if( preg_match('/^multipart\/form\-data; boundary=(.+)$/i', $pContentType) ) + return self::CT_MULTIPART_FORM_DATA; + + /* (2) application/x-www-form-urlencoded */ + if( preg_match('/^application\/x\-www\-form\-urlencoded/i', $pContentType) ) + return self::CT_X_WWW_FORM_URLENCODED; + + + /* (2) Data types + ---------------------------------------------------------*/ + /* (1) Basic JSON content type */ + if( preg_match('/^application\/json/i', $pContentType) ) + return self::CT_JSON; + + /* (2) Basic YAML content type */ + if( preg_match('/^application\/yaml/i', $pContentType) ) + return self::CT_YAML; + + /* (3) Basic TEXT content type */ + if( preg_match('/text\/[a-z]+/', $pContentType) ) + return self::CT_TEXT; + + + /* (3) Default Type + ---------------------------------------------------------*/ + return self::CT_BINARY; + + } + + + + /* PARSES BODY DATA + * + */ + private function parseBody(){ + /* [1] If empty body -> do nothing + =========================================================*/ + if( strlen($this->body) === 0 ) + return true; + + + /* [2] Management for each ContentType + =========================================================*/ + switch($this->type){ + + /* (1) multipart/form-data -> parse for not-POST methods + ---------------------------------------------------------*/ + case self::CT_MULTIPART_FORM_DATA: + /* (1) Fetch the boundary */ + if( !preg_match('/boundary=(.+)$/i', $this->headers['Content-Type'], $match) ) + return false; + + $boundary = $match[1]; + + /* (2) Break body into parts */ + $splitter = "/(?:\n|\r\n|--)*$boundary(?:\n|\r\n|--)?/im"; + $parts = preg_split($splitter, $this->body); + + /* (3) Process parts */ + foreach($parts as $part) + if( preg_match('/^Content\-Disposition: form\-data; name=\"([^"]+)\"(?:\n|\r\n){2}(.+)$/mi', $part, $match) ) + $this->postdata[$match[1]] = $match[2]; + + /* (4) Erases body */ + $this->body = ''; + break; + + + /* (2) application/x-www-form-urlencoded -> parse for not-POST methods + ---------------------------------------------------------*/ + case self::CT_X_WWW_FORM_URLENCODED: + /* Auto parse builtin-php function */ + parse_str($this->body, $this->postdata); + + /* Erases body */ + $this->body = ''; + break; + + + /* (3) application/json -> parse if no error + ---------------------------------------------------------*/ + case self::CT_JSON: + /* (1) Decode body content */ + $decoded = json_decode($this->body, true); + + /* (2) If error -> do nothing */ + if( is_null($decoded) ) + return; + + /* (3) Parse body into body */ + $this->body = $decoded; + break; + + + /* (4) application/yaml -> parse if no error + ---------------------------------------------------------*/ + case self::CT_YAML: + + break; + } + } + + + public function BODY(){ return $this->body; } + public function POST(){ return $this->postdata; } + public function GET(){ return $this->getdata; } + public function HEADERS(){ return $this->headers; } + public function METHOD(){ return $this->method; } + public function URI(){ return $this->uri; } + } diff --git a/build/router/core/Router.php b/build/router/core/Router.php index f2200a7..19d7f1f 100644 --- a/build/router/core/Router.php +++ b/build/router/core/Router.php @@ -239,7 +239,7 @@ /* (2) Replace special characters + replace vars ---------------------------------------------------------*/ /* (1) Check default URL format */ - if( !preg_match('/^(\/[\w\{\}-]*)*\/?$/', $pattern) ) + if( !preg_match('/^(\/[\w\{\}\.-]*)*\/?$/', $pattern) ) return false; /* (2) Escape special characters */ diff --git a/public_html/view/homepage.php b/public_html/view/homepage.php index 919c3e4..826e0da 100644 --- a/public_html/view/homepage.php +++ b/public_html/view/homepage.php @@ -1,3 +1,3 @@ -