1
0
Fork 0

virtualize functions of request/response

This commit is contained in:
Jan Christoph Uhde 2016-07-14 08:40:05 +02:00
parent 2695b2bea9
commit 682cf7a2f1
14 changed files with 134 additions and 163 deletions

View File

@ -793,9 +793,7 @@ void ClusterComm::drop(ClientTransactionID const& clientTransactionID,
////////////////////////////////////////////////////////////////////////////////
void ClusterComm::asyncAnswer(std::string& coordinatorHeader,
GeneralResponse* responseToSendGeneral) {
// TODO needs to generalized
auto responseToSend = dynamic_cast<HttpResponse*>(responseToSendGeneral);
GeneralResponse* responseToSend) {
if (responseToSend == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
@ -881,10 +879,7 @@ void ClusterComm::asyncAnswer(std::string& coordinatorHeader,
////////////////////////////////////////////////////////////////////////////////
std::string ClusterComm::processAnswer(std::string& coordinatorHeader,
GeneralRequest* answerGeneral) {
// TODO needs to generalized
auto answer = dynamic_cast<HttpRequest*>(answerGeneral);
GeneralRequest* answer) {
if (answer == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}

View File

@ -32,6 +32,8 @@
#include "Basics/Thread.h"
#include "Cluster/AgencyComm.h"
#include "Cluster/ClusterInfo.h"
#include "Rest/GeneralRequest.h"
#include "Rest/GeneralResponse.h"
#include "Rest/HttpRequest.h"
#include "Rest/HttpResponse.h"
#include "SimpleHttpClient/SimpleHttpResult.h"
@ -189,7 +191,7 @@ struct ClusterCommResult {
std::shared_ptr<httpclient::SimpleHttpResult> result;
// the field answer is != nullptr if status is == CL_COMM_RECEIVED
// answer_code is valid iff answer is != 0
std::shared_ptr<HttpRequest> answer;
std::shared_ptr<GeneralRequest> answer;
GeneralResponse::ResponseCode answer_code;
// The following flag indicates whether or not the complete request was

View File

@ -405,10 +405,8 @@ std::unordered_map<std::string, std::string> getForwardableRequestHeaders(
++it;
}
auto httpRequest = dynamic_cast<HttpRequest*>(request);
if (httpRequest != nullptr) {
result["content-length"] = StringUtils::itoa(httpRequest->contentLength());
if (request != nullptr) {
result["content-length"] = StringUtils::itoa(request->contentLength());
}
return result;

View File

@ -46,17 +46,12 @@ RestBatchHandler::~RestBatchHandler() {}
////////////////////////////////////////////////////////////////////////////////
RestHandler::status RestBatchHandler::execute() {
// TODO needs to generalized
auto response = dynamic_cast<HttpResponse*>(_response);
if (response == nullptr) {
// TODO OBI - generalize function
if (_response == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
// TODO needs to generalized
auto httpRequest = dynamic_cast<HttpRequest*>(_request);
if (httpRequest == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
@ -86,15 +81,15 @@ RestHandler::status RestBatchHandler::execute() {
// get authorization header. we will inject this into the subparts
std::string const& authorization =
httpRequest->header(StaticStrings::Authorization);
_request->header(StaticStrings::Authorization);
// create the response
setResponseCode(GeneralResponse::ResponseCode::OK);
response->setContentType(
httpRequest->header(StaticStrings::ContentTypeHeader));
_response->setContentType(
_request->header(StaticStrings::ContentTypeHeader));
// setup some auxiliary structures to parse the multipart message
std::string const& bodyStr = httpRequest->body();
std::string const& bodyStr = _request->body();
MultipartMessage message(boundary.c_str(), boundary.size(), bodyStr.c_str(),
bodyStr.c_str() + bodyStr.size());
@ -148,7 +143,7 @@ RestHandler::status RestBatchHandler::execute() {
// set up request object for the part
LOG(TRACE) << "part header is: " << std::string(headerStart, headerLength);
HttpRequest* request = new HttpRequest(httpRequest->connectionInfo(),
HttpRequest* request = new HttpRequest(_request->connectionInfo(),
headerStart, headerLength, false);
// we do not have a client task id here
@ -204,8 +199,7 @@ RestHandler::status RestBatchHandler::execute() {
return status::FAILED;
}
HttpResponse* partResponse =
dynamic_cast<HttpResponse*>(handler->response());
GeneralResponse* partResponse = handler->response();
if (partResponse == nullptr) {
generateError(GeneralResponse::ResponseCode::BAD, TRI_ERROR_INTERNAL,
@ -222,28 +216,28 @@ RestHandler::status RestBatchHandler::execute() {
}
// append the boundary for this subpart
response->body().appendText(boundary + "\r\nContent-Type: ");
response->body().appendText(StaticStrings::BatchContentType);
_response->body().appendText(boundary + "\r\nContent-Type: ");
_response->body().appendText(StaticStrings::BatchContentType);
// append content-id if it is present
if (helper.contentId != 0) {
response->body().appendText(
_response->body().appendText(
"\r\nContent-Id: " +
std::string(helper.contentId, helper.contentIdLength));
}
response->body().appendText(TRI_CHAR_LENGTH_PAIR("\r\n\r\n"));
_response->body().appendText(TRI_CHAR_LENGTH_PAIR("\r\n\r\n"));
// remove some headers we don't need
partResponse->setConnectionType(HttpResponse::CONNECTION_NONE);
partResponse->setHeaderNC(StaticStrings::Server, "");
// append the part response header
partResponse->writeHeader(&response->body());
partResponse->writeHeader(&_response->body());
// append the part response body
response->body().appendText(partResponse->body());
response->body().appendText(TRI_CHAR_LENGTH_PAIR("\r\n"));
_response->body().appendText(partResponse->body());
_response->body().appendText(TRI_CHAR_LENGTH_PAIR("\r\n"));
}
// we've read the last part
@ -253,10 +247,10 @@ RestHandler::status RestBatchHandler::execute() {
}
// append final boundary + "--"
response->body().appendText(boundary + "--");
_response->body().appendText(boundary + "--");
if (errors > 0) {
response->setHeaderNC(StaticStrings::Errors, StringUtils::itoa(errors));
_response->setHeaderNC(StaticStrings::Errors, StringUtils::itoa(errors));
}
// success
@ -268,14 +262,12 @@ RestHandler::status RestBatchHandler::execute() {
////////////////////////////////////////////////////////////////////////////////
bool RestBatchHandler::getBoundaryBody(std::string* result) {
// TODO needs to generalized
auto request = dynamic_cast<HttpRequest*>(_request);
if (request == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
std::string const& bodyStr = request->body();
std::string const& bodyStr = _request->body();
char const* p = bodyStr.c_str();
char const* e = p + bodyStr.size();

View File

@ -253,10 +253,8 @@ int RestImportHandler::handleSingleDocument(
////////////////////////////////////////////////////////////////////////////////
bool RestImportHandler::createFromJson(std::string const& type) {
// TODO needs to generalized
auto request = dynamic_cast<HttpRequest*>(_request);
if (request == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
@ -300,15 +298,12 @@ bool RestImportHandler::createFromJson(std::string const& type) {
} else if (type == "auto") {
linewise = true;
// TODO generalize
auto* httpResponse = dynamic_cast<HttpResponse*>(_response);
if (httpResponse == nullptr) {
if (_response == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
// auto detect import type by peeking at first non-whitespace character
std::string const& body = request->body();
std::string const& body = _request->body();
char const* ptr = body.c_str();
char const* end = ptr + body.size();
@ -361,7 +356,7 @@ bool RestImportHandler::createFromJson(std::string const& type) {
if (linewise) {
// each line is a separate JSON document
std::string const& body = request->body();
std::string const& body = _request->body();
char const* ptr = body.c_str();
char const* end = ptr + body.size();
size_t i = 0;
@ -442,7 +437,7 @@ bool RestImportHandler::createFromJson(std::string const& type) {
// the entire request body is one JSON document
std::shared_ptr<VPackBuilder> parsedDocuments;
try {
parsedDocuments = VPackParser::fromJson(request->body());
parsedDocuments = VPackParser::fromJson(_request->body());
} catch (VPackException const&) {
generateError(GeneralResponse::ResponseCode::BAD,
TRI_ERROR_HTTP_BAD_PARAMETER,
@ -500,10 +495,7 @@ bool RestImportHandler::createFromJson(std::string const& type) {
////////////////////////////////////////////////////////////////////////////////
bool RestImportHandler::createFromKeyValueList() {
// TODO needs to generalized
auto* request = dynamic_cast<HttpRequest*>(_request);
if (request == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
@ -544,7 +536,7 @@ bool RestImportHandler::createFromKeyValueList() {
lineNumber = StringUtils::int64(lineNumValue);
}
std::string const& bodyStr = request->body();
std::string const& bodyStr = _request->body();
char const* current = bodyStr.c_str();
char const* bodyEnd = current + bodyStr.size();

View File

@ -778,10 +778,8 @@ void RestReplicationHandler::handleCommandBarrier() {
////////////////////////////////////////////////////////////////////////////////
void RestReplicationHandler::handleTrampolineCoordinator() {
// TODO needs to generalized
auto request = dynamic_cast<HttpRequest*>(_request);
if (request == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
@ -821,7 +819,7 @@ void RestReplicationHandler::handleTrampolineCoordinator() {
_request->requestType(),
"/_db/" + StringUtils::urlEncode(dbname) +
_request->requestPath() + params,
request->body(), *headers, 300.0);
_request->body(), *headers, 300.0);
if (res->status == CL_COMM_TIMEOUT) {
// No reply, we give up:
@ -848,16 +846,13 @@ void RestReplicationHandler::handleTrampolineCoordinator() {
setResponseCode(static_cast<GeneralResponse::ResponseCode>(
res->result->getHttpReturnCode()));
// TODO needs to generalized
auto response = dynamic_cast<HttpResponse*>(_response);
if (response == nullptr) {
if (_response == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
response->setContentType(
_response->setContentType(
res->result->getHeaderField(StaticStrings::ContentTypeHeader, dummy));
response->body().swap(&(res->result->getBody()));
_response->body().swap(&(res->result->getBody()));
auto const& resultHeaders = res->result->getHeaderFields();
for (auto const& it : resultHeaders) {
@ -1003,14 +998,11 @@ void RestReplicationHandler::handleCommandLoggerFollow() {
setResponseCode(GeneralResponse::ResponseCode::OK);
}
// TODO needs to generalized
auto response = dynamic_cast<HttpResponse*>(_response);
if (response == nullptr) {
if (_response == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
response->setContentType(GeneralResponse::ContentType::DUMP);
_response->setContentType(GeneralResponse::ContentType::DUMP);
// set headers
_response->setHeaderNC(TRI_REPLICATION_HEADER_CHECKMORE,
@ -1029,7 +1021,7 @@ void RestReplicationHandler::handleCommandLoggerFollow() {
if (length > 0) {
// transfer ownership of the buffer contents
response->body().set(dump._buffer);
_response->body().set(dump._buffer);
// to avoid double freeing
TRI_StealStringBuffer(dump._buffer);
@ -1106,14 +1098,11 @@ void RestReplicationHandler::handleCommandDetermineOpenTransactions() {
setResponseCode(GeneralResponse::ResponseCode::OK);
}
// TODO needs to generalized
auto response = dynamic_cast<HttpResponse*>(_response);
if (response == nullptr) {
if (_response == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
response->setContentType(HttpResponse::ContentType::DUMP);
_response->setContentType(HttpResponse::ContentType::DUMP);
_response->setHeaderNC(TRI_REPLICATION_HEADER_FROMPRESENT,
dump._fromTickIncluded ? "true" : "false");
@ -1123,7 +1112,7 @@ void RestReplicationHandler::handleCommandDetermineOpenTransactions() {
if (length > 0) {
// transfer ownership of the buffer contents
response->body().set(dump._buffer);
_response->body().set(dump._buffer);
// to avoid double freeing
TRI_StealStringBuffer(dump._buffer);
@ -2186,14 +2175,12 @@ int RestReplicationHandler::processRestoreDataBatch(
VPackBuilder builder;
// TODO needs to generalized
auto request = dynamic_cast<HttpRequest*>(_request);
if (request == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
std::string const& bodyStr = request->body();
std::string const& bodyStr = _request->body();
char const* ptr = bodyStr.c_str();
char const* end = ptr + bodyStr.size();
@ -2528,14 +2515,11 @@ void RestReplicationHandler::handleCommandRestoreDataCoordinator() {
std::string("received invalid JSON data for collection ") + name;
VPackBuilder builder;
// TODO needs to generalized
auto request = dynamic_cast<HttpRequest*>(_request);
if (request == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
std::string const& bodyStr = request->body();
std::string const& bodyStr = _request->body();
char const* ptr = bodyStr.c_str();
char const* end = ptr + bodyStr.size();

View File

@ -42,10 +42,8 @@ RestUploadHandler::RestUploadHandler(GeneralRequest* request,
RestUploadHandler::~RestUploadHandler() {}
RestHandler::status RestUploadHandler::execute() {
// TODO needs to generalized
auto request = dynamic_cast<HttpRequest*>(_request);
if (request == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
@ -73,7 +71,7 @@ RestHandler::status RestUploadHandler::execute() {
char* relative = TRI_GetFilename(filename);
std::string const& bodyStr = request->body();
std::string const& bodyStr = _request->body();
char const* body = bodyStr.c_str();
size_t bodySize = bodyStr.size();
@ -135,14 +133,12 @@ RestHandler::status RestUploadHandler::execute() {
////////////////////////////////////////////////////////////////////////////////
bool RestUploadHandler::parseMultiPart(char const*& body, size_t& length) {
// TODO needs to generalized
auto request = dynamic_cast<HttpRequest*>(_request);
if (request == nullptr) {
if (_request == nullptr) {
THROW_ARANGO_EXCEPTION(TRI_ERROR_INTERNAL);
}
std::string const& bodyStr = request->body();
std::string const& bodyStr = _request->body();
char const* beg = bodyStr.c_str();
char const* end = beg + bodyStr.size();

View File

@ -655,12 +655,8 @@ std::shared_ptr<VPackBuilder> RestVocbaseBaseHandler::parseVelocyPackBody(
contentType == StaticStrings::MimeTypeVPack) {
VPackValidator validator;
//FIXME broken casts!!
validator.validate(static_cast<HttpRequest*>(_request)->body().c_str()
,static_cast<HttpRequest*>(_request)->body().length()
);
VPackSlice slice{ static_cast<HttpRequest*>(_request)->body().c_str()};
validator.validate(_request->body().c_str() ,_request->body().length());
VPackSlice slice{_request->body().c_str()};
auto builder = std::make_shared<VPackBuilder>(options);
builder->add(slice);
return builder;

View File

@ -290,11 +290,11 @@ static void AddCookie(v8::Isolate* isolate, TRI_v8_global_t const* v8g,
static v8::Handle<v8::Object> RequestCppToV8(v8::Isolate* isolate,
TRI_v8_global_t const* v8g,
GeneralRequest* generalRequest) {
GeneralRequest* request) {
// setup the request
v8::Handle<v8::Object> req = v8::Object::New(isolate);
auto request = dynamic_cast<HttpRequest*>(generalRequest);
//auto request = dynamic_cast<HttpRequest*>(generalRequest);
// TODO generalize
if (request == nullptr) {

View File

@ -173,6 +173,11 @@ class GeneralRequest {
virtual std::shared_ptr<arangodb::velocypack::Builder> toVelocyPack(
arangodb::velocypack::Options const*) = 0;
virtual std::string const& body() const = 0;
virtual int64_t contentLength() const = 0;
virtual std::unordered_map<std::string, std::string> cookieValues() const = 0;
protected:
void setValue(char const* key, char const* value);
void setArrayValue(char* key, size_t length, char const* value);

View File

@ -28,6 +28,7 @@
#include "Basics/StaticStrings.h"
#include "Basics/StringUtils.h"
#include "Basics/StringBuffer.h"
namespace arangodb {
namespace velocypack {
@ -106,6 +107,13 @@ class GeneralResponse {
DUMP // application/x-arango-dump
};
enum ConnectionType {
CONNECTION_NONE,
CONNECTION_KEEP_ALIVE,
CONNECTION_CLOSE
};
public:
// converts the response code to a string for delivering to a http client.
static std::string responseString(ResponseCode);
@ -116,6 +124,15 @@ class GeneralResponse {
// response code from integer error code
static ResponseCode responseCode(int);
// TODO OBI - check what can be implemented in this base class
virtual basics::StringBuffer& body() = 0;
virtual void setContentType(ContentType type) = 0;
virtual void setContentType(std::string const& contentType) = 0;
virtual void setContentType(std::string&& contentType) = 0;
virtual void setConnectionType(ConnectionType type) = 0;
virtual void writeHeader(basics::StringBuffer*) = 0;
protected:
explicit GeneralResponse(ResponseCode);

View File

@ -60,15 +60,15 @@ class HttpRequest : public GeneralRequest {
public:
// the content length
int64_t contentLength() const { return _contentLength; }
int64_t contentLength() const override { return _contentLength; }
std::string const& cookieValue(std::string const& key) const;
std::string const& cookieValue(std::string const& key, bool& found) const;
std::unordered_map<std::string, std::string> cookieValues() const {
std::unordered_map<std::string, std::string> cookieValues() const override {
return _cookies;
}
std::string const& body() const;
std::string const& body() const override;
void setBody(char const* body, size_t length);
// the request body as VelocyPackBuilder

View File

@ -49,12 +49,6 @@ class HttpResponse : public GeneralResponse {
public:
bool isHeadResponse() const { return _isHeadResponse; }
enum ConnectionType {
CONNECTION_NONE,
CONNECTION_KEEP_ALIVE,
CONNECTION_CLOSE
};
public:
void setCookie(std::string const& name, std::string const& value,
int lifeTimeSeconds, std::string const& path,
@ -69,30 +63,30 @@ class HttpResponse : public GeneralResponse {
// information to the string buffer. Note that adding data to the body
// invalidates any previously returned header. You must call header
// again.
basics::StringBuffer& body() { return _body; }
basics::StringBuffer& body() override { return _body; }
size_t bodySize() const;
/// @brief set type of connection
void setConnectionType(ConnectionType type) { _connectionType = type; }
void setConnectionType(ConnectionType type) override { _connectionType = type; }
/// @brief set content-type
void setContentType(ContentType type) { _contentType = type; }
void setContentType(ContentType type) override { _contentType = type; }
/// @brief set content-type from a string. this should only be used in
/// cases when the content-type is user-defined
void setContentType(std::string const& contentType) {
void setContentType(std::string const& contentType) override {
_headers[arangodb::StaticStrings::ContentTypeHeader] = contentType;
_contentType = ContentType::CUSTOM;
}
void setContentType(std::string&& contentType) {
void setContentType(std::string&& contentType) override {
_headers[arangodb::StaticStrings::ContentTypeHeader] =
std::move(contentType);
_contentType = ContentType::CUSTOM;
}
// you should call writeHeader only after the body has been created
void writeHeader(basics::StringBuffer*);
void writeHeader(basics::StringBuffer*) override;
public:
void reset(ResponseCode code) override final;