Closure sent to main thread to request a reply to be sent to * a HTTP request. * Replies must be sent in the main loop in the main http thread, * this cannot be done from worker threads. */
| 566 | * this cannot be done from worker threads. |
| 567 | */ |
| 568 | void HTTPRequest::WriteReply(int nStatus, const std::string& strReply) |
| 569 | { |
| 570 | assert(!replySent && req); |
| 571 | if (ShutdownRequested()) { |
| 572 | WriteHeader("Connection", "close"); |
| 573 | } |
| 574 | // Send event to main http thread to send reply message |
| 575 | struct evbuffer* evb = evhttp_request_get_output_buffer(req); |
| 576 | assert(evb); |
| 577 | evbuffer_add(evb, strReply.data(), strReply.size()); |
| 578 | auto req_copy = req; |
| 579 | HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus]{ |
| 580 | evhttp_send_reply(req_copy, nStatus, nullptr, nullptr); |
| 581 | // Re-enable reading from the socket. This is the second part of the libevent |
| 582 | // workaround above. |
| 583 | if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02020001) { |
| 584 | evhttp_connection* conn = evhttp_request_get_connection(req_copy); |
| 585 | if (conn) { |
| 586 | bufferevent* bev = evhttp_connection_get_bufferevent(conn); |
| 587 | if (bev) { |
| 588 | bufferevent_enable(bev, EV_READ | EV_WRITE); |
| 589 | } |
| 590 | } |
| 591 | } |
| 592 | }); |
| 593 | ev->trigger(nullptr); |
| 594 | replySent = true; |
| 595 | req = nullptr; // transferred back to main thread |
| 596 | } |
| 597 | |
| 598 | CService HTTPRequest::GetPeer() const |
| 599 | { |
no test coverage detected