Remove PostFile() from session; refine cmake files; replace final with override.
This commit is contained in:
@@ -121,36 +121,6 @@ ResponsePtr ClientSession::Patch(const std::string& url, std::string&& data,
|
||||
return Request(builder());
|
||||
}
|
||||
|
||||
ResponsePtr ClientSession::PostFile(const std::string& url,
|
||||
const std::string& name, const Path& path,
|
||||
const std::vector<std::string>& headers) {
|
||||
RequestBuilder builder;
|
||||
builder.Post().Url(url);
|
||||
|
||||
SetHeaders(headers, &builder);
|
||||
|
||||
builder.File(name, path);
|
||||
|
||||
return Request(builder());
|
||||
}
|
||||
|
||||
ResponsePtr ClientSession::PostFiles(const std::string& url,
|
||||
const std::map<std::string, Path>& paths,
|
||||
const std::vector<std::string>& headers) {
|
||||
assert(!paths.empty());
|
||||
|
||||
RequestBuilder builder;
|
||||
builder.Post().Url(url);
|
||||
|
||||
SetHeaders(headers, &builder);
|
||||
|
||||
for (auto& pair : paths) {
|
||||
builder.File(pair.first, pair.second);
|
||||
}
|
||||
|
||||
return Request(builder());
|
||||
}
|
||||
|
||||
void ClientSession::InitHeaders() {
|
||||
using namespace headers;
|
||||
|
||||
|
||||
@@ -79,16 +79,6 @@ public:
|
||||
ResponsePtr Patch(const std::string& url, std::string&& data, bool json,
|
||||
const std::vector<std::string>& headers = {});
|
||||
|
||||
// Post a file.
|
||||
ResponsePtr PostFile(const std::string& url, const std::string& name,
|
||||
const Path& path,
|
||||
const std::vector<std::string>& headers = {});
|
||||
|
||||
// Post multiple files.
|
||||
ResponsePtr PostFiles(const std::string& url,
|
||||
const std::map<std::string, Path>& paths,
|
||||
const std::vector<std::string>& headers = {});
|
||||
|
||||
private:
|
||||
void InitHeaders();
|
||||
|
||||
|
||||
+12
-23
@@ -6,6 +6,7 @@
|
||||
#include "boost/filesystem/fstream.hpp"
|
||||
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/utility.h"
|
||||
|
||||
namespace bfs = boost::filesystem;
|
||||
|
||||
@@ -24,33 +25,21 @@ const char CRLF[] = { '\r', '\n' };
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
bool Split2(const std::string& str, char token, std::string* part1,
|
||||
std::string* part2) {
|
||||
std::size_t pos = str.find(token);
|
||||
if (pos == std::string::npos) {
|
||||
// Read entire file into string.
|
||||
static bool ReadFile(const Path& path, std::string* output) {
|
||||
// Flag "ate": seek to the end of stream immediately after open.
|
||||
bfs::ifstream stream{ path, std::ios::binary | std::ios::ate };
|
||||
if (stream.fail()) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*part1 = str.substr(0, pos);
|
||||
*part2 = str.substr(pos + 1);
|
||||
|
||||
boost::trim(*part1);
|
||||
boost::trim(*part2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
bool ReadFile(const Path& path, std::string* output) {
|
||||
bfs::ifstream ifs{ path, std::ios::binary | std::ios::ate };
|
||||
if (!ifs) {
|
||||
return false;
|
||||
}
|
||||
|
||||
auto size = ifs.tellg();
|
||||
auto size = stream.tellg();
|
||||
output->resize(static_cast<std::size_t>(size), '\0');
|
||||
ifs.seekg(0);
|
||||
ifs.read(&(*output)[0], size); // TODO: Error handling
|
||||
|
||||
stream.seekg(std::ios::beg);
|
||||
stream.read(&(*output)[0], size);
|
||||
if (stream.fail()) {
|
||||
return false;
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
|
||||
@@ -21,15 +21,6 @@ using Payload = std::vector<boost::asio::const_buffer>;
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
// Split a string to two parts by the given token.
|
||||
bool Split2(const std::string& str, char token, std::string* part1,
|
||||
std::string* part2);
|
||||
|
||||
// Read entire file into string.
|
||||
bool ReadFile(const Path& path, std::string* output);
|
||||
|
||||
// -----------------------------------------------------------------------------
|
||||
|
||||
using Header = std::pair<std::string, std::string>;
|
||||
|
||||
class Headers {
|
||||
|
||||
+1
-1
@@ -68,7 +68,7 @@ public:
|
||||
}
|
||||
|
||||
// Prepare payload.
|
||||
void Prepare() final;
|
||||
void Prepare() override;
|
||||
|
||||
private:
|
||||
void CreateStartLine();
|
||||
|
||||
@@ -4,8 +4,9 @@
|
||||
|
||||
#include "boost/algorithm/string.hpp"
|
||||
|
||||
#include "webcc/request.h"
|
||||
#include "webcc/logger.h"
|
||||
#include "webcc/request.h"
|
||||
#include "webcc/utility.h"
|
||||
|
||||
namespace webcc {
|
||||
|
||||
|
||||
@@ -18,10 +18,10 @@ public:
|
||||
void Init(Request* request);
|
||||
|
||||
private:
|
||||
bool ParseStartLine(const std::string& line) final;
|
||||
bool ParseStartLine(const std::string& line) override;
|
||||
|
||||
// Override to handle multipart form data which is request only.
|
||||
bool ParseContent(const char* data, std::size_t length) final;
|
||||
bool ParseContent(const char* data, std::size_t length) override;
|
||||
|
||||
// Multipart specific parsing helpers.
|
||||
|
||||
|
||||
+1
-1
@@ -28,7 +28,7 @@ public:
|
||||
}
|
||||
|
||||
// Set start line according to status code.
|
||||
void Prepare() final;
|
||||
void Prepare() override;
|
||||
|
||||
private:
|
||||
int status_;
|
||||
|
||||
@@ -16,6 +16,7 @@ void ResponseParser::Init(Response* response) {
|
||||
response_ = response;
|
||||
}
|
||||
|
||||
// TODO: Keep the original message.
|
||||
bool ResponseParser::ParseStartLine(const std::string& line) {
|
||||
std::vector<std::string> parts;
|
||||
boost::split(parts, line, boost::is_any_of(" "), boost::token_compress_on);
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
|
||||
private:
|
||||
// Parse HTTP start line; E.g., "HTTP/1.1 200 OK".
|
||||
bool ParseStartLine(const std::string& line) final;
|
||||
bool ParseStartLine(const std::string& line) override;
|
||||
|
||||
// The result response message.
|
||||
Response* response_;
|
||||
|
||||
@@ -19,7 +19,7 @@ public:
|
||||
bool Bind(RestServicePtr service, const std::string& url, bool is_regex);
|
||||
|
||||
private:
|
||||
void HandleConnection(ConnectionPtr connection) final;
|
||||
void HandleConnection(ConnectionPtr connection) override;
|
||||
|
||||
void SetContent(RequestPtr request, ResponsePtr response,
|
||||
std::string&& content);
|
||||
|
||||
+1
-1
@@ -32,7 +32,7 @@ public:
|
||||
}
|
||||
|
||||
private:
|
||||
RequestHandler* GetRequestHandler() final {
|
||||
RequestHandler* GetRequestHandler() override {
|
||||
return &request_handler_;
|
||||
}
|
||||
|
||||
|
||||
+8
-8
@@ -47,14 +47,14 @@ public:
|
||||
|
||||
void Connect(const std::string& host,
|
||||
const Endpoints& endpoints,
|
||||
boost::system::error_code* ec) final;
|
||||
boost::system::error_code* ec) override;
|
||||
|
||||
void Write(const Request& request,
|
||||
boost::system::error_code* ec) final;
|
||||
boost::system::error_code* ec) override;
|
||||
|
||||
void AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) final;
|
||||
void AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) override;
|
||||
|
||||
void Close(boost::system::error_code* ec) final;
|
||||
void Close(boost::system::error_code* ec) override;
|
||||
|
||||
private:
|
||||
boost::asio::ip::tcp::socket socket_;
|
||||
@@ -71,14 +71,14 @@ public:
|
||||
|
||||
void Connect(const std::string& host,
|
||||
const Endpoints& endpoints,
|
||||
boost::system::error_code* ec) final;
|
||||
boost::system::error_code* ec) override;
|
||||
|
||||
void Write(const Request& request,
|
||||
boost::system::error_code* ec) final;
|
||||
boost::system::error_code* ec) override;
|
||||
|
||||
void AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) final;
|
||||
void AsyncReadSome(ReadHandler&& handler, std::vector<char>* buffer) override;
|
||||
|
||||
void Close(boost::system::error_code* ec) final;
|
||||
void Close(boost::system::error_code* ec) override;
|
||||
|
||||
private:
|
||||
void Handshake(const std::string& host, boost::system::error_code* ec);
|
||||
|
||||
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <sstream>
|
||||
|
||||
#include "boost/algorithm/string.hpp"
|
||||
#include "boost/uuid/random_generator.hpp"
|
||||
#include "boost/uuid/uuid_io.hpp"
|
||||
|
||||
@@ -14,4 +15,20 @@ std::string RandomUuid() {
|
||||
return ss.str();
|
||||
}
|
||||
|
||||
bool Split2(const std::string& str, char token, std::string* part1,
|
||||
std::string* part2) {
|
||||
std::size_t pos = str.find(token);
|
||||
if (pos == std::string::npos) {
|
||||
return false;
|
||||
}
|
||||
|
||||
*part1 = str.substr(0, pos);
|
||||
*part2 = str.substr(pos + 1);
|
||||
|
||||
boost::trim(*part1);
|
||||
boost::trim(*part2);
|
||||
|
||||
return true;
|
||||
}
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
@@ -7,6 +7,10 @@ namespace webcc {
|
||||
|
||||
std::string RandomUuid();
|
||||
|
||||
// Split a string to two parts by the given token.
|
||||
bool Split2(const std::string& str, char token, std::string* part1,
|
||||
std::string* part2);
|
||||
|
||||
} // namespace webcc
|
||||
|
||||
#endif // WEBCC_UTILITY_H_
|
||||
|
||||
Reference in New Issue
Block a user