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 support for un-managed content (std::string_view) in http reply object #844

Closed
wants to merge 1 commit into from
Closed
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
2 changes: 1 addition & 1 deletion apps/httpd/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -42,7 +42,7 @@ class handl : public httpd::handler_base {
public:
virtual future<std::unique_ptr<reply> > handle(const sstring& path,
std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
rep->_content = "hello";
rep->_content.append("hello");
rep->done("html");
return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
}
Expand Down
8 changes: 4 additions & 4 deletions include/seastar/http/function_handlers.hh
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ public:
function_handler(const handle_function & f_handle, const sstring& type)
: _f_handle(
[f_handle](std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
rep->_content += f_handle(*req.get(), *rep.get());
rep->_content.append(f_handle(*req.get(), *rep.get()));
return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
}), _type(type) {
}
Expand All @@ -82,7 +82,7 @@ public:
function_handler(const request_function & _handle, const sstring& type)
: _f_handle(
[_handle](std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
rep->_content += _handle(*req.get());
rep->_content.append(_handle(*req.get()));
return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
}), _type(type) {
}
Expand All @@ -91,7 +91,7 @@ public:
: _f_handle(
[_handle](std::unique_ptr<request> req, std::unique_ptr<reply> rep) {
json::json_return_type res = _handle(*req.get());
rep->_content += res._res;
rep->_content.append(res._res);
return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
}), _type("json") {
}
Expand All @@ -103,7 +103,7 @@ public:
if (res._body_writer) {
rep->write_body("json", std::move(res._body_writer));
} else {
rep->_content += res._res;
rep->_content.append(res._res);

}
return make_ready_future<std::unique_ptr<reply>>(std::move(rep));
Expand Down
54 changes: 51 additions & 3 deletions include/seastar/http/reply.hh
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,42 @@ namespace httpd {
class connection;
class routes;

struct reply_content {
friend class reply;
private:
bool is_unmanaged;
sstring _content;
std::string_view _content_um;

public:
reply_content() {
}
reply_content(sstring _content) : is_unmanaged(false), _content(_content) {
}
reply_content(std::string_view _content) : is_unmanaged(true), _content_um(_content) {
}

const char* data() {
if(is_unmanaged) {
return _content_um.data();
}
return _content.data();
}

size_t size() const noexcept {
if(is_unmanaged) {
return _content_um.size();
}
return _content.size();
}

void append(const sstring& cont) {
if(!is_unmanaged) {
_content += cont;
}
}
};

/**
* A reply to be sent to a client.
*/
Expand Down Expand Up @@ -82,7 +118,7 @@ struct reply {
/**
* The content to be sent in the reply.
*/
sstring _content;
reply_content _content;

sstring _response_line;
reply()
Expand All @@ -101,8 +137,9 @@ struct reply {

reply& set_status(status_type status, const sstring& content = "") {
_status = status;
if (content != "") {
_content = content;
if (content.size()>0) {
_content.is_unmanaged = false;
_content._content = content;
}
return *this;
}
Expand Down Expand Up @@ -168,6 +205,17 @@ struct reply {
*/
void write_body(const sstring& content_type, const sstring& content);

/*!
* \brief Write a string_view as the reply (un-managed string)
*
* \param content_type - is used to choose the content type of the body. Use the file extension
* you would have used for such a content, (i.e. "txt", "html", "json", etc')
* \param content - the message content.
* This would set the the content and content type of the message along
* with any additional information that is needed to send the message.
*/
void write_body(const sstring& content_type, const std::string_view& content);

private:
future<> write_reply_to_connection(connection& con);
future<> write_reply_headers(connection& connection);
Expand Down