void HttpToolbox::CompileGetArguments(IHttpHandler::Arguments& compiled, const IHttpHandler::GetArguments& source) { compiled.clear(); for (size_t i = 0; i < source.size(); i++) { compiled[source[i].first] = source[i].second; } }
std::string HttpToolbox::GetArgument(const IHttpHandler::GetArguments& getArguments, const std::string& name, const std::string& defaultValue) { for (size_t i = 0; i < getArguments.size(); i++) { if (getArguments[i].first == name) { return getArguments[i].second; } } return defaultValue; }
void HttpToolbox::ParseGetQuery(UriComponents& uri, IHttpHandler::GetArguments& getArguments, const char* query) { const char *questionMark = ::strchr(query, '?'); if (questionMark == NULL) { // No question mark in the string Toolbox::SplitUriComponents(uri, query); getArguments.clear(); } else { Toolbox::SplitUriComponents(uri, std::string(query, questionMark)); HttpToolbox::ParseGetArguments(getArguments, questionMark + 1); } }
static void SplitGETNameValue(IHttpHandler::GetArguments& result, const char* start, const char* end) { std::string name, value; const char* equal = strchr(start, '='); if (equal == NULL || equal >= end) { name = std::string(start, end - start); //value = ""; } else { name = std::string(start, equal - start); value = std::string(equal + 1, end); } Toolbox::UrlDecode(name); Toolbox::UrlDecode(value); result.push_back(std::make_pair(name, value)); }
static bool ExtractMethod(HttpMethod& method, const struct mg_request_info *request, const IHttpHandler::Arguments& headers, const IHttpHandler::GetArguments& argumentsGET) { std::string overriden; // Check whether some PUT/DELETE faking is done // 1. Faking with Google's approach IHttpHandler::Arguments::const_iterator methodOverride = headers.find("x-http-method-override"); if (methodOverride != headers.end()) { overriden = methodOverride->second; } else if (!strcmp(request->request_method, "GET")) { // 2. Faking with Ruby on Rail's approach // GET /my/resource?_method=delete <=> DELETE /my/resource for (size_t i = 0; i < argumentsGET.size(); i++) { if (argumentsGET[i].first == "_method") { overriden = argumentsGET[i].second; break; } } } if (overriden.size() > 0) { // A faking has been done within this request Toolbox::ToUpperCase(overriden); LOG(INFO) << "HTTP method faking has been detected for " << overriden; if (overriden == "PUT") { method = HttpMethod_Put; return true; } else if (overriden == "DELETE") { method = HttpMethod_Delete; return true; } else { return false; } } // No PUT/DELETE faking was present if (!strcmp(request->request_method, "GET")) { method = HttpMethod_Get; } else if (!strcmp(request->request_method, "POST")) { method = HttpMethod_Post; } else if (!strcmp(request->request_method, "DELETE")) { method = HttpMethod_Delete; } else if (!strcmp(request->request_method, "PUT")) { method = HttpMethod_Put; } else { return false; } return true; }