// _____________________________________________________________________________ string Server::createQueryFromHttpParams(const ParamValueMap& params) const { string query; // Construct a Query object from the parsed request. auto it = params.find("query"); if (it == params.end() || it->second == "") { AD_THROW(ad_semsearch::Exception::BAD_REQUEST, "Expected at least one non-empty attribute \"query\"."); } return it->second; }
void CommandBuilder::BuildCommand(const wxString &cmdName, const wxString &cmdParamsArg) { // Stage 1: create a Command object of the right type auto scriptOutput = ScriptCommandRelay::GetResponseTarget(); auto output = std::make_unique<CommandOutputTargets>(std::make_unique<NullProgressTarget>(), scriptOutput, scriptOutput); #ifdef OLD_BATCH_SYSTEM OldStyleCommandType *factory = CommandDirectory::Get()->LookUp(cmdName); if (factory == NULL) { // Fall back to hoping the Batch Command system can handle it #endif OldStyleCommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand")); wxASSERT(type != NULL); mCommand = type->Create(nullptr); mCommand->SetParameter(wxT("CommandName"), cmdName); mCommand->SetParameter(wxT("ParamString"), cmdParamsArg); auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output); Success(aCommand); return; #ifdef OLD_BATCH_SYSTEM } CommandSignature &signature = factory->GetSignature(); mCommand = factory->Create(nullptr); //mCommand->SetOutput( std::move(output) ); // Stage 2: set the parameters ShuttleCli shuttle; shuttle.mParams = cmdParamsArg; shuttle.mbStoreInClient = true; ParamValueMap::const_iterator iter; ParamValueMap params = signature.GetDefaults(); // Iterate through the parameters defined by the command for (iter = params.begin(); iter != params.end(); ++iter) { wxString paramString; // IF there is a match in the args actually used if (shuttle.TransferString(iter->first, paramString, wxT(""))) { // Then set that parameter. if (!mCommand->SetParameter(iter->first, paramString)) { Failure(); return; } } } // Check for unrecognised parameters wxString cmdParams(cmdParamsArg); while (!cmdParams.empty()) { cmdParams.Trim(true); cmdParams.Trim(false); int splitAt = cmdParams.Find(wxT('=')); if (splitAt < 0 && !cmdParams.empty()) { Failure(wxT("Parameter string is missing '='")); return; } wxString paramName = cmdParams.Left(splitAt); if (params.find(paramName) == params.end()) { Failure(wxT("Unrecognized parameter: '") + paramName + wxT("'")); return; } // Handling of quoted strings is quite limitted. // You start and end with a " or a '. // There is no escaping in the string. cmdParams = cmdParams.Mid(splitAt+1); if( cmdParams.empty() ) splitAt =-1; else if( cmdParams[0] == '\"' ){ cmdParams = cmdParams.Mid(1); splitAt = cmdParams.Find(wxT('\"'))+1; } else if( cmdParams[0] == '\'' ){ cmdParams = cmdParams.Mid(1); splitAt = cmdParams.Find(wxT('\''))+1; } else splitAt = cmdParams.Find(wxT(' '))+1; if (splitAt < 1) { splitAt = cmdParams.length(); } cmdParams = cmdParams.Mid(splitAt); } auto aCommand = std::make_shared<ApplyAndSendResponse>(mCommand, output); Success(aCommand); #endif }
// _____________________________________________________________________________ Server::ParamValueMap Server::parseHttpRequest( const string& httpRequest) const { LOG(DEBUG) << "Parsing HTTP Request." << endl; ParamValueMap params; _requestProcessingTimer.start(); // Parse the HTTP Request. size_t indexOfGET = httpRequest.find("GET"); size_t indexOfHTTP = httpRequest.find("HTTP"); if (indexOfGET == httpRequest.npos || indexOfHTTP == httpRequest.npos) { AD_THROW(ad_semsearch::Exception::BAD_REQUEST, "Invalid request. Only supporting proper HTTP GET requests!\n" + httpRequest); } string request = httpRequest.substr(indexOfGET + 3, indexOfHTTP - (indexOfGET + 3)); size_t index = request.find("?"); if (index == request.npos) { AD_THROW(ad_semsearch::Exception::BAD_REQUEST, "Invalid request. At least one parameters is " "required for meaningful queries!\n" + httpRequest); } size_t next = request.find('&', index + 1); while (next != request.npos) { size_t posOfEq = request.find('=', index + 1); if (posOfEq == request.npos) { AD_THROW(ad_semsearch::Exception::BAD_REQUEST, "Parameter without \"=\" in HTTP Request.\n" + httpRequest); } string param = ad_utility::getLowercaseUtf8( request.substr(index + 1, posOfEq - (index + 1))); string value = ad_utility::decodeUrl( request.substr(posOfEq + 1, next - (posOfEq + 1))); if (params.count(param) > 0) { AD_THROW(ad_semsearch::Exception::BAD_REQUEST, "Duplicate HTTP parameter: " + param); } params[param] = value; index = next; next = request.find('&', index + 1); } size_t posOfEq = request.find('=', index + 1); if (posOfEq == request.npos) { AD_THROW(ad_semsearch::Exception::BAD_REQUEST, "Parameter without \"=\" in HTTP Request." + httpRequest); } string param = ad_utility::getLowercaseUtf8( request.substr(index + 1, posOfEq - (index + 1))); string value = ad_utility::decodeUrl(request.substr(posOfEq + 1, request.size() - 1 - (posOfEq + 1))); if (params.count(param) > 0) { AD_THROW(ad_semsearch::Exception::BAD_REQUEST, "Duplicate HTTP parameter."); } params[param] = value; LOG(DEBUG) << "Done parsing HTTP Request." << endl; return params; }
void CommandBuilder::BuildCommand(const wxString &cmdName, wxString cmdParams) { // Stage 1: create a Command object of the right type CommandMessageTarget *scriptOutput = ScriptCommandRelay::GetResponseTarget(); CommandOutputTarget *output = new CommandOutputTarget(new NullProgressTarget(), scriptOutput, scriptOutput); CommandType *factory = CommandDirectory::Get()->LookUp(cmdName); if (factory == NULL) { // Fall back to hoping the Batch Command system can handle it CommandType *type = CommandDirectory::Get()->LookUp(wxT("BatchCommand")); wxASSERT(type != NULL); mCommand = type->Create(output); mCommand->SetParameter(wxT("CommandName"), cmdName); mCommand->SetParameter(wxT("ParamString"), cmdParams); Success(new ApplyAndSendResponse(mCommand)); return; } CommandSignature &signature = factory->GetSignature(); mCommand = factory->Create(output); // Stage 2: set the parameters ShuttleCli shuttle; shuttle.mParams = cmdParams; shuttle.mbStoreInClient = true; ParamValueMap::const_iterator iter; ParamValueMap params = signature.GetDefaults(); for (iter = params.begin(); iter != params.end(); ++iter) { wxString paramString; if (shuttle.TransferString(iter->first, paramString, wxT(""))) { if (!mCommand->SetParameter(iter->first, paramString)) { Failure(); return; } } } // Check for unrecognised parameters while (cmdParams != wxEmptyString) { cmdParams.Trim(true); cmdParams.Trim(false); int splitAt = cmdParams.Find(wxT('=')); if (splitAt < 0 && cmdParams != wxEmptyString) { Failure(wxT("Parameter string is missing '='")); return; } wxString paramName = cmdParams.Left(splitAt); if (params.find(paramName) == params.end()) { Failure(wxT("Unrecognized parameter: '") + paramName + wxT("'")); return; } cmdParams = cmdParams.Mid(splitAt+1); splitAt = cmdParams.Find(wxT(' ')); if (splitAt < 0) { splitAt = cmdParams.Len(); } cmdParams = cmdParams.Mid(splitAt); } Success(new ApplyAndSendResponse(mCommand)); }