-
Notifications
You must be signed in to change notification settings - Fork 31
Expand file tree
/
Copy pathHttpUtil.cpp
More file actions
127 lines (107 loc) · 4.28 KB
/
Copy pathHttpUtil.cpp
File metadata and controls
127 lines (107 loc) · 4.28 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
/*
* Copyright 2017 Sony Corporation
*/
#include <sstream>
#include "Poco/DateTime.h"
#include "Poco/DateTimeFormat.h"
#include "Poco/DateTimeParser.h"
#include "Poco/Dynamic/Var.h"
#include "Poco/JSON/Object.h"
#include "Poco/JSON/Parser.h"
#include "Poco/Net/HTTPRequest.h"
#include "easyhttpcpp/common/CoreLogger.h"
#include "easyhttpcpp/common/StringUtil.h"
#include "easyhttpcpp/HttpException.h"
#include "easyhttpcpp/messagedigest/DigestUtil.h"
#include "HttpInternalConstants.h"
#include "HttpUtil.h"
using easyhttpcpp::common::StringUtil;
using easyhttpcpp::messagedigest::DigestUtil;
namespace easyhttpcpp {
static const std::string Tag = "HttpUtil";
const std::string& HttpUtil::httpMethodToString(Request::HttpMethod httpMethod)
{
switch (httpMethod) {
case Request::HttpMethodDelete:
return Poco::Net::HTTPRequest::HTTP_DELETE;
case Request::HttpMethodGet:
return Poco::Net::HTTPRequest::HTTP_GET;
case Request::HttpMethodHead:
return Poco::Net::HTTPRequest::HTTP_HEAD;
case Request::HttpMethodPatch:
return Poco::Net::HTTPRequest::HTTP_PATCH;
case Request::HttpMethodPost:
return Poco::Net::HTTPRequest::HTTP_POST;
case Request::HttpMethodPut:
return Poco::Net::HTTPRequest::HTTP_PUT;
default:
throw HttpIllegalArgumentException(StringUtil::format("Unknown http method: [%d]", httpMethod));
}
}
bool HttpUtil::tryParseDate(const std::string& value, Poco::Timestamp& timeStamp)
{
// correspond 3 format of RFC2616 3.3.1
// Sun, 06 Nov 1994 08:49:37 GMT ; RFC 822, updated by RFC 1123 -> Poco::DateTimeFormat::HTTP_FORMAT
// Sunday, 06-Nov-94 08:49:37 GMT ; RFC 850, obsoleted by RFC 1036 -> Poco::DateTimeFormat::HTTP_FORMAT
// Sun Nov 6 08:49:37 1994 ; ANSI C's asctime() format -> Poco::DateTimeFormat::ASCTIME_FORMAT
Poco::DateTime dateTime;
int timeZone;
if (!Poco::DateTimeParser::tryParse(Poco::DateTimeFormat::HTTP_FORMAT, value, dateTime, timeZone)) {
if (!Poco::DateTimeParser::tryParse(Poco::DateTimeFormat::ASCTIME_FORMAT, value, dateTime, timeZone)) {
EASYHTTPCPP_LOG_D(Tag, "can not parse header date/time [%s]", value.c_str());
return false;
}
}
timeStamp = dateTime.timestamp();
return true;
}
std::string HttpUtil::makeCacheKey(Request::Ptr pRequest)
{
return makeCacheKey(pRequest->getMethod(), pRequest->getUrl());
}
std::string HttpUtil::makeCacheKey(Request::HttpMethod httpMethod, const std::string& url)
{
// method + url -> digest
std::string key = DigestUtil::createHashedFileName(httpMethodToString(httpMethod) + url);
if (key.empty()) {
EASYHTTPCPP_LOG_D(Tag, "makeCacheKey failed.(empty string)");
}
return key;
}
std::string HttpUtil::makeCachedResponseBodyFilename(const Poco::Path& cacheRootDir, const std::string& key)
{
Poco::Path filename(cacheRootDir, key + HttpInternalConstants::Caches::DataFileExtention);
return filename.toString();
}
Headers::Ptr HttpUtil::exchangeJsonStrToHeaders(const std::string& headerJsonStr)
{
try {
Poco::JSON::Parser parser;
Poco::Dynamic::Var result = parser.parse(headerJsonStr);
Poco::JSON::Object::Ptr headerJson = result.extract<Poco::JSON::Object::Ptr>();
Headers::Ptr pHeaders = new Headers();
for (Poco::JSON::Object::ConstIterator it = headerJson->begin(); it != headerJson->end(); it++) {
pHeaders->add(it->first, it->second);
}
return pHeaders;
} catch (const Poco::Exception& e) {
EASYHTTPCPP_LOG_D(Tag, "exchangeJsonStrToHeaders failed. error:[%s]", e.message().c_str());
return NULL;
}
}
std::string HttpUtil::exchangeHeadersToJsonStr(Headers::Ptr pHeaders)
{
try {
Poco::JSON::Object headerJson;
for (Headers::HeaderMap::ConstIterator it = pHeaders->begin(); it != pHeaders->end(); it++) {
headerJson.set(it->first, it->second);
}
std::stringstream strStream;
headerJson.stringify(strStream);
return strStream.str();
} catch (const Poco::Exception& e) {
EASYHTTPCPP_LOG_D(Tag, "exchangeHeadersToJsonStr failed. error:[%s]", e.message().c_str());
return "";
}
}
} /* namespace easyhttpcpp */