Skip to content

Commit

Permalink
Propagate request context to handlers
Browse files Browse the repository at this point in the history
  • Loading branch information
Woazboat committed Jul 2, 2024
1 parent 7053f74 commit 9bef335
Show file tree
Hide file tree
Showing 24 changed files with 437 additions and 199 deletions.
6 changes: 4 additions & 2 deletions include/cgimap/api06/changeset_close_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,8 @@
#include "cgimap/handler.hpp"
#include "cgimap/request.hpp"

struct RequestContext;

namespace api06 {

class changeset_close_responder : public text_responder {
Expand All @@ -24,7 +26,7 @@ class changeset_close_responder : public text_responder {
data_update &,
osm_changeset_id_t,
const std::string &,
std::optional<osm_user_id_t>);
const RequestContext& req_ctx);
};

class changeset_close_handler : public payload_enabled_handler {
Expand All @@ -36,7 +38,7 @@ class changeset_close_handler : public payload_enabled_handler {

responder_ptr_t responder(data_update &,
const std::string &payload,
std::optional<osm_user_id_t> user_id) const override;
const RequestContext& req_ctx) const override;
bool requires_selection_after_update() const override;

private:
Expand Down
4 changes: 2 additions & 2 deletions include/cgimap/api06/changeset_create_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -23,7 +23,7 @@ class changeset_create_responder : public text_responder {
changeset_create_responder(mime::type,
data_update &,
const std::string &,
std::optional<osm_user_id_t>);
const RequestContext& req_ctx);
};

class changeset_create_handler : public payload_enabled_handler {
Expand All @@ -35,7 +35,7 @@ class changeset_create_handler : public payload_enabled_handler {

responder_ptr_t responder(data_update &,
const std::string &payload,
std::optional<osm_user_id_t> user_id) const override;
const RequestContext& req_ctx) const override;
bool requires_selection_after_update() const override;
};

Expand Down
4 changes: 2 additions & 2 deletions include/cgimap/api06/changeset_update_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@ class changeset_update_responder : public text_responder {
data_update &,
osm_changeset_id_t id_,
const std::string & payload,
std::optional<osm_user_id_t> user_id);
const RequestContext& req_ctx);
};

class changeset_update_sel_responder : public osm_current_responder {
Expand All @@ -46,7 +46,7 @@ class changeset_update_handler : public payload_enabled_handler {

responder_ptr_t responder(data_update &,
const std::string &payload,
std::optional<osm_user_id_t> user_id) const override;
const RequestContext& req_ctx) const override;
bool requires_selection_after_update() const override;

private:
Expand Down
6 changes: 4 additions & 2 deletions include/cgimap/api06/changeset_upload_handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -16,13 +16,15 @@
#include "cgimap/osm_diffresult_responder.hpp"
#include "cgimap/request.hpp"

struct RequestContext;

namespace api06 {

class changeset_upload_responder : public osm_diffresult_responder {
public:
changeset_upload_responder(mime::type, data_update &, osm_changeset_id_t,
const std::string &,
std::optional<osm_user_id_t>);
const RequestContext& req_ctx);
};

class changeset_upload_handler : public payload_enabled_handler {
Expand All @@ -34,7 +36,7 @@ class changeset_upload_handler : public payload_enabled_handler {

responder_ptr_t responder(data_update &,
const std::string &payload,
std::optional<osm_user_id_t> user_id) const override;
const RequestContext& req_ctx) const override;
bool requires_selection_after_update() const override;

private:
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,12 +17,14 @@
#include "cgimap/api06/changeset_upload/osmchange_tracking.hpp"
#include "cgimap/backend/apidb/transaction_manager.hpp"

struct RequestContext;

class ApiDB_Changeset_Updater : public api06::Changeset_Updater {

public:
ApiDB_Changeset_Updater(Transaction_Manager &_m,
osm_changeset_id_t _changeset,
osm_user_id_t _uid);
const RequestContext& _req_ctx,
osm_changeset_id_t _changeset);

~ApiDB_Changeset_Updater() override = default;

Expand All @@ -48,9 +50,9 @@ class ApiDB_Changeset_Updater : public api06::Changeset_Updater {
void changeset_insert_cs ();

Transaction_Manager &m;
const RequestContext& req_ctx;
uint32_t cs_num_changes{0};
osm_changeset_id_t changeset;
osm_user_id_t uid;
bbox_t cs_bbox{};
};

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -20,11 +20,14 @@
#include <memory>
#include <set>

struct RequestContext;


class ApiDB_Node_Updater : public api06::Node_Updater {

public:
ApiDB_Node_Updater(Transaction_Manager &_m,
const RequestContext& req_ctx,
api06::OSMChange_Tracking &ct);

~ApiDB_Node_Updater() override = default;
Expand Down Expand Up @@ -108,6 +111,7 @@ class ApiDB_Node_Updater : public api06::Node_Updater {
void delete_current_node_tags(const std::vector<osm_nwr_id_t> &ids);

Transaction_Manager &m;
const RequestContext& req_ctx;
api06::OSMChange_Tracking &ct;

std::vector<node_t> create_nodes;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,13 +21,16 @@
#include <memory>
#include <set>

struct RequestContext;

using RelationMemberList = std::vector<api06::RelationMember>;
using TagList = std::map<std::string, std::string>;

class ApiDB_Relation_Updater : public api06::Relation_Updater {

public:
ApiDB_Relation_Updater(Transaction_Manager &_m,
const RequestContext& _req_ctx,
api06::OSMChange_Tracking &ct);

~ApiDB_Relation_Updater() override = default;
Expand Down Expand Up @@ -175,6 +178,7 @@ class ApiDB_Relation_Updater : public api06::Relation_Updater {
const std::set<osm_nwr_id_t> &direct_relation_ids);

Transaction_Manager &m;
const RequestContext& req_ctx;
api06::OSMChange_Tracking &ct;

std::vector<relation_t> create_relations;
Expand Down
4 changes: 4 additions & 0 deletions include/cgimap/backend/apidb/changeset_upload/way_updater.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@
#include <memory>
#include <set>

struct RequestContext;

/* Way operations
*
*/
Expand All @@ -29,6 +31,7 @@ class ApiDB_Way_Updater : public api06::Way_Updater {

public:
ApiDB_Way_Updater(Transaction_Manager &_m,
const RequestContext& _req_ctx,
api06::OSMChange_Tracking &ct);

~ApiDB_Way_Updater() override = default;
Expand Down Expand Up @@ -130,6 +133,7 @@ class ApiDB_Way_Updater : public api06::Way_Updater {
void delete_current_way_nodes(std::vector<osm_nwr_id_t> ids);

Transaction_Manager &m;
const RequestContext& req_ctx;
api06::OSMChange_Tracking &ct;

std::vector<way_t> create_ways;
Expand Down
10 changes: 6 additions & 4 deletions include/cgimap/backend/apidb/pgsql_update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,8 @@
#include <pqxx/pqxx>
#include <boost/program_options.hpp>

struct RequestContext;

class pgsql_update : public data_update {

public:
Expand All @@ -26,16 +28,16 @@ class pgsql_update : public data_update {
~pgsql_update() override = default;

std::unique_ptr<api06::Changeset_Updater>
get_changeset_updater(osm_changeset_id_t _changeset, osm_user_id_t _uid) override;
get_changeset_updater(const RequestContext& ctx, osm_changeset_id_t _changeset) override;

std::unique_ptr<api06::Node_Updater>
get_node_updater(api06::OSMChange_Tracking &ct) override;
get_node_updater(const RequestContext& ctx, api06::OSMChange_Tracking &ct) override;

std::unique_ptr<api06::Way_Updater>
get_way_updater(api06::OSMChange_Tracking &ct) override;
get_way_updater(const RequestContext& ctx, api06::OSMChange_Tracking &ct) override;

std::unique_ptr<api06::Relation_Updater>
get_relation_updater(api06::OSMChange_Tracking &ct) override;
get_relation_updater(const RequestContext& ctx, api06::OSMChange_Tracking &ct) override;

void commit() override;

Expand Down
10 changes: 6 additions & 4 deletions include/cgimap/data_update.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
#include <memory>
#include <vector>

struct RequestContext;

class data_update {
public:
data_update() = default;
Expand All @@ -37,16 +39,16 @@ class data_update {
data_update& operator=(data_update&&) = delete;

virtual std::unique_ptr<api06::Changeset_Updater>
get_changeset_updater(osm_changeset_id_t _changeset, osm_user_id_t _uid) = 0;
get_changeset_updater(const RequestContext& ctx, osm_changeset_id_t _changeset) = 0;

virtual std::unique_ptr<api06::Node_Updater>
get_node_updater(api06::OSMChange_Tracking &ct) = 0;
get_node_updater(const RequestContext& ctx, api06::OSMChange_Tracking &ct) = 0;

virtual std::unique_ptr<api06::Way_Updater>
get_way_updater(api06::OSMChange_Tracking &ct) = 0;
get_way_updater(const RequestContext& ctx, api06::OSMChange_Tracking &ct) = 0;

virtual std::unique_ptr<api06::Relation_Updater>
get_relation_updater(api06::OSMChange_Tracking &ct) = 0;
get_relation_updater(const RequestContext& ctx, api06::OSMChange_Tracking &ct) = 0;

virtual void
commit() = 0;
Expand Down
2 changes: 1 addition & 1 deletion include/cgimap/handler.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -96,7 +96,7 @@ class payload_enabled_handler : public handler {
// Responder used to update the database
virtual responder_ptr_t responder(data_update &,
const std::string & payload,
std::optional<osm_user_id_t> user_id) const = 0;
const RequestContext& req_ctx) const = 0;

// Optional responder to return XML response back to caller of the API method
responder_ptr_t responder(data_selection &) const override = 0;
Expand Down
37 changes: 37 additions & 0 deletions include/cgimap/request_context.hpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
/**
* SPDX-License-Identifier: GPL-2.0-only
*
* This file is part of openstreetmap-cgimap (https://github.com/zerebubuth/openstreetmap-cgimap/).
*
* Copyright (C) 2009-2024 by the CGImap developer community.
* For a full list of authors see the git log.
*/

#ifndef REQUEST_CONTEXT_HPP
#define REQUEST_CONTEXT_HPP

#include "cgimap/types.hpp"

#include <optional>
#include <set>
#include <chrono>

struct request;

struct UserInfo
{
osm_user_id_t id = {};
std::set<osm_user_role_t> user_roles = {};
bool allow_api_write = false;

bool has_role(osm_user_role_t role) const { return user_roles.count(role) > 0; }
};

struct RequestContext
{
request& req;
std::chrono::system_clock::time_point start_time = {};
std::optional<UserInfo> user = {};
};

#endif /* REQUEST_CONTEXT_HPP */
3 changes: 2 additions & 1 deletion include/cgimap/request_helpers.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -10,9 +10,10 @@
#ifndef REQUEST_HELPERS_HPP
#define REQUEST_HELPERS_HPP

#include <string>
#include "cgimap/request.hpp"
#include "cgimap/http.hpp"

#include <string>
#include <memory>

/**
Expand Down
8 changes: 4 additions & 4 deletions src/api06/changeset_close_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,10 +29,10 @@ changeset_close_responder::changeset_close_responder(mime::type mt,
data_update & upd,
osm_changeset_id_t changeset,
const std::string &,
std::optional<osm_user_id_t> user_id)
const RequestContext& req_ctx)
: text_responder(mt) {

auto changeset_updater = upd.get_changeset_updater(changeset, *user_id);
auto changeset_updater = upd.get_changeset_updater(req_ctx, changeset);
changeset_updater->api_close_changeset();
upd.commit();
}
Expand All @@ -55,8 +55,8 @@ responder_ptr_t changeset_close_handler::responder(data_selection &) const {

responder_ptr_t changeset_close_handler::responder(data_update & upd,
const std::string &payload,
std::optional<osm_user_id_t> user_id) const {
return std::make_unique<changeset_close_responder>(mime_type, upd, id, payload, user_id);
const RequestContext& req_ctx) const {
return std::make_unique<changeset_close_responder>(mime_type, upd, id, payload, req_ctx);
}

bool changeset_close_handler::requires_selection_after_update() const {
Expand Down
8 changes: 4 additions & 4 deletions src/api06/changeset_create_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,12 +28,12 @@ namespace api06 {
changeset_create_responder::changeset_create_responder(mime::type mt,
data_update & upd,
const std::string &payload,
std::optional<osm_user_id_t> user_id)
const RequestContext& req_ctx)
: text_responder(mt) {

osm_changeset_id_t changeset = 0;

auto changeset_updater = upd.get_changeset_updater(changeset, *user_id);
auto changeset_updater = upd.get_changeset_updater(req_ctx, changeset);
auto tags = ChangesetXMLParser().process_message(payload);
changeset = changeset_updater->api_create_changeset(tags);

Expand All @@ -56,8 +56,8 @@ changeset_create_handler::responder(data_selection &) const {

responder_ptr_t changeset_create_handler::responder(data_update & upd,
const std::string &payload,
std::optional<osm_user_id_t> user_id) const {
return std::make_unique<changeset_create_responder>(mime_type, upd, payload, user_id);
const RequestContext& req_ctx) const {
return std::make_unique<changeset_create_responder>(mime_type, upd, payload, req_ctx);
}

bool changeset_create_handler::requires_selection_after_update() const {
Expand Down
8 changes: 4 additions & 4 deletions src/api06/changeset_update_handler.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,10 +30,10 @@ changeset_update_responder::changeset_update_responder(
data_update &upd,
osm_changeset_id_t changeset_id,
const std::string &payload,
std::optional<osm_user_id_t> user_id)
const RequestContext& req_ctx)
: text_responder(mt){

auto changeset_updater = upd.get_changeset_updater(changeset_id, *user_id);
auto changeset_updater = upd.get_changeset_updater(req_ctx, changeset_id);

auto tags = ChangesetXMLParser().process_message(payload);

Expand Down Expand Up @@ -69,8 +69,8 @@ changeset_update_handler::responder(data_selection &sel) const {

responder_ptr_t changeset_update_handler::responder(data_update & upd,
const std::string &payload,
std::optional<osm_user_id_t> user_id) const {
return std::make_unique<changeset_update_responder>(mime_type, upd, id, payload, user_id);
const RequestContext& req_ctx) const {
return std::make_unique<changeset_update_responder>(mime_type, upd, id, payload, req_ctx);
}

bool changeset_update_handler::requires_selection_after_update() const {
Expand Down
Loading

0 comments on commit 9bef335

Please sign in to comment.