From b42211965a5246735c6551f1472c82da8d20d508 Mon Sep 17 00:00:00 2001 From: Ariel Constenla-Haile Date: Sun, 24 Mar 2013 12:20:58 +0000 Subject: Resolves; #i121201# Handle servers not supporting HEAD requests (cherry picked from commit d7085ea6fe26d2fc2ba384da04e454e78887f1f1) Change-Id: I4b3d50906468b998a1f4964d0ad1af3d31725332 --- ucb/source/ucp/webdav/webdavcontent.cxx | 152 ++++++++++++++++++++++++++++++-- 1 file changed, 147 insertions(+), 5 deletions(-) (limited to 'ucb/source/ucp/webdav/webdavcontent.cxx') diff --git a/ucb/source/ucp/webdav/webdavcontent.cxx b/ucb/source/ucp/webdav/webdavcontent.cxx index d5e4f90b6d80..6410dd376423 100644 --- a/ucb/source/ucp/webdav/webdavcontent.cxx +++ b/ucb/source/ucp/webdav/webdavcontent.cxx @@ -75,6 +75,118 @@ using namespace com::sun::star; using namespace http_dav_ucp; +namespace +{ +static void lcl_sendPartialGETRequest( bool &bError, + DAVException &aLastException, + const std::vector< rtl::OUString > aProps, + std::vector< rtl::OUString > &aHeaderNames, + const std::auto_ptr< DAVResourceAccess > &xResAccess, + std::auto_ptr< ContentProperties > &xProps, + const uno::Reference< ucb::XCommandEnvironment >& xEnv ) +{ + bool bIsRequestSize = false; + DAVResource aResource; + DAVRequestHeaders aPartialGet; + aPartialGet.push_back( + DAVRequestHeader( + rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Range" ) ), + rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "bytes=0-0" )))); + + for ( std::vector< rtl::OUString >::const_iterator it = aHeaderNames.begin(); + it != aHeaderNames.end(); it++ ) + { + if ( it->equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Content-Length" ) ) ) + { + bIsRequestSize = true; + break; + } + } + + if ( bIsRequestSize ) + { + // we need to know if the server accepts range requests for a resource + // and the range unit it uses + aHeaderNames.push_back( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Accept-Ranges" ) ) ); + aHeaderNames.push_back( rtl::OUString( RTL_CONSTASCII_USTRINGPARAM( "Content-Range" ) ) ); + } + try + { + uno::Reference< io::XInputStream > xIn = xResAccess->GET( aPartialGet, + aHeaderNames, + aResource, + xEnv ); + bError = false; + + if ( bIsRequestSize ) + { + // the ContentProperties maps "Content-Length" to the UCB "Size" property + // This would have an unrealistic value of 1 byte because we did only a partial GET + // Solution: if "Content-Range" is present, map it with UCB "Size" property + rtl::OUString aAcceptRanges, aContentRange, aContentLength; + std::vector< DAVPropertyValue > &aResponseProps = aResource.properties; + for ( std::vector< DAVPropertyValue >::const_iterator it = aResponseProps.begin(); + it != aResponseProps.end(); it++ ) + { + if ( it->Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Accept-Ranges" ) ) ) + it->Value >>= aAcceptRanges; + else if ( it->Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Content-Range" ) ) ) + it->Value >>= aContentRange; + else if ( it->Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Content-Length" ) ) ) + it->Value >>= aContentLength; + } + + sal_Int64 nSize = 1; + if ( aContentLength.getLength() ) + { + nSize = aContentLength.toInt64(); + } + + // according to http://tools.ietf.org/html/rfc2616#section-3.12 + // the only range unit defined is "bytes" and implementations + // MAY ignore ranges specified using other units. + if ( nSize == 1 && + aContentRange.getLength() && + aAcceptRanges.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "bytes" ) ) ) + { + // Parse the Content-Range to get the size + // vid. http://tools.ietf.org/html/rfc2616#section-14.16 + // Content-Range: / + sal_Int32 nSlash = aContentRange.lastIndexOf( sal_Unicode('/')); + if ( nSlash != -1 ) + { + rtl::OUString aSize = aContentRange.copy( nSlash + 1 ); + // "*" means that the instance-length is unknown at the time when the response was generated + if ( !aSize.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "*" ))) + { + for ( std::vector< DAVPropertyValue >::iterator it = aResponseProps.begin(); + it != aResponseProps.end(); it++ ) + { + if ( it->Name.equalsAsciiL( RTL_CONSTASCII_STRINGPARAM( "Content-Length" ) ) ) + { + it->Value <<= aSize; + break; + } + } + } + } + } + } + + if ( xProps.get() ) + xProps->addProperties( + aProps, + ContentProperties( aResource ) ); + else + xProps.reset ( new ContentProperties( aResource ) ); + } + catch ( DAVException const & ex ) + { + aLastException = ex; + } +} +} + //========================================================================= //========================================================================= // @@ -1335,13 +1447,43 @@ uno::Reference< sdbc::XRow > Content::getPropertyValues( } catch ( DAVException const & e ) { - bNetworkAccessAllowed - = shouldAccessNetworkAfterException( e ); + // non "general-purpose servers" may not support HEAD requests + // see http://www.w3.org/Protocols/rfc2616/rfc2616-sec5.html#sec5.1.1 + // In this case, perform a partial GET only to get the header info + // vid. http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.35 + // WARNING if the server does not support partial GETs, + // the GET will transfer the whole content + bool bError = true; + DAVException aLastException = e; + + // According to the spec. the origin server SHOULD return + // * 405 (Method Not Allowed): + // the method is known but not allowed for the requested resource + // * 501 (Not Implemented): + // the method is unrecognized or not implemented + // TODO SC_NOT_FOUND is only for google-code server + if ( aLastException.getStatus() == SC_NOT_IMPLEMENTED || + aLastException.getStatus() == SC_METHOD_NOT_ALLOWED || + aLastException.getStatus() == SC_NOT_FOUND ) + { + lcl_sendPartialGETRequest( bError, + aLastException, + aMissingProps, + aHeaderNames, + xResAccess, + xProps, + xEnv ); + m_bDidGetOrHead = !bError; + } - if ( !bNetworkAccessAllowed ) + if ( bError ) { - cancelCommandExecution( e, xEnv ); - // unreachable + if ( !(bNetworkAccessAllowed + = shouldAccessNetworkAfterException( aLastException )) ) + { + cancelCommandExecution( aLastException, xEnv ); + // unreachable + } } } } -- cgit v1.2.3