// // A PUT to a table resource makes a card request (hit / stay). // void BlackJackDealer::handle_put(http_request message) { ucout << message.to_string() << endl; auto paths = uri::split_path(uri::decode(message.relative_uri().path())); auto query = uri::split_query(uri::decode(message.relative_uri().query())); auto queryItr = query.find(REQUEST); if (paths.empty() || queryItr == query.end()) { message.reply(status_codes::Forbidden, U("TableId and request are required.")); } utility::string_t wtable_id = paths[0]; utility::string_t request = queryItr->second; 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); } auto table = std::static_pointer_cast<DealerTable>(found->second); if ( request == BET ) { table->Bet(message); } else if ( request == DOUBLE ) { table->DoubleDown(message); } else if ( request == INSURE ) { table->Insure(message); } else if ( request == HIT ) { table->Hit(message); } else if ( request == STAY ) { table->Stay(message); } else if ( request == REFRESH ) { table->Wait(message); } else { message.reply(status_codes::Forbidden, U("Unrecognized request")); } };
void RDService::handle_get(http_request message) { std::cout << "GET request got" << std::endl; web::uri reqUri = message.relative_uri(); wcout << "Query:" << reqUri.query() << endl << reqUri.resource().to_string() << endl; utility::string_t queryStr = reqUri.query(); auto path = reqUri.path(); vector<utility::string_t> queryList = splitStringByAnd(queryStr); wstring conditions = U(""); if (queryList.size() > 0) { conditions += queryList[0]; } for (size_t i = 1; i < queryList.size(); i++) { conditions += U(" AND ") + queryList[i]; } string finalCondition(conditions.begin(), conditions.end()); vector<Vehicle> dbResult = DbHelper::getInstance()->getVehicleList((char *)finalCondition.c_str()); string table = ""; auto path2 = message.relative_uri().path(); string reply; for (int i = 0; i < dbResult.size(); i++) { Vehicle v = dbResult[i]; string tr = ""; tr += v.Registration + "#"; tr += to_string(v.Make) + "#"; tr += "" + v.Model + "#"; tr += "" + v.Owner + "#"; table += tr; } utility::string_t replyText(table.begin(), table.end()); message.reply(status_codes::OK, replyText).then([](pplx::task<void> t) { handle_error(t); }); }
// Handler to process HTTP::GET requests. // Replies to the request with data. void CasaLens::handle_get(http_request message) { auto path = message.relative_uri().path(); auto content_data = m_htmlcontentmap.find(path); if (content_data == m_htmlcontentmap.end()) { message.reply(status_codes::NotFound, U("Path not found")).then(std::bind(&handle_error, std::placeholders::_1)); return; } auto file_name = std::get<0>(content_data->second); auto content_type = std::get<1>(content_data->second); concurrency::streams::fstream::open_istream(file_name, std::ios::in).then([=](concurrency::streams::istream is) { message.reply(status_codes::OK, is, content_type).then(std::bind(&handle_error, std::placeholders::_1)); }).then([=](pplx::task<void>& t) { try { t.get(); } catch(...) { // opening the file (open_istream) failed. // Reply with an error. message.reply(status_codes::InternalError).then(std::bind(&handle_error, std::placeholders::_1)); } }); }
// // A GET of the dealer resource produces a list of existing tables. // void BlackJackDealer::handle_get(http_request message) { ucout << message.to_string() << endl; auto paths = http::uri::split_path(http::uri::decode(message.relative_uri().path())); if (paths.empty()) { message.reply(status_codes::OK, TablesAsJSON(U("Available Tables"), s_tables)); 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); } else { message.reply(status_codes::OK, found->second->AsJSON()); } };
// // 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()); } };
// Respond to HTTP::POST messages // Post data will contain the postal code or location string. // Aggregate location data from different services and reply to the POST request. void CasaLens::handle_post(http_request message) { auto path = message.relative_uri().path(); if (0 == path.compare(U("/"))) { message.extract_string() .then([=](const utility::string_t& location) { get_data(message, location); }) .then([](pplx::task<void> t) { handle_error(t); }); } else { message.reply(status_codes::NotFound, U("Path not found")).then([](pplx::task<void> t) { handle_error(t); }); } }
// Respond to HTTP::POST messages // Post data will contain the postal code or location string. // Aggregate location data from different services and reply to the POST request. void CasaLens::handle_post(http_request message) { auto path = message.relative_uri().path(); if (0 == path.compare(U("/"))) { message.extract_string().then([=](const utility::string_t& location) { get_data(message, location); }).then(std::bind(&handle_error, std::placeholders::_1)); } else { message.reply(status_codes::NotFound, U("Path not found")).then(std::bind(&handle_error, std::placeholders::_1)); } }
// // 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")); } };
// Handler to process HTTP::GET requests. // Replies to the request with data. void odata_test_service::handle_get(http_request message) { try { bool is_async = false; auto prefer_header = message.headers().find(U("Prefer")); if (prefer_header != message.headers().end()) { if (prefer_header->second.find(U("respond-async")) != ::odata::utility::string_t::npos) { is_async = true; } } auto parsed_uri = m_uri_parser->parse_uri(message.relative_uri()); odata_message_writer writer(m_model, m_service_root); odata_context_url_builder context_url_builder(m_model, m_service_root); odata_metadata_builder metadata_builder(m_model, m_service_root); ::odata::utility::string_t content; if (parsed_uri->is_service_document()) { // Write service document content = writer.write_service_document(m_service_document); } else if (parsed_uri->is_metadata_document()) { // Write metadata document content = writer.write_metadata_document(); } else { if (parsed_uri->path()->size() >= 1) { if (parsed_uri->path()->segment_at(0)->segment_type() == odata_path_segment_type::EntitySet) { auto entity_set_segment = parsed_uri->path()->segment_at(0)->as<odata_entity_set_segment>(); if (entity_set_segment->entity_set()->get_name() == U("People")) { if (parsed_uri->path()->size() == 1) { auto people = get_people(); auto context_url = context_url_builder.get_context_uri_for_collection_of_entities(entity_set_segment->entity_set()); people->set_context_url(context_url); if (is_async) { std::unordered_map<string_t, string_t> headers; headers[U("OData-Version")] = U("4.0"); headers[U("Content-Type")] = U("application/json;odata.metadata=full"); content = writer.write_asynchronous_odata_value(people, 200, U("OK"), headers); } else { content = writer.write_odata_value(people); } } else if (parsed_uri->path()->segment_at(1)->segment_type() == odata_path_segment_type::Key) { auto key_segment = parsed_uri->path()->segment_at(1)->as<odata_key_segment>(); auto key = key_segment->keys()[0].second->as<::odata::utility::string_t>(); auto single_person = get_single_people(key); single_person->set_is_top_level(true); auto context_url = context_url_builder.get_context_uri_for_entity(entity_set_segment->entity_set()); single_person->set_context_url(context_url); auto id = metadata_builder.get_entity_id(single_person, entity_set_segment->entity_set()); single_person->set_id(id); auto read_link = metadata_builder.get_read_link(single_person, entity_set_segment->entity_set()); single_person->set_read_link(read_link); auto edit_link = metadata_builder.get_edit_link(single_person, entity_set_segment->entity_set()); single_person->set_edit_link(edit_link); content = writer.write_odata_value(single_person); } } } } } message.reply(status_codes::OK, content).then(std::bind(&handle_error, std::placeholders::_1)); } catch (::odata::core::odata_exception &e) { message.reply(status_codes::BadRequest, U("Exception: ") + e.what()).then(std::bind(&handle_error, std::placeholders::_1)); } ////Get odata objects from resorce and odata_path // }