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. */
| 584 | * this cannot be done from worker threads. |
| 585 | */ |
| 586 | void HTTPRequest::WriteReply(int nStatus, const std::string& strReply) |
| 587 | { |
| 588 | assert(!replySent && req); |
| 589 | // Send event to main http thread to send reply message |
| 590 | struct evbuffer* evb = evhttp_request_get_output_buffer(req); |
| 591 | assert(evb); |
| 592 | evbuffer_add(evb, strReply.data(), strReply.size()); |
| 593 | auto req_copy = req; |
| 594 | HTTPEvent* ev = new HTTPEvent(eventBase, true, [req_copy, nStatus]{ |
| 595 | evhttp_send_reply(req_copy, nStatus, nullptr, nullptr); |
| 596 | // Re-enable reading from the socket. This is the second part of the libevent |
| 597 | // workaround above. |
| 598 | if (event_get_version_number() >= 0x02010600 && event_get_version_number() < 0x02020001) { |
| 599 | evhttp_connection* conn = evhttp_request_get_connection(req_copy); |
| 600 | if (conn) { |
| 601 | bufferevent* bev = evhttp_connection_get_bufferevent(conn); |
| 602 | if (bev) { |
| 603 | bufferevent_enable(bev, EV_READ | EV_WRITE); |
| 604 | } |
| 605 | } |
| 606 | } |
| 607 | }); |
| 608 | ev->trigger(nullptr); |
| 609 | replySent = true; |
| 610 | req = nullptr; // transferred back to main thread |
| 611 | } |
| 612 | |
| 613 | CService HTTPRequest::GetPeer() |
| 614 | { |
no test coverage detected