2019-04-11 14:40:54 +08:00
|
|
|
|
// Copyright (c) 2016-2018 Kiwano - Nomango
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// 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:
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// The above copyright notice and this permission notice shall be included in
|
|
|
|
|
|
// all copies or substantial portions of the Software.
|
2020-01-21 10:09:55 +08:00
|
|
|
|
//
|
2019-03-31 01:37:06 +08:00
|
|
|
|
// 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>
|
2020-05-24 11:26:21 +08:00
|
|
|
|
#include <kiwano/base/ObjectBase.h>
|
2020-05-20 23:48:56 +08:00
|
|
|
|
#include <kiwano/utils/Json.h>
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2019-04-11 14:40:54 +08:00
|
|
|
|
namespace kiwano
|
2019-03-31 01:37:06 +08:00
|
|
|
|
{
|
2020-01-21 10:09:55 +08:00
|
|
|
|
namespace network
|
|
|
|
|
|
{
|
|
|
|
|
|
class HttpResponse;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
KGE_DECLARE_SMART_PTR(HttpRequest);
|
2019-03-31 23:09:49 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* \addtogroup Network
|
|
|
|
|
|
* @{
|
|
|
|
|
|
*/
|
2019-12-31 10:37:29 +08:00
|
|
|
|
|
2020-02-06 16:54:47 +08:00
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief HTTP<54><50><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-06 16:54:47 +08:00
|
|
|
|
enum class HttpType
|
|
|
|
|
|
{
|
2020-03-19 14:28:50 +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>
|
2020-02-06 16:54:47 +08:00
|
|
|
|
};
|
|
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
/**
|
|
|
|
|
|
* \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
* @brief HTTP<EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-01-21 10:09:55 +08:00
|
|
|
|
*/
|
2020-02-20 18:22:52 +08:00
|
|
|
|
class KGE_API HttpRequest : public ObjectBase
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
public:
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2020-01-21 10:09:55 +08:00
|
|
|
|
using ResponseCallback = Function<void(HttpRequest* /* request */, HttpResponse* /* response */)>;
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD>HTTP<54><50><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param url <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
|
/// @param type <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param callback <20><>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2020-07-22 21:08:48 +08:00
|
|
|
|
HttpRequest(const String& url, HttpType type, const ResponseCallback& callback);
|
2020-02-06 16:54:47 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD>HTTP<54><50><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param url <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
|
/// @param type <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param data <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param callback <20><>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2020-07-22 21:08:48 +08:00
|
|
|
|
HttpRequest(const String& url, HttpType type, const String& data, const ResponseCallback& callback);
|
2020-02-06 16:54:47 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD>HTTP<54><50><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param url <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
|
|
|
|
|
/// @param type <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param json <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>JSON<4F><4E><EFBFBD><EFBFBD>
|
|
|
|
|
|
/// @param callback <20><>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2020-07-22 21:08:48 +08:00
|
|
|
|
HttpRequest(const String& url, HttpType type, const Json& json, const ResponseCallback& callback);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
HttpRequest();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
2020-02-19 12:09:50 +08:00
|
|
|
|
void SetUrl(const String& url);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-06 16:54:47 +08:00
|
|
|
|
void SetType(HttpType type);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-19 12:09:50 +08:00
|
|
|
|
void SetData(const String& data);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>JSON<4F><4E><EFBFBD><EFBFBD>
|
2020-02-19 12:09:50 +08:00
|
|
|
|
void SetJsonData(const Json& json);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD>HTTPͷ
|
2020-02-19 12:09:50 +08:00
|
|
|
|
void SetHeaders(const Map<String, String>& headers);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD>HTTPͷ
|
2020-02-19 12:09:50 +08:00
|
|
|
|
void SetHeader(const String& field, const String& content);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><><EFBFBD><EFBFBD><EFBFBD><EFBFBD>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-19 12:09:50 +08:00
|
|
|
|
void SetResponseCallback(const ResponseCallback& callback);
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD>ַ
|
2020-02-19 12:09:50 +08:00
|
|
|
|
const String& GetUrl() const;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-06 16:54:47 +08:00
|
|
|
|
HttpType GetType() const;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-19 12:09:50 +08:00
|
|
|
|
const String& GetData() const;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ȡHTTPͷ
|
2020-01-21 10:09:55 +08:00
|
|
|
|
Map<String, String>& GetHeaders();
|
|
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ȡHTTPͷ
|
2020-02-19 12:09:50 +08:00
|
|
|
|
const String& GetHeader(const String& header) const;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
/// \~chinese
|
2020-03-19 14:28:50 +08:00
|
|
|
|
/// @brief <20><>ȡ<EFBFBD><C8A1>Ӧ<EFBFBD>ص<EFBFBD><D8B5><EFBFBD><EFBFBD><EFBFBD>
|
2020-02-19 12:09:50 +08:00
|
|
|
|
const ResponseCallback& GetResponseCallback() const;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
|
|
|
|
|
|
private:
|
2020-02-06 16:54:47 +08:00
|
|
|
|
HttpType type_;
|
2020-01-21 10:09:55 +08:00
|
|
|
|
String url_;
|
|
|
|
|
|
String data_;
|
|
|
|
|
|
Map<String, String> headers_;
|
|
|
|
|
|
ResponseCallback response_cb_;
|
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
|
|
/** @} */
|
|
|
|
|
|
|
|
|
|
|
|
inline HttpRequest::HttpRequest()
|
2020-02-06 16:54:47 +08:00
|
|
|
|
: type_(HttpType::Unknown)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline void HttpRequest::SetUrl(const String& url)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
url_ = url;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline const String& HttpRequest::GetUrl() const
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
return url_;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-02-06 16:54:47 +08:00
|
|
|
|
inline void HttpRequest::SetType(HttpType type)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
type_ = type;
|
|
|
|
|
|
}
|
2019-03-31 23:09:49 +08:00
|
|
|
|
|
2020-02-06 16:54:47 +08:00
|
|
|
|
inline HttpType HttpRequest::GetType() const
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
return type_;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline void HttpRequest::SetData(const String& data)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
data_ = data;
|
|
|
|
|
|
}
|
2019-03-31 01:37:06 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline const String& HttpRequest::GetData() const
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
return data_;
|
|
|
|
|
|
}
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline void HttpRequest::SetHeaders(const Map<String, String>& headers)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
headers_ = headers;
|
|
|
|
|
|
}
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline void HttpRequest::SetHeader(const String& field, const String& content)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
headers_[field] = content;
|
|
|
|
|
|
}
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
2020-01-21 10:09:55 +08:00
|
|
|
|
inline Map<String, String>& HttpRequest::GetHeaders()
|
|
|
|
|
|
{
|
|
|
|
|
|
return headers_;
|
|
|
|
|
|
}
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline const String& HttpRequest::GetHeader(const String& header) const
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
return headers_.at(header);
|
|
|
|
|
|
}
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline void HttpRequest::SetResponseCallback(const ResponseCallback& callback)
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
response_cb_ = callback;
|
|
|
|
|
|
}
|
2019-11-13 14:33:15 +08:00
|
|
|
|
|
2020-02-19 12:09:50 +08:00
|
|
|
|
inline const HttpRequest::ResponseCallback& HttpRequest::GetResponseCallback() const
|
2020-01-21 10:09:55 +08:00
|
|
|
|
{
|
|
|
|
|
|
return response_cb_;
|
2019-03-31 01:37:06 +08:00
|
|
|
|
}
|
2020-01-21 10:09:55 +08:00
|
|
|
|
} // namespace network
|
|
|
|
|
|
} // namespace kiwano
|