Magic_Game/src/kiwano-network/HttpRequest.h

157 lines
4.4 KiB
C
Raw Normal View History

2019-04-11 14:40:54 +08:00
// Copyright (c) 2016-2018 Kiwano - Nomango
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in
// all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
// THE SOFTWARE.
#pragma once
2020-01-17 16:55:47 +08:00
#include <kiwano/core/Common.h>
2019-11-13 14:33:15 +08:00
#include <kiwano/core/ObjectBase.h>
#include <kiwano/core/SmartPtr.hpp>
2019-04-11 14:40:54 +08:00
namespace kiwano
{
namespace network
{
2019-09-30 10:59:04 +08:00
class HttpResponse;
KGE_DECLARE_SMART_PTR(HttpRequest);
2019-12-31 10:37:29 +08:00
/**
* \addtogroup Network
* @{
*/
/**
* \~chinese
* @brief HTTP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
*/
2019-04-11 14:40:54 +08:00
class KGE_API HttpRequest
2020-01-09 08:45:00 +08:00
: public virtual ObjectBase
{
public:
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
using ResponseCallback = Function<void(HttpRequest* /* request */, HttpResponse* /* response */)>;
2019-09-30 10:59:04 +08:00
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
enum class Type
{
2019-12-31 10:37:29 +08:00
Unknown, ///< δ֪
Get, ///< HTTP GET<45><54><EFBFBD><EFBFBD>
Post, ///< HTTP POST<53><54><EFBFBD><EFBFBD>
Put, ///< HTTP PUT<55><54><EFBFBD><EFBFBD>
Delete ///< HTTP DELETE<54><45><EFBFBD><EFBFBD>
};
2019-11-13 14:33:15 +08:00
HttpRequest();
2019-11-13 14:33:15 +08:00
HttpRequest(Type type);
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
2019-11-13 14:33:15 +08:00
void SetUrl(String const& url);
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
void SetType(Type type);
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Я<EFBFBD><D0AF><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
void SetData(String const& data);
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Я<EFBFBD><D0AF><EFBFBD><EFBFBD>JSON<4F><4E><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
void SetJsonData(Json const& json);
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>HTTPͷ
2019-11-13 14:33:15 +08:00
void SetHeaders(Map<String, String> const& headers);
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD>HTTPͷ
2019-11-13 14:33:15 +08:00
void SetHeader(String const& field, String const& content);
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
void SetResponseCallback(ResponseCallback const& callback);
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
String const& GetUrl() const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
Type GetType() const;
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
String const& GetData() const;
/// \~chinese
/// @brief <20><>ȡHTTPͷ
2019-11-13 14:33:15 +08:00
Map<String, String>& GetHeaders();
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><>ȡHTTPͷ
2019-11-13 14:33:15 +08:00
String const& GetHeader(String const& header) const;
2019-12-31 10:37:29 +08:00
/// \~chinese
/// @brief <20><>ȡ<EFBFBD><C8A1>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
2019-11-13 14:33:15 +08:00
ResponseCallback const& GetResponseCallback() const;
2019-12-23 18:05:08 +08:00
private:
2019-11-13 14:33:15 +08:00
Type type_;
String url_;
String data_;
Map<String, String> headers_;
ResponseCallback response_cb_;
};
2019-12-31 10:37:29 +08:00
/** @} */
2019-11-13 14:33:15 +08:00
inline HttpRequest::HttpRequest() : type_(Type::Unknown) {}
2019-11-13 14:33:15 +08:00
inline HttpRequest::HttpRequest(Type type) : type_(type) {}
2019-11-13 14:33:15 +08:00
inline void HttpRequest::SetUrl(String const& url) { url_ = url; }
2019-11-13 14:33:15 +08:00
inline String const& HttpRequest::GetUrl() const { return url_; }
2019-11-13 14:33:15 +08:00
inline void HttpRequest::SetType(Type type) { type_ = type; }
2019-11-13 14:33:15 +08:00
inline HttpRequest::Type HttpRequest::GetType() const { return type_; }
2019-11-13 14:33:15 +08:00
inline void HttpRequest::SetData(String const& data) { data_ = data; }
2019-11-13 14:33:15 +08:00
inline String const& HttpRequest::GetData() const { return data_; }
inline void HttpRequest::SetHeaders(Map<String, String> const& headers) { headers_ = headers; }
inline void HttpRequest::SetHeader(String const& field, String const& content) { headers_[field] = content; }
inline Map<String, String>& HttpRequest::GetHeaders() { return headers_; }
inline String const& HttpRequest::GetHeader(String const& header) const { return headers_.at(header); }
inline void HttpRequest::SetResponseCallback(ResponseCallback const& callback) { response_cb_ = callback; }
inline HttpRequest::ResponseCallback const& HttpRequest::GetResponseCallback() const { return response_cb_; }
}
}