1
|
|
|
<?php |
2
|
|
|
|
3
|
|
|
/** |
4
|
|
|
* |
5
|
|
|
* This file is part of the Apix Project. |
6
|
|
|
* |
7
|
|
|
* (c) Franck Cassedanne <franck at ouarz.net> |
8
|
|
|
* |
9
|
|
|
* @license http://opensource.org/licenses/BSD-3-Clause New BSD License |
10
|
|
|
* |
11
|
|
|
*/ |
12
|
|
|
|
13
|
|
|
namespace Apix; |
14
|
|
|
|
15
|
|
|
use Apix\Request, |
16
|
|
|
Apix\Input\InputInterface, |
17
|
|
|
Apix\Input\Xml, |
18
|
|
|
Apix\Input\Json; |
19
|
|
|
|
20
|
|
|
class HttpRequest extends Request |
21
|
|
|
{ |
22
|
|
|
/** |
23
|
|
|
* List of supported input formats. |
24
|
|
|
* @var array |
25
|
|
|
*/ |
26
|
|
|
protected $formats = array('post', 'json', 'xml'); |
27
|
|
|
|
28
|
|
|
/** |
29
|
|
|
* Returns the format from an HTTP context. |
30
|
|
|
* |
31
|
|
|
* @param string $context The context to extract the format from. |
32
|
|
|
* @return string The extracted format. |
33
|
|
|
*/ |
34
|
|
|
public static function getFormat($context) |
35
|
|
|
{ |
36
|
|
|
switch (true) { |
|
|
|
|
37
|
|
|
// text/html |
38
|
|
|
case (strstr($context, '/html')): |
39
|
|
|
return 'html'; |
40
|
|
|
|
41
|
|
|
// application/json |
42
|
|
|
case (strstr($context, '/json')): |
43
|
|
|
return 'json'; |
44
|
|
|
|
45
|
|
|
// text/xml, application/xml |
46
|
|
|
case (strstr($context, '/xml') |
47
|
|
|
&& (!strstr($context, 'html'))): |
48
|
|
|
return 'xml'; |
49
|
|
|
|
50
|
|
|
default: |
51
|
|
|
return null; |
52
|
|
|
} |
53
|
|
|
} |
54
|
|
|
|
55
|
|
|
/** |
56
|
|
|
* Returns the format from an HTTP Accept. |
57
|
|
|
* |
58
|
|
|
* @return string The output format |
59
|
|
|
*/ |
60
|
|
|
public function getAcceptFormat() |
61
|
|
|
{ |
62
|
|
|
if ($this->hasHeader('HTTP_ACCEPT')) { |
63
|
|
|
$accept = $this->getHeader('HTTP_ACCEPT'); |
64
|
|
|
|
65
|
|
|
if (!$format = self::getFormat($accept)) { |
66
|
|
|
// 'application/javascript' |
67
|
|
|
$format = strstr($accept, '/javascript') ? 'jsonp' : null; |
68
|
|
|
} |
69
|
|
|
} |
70
|
|
|
|
71
|
|
|
return isset($format) ? $format : false; |
72
|
|
|
} |
73
|
|
|
|
74
|
|
|
/** |
75
|
|
|
* Get & parse the request body-data. |
76
|
|
|
* |
77
|
|
|
* @param boolean $assoc Wether to convert objects to associative arrays or not. |
78
|
|
|
* @return array |
79
|
|
|
* @link http://www.w3.org/TR/html401/interact/forms.html#h-17.13.4 |
80
|
|
|
* @link http://www.w3.org/TR/html401/references.html#ref-RFC2388 |
81
|
|
|
*/ |
82
|
|
|
public function getBodyData($assoc=true) |
83
|
|
|
{ |
84
|
|
|
if ($this->hasBody() && $this->hasHeader('CONTENT_TYPE')) { |
85
|
|
|
$ctx = $this->getHeader('CONTENT_TYPE'); |
86
|
|
|
|
87
|
|
|
if ( |
88
|
|
|
in_array('post', $this->formats) |
89
|
|
|
// application/x-www-form-urlencoded |
90
|
|
|
&& strstr($ctx, '/x-www-form-urlencoded') |
91
|
|
|
// TODO: shall we do multipart/form-data too? |
92
|
|
|
) { |
93
|
|
|
// handles by PHP already. No need to parse the body. |
94
|
|
|
return $this->getParams(); |
95
|
|
|
} elseif ($format = self::getFormat($ctx)) { |
96
|
|
|
if (in_array(strtolower($format), $this->formats)) { |
97
|
|
|
$class = __NAMESPACE__ . '\Input\\' . ucfirst($format); |
98
|
|
|
$input = new $class(); |
99
|
|
|
|
100
|
|
|
return $input->decode($this->getBody(), $assoc); |
101
|
|
|
#$this->setParams($r); // TODO: maybe set as request params? |
|
|
|
|
102
|
|
|
} |
103
|
|
|
} |
104
|
|
|
|
105
|
|
|
return null; |
106
|
|
|
} |
107
|
|
|
} |
108
|
|
|
|
109
|
|
|
/** |
110
|
|
|
* Sets the input formats. |
111
|
|
|
* |
112
|
|
|
* @return void |
113
|
|
|
*/ |
114
|
|
|
public function setFormats(array $formats) |
115
|
|
|
{ |
116
|
|
|
$this->formats = $formats; |
117
|
|
|
} |
118
|
|
|
|
119
|
|
|
} |
120
|
|
|
|