| 118 | } |
| 119 | |
| 120 | void TEvhttpServer::complete(RequestContext* ctx, bool success) { |
| 121 | (void)success; |
| 122 | std::unique_ptr<RequestContext> ptr(ctx); |
| 123 | |
| 124 | int code = success ? 200 : 400; |
| 125 | const char* reason = success ? "OK" : "Bad Request"; |
| 126 | |
| 127 | int rv = evhttp_add_header(ctx->req->output_headers, "Content-Type", "application/x-thrift"); |
| 128 | if (rv != 0) { |
| 129 | // TODO: Log an error. |
| 130 | std::cerr << "evhttp_add_header failed " << __FILE__ << ":" << __LINE__ << '\n'; |
| 131 | } |
| 132 | |
| 133 | struct evbuffer* buf = evbuffer_new(); |
| 134 | if (buf == nullptr) { |
| 135 | // TODO: Log an error. |
| 136 | std::cerr << "evbuffer_new failed " << __FILE__ << ":" << __LINE__ << '\n'; |
| 137 | } else { |
| 138 | uint8_t* obuf; |
| 139 | uint32_t sz; |
| 140 | ctx->obuf->getBuffer(&obuf, &sz); |
| 141 | int ret = evbuffer_add(buf, obuf, sz); |
| 142 | if (ret != 0) { |
| 143 | // TODO: Log an error. |
| 144 | std::cerr << "evhttp_add failed with " << ret << " " << __FILE__ << ":" << __LINE__ << '\n'; |
| 145 | } |
| 146 | } |
| 147 | |
| 148 | evhttp_send_reply(ctx->req, code, reason, buf); |
| 149 | if (buf != nullptr) { |
| 150 | evbuffer_free(buf); |
| 151 | } |
| 152 | } |
| 153 | |
| 154 | struct event_base* TEvhttpServer::getEventBase() { |
| 155 | return eb_; |