diff --git a/src/Pecee/Http/Request.php b/src/Pecee/Http/Request.php index 15f9cb4..f645189 100644 --- a/src/Pecee/Http/Request.php +++ b/src/Pecee/Http/Request.php @@ -19,6 +19,10 @@ class Request public const REQUEST_TYPE_DELETE = 'delete'; public const REQUEST_TYPE_HEAD = 'head'; + public const CONTENT_TYPE_JSON = 'application/json'; + public const CONTENT_TYPE_FORM_DATA = 'multipart/form-data'; + public const CONTENT_TYPE_X_FORM_ENCODED = 'application/x-www-form-urlencoded'; + /** * All request-types * @var string[] @@ -57,6 +61,12 @@ class Request */ protected $headers = []; + /** + * Request ContentType + * @var string + */ + protected $contentType; + /** * Request host * @var string @@ -117,10 +127,10 @@ class Request $this->setHost($this->getHeader('http-host')); // Check if special IIS header exist, otherwise use default. - - $this->setUrl(new Url($this->getFirstHeader(['unencoded-url', 'request-uri',]))); + $this->setContentType(strtolower($this->getHeader('content-type'))); + $this->method = strtolower($this->getHeader('request-method')); $this->inputHandler = new InputHandler($this); $this->method = strtolower($this->inputHandler->value('_method', $this->getHeader('request-method'))); @@ -289,6 +299,31 @@ class Request return $defaultValue; } + /** + * Get request content-type + * @return string|null + */ + public function getContentType(): ?string + { + return $this->contentType; + } + + /** + * Set request content-type + * @param string $contentType + * @return $this + */ + protected function setContentType(string $contentType): self + { + if(strpos($contentType, ';') > 0) { + $this->contentType = substr($contentType, 0, strpos($contentType, ';')); + } else { + $this->contentType = $contentType; + } + + return $this; + } + /** * Get input class * @return InputHandler @@ -497,4 +532,4 @@ class Request return $this->data[$name] ?? null; } -} +} \ No newline at end of file diff --git a/tests/Pecee/SimpleRouter/RequestTest.php b/tests/Pecee/SimpleRouter/RequestTest.php new file mode 100644 index 0000000..736164a --- /dev/null +++ b/tests/Pecee/SimpleRouter/RequestTest.php @@ -0,0 +1,38 @@ +reset(); + + $request = $router->getRequest(); + + $this->assertEquals($contentType, $request->getContentType()); + + // Test special content-types + $router->reset(); + + $_SERVER['content_type'] = 'application/x-www-form-urlencoded; charset=UTF-8'; + + $this->assertEquals($contentType, $request->getContentType()); + + $router->reset(); + } + + // TODO: implement more test-cases + +} \ No newline at end of file