Add RawDisplayHandler and fix RSS/Atom not producing the correct headers

This commit is contained in:
Kijin Sung 2020-12-13 22:06:16 +09:00
parent 35203b684d
commit a405b91e42
5 changed files with 49 additions and 4 deletions

View file

@ -1021,10 +1021,14 @@ class Context
* @param string $method Response method. [HTML|XMLRPC|JSON]
* @return void
*/
public static function setResponseMethod($method = 'HTML')
public static function setResponseMethod($method = 'HTML', $content_type = null)
{
$methods = array('HTML' => 1, 'XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1);
$methods = array('HTML' => 1, 'XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1, 'RAW' => 1);
self::$_instance->response_method = isset($methods[$method]) ? $method : 'HTML';
if ($content_type)
{
self::$_instance->response_content_type = $content_type;
}
}
/**
@ -1040,7 +1044,7 @@ class Context
}
$method = self::getRequestMethod();
$methods = array('HTML' => 1, 'XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1);
$methods = array('HTML' => 1, 'XMLRPC' => 1, 'JSON' => 1, 'JS_CALLBACK' => 1, 'RAW' => 1);
return isset($methods[$method]) ? $method : 'HTML';
}

View file

@ -52,6 +52,10 @@ class DisplayHandler extends Handler
$this->gz_enabled = FALSE;
}
}
elseif(Context::getResponseMethod() == 'RAW')
{
$handler = new RawDisplayHandler();
}
else
{
$handler = new HTMLDisplayHandler();
@ -114,6 +118,10 @@ class DisplayHandler extends Handler
{
self::_printXMLHeader();
}
elseif(Context::getResponseMethod() == 'RAW' && $content_type = Context::get('response_content_type'))
{
self::_printCustomContentTypeHeader($content_type);
}
else
{
self::_printHTMLHeader();
@ -329,6 +337,18 @@ class DisplayHandler extends Handler
{
header("Content-Type: application/json; charset=UTF-8");
}
/**
* print a custom Content-Type header.
*
* @param string $content_type
* @return void
*/
public static function _printCustomContentTypeHeader($content_type)
{
$charset = (strpos($content_type, 'text/') === 0) ? '; charset=UTF-8' : '';
header('Content-Type: ' . $content_type . $charset);
}
/**
* print a HTTP HEADER for HTML, which is encoded in UTF-8

View file

@ -0,0 +1,20 @@
<?php
class RawDisplayHandler
{
function toDoc($oModule)
{
$tpl_path = $oModule->getTemplatePath();
$tpl_file = $oModule->getTemplateFile();
if ($tpl_path && $tpl_file)
{
$oTemplate = TemplateHandler::getInstance();
$output = $oTemplate->compile($tpl_path, $tpl_file);
}
else
{
$output = '';
}
return $output;
}
}

View file

@ -53,6 +53,7 @@ $GLOBALS['RX_AUTOLOAD_FILE_MAP'] = array_change_key_case(array(
'HTMLDisplayHandler' => 'classes/display/HTMLDisplayHandler.php',
'JSCallbackDisplayHandler' => 'classes/display/JSCallbackDisplayHandler.php',
'JSONDisplayHandler' => 'classes/display/JSONDisplayHandler.php',
'RawDisplayHandler' => 'classes/display/RawDisplayHandler.php',
'VirtualXMLDisplayHandler' => 'classes/display/VirtualXMLDisplayHandler.php',
'XMLDisplayHandler' => 'classes/display/XMLDisplayHandler.php',
'EditorHandler' => 'classes/editor/EditorHandler.class.php',

View file

@ -187,7 +187,7 @@ class rssView extends rss
Context::set('info', $info);
// Set XML Output
Context::setResponseMethod('XMLRPC');
Context::setResponseMethod('RAW', 'text/xml');
$this->setTemplatePath($this->module_path . 'tpl/format');
$this->setTemplateFile($template);
}