Kaydet (Commit) 16df731a authored tarafından Giuseppe Castagno's avatar Giuseppe Castagno

tdf#102499 (5): Deal with HTTP unofficial response status codes

A reference can be found here:
<https://en.wikipedia.org/wiki/List_of_HTTP_status_codes>
(retrieved 2016-09-13).

Changes done:
Add set of 'HEAD method not available' before using fall back GET method.
Add new method in OPTIONS cache.
Add response status code if fall-back GET didn't succeeded.
Add copy-assignement operator to DAVOptions.
Fix behaviour of GET fall back when HEAD missing.

Change-Id: I6aad0e22bb444abf37b90186588f64f79b03cc3d
Reviewed-on: https://gerrit.libreoffice.org/29680Tested-by: 's avatarJenkins <ci@libreoffice.org>
Reviewed-by: 's avatarGiuseppe Castagno <giuseppe.castagno@acca-esse.eu>
üst 08655031
......@@ -34,7 +34,6 @@ DAVOptions::DAVOptions() :
{
}
DAVOptions::DAVOptions( const DAVOptions & rOther ) :
m_isClass1( rOther.m_isClass1 ),
m_isClass2( rOther.m_isClass2 ),
......@@ -50,11 +49,25 @@ DAVOptions::DAVOptions( const DAVOptions & rOther ) :
{
}
DAVOptions::~DAVOptions()
{
}
DAVOptions & DAVOptions::operator=( const DAVOptions& rOpts )
{
m_isClass1 = rOpts.m_isClass1;
m_isClass2 = rOpts.m_isClass2;
m_isClass3 = rOpts.m_isClass3;
m_isLocked = rOpts.m_isLocked;
m_isHeadAllowed = rOpts.m_isHeadAllowed;
m_aAllowedMethods = rOpts.m_aAllowedMethods;
m_nStaleTime = rOpts.m_nStaleTime;
m_sURL = rOpts.m_sURL;
m_sRedirectedURL = rOpts.m_sRedirectedURL;
m_nHttpResponseStatusCode = rOpts.m_nHttpResponseStatusCode;
m_sHttpResponseStatusText = rOpts.m_sHttpResponseStatusText;
return *this;
}
bool DAVOptions::operator==( const DAVOptions& rOpts ) const
{
......@@ -79,12 +92,10 @@ DAVOptionsCache::DAVOptionsCache()
{
}
DAVOptionsCache::~DAVOptionsCache()
{
}
bool DAVOptionsCache::getDAVOptions( const OUString & rURL, DAVOptions & rDAVOptions )
{
osl::MutexGuard aGuard( m_aMutex );
......@@ -113,7 +124,6 @@ bool DAVOptionsCache::getDAVOptions( const OUString & rURL, DAVOptions & rDAVOpt
}
}
void DAVOptionsCache::removeDAVOptions( const OUString & rURL )
{
osl::MutexGuard aGuard( m_aMutex );
......@@ -128,7 +138,6 @@ void DAVOptionsCache::removeDAVOptions( const OUString & rURL )
}
}
void DAVOptionsCache::addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime )
{
osl::MutexGuard aGuard( m_aMutex );
......@@ -149,6 +158,39 @@ void DAVOptionsCache::addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32
m_aTheCache[ aEncodedUrl ] = rDAVOptions;
}
void DAVOptionsCache::updateCachedOption( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime )
{
osl::MutexGuard aGuard( m_aMutex );
OUString aURL( rDAVOptions.getURL() );
OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( aURL ) ) );
normalizeURLLastChar( aEncodedUrl );
rDAVOptions.setURL( aEncodedUrl );
// unchanged, it may be used to access a server
OUString aRedirURL( rDAVOptions.getRedirectedURL() );
rDAVOptions.setRedirectedURL( aRedirURL );
// check if already cached
DAVOptionsMap::iterator it;
it = m_aTheCache.find( aEncodedUrl );
if ( it != m_aTheCache.end() )
{
DAVOptions &opts = (*it).second;
// exists, set new staletime, only if remaining time is higher
TimeValue t1;
osl_getSystemTime( &t1 );
if ( ( opts.getStaleTime() - t1.Seconds ) > nLifeTime )
{
opts.setStaleTime( t1.Seconds + nLifeTime );
}
// update relevant fields
opts.setHttpResponseStatusCode( rDAVOptions.getHttpResponseStatusCode() );
opts.setHttpResponseStatusText( rDAVOptions.getHttpResponseStatusText() );
}
}
sal_uInt16 DAVOptionsCache::getHttpResponseStatusCode( const OUString & rURL, OUString & rHttpResponseStatusText )
{
osl::MutexGuard aGuard( m_aMutex );
......@@ -174,6 +216,29 @@ sal_uInt16 DAVOptionsCache::getHttpResponseStatusCode( const OUString & rURL, OU
return 0;
}
void DAVOptionsCache::setHeadAllowed( const OUString & rURL, const bool HeadAllowed )
{
osl::MutexGuard aGuard( m_aMutex );
OUString aEncodedUrl( ucb_impl::urihelper::encodeURI( NeonUri::unescape( rURL ) ) );
normalizeURLLastChar( aEncodedUrl );
DAVOptionsMap::iterator it;
it = m_aTheCache.find( aEncodedUrl );
if ( it != m_aTheCache.end() )
{
// first check for stale
TimeValue t1;
osl_getSystemTime( &t1 );
if( (*it).second.getStaleTime() < t1.Seconds )
{
m_aTheCache.erase( it );
return;
}
// check if the resource was present on server
(*it).second.setHeadAllowed( HeadAllowed );
}
}
bool DAVOptionsCache::isHeadAllowed( const OUString & rURL )
{
osl::MutexGuard aGuard( m_aMutex );
......
......@@ -152,6 +152,7 @@ namespace webdav_ucp
m_sHttpResponseStatusText.clear();
};
DAVOptions & operator=( const DAVOptions& rOpts );
bool operator==( const DAVOptions& rOpts ) const;
};
......@@ -174,6 +175,8 @@ namespace webdav_ucp
void removeDAVOptions( const OUString & rURL );
void addDAVOptions( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime );
void updateCachedOption( DAVOptions & rDAVOptions, const sal_uInt32 nLifeTime );
/** return the cached value of HTTP response status code
If the cached value is found stale, it is removed.
......@@ -190,6 +193,8 @@ namespace webdav_ucp
bool isHeadAllowed( const OUString & rURL );
void setHeadAllowed( const OUString & rURL, bool HeadAllowed = true );
private:
/// remove the last '/' in aUrl, if it exists
......
......@@ -1841,33 +1841,106 @@ void NeonSession::HandleError( int nError,
sal_uInt16 code = makeStatusCode( aText );
SAL_WARN( "ucb.ucp.webdav", "Neon returned NE_ERROR, http response status code was: " << code << " '" << aText << "'" );
if ( code == SC_LOCKED )
if ( SC_BAD_REQUEST <= code && code < SC_INTERNAL_SERVER_ERROR )
{
if ( m_aNeonLockStore.findByUri(
makeAbsoluteURL( inPath ) ) == nullptr )
// error codes in the range 4xx
switch ( code )
{
// locked by 3rd party
throw DAVException( DAVException::DAV_LOCKED );
}
else
{
// locked by ourself
throw DAVException( DAVException::DAV_LOCKED_SELF );
case SC_LOCKED:
{
if ( m_aNeonLockStore.findByUri(
makeAbsoluteURL( inPath ) ) == nullptr )
{
// locked by 3rd party
throw DAVException( DAVException::DAV_LOCKED );
}
else
{
// locked by ourself
throw DAVException( DAVException::DAV_LOCKED_SELF );
}
}
break;
case SC_PRECONDITION_FAILED:
case SC_BAD_REQUEST:
{
// Special handling for 400 and 412 status codes, which may indicate
// that a lock previously obtained by us has been released meanwhile
// by the server. Unfortunately, RFC is not clear at this point,
// thus server implementations behave different...
if ( removeExpiredLocktoken( makeAbsoluteURL( inPath ), rEnv ) )
throw DAVException( DAVException::DAV_LOCK_EXPIRED );
}
break;
case SC_REQUEST_TIMEOUT:
{
throw DAVException( DAVException::DAV_HTTP_TIMEOUT,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
}
break;
case SC_UNAUTHORIZED: // User authentication failed on server
{
throw DAVException( DAVException::DAV_HTTP_AUTH,
NeonUri::makeConnectionEndPointString(
m_aHostName, m_nPort ) );
}
break;
case SC_GONE:
case SC_LENGTH_REQUIRED:
case SC_REQUEST_ENTITY_TOO_LARGE:
case SC_REQUEST_URI_TOO_LONG:
case SC_UNSUPPORTED_MEDIA_TYPE:
case SC_REQUESTED_RANGE_NOT_SATISFIABLE:
case SC_EXPECTATION_FAILED:
case SC_UNPROCESSABLE_ENTITY:
case SC_FAILED_DEPENDENCY:
case SC_CONFLICT:
case SC_NOT_ACCEPTABLE:
case SC_PAYMENT_REQUIRED:
case SC_PROXY_AUTHENTICATION_REQUIRED:
default:
// set 400 error, if not one of others
code = SC_BAD_REQUEST;
SAL_FALLTHROUGH;
case SC_FORBIDDEN:
case SC_NOT_FOUND:
case SC_METHOD_NOT_ALLOWED:
throw DAVException( DAVException::DAV_HTTP_ERROR, aText, code );
break;
}
}
// Special handling for 400 and 412 status codes, which may indicate
// that a lock previously obtained by us has been released meanwhile
// by the server. Unfortunately, RFC is not clear at this point,
// thus server implementations behave different...
else if ( code == SC_BAD_REQUEST || code == SC_PRECONDITION_FAILED )
else if ( SC_INTERNAL_SERVER_ERROR <= code )
{
if ( removeExpiredLocktoken( makeAbsoluteURL( inPath ), rEnv ) )
throw DAVException( DAVException::DAV_LOCK_EXPIRED );
// deal with HTTP response status codes higher then 500
// error codes in the range 5xx, server errors
// but there exists unofficial code in the range 1000 and up
// for example see:
// <https://support.cloudflare.com/hc/en-us/sections/200820298-Error-Pages> (retrieved 2016-10-05)
switch ( code )
{
// the error codes case before the default case are not actively
// managed by LO
case SC_BAD_GATEWAY:
case SC_SERVICE_UNAVAILABLE:
case SC_GATEWAY_TIMEOUT:
case SC_HTTP_VERSION_NOT_SUPPORTED:
case SC_INSUFFICIENT_STORAGE:
default:
// set 500 error, if not one of others
// expand the error code
code = SC_INTERNAL_SERVER_ERROR;
SAL_FALLTHROUGH;
case SC_INTERNAL_SERVER_ERROR:
case SC_NOT_IMPLEMENTED:
throw DAVException( DAVException::DAV_HTTP_ERROR, aText, code );
break;
}
}
throw DAVException( DAVException::DAV_HTTP_ERROR, aText, code );
else
throw DAVException( DAVException::DAV_HTTP_ERROR, aText, code );
}
break;
case NE_LOOKUP: // Name lookup failed.
SAL_WARN( "ucb.ucp.webdav", "Name lookup failed" );
throw DAVException( DAVException::DAV_HTTP_LOOKUP,
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment