1
0
Fork 0
arangodb/lib/Rest/GeneralRequest.cpp

215 lines
5.9 KiB
C++

////////////////////////////////////////////////////////////////////////////////
/// 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
////////////////////////////////////////////////////////////////////////////////
#include "GeneralRequest.h"
#include "Basics/StringUtils.h"
#include "Basics/Utf8Helper.h"
#include "Logger/Logger.h"
#include "Rest/RequestContext.h"
using namespace arangodb;
using namespace arangodb::basics;
std::string GeneralRequest::translateVersion(ProtocolVersion version) {
switch (version) {
case ProtocolVersion::VPP_1_0:
return "VPP/1.0";
case ProtocolVersion::HTTP_1_1:
return "HTTP/1.1";
case ProtocolVersion::HTTP_1_0:
return "HTTP/1.0";
case ProtocolVersion::UNKNOWN:
default: { return "HTTP/1.0"; }
}
return "UNKNOWN"; // in order please MSVC
}
std::string GeneralRequest::translateMethod(RequestType method) {
switch (method) {
case RequestType::DELETE_REQ:
return "DELETE";
case RequestType::GET:
return "GET";
case RequestType::HEAD:
return "HEAD";
case RequestType::OPTIONS:
return "OPTIONS";
case RequestType::PATCH:
return "PATCH";
case RequestType::POST:
return "POST";
case RequestType::PUT:
return "PUT";
case RequestType::VSTREAM_CRED:
return "CRED";
case RequestType::VSTREAM_REGISTER:
return "REGISTER";
case RequestType::VSTREAM_STATUS:
return "STATUS";
case RequestType::ILLEGAL:
LOG(WARN) << "illegal http request method encountered in switch";
return "UNKNOWN";
}
return "UNKNOWN"; // in order please MSVC
}
GeneralRequest::RequestType GeneralRequest::translateMethod(
std::string const& method) {
std::string const methodString = StringUtils::toupper(method);
if (methodString == "DELETE") {
return RequestType::DELETE_REQ;
} else if (methodString == "GET") {
return RequestType::GET;
} else if (methodString == "HEAD") {
return RequestType::HEAD;
} else if (methodString == "OPTIONS") {
return RequestType::OPTIONS;
} else if (methodString == "PATCH") {
return RequestType::PATCH;
} else if (methodString == "POST") {
return RequestType::POST;
} else if (methodString == "PUT") {
return RequestType::PUT;
} else if (methodString == "CRED") {
return RequestType::VSTREAM_CRED;
} else if (methodString == "REGISTER") {
return RequestType::VSTREAM_REGISTER;
} else if (methodString == "STATUS") {
return RequestType::VSTREAM_STATUS;
}
return RequestType::ILLEGAL;
}
void GeneralRequest::appendMethod(RequestType method, StringBuffer* buffer) {
// append RequestType as string value to given String buffer
buffer->appendText(translateMethod(method));
buffer->appendChar(' ');
}
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') {
return RequestType::GET;
}
if (ptr[0] == 'p' && ptr[1] == 'u' && ptr[2] == 't') {
return RequestType::PUT;
}
break;
case 4:
if (ptr[0] == 'p' && ptr[1] == 'o' && ptr[2] == 's' && ptr[3] == 't') {
return RequestType::POST;
}
if (ptr[0] == 'h' && ptr[1] == 'e' && ptr[2] == 'a' && ptr[3] == 'd') {
return RequestType::HEAD;
}
break;
case 5:
if (ptr[0] == 'p' && ptr[1] == 'a' && ptr[2] == 't' && ptr[3] == 'c' &&
ptr[4] == 'h') {
return RequestType::PATCH;
}
break;
case 6:
if (ptr[0] == 'd' && ptr[1] == 'e' && ptr[2] == 'l' && ptr[3] == 'e' &&
ptr[4] == 't' && ptr[5] == 'e') {
return RequestType::DELETE_REQ;
}
break;
case 7:
if (ptr[0] == 'o' && ptr[1] == 'p' && ptr[2] == 't' && ptr[3] == 'i' &&
ptr[4] == 'o' && ptr[5] == 'n' && ptr[6] == 's') {
return RequestType::OPTIONS;
}
break;
}
return RequestType::ILLEGAL;
}
GeneralRequest::~GeneralRequest() {
// only delete if we are the owner of the context
if (_requestContext != nullptr && _isRequestContextOwner) {
delete _requestContext;
}
}
void GeneralRequest::setRequestContext(RequestContext* requestContext,
bool isRequestContextOwner) {
if (_requestContext) {
// if we have a shared context, we should not have got here
TRI_ASSERT(isRequestContextOwner);
// delete any previous context
TRI_ASSERT(false);
delete _requestContext;
}
_requestContext = requestContext;
_isRequestContextOwner = isRequestContextOwner;
}
void GeneralRequest::setFullUrl(char const* begin, char const* end) {
TRI_ASSERT(begin != nullptr);
TRI_ASSERT(end != nullptr);
TRI_ASSERT(begin <= end);
_fullUrl = std::string(begin, end - begin);
}
void GeneralRequest::setFullUrl(std::string url) {
TRI_ASSERT(!url.empty());
_fullUrl = std::move(url);
}
void GeneralRequest::addSuffix(std::string&& part) {
_suffix.emplace_back(StringUtils::urlDecode(part));
}
bool GeneralRequest::velocyPackResponse() const {
// needs only to be used in http case?!
std::string const& result = header(StaticStrings::Accept);
return (std::string::npos != result.find(StaticStrings::MimeTypeVPack));
}