Add response builder to simplify the response build; refine the service interfaces.
parent
4140b4f94c
commit
8a7f53313b
@ -0,0 +1,57 @@
|
||||
#include "webcc/response_builder.h"
|
||||
|
||||
#include "webcc/base64.h"
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/utility.h"
|
||||
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
#include "webcc/gzip.h"
|
||||
#endif
|
||||
|
||||
namespace webcc {
|
||||
|
||||
ResponsePtr ResponseBuilder::operator()() {
|
||||
assert(headers_.size() % 2 == 0);
|
||||
|
||||
auto request = std::make_shared<Response>(code_);
|
||||
|
||||
for (std::size_t i = 1; i < headers_.size(); i += 2) {
|
||||
request->SetHeader(std::move(headers_[i - 1]), std::move(headers_[i]));
|
||||
}
|
||||
|
||||
if (!data_.empty()) {
|
||||
SetContent(request, std::move(data_));
|
||||
|
||||
// TODO: charset.
|
||||
if (json_) {
|
||||
request->SetContentType(media_types::kApplicationJson, "");
|
||||
}
|
||||
}
|
||||
|
||||
return request;
|
||||
}
|
||||
|
||||
ResponseBuilder& ResponseBuilder::Date() {
|
||||
headers_.push_back(headers::kDate);
|
||||
headers_.push_back(utility::GetTimestamp());
|
||||
return *this;
|
||||
}
|
||||
|
||||
void ResponseBuilder::SetContent(ResponsePtr response, std::string&& data) {
|
||||
#if WEBCC_ENABLE_GZIP
|
||||
if (gzip_ && data.size() > kGzipThreshold) {
|
||||
std::string compressed;
|
||||
if (gzip::Compress(data, &compressed)) {
|
||||
response->SetContent(std::move(compressed), true);
|
||||
response->SetHeader(headers::kContentEncoding, "gzip");
|
||||
return;
|
||||
}
|
||||
|
||||
LOG_WARN("Cannot compress the content data!");
|
||||
}
|
||||
#endif // WEBCC_ENABLE_GZIP
|
||||
|
||||
response->SetContent(std::move(data), true);
|
||||
}
|
||||
|
||||
} // namespace webcc
|
@ -0,0 +1,88 @@
|
||||
#ifndef WEBCC_RESPONSE_BUILDER_H_
|
||||
#define WEBCC_RESPONSE_BUILDER_H_
|
||||
|
||||
#include <string>
|
||||
#include <vector>
|
||||
|
||||
#include "webcc/response.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
class ResponseBuilder {
|
||||
public:
|
||||
ResponseBuilder() = default;
|
||||
|
||||
ResponseBuilder(const ResponseBuilder&) = delete;
|
||||
ResponseBuilder& operator=(const ResponseBuilder&) = delete;
|
||||
|
||||
// Build the response.
|
||||
ResponsePtr operator()();
|
||||
|
||||
// NOTE:
|
||||
// The naming convention doesn't follow Google C++ Style for
|
||||
// consistency and simplicity.
|
||||
|
||||
// Some shortcuts for different status codes:
|
||||
ResponseBuilder& OK() { return Code(Status::kOK); }
|
||||
ResponseBuilder& Created() { return Code(Status::kCreated); }
|
||||
ResponseBuilder& BadRequest() { return Code(Status::kBadRequest); }
|
||||
ResponseBuilder& NotFound() { return Code(Status::kNotFound); }
|
||||
ResponseBuilder& NotImplemented() { return Code(Status::kNotImplemented); }
|
||||
|
||||
ResponseBuilder& Code(Status code) {
|
||||
code_ = code;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ResponseBuilder& Data(const std::string& data) {
|
||||
data_ = data;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ResponseBuilder& Data(std::string&& data) {
|
||||
data_ = std::move(data);
|
||||
return *this;
|
||||
}
|
||||
|
||||
ResponseBuilder& Json(bool json = true) {
|
||||
json_ = json;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ResponseBuilder& Gzip(bool gzip = true) {
|
||||
gzip_ = gzip;
|
||||
return *this;
|
||||
}
|
||||
|
||||
ResponseBuilder& Header(const std::string& key, const std::string& value) {
|
||||
headers_.push_back(key);
|
||||
headers_.push_back(value);
|
||||
return *this;
|
||||
}
|
||||
|
||||
// Add the Date header to the response.
|
||||
ResponseBuilder& Date();
|
||||
|
||||
private:
|
||||
void SetContent(ResponsePtr response, std::string&& data);
|
||||
|
||||
private:
|
||||
// Status code.
|
||||
Status code_ = Status::kOK;
|
||||
|
||||
// Data to send in the body of the request.
|
||||
std::string data_;
|
||||
|
||||
// Is the data to send a JSON string?
|
||||
bool json_ = false;
|
||||
|
||||
// Compress the response content.
|
||||
bool gzip_ = false;
|
||||
|
||||
// Additional headers.
|
||||
std::vector<std::string> headers_;
|
||||
};
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_RESPONSE_BUILDER_H_
|
Loading…
Reference in New Issue