bool ServiceHost::FormatResponse( HTTPRequest *pRequest, QVariant vValue ) { if ( vValue.canConvert< QObject* >()) { const QObject *pObject = vValue.value< QObject* >(); return FormatResponse( pRequest, (QObject *)pObject ); } if ( vValue.canConvert< QFileInfo >()) { const QFileInfo oFileInfo = vValue.value< QFileInfo >(); return FormatResponse( pRequest, oFileInfo ); } // ---------------------------------------------------------------------- // Simple Variant... serialize it. // ---------------------------------------------------------------------- Serializer *pSer = pRequest->GetSerializer(); pSer->Serialize( vValue, vValue.typeName() ); pRequest->FormatActionResponse( pSer ); return true; }
explicit HttpResponse(HttpStatus::Value status_) : dataType(HttpDataType::Html), httpVersion(1.1f), status(status_) { FormatResponse(); }
HttpResponse() : data("<html><heading>Hi there! I'm a web server for Olympus. :)</heading></html>"), dataType(HttpDataType::Html), status(HttpStatus::Ok) { FormatResponse(); }
HttpResponse( std::string data_, HttpDataType::Value dataType_ = HttpDataType::Json, HttpStatus::Value status_ = HttpStatus::Ok) : dataType(dataType_), httpVersion(1.1f), status(status_) { FormatResponse(); }
bool ServiceHost::ProcessRequest( HTTPRequest *pRequest ) { bool bHandled = false; Service *pService = nullptr; try { if (pRequest) { if (pRequest->m_sBaseUrl != m_sBaseUrl) return false; LOG(VB_HTTP, LOG_INFO, QString("ServiceHost::ProcessRequest: %1 : %2") .arg(pRequest->m_sMethod) .arg(pRequest->m_sRawRequest)); // -------------------------------------------------------------- // Check to see if they are requesting the WSDL service Definition // -------------------------------------------------------------- if (( pRequest->m_eType == RequestTypeGet ) && ( pRequest->m_sMethod == "wsdl" )) { pService = qobject_cast<Service*>(m_oMetaObject.newInstance()); Wsdl wsdl( this ); wsdl.GetWSDL( pRequest ); delete pService; return true; } // -------------------------------------------------------------- // Check to see if they are requesting XSD - Type Definition // -------------------------------------------------------------- if (( pRequest->m_eType == RequestTypeGet ) && ( pRequest->m_sMethod == "xsd" )) { bool bHandled2 = false; if ( pRequest->m_mapParams.count() > 0) { pService = qobject_cast<Service*>(m_oMetaObject.newInstance()); Xsd xsd; if (pRequest->m_mapParams.contains( "type" )) bHandled2 = xsd.GetXSD( pRequest, pRequest->m_mapParams[ "type" ] ); else bHandled2 = xsd.GetEnumXSD( pRequest, pRequest->m_mapParams[ "enum" ] ); delete pService; pService = nullptr; } if (!bHandled2) throw QString("Invalid arguments to xsd query: %1") .arg(pRequest->m_sRequestUrl.section('?', 1)); return true; } // -------------------------------------------------------------- if (( pRequest->m_eType == RequestTypeGet ) && ( pRequest->m_sMethod == "version" )) { int nClassIdx = m_oMetaObject.indexOfClassInfo( "version" ); if (nClassIdx >=0) { QString sVersion = m_oMetaObject.classInfo(nClassIdx).value(); return FormatResponse( pRequest, QVariant( sVersion )); } } // -------------------------------------------------------------- // Allow a more REST like calling convention. If the Method // Name isn't found, search for one with the request method // appended to the name ( "Get" or "Put" for POST) // -------------------------------------------------------------- QString sMethodName = pRequest->m_sMethod; bool bMethodFound = false; if (m_Methods.contains(sMethodName)) bMethodFound = true; else { switch( pRequest->m_eType ) { case RequestTypeHead: case RequestTypeGet : sMethodName = "Get" + sMethodName; break; case RequestTypePost: sMethodName = "Put" + sMethodName; break; case RequestTypeUnknown: case RequestTypeOptions: case RequestTypeMSearch: case RequestTypeSubscribe: case RequestTypeUnsubscribe: case RequestTypeNotify: case RequestTypeResponse: // silence compiler break; } if (m_Methods.contains(sMethodName)) bMethodFound = true; } if (bMethodFound) { MethodInfo oInfo = m_Methods.value( sMethodName ); if (( pRequest->m_eType & oInfo.m_eRequestType ) != 0) { // ------------------------------------------------------ // Create new Instance of the Service Class so // it's guaranteed to be on the same thread // since we are making direct calls into it. // ------------------------------------------------------ pService = qobject_cast<Service*>(m_oMetaObject.newInstance()); QVariant vResult = oInfo.Invoke(pService, pRequest->m_mapParams); bHandled = FormatResponse( pRequest, vResult ); } } if (!bHandled) UPnp::FormatErrorResponse( pRequest, UPnPResult_InvalidAction ); } } catch (HttpRedirectException &ex) { UPnp::FormatRedirectResponse( pRequest, ex.hostName ); bHandled = true; } catch (HttpException &ex) { LOG(VB_GENERAL, LOG_ERR, ex.msg); UPnp::FormatErrorResponse( pRequest, UPnPResult_ActionFailed, ex.msg ); bHandled = true; } catch (QString &sMsg) { LOG(VB_GENERAL, LOG_ERR, sMsg); UPnp::FormatErrorResponse( pRequest, UPnPResult_ActionFailed, sMsg ); bHandled = true; } catch ( ...) { QString sMsg( "ServiceHost::ProcessRequest - Unexpected Exception" ); LOG(VB_GENERAL, LOG_ERR, sMsg); UPnp::FormatErrorResponse( pRequest, UPnPResult_ActionFailed, sMsg ); bHandled = true; } if (pService != nullptr) delete pService; return bHandled; }