2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "shrpx_http_downstream_connection.h"
27 #include "shrpx_client_handler.h"
28 #include "shrpx_upstream.h"
29 #include "shrpx_downstream.h"
30 #include "shrpx_config.h"
31 #include "shrpx_error.h"
32 #include "shrpx_http.h"
33 #include "shrpx_log_config.h"
34 #include "shrpx_connect_blocker.h"
35 #include "shrpx_downstream_connection_pool.h"
36 #include "shrpx_worker.h"
37 #include "shrpx_http2_session.h"
38 #include "shrpx_tls.h"
39 #include "shrpx_log.h"
42 #include "ssl_compat.h"
44 using namespace nghttp2;
49 void timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
50 auto conn = static_cast<Connection *>(w->data);
51 auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
53 if (w == &conn->rt && !conn->expired_rt()) {
57 if (LOG_ENABLED(INFO)) {
58 DCLOG(INFO, dconn) << "Time out";
61 auto downstream = dconn->get_downstream();
62 auto upstream = downstream->get_upstream();
63 auto handler = upstream->get_client_handler();
64 auto &resp = downstream->response();
66 // Do this so that dconn is not pooled
67 resp.connection_close = true;
69 if (upstream->downstream_error(dconn, Downstream::EVENT_TIMEOUT) != 0) {
76 void retry_downstream_connection(Downstream *downstream,
77 unsigned int status_code) {
78 auto upstream = downstream->get_upstream();
79 auto handler = upstream->get_client_handler();
81 assert(!downstream->get_request_header_sent());
83 downstream->add_retry();
85 if (downstream->no_more_retry()) {
90 downstream->pop_downstream_connection();
91 auto buf = downstream->get_request_buf();
97 auto ndconn = handler->get_downstream_connection(rv, downstream);
101 if (downstream->attach_downstream_connection(std::move(ndconn)) != 0) {
104 if (downstream->push_request_headers() == 0) {
109 downstream->set_request_state(DownstreamState::CONNECT_FAIL);
111 if (rv == SHRPX_ERR_TLS_REQUIRED) {
112 rv = upstream->on_downstream_abort_request_with_https_redirect(downstream);
114 rv = upstream->on_downstream_abort_request(downstream, status_code);
124 void connect_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
125 auto conn = static_cast<Connection *>(w->data);
126 auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
127 auto addr = dconn->get_addr();
128 auto raddr = dconn->get_raddr();
130 DCLOG(WARN, dconn) << "Connect time out; addr="
131 << util::to_numeric_addr(raddr);
133 downstream_failure(addr, raddr);
135 auto downstream = dconn->get_downstream();
137 retry_downstream_connection(downstream, 504);
142 void backend_retry(Downstream *downstream) {
143 retry_downstream_connection(downstream, 502);
148 void readcb(struct ev_loop *loop, ev_io *w, int revents) {
150 auto conn = static_cast<Connection *>(w->data);
151 auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
152 auto downstream = dconn->get_downstream();
153 auto upstream = downstream->get_upstream();
154 auto handler = upstream->get_client_handler();
156 rv = upstream->downstream_read(dconn);
158 if (rv == SHRPX_ERR_RETRY) {
159 backend_retry(downstream);
169 void writecb(struct ev_loop *loop, ev_io *w, int revents) {
171 auto conn = static_cast<Connection *>(w->data);
172 auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
173 auto downstream = dconn->get_downstream();
174 auto upstream = downstream->get_upstream();
175 auto handler = upstream->get_client_handler();
177 rv = upstream->downstream_write(dconn);
178 if (rv == SHRPX_ERR_RETRY) {
179 backend_retry(downstream);
190 void connectcb(struct ev_loop *loop, ev_io *w, int revents) {
191 auto conn = static_cast<Connection *>(w->data);
192 auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
193 auto downstream = dconn->get_downstream();
194 if (dconn->connected() != 0) {
195 backend_retry(downstream);
198 writecb(loop, w, revents);
202 HttpDownstreamConnection::HttpDownstreamConnection(
203 const std::shared_ptr<DownstreamAddrGroup> &group, DownstreamAddr *addr,
204 struct ev_loop *loop, Worker *worker)
205 : conn_(loop, -1, nullptr, worker->get_mcpool(),
206 group->shared_addr->timeout.write, group->shared_addr->timeout.read,
207 {}, {}, connectcb, readcb, connect_timeoutcb, this,
208 get_config()->tls.dyn_rec.warmup_threshold,
209 get_config()->tls.dyn_rec.idle_timeout, Proto::HTTP1),
210 on_read_(&HttpDownstreamConnection::noop),
211 on_write_(&HttpDownstreamConnection::noop),
212 signal_write_(&HttpDownstreamConnection::noop),
214 ssl_ctx_(worker->get_cl_ssl_ctx()),
218 ioctrl_(&conn_.rlimit),
220 first_write_done_(false),
222 request_header_written_(false) {}
224 HttpDownstreamConnection::~HttpDownstreamConnection() {
225 if (LOG_ENABLED(INFO)) {
226 DCLOG(INFO, this) << "Deleted";
230 auto dns_tracker = worker_->get_dns_tracker();
231 dns_tracker->cancel(dns_query_.get());
235 int HttpDownstreamConnection::attach_downstream(Downstream *downstream) {
238 if (LOG_ENABLED(INFO)) {
239 DCLOG(INFO, this) << "Attaching to DOWNSTREAM:" << downstream;
242 downstream_ = downstream;
244 rv = initiate_connection();
246 downstream_ = nullptr;
254 int htp_msg_begincb(llhttp_t *htp);
255 int htp_hdr_keycb(llhttp_t *htp, const char *data, size_t len);
256 int htp_hdr_valcb(llhttp_t *htp, const char *data, size_t len);
257 int htp_hdrs_completecb(llhttp_t *htp);
258 int htp_bodycb(llhttp_t *htp, const char *data, size_t len);
259 int htp_msg_completecb(llhttp_t *htp);
263 constexpr llhttp_settings_t htp_hooks = {
264 htp_msg_begincb, // llhttp_cb on_message_begin;
265 nullptr, // llhttp_data_cb on_url;
266 nullptr, // llhttp_data_cb on_status;
267 htp_hdr_keycb, // llhttp_data_cb on_header_field;
268 htp_hdr_valcb, // llhttp_data_cb on_header_value;
269 htp_hdrs_completecb, // llhttp_cb on_headers_complete;
270 htp_bodycb, // llhttp_data_cb on_body;
271 htp_msg_completecb, // llhttp_cb on_message_complete;
272 nullptr, // llhttp_cb on_chunk_header
273 nullptr, // llhttp_cb on_chunk_complete
277 int HttpDownstreamConnection::initiate_connection() {
280 auto worker_blocker = worker_->get_connect_blocker();
281 if (worker_blocker->blocked()) {
282 if (LOG_ENABLED(INFO)) {
284 << "Worker wide backend connection was blocked temporarily";
286 return SHRPX_ERR_NETWORK;
289 auto &downstreamconf = *worker_->get_downstream_config();
291 if (conn_.fd == -1) {
292 auto check_dns_result = dns_query_.get() != nullptr;
294 if (check_dns_result) {
298 auto &connect_blocker = addr_->connect_blocker;
300 if (connect_blocker->blocked()) {
301 if (LOG_ENABLED(INFO)) {
302 DCLOG(INFO, this) << "Backend server " << addr_->host << ":"
303 << addr_->port << " was not available temporarily";
306 return SHRPX_ERR_NETWORK;
312 if (!check_dns_result) {
313 auto dns_query = std::make_unique<DNSQuery>(
315 [this](DNSResolverStatus status, const Address *result) {
318 if (status == DNSResolverStatus::OK) {
319 *this->resolved_addr_ = *result;
322 rv = this->initiate_connection();
324 // This callback destroys |this|.
325 auto downstream = this->downstream_;
326 backend_retry(downstream);
330 auto dns_tracker = worker_->get_dns_tracker();
332 if (!resolved_addr_) {
333 resolved_addr_ = std::make_unique<Address>();
335 switch (dns_tracker->resolve(resolved_addr_.get(), dns_query.get())) {
336 case DNSResolverStatus::ERROR:
337 downstream_failure(addr_, nullptr);
338 return SHRPX_ERR_NETWORK;
339 case DNSResolverStatus::RUNNING:
340 dns_query_ = std::move(dns_query);
342 case DNSResolverStatus::OK:
348 switch (dns_query_->status) {
349 case DNSResolverStatus::ERROR:
351 downstream_failure(addr_, nullptr);
352 return SHRPX_ERR_NETWORK;
353 case DNSResolverStatus::OK:
361 raddr = resolved_addr_.get();
362 util::set_port(*resolved_addr_, addr_->port);
364 raddr = &addr_->addr;
367 conn_.fd = util::create_nonblock_socket(raddr->su.storage.ss_family);
369 if (conn_.fd == -1) {
371 DCLOG(WARN, this) << "socket() failed; addr="
372 << util::to_numeric_addr(raddr) << ", errno=" << error;
374 worker_blocker->on_failure();
376 return SHRPX_ERR_NETWORK;
379 worker_blocker->on_success();
381 rv = connect(conn_.fd, &raddr->su.sa, raddr->len);
382 if (rv != 0 && errno != EINPROGRESS) {
384 DCLOG(WARN, this) << "connect() failed; addr="
385 << util::to_numeric_addr(raddr) << ", errno=" << error;
387 downstream_failure(addr_, raddr);
389 return SHRPX_ERR_NETWORK;
392 if (LOG_ENABLED(INFO)) {
393 DCLOG(INFO, this) << "Connecting to downstream server";
401 auto ssl = tls::create_ssl(ssl_ctx_);
406 tls::setup_downstream_http1_alpn(ssl);
409 conn_.tls.client_session_cache = &addr_->tls_session_cache;
412 addr_->sni.empty() ? StringRef{addr_->host} : StringRef{addr_->sni};
413 if (!util::numeric_host(sni_name.c_str())) {
414 SSL_set_tlsext_host_name(conn_.tls.ssl, sni_name.c_str());
417 auto session = tls::reuse_tls_session(addr_->tls_session_cache);
419 SSL_set_session(conn_.tls.ssl, session);
420 SSL_SESSION_free(session);
423 conn_.prepare_client_handshake();
426 ev_io_set(&conn_.wev, conn_.fd, EV_WRITE);
427 ev_io_set(&conn_.rev, conn_.fd, EV_READ);
429 conn_.wlimit.startw();
431 conn_.wt.repeat = downstreamconf.timeout.connect;
432 ev_timer_again(conn_.loop, &conn_.wt);
434 // we may set read timer cb to idle_timeoutcb. Reset again.
435 ev_set_cb(&conn_.rt, timeoutcb);
436 if (conn_.read_timeout < group_->shared_addr->timeout.read) {
437 conn_.read_timeout = group_->shared_addr->timeout.read;
438 conn_.last_read = ev_now(conn_.loop);
440 conn_.again_rt(group_->shared_addr->timeout.read);
443 ev_set_cb(&conn_.rev, readcb);
445 on_write_ = &HttpDownstreamConnection::write_first;
446 first_write_done_ = false;
447 request_header_written_ = false;
450 llhttp_init(&response_htp_, HTTP_RESPONSE, &htp_hooks);
451 response_htp_.data = downstream_;
456 int HttpDownstreamConnection::push_request_headers() {
457 if (request_header_written_) {
462 const auto &downstream_hostport = addr_->hostport;
463 const auto &req = downstream_->request();
465 auto &balloc = downstream_->get_block_allocator();
467 auto connect_method = req.regular_connect_method();
469 auto config = get_config();
470 auto &httpconf = config->http;
472 request_header_written_ = true;
474 // For HTTP/1.0 request, there is no authority in request. In that
475 // case, we use backend server's host nonetheless.
476 auto authority = StringRef(downstream_hostport);
477 auto no_host_rewrite =
478 httpconf.no_host_rewrite || config->http2_proxy || connect_method;
480 if (no_host_rewrite && !req.authority.empty()) {
481 authority = req.authority;
484 downstream_->set_request_downstream_host(authority);
486 auto buf = downstream_->get_request_buf();
488 // Assume that method and request path do not contain \r\n.
489 auto meth = http2::to_method_string(
490 req.connect_proto == ConnectProto::WEBSOCKET ? HTTP_GET : req.method);
494 if (connect_method) {
495 buf->append(authority);
496 } else if (config->http2_proxy) {
497 // Construct absolute-form request target because we are going to
498 // send a request to a HTTP/1 proxy.
499 assert(!req.scheme.empty());
500 buf->append(req.scheme);
502 buf->append(authority);
503 buf->append(req.path);
504 } else if (req.method == HTTP_OPTIONS && req.path.empty()) {
505 // Server-wide OPTIONS
508 buf->append(req.path);
510 buf->append(" HTTP/1.1\r\nHost: ");
511 buf->append(authority);
514 auto &fwdconf = httpconf.forwarded;
515 auto &xffconf = httpconf.xff;
516 auto &xfpconf = httpconf.xfp;
517 auto &earlydataconf = httpconf.early_data;
519 uint32_t build_flags =
520 (fwdconf.strip_incoming ? http2::HDOP_STRIP_FORWARDED : 0) |
521 (xffconf.strip_incoming ? http2::HDOP_STRIP_X_FORWARDED_FOR : 0) |
522 (xfpconf.strip_incoming ? http2::HDOP_STRIP_X_FORWARDED_PROTO : 0) |
523 (earlydataconf.strip_incoming ? http2::HDOP_STRIP_EARLY_DATA : 0) |
524 (req.http_major == 2 ? http2::HDOP_STRIP_SEC_WEBSOCKET_KEY : 0);
526 http2::build_http1_headers_from_headers(buf, req.fs.headers(), build_flags);
528 auto cookie = downstream_->assemble_request_cookie();
529 if (!cookie.empty()) {
530 buf->append("Cookie: ");
535 // set transfer-encoding only when content-length is unknown and
536 // request body is expected.
537 if (req.method != HTTP_CONNECT && req.http2_expect_body &&
538 req.fs.content_length == -1) {
539 downstream_->set_chunked_request(true);
540 buf->append("Transfer-Encoding: chunked\r\n");
543 if (req.connect_proto == ConnectProto::WEBSOCKET) {
544 if (req.http_major == 2) {
545 std::array<uint8_t, 16> nonce;
546 util::random_bytes(std::begin(nonce), std::end(nonce),
547 worker_->get_randgen());
548 auto iov = make_byte_ref(balloc, base64::encode_length(nonce.size()) + 1);
549 auto p = base64::encode(std::begin(nonce), std::end(nonce), iov.base);
551 auto key = StringRef{iov.base, p};
552 downstream_->set_ws_key(key);
554 buf->append("Sec-Websocket-Key: ");
559 buf->append("Upgrade: websocket\r\nConnection: Upgrade\r\n");
560 } else if (!connect_method && req.upgrade_request) {
561 auto connection = req.fs.header(http2::HD_CONNECTION);
563 buf->append("Connection: ");
564 buf->append((*connection).value);
568 auto upgrade = req.fs.header(http2::HD_UPGRADE);
570 buf->append("Upgrade: ");
571 buf->append((*upgrade).value);
574 } else if (req.connection_close) {
575 buf->append("Connection: close\r\n");
578 auto upstream = downstream_->get_upstream();
579 auto handler = upstream->get_client_handler();
581 #if OPENSSL_1_1_1_API
582 auto conn = handler->get_connection();
584 if (conn->tls.ssl && !SSL_is_init_finished(conn->tls.ssl)) {
585 buf->append("Early-Data: 1\r\n");
587 #endif // OPENSSL_1_1_1_API
590 fwdconf.strip_incoming ? nullptr : req.fs.header(http2::HD_FORWARDED);
592 if (fwdconf.params) {
593 auto params = fwdconf.params;
595 if (config->http2_proxy || connect_method) {
596 params &= ~FORWARDED_PROTO;
599 auto value = http::create_forwarded(
600 balloc, params, handler->get_forwarded_by(),
601 handler->get_forwarded_for(), req.authority, req.scheme);
603 if (fwd || !value.empty()) {
604 buf->append("Forwarded: ");
606 buf->append(fwd->value);
608 if (!value.empty()) {
616 buf->append("Forwarded: ");
617 buf->append(fwd->value);
621 auto xff = xffconf.strip_incoming ? nullptr
622 : req.fs.header(http2::HD_X_FORWARDED_FOR);
625 buf->append("X-Forwarded-For: ");
627 buf->append((*xff).value);
630 buf->append(client_handler_->get_ipaddr());
633 buf->append("X-Forwarded-For: ");
634 buf->append((*xff).value);
637 if (!config->http2_proxy && !connect_method) {
638 auto xfp = xfpconf.strip_incoming
640 : req.fs.header(http2::HD_X_FORWARDED_PROTO);
643 buf->append("X-Forwarded-Proto: ");
645 buf->append((*xfp).value);
648 assert(!req.scheme.empty());
649 buf->append(req.scheme);
652 buf->append("X-Forwarded-Proto: ");
653 buf->append((*xfp).value);
657 auto via = req.fs.header(http2::HD_VIA);
658 if (httpconf.no_via) {
660 buf->append("Via: ");
661 buf->append((*via).value);
665 buf->append("Via: ");
667 buf->append((*via).value);
670 std::array<char, 16> viabuf;
671 auto end = http::create_via_header_value(viabuf.data(), req.http_major,
673 buf->append(viabuf.data(), end - viabuf.data());
677 for (auto &p : httpconf.add_request_headers) {
680 buf->append(p.value);
686 if (LOG_ENABLED(INFO)) {
688 for (auto chunk = buf->head; chunk; chunk = chunk->next) {
689 nhdrs.append(chunk->pos, chunk->last);
691 if (log_config()->errorlog_tty) {
692 nhdrs = http::colorizeHeaders(nhdrs.c_str());
694 DCLOG(INFO, this) << "HTTP request headers. stream_id="
695 << downstream_->get_stream_id() << "\n"
699 // Don't call signal_write() if we anticipate request body. We call
700 // signal_write() when we received request body chunk, and it
701 // enables us to send headers and data in one writev system call.
702 if (req.method == HTTP_CONNECT ||
703 downstream_->get_blocked_request_buf()->rleft() ||
704 (!req.http2_expect_body && req.fs.content_length == 0) ||
705 downstream_->get_expect_100_continue()) {
712 int HttpDownstreamConnection::process_blocked_request_buf() {
713 auto src = downstream_->get_blocked_request_buf();
716 auto dest = downstream_->get_request_buf();
717 auto chunked = downstream_->get_chunked_request();
719 auto chunk_size_hex = util::utox(src->rleft());
720 dest->append(chunk_size_hex);
721 dest->append("\r\n");
727 dest->append("\r\n");
731 if (downstream_->get_blocked_request_data_eof() &&
732 downstream_->get_chunked_request()) {
733 end_upload_data_chunk();
739 int HttpDownstreamConnection::push_upload_data_chunk(const uint8_t *data,
741 if (!downstream_->get_request_header_sent()) {
742 auto output = downstream_->get_blocked_request_buf();
743 auto &req = downstream_->request();
744 output->append(data, datalen);
745 req.unconsumed_body_length += datalen;
746 if (request_header_written_) {
752 auto chunked = downstream_->get_chunked_request();
753 auto output = downstream_->get_request_buf();
756 auto chunk_size_hex = util::utox(datalen);
757 output->append(chunk_size_hex);
758 output->append("\r\n");
761 output->append(data, datalen);
764 output->append("\r\n");
772 int HttpDownstreamConnection::end_upload_data() {
773 if (!downstream_->get_request_header_sent()) {
774 downstream_->set_blocked_request_data_eof(true);
775 if (request_header_written_) {
783 if (!downstream_->get_chunked_request()) {
787 end_upload_data_chunk();
792 void HttpDownstreamConnection::end_upload_data_chunk() {
793 const auto &req = downstream_->request();
795 auto output = downstream_->get_request_buf();
796 const auto &trailers = req.fs.trailers();
797 if (trailers.empty()) {
798 output->append("0\r\n\r\n");
800 output->append("0\r\n");
801 http2::build_http1_headers_from_headers(output, trailers,
802 http2::HDOP_STRIP_ALL);
803 output->append("\r\n");
808 void remove_from_pool(HttpDownstreamConnection *dconn) {
809 auto addr = dconn->get_addr();
810 auto &dconn_pool = addr->dconn_pool;
811 dconn_pool->remove_downstream_connection(dconn);
816 void idle_readcb(struct ev_loop *loop, ev_io *w, int revents) {
817 auto conn = static_cast<Connection *>(w->data);
818 auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
819 if (LOG_ENABLED(INFO)) {
820 DCLOG(INFO, dconn) << "Idle connection EOF";
823 remove_from_pool(dconn);
829 void idle_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
830 auto conn = static_cast<Connection *>(w->data);
831 auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
833 if (w == &conn->rt && !conn->expired_rt()) {
837 if (LOG_ENABLED(INFO)) {
838 DCLOG(INFO, dconn) << "Idle connection timeout";
841 remove_from_pool(dconn);
846 void HttpDownstreamConnection::detach_downstream(Downstream *downstream) {
847 if (LOG_ENABLED(INFO)) {
848 DCLOG(INFO, this) << "Detaching from DOWNSTREAM:" << downstream;
850 downstream_ = nullptr;
852 ev_set_cb(&conn_.rev, idle_readcb);
853 ioctrl_.force_resume_read();
855 auto &downstreamconf = *worker_->get_downstream_config();
857 ev_set_cb(&conn_.rt, idle_timeoutcb);
858 if (conn_.read_timeout < downstreamconf.timeout.idle_read) {
859 conn_.read_timeout = downstreamconf.timeout.idle_read;
860 conn_.last_read = ev_now(conn_.loop);
862 conn_.again_rt(downstreamconf.timeout.idle_read);
865 conn_.wlimit.stopw();
866 ev_timer_stop(conn_.loop, &conn_.wt);
869 void HttpDownstreamConnection::pause_read(IOCtrlReason reason) {
870 ioctrl_.pause_read(reason);
873 int HttpDownstreamConnection::resume_read(IOCtrlReason reason,
875 auto &downstreamconf = *worker_->get_downstream_config();
877 if (downstream_->get_response_buf()->rleft() <=
878 downstreamconf.request_buffer_size / 2) {
879 ioctrl_.resume_read(reason);
885 void HttpDownstreamConnection::force_resume_read() {
886 ioctrl_.force_resume_read();
890 int htp_msg_begincb(llhttp_t *htp) {
891 auto downstream = static_cast<Downstream *>(htp->data);
893 if (downstream->get_response_state() != DownstreamState::INITIAL) {
894 llhttp_set_error_reason(htp, "HTTP message started when it shouldn't");
903 int htp_hdrs_completecb(llhttp_t *htp) {
904 auto downstream = static_cast<Downstream *>(htp->data);
905 auto upstream = downstream->get_upstream();
906 auto handler = upstream->get_client_handler();
907 const auto &req = downstream->request();
908 auto &resp = downstream->response();
911 auto config = get_config();
912 auto &loggingconf = config->logging;
914 resp.http_status = htp->status_code;
915 resp.http_major = htp->http_major;
916 resp.http_minor = htp->http_minor;
918 if (resp.http_major > 1 || req.http_minor > 1) {
924 auto dconn = downstream->get_downstream_connection();
926 downstream->set_downstream_addr_group(dconn->get_downstream_addr_group());
927 downstream->set_addr(dconn->get_addr());
929 // Server MUST NOT send Transfer-Encoding with a status code 1xx or
930 // 204. Also server MUST NOT send Transfer-Encoding with a status
931 // code 2xx to a CONNECT request. Same holds true with
933 if (resp.http_status == 204) {
934 if (resp.fs.header(http2::HD_TRANSFER_ENCODING)) {
937 // Some server send content-length: 0 for 204. Until they get
938 // fixed, we accept, but ignore it.
940 // Calling parse_content_length() detects duplicated
941 // content-length header fields.
942 if (resp.fs.parse_content_length() != 0) {
945 if (resp.fs.content_length == 0) {
946 resp.fs.erase_content_length_and_transfer_encoding();
947 } else if (resp.fs.content_length != -1) {
950 } else if (resp.http_status / 100 == 1 ||
951 (resp.http_status / 100 == 2 && req.method == HTTP_CONNECT)) {
952 // Server MUST NOT send Content-Length and Transfer-Encoding in
954 resp.fs.erase_content_length_and_transfer_encoding();
955 } else if (resp.fs.parse_content_length() != 0) {
956 downstream->set_response_state(DownstreamState::MSG_BAD_HEADER);
960 // Check upgrade before processing non-final response, since if
961 // upgrade succeeded, 101 response is treated as final in nghttpx.
962 downstream->check_upgrade_fulfilled_http1();
964 if (downstream->get_non_final_response()) {
965 // Reset content-length because we reuse same Downstream for the
967 resp.fs.content_length = -1;
968 // For non-final response code, we just call
969 // on_downstream_header_complete() without changing response
971 rv = upstream->on_downstream_header_complete(downstream);
977 // Ignore response body for non-final response.
981 resp.connection_close = !llhttp_should_keep_alive(htp);
982 downstream->set_response_state(DownstreamState::HEADER_COMPLETE);
983 downstream->inspect_http1_response();
984 if (downstream->get_upgraded()) {
985 // content-length must be ignored for upgraded connection.
986 resp.fs.content_length = -1;
987 resp.connection_close = true;
988 // transfer-encoding not applied to upgraded connection
989 downstream->set_chunked_response(false);
990 } else if (http2::legacy_http1(req.http_major, req.http_minor)) {
991 if (resp.fs.content_length == -1) {
992 resp.connection_close = true;
994 downstream->set_chunked_response(false);
995 } else if (!downstream->expect_response_body()) {
996 downstream->set_chunked_response(false);
999 if (loggingconf.access.write_early && downstream->accesslog_ready()) {
1000 handler->write_accesslog(downstream);
1001 downstream->set_accesslog_written(true);
1004 if (upstream->on_downstream_header_complete(downstream) != 0) {
1008 if (downstream->get_upgraded()) {
1009 // Upgrade complete, read until EOF in both ends
1010 if (upstream->resume_read(SHRPX_NO_BUFFER, downstream, 0) != 0) {
1013 downstream->set_request_state(DownstreamState::HEADER_COMPLETE);
1014 if (LOG_ENABLED(INFO)) {
1015 LOG(INFO) << "HTTP upgrade success. stream_id="
1016 << downstream->get_stream_id();
1020 // Ignore the response body. HEAD response may contain
1021 // Content-Length or Transfer-Encoding: chunked. Some server send
1022 // 304 status code with nonzero Content-Length, but without response
1024 // https://tools.ietf.org/html/rfc7230#section-3.3
1026 // TODO It seems that the cases other than HEAD are handled by
1027 // llhttp. Need test.
1028 return !http2::expect_response_body(req.method, resp.http_status);
1033 int ensure_header_field_buffer(const Downstream *downstream,
1034 const HttpConfig &httpconf, size_t len) {
1035 auto &resp = downstream->response();
1037 if (resp.fs.buffer_size() + len > httpconf.response_header_field_buffer) {
1038 if (LOG_ENABLED(INFO)) {
1039 DLOG(INFO, downstream) << "Too large header header field size="
1040 << resp.fs.buffer_size() + len;
1050 int ensure_max_header_fields(const Downstream *downstream,
1051 const HttpConfig &httpconf) {
1052 auto &resp = downstream->response();
1054 if (resp.fs.num_fields() >= httpconf.max_response_header_fields) {
1055 if (LOG_ENABLED(INFO)) {
1056 DLOG(INFO, downstream)
1057 << "Too many header field num=" << resp.fs.num_fields() + 1;
1067 int htp_hdr_keycb(llhttp_t *htp, const char *data, size_t len) {
1068 auto downstream = static_cast<Downstream *>(htp->data);
1069 auto &resp = downstream->response();
1070 auto &httpconf = get_config()->http;
1072 if (ensure_header_field_buffer(downstream, httpconf, len) != 0) {
1076 if (downstream->get_response_state() == DownstreamState::INITIAL) {
1077 if (resp.fs.header_key_prev()) {
1078 resp.fs.append_last_header_key(data, len);
1080 if (ensure_max_header_fields(downstream, httpconf) != 0) {
1083 resp.fs.alloc_add_header_name(StringRef{data, len});
1087 if (resp.fs.trailer_key_prev()) {
1088 resp.fs.append_last_trailer_key(data, len);
1090 if (ensure_max_header_fields(downstream, httpconf) != 0) {
1091 // Could not ignore this trailer field easily, since we may
1092 // get its value in htp_hdr_valcb, and it will be added to
1093 // wrong place or crash if trailer fields are currently empty.
1096 resp.fs.alloc_add_trailer_name(StringRef{data, len});
1104 int htp_hdr_valcb(llhttp_t *htp, const char *data, size_t len) {
1105 auto downstream = static_cast<Downstream *>(htp->data);
1106 auto &resp = downstream->response();
1107 auto &httpconf = get_config()->http;
1109 if (ensure_header_field_buffer(downstream, httpconf, len) != 0) {
1113 if (downstream->get_response_state() == DownstreamState::INITIAL) {
1114 resp.fs.append_last_header_value(data, len);
1116 resp.fs.append_last_trailer_value(data, len);
1123 int htp_bodycb(llhttp_t *htp, const char *data, size_t len) {
1124 auto downstream = static_cast<Downstream *>(htp->data);
1125 auto &resp = downstream->response();
1127 resp.recv_body_length += len;
1129 return downstream->get_upstream()->on_downstream_body(
1130 downstream, reinterpret_cast<const uint8_t *>(data), len, true);
1135 int htp_msg_completecb(llhttp_t *htp) {
1136 auto downstream = static_cast<Downstream *>(htp->data);
1138 // llhttp does not treat "200 connection established" response
1139 // against CONNECT request, and in that case, this function is not
1140 // called. But if HTTP Upgrade is made (e.g., WebSocket), this
1141 // function is called, and llhttp_execute() returns just after that.
1142 if (downstream->get_upgraded()) {
1146 if (downstream->get_non_final_response()) {
1147 downstream->reset_response();
1152 downstream->set_response_state(DownstreamState::MSG_COMPLETE);
1153 // Block reading another response message from (broken?)
1154 // server. This callback is not called if the connection is
1156 downstream->pause_read(SHRPX_MSG_BLOCK);
1157 return downstream->get_upstream()->on_downstream_body_complete(downstream);
1161 int HttpDownstreamConnection::write_first() {
1164 process_blocked_request_buf();
1166 if (conn_.tls.ssl) {
1173 return SHRPX_ERR_RETRY;
1176 if (conn_.tls.ssl) {
1177 on_write_ = &HttpDownstreamConnection::write_tls;
1179 on_write_ = &HttpDownstreamConnection::write_clear;
1182 first_write_done_ = true;
1183 downstream_->set_request_header_sent(true);
1185 auto buf = downstream_->get_blocked_request_buf();
1188 // upstream->resume_read() might be called in
1189 // write_tls()/write_clear(), but before blocked_request_buf_ is
1190 // reset. So upstream read might still be blocked. Let's do it
1192 auto input = downstream_->get_request_buf();
1193 if (input->rleft() == 0) {
1194 auto upstream = downstream_->get_upstream();
1195 auto &req = downstream_->request();
1197 upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1198 req.unconsumed_body_length);
1204 int HttpDownstreamConnection::read_clear() {
1205 conn_.last_read = ev_now(conn_.loop);
1207 std::array<uint8_t, 16_k> buf;
1211 auto nread = conn_.read_clear(buf.data(), buf.size());
1220 rv = process_input(buf.data(), nread);
1225 if (!ev_is_active(&conn_.rev)) {
1231 int HttpDownstreamConnection::write_clear() {
1232 conn_.last_read = ev_now(conn_.loop);
1234 auto upstream = downstream_->get_upstream();
1235 auto input = downstream_->get_request_buf();
1237 std::array<struct iovec, MAX_WR_IOVCNT> iov;
1239 while (input->rleft() > 0) {
1240 auto iovcnt = input->riovec(iov.data(), iov.size());
1242 auto nwrite = conn_.writev_clear(iov.data(), iovcnt);
1249 if (!first_write_done_) {
1252 // We may have pending data in receive buffer which may contain
1253 // part of response body. So keep reading. Invoke read event
1254 // to get read(2) error just in case.
1255 ev_feed_event(conn_.loop, &conn_.rev, EV_READ);
1256 on_write_ = &HttpDownstreamConnection::noop;
1261 input->drain(nwrite);
1264 conn_.wlimit.stopw();
1265 ev_timer_stop(conn_.loop, &conn_.wt);
1267 if (input->rleft() == 0) {
1268 auto &req = downstream_->request();
1270 upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1271 req.unconsumed_body_length);
1277 int HttpDownstreamConnection::tls_handshake() {
1280 conn_.last_read = ev_now(conn_.loop);
1282 auto rv = conn_.tls_handshake();
1283 if (rv == SHRPX_ERR_INPROGRESS) {
1288 downstream_failure(addr_, raddr_);
1293 if (LOG_ENABLED(INFO)) {
1294 DCLOG(INFO, this) << "SSL/TLS handshake completed";
1297 if (!get_config()->tls.insecure &&
1298 tls::check_cert(conn_.tls.ssl, addr_, raddr_) != 0) {
1299 downstream_failure(addr_, raddr_);
1304 auto &connect_blocker = addr_->connect_blocker;
1306 signal_write_ = &HttpDownstreamConnection::actual_signal_write;
1308 connect_blocker->on_success();
1310 ev_set_cb(&conn_.rt, timeoutcb);
1311 ev_set_cb(&conn_.wt, timeoutcb);
1313 on_read_ = &HttpDownstreamConnection::read_tls;
1314 on_write_ = &HttpDownstreamConnection::write_first;
1316 // TODO Check negotiated ALPN
1321 int HttpDownstreamConnection::read_tls() {
1322 conn_.last_read = ev_now(conn_.loop);
1326 std::array<uint8_t, 16_k> buf;
1330 auto nread = conn_.read_tls(buf.data(), buf.size());
1339 rv = process_input(buf.data(), nread);
1344 if (!ev_is_active(&conn_.rev)) {
1350 int HttpDownstreamConnection::write_tls() {
1351 conn_.last_read = ev_now(conn_.loop);
1355 auto upstream = downstream_->get_upstream();
1356 auto input = downstream_->get_request_buf();
1360 while (input->rleft() > 0) {
1361 auto iovcnt = input->riovec(&iov, 1);
1366 auto nwrite = conn_.write_tls(iov.iov_base, iov.iov_len);
1373 if (!first_write_done_) {
1376 // We may have pending data in receive buffer which may contain
1377 // part of response body. So keep reading. Invoke read event
1378 // to get read(2) error just in case.
1379 ev_feed_event(conn_.loop, &conn_.rev, EV_READ);
1380 on_write_ = &HttpDownstreamConnection::noop;
1385 input->drain(nwrite);
1388 conn_.wlimit.stopw();
1389 ev_timer_stop(conn_.loop, &conn_.wt);
1391 if (input->rleft() == 0) {
1392 auto &req = downstream_->request();
1394 upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1395 req.unconsumed_body_length);
1401 int HttpDownstreamConnection::process_input(const uint8_t *data,
1405 if (downstream_->get_upgraded()) {
1406 // For upgraded connection, just pass data to the upstream.
1407 rv = downstream_->get_upstream()->on_downstream_body(downstream_, data,
1413 if (downstream_->response_buf_full()) {
1414 downstream_->pause_read(SHRPX_NO_BUFFER);
1421 auto htperr = llhttp_execute(&response_htp_,
1422 reinterpret_cast<const char *>(data), datalen);
1426 : static_cast<size_t>(reinterpret_cast<const uint8_t *>(
1427 llhttp_get_error_pos(&response_htp_)) -
1430 if (htperr != HPE_OK &&
1431 (!downstream_->get_upgraded() || htperr != HPE_PAUSED_UPGRADE)) {
1432 // Handling early return (in other words, response was hijacked by
1433 // mruby scripting).
1434 if (downstream_->get_response_state() == DownstreamState::MSG_COMPLETE) {
1435 return SHRPX_ERR_DCONN_CANCELED;
1438 if (LOG_ENABLED(INFO)) {
1439 DCLOG(INFO, this) << "HTTP parser failure: "
1440 << "(" << llhttp_errno_name(htperr) << ") "
1441 << llhttp_get_error_reason(&response_htp_);
1447 if (downstream_->get_upgraded()) {
1448 if (nproc < datalen) {
1449 // Data from data + nproc are for upgraded protocol.
1450 rv = downstream_->get_upstream()->on_downstream_body(
1451 downstream_, data + nproc, datalen - nproc, true);
1456 if (downstream_->response_buf_full()) {
1457 downstream_->pause_read(SHRPX_NO_BUFFER);
1464 if (downstream_->response_buf_full()) {
1465 downstream_->pause_read(SHRPX_NO_BUFFER);
1472 int HttpDownstreamConnection::connected() {
1473 auto &connect_blocker = addr_->connect_blocker;
1475 auto sock_error = util::get_socket_error(conn_.fd);
1476 if (sock_error != 0) {
1477 conn_.wlimit.stopw();
1479 DCLOG(WARN, this) << "Backend connect failed; addr="
1480 << util::to_numeric_addr(raddr_)
1481 << ": errno=" << sock_error;
1483 downstream_failure(addr_, raddr_);
1488 if (LOG_ENABLED(INFO)) {
1489 DCLOG(INFO, this) << "Connected to downstream host";
1492 // Reset timeout for write. Previously, we set timeout for connect.
1493 conn_.wt.repeat = group_->shared_addr->timeout.write;
1494 ev_timer_again(conn_.loop, &conn_.wt);
1496 conn_.rlimit.startw();
1499 ev_set_cb(&conn_.wev, writecb);
1501 if (conn_.tls.ssl) {
1502 on_read_ = &HttpDownstreamConnection::tls_handshake;
1503 on_write_ = &HttpDownstreamConnection::tls_handshake;
1508 signal_write_ = &HttpDownstreamConnection::actual_signal_write;
1510 connect_blocker->on_success();
1512 ev_set_cb(&conn_.rt, timeoutcb);
1513 ev_set_cb(&conn_.wt, timeoutcb);
1515 on_read_ = &HttpDownstreamConnection::read_clear;
1516 on_write_ = &HttpDownstreamConnection::write_first;
1521 int HttpDownstreamConnection::on_read() { return on_read_(*this); }
1523 int HttpDownstreamConnection::on_write() { return on_write_(*this); }
1525 void HttpDownstreamConnection::on_upstream_change(Upstream *upstream) {}
1527 void HttpDownstreamConnection::signal_write() { signal_write_(*this); }
1529 int HttpDownstreamConnection::actual_signal_write() {
1530 ev_feed_event(conn_.loop, &conn_.wev, EV_WRITE);
1534 int HttpDownstreamConnection::noop() { return 0; }
1536 const std::shared_ptr<DownstreamAddrGroup> &
1537 HttpDownstreamConnection::get_downstream_addr_group() const {
1541 DownstreamAddr *HttpDownstreamConnection::get_addr() const { return addr_; }
1543 bool HttpDownstreamConnection::poolable() const {
1544 return !group_->retired && reusable_;
1547 const Address *HttpDownstreamConnection::get_raddr() const { return raddr_; }
1549 } // namespace shrpx