* CalDAV/CardDAV/GroupDAV new user preference to log requests and responses to Apache error-log

This commit is contained in:
Ralf Becker 2011-11-23 16:41:45 +00:00
parent 9e333ddda4
commit 8cdf689b7f
5 changed files with 621 additions and 15 deletions

View File

@ -585,6 +585,19 @@ class HTTP_WebDAV_Server
// {{{ http_PROPFIND() // {{{ http_PROPFIND()
/**
* Should the whole PROPFIND request (xml) be stored
*
* @var boolean
*/
var $store_request = false;
/**
* Content of (last) PROPFIND request
*
* @var string
*/
var $request;
/** /**
* PROPFIND method handler * PROPFIND method handler
* *
@ -606,7 +619,8 @@ class HTTP_WebDAV_Server
} }
// analyze request payload // analyze request payload
$propinfo = new _parse_propfind("php://input"); $propinfo = new _parse_propfind("php://input", $this->store_request);
if ($this->store_request) $this->request = $propinfo->request;
if (!$propinfo->success) { if (!$propinfo->success) {
$this->http_status("400 Error"); $this->http_status("400 Error");
return; return;
@ -1016,7 +1030,7 @@ class HTTP_WebDAV_Server
$options["path"] = $this->path; $options["path"] = $this->path;
$propinfo = new _parse_proppatch("php://input"); $propinfo = new _parse_proppatch("php://input", $this->store_request);
if (!$propinfo->success) { if (!$propinfo->success) {
$this->http_status("400 Error"); $this->http_status("400 Error");
@ -1374,6 +1388,28 @@ class HTTP_WebDAV_Server
$options['content_type'] = 'application/octet-stream'; $options['content_type'] = 'application/octet-stream';
} }
$options['stream'] = fopen('php://input', 'r');
switch($this->_SERVER['HTTP_CONTENT_ENCODING'])
{
case 'gzip':
case 'deflate': //zlib
if (extension_loaded('zlib'))
{
stream_filter_append($options['stream'], 'zlib.inflate', STREAM_FILTER_READ);
}
}
// store request in $this->request, if requested via $this->store_request
if ($this->store_request)
{
$options['content'] = '';
while(!feof($options['stream']))
{
$options['content'] .= fread($options['stream'],8192);
}
$this->request =& $options['content'];
unset($options['stream']);
}
/* RFC 2616 2.6 says: "The recipient of the entity MUST NOT /* RFC 2616 2.6 says: "The recipient of the entity MUST NOT
ignore any Content-* (e.g. Content-Range) headers that it ignore any Content-* (e.g. Content-Range) headers that it
does not understand or implement and MUST return a 501 does not understand or implement and MUST return a 501
@ -1383,10 +1419,18 @@ class HTTP_WebDAV_Server
if (strncmp($key, 'HTTP_CONTENT', 11)) continue; if (strncmp($key, 'HTTP_CONTENT', 11)) continue;
switch ($key) { switch ($key) {
case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11 case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11
// TODO support this if ext/zlib filters are available switch($this->_SERVER['HTTP_CONTENT_ENCODING'])
$this->http_status('501 not implemented'); {
case 'gzip':
case 'deflate': //zlib
if (extension_loaded('zlib')) break;
// fall through for no zlib support
default:
$this->http_status('415 Unsupported Media Type');
echo "The service does not support '$val' content encoding"; echo "The service does not support '$val' content encoding";
return; return;
}
break;
case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12 case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12
// we assume it is not critical if this one is ignored // we assume it is not critical if this one is ignored
@ -1521,6 +1565,28 @@ class HTTP_WebDAV_Server
$options["content_type"] = "application/octet-stream"; $options["content_type"] = "application/octet-stream";
} }
$options["stream"] = fopen("php://input", "r");
switch($this->_SERVER['HTTP_CONTENT_ENCODING'])
{
case 'gzip':
case 'deflate': //zlib
if (extension_loaded('zlib'))
{
stream_filter_append($options['stream'], 'zlib.inflate', STREAM_FILTER_READ);
}
}
// store request in $this->request, if requested via $this->store_request
if ($this->store_request)
{
$options['content'] = '';
while(!feof($options['stream']))
{
$options['content'] .= fread($options['stream'],8192);
}
$this->request =& $options['content'];
unset($options['stream']);
}
/* RFC 2616 2.6 says: "The recipient of the entity MUST NOT /* RFC 2616 2.6 says: "The recipient of the entity MUST NOT
ignore any Content-* (e.g. Content-Range) headers that it ignore any Content-* (e.g. Content-Range) headers that it
does not understand or implement and MUST return a 501 does not understand or implement and MUST return a 501
@ -1530,10 +1596,18 @@ class HTTP_WebDAV_Server
if (strncmp($key, "HTTP_CONTENT", 11)) continue; if (strncmp($key, "HTTP_CONTENT", 11)) continue;
switch ($key) { switch ($key) {
case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11 case 'HTTP_CONTENT_ENCODING': // RFC 2616 14.11
// TODO support this if ext/zlib filters are available switch($this->_SERVER['HTTP_CONTENT_ENCODING'])
$this->http_status("501 not implemented"); {
case 'gzip':
case 'deflate': //zlib
if (extension_loaded('zlib')) break;
// fall through for no zlib support
default:
$this->http_status('415 Unsupported Media Type');
echo "The service does not support '$val' content encoding"; echo "The service does not support '$val' content encoding";
return; return;
}
break;
case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12 case 'HTTP_CONTENT_LANGUAGE': // RFC 2616 14.12
// we assume it is not critical if this one is ignored // we assume it is not critical if this one is ignored

View File

@ -0,0 +1,259 @@
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Hartmut Holzgraefe <hholzgra@php.net> |
// | Christian Stocker <chregu@bitflux.ch> |
// +----------------------------------------------------------------------+
//
// $Id: _parse_propfind.php,v 1.4 2006/10/10 11:53:17 hholzgra Exp $
//
/**
* helper class for parsing PROPFIND request bodies
*
* @package HTTP_WebDAV_Server
* @author Hartmut Holzgraefe <hholzgra@php.net>
* @version @package-version@
*/
class _parse_propfind
{
/**
* success state flag
*
* @var bool
* @access public
*/
var $success = false;
/**
* found properties are collected here
*
* @var array
* @access public
*/
var $props = false;
/**
* found (CalDAV) filters are collected here
*
* @var array
* @access public
*/
var $filters = false;
/**
* found other tags, eg. CalDAV calendar-multiget href's
*
* @var array
* @access public
*/
var $other = false;
/**
* what we are currently parsing: props or filters
*
* @var array
* @access private
*/
var $use = 'props';
/**
* Root tag, usually 'propfind' for PROPFIND, but can be eg. 'calendar-query' or 'calendar-multiget' for CalDAV REPORT
*
* @var array with keys 'name' and 'ns'
*/
var $root;
/**
* internal tag nesting depth counter
*
* @var int
* @access private
*/
var $depth = 0;
/**
* On return whole request, if $store_request == true was specified in constructor
*
* @var string
*/
var $request;
/**
* constructor
*
* @access public
* @param string $path
* @param boolean $store_request=false if true whole request data will be made available in $this->request
*/
function _parse_propfind($path, $store_request=false)
{
// success state flag
$this->success = true;
// property storage array
$this->props = array();
// internal tag depth counter
$this->depth = 0;
// remember if any input was parsed
$had_input = false;
// open input stream
$f_in = fopen($path, "r");
if (!$f_in) {
$this->success = false;
return;
}
// create XML parser
$xml_parser = xml_parser_create_ns("UTF-8", " ");
// set tag and data handlers
xml_set_element_handler($xml_parser,
array(&$this, "_startElement"),
array(&$this, "_endElement"));
xml_set_character_data_handler($xml_parser,
array(&$this,'_charData')
);
// we want a case sensitive parser
xml_parser_set_option($xml_parser,
XML_OPTION_CASE_FOLDING, false);
// parse input
while ($this->success && !feof($f_in)) {
$line = fgets($f_in);
if ($store_request) $this->request .= $line;
if (is_string($line)) {
$had_input = true;
$this->success &= xml_parse($xml_parser, $line, false);
}
}
// finish parsing
if ($had_input) {
$this->success &= xml_parse($xml_parser, "", true);
}
// free parser
xml_parser_free($xml_parser);
// close input stream
fclose($f_in);
// if no input was parsed it was a request
if(!count($this->props)) $this->props = "all"; // default
}
/**
* start tag handler
*
* @access private
* @param resource parser
* @param string tag name
* @param array tag attributes
*/
function _startElement($parser, $name, $attrs)
{
// name space handling
if (strstr($name, " ")) {
list($ns, $tag) = explode(" ", $name);
if ($ns == "")
$this->success = false;
} else {
$ns = "";
$tag = $name;
}
// record root tag
if ($this->depth == 0) {
$this->root = array('name' => $tag, 'xmlns' => $ns);
}
// special tags at level 1: <allprop> and <propname>
if ($this->depth == 1) {
$this->use = 'props';
switch ($tag)
{
case "allprop":
$this->props = "all";
break;
case "propname":
$this->props = "names";
break;
case 'prop':
break;
case 'filter':
$this->use = 'filters';
break;
default:
$this->use = 'other';
break;
}
}
// requested properties are found at level 2
// CalDAV filters can be at deeper levels too and we need the attrs, same for other tags (eg. multiget href's)
if ($this->depth == 2 || $this->use == 'filters' && $this->depth >= 2 || $this->use == 'other') {
$prop = array("name" => $tag);
if ($ns)
$prop["xmlns"] = $ns;
if ($this->use != 'props') {
$prop['attrs'] = $attrs;
$prop['depth'] = $this->depth;
}
// this can happen if we have allprop and prop in one propfind:
// <allprop /><prop><blah /></prop>, eg. blah is not automatic returned by allprop
if (!is_array($this->{$this->use}) && $this->{$this->use}) $this->{$this->use} = array($this->{$this->use});
$this->{$this->use}[] = $prop;
}
// increment depth count
$this->depth++;
}
/**
* end tag handler
*
* @access private
* @param resource parser
* @param string tag name
*/
function _endElement($parser, $name)
{
// here we only need to decrement the depth count
$this->depth--;
}
/**
* char data handler for non prop tags, eg. href's in CalDAV multiget, or filter contents
*
* @access private
* @param resource parser
* @param string character data
*/
function _charData($parser, $data)
{
if ($this->use != 'props' && ($n = count($this->{$this->use})) && ($data = trim($data))) {
$this->{$this->use}[$n-1]['data'] = $data;
}
}
}

View File

@ -0,0 +1,232 @@
<?php
//
// +----------------------------------------------------------------------+
// | PHP Version 4 |
// +----------------------------------------------------------------------+
// | Copyright (c) 1997-2003 The PHP Group |
// +----------------------------------------------------------------------+
// | This source file is subject to version 2.02 of the PHP license, |
// | that is bundled with this package in the file LICENSE, and is |
// | available at through the world-wide-web at |
// | http://www.php.net/license/2_02.txt. |
// | If you did not receive a copy of the PHP license and are unable to |
// | obtain it through the world-wide-web, please send a note to |
// | license@php.net so we can mail you a copy immediately. |
// +----------------------------------------------------------------------+
// | Authors: Hartmut Holzgraefe <hholzgra@php.net> |
// | Christian Stocker <chregu@bitflux.ch> |
// +----------------------------------------------------------------------+
//
// $Id: _parse_proppatch.php,v 1.6 2006/10/10 11:53:17 hholzgra Exp $
//
/**
* helper class for parsing PROPPATCH request bodies
*
* @package HTTP_WebDAV_Server
* @author Hartmut Holzgraefe <hholzgra@php.net>
* @version @package-version@
*/
class _parse_proppatch
{
/**
*
*
* @var
* @access
*/
var $success;
/**
*
*
* @var
* @access
*/
var $props;
/**
*
*
* @var
* @access
*/
var $depth;
/**
*
*
* @var
* @access
*/
var $mode;
/**
*
*
* @var
* @access
*/
var $current;
/**
* On return whole request, if $store_request == true was specified in constructor
*
* @var string
*/
var $request;
/**
* constructor
*
* @param string path of input stream
* @param boolean $store_request=false if true whole request data will be made available in $this->request
* @access public
*/
function _parse_proppatch($path, $store_request=false)
{
$this->success = true;
$this->depth = 0;
$this->props = array();
$had_input = false;
$f_in = fopen($path, "r");
if (!$f_in) {
$this->success = false;
return;
}
$xml_parser = xml_parser_create_ns("UTF-8", " ");
xml_set_element_handler($xml_parser,
array(&$this, "_startElement"),
array(&$this, "_endElement"));
xml_set_character_data_handler($xml_parser,
array(&$this, "_data"));
xml_parser_set_option($xml_parser,
XML_OPTION_CASE_FOLDING, false);
while($this->success && !feof($f_in)) {
$line = fgets($f_in);
if ($store_request) $this->request .= $line;
if (is_string($line)) {
$had_input = true;
$this->success &= xml_parse($xml_parser, $line, false);
}
}
if($had_input) {
$this->success &= xml_parse($xml_parser, "", true);
}
xml_parser_free($xml_parser);
fclose($f_in);
}
/**
* tag start handler
*
* @param resource parser
* @param string tag name
* @param array tag attributes
* @return void
* @access private
*/
function _startElement($parser, $name, $attrs)
{
if (strstr($name, " ")) {
list($ns, $tag) = explode(" ", $name);
if ($ns == "")
$this->success = false;
} else {
$ns = "";
$tag = $name;
}
if ($this->depth == 1) {
$this->mode = $tag;
}
if ($this->depth == 3) {
$prop = array("name" => $tag);
$this->current = array("name" => $tag, "ns" => $ns, "status"=> 200);
if ($this->mode == "set") {
$this->current["val"] = ""; // default set val
}
}
if ($this->depth >= 4) {
$this->current["val"] .= "<$tag";
if (isset($attr)) {
foreach ($attr as $key => $val) {
$this->current["val"] .= ' '.$key.'="'.str_replace('"','&quot;', $val).'"';
}
}
$this->current["val"] .= ">";
}
$this->depth++;
}
/**
* tag end handler
*
* @param resource parser
* @param string tag name
* @return void
* @access private
*/
function _endElement($parser, $name)
{
if (strstr($name, " ")) {
list($ns, $tag) = explode(" ", $name);
if ($ns == "")
$this->success = false;
} else {
$ns = "";
$tag = $name;
}
$this->depth--;
if ($this->depth >= 4) {
$this->current["val"] .= "</$tag>";
}
if ($this->depth == 3) {
if (isset($this->current)) {
$this->props[] = $this->current;
unset($this->current);
}
}
}
/**
* input data handler
*
* @param resource parser
* @param string data
* @return void
* @access private
*/
function _data($parser, $data)
{
if (isset($this->current)) {
$this->current["val"] .= $data;
}
}
}
/*
* Local variables:
* tab-width: 4
* c-basic-offset: 4
* indent-tabs-mode:nil
* End:
*/

View File

@ -989,4 +989,43 @@ class groupdav extends HTTP_WebDAV_Server
)))); ))));
return $props; return $props;
} }
/**
* Serve WebDAV HTTP request
*
* Reimplemented to add logging, currently only to Apache error-log
*/
function ServeRequest()
{
if (($debug_level=$GLOBALS['egw_info']['user']['preferences']['groupdav']['debug_level']) === 'r' ||
$debug_level === 'f' || $this->debug)
{
$starttime = microtime(true);
$this->store_request = true;
ob_start();
}
parent::ServeRequest();
if ($starttime)
{
error_log($_SERVER['REQUEST_METHOD'].' '.$_SERVER['PATH_INFO'].' HTTP/1.1');
// reconstruct headers
foreach($_SERVER as $name => $value)
{
list($type,$name) = explode('_',$name,2);
if ($type == 'HTTP' || $type == 'CONTENT') error_log(str_replace(' ','-',ucwords(strtolower(($type=='HTTP'?'':$type.' ').str_replace('_',' ',$name)))).': '.$value);
}
if ($this->request)
{
error_log('');
foreach(explode("\n",$this->request) as $line) error_log($line);
}
error_log('HTTP/1.1 '.$this->_http_status);
foreach(headers_list() as $line) error_log($line);
if (($content = ob_get_flush())) error_log('');
if (strlen($content) > 1024 && $debug_level !== 'f') $content = substr($content,0,1024)."\n*** LOG TRUNKATED ";
$content .= sprintf('*** %s --> "%s" took %5.3f s',$_SERVER['REQUEST_METHOD'].($_SERVER['REQUEST_METHOD']=='REPORT'?' '.$this->propfind_options['root']['name']:'').' '.$_SERVER['PATH_INFO'],$this->_http_status,microtime(true)-$starttime)."\n";
foreach(explode("\n",$content) as $line) error_log($line);
}
}
} }

View File

@ -99,10 +99,12 @@ class groupdav_hooks
'name' => 'debug_level', 'name' => 'debug_level',
'help' => 'Enables debug-messages to Apache/PHP error-log, allowing to diagnose problems on a per user basis.', 'help' => 'Enables debug-messages to Apache/PHP error-log, allowing to diagnose problems on a per user basis.',
'values' => array( 'values' => array(
'0' => '0 - off', '0' => 'Off',
'1' => '1 - function calls', 'r' => 'Requests and truncated responses',
'2' => '2 - more info', 'f' => 'Requests and full responses',
'3' => '3 - complete $_SERVER array', '1' => 'Debug 1 - function calls',
'2' => 'Debug 2 - more info',
'3' => 'Debug 3 - complete $_SERVER array',
), ),
'xmlrpc' => true, 'xmlrpc' => true,
'admin' => false, 'admin' => false,