载入中...
搜索中...
未找到
HttpRequest.cpp
浏览该文件的文档.
2#include "network/Network.h"
3#include "network/NetWorker.h"
4#include "data/ByteData.h"
5
6#include <Poco/URI.h>
7#include <Poco/Timespan.h>
8#include <Poco/Net/HTTPClientSession.h>
9#include <Poco/Net/HTTPRequest.h>
10#include <Poco/Net/HTTPResponse.h>
11#include <Poco/StreamCopier.h>
12#include <Poco/Exception.h>
13
14#include <sstream>
15#include <thread>
16
17namespace eve::network {
18
19HttpRequest::HttpRequest(Network* net, std::string method, std::string url)
20 : net_(net), method_(std::move(method)), url_(std::move(url)) {}
21
22void HttpRequest::setHeader(std::string k, std::string v) {
23 headers_[std::move(k)] = std::move(v);
24}
25
27 if (!data) {
28 body_.reset();
29 return;
30 }
31 body_ = std::make_shared<std::vector<char>>(
32 static_cast<char*>(data->getData()),
33 static_cast<char*>(data->getData()) + data->getSize());
34}
35
36void HttpRequest::setBodyString(std::string s) {
37 body_ = std::make_shared<std::vector<char>>(s.begin(), s.end());
38}
39
41 timeoutMs_ = ms;
42}
43
44void HttpRequest::setVerifySsl(bool verify) {
45 verifySsl_ = verify ? 1 : 0;
46}
47
49 if (!net_) return false;
50 auto* self = this;
51 auto method = method_;
52 auto url = url_;
53 auto headers = headers_;
54 auto body = body_;
55 int timeoutMs = timeoutMs_ >= 0 ? timeoutMs_ : net_->getTimeout();
56 // -1 = inherit Network default; request-level setVerifySsl overrides.
57 const bool verifySsl = verifySsl_ < 0 ? net_->getVerifySsl() : (verifySsl_ != 0);
58
59 // Dedicated thread: HTTP would block NetWorker and prevent accept/poll.
60 std::thread([self, method, url, headers, body, timeoutMs, verifySsl]() {
63 c.handle = self;
64 try {
65 Poco::URI uri(url);
66 if (uri.getScheme() == "https") {
67 // TLS stack (Poco NetSSL) is not linked yet; honor verifySsl once HTTPS is enabled.
68 (void)verifySsl;
69 c.type = NetEvType::Err;
70 c.reason = "tls";
71 self->net_->post(std::move(c));
72 return;
73 }
74 Poco::Net::HTTPClientSession session(uri.getHost(), uri.getPort());
75 session.setTimeout(Poco::Timespan(timeoutMs / 1000, (timeoutMs % 1000) * 1000));
76 std::string path = uri.getPathAndQuery();
77 if (path.empty()) path = "/";
78 Poco::Net::HTTPRequest req(method, path, Poco::Net::HTTPMessage::HTTP_1_1);
79 req.setHost(uri.getHost());
80 for (auto& kv : headers) req.set(kv.first, kv.second);
81 if (body && !body->empty()) {
82 req.setContentLength(static_cast<std::streamsize>(body->size()));
83 }
84 std::ostream& os = session.sendRequest(req);
85 if (body && !body->empty()) {
86 os.write(body->data(), static_cast<std::streamsize>(body->size()));
87 }
88 Poco::Net::HTTPResponse resp;
89 std::istream& rs = session.receiveResponse(resp);
90 std::ostringstream oss;
91 Poco::StreamCopier::copyStream(rs, oss);
92 std::string content = oss.str();
94 c.status = static_cast<int>(resp.getStatus());
95 c.bytes = std::make_shared<std::vector<char>>(content.begin(), content.end());
96 self->net_->post(std::move(c));
97 } catch (const Poco::Exception& ex) {
98 c.type = NetEvType::Err;
99 std::string msg = ex.displayText();
100 if (msg.find("timed") != std::string::npos || msg.find("Timeout") != std::string::npos)
101 c.reason = "timeout";
102 else if (msg.find("host") != std::string::npos || msg.find("DNS") != std::string::npos)
103 c.reason = "dns";
104 else
105 c.reason = "refused";
106 self->net_->post(std::move(c));
107 } catch (...) {
108 c.type = NetEvType::Err;
109 c.reason = "refused";
110 self->net_->post(std::move(c));
111 }
112 }).detach();
113 return true;
114}
115
116} // namespace eve::network
JobFunc body
std::ostringstream & os
uint32_t c
Light2D::Data * data
int v
uint32_t s
Definition Weather.cpp:28
In-memory byte buffer implementing eve::Data (ref-counted).
Definition ByteData.h:11
void setBodyString(std::string s)
Sets the request body as a string.
void setTimeout(int ms)
Overrides the module default timeout for this request (ms).
HttpRequest(Network *net, std::string method, std::string url)
Creates a request; method is e.g. "GET"/"POST", url is absolute.
void setBody(eve::data::ByteData *data)
Sets the request body bytes.
void setHeader(std::string k, std::string v)
Sets one request header.
bool submit()
Queues the request on the worker; true when accepted.
void setVerifySsl(bool verify)
Overrides TLS verification for this request.
Network module: TCP/UDP/HTTP factories, background worker, and completion event plumbing....
Definition Network.h:30
bool getVerifySsl() const
Definition Network.cpp:163
int getTimeout() const
Definition Network.cpp:155
One asynchronous network result/event. handle points at the originating TcpSocket/UdpSocket/HttpReque...
Definition NetTypes.h:33