bool Uri::Private::parseURIReference() { if (parseURI()) { return true; } clearContentsKeepUri(); return parseRelativeRef(); }
bool NotificationManager::checkPermission(const Compositor::NotificationData& data) { ILOG_TRACE_F(ILX_NOTIFICATIONMAN); // TODO check permission for notifications. if (data.client) return true; std::string query; std::string path; parseURI(data.origin, query, path); ILOG_DEBUG(ILX_NOTIFICATIONMAN, " -> path: %s\n", path.c_str()); ILOG_DEBUG(ILX_NOTIFICATIONMAN, " -> query: %s\n", query.c_str()); return true; }
bool HTTPParser::parse(HTTPRequest &request) { static const string delim = "\r\n\r\n"; int pos = m_buf.find(delim); if (pos == string::npos) return false; string data = m_buf.substr(0, pos); m_buf.erase(0, pos + delim.length()); vector<string> lines = move(Utils::Strings::split(data, "\r\n")); vector<string> parts = move(Utils::Strings::split(lines[0])); if (parts.size() != 3) throw HTTPIncompleteRequest(Utils::Strings::format("Invalid HTTP request \"%s\"", lines[0].c_str())); string method = parts[0]; if (!isMethod(method)) throw HTTPInvalidMethod(Utils::Strings::format("Invalid HTTP method \"%s\"", method.c_str())); string uri = parts[1]; map<string, string> params; parseURI(uri, params); string version = parts[2]; if (version != "HTTP/1.1") throw HTTPUnsupportedVersion(Utils::Strings::format("Unsupported HTTP version \"%s\"", version.c_str())); request = HTTPRequest(method, uri, version, params); return true; }
QTSS_Error HTTPRequest::parseRequestLine(StringParser* parser) { // Get the method - If the method is not one of the defined methods // then it doesn't return an error but sets fMethod to httpIllegalMethod StrPtrLen theParsedData; parser->ConsumeWord(&theParsedData); fMethod = HTTPProtocol::GetMethod(&theParsedData); //还有可能是HTTP Response类型 if ((fMethod == httpIllegalMethod) && theParsedData.Equal("HTTP")) { parser->ConsumeUntilWhitespace();//过滤掉HTTP/1.1 parser->ConsumeUntilDigit(NULL); UInt32 statusCode = parser->ConsumeInteger(NULL); if (statusCode != 0) { fHTTPType = httpResponseType; parser->ConsumeWhitespace(); parser->ConsumeUntilWhitespace(NULL); // Go past the end of line if (!parser->ExpectEOL()) { fStatusCode = httpBadRequest; return QTSS_BadArgument; // Request line is not properly formatted! } return QTSS_NoErr; } } else if (fMethod == httpIllegalMethod) { if (!parser->ExpectEOL()) { fStatusCode = httpBadRequest; return QTSS_BadArgument; // Request line is not properly formatted! } return QTSS_NoErr; } // Consume whitespace parser->ConsumeWhitespace(); fHTTPType = httpRequestType; // Parse the URI - If it fails returns an error after setting // the fStatusCode to the appropriate error code QTSS_Error err = parseURI(parser); if (err != QTSS_NoErr) return err; // Consume whitespace parser->ConsumeWhitespace(); // If there is a version, consume the version string StrPtrLen versionStr; parser->ConsumeUntil(&versionStr, StringParser::sEOLMask); // Check the version if (versionStr.Len > 0) fVersion = HTTPProtocol::GetVersion(&versionStr); // Go past the end of line if (!parser->ExpectEOL()) { fStatusCode = httpBadRequest; return QTSS_BadArgument; // Request line is not properly formatted! } return QTSS_NoErr; }
/** * @brief RequestHandler::handlePost * @param uri * @param p_data * @return */ void RequestHandler::handleRequest(ConnectionInfo &conInfo) { vector<string> parsedURI = parseURI(conInfo.url); string p_image[] = {"index", "images", "IDENTIFIER", ""}; string p_searchImage[] = {"index", "searcher", ""}; string p_ioIndex[] = {"index", "io", ""}; Json::Value ret; conInfo.answerCode = MHD_HTTP_OK; if (testURIWithPattern(parsedURI, p_image) && conInfo.connectionType == PUT) { u_int32_t i_imageId = atoi(parsedURI[2].c_str()); u_int32_t i_ret = featureExtractor->processNewImage( i_imageId, conInfo.uploadedData.size(), conInfo.uploadedData.data()); ret["type"] = Converter::codeToString(i_ret); ret["image_id"] = Json::Value(i_imageId); } else if (testURIWithPattern(parsedURI, p_image) && conInfo.connectionType == DELETE) { u_int32_t i_imageId = atoi(parsedURI[2].c_str()); u_int32_t i_ret = index->removeImage(i_imageId); ret["type"] = Converter::codeToString(i_ret); ret["image_id"] = Json::Value(i_imageId); } else if (testURIWithPattern(parsedURI, p_searchImage) && conInfo.connectionType == POST) { SearchRequest req; req.imageData = conInfo.uploadedData; req.client = NULL; u_int32_t i_ret = imageSearcher->searchImage(req); ret["type"] = Converter::codeToString(i_ret); if (i_ret == OK) { Json::Value imageIds(Json::arrayValue); for (unsigned i = 0; i < req.results.size(); ++i) imageIds.append(req.results[i]); ret["image_ids"] = imageIds; } } else if (testURIWithPattern(parsedURI, p_ioIndex) && conInfo.connectionType == POST) { string dataStr(conInfo.uploadedData.begin(), conInfo.uploadedData.end()); Json::Value data = StringToJson(dataStr); u_int32_t i_ret; if (data["type"] == "LOAD") i_ret = index->load(data["index_path"].asString()); else if (data["type"] == "WRITE") i_ret = index->write(data["index_path"].asString()); else if (data["type"] == "CLEAR") i_ret = index->clear(); else i_ret = ERROR_GENERIC; ret["type"] = Converter::codeToString(i_ret); } else { conInfo.answerCode = MHD_HTTP_INTERNAL_SERVER_ERROR; ret["type"] = Converter::codeToString(ERROR_GENERIC); } conInfo.answerString = JsonToString(ret); }
void WebInterface::worker() { /* Backup the stdio streambufs */ std::streambuf * cin_streambuf = std::cin.rdbuf(); std::streambuf * cout_streambuf = std::cout.rdbuf(); std::streambuf * cerr_streambuf = std::cerr.rdbuf(); const std::string kw_title(KW_TITLE); const std::string kw_head(KW_HEAD); const std::string kw_menu(KW_MENU); const std::string kw_content(KW_CONTENT); FCGX_Request request; /* Initialize FastCGI library and request */ FCGX_Init(); FCGX_InitRequest(&request, 0, FCGI_FAIL_ACCEPT_ON_INTR); LOG_DBG("FastCGI initialization success!"); while (!stop_flag_) { if(FCGX_Accept_r(&request) >= 0) { fcgi_streambuf cin_fcgi_streambuf(request.in); fcgi_streambuf cout_fcgi_streambuf(request.out); fcgi_streambuf cerr_fcgi_streambuf(request.err); std::cin.rdbuf(&cin_fcgi_streambuf); std::cout.rdbuf(&cout_fcgi_streambuf); std::cerr.rdbuf(&cerr_fcgi_streambuf); /* getting the uri from the request */ std::string uri; const char *uri_param = FCGX_GetParam("REQUEST_URI", request.envp); if(!uri_param) { LOG_ERR("Failed to retrieve the request URI environment value!"); uri = URI_PAGE_ERROR; } else { uri = uri_param; } LOG_DBG("Request received: %s", uri.c_str()); /* Check if URI is a file in the home folder and get the mime of * that file (by extension) */ std::string path; std::string mime = if_file_get_mime(uri, &path); if (!mime.empty()) { /* This is a file we need to serve */ StringPtr file_data = Utils::read_file(path); std::cout << "Content-type: " << mime << "\r\n\r\n"; std::cout << *(file_data); file_data.reset(); } else { /* Parse the URI */ std::map<std::string, std::string> uri_data = parseURI(uri); LOG_DBG("URI Parsed, page requested: %s", uri_data[URI_PAGE].c_str()); /* Generate and serve the page depending on the URI */ StringPtr page; std::string content_type = "text/html"; /* Main page requested */ if (uri_data[URI_PAGE].compare(URI_PAGE_MAIN) == 0) { bool success = false; /* Check if a command was sent from the client. */ if (uri_data.find(URI_PAGE_COMMAND) != uri_data.end()) { success = add_command(uri_data[URI_PAGE_COMMAND]); } std::string s; /* Check if the request was sent from javascript or pure HTML */ if(uri_data.find(URI_PAGE_SOURCE) != uri_data.end()) { LOG_DBG("This query's source IS javascript: %s, Source: %s", uri.c_str(), (uri_data[URI_PAGE_SOURCE]).c_str()); content_type = "application/json"; page = generate_command_json(success); } else { LOG_DBG("This query's source IS NOT javascript: %s", uri.c_str()); /* Just generate a standard main page */ page = generate_main_page(); } /* Log page requested */ } else if (uri_data[URI_PAGE].compare(URI_PAGE_LOG) == 0) { page = generate_log_page(); /* Status page requested */ } else if (uri_data[URI_PAGE].compare(URI_PAGE_STATUS) == 0) { page = generate_status_page(); /* Console lines JSON page requested */ } else if (uri_data[URI_PAGE].compare(URI_PAGE_CL) == 0) { if (uri_data.find(URI_PAGE_BEFORE) != uri_data.end()) { content_type = "application/json"; page = generate_cljson_before( uri_data[URI_PAGE_BEFORE]); } else if (uri_data.find(URI_PAGE_AFTER) != uri_data.end()) { content_type = "application/json"; page = generate_cljson_after(uri_data[URI_PAGE_AFTER]); } else { page = generate_error_page(); } /* Log lines JSON page requested */ } else if (uri_data[URI_PAGE].compare(URI_PAGE_LL) == 0) { if (uri_data.find(URI_PAGE_BEFORE) != uri_data.end()) { content_type = "application/json"; page = generate_lljson_before( uri_data[URI_PAGE_BEFORE]); } else if (uri_data.find(URI_PAGE_AFTER) != uri_data.end()) { content_type = "application/json"; page = generate_lljson_after(uri_data[URI_PAGE_AFTER]); } else { page = generate_error_page(); } } else { page = generate_error_page(); } /* Output the generated page with the correct content type */ std::cout << "Content-type: " << content_type << "\r\n\r\n"; std::cout << *(page.get()); } } else { LOG_TRC("FCGX_Aceept_r returned less than 0!"); } } LOG_TRC("Out of accept request loop!"); // Free request strucure FCGX_Finish_r(&request); // Flag the thread as not running anymore. running_ = false; // restore stdio streambufs std::cin.rdbuf(cin_streambuf); std::cout.rdbuf(cout_streambuf); std::cerr.rdbuf(cerr_streambuf); }
URI::URI(const wxString& uri) { parseURI(uri); }