HTTPRequest(http_request& request) : request(request) { // store all argument keys as lower case to be ensure we support any spelling auto& f = std::use_facet<std::ctype<utility::char_t>>(std::locale()); map<string_t, string_t> srcArguments = uri::split_query(uri::decode(request.request_uri().query())); for (const auto& srcArg : srcArguments) { string_t key = srcArg.first; f.tolower(&key[0], &key[0] + key.size()); arguments[string(key.begin(), key.end())] = string(srcArg.second.begin(), srcArg.second.end()); } }
void MyListener::handle_get(http_request message) { web::uri req_uri = message.request_uri(); utility::string_t req_query = req_uri.query(); auto req_split_query = web::uri::split_query(req_query); //utility::string_t params; std::string error; for (auto it = req_split_query.begin(); it != req_split_query.end(); ++it) { //params += (*it).first + utility::conversions::to_string_t(": ") + (*it).second + utility::conversions::to_string_t("\n"); // ***** Проверка логина ***** if (utility::conversions::to_utf8string((*it).first) == "login") { std::string login = utility::conversions::to_utf8string((*it).second); for (auto iter = login.begin(); iter != login.end(); ++iter) { if (!(*iter >= 'a' && *iter <= 'z')) error += "Error in login. Unacceptable symbols\n"; } } // ***** Проверка пароля ***** if (utility::conversions::to_utf8string((*it).first) == "password") { std::string pass = utility::conversions::to_utf8string((*it).second); if (pass.length() < 8) error += "Error in password. Insufficient password length\n"; for (auto iter = pass.begin(); iter != pass.end(); ++iter) { //if (!isdigit(*iter) && !isalpha(*iter) && !ispunct(*iter)) if (!(*iter >= 33 && *iter <= 126)) { error += "Error in password. Unacceptable symbols\n"; } } } } if (error == "") message.reply(status_codes::OK, "OK");// "OK\n" + utility::conversions::to_utf8string(params)); else message.reply(status_codes::BadRequest, error);// + '\n' + utility::conversions::to_utf8string(params)); ////message.reply(status_codes::OK, params); }
void BlackJackDealer::word_article_get(http_request message) { // message.method() auto r_uri = message.request_uri(); uri_builder uri_b(r_uri); string s_q = uri_b.query(); // stringstream ss; // auto json = dictHandler->PrefixMatch(); // json.serialize(ss); // auto str = ss.str(); UrlParameter url_par(s_q); string s_res; dictHandler->FindArticle(url_par.value, s_res); cout << s_res << endl; QString qt_str = QString::fromStdString(s_res); QDomDocument qdoc; qdoc.setContent(qt_str); QDomNode child = qdoc.firstChild(); QString body_str; QTextStream stream(&body_str); child.save(stream, QDomNode::CDATASectionNode /* = 4 */); s_res = body_str.toUtf8().constData();; http_response http_resp(status_codes::OK); // http_resp.set_body(s_res); http_resp.set_body(s_res); http_resp.headers().add("Access-Control-Allow-Origin", "*"); http_resp.headers().add("Content-Type","application/json"); message.reply(http_resp); }
void BlackJackDealer::handle_get(http_request message) { // UrlParameters pars(message.request_uri()); auto q_str = message.request_uri().query(); UrlParameter url_par(q_str); stringstream ss; auto json = dictHandler->PrefixMatch(url_par.value); json.serialize(ss); auto str = ss.str(); http_response http_resp(status_codes::OK); http_resp.set_body(str); http_resp.headers().add("Access-Control-Allow-Origin", "*"); http_resp.headers().add("Content-Type","application/json"); message.reply(http_resp); }
// // A POST of the dealer resource creates a new table and returns a resource for // that table. // void BlackJackDealer::handle_post(http_request message) { ucout << message.to_string() << endl; auto paths = uri::split_path(uri::decode(message.relative_uri().path())); if (paths.empty()) { utility::ostringstream_t nextIdString; nextIdString << nextId; std::shared_ptr<DealerTable> tbl = std::make_shared<DealerTable>(nextId, 8, 6); s_tables[nextIdString.str()] = tbl; nextId += 1; message.reply(status_codes::OK, BJPutResponse(ST_PlaceBet, tbl->AsJSON()).AsJSON()); return; } utility::string_t wtable_id = paths[0]; const utility::string_t table_id = wtable_id; // Join an existing table. auto found = s_tables.find(table_id); if (found == s_tables.end()) { message.reply(status_codes::NotFound); return; } auto table = std::static_pointer_cast<DealerTable>(found->second); if ( table->Players.size() < table->Capacity ) { std::map<utility::string_t, utility::string_t> query = uri::split_query(uri::decode(message.request_uri().query())); auto cntEntry = query.find(QUERY_NAME); if (cntEntry != query.end() && !cntEntry->second.empty()) { table->AddPlayer(Player(cntEntry->second)); message.reply(status_codes::OK, BJPutResponse(ST_PlaceBet, table->AsJSON()).AsJSON()); } else { message.reply(status_codes::Forbidden, U("Player name is required in query")); } } else { utility::ostringstream_t os; os << U("Table ") << table->Id << U(" is full"); message.reply(status_codes::Forbidden, os.str()); } };
// // A DELETE of the player resource leaves the table. // void BlackJackDealer::handle_delete(http_request message) { ucout << message.to_string() << endl; auto paths = uri::split_path(uri::decode(message.relative_uri().path())); if (paths.empty()) { message.reply(status_codes::Forbidden, U("TableId is required.")); return; } utility::string_t wtable_id = paths[0]; const utility::string_t table_id = wtable_id; // Get information on a specific table. auto found = s_tables.find(table_id); if (found == s_tables.end()) { message.reply(status_codes::NotFound); return; } auto table = std::static_pointer_cast<DealerTable>(found->second); std::map<utility::string_t, utility::string_t> query = uri::split_query(uri::decode(message.request_uri().query())); auto cntEntry = query.find(QUERY_NAME); if ( cntEntry != query.end() ) { if ( table->RemovePlayer(cntEntry->second) ) { message.reply(status_codes::OK); } else { message.reply(status_codes::NotFound); } } else { message.reply(status_codes::Forbidden, U("Player name is required in query")); } };