Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

add option to allow access methods from default url #1214

Merged
merged 3 commits into from
Sep 2, 2020
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion src/brpc/policy/http_rpc_protocol.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1011,7 +1011,7 @@ FindMethodPropertyByURI(const std::string& uri_path, const Server* server,
const Server::MethodProperty* mp =
FindMethodPropertyByURIImpl(uri_path, server, unresolved_path);
if (mp != NULL) {
if (mp->http_url != NULL) {
if (mp->http_url != NULL && mp->restful_mapped_only) {
// the restful method is accessed from its
// default url (SERVICE/METHOD) which should be rejected.
return NULL;
Expand Down
7 changes: 6 additions & 1 deletion src/brpc/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1305,6 +1305,7 @@ int Server::AddServiceInternal(google::protobuf::Service* service,
}
if (mp->http_url == NULL) {
mp->http_url = new std::string(mappings[i].path.to_string());
mp->restful_mapped_only = svc_opt.restful_mapped_only;
} else {
if (!mp->http_url->empty()) {
mp->http_url->append(" @");
Expand Down Expand Up @@ -1345,6 +1346,7 @@ int Server::AddServiceInternal(google::protobuf::Service* service,
}
if (mp->http_url == NULL) {
mp->http_url = new std::string(mappings[i].path.to_string());
mp->restful_mapped_only = svc_opt.restful_mapped_only;
} else {
if (!mp->http_url->empty()) {
mp->http_url->append(" @");
Expand Down Expand Up @@ -1384,6 +1386,7 @@ int Server::AddServiceInternal(google::protobuf::Service* service,

ServiceOptions::ServiceOptions()
: ownership(SERVER_DOESNT_OWN_SERVICE)
, restful_mapped_only(true)
, allow_http_body_to_pb(true)
#ifdef BAIDU_INTERNAL
, pb_bytes_to_base64(false)
Expand All @@ -1401,11 +1404,13 @@ int Server::AddService(google::protobuf::Service* service,

int Server::AddService(google::protobuf::Service* service,
ServiceOwnership ownership,
const butil::StringPiece& restful_mappings) {
const butil::StringPiece& restful_mappings,
bool restful_mapped_only) {
ServiceOptions options;
options.ownership = ownership;
// TODO: This is weird
options.restful_mappings = restful_mappings.as_string();
options.restful_mapped_only = restful_mapped_only;
return AddServiceInternal(service, false, options);
}

Expand Down
9 changes: 8 additions & 1 deletion src/brpc/server.h
Original file line number Diff line number Diff line change
Expand Up @@ -277,6 +277,11 @@ struct ServiceOptions {
// Default: empty
std::string restful_mappings;

// Work with restful_mappings, if this flag is true, reject methods accessed
// from default urls (SERVICE/METHOD).
// Default: true
bool restful_mapped_only;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

叫allow_default_url吧,更明确一点。注意改名后,值是相反的。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改


// [ Not recommended to change this option ]
// If this flag is true, the service will convert http body to protobuf
// when the pb schema is non-empty in http servings. The body must be
Expand Down Expand Up @@ -334,6 +339,7 @@ class Server {
struct MethodProperty {
bool is_builtin_service;
bool own_method_status;
bool restful_mapped_only;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

这个要移到下面的params中。

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

已改

// Parameters which have nothing to do with management of services, but
// will be used when the service is queried.
struct OpaqueParams {
Expand Down Expand Up @@ -414,7 +420,8 @@ class Server {
ServiceOwnership ownership);
int AddService(google::protobuf::Service* service,
ServiceOwnership ownership,
const butil::StringPiece& restful_mappings);
const butil::StringPiece& restful_mappings,
bool restful_mapped_only = true);
int AddService(google::protobuf::Service* service,
const ServiceOptions& options);

Expand Down