Browse files

Solved the issue with in/out mimetypes (Refs #120)

  • Loading branch information...
1 parent e184e28 commit 8d0d42c9f3f785cf2177f2325f0c29b9f367b7c1 @dshafik dshafik committed Apr 20, 2012
Showing with 55 additions and 28 deletions.
  1. +15 −13 src/frapi/library/Frapi/Controller/Api.php
  2. +40 −15 src/frapi/library/Frapi/Controller/Main.php
View
28 src/frapi/library/Frapi/Controller/Api.php
@@ -244,7 +244,7 @@ public function processAction()
->setActionParams($this->getParams())
->setActionFiles($this->getFiles())
->setAcceptParams(
- isset($this->options) && is_array($this->options)
+ isset($this->options) && is_array($this->options)
? $this->options : array()
);
@@ -339,6 +339,8 @@ public function authorize()
*/
public function detectAndSetMimeType()
{
+ $mimetypes = Frapi_Output::getMimeTypeMap();
+
$types = $this->parseAcceptHeader();
if(empty($types)) {
@@ -347,16 +349,16 @@ public function detectAndSetMimeType()
if ($this->formatSetByExtension) {
return array(
- 'mimeType' => $this->setformat(strtolower($this->format)),
+ 'mimeType' => $this->setFormatByExtension($this->format),
'outputFormat' => $this->format,
);
}
- $mimetypes = $this->parseMimeTypes();
+ $patterns = $this->getMimeTypePatterns();
foreach($types as $type) {
- if (isset($this->mimeMaps[$type['mimetype']])) {
- $outputFormat = strtoupper($this->mimeMaps[$type['mimetype']]);
+ if (isset($mimetypes[$type['mimetype']])) {
+ $outputFormat = strtoupper($mimetypes[$type['mimetype']]);
$this->setFormat(strtolower($outputFormat));
@@ -367,25 +369,25 @@ public function detectAndSetMimeType()
return $return;
} else {
- foreach ($mimetypes as $mimetype) {
+ foreach ($patterns as $pattern) {
$matches = array();
- if (preg_match($mimetype['pattern'], $type['mimetype'], $matches)) {
+ if (preg_match($pattern['pattern'], $type['mimetype'], $matches)) {
$return = array(
'mimeType' => $type['mimetype'],
);
- if ($mimetype['output_format'][0] == ':') {
- $format = substr($mimetype['output_format'], 1);
+ if ($pattern['output_format'][0] == ':') {
+ $format = substr($pattern['output_format'], 1);
if (!empty($matches[$format])) {
$return['outputFormat'] = $matches[$format];
} else {
$return['outputFormat'] = self::DEFAULT_OUTPUT_FORMAT;
}
} else {
- $return['outputFormat'] = $mimetype['output_format'];
+ $return['outputFormat'] = $pattern['output_format'];
}
- foreach ($mimetype['params'] as $param) {
+ foreach ($pattern['params'] as $param) {
$return[$param] = $matches[$param];
}
@@ -413,7 +415,7 @@ protected function parseAcceptHeader()
$types = explode(',', $_SERVER['HTTP_ACCEPT']);
foreach($types AS $type) {
- $typeMatch = preg_match('/(?P<mimetype>[a-z\*]+\/[a-z\+\-\*]+)(?:(?:;)(?P<params>.*?$))?/i', $type, $typeComponents);
+ $typeMatch = preg_match('/(?P<mimetype>[a-z\*]+\/[a-z0-9\+\-\*\.]+)(?:(?:;)(?P<params>.*?$))?/i', $type, $typeComponents);
if(!$typeMatch) {
continue;
}
@@ -464,7 +466,7 @@ protected function parseAcceptHeaderParams($params)
*
* @return array
*/
- protected function parseMimeTypes()
+ protected function getMimeTypePatterns()
{
$cache = new Frapi_Internal();
$mimetypes = $cache->getConfiguration('mimetypes')->getAll('mimetype');
View
55 src/frapi/library/Frapi/Controller/Main.php
@@ -152,7 +152,7 @@ class Frapi_Controller_Main
* format. In order to add a new mimetype, add it's
* mimetype name and then add it's output as the associated
* value.
- *
+ *
* The list here is left for legacy installations. Defining mimetypes in the admin
* will override these once defined.
*
@@ -257,6 +257,7 @@ public function __construct($customAuthorization=null)
? $format : $setFormat
);
} catch (Frapi_Exception $fex) {
+ /* @todo HTTP/1.1 412 Precondition Failed */
$this->setFormat($this->getDefaultFormatFromConfiguration());
}
@@ -342,17 +343,17 @@ public function getParams()
$xmlJsonMatch = preg_grep('/\<|\{/i', array_keys($puts));
$inputFormat = $this->getFormat();
}
-
+
/**
* When doing parse_str("{json:string}") it creates an array like:
* array(
* "{json:string}" => ""
* )
- *
+ *
* If args are also present along with the body, they are in the array
* before the body.
- *
- * Checks if the last argument is an empty string, this + inputForm is
+ *
+ * Checks if the last argument is an empty string, this + inputForm is
* indicative of the body needing parsing.
*/
if (end($puts) == '' && !empty($inputFormat) || !empty($xmlJsonMatch)) {
@@ -362,7 +363,7 @@ public function getParams()
$inputFormat,
$input
);
-
+
if (!empty($requestBody)) {
$rootElement = array_keys($requestBody);
@@ -462,6 +463,12 @@ public function getDefaultFormatFromConfiguration()
*/
protected function setFormat($format = false)
{
+ $mimetypes = Frapi_Output::getMimeTypeMap();
+
+ if ($mimetypes) {
+ $this->mimeMaps = $mimetypes;
+ }
+
if ($format) {
$typeValid = Frapi_Rules::validateOutputType($format);
$this->format = strtolower($format);
@@ -477,6 +484,28 @@ protected function setFormat($format = false)
}
}
+ protected function setFormatByExtension($extension = false)
+ {
+ if ($extension == false) {
+ throw new Frapi_Error (
+ Frapi_Error::ERROR_INVALID_URL_PROMPT_FORMAT_NAME,
+ Frapi_Error::ERROR_INVALID_URL_PROMPT_FORMAT_MSG,
+ Frapi_Error::ERROR_INVALID_URL_PROMPT_FORMAT_NO
+ );
+ }
+
+ $cache = new Frapi_Internal();
+
+ $outputs = $cache->getConfiguration('outputs')->getAll('output');
+
+
+ foreach ($outputs as $output) {
+ if (in_array(strtolower($extension), $output['extensions'])) {
+ return $output['mimetype'];
+ }
+ }
+ }
+
/**
* Get action
*
@@ -518,16 +547,12 @@ public function setInputFormat()
$_SERVER['CONTENT_TYPE'] :
null;
- $mimetypes = Frapi_Output::getMimeTypeMap();
-
- if ($mimetypes) {
- $this->mimeMaps = $mimetypes;
- }
-
+ $mimetypes = Frapi_Output::getMimeTypeMap() + $this->mimeMaps;
+
if(!empty($contentType) &&
- isset($this->mimeMaps[$contentType]) &&
- in_array($this->mimeMaps[$contentType], $this->allowedInputTypes)) {
- $this->inputFormat = $this->mimeMaps[$contentType];
+ isset($mimetypes[$contentType]) &&
+ in_array($mimetypes[$contentType], $this->allowedInputTypes)) {
+ $this->inputFormat = $mimetypes[$contentType];
}
}

0 comments on commit 8d0d42c

Please sign in to comment.