From 64f20edaab2dfa7cf6f8bc0fc5c4f48accefb96f Mon Sep 17 00:00:00 2001 From: Andrew McMillan Date: Sun, 29 Aug 2010 13:01:56 +1200 Subject: [PATCH] Coerce the content-type on PUT. It seems that failing to correctly set the content-type header on PUT is a common error. --- htdocs/caldav.php | 1 + inc/CalDAVRequest.php | 35 +++++++++++++++++++++++++++++++++++ 2 files changed, 36 insertions(+) diff --git a/htdocs/caldav.php b/htdocs/caldav.php index 5a185253..0739265b 100644 --- a/htdocs/caldav.php +++ b/htdocs/caldav.php @@ -81,6 +81,7 @@ switch ( $request->method ) { case 'HEAD': include('caldav-GET.php'); break; case 'PROPPATCH': include('caldav-PROPPATCH.php'); break; case 'PUT': + $request->CoerceContentType(); switch( $request->content_type ) { case 'text/calendar': /** use original DAViCal 'PUT' code which will be rewritten */ diff --git a/inc/CalDAVRequest.php b/inc/CalDAVRequest.php index 7ac2d7dd..42d5f9de 100644 --- a/inc/CalDAVRequest.php +++ b/inc/CalDAVRequest.php @@ -852,6 +852,41 @@ EOSQL; } + /** + * Coerces the Content-type of the request into something valid/appropriate + */ + function CoerceContentType() { + if ( isset($this->content_type) ) { + $type = explode( '/', $this->content_type, 2); + if ( $type[0] == 'text' ) return; + } + + /** Null (or peculiar) content-type supplied so we have to try and work it out... */ + $first_word = trim(substr( $this->raw_post, 0, 30)); + $first_word = strtoupper( preg_replace( '/\s.*/s', '', $first_word ) ); + switch( $first_word ) { + case 'content_type)?$this->content_type:'(null)') ); + $this->content_type = 'text/xml'; + break; + case 'BEGIN:VCALENDAR': + dbg_error_log( 'LOG WARNING', 'Application sent content-type of "%s" instead of "text/calendar"', + (isset($this->content_type)?$this->content_type:'(null)') ); + $this->content_type = 'text/calendar'; + break; + case 'BEGIN:VCARD': + dbg_error_log( 'LOG WARNING', 'Application sent content-type of "%s" instead of "text/vcard"', + (isset($this->content_type)?$this->content_type:'(null)') ); + $this->content_type = 'text/vcard'; + break; + default: + dbg_error_log( 'LOG NOTICE', 'Unusual content-type of "%s" and first word of content is "%s"', + (isset($this->content_type)?$this->content_type:'(null)'), $first_word ); + } + } + + /** * Returns true if the URL referenced by this request points at a collection. */