Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
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
5 changes: 5 additions & 0 deletions include/envoy/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -83,3 +83,8 @@ envoy_cc_library(
name = "protocol_interface",
hdrs = ["protocol.h"],
)

envoy_cc_library(
name = "query_params_interface",
hdrs = ["query_params.h"],
)
16 changes: 16 additions & 0 deletions include/envoy/http/query_params.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
#include <map>
#include <string>

namespace Envoy {
namespace Http {
namespace Utility {

// TODO(jmarantz): this should probably be a proper class, with methods to serialize
// using proper formatting. Perhaps similar to
// https://github.com/apache/incubator-pagespeed-mod/blob/master/pagespeed/kernel/http/query_params.h

typedef std::map<std::string, std::string> QueryParams;

} // namespace Utility
} // namespace Http
} // namespace Envoy
2 changes: 2 additions & 0 deletions include/envoy/server/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ envoy_cc_library(
"//include/envoy/http:codes_interface",
"//include/envoy/http:filter_interface",
"//include/envoy/http:header_map_interface",
"//include/envoy/http:query_params_interface",
"//include/envoy/network:listen_socket_interface",
],
)
Expand Down Expand Up @@ -92,6 +93,7 @@ envoy_cc_library(
":options_interface",
"//include/envoy/access_log:access_log_interface",
"//include/envoy/api:api_interface",
"//include/envoy/http:query_params_interface",
"//include/envoy/init:init_interface",
"//include/envoy/local_info:local_info_interface",
"//include/envoy/ratelimit:ratelimit_interface",
Expand Down
18 changes: 18 additions & 0 deletions include/envoy/server/admin.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
#include "envoy/http/codes.h"
#include "envoy/http/filter.h"
#include "envoy/http/header_map.h"
#include "envoy/http/query_params.h"
#include "envoy/network/listen_socket.h"
#include "envoy/server/config_tracker.h"

Expand Down Expand Up @@ -44,6 +45,7 @@ class AdminStream {
*/
virtual const Http::HeaderMap& getRequestHeaders() const PURE;
};

/**
* This macro is used to add handlers to the Admin HTTP Endpoint. It builds
* a callback that executes X when the specified admin handler is hit. This macro can be
Expand Down Expand Up @@ -108,6 +110,22 @@ class Admin {
* @return ConfigTracker& tracker for /config_dump endpoint.
*/
virtual ConfigTracker& getConfigTracker() PURE;

/**
* Executes an admin request with the specified query params. Note: this must
* be called from Envoy's main thread.
*
* @param path the path of the admin URL.
* @param param the query-params passed to the admin request handler.
* @param method the HTTP method (POST or GET).
* @param response_headers populated the the response headers from executing the request,
* most notably content-type.
* @param body populated with the response-body from the admin request.
* @return Http::Code The HTTP response code from the admin request.
*/
virtual Http::Code request(absl::string_view path, const Http::Utility::QueryParams& params,
absl::string_view method, Http::HeaderMap& response_headers,
std::string& body) PURE;
};

} // namespace Server
Expand Down
17 changes: 17 additions & 0 deletions source/common/buffer/buffer_impl.cc
Original file line number Diff line number Diff line change
Expand Up @@ -183,5 +183,22 @@ OwnedImpl::OwnedImpl(const Instance& data) : OwnedImpl() { add(data); }

OwnedImpl::OwnedImpl(const void* data, uint64_t size) : OwnedImpl() { add(data, size); }

std::string OwnedImpl::toString() const {
Copy link
Member

Choose a reason for hiding this comment

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

How is this different to linearize()? Do we need both variants?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I looked at linearize and though they seem similar, I don't see how either can be efficiently implemented on the other.

Copy link
Member

Choose a reason for hiding this comment

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

well, you could linearize and then create a std;;string with the now linearized buffer. This would involve two copies, but is still O(n) and might be fine for the applications we would use this for, with the assumption being that you don't want to do toString() or linearize for anything performance sensitive anyway. There's also the option of just doing an absl::string_view around the linearized buffer.

Copy link
Contributor Author

@jmarantz jmarantz Jun 25, 2018

Choose a reason for hiding this comment

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

linearize mutates the buffer IIUC; I thought it made sense to have a simple observer that can return the fully flattened string.

Moreover it exists in the codebase now, in a test helper, and I'm mostly just moving it. However per your other comment -- I don't really need this in the interface, as everywhere in tests and in admin.cc where it's needed, I have an OwnedImpl. Removed.

Copy link
Member

Choose a reason for hiding this comment

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

I think mutating is a benefit here, it's kind of like defragging. At least naively, it seems you should be able to implement it about as efficiently as the toString() operation.

uint64_t num_slices = getRawSlices(nullptr, 0);
RawSlice slices[num_slices];
getRawSlices(slices, num_slices);
size_t len = 0;
for (RawSlice& slice : slices) {
len += slice.len_;
}
std::string output;
output.reserve(len);
for (RawSlice& slice : slices) {
output.append(static_cast<const char*>(slice.mem_), slice.len_);
}

return output;
}

} // namespace Buffer
} // namespace Envoy
6 changes: 6 additions & 0 deletions source/common/buffer/buffer_impl.h
Original file line number Diff line number Diff line change
Expand Up @@ -86,6 +86,12 @@ class OwnedImpl : public LibEventInstance {
int write(int fd) override;
void postProcess() override {}

/**
* Construct a flattened string from a buffer.
* @return the flattened string.
*/
std::string toString() const;

Event::Libevent::BufferPtr& buffer() override { return buffer_; }

private:
Expand Down
1 change: 1 addition & 0 deletions source/common/http/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -243,6 +243,7 @@ envoy_cc_library(
"//include/envoy/http:codes_interface",
"//include/envoy/http:filter_interface",
"//include/envoy/http:header_map_interface",
"//include/envoy/http:query_params_interface",
"//source/common/buffer:buffer_lib",
"//source/common/common:assert_lib",
"//source/common/common:empty_string",
Expand Down
12 changes: 12 additions & 0 deletions source/common/http/utility.cc
Original file line number Diff line number Diff line change
Expand Up @@ -377,5 +377,17 @@ MessagePtr Utility::prepareHeaders(const ::envoy::api::v2::core::HttpUri& http_u
return message;
}

// TODO(jmarantz): make QueryParams a real class and put this serializer there,
// along with proper URL escaping of the name and value.
std::string Utility::queryParamsToString(const QueryParams& params) {
std::string out;
std::string delim = "?";
for (auto p : params) {
absl::StrAppend(&out, delim, p.first, "=", p.second);
delim = "&";
}
return out;
}

} // namespace Http
} // namespace Envoy
Loading