Skip to content

Commit fd7322d

Browse files
author
Nathan Macnamara
committed
Improve content-type detection
1 parent 67d295b commit fd7322d

File tree

2 files changed

+39
-9
lines changed

2 files changed

+39
-9
lines changed

src/Parser.php

Lines changed: 30 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -166,10 +166,7 @@ private function process_mask($mask)
166166
*/
167167
public function payload($format = '')
168168
{
169-
if (!empty($format))
170-
if (isset($this->supported_formats[$format]))
171-
return $this->{$this->supported_formats[$format]}($this->getPayload());
172-
return $this->{$this->getFormat()}($this->getPayload());
169+
return $this->{$this->getFormat($format)}($this->getPayload());
173170
}
174171

175172
/**
@@ -187,22 +184,46 @@ public function all()
187184
*
188185
* @return string Return the short format code (xml, json, ...).
189186
*/
190-
public function getFormat()
187+
public function getFormat($format = '')
191188
{
189+
if (! empty($format)) {
190+
return $this->processContentType($format);
191+
}
192+
192193
if (isset($_SERVER['CONTENT_TYPE']))
193194
{
194-
if (isset($this->supported_formats[$_SERVER['CONTENT_TYPE']]))
195-
return $this->supported_formats[$_SERVER['CONTENT_TYPE']];
195+
$type = $this->processContentType($_SERVER['CONTENT_TYPE']);
196+
if ($type !== false) return $type;
196197
}
198+
197199
if (isset($_SERVER['HTTP_CONTENT_TYPE']))
198200
{
199-
if (isset($this->supported_formats[$_SERVER['HTTP_CONTENT_TYPE']]))
200-
return $this->supported_formats[$_SERVER['HTTP_CONTENT_TYPE']];
201+
$type = $this->processContentType($_SERVER['HTTP_CONTENT_TYPE']);
202+
if ($type !== false) return $type;
201203
}
202204

203205
return 'json';
204206
}
205207

208+
/**
209+
* Process the content-type values
210+
*
211+
* @param string $contentType Content-Type raw string
212+
*
213+
* @return bool|string
214+
*/
215+
private function processContentType($contentType)
216+
{
217+
foreach (explode(';', $contentType) as $type) {
218+
$type = strtolower(trim($type));
219+
if (isset($this->supported_formats[$type])) {
220+
return $this->supported_formats[$type];
221+
}
222+
}
223+
224+
return false;
225+
}
226+
206227
/**
207228
* Return the payload data from the HTTP post request.
208229
*

tests/XMLTest.php

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,15 @@ public function format_detection_xml()
104104
$_SERVER['HTTP_CONTENT_TYPE'] = "application/xml";
105105
$this->assertEquals('xml', $parser->getFormat());
106106

107+
$_SERVER['HTTP_CONTENT_TYPE'] = "application/xml; charset=utf8";
108+
$this->assertEquals('xml', $parser->getFormat());
109+
110+
$_SERVER['HTTP_CONTENT_TYPE'] = "charset=utf8; application/xml";
111+
$this->assertEquals('xml', $parser->getFormat());
112+
113+
$_SERVER['HTTP_CONTENT_TYPE'] = "APPLICATION/XML";
114+
$this->assertEquals('xml', $parser->getFormat());
115+
107116
$_SERVER['HTTP_CONTENT_TYPE'] = "text/xml";
108117
$this->assertEquals('xml', $parser->getFormat());
109118
}

0 commit comments

Comments
 (0)