Skip to content

Commit

Permalink
WebAPI: make header field lookup case insensitive
Browse files Browse the repository at this point in the history
As explained in #767 HTTP headers should be handled case insensitive.
Since we can't look up values in a map case insensitively directly,
add a helper function.

Closes #767.
  • Loading branch information
tobydox committed Nov 5, 2021
1 parent 9b2f1b2 commit 5d3e5a6
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 3 deletions.
26 changes: 23 additions & 3 deletions plugins/webapi/WebApiController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,7 @@ WebApiController::Response WebApiController::closeConnection( const Request& req
return checkResponse;
}

removeConnection( QUuid( request.headers[connectionUidHeaderFieldName()].toString() ) );
removeConnection( lookupHeaderField(request.headers, connectionUidHeaderFieldName()).toUuid() );

return {};
}
Expand Down Expand Up @@ -453,18 +453,38 @@ void WebApiController::removeConnection( QUuid connectionUuid )



QVariant WebApiController::lookupHeaderField(const Request& request, const QString& fieldName)
{
if( request.headers.contains(fieldName) )
{
return request.headers[fieldName];
}

for( auto it = request.headers.constBegin(), end = request.headers.constEnd(); it != end; ++it )
{
if( it.key().compare( fieldName, Qt::CaseInsensitive ) == 0 )
{
return it.value();
}
}

return {};
}



WebApiController::LockingConnectionPointer WebApiController::lookupConnection( const Request& request )
{
QReadLocker connectionsReadLocker{&m_connectionsLock};

return m_connections.value( request.headers[connectionUidHeaderFieldName()].toUuid() );
return m_connections.value( lookupHeaderField(request.headers, connectionUidHeaderFieldName()).toUuid() );
}



WebApiController::Response WebApiController::checkConnection( const Request& request )
{
const auto connectionUuid = QUuid( request.headers[connectionUidHeaderFieldName()].toString() );
const auto connectionUuid = lookupHeaderField(request.headers, connectionUidHeaderFieldName()).toUuid();

return runInMainThread<WebApiController::Response>( [=]() -> WebApiController::Response {
m_connectionsLock.lockForRead();
Expand Down
2 changes: 2 additions & 0 deletions plugins/webapi/WebApiController.h
Original file line number Diff line number Diff line change
Expand Up @@ -131,6 +131,8 @@ class WebApiController : public QObject
return QStringLiteral("Connection-Uid");
}

static QVariant lookupHeaderField( const Request& request, const QString& fieldName );

using WebApiConnectionPointer = QSharedPointer<WebApiConnection>;
using LockingConnectionPointer = LockingPointer<WebApiConnectionPointer>;

Expand Down

0 comments on commit 5d3e5a6

Please sign in to comment.