From 0e25dca075e6e60b0bb1b24061dc40ced22049df Mon Sep 17 00:00:00 2001 From: Jan Christoph Uhde Date: Mon, 25 Jul 2016 14:22:25 +0200 Subject: [PATCH] add: VppRequest --- lib/Rest/GeneralRequest.cpp | 23 +++++------ lib/Rest/GeneralRequest.h | 7 ++-- lib/Rest/VppRequest.cpp | 60 ++++++++++++++++++++++++++++ lib/Rest/VppRequest.h | 78 +++++++++++++++++++++++++++++++++++++ 4 files changed, 154 insertions(+), 14 deletions(-) create mode 100644 lib/Rest/VppRequest.cpp create mode 100644 lib/Rest/VppRequest.h diff --git a/lib/Rest/GeneralRequest.cpp b/lib/Rest/GeneralRequest.cpp index d693cf30ea..bba85e0991 100644 --- a/lib/Rest/GeneralRequest.cpp +++ b/lib/Rest/GeneralRequest.cpp @@ -34,7 +34,7 @@ using namespace arangodb::basics; std::string GeneralRequest::translateVersion(ProtocolVersion version) { switch (version) { - case ProtocolVersion::VSTREAM_1_0: + case ProtocolVersion::VPP_1_0: return "VPP/1.0"; case ProtocolVersion::HTTP_1_1: @@ -44,12 +44,10 @@ std::string GeneralRequest::translateVersion(ProtocolVersion version) { return "HTTP/1.0"; case ProtocolVersion::UNKNOWN: - default: { - return "HTTP/1.0"; - } + default: { return "HTTP/1.0"; } } - return "UNKNOWN"; // in order please MSVC + return "UNKNOWN"; // in order please MSVC } std::string GeneralRequest::translateMethod(RequestType method) { @@ -89,7 +87,7 @@ std::string GeneralRequest::translateMethod(RequestType method) { return "UNKNOWN"; } - return "UNKNOWN"; // in order please MSVC + return "UNKNOWN"; // in order please MSVC } GeneralRequest::RequestType GeneralRequest::translateMethod( @@ -126,8 +124,8 @@ void GeneralRequest::appendMethod(RequestType method, StringBuffer* buffer) { buffer->appendChar(' '); } -GeneralRequest::RequestType GeneralRequest::findRequestType(char const* ptr, - size_t const length) { +GeneralRequest::RequestType GeneralRequest::findRequestType( + char const* ptr, size_t const length) { switch (length) { case 3: if (ptr[0] == 'g' && ptr[1] == 'e' && ptr[2] == 't') { @@ -216,7 +214,8 @@ std::string const& GeneralRequest::header(std::string const& key) const { return it->second; } -std::string const& GeneralRequest::header(std::string const& key, bool& found) const { +std::string const& GeneralRequest::header(std::string const& key, + bool& found) const { auto it = _headers.find(key); if (it == _headers.end()) { @@ -240,7 +239,8 @@ std::string const& GeneralRequest::value(std::string const& key) const { return StaticStrings::Empty; } -std::string const& GeneralRequest::value(std::string const& key, bool& found) const { +std::string const& GeneralRequest::value(std::string const& key, + bool& found) const { if (!_values.empty()) { auto it = _values.find(key); @@ -254,7 +254,8 @@ std::string const& GeneralRequest::value(std::string const& key, bool& found) co return StaticStrings::Empty; } -void GeneralRequest::setArrayValue(char* key, size_t length, char const* value) { +void GeneralRequest::setArrayValue(char* key, size_t length, + char const* value) { _arrayValues[std::string(key, length)].emplace_back(value); } diff --git a/lib/Rest/GeneralRequest.h b/lib/Rest/GeneralRequest.h index 43031caa38..f1dbc94e1d 100644 --- a/lib/Rest/GeneralRequest.h +++ b/lib/Rest/GeneralRequest.h @@ -72,7 +72,7 @@ class GeneralRequest { ILLEGAL // must be last }; - enum class ProtocolVersion { HTTP_1_0, HTTP_1_1, VSTREAM_1_0, UNKNOWN }; + enum class ProtocolVersion { HTTP_1_0, HTTP_1_1, VPP_1_0, UNKNOWN }; enum class ContentType { UNSET, VPACK, JSON }; public: @@ -101,7 +101,7 @@ class GeneralRequest { _isRequestContextOwner(false), _type(RequestType::ILLEGAL), _contentType(ContentType::UNSET), - _contentTypeResponse(ContentType::UNSET){} + _contentTypeResponse(ContentType::UNSET) {} virtual ~GeneralRequest(); @@ -178,7 +178,8 @@ class GeneralRequest { bool velocyPackResponse() const; // should toVelocyPack be renamed to payload? - virtual VPackSlice payload(arangodb::velocypack::Options const* options = &VPackOptions::Defaults) = 0; + virtual VPackSlice payload(arangodb::velocypack::Options const* + options = &VPackOptions::Defaults) = 0; std::shared_ptr toVelocyPackBuilderPtr( arangodb::velocypack::Options const* options) { diff --git a/lib/Rest/VppRequest.cpp b/lib/Rest/VppRequest.cpp new file mode 100644 index 0000000000..66f0a278f2 --- /dev/null +++ b/lib/Rest/VppRequest.cpp @@ -0,0 +1,60 @@ +//////////////////////////////////////////////////////////////////////////////// +/// DISCLAIMER +/// +/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany +/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany +/// +/// Licensed under the Apache License, Version 2.0 (the "License"); +/// you may not use this file except in compliance with the License. +/// You may obtain a copy of the License at +/// +/// http://www.apache.org/licenses/LICENSE-2.0 +/// +/// Unless required by applicable law or agreed to in writing, software +/// distributed under the License is distributed on an "AS IS" BASIS, +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +/// See the License for the specific language governing permissions and +/// limitations under the License. +/// +/// Copyright holder is ArangoDB GmbH, Cologne, Germany +/// +/// @author Jan Christoph Uhde +//////////////////////////////////////////////////////////////////////////////// + +#include "VppRequest.h" + +#include +#include +#include +#include +#include + +#include "Basics/conversions.h" +#include "Basics/StaticStrings.h" +#include "Basics/StringUtils.h" +#include "Basics/tri-strings.h" +#include "Logger/Logger.h" + +using namespace arangodb; +using namespace arangodb::basics; + +VppRequest::VppRequest(ConnectionInfo const& connectionInfo, + VPackBuffer&& header, size_t length) + : GeneralRequest(connectionInfo), + _contentLength(0), + _header(std::move(header)), + _cookies(std::unordered_map() /*TODO remove*/) { + if (0 < length) { + _contentType = ContentType::VPACK; + _contentTypeResponse = ContentType::VPACK; + parseHeader(_header); + } +} + +// good +VPackSlice VppRequest::payload(VPackOptions const* options) { + VPackValidator validator; + validator.validate(_payload.data(), _payload.size()); + return VPackSlice(_payload().data()); +} +} diff --git a/lib/Rest/VppRequest.h b/lib/Rest/VppRequest.h new file mode 100644 index 0000000000..2c84f1ccef --- /dev/null +++ b/lib/Rest/VppRequest.h @@ -0,0 +1,78 @@ +//////////////////////////////////////////////////////////////////////////////// +/// DISCLAIMER +/// +/// Copyright 2014-2016 ArangoDB GmbH, Cologne, Germany +/// Copyright 2004-2014 triAGENS GmbH, Cologne, Germany +/// +/// Licensed under the Apache License, Version 2.0 (the "License"); +/// you may not use this file except in compliance with the License. +/// You may obtain a copy of the License at +/// +/// http://www.apache.org/licenses/LICENSE-2.0 +/// +/// Unless required by applicable law or agreed to in writing, software +/// distributed under the License is distributed on an "AS IS" BASIS, +/// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +/// See the License for the specific language governing permissions and +/// limitations under the License. +/// +/// Copyright holder is ArangoDB GmbH, Cologne, Germany +/// +/// @author Dr. Frank Celler +/// @author Achim Brandt +//////////////////////////////////////////////////////////////////////////////// + +#ifndef ARANGODB_REST_VPP_REQUEST_H +#define ARANGODB_REST_VPP_REQUEST_H 1 + +#include "Rest/VppRequest.h" +#include "Endpoint/ConnectionInfo.h" + +namespace arangodb { +class RestBatchHandler; + +namespace rest { +class GeneralCommTask; +class VppCommTask; +// class VppsCommTask; +} + +namespace velocypack { +class Builder; +struct Options; +} + +class VppRequest : public GeneralRequest { + friend class rest::VppCommTask; + // friend class rest::VppsCommTask; + friend class rest::GeneralCommTask; + friend class RestBatchHandler; // TODO must be removed + + private: + VppRequest(ConnectionInfo const&, char const*, size_t, bool); + + public: + ~VppRequest(); + + public: + VPackSlice payload(arangodb::velocypack::Options const*) override; + int64_t contentLength() const override { return _contentLength; } + + // TODO REMOVE + std::unordered_map cookieValues() const override { + return _cookies; + } + + void setPayload(VPackBuffer&& payload) { _payload = payload; } + + private: + void parseHeader(); // converts _header(vpack) to + // _headers(map) + + int64_t _contentLength; + VPackBuffer _header; + VPackBuffer _payload; + const std::unordered_map _cookies; // TODO remove +}; +} +#endif