2018-05-14 16:40:03 +08:00
|
|
|
// Copyright 2010, Shuo Chen. All rights reserved.
|
|
|
|
// http://code.google.com/p/muduo/
|
|
|
|
//
|
|
|
|
// Use of this source code is governed by a BSD-style license
|
|
|
|
// that can be found in the License file.
|
|
|
|
|
|
|
|
// Author: Shuo Chen (chenshuo at chenshuo dot com)
|
|
|
|
//
|
|
|
|
// This is a public header file, it must only include public header files.
|
|
|
|
|
|
|
|
//taken from muduo and modified
|
2018-05-31 17:00:31 +08:00
|
|
|
|
|
|
|
/**
|
|
|
|
*
|
|
|
|
* @file
|
|
|
|
* @author An Tao
|
|
|
|
* @section LICENSE
|
|
|
|
*
|
|
|
|
* Copyright 2018, An Tao. All rights reserved.
|
|
|
|
* Use of this source code is governed by a MIT license
|
|
|
|
* that can be found in the License file.
|
|
|
|
*
|
|
|
|
* @section DESCRIPTION
|
|
|
|
*
|
|
|
|
*/
|
2018-05-14 16:40:03 +08:00
|
|
|
|
|
|
|
#pragma once
|
|
|
|
|
|
|
|
#include <drogon/HttpResponse.h>
|
|
|
|
#include <trantor/utils/MsgBuffer.h>
|
2018-06-21 17:44:19 +08:00
|
|
|
#include <trantor/net/InetAddress.h>
|
2018-09-11 16:49:22 +08:00
|
|
|
#include <trantor/utils/Date.h>
|
2018-09-11 17:31:10 +08:00
|
|
|
#include <drogon/utils/Utilities.h>
|
2018-05-14 16:40:03 +08:00
|
|
|
#include <map>
|
|
|
|
#include <string>
|
2018-10-13 21:49:37 +08:00
|
|
|
#include <memory>
|
2018-09-11 16:49:22 +08:00
|
|
|
|
2018-05-14 16:40:03 +08:00
|
|
|
using namespace trantor;
|
|
|
|
namespace drogon
|
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
class HttpResponseImpl : public HttpResponse
|
|
|
|
{
|
2018-11-15 22:00:23 +08:00
|
|
|
friend class HttpClientContext;
|
2018-10-13 21:49:37 +08:00
|
|
|
|
|
|
|
public:
|
|
|
|
explicit HttpResponseImpl()
|
|
|
|
: _statusCode(kUnknown),
|
|
|
|
_closeConnection(false),
|
|
|
|
_left_body_length(0),
|
2018-10-15 11:13:14 +08:00
|
|
|
_current_chunk_length(0),
|
|
|
|
_bodyPtr(new std::string())
|
2018-05-14 16:40:03 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
|
|
|
virtual HttpStatusCode statusCode() override
|
|
|
|
{
|
|
|
|
return _statusCode;
|
|
|
|
}
|
|
|
|
virtual void setStatusCode(HttpStatusCode code) override
|
|
|
|
{
|
|
|
|
_statusCode = code;
|
|
|
|
setStatusMessage(web_response_code_to_string(code));
|
|
|
|
}
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void setStatusCode(HttpStatusCode code, const std::string &status_message) override
|
|
|
|
{
|
|
|
|
_statusCode = code;
|
|
|
|
setStatusMessage(status_message);
|
|
|
|
}
|
2018-10-09 19:00:00 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void setVersion(const Version v) override
|
|
|
|
{
|
|
|
|
_v = v;
|
|
|
|
}
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void setCloseConnection(bool on) override
|
|
|
|
{
|
|
|
|
_closeConnection = on;
|
|
|
|
}
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual bool closeConnection() const override
|
|
|
|
{
|
|
|
|
return _closeConnection;
|
|
|
|
}
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void setContentTypeCode(uint8_t type) override
|
|
|
|
{
|
|
|
|
_contentType = type;
|
|
|
|
setContentType(web_content_type_to_string(type));
|
|
|
|
}
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void setContentTypeCodeAndCharacterSet(uint8_t type, const std::string charSet = "utf-8") override
|
|
|
|
{
|
|
|
|
_contentType = type;
|
|
|
|
setContentType(web_content_type_and_charset_to_string(type, charSet));
|
|
|
|
}
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual uint8_t getContentTypeCode() override
|
|
|
|
{
|
|
|
|
return _contentType;
|
|
|
|
}
|
|
|
|
// virtual uint8_t contentTypeCode() override
|
|
|
|
// {
|
|
|
|
// return _contentType;
|
|
|
|
// }
|
|
|
|
|
|
|
|
virtual std::string getHeader(const std::string &key) const override
|
|
|
|
{
|
|
|
|
auto field = key;
|
|
|
|
transform(field.begin(), field.end(), field.begin(), ::tolower);
|
|
|
|
auto iter = _headers.find(field);
|
|
|
|
if (iter == _headers.end())
|
2018-05-14 16:40:03 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
return "";
|
2018-05-14 16:40:03 +08:00
|
|
|
}
|
2018-10-13 21:49:37 +08:00
|
|
|
else
|
2018-05-14 16:40:03 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
return iter->second;
|
2018-05-14 16:40:03 +08:00
|
|
|
}
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
|
|
|
virtual void addHeader(const std::string &key, const std::string &value) override
|
|
|
|
{
|
|
|
|
_fullHeaderString.reset();
|
|
|
|
auto field = key;
|
|
|
|
transform(field.begin(), field.end(), field.begin(), ::tolower);
|
|
|
|
_headers[field] = value;
|
|
|
|
}
|
|
|
|
|
|
|
|
virtual void addHeader(const std::string &key, std::string &&value) override
|
|
|
|
{
|
|
|
|
_fullHeaderString.reset();
|
|
|
|
auto field = key;
|
|
|
|
transform(field.begin(), field.end(), field.begin(), ::tolower);
|
|
|
|
_headers[field] = std::move(value);
|
|
|
|
}
|
2018-09-06 14:45:29 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void addHeader(const char *start, const char *colon, const char *end) override
|
|
|
|
{
|
|
|
|
_fullHeaderString.reset();
|
|
|
|
std::string field(start, colon);
|
|
|
|
transform(field.begin(), field.end(), field.begin(), ::tolower);
|
|
|
|
++colon;
|
|
|
|
while (colon < end && isspace(*colon))
|
2018-09-06 14:45:29 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
++colon;
|
2018-09-06 14:45:29 +08:00
|
|
|
}
|
2018-10-13 21:49:37 +08:00
|
|
|
std::string value(colon, end);
|
|
|
|
while (!value.empty() && isspace(value[value.size() - 1]))
|
2018-09-03 10:56:11 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
value.resize(value.size() - 1);
|
2018-09-03 10:56:11 +08:00
|
|
|
}
|
2018-10-13 21:49:37 +08:00
|
|
|
_headers[field] = value;
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
//FIXME:reponse cookie should be "Set-Cookie:...."
|
|
|
|
if (field == "cookie")
|
2018-05-14 16:40:03 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
//LOG_INFO<<"cookies!!!:"<<value;
|
|
|
|
std::string::size_type pos;
|
|
|
|
while ((pos = value.find(";")) != std::string::npos)
|
2018-05-14 16:40:03 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
std::string coo = value.substr(0, pos);
|
|
|
|
auto epos = coo.find("=");
|
|
|
|
if (epos != std::string::npos)
|
|
|
|
{
|
|
|
|
std::string cookie_name = coo.substr(0, epos);
|
|
|
|
std::string::size_type cpos = 0;
|
|
|
|
while (cpos < cookie_name.length() && isspace(cookie_name[cpos]))
|
|
|
|
cpos++;
|
|
|
|
cookie_name = cookie_name.substr(cpos);
|
|
|
|
std::string cookie_value = coo.substr(epos + 1);
|
|
|
|
_cookies.insert(std::make_pair(cookie_name, Cookie(cookie_name, cookie_value)));
|
|
|
|
}
|
|
|
|
value = value.substr(pos + 1);
|
2018-05-14 16:40:03 +08:00
|
|
|
}
|
2018-10-13 21:49:37 +08:00
|
|
|
if (value.length() > 0)
|
2018-05-14 16:40:03 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
std::string &coo = value;
|
|
|
|
auto epos = coo.find("=");
|
|
|
|
if (epos != std::string::npos)
|
2018-05-14 16:40:03 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
std::string cookie_name = coo.substr(0, epos);
|
|
|
|
std::string::size_type cpos = 0;
|
|
|
|
while (cpos < cookie_name.length() && isspace(cookie_name[cpos]))
|
|
|
|
cpos++;
|
|
|
|
cookie_name = cookie_name.substr(cpos);
|
|
|
|
std::string cookie_value = coo.substr(epos + 1);
|
|
|
|
_cookies.insert(std::make_pair(cookie_name, Cookie(cookie_name, cookie_value)));
|
2018-05-14 16:40:03 +08:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void addCookie(const std::string &key, const std::string &value) override
|
|
|
|
{
|
|
|
|
_cookies.insert(std::make_pair(key, Cookie(key, value)));
|
|
|
|
}
|
2018-06-19 16:55:25 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void addCookie(const Cookie &cookie) override
|
|
|
|
{
|
|
|
|
_cookies.insert(std::make_pair(cookie.key(), cookie));
|
|
|
|
}
|
2018-10-11 11:20:40 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void removeCookie(const std::string &key) override
|
|
|
|
{
|
|
|
|
_cookies.erase(key);
|
|
|
|
}
|
2018-06-21 17:44:19 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void setBody(const std::string &body) override
|
|
|
|
{
|
2018-10-15 11:13:14 +08:00
|
|
|
_bodyPtr = std::make_shared<std::string>(body);
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
|
|
|
virtual void setBody(std::string &&body) override
|
|
|
|
{
|
2018-10-15 11:13:14 +08:00
|
|
|
_bodyPtr = std::make_shared<std::string>(std::move(body));
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void redirect(const std::string &url) override
|
|
|
|
{
|
|
|
|
_headers["Location"] = url;
|
|
|
|
}
|
|
|
|
void appendToBuffer(MsgBuffer *output) const;
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual void clear() override
|
|
|
|
{
|
|
|
|
_statusCode = kUnknown;
|
|
|
|
_v = kHttp11;
|
|
|
|
_statusMessage.clear();
|
|
|
|
_fullHeaderString.reset();
|
|
|
|
_headers.clear();
|
|
|
|
_cookies.clear();
|
2018-10-15 11:13:14 +08:00
|
|
|
_bodyPtr.reset(new std::string());
|
2018-10-13 21:49:37 +08:00
|
|
|
_left_body_length = 0;
|
|
|
|
_current_chunk_length = 0;
|
2018-11-15 19:42:25 +08:00
|
|
|
_jsonPtr.reset();
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
|
|
|
|
|
|
|
virtual void setExpiredTime(ssize_t expiredTime) override
|
|
|
|
{
|
|
|
|
_expriedTime = expiredTime;
|
|
|
|
}
|
2018-10-11 11:20:40 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual ssize_t expiredTime() const override { return _expriedTime; }
|
2018-10-11 11:20:40 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
// void setReceiveTime(trantor::Date t)
|
|
|
|
// {
|
|
|
|
// receiveTime_ = t;
|
|
|
|
// }
|
2018-05-14 16:40:03 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
virtual const std::string &getBody() const override
|
|
|
|
{
|
2018-10-15 11:13:14 +08:00
|
|
|
return *_bodyPtr;
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
|
|
|
virtual std::string &getBody() override
|
|
|
|
{
|
2018-10-15 11:13:14 +08:00
|
|
|
return *_bodyPtr;
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
|
|
|
void swap(HttpResponseImpl &that)
|
|
|
|
{
|
|
|
|
_headers.swap(that._headers);
|
|
|
|
_cookies.swap(that._cookies);
|
|
|
|
std::swap(_statusCode, that._statusCode);
|
|
|
|
std::swap(_v, that._v);
|
|
|
|
_statusMessage.swap(that._statusMessage);
|
|
|
|
std::swap(_closeConnection, that._closeConnection);
|
2018-10-15 11:13:14 +08:00
|
|
|
_bodyPtr.swap(that._bodyPtr);
|
2018-10-13 21:49:37 +08:00
|
|
|
std::swap(_left_body_length, that._left_body_length);
|
|
|
|
std::swap(_current_chunk_length, that._current_chunk_length);
|
|
|
|
std::swap(_contentType, that._contentType);
|
|
|
|
_jsonPtr.swap(that._jsonPtr);
|
|
|
|
_fullHeaderString.swap(that._fullHeaderString);
|
|
|
|
}
|
|
|
|
void parseJson() const
|
|
|
|
{
|
|
|
|
//parse json data in reponse
|
|
|
|
_jsonPtr = std::make_shared<Json::Value>();
|
|
|
|
Json::CharReaderBuilder builder;
|
|
|
|
builder["collectComments"] = false;
|
|
|
|
JSONCPP_STRING errs;
|
|
|
|
std::unique_ptr<Json::CharReader> reader(builder.newCharReader());
|
2018-10-15 11:13:14 +08:00
|
|
|
if (!reader->parse(_bodyPtr->data(), _bodyPtr->data() + _bodyPtr->size(), _jsonPtr.get(), &errs))
|
2018-05-14 16:40:03 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
LOG_ERROR << errs;
|
|
|
|
_jsonPtr.reset();
|
2018-05-14 16:40:03 +08:00
|
|
|
}
|
2018-10-13 21:49:37 +08:00
|
|
|
}
|
|
|
|
virtual const std::shared_ptr<Json::Value> getJsonObject() const override
|
|
|
|
{
|
|
|
|
if (!_jsonPtr)
|
2018-09-14 14:19:43 +08:00
|
|
|
{
|
2018-10-13 21:49:37 +08:00
|
|
|
parseJson();
|
2018-09-14 14:19:43 +08:00
|
|
|
}
|
2018-10-13 21:49:37 +08:00
|
|
|
return _jsonPtr;
|
|
|
|
}
|
|
|
|
const std::string &sendfileName() const
|
|
|
|
{
|
|
|
|
return _sendfileName;
|
|
|
|
}
|
|
|
|
void setSendfile(const std::string &filename)
|
|
|
|
{
|
|
|
|
_sendfileName = filename;
|
|
|
|
}
|
|
|
|
void makeHeaderString()
|
|
|
|
{
|
|
|
|
trantor::MsgBuffer buf;
|
|
|
|
makeHeaderString(&buf);
|
|
|
|
_fullHeaderString = std::make_shared<std::string>(buf.peek(), buf.readableBytes());
|
|
|
|
}
|
|
|
|
|
|
|
|
protected:
|
|
|
|
static std::string web_content_type_to_string(uint8_t contenttype);
|
|
|
|
static const std::string web_content_type_and_charset_to_string(uint8_t contenttype,
|
|
|
|
const std::string &charSet);
|
|
|
|
|
|
|
|
static std::string web_response_code_to_string(int code);
|
|
|
|
|
|
|
|
void makeHeaderString(MsgBuffer *output) const;
|
|
|
|
|
|
|
|
private:
|
|
|
|
std::map<std::string, std::string> _headers;
|
|
|
|
std::map<std::string, Cookie> _cookies;
|
|
|
|
HttpStatusCode _statusCode;
|
|
|
|
// FIXME: add http version
|
|
|
|
Version _v;
|
|
|
|
std::string _statusMessage;
|
|
|
|
bool _closeConnection;
|
2018-10-15 11:13:14 +08:00
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
size_t _left_body_length;
|
|
|
|
size_t _current_chunk_length;
|
2018-10-15 11:13:14 +08:00
|
|
|
std::shared_ptr<std::string> _bodyPtr;
|
|
|
|
|
2018-10-13 21:49:37 +08:00
|
|
|
uint8_t _contentType = CT_TEXT_HTML;
|
|
|
|
|
|
|
|
ssize_t _expriedTime = -1;
|
|
|
|
std::string _sendfileName;
|
|
|
|
mutable std::shared_ptr<Json::Value> _jsonPtr;
|
|
|
|
|
|
|
|
std::shared_ptr<std::string> _fullHeaderString;
|
|
|
|
//trantor::Date receiveTime_;
|
|
|
|
|
|
|
|
void setContentType(const std::string &contentType)
|
|
|
|
{
|
|
|
|
addHeader("Content-Type", contentType);
|
|
|
|
}
|
|
|
|
void setContentType(std::string &&contentType)
|
|
|
|
{
|
|
|
|
addHeader("Content-Type", std::move(contentType));
|
|
|
|
}
|
|
|
|
void setStatusMessage(const std::string &message)
|
|
|
|
{
|
|
|
|
_statusMessage = message;
|
|
|
|
}
|
|
|
|
void setStatusMessage(std::string &&message)
|
|
|
|
{
|
|
|
|
_statusMessage = std::move(message);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
typedef std::shared_ptr<HttpResponseImpl> HttpResponseImplPtr;
|
|
|
|
} // namespace drogon
|