• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * nghttp2 - HTTP/2 C Library
3  *
4  * Copyright (c) 2012 Tatsuhiro Tsujikawa
5  *
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:
13  *
14  * The above copyright notice and this permission notice shall be
15  * included in all copies or substantial portions of the Software.
16  *
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.
24  */
25 #include "shrpx_http_downstream_connection.h"
26 
27 #include <openssl/rand.h>
28 
29 #include "shrpx_client_handler.h"
30 #include "shrpx_upstream.h"
31 #include "shrpx_downstream.h"
32 #include "shrpx_config.h"
33 #include "shrpx_error.h"
34 #include "shrpx_http.h"
35 #include "shrpx_log_config.h"
36 #include "shrpx_connect_blocker.h"
37 #include "shrpx_downstream_connection_pool.h"
38 #include "shrpx_worker.h"
39 #include "shrpx_http2_session.h"
40 #include "shrpx_tls.h"
41 #include "shrpx_log.h"
42 #include "http2.h"
43 #include "util.h"
44 #include "ssl_compat.h"
45 
46 using namespace nghttp2;
47 
48 namespace shrpx {
49 
50 namespace {
timeoutcb(struct ev_loop * loop,ev_timer * w,int revents)51 void timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
52   auto conn = static_cast<Connection *>(w->data);
53   auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
54 
55   if (w == &conn->rt && !conn->expired_rt()) {
56     return;
57   }
58 
59   if (LOG_ENABLED(INFO)) {
60     DCLOG(INFO, dconn) << "Time out";
61   }
62 
63   auto downstream = dconn->get_downstream();
64   auto upstream = downstream->get_upstream();
65   auto handler = upstream->get_client_handler();
66   auto &resp = downstream->response();
67 
68   // Do this so that dconn is not pooled
69   resp.connection_close = true;
70 
71   if (upstream->downstream_error(dconn, Downstream::EVENT_TIMEOUT) != 0) {
72     delete handler;
73   }
74 }
75 } // namespace
76 
77 namespace {
retry_downstream_connection(Downstream * downstream,unsigned int status_code)78 void retry_downstream_connection(Downstream *downstream,
79                                  unsigned int status_code) {
80   auto upstream = downstream->get_upstream();
81   auto handler = upstream->get_client_handler();
82 
83   assert(!downstream->get_request_header_sent());
84 
85   downstream->add_retry();
86 
87   if (downstream->no_more_retry()) {
88     delete handler;
89     return;
90   }
91 
92   downstream->pop_downstream_connection();
93   auto buf = downstream->get_request_buf();
94   buf->reset();
95 
96   int rv;
97 
98   for (;;) {
99     auto ndconn = handler->get_downstream_connection(rv, downstream);
100     if (!ndconn) {
101       break;
102     }
103     if (downstream->attach_downstream_connection(std::move(ndconn)) != 0) {
104       continue;
105     }
106     if (downstream->push_request_headers() == 0) {
107       return;
108     }
109   }
110 
111   downstream->set_request_state(DownstreamState::CONNECT_FAIL);
112 
113   if (rv == SHRPX_ERR_TLS_REQUIRED) {
114     rv = upstream->on_downstream_abort_request_with_https_redirect(downstream);
115   } else {
116     rv = upstream->on_downstream_abort_request(downstream, status_code);
117   }
118 
119   if (rv != 0) {
120     delete handler;
121   }
122 }
123 } // namespace
124 
125 namespace {
connect_timeoutcb(struct ev_loop * loop,ev_timer * w,int revents)126 void connect_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
127   auto conn = static_cast<Connection *>(w->data);
128   auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
129   auto addr = dconn->get_addr();
130   auto raddr = dconn->get_raddr();
131 
132   DCLOG(WARN, dconn) << "Connect time out; addr="
133                      << util::to_numeric_addr(raddr);
134 
135   downstream_failure(addr, raddr);
136 
137   auto downstream = dconn->get_downstream();
138 
139   retry_downstream_connection(downstream, 504);
140 }
141 } // namespace
142 
143 namespace {
backend_retry(Downstream * downstream)144 void backend_retry(Downstream *downstream) {
145   retry_downstream_connection(downstream, 502);
146 }
147 } // namespace
148 
149 namespace {
readcb(struct ev_loop * loop,ev_io * w,int revents)150 void readcb(struct ev_loop *loop, ev_io *w, int revents) {
151   int rv;
152   auto conn = static_cast<Connection *>(w->data);
153   auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
154   auto downstream = dconn->get_downstream();
155   auto upstream = downstream->get_upstream();
156   auto handler = upstream->get_client_handler();
157 
158   rv = upstream->downstream_read(dconn);
159   if (rv != 0) {
160     if (rv == SHRPX_ERR_RETRY) {
161       backend_retry(downstream);
162       return;
163     }
164 
165     delete handler;
166   }
167 }
168 } // namespace
169 
170 namespace {
writecb(struct ev_loop * loop,ev_io * w,int revents)171 void writecb(struct ev_loop *loop, ev_io *w, int revents) {
172   int rv;
173   auto conn = static_cast<Connection *>(w->data);
174   auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
175   auto downstream = dconn->get_downstream();
176   auto upstream = downstream->get_upstream();
177   auto handler = upstream->get_client_handler();
178 
179   rv = upstream->downstream_write(dconn);
180   if (rv == SHRPX_ERR_RETRY) {
181     backend_retry(downstream);
182     return;
183   }
184 
185   if (rv != 0) {
186     delete handler;
187   }
188 }
189 } // namespace
190 
191 namespace {
connectcb(struct ev_loop * loop,ev_io * w,int revents)192 void connectcb(struct ev_loop *loop, ev_io *w, int revents) {
193   auto conn = static_cast<Connection *>(w->data);
194   auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
195   auto downstream = dconn->get_downstream();
196   if (dconn->connected() != 0) {
197     backend_retry(downstream);
198     return;
199   }
200   writecb(loop, w, revents);
201 }
202 } // namespace
203 
HttpDownstreamConnection(const std::shared_ptr<DownstreamAddrGroup> & group,DownstreamAddr * addr,struct ev_loop * loop,Worker * worker)204 HttpDownstreamConnection::HttpDownstreamConnection(
205     const std::shared_ptr<DownstreamAddrGroup> &group, DownstreamAddr *addr,
206     struct ev_loop *loop, Worker *worker)
207     : conn_(loop, -1, nullptr, worker->get_mcpool(),
208             group->shared_addr->timeout.write, group->shared_addr->timeout.read,
209             {}, {}, connectcb, readcb, connect_timeoutcb, this,
210             get_config()->tls.dyn_rec.warmup_threshold,
211             get_config()->tls.dyn_rec.idle_timeout, Proto::HTTP1),
212       on_read_(&HttpDownstreamConnection::noop),
213       on_write_(&HttpDownstreamConnection::noop),
214       signal_write_(&HttpDownstreamConnection::noop),
215       worker_(worker),
216       ssl_ctx_(worker->get_cl_ssl_ctx()),
217       group_(group),
218       addr_(addr),
219       raddr_(nullptr),
220       ioctrl_(&conn_.rlimit),
221       response_htp_{0},
222       first_write_done_(false),
223       reusable_(true),
224       request_header_written_(false) {}
225 
~HttpDownstreamConnection()226 HttpDownstreamConnection::~HttpDownstreamConnection() {
227   if (LOG_ENABLED(INFO)) {
228     DCLOG(INFO, this) << "Deleted";
229   }
230 
231   if (dns_query_) {
232     auto dns_tracker = worker_->get_dns_tracker();
233     dns_tracker->cancel(dns_query_.get());
234   }
235 }
236 
attach_downstream(Downstream * downstream)237 int HttpDownstreamConnection::attach_downstream(Downstream *downstream) {
238   int rv;
239 
240   if (LOG_ENABLED(INFO)) {
241     DCLOG(INFO, this) << "Attaching to DOWNSTREAM:" << downstream;
242   }
243 
244   downstream_ = downstream;
245 
246   rv = initiate_connection();
247   if (rv != 0) {
248     downstream_ = nullptr;
249     return rv;
250   }
251 
252   return 0;
253 }
254 
255 namespace {
256 int htp_msg_begincb(llhttp_t *htp);
257 int htp_hdr_keycb(llhttp_t *htp, const char *data, size_t len);
258 int htp_hdr_valcb(llhttp_t *htp, const char *data, size_t len);
259 int htp_hdrs_completecb(llhttp_t *htp);
260 int htp_bodycb(llhttp_t *htp, const char *data, size_t len);
261 int htp_msg_completecb(llhttp_t *htp);
262 } // namespace
263 
264 namespace {
265 constexpr llhttp_settings_t htp_hooks = {
266     htp_msg_begincb,     // llhttp_cb      on_message_begin;
267     nullptr,             // llhttp_data_cb on_url;
268     nullptr,             // llhttp_data_cb on_status;
269     nullptr,             // llhttp_data_cb on_method;
270     nullptr,             // llhttp_data_cb on_version;
271     htp_hdr_keycb,       // llhttp_data_cb on_header_field;
272     htp_hdr_valcb,       // llhttp_data_cb on_header_value;
273     nullptr,             // llhttp_data_cb on_chunk_extension_name;
274     nullptr,             // llhttp_data_cb on_chunk_extension_value;
275     htp_hdrs_completecb, // llhttp_cb      on_headers_complete;
276     htp_bodycb,          // llhttp_data_cb on_body;
277     htp_msg_completecb,  // llhttp_cb      on_message_complete;
278     nullptr,             // llhttp_cb      on_url_complete;
279     nullptr,             // llhttp_cb      on_status_complete;
280     nullptr,             // llhttp_cb      on_method_complete;
281     nullptr,             // llhttp_cb      on_version_complete;
282     nullptr,             // llhttp_cb      on_header_field_complete;
283     nullptr,             // llhttp_cb      on_header_value_complete;
284     nullptr,             // llhttp_cb      on_chunk_extension_name_complete;
285     nullptr,             // llhttp_cb      on_chunk_extension_value_complete;
286     nullptr,             // llhttp_cb      on_chunk_header;
287     nullptr,             // llhttp_cb      on_chunk_complete;
288     nullptr,             // llhttp_cb      on_reset;
289 };
290 } // namespace
291 
initiate_connection()292 int HttpDownstreamConnection::initiate_connection() {
293   int rv;
294 
295   auto worker_blocker = worker_->get_connect_blocker();
296   if (worker_blocker->blocked()) {
297     if (LOG_ENABLED(INFO)) {
298       DCLOG(INFO, this)
299           << "Worker wide backend connection was blocked temporarily";
300     }
301     return SHRPX_ERR_NETWORK;
302   }
303 
304   auto &downstreamconf = *worker_->get_downstream_config();
305 
306   if (conn_.fd == -1) {
307     auto check_dns_result = dns_query_.get() != nullptr;
308 
309     if (check_dns_result) {
310       assert(addr_->dns);
311     }
312 
313     auto &connect_blocker = addr_->connect_blocker;
314 
315     if (connect_blocker->blocked()) {
316       if (LOG_ENABLED(INFO)) {
317         DCLOG(INFO, this) << "Backend server " << addr_->host << ":"
318                           << addr_->port << " was not available temporarily";
319       }
320 
321       return SHRPX_ERR_NETWORK;
322     }
323 
324     Address *raddr;
325 
326     if (addr_->dns) {
327       if (!check_dns_result) {
328         auto dns_query = std::make_unique<DNSQuery>(
329             addr_->host,
330             [this](DNSResolverStatus status, const Address *result) {
331               int rv;
332 
333               if (status == DNSResolverStatus::OK) {
334                 *this->resolved_addr_ = *result;
335               }
336 
337               rv = this->initiate_connection();
338               if (rv != 0) {
339                 // This callback destroys |this|.
340                 auto downstream = this->downstream_;
341                 backend_retry(downstream);
342               }
343             });
344 
345         auto dns_tracker = worker_->get_dns_tracker();
346 
347         if (!resolved_addr_) {
348           resolved_addr_ = std::make_unique<Address>();
349         }
350         switch (dns_tracker->resolve(resolved_addr_.get(), dns_query.get())) {
351         case DNSResolverStatus::ERROR:
352           downstream_failure(addr_, nullptr);
353           return SHRPX_ERR_NETWORK;
354         case DNSResolverStatus::RUNNING:
355           dns_query_ = std::move(dns_query);
356           return 0;
357         case DNSResolverStatus::OK:
358           break;
359         default:
360           assert(0);
361         }
362       } else {
363         switch (dns_query_->status) {
364         case DNSResolverStatus::ERROR:
365           dns_query_.reset();
366           downstream_failure(addr_, nullptr);
367           return SHRPX_ERR_NETWORK;
368         case DNSResolverStatus::OK:
369           dns_query_.reset();
370           break;
371         default:
372           assert(0);
373         }
374       }
375 
376       raddr = resolved_addr_.get();
377       util::set_port(*resolved_addr_, addr_->port);
378     } else {
379       raddr = &addr_->addr;
380     }
381 
382     conn_.fd = util::create_nonblock_socket(raddr->su.storage.ss_family);
383 
384     if (conn_.fd == -1) {
385       auto error = errno;
386       DCLOG(WARN, this) << "socket() failed; addr="
387                         << util::to_numeric_addr(raddr) << ", errno=" << error;
388 
389       worker_blocker->on_failure();
390 
391       return SHRPX_ERR_NETWORK;
392     }
393 
394     worker_blocker->on_success();
395 
396     rv = connect(conn_.fd, &raddr->su.sa, raddr->len);
397     if (rv != 0 && errno != EINPROGRESS) {
398       auto error = errno;
399       DCLOG(WARN, this) << "connect() failed; addr="
400                         << util::to_numeric_addr(raddr) << ", errno=" << error;
401 
402       downstream_failure(addr_, raddr);
403 
404       return SHRPX_ERR_NETWORK;
405     }
406 
407     if (LOG_ENABLED(INFO)) {
408       DCLOG(INFO, this) << "Connecting to downstream server";
409     }
410 
411     raddr_ = raddr;
412 
413     if (addr_->tls) {
414       assert(ssl_ctx_);
415 
416       auto ssl = tls::create_ssl(ssl_ctx_);
417       if (!ssl) {
418         return -1;
419       }
420 
421       tls::setup_downstream_http1_alpn(ssl);
422 
423       conn_.set_ssl(ssl);
424       conn_.tls.client_session_cache = &addr_->tls_session_cache;
425 
426       auto sni_name =
427           addr_->sni.empty() ? StringRef{addr_->host} : StringRef{addr_->sni};
428       if (!util::numeric_host(sni_name.c_str())) {
429         SSL_set_tlsext_host_name(conn_.tls.ssl, sni_name.c_str());
430       }
431 
432       auto session = tls::reuse_tls_session(addr_->tls_session_cache);
433       if (session) {
434         SSL_set_session(conn_.tls.ssl, session);
435         SSL_SESSION_free(session);
436       }
437 
438       conn_.prepare_client_handshake();
439     }
440 
441     ev_io_set(&conn_.wev, conn_.fd, EV_WRITE);
442     ev_io_set(&conn_.rev, conn_.fd, EV_READ);
443 
444     conn_.wlimit.startw();
445 
446     conn_.wt.repeat = downstreamconf.timeout.connect;
447     ev_timer_again(conn_.loop, &conn_.wt);
448   } else {
449     // we may set read timer cb to idle_timeoutcb.  Reset again.
450     ev_set_cb(&conn_.rt, timeoutcb);
451     if (conn_.read_timeout < group_->shared_addr->timeout.read) {
452       conn_.read_timeout = group_->shared_addr->timeout.read;
453       conn_.last_read = std::chrono::steady_clock::now();
454     } else {
455       conn_.again_rt(group_->shared_addr->timeout.read);
456     }
457 
458     ev_set_cb(&conn_.rev, readcb);
459 
460     on_write_ = &HttpDownstreamConnection::write_first;
461     first_write_done_ = false;
462     request_header_written_ = false;
463   }
464 
465   llhttp_init(&response_htp_, HTTP_RESPONSE, &htp_hooks);
466   response_htp_.data = downstream_;
467 
468   return 0;
469 }
470 
push_request_headers()471 int HttpDownstreamConnection::push_request_headers() {
472   if (request_header_written_) {
473     signal_write();
474     return 0;
475   }
476 
477   const auto &downstream_hostport = addr_->hostport;
478   const auto &req = downstream_->request();
479 
480   auto &balloc = downstream_->get_block_allocator();
481 
482   auto connect_method = req.regular_connect_method();
483 
484   auto config = get_config();
485   auto &httpconf = config->http;
486 
487   request_header_written_ = true;
488 
489   // For HTTP/1.0 request, there is no authority in request.  In that
490   // case, we use backend server's host nonetheless.
491   auto authority = StringRef(downstream_hostport);
492   auto no_host_rewrite =
493       httpconf.no_host_rewrite || config->http2_proxy || connect_method;
494 
495   if (no_host_rewrite && !req.authority.empty()) {
496     authority = req.authority;
497   }
498 
499   downstream_->set_request_downstream_host(authority);
500 
501   auto buf = downstream_->get_request_buf();
502 
503   // Assume that method and request path do not contain \r\n.
504   auto meth = http2::to_method_string(
505       req.connect_proto == ConnectProto::WEBSOCKET ? HTTP_GET : req.method);
506   buf->append(meth);
507   buf->append(' ');
508 
509   if (connect_method) {
510     buf->append(authority);
511   } else if (config->http2_proxy) {
512     // Construct absolute-form request target because we are going to
513     // send a request to a HTTP/1 proxy.
514     assert(!req.scheme.empty());
515     buf->append(req.scheme);
516     buf->append("://");
517     buf->append(authority);
518     buf->append(req.path);
519   } else if (req.method == HTTP_OPTIONS && req.path.empty()) {
520     // Server-wide OPTIONS
521     buf->append("*");
522   } else {
523     buf->append(req.path);
524   }
525   buf->append(" HTTP/1.1\r\nHost: ");
526   buf->append(authority);
527   buf->append("\r\n");
528 
529   auto &fwdconf = httpconf.forwarded;
530   auto &xffconf = httpconf.xff;
531   auto &xfpconf = httpconf.xfp;
532   auto &earlydataconf = httpconf.early_data;
533 
534   uint32_t build_flags =
535       (fwdconf.strip_incoming ? http2::HDOP_STRIP_FORWARDED : 0) |
536       (xffconf.strip_incoming ? http2::HDOP_STRIP_X_FORWARDED_FOR : 0) |
537       (xfpconf.strip_incoming ? http2::HDOP_STRIP_X_FORWARDED_PROTO : 0) |
538       (earlydataconf.strip_incoming ? http2::HDOP_STRIP_EARLY_DATA : 0) |
539       ((req.http_major == 3 || req.http_major == 2)
540            ? http2::HDOP_STRIP_SEC_WEBSOCKET_KEY
541            : 0);
542 
543   http2::build_http1_headers_from_headers(buf, req.fs.headers(), build_flags);
544 
545   auto cookie = downstream_->assemble_request_cookie();
546   if (!cookie.empty()) {
547     buf->append("Cookie: ");
548     buf->append(cookie);
549     buf->append("\r\n");
550   }
551 
552   // set transfer-encoding only when content-length is unknown and
553   // request body is expected.
554   if (req.method != HTTP_CONNECT && req.http2_expect_body &&
555       req.fs.content_length == -1) {
556     downstream_->set_chunked_request(true);
557     buf->append("Transfer-Encoding: chunked\r\n");
558   }
559 
560   if (req.connect_proto == ConnectProto::WEBSOCKET) {
561     if (req.http_major == 3 || req.http_major == 2) {
562       std::array<uint8_t, 16> nonce;
563       if (RAND_bytes(nonce.data(), nonce.size()) != 1) {
564         return -1;
565       }
566       auto iov = make_byte_ref(balloc, base64::encode_length(nonce.size()) + 1);
567       auto p = base64::encode(std::begin(nonce), std::end(nonce), iov.base);
568       *p = '\0';
569       auto key = StringRef{iov.base, p};
570       downstream_->set_ws_key(key);
571 
572       buf->append("Sec-Websocket-Key: ");
573       buf->append(key);
574       buf->append("\r\n");
575     }
576 
577     buf->append("Upgrade: websocket\r\nConnection: Upgrade\r\n");
578   } else if (!connect_method && req.upgrade_request) {
579     auto connection = req.fs.header(http2::HD_CONNECTION);
580     if (connection) {
581       buf->append("Connection: ");
582       buf->append((*connection).value);
583       buf->append("\r\n");
584     }
585 
586     auto upgrade = req.fs.header(http2::HD_UPGRADE);
587     if (upgrade) {
588       buf->append("Upgrade: ");
589       buf->append((*upgrade).value);
590       buf->append("\r\n");
591     }
592   } else if (req.connection_close) {
593     buf->append("Connection: close\r\n");
594   }
595 
596   auto upstream = downstream_->get_upstream();
597   auto handler = upstream->get_client_handler();
598 
599 #if OPENSSL_1_1_1_API
600   auto conn = handler->get_connection();
601 
602   if (conn->tls.ssl && !SSL_is_init_finished(conn->tls.ssl)) {
603     buf->append("Early-Data: 1\r\n");
604   }
605 #endif // OPENSSL_1_1_1_API
606 
607   auto fwd =
608       fwdconf.strip_incoming ? nullptr : req.fs.header(http2::HD_FORWARDED);
609 
610   if (fwdconf.params) {
611     auto params = fwdconf.params;
612 
613     if (config->http2_proxy || connect_method) {
614       params &= ~FORWARDED_PROTO;
615     }
616 
617     auto value = http::create_forwarded(
618         balloc, params, handler->get_forwarded_by(),
619         handler->get_forwarded_for(), req.authority, req.scheme);
620 
621     if (fwd || !value.empty()) {
622       buf->append("Forwarded: ");
623       if (fwd) {
624         buf->append(fwd->value);
625 
626         if (!value.empty()) {
627           buf->append(", ");
628         }
629       }
630       buf->append(value);
631       buf->append("\r\n");
632     }
633   } else if (fwd) {
634     buf->append("Forwarded: ");
635     buf->append(fwd->value);
636     buf->append("\r\n");
637   }
638 
639   auto xff = xffconf.strip_incoming ? nullptr
640                                     : req.fs.header(http2::HD_X_FORWARDED_FOR);
641 
642   if (xffconf.add) {
643     buf->append("X-Forwarded-For: ");
644     if (xff) {
645       buf->append((*xff).value);
646       buf->append(", ");
647     }
648     buf->append(client_handler_->get_ipaddr());
649     buf->append("\r\n");
650   } else if (xff) {
651     buf->append("X-Forwarded-For: ");
652     buf->append((*xff).value);
653     buf->append("\r\n");
654   }
655   if (!config->http2_proxy && !connect_method) {
656     auto xfp = xfpconf.strip_incoming
657                    ? nullptr
658                    : req.fs.header(http2::HD_X_FORWARDED_PROTO);
659 
660     if (xfpconf.add) {
661       buf->append("X-Forwarded-Proto: ");
662       if (xfp) {
663         buf->append((*xfp).value);
664         buf->append(", ");
665       }
666       assert(!req.scheme.empty());
667       buf->append(req.scheme);
668       buf->append("\r\n");
669     } else if (xfp) {
670       buf->append("X-Forwarded-Proto: ");
671       buf->append((*xfp).value);
672       buf->append("\r\n");
673     }
674   }
675   auto via = req.fs.header(http2::HD_VIA);
676   if (httpconf.no_via) {
677     if (via) {
678       buf->append("Via: ");
679       buf->append((*via).value);
680       buf->append("\r\n");
681     }
682   } else {
683     buf->append("Via: ");
684     if (via) {
685       buf->append((*via).value);
686       buf->append(", ");
687     }
688     std::array<char, 16> viabuf;
689     auto end = http::create_via_header_value(viabuf.data(), req.http_major,
690                                              req.http_minor);
691     buf->append(viabuf.data(), end - viabuf.data());
692     buf->append("\r\n");
693   }
694 
695   for (auto &p : httpconf.add_request_headers) {
696     buf->append(p.name);
697     buf->append(": ");
698     buf->append(p.value);
699     buf->append("\r\n");
700   }
701 
702   buf->append("\r\n");
703 
704   if (LOG_ENABLED(INFO)) {
705     std::string nhdrs;
706     for (auto chunk = buf->head; chunk; chunk = chunk->next) {
707       nhdrs.append(chunk->pos, chunk->last);
708     }
709     if (log_config()->errorlog_tty) {
710       nhdrs = http::colorizeHeaders(nhdrs.c_str());
711     }
712     DCLOG(INFO, this) << "HTTP request headers. stream_id="
713                       << downstream_->get_stream_id() << "\n"
714                       << nhdrs;
715   }
716 
717   // Don't call signal_write() if we anticipate request body.  We call
718   // signal_write() when we received request body chunk, and it
719   // enables us to send headers and data in one writev system call.
720   if (req.method == HTTP_CONNECT ||
721       downstream_->get_blocked_request_buf()->rleft() ||
722       (!req.http2_expect_body && req.fs.content_length == 0) ||
723       downstream_->get_expect_100_continue()) {
724     signal_write();
725   }
726 
727   return 0;
728 }
729 
process_blocked_request_buf()730 int HttpDownstreamConnection::process_blocked_request_buf() {
731   auto src = downstream_->get_blocked_request_buf();
732 
733   if (src->rleft()) {
734     auto dest = downstream_->get_request_buf();
735     auto chunked = downstream_->get_chunked_request();
736     if (chunked) {
737       auto chunk_size_hex = util::utox(src->rleft());
738       dest->append(chunk_size_hex);
739       dest->append("\r\n");
740     }
741 
742     src->copy(*dest);
743 
744     if (chunked) {
745       dest->append("\r\n");
746     }
747   }
748 
749   if (downstream_->get_blocked_request_data_eof() &&
750       downstream_->get_chunked_request()) {
751     end_upload_data_chunk();
752   }
753 
754   return 0;
755 }
756 
push_upload_data_chunk(const uint8_t * data,size_t datalen)757 int HttpDownstreamConnection::push_upload_data_chunk(const uint8_t *data,
758                                                      size_t datalen) {
759   if (!downstream_->get_request_header_sent()) {
760     auto output = downstream_->get_blocked_request_buf();
761     auto &req = downstream_->request();
762     output->append(data, datalen);
763     req.unconsumed_body_length += datalen;
764     if (request_header_written_) {
765       signal_write();
766     }
767     return 0;
768   }
769 
770   auto chunked = downstream_->get_chunked_request();
771   auto output = downstream_->get_request_buf();
772 
773   if (chunked) {
774     auto chunk_size_hex = util::utox(datalen);
775     output->append(chunk_size_hex);
776     output->append("\r\n");
777   }
778 
779   output->append(data, datalen);
780 
781   if (chunked) {
782     output->append("\r\n");
783   }
784 
785   signal_write();
786 
787   return 0;
788 }
789 
end_upload_data()790 int HttpDownstreamConnection::end_upload_data() {
791   if (!downstream_->get_request_header_sent()) {
792     downstream_->set_blocked_request_data_eof(true);
793     if (request_header_written_) {
794       signal_write();
795     }
796     return 0;
797   }
798 
799   signal_write();
800 
801   if (!downstream_->get_chunked_request()) {
802     return 0;
803   }
804 
805   end_upload_data_chunk();
806 
807   return 0;
808 }
809 
end_upload_data_chunk()810 void HttpDownstreamConnection::end_upload_data_chunk() {
811   const auto &req = downstream_->request();
812 
813   auto output = downstream_->get_request_buf();
814   const auto &trailers = req.fs.trailers();
815   if (trailers.empty()) {
816     output->append("0\r\n\r\n");
817   } else {
818     output->append("0\r\n");
819     http2::build_http1_headers_from_headers(output, trailers,
820                                             http2::HDOP_STRIP_ALL);
821     output->append("\r\n");
822   }
823 }
824 
825 namespace {
remove_from_pool(HttpDownstreamConnection * dconn)826 void remove_from_pool(HttpDownstreamConnection *dconn) {
827   auto addr = dconn->get_addr();
828   auto &dconn_pool = addr->dconn_pool;
829   dconn_pool->remove_downstream_connection(dconn);
830 }
831 } // namespace
832 
833 namespace {
idle_readcb(struct ev_loop * loop,ev_io * w,int revents)834 void idle_readcb(struct ev_loop *loop, ev_io *w, int revents) {
835   auto conn = static_cast<Connection *>(w->data);
836   auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
837   if (LOG_ENABLED(INFO)) {
838     DCLOG(INFO, dconn) << "Idle connection EOF";
839   }
840 
841   remove_from_pool(dconn);
842   // dconn was deleted
843 }
844 } // namespace
845 
846 namespace {
idle_timeoutcb(struct ev_loop * loop,ev_timer * w,int revents)847 void idle_timeoutcb(struct ev_loop *loop, ev_timer *w, int revents) {
848   auto conn = static_cast<Connection *>(w->data);
849   auto dconn = static_cast<HttpDownstreamConnection *>(conn->data);
850 
851   if (w == &conn->rt && !conn->expired_rt()) {
852     return;
853   }
854 
855   if (LOG_ENABLED(INFO)) {
856     DCLOG(INFO, dconn) << "Idle connection timeout";
857   }
858 
859   remove_from_pool(dconn);
860   // dconn was deleted
861 }
862 } // namespace
863 
detach_downstream(Downstream * downstream)864 void HttpDownstreamConnection::detach_downstream(Downstream *downstream) {
865   if (LOG_ENABLED(INFO)) {
866     DCLOG(INFO, this) << "Detaching from DOWNSTREAM:" << downstream;
867   }
868   downstream_ = nullptr;
869 
870   ev_set_cb(&conn_.rev, idle_readcb);
871   ioctrl_.force_resume_read();
872 
873   auto &downstreamconf = *worker_->get_downstream_config();
874 
875   ev_set_cb(&conn_.rt, idle_timeoutcb);
876   if (conn_.read_timeout < downstreamconf.timeout.idle_read) {
877     conn_.read_timeout = downstreamconf.timeout.idle_read;
878     conn_.last_read = std::chrono::steady_clock::now();
879   } else {
880     conn_.again_rt(downstreamconf.timeout.idle_read);
881   }
882 
883   conn_.wlimit.stopw();
884   ev_timer_stop(conn_.loop, &conn_.wt);
885 }
886 
pause_read(IOCtrlReason reason)887 void HttpDownstreamConnection::pause_read(IOCtrlReason reason) {
888   ioctrl_.pause_read(reason);
889 }
890 
resume_read(IOCtrlReason reason,size_t consumed)891 int HttpDownstreamConnection::resume_read(IOCtrlReason reason,
892                                           size_t consumed) {
893   auto &downstreamconf = *worker_->get_downstream_config();
894 
895   if (downstream_->get_response_buf()->rleft() <=
896       downstreamconf.request_buffer_size / 2) {
897     ioctrl_.resume_read(reason);
898   }
899 
900   return 0;
901 }
902 
force_resume_read()903 void HttpDownstreamConnection::force_resume_read() {
904   ioctrl_.force_resume_read();
905 }
906 
907 namespace {
htp_msg_begincb(llhttp_t * htp)908 int htp_msg_begincb(llhttp_t *htp) {
909   auto downstream = static_cast<Downstream *>(htp->data);
910 
911   if (downstream->get_response_state() != DownstreamState::INITIAL) {
912     return -1;
913   }
914 
915   return 0;
916 }
917 } // namespace
918 
919 namespace {
htp_hdrs_completecb(llhttp_t * htp)920 int htp_hdrs_completecb(llhttp_t *htp) {
921   auto downstream = static_cast<Downstream *>(htp->data);
922   auto upstream = downstream->get_upstream();
923   auto handler = upstream->get_client_handler();
924   const auto &req = downstream->request();
925   auto &resp = downstream->response();
926   int rv;
927 
928   auto &balloc = downstream->get_block_allocator();
929 
930   for (auto &kv : resp.fs.headers()) {
931     kv.value = util::rstrip(balloc, kv.value);
932 
933     if (kv.token == http2::HD_TRANSFER_ENCODING &&
934         !http2::check_transfer_encoding(kv.value)) {
935       return -1;
936     }
937   }
938 
939   auto config = get_config();
940   auto &loggingconf = config->logging;
941 
942   resp.http_status = htp->status_code;
943   resp.http_major = htp->http_major;
944   resp.http_minor = htp->http_minor;
945 
946   if (resp.http_major > 1 || req.http_minor > 1) {
947     resp.http_major = 1;
948     resp.http_minor = 1;
949     return -1;
950   }
951 
952   auto dconn = downstream->get_downstream_connection();
953 
954   downstream->set_downstream_addr_group(dconn->get_downstream_addr_group());
955   downstream->set_addr(dconn->get_addr());
956 
957   // Server MUST NOT send Transfer-Encoding with a status code 1xx or
958   // 204.  Also server MUST NOT send Transfer-Encoding with a status
959   // code 2xx to a CONNECT request.  Same holds true with
960   // Content-Length.
961   if (resp.http_status == 204) {
962     if (resp.fs.header(http2::HD_TRANSFER_ENCODING)) {
963       return -1;
964     }
965     // Some server send content-length: 0 for 204.  Until they get
966     // fixed, we accept, but ignore it.
967 
968     // Calling parse_content_length() detects duplicated
969     // content-length header fields.
970     if (resp.fs.parse_content_length() != 0) {
971       return -1;
972     }
973     if (resp.fs.content_length == 0) {
974       resp.fs.erase_content_length_and_transfer_encoding();
975     } else if (resp.fs.content_length != -1) {
976       return -1;
977     }
978   } else if (resp.http_status / 100 == 1 ||
979              (resp.http_status / 100 == 2 && req.method == HTTP_CONNECT)) {
980     // Server MUST NOT send Content-Length and Transfer-Encoding in
981     // these responses.
982     resp.fs.erase_content_length_and_transfer_encoding();
983   } else if (resp.fs.parse_content_length() != 0) {
984     downstream->set_response_state(DownstreamState::MSG_BAD_HEADER);
985     return -1;
986   }
987 
988   // Check upgrade before processing non-final response, since if
989   // upgrade succeeded, 101 response is treated as final in nghttpx.
990   downstream->check_upgrade_fulfilled_http1();
991 
992   if (downstream->get_non_final_response()) {
993     // Reset content-length because we reuse same Downstream for the
994     // next response.
995     resp.fs.content_length = -1;
996     // For non-final response code, we just call
997     // on_downstream_header_complete() without changing response
998     // state.
999     rv = upstream->on_downstream_header_complete(downstream);
1000 
1001     if (rv != 0) {
1002       return -1;
1003     }
1004 
1005     // Ignore response body for non-final response.
1006     return 1;
1007   }
1008 
1009   resp.connection_close = !llhttp_should_keep_alive(htp);
1010   downstream->set_response_state(DownstreamState::HEADER_COMPLETE);
1011   downstream->inspect_http1_response();
1012 
1013   if (htp->flags & F_CHUNKED) {
1014     downstream->set_chunked_response(true);
1015   }
1016 
1017   auto transfer_encoding = resp.fs.header(http2::HD_TRANSFER_ENCODING);
1018   if (transfer_encoding && !downstream->get_chunked_response()) {
1019     resp.connection_close = true;
1020   }
1021 
1022   if (downstream->get_upgraded()) {
1023     // content-length must be ignored for upgraded connection.
1024     resp.fs.content_length = -1;
1025     resp.connection_close = true;
1026     // transfer-encoding not applied to upgraded connection
1027     downstream->set_chunked_response(false);
1028   } else if (http2::legacy_http1(req.http_major, req.http_minor)) {
1029     if (resp.fs.content_length == -1) {
1030       resp.connection_close = true;
1031     }
1032     downstream->set_chunked_response(false);
1033   } else if (!downstream->expect_response_body()) {
1034     downstream->set_chunked_response(false);
1035   }
1036 
1037   if (loggingconf.access.write_early && downstream->accesslog_ready()) {
1038     handler->write_accesslog(downstream);
1039     downstream->set_accesslog_written(true);
1040   }
1041 
1042   if (upstream->on_downstream_header_complete(downstream) != 0) {
1043     return -1;
1044   }
1045 
1046   if (downstream->get_upgraded()) {
1047     // Upgrade complete, read until EOF in both ends
1048     if (upstream->resume_read(SHRPX_NO_BUFFER, downstream, 0) != 0) {
1049       return -1;
1050     }
1051     downstream->set_request_state(DownstreamState::HEADER_COMPLETE);
1052     if (LOG_ENABLED(INFO)) {
1053       LOG(INFO) << "HTTP upgrade success. stream_id="
1054                 << downstream->get_stream_id();
1055     }
1056   }
1057 
1058   // Ignore the response body. HEAD response may contain
1059   // Content-Length or Transfer-Encoding: chunked.  Some server send
1060   // 304 status code with nonzero Content-Length, but without response
1061   // body. See
1062   // https://tools.ietf.org/html/rfc7230#section-3.3
1063 
1064   // TODO It seems that the cases other than HEAD are handled by
1065   // llhttp.  Need test.
1066   return !http2::expect_response_body(req.method, resp.http_status);
1067 }
1068 } // namespace
1069 
1070 namespace {
ensure_header_field_buffer(const Downstream * downstream,const HttpConfig & httpconf,size_t len)1071 int ensure_header_field_buffer(const Downstream *downstream,
1072                                const HttpConfig &httpconf, size_t len) {
1073   auto &resp = downstream->response();
1074 
1075   if (resp.fs.buffer_size() + len > httpconf.response_header_field_buffer) {
1076     if (LOG_ENABLED(INFO)) {
1077       DLOG(INFO, downstream) << "Too large header header field size="
1078                              << resp.fs.buffer_size() + len;
1079     }
1080     return -1;
1081   }
1082 
1083   return 0;
1084 }
1085 } // namespace
1086 
1087 namespace {
ensure_max_header_fields(const Downstream * downstream,const HttpConfig & httpconf)1088 int ensure_max_header_fields(const Downstream *downstream,
1089                              const HttpConfig &httpconf) {
1090   auto &resp = downstream->response();
1091 
1092   if (resp.fs.num_fields() >= httpconf.max_response_header_fields) {
1093     if (LOG_ENABLED(INFO)) {
1094       DLOG(INFO, downstream)
1095           << "Too many header field num=" << resp.fs.num_fields() + 1;
1096     }
1097     return -1;
1098   }
1099 
1100   return 0;
1101 }
1102 } // namespace
1103 
1104 namespace {
htp_hdr_keycb(llhttp_t * htp,const char * data,size_t len)1105 int htp_hdr_keycb(llhttp_t *htp, const char *data, size_t len) {
1106   auto downstream = static_cast<Downstream *>(htp->data);
1107   auto &resp = downstream->response();
1108   auto &httpconf = get_config()->http;
1109 
1110   if (ensure_header_field_buffer(downstream, httpconf, len) != 0) {
1111     return -1;
1112   }
1113 
1114   if (downstream->get_response_state() == DownstreamState::INITIAL) {
1115     if (resp.fs.header_key_prev()) {
1116       resp.fs.append_last_header_key(data, len);
1117     } else {
1118       if (ensure_max_header_fields(downstream, httpconf) != 0) {
1119         return -1;
1120       }
1121       resp.fs.alloc_add_header_name(StringRef{data, len});
1122     }
1123   } else {
1124     // trailer part
1125     if (resp.fs.trailer_key_prev()) {
1126       resp.fs.append_last_trailer_key(data, len);
1127     } else {
1128       if (ensure_max_header_fields(downstream, httpconf) != 0) {
1129         // Could not ignore this trailer field easily, since we may
1130         // get its value in htp_hdr_valcb, and it will be added to
1131         // wrong place or crash if trailer fields are currently empty.
1132         return -1;
1133       }
1134       resp.fs.alloc_add_trailer_name(StringRef{data, len});
1135     }
1136   }
1137   return 0;
1138 }
1139 } // namespace
1140 
1141 namespace {
htp_hdr_valcb(llhttp_t * htp,const char * data,size_t len)1142 int htp_hdr_valcb(llhttp_t *htp, const char *data, size_t len) {
1143   auto downstream = static_cast<Downstream *>(htp->data);
1144   auto &resp = downstream->response();
1145   auto &httpconf = get_config()->http;
1146 
1147   if (ensure_header_field_buffer(downstream, httpconf, len) != 0) {
1148     return -1;
1149   }
1150 
1151   if (downstream->get_response_state() == DownstreamState::INITIAL) {
1152     resp.fs.append_last_header_value(data, len);
1153   } else {
1154     resp.fs.append_last_trailer_value(data, len);
1155   }
1156   return 0;
1157 }
1158 } // namespace
1159 
1160 namespace {
htp_bodycb(llhttp_t * htp,const char * data,size_t len)1161 int htp_bodycb(llhttp_t *htp, const char *data, size_t len) {
1162   auto downstream = static_cast<Downstream *>(htp->data);
1163   auto &resp = downstream->response();
1164 
1165   resp.recv_body_length += len;
1166 
1167   return downstream->get_upstream()->on_downstream_body(
1168       downstream, reinterpret_cast<const uint8_t *>(data), len, true);
1169 }
1170 } // namespace
1171 
1172 namespace {
htp_msg_completecb(llhttp_t * htp)1173 int htp_msg_completecb(llhttp_t *htp) {
1174   auto downstream = static_cast<Downstream *>(htp->data);
1175   auto &resp = downstream->response();
1176   auto &balloc = downstream->get_block_allocator();
1177 
1178   for (auto &kv : resp.fs.trailers()) {
1179     kv.value = util::rstrip(balloc, kv.value);
1180   }
1181 
1182   // llhttp does not treat "200 connection established" response
1183   // against CONNECT request, and in that case, this function is not
1184   // called.  But if HTTP Upgrade is made (e.g., WebSocket), this
1185   // function is called, and llhttp_execute() returns just after that.
1186   if (downstream->get_upgraded()) {
1187     return 0;
1188   }
1189 
1190   if (downstream->get_non_final_response()) {
1191     downstream->reset_response();
1192 
1193     return 0;
1194   }
1195 
1196   downstream->set_response_state(DownstreamState::MSG_COMPLETE);
1197   // Block reading another response message from (broken?)
1198   // server. This callback is not called if the connection is
1199   // tunneled.
1200   downstream->pause_read(SHRPX_MSG_BLOCK);
1201   return downstream->get_upstream()->on_downstream_body_complete(downstream);
1202 }
1203 } // namespace
1204 
write_first()1205 int HttpDownstreamConnection::write_first() {
1206   int rv;
1207 
1208   process_blocked_request_buf();
1209 
1210   if (conn_.tls.ssl) {
1211     rv = write_tls();
1212   } else {
1213     rv = write_clear();
1214   }
1215 
1216   if (rv != 0) {
1217     return SHRPX_ERR_RETRY;
1218   }
1219 
1220   if (conn_.tls.ssl) {
1221     on_write_ = &HttpDownstreamConnection::write_tls;
1222   } else {
1223     on_write_ = &HttpDownstreamConnection::write_clear;
1224   }
1225 
1226   first_write_done_ = true;
1227   downstream_->set_request_header_sent(true);
1228 
1229   auto buf = downstream_->get_blocked_request_buf();
1230   buf->reset();
1231 
1232   // upstream->resume_read() might be called in
1233   // write_tls()/write_clear(), but before blocked_request_buf_ is
1234   // reset.  So upstream read might still be blocked.  Let's do it
1235   // again here.
1236   auto input = downstream_->get_request_buf();
1237   if (input->rleft() == 0) {
1238     auto upstream = downstream_->get_upstream();
1239     auto &req = downstream_->request();
1240 
1241     upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1242                           req.unconsumed_body_length);
1243   }
1244 
1245   return 0;
1246 }
1247 
read_clear()1248 int HttpDownstreamConnection::read_clear() {
1249   conn_.last_read = std::chrono::steady_clock::now();
1250 
1251   std::array<uint8_t, 16_k> buf;
1252   int rv;
1253 
1254   for (;;) {
1255     auto nread = conn_.read_clear(buf.data(), buf.size());
1256     if (nread == 0) {
1257       return 0;
1258     }
1259 
1260     if (nread < 0) {
1261       if (nread == SHRPX_ERR_EOF && !downstream_->get_upgraded()) {
1262         auto htperr = llhttp_finish(&response_htp_);
1263         if (htperr != HPE_OK) {
1264           if (LOG_ENABLED(INFO)) {
1265             DCLOG(INFO, this) << "HTTP response ended prematurely: "
1266                               << llhttp_errno_name(htperr);
1267           }
1268 
1269           return -1;
1270         }
1271       }
1272 
1273       return nread;
1274     }
1275 
1276     rv = process_input(buf.data(), nread);
1277     if (rv != 0) {
1278       return rv;
1279     }
1280 
1281     if (!ev_is_active(&conn_.rev)) {
1282       return 0;
1283     }
1284   }
1285 }
1286 
write_clear()1287 int HttpDownstreamConnection::write_clear() {
1288   conn_.last_read = std::chrono::steady_clock::now();
1289 
1290   auto upstream = downstream_->get_upstream();
1291   auto input = downstream_->get_request_buf();
1292 
1293   std::array<struct iovec, MAX_WR_IOVCNT> iov;
1294 
1295   while (input->rleft() > 0) {
1296     auto iovcnt = input->riovec(iov.data(), iov.size());
1297 
1298     auto nwrite = conn_.writev_clear(iov.data(), iovcnt);
1299 
1300     if (nwrite == 0) {
1301       return 0;
1302     }
1303 
1304     if (nwrite < 0) {
1305       if (!first_write_done_) {
1306         return nwrite;
1307       }
1308       // We may have pending data in receive buffer which may contain
1309       // part of response body.  So keep reading.  Invoke read event
1310       // to get read(2) error just in case.
1311       ev_feed_event(conn_.loop, &conn_.rev, EV_READ);
1312       on_write_ = &HttpDownstreamConnection::noop;
1313       reusable_ = false;
1314       break;
1315     }
1316 
1317     input->drain(nwrite);
1318   }
1319 
1320   conn_.wlimit.stopw();
1321   ev_timer_stop(conn_.loop, &conn_.wt);
1322 
1323   if (input->rleft() == 0) {
1324     auto &req = downstream_->request();
1325 
1326     upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1327                           req.unconsumed_body_length);
1328   }
1329 
1330   return 0;
1331 }
1332 
tls_handshake()1333 int HttpDownstreamConnection::tls_handshake() {
1334   ERR_clear_error();
1335 
1336   conn_.last_read = std::chrono::steady_clock::now();
1337 
1338   auto rv = conn_.tls_handshake();
1339   if (rv == SHRPX_ERR_INPROGRESS) {
1340     return 0;
1341   }
1342 
1343   if (rv < 0) {
1344     downstream_failure(addr_, raddr_);
1345 
1346     return rv;
1347   }
1348 
1349   if (LOG_ENABLED(INFO)) {
1350     DCLOG(INFO, this) << "SSL/TLS handshake completed";
1351   }
1352 
1353   if (!get_config()->tls.insecure &&
1354       tls::check_cert(conn_.tls.ssl, addr_, raddr_) != 0) {
1355     downstream_failure(addr_, raddr_);
1356 
1357     return -1;
1358   }
1359 
1360   auto &connect_blocker = addr_->connect_blocker;
1361 
1362   signal_write_ = &HttpDownstreamConnection::actual_signal_write;
1363 
1364   connect_blocker->on_success();
1365 
1366   ev_set_cb(&conn_.rt, timeoutcb);
1367   ev_set_cb(&conn_.wt, timeoutcb);
1368 
1369   on_read_ = &HttpDownstreamConnection::read_tls;
1370   on_write_ = &HttpDownstreamConnection::write_first;
1371 
1372   // TODO Check negotiated ALPN
1373 
1374   return on_write();
1375 }
1376 
read_tls()1377 int HttpDownstreamConnection::read_tls() {
1378   conn_.last_read = std::chrono::steady_clock::now();
1379 
1380   ERR_clear_error();
1381 
1382   std::array<uint8_t, 16_k> buf;
1383   int rv;
1384 
1385   for (;;) {
1386     auto nread = conn_.read_tls(buf.data(), buf.size());
1387     if (nread == 0) {
1388       return 0;
1389     }
1390 
1391     if (nread < 0) {
1392       if (nread == SHRPX_ERR_EOF && !downstream_->get_upgraded()) {
1393         auto htperr = llhttp_finish(&response_htp_);
1394         if (htperr != HPE_OK) {
1395           if (LOG_ENABLED(INFO)) {
1396             DCLOG(INFO, this) << "HTTP response ended prematurely: "
1397                               << llhttp_errno_name(htperr);
1398           }
1399 
1400           return -1;
1401         }
1402       }
1403 
1404       return nread;
1405     }
1406 
1407     rv = process_input(buf.data(), nread);
1408     if (rv != 0) {
1409       return rv;
1410     }
1411 
1412     if (!ev_is_active(&conn_.rev)) {
1413       return 0;
1414     }
1415   }
1416 }
1417 
write_tls()1418 int HttpDownstreamConnection::write_tls() {
1419   conn_.last_read = std::chrono::steady_clock::now();
1420 
1421   ERR_clear_error();
1422 
1423   auto upstream = downstream_->get_upstream();
1424   auto input = downstream_->get_request_buf();
1425 
1426   struct iovec iov;
1427 
1428   while (input->rleft() > 0) {
1429     auto iovcnt = input->riovec(&iov, 1);
1430     if (iovcnt != 1) {
1431       assert(0);
1432       return -1;
1433     }
1434     auto nwrite = conn_.write_tls(iov.iov_base, iov.iov_len);
1435 
1436     if (nwrite == 0) {
1437       return 0;
1438     }
1439 
1440     if (nwrite < 0) {
1441       if (!first_write_done_) {
1442         return nwrite;
1443       }
1444       // We may have pending data in receive buffer which may contain
1445       // part of response body.  So keep reading.  Invoke read event
1446       // to get read(2) error just in case.
1447       ev_feed_event(conn_.loop, &conn_.rev, EV_READ);
1448       on_write_ = &HttpDownstreamConnection::noop;
1449       reusable_ = false;
1450       break;
1451     }
1452 
1453     input->drain(nwrite);
1454   }
1455 
1456   conn_.wlimit.stopw();
1457   ev_timer_stop(conn_.loop, &conn_.wt);
1458 
1459   if (input->rleft() == 0) {
1460     auto &req = downstream_->request();
1461 
1462     upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1463                           req.unconsumed_body_length);
1464   }
1465 
1466   return 0;
1467 }
1468 
process_input(const uint8_t * data,size_t datalen)1469 int HttpDownstreamConnection::process_input(const uint8_t *data,
1470                                             size_t datalen) {
1471   int rv;
1472 
1473   if (downstream_->get_upgraded()) {
1474     // For upgraded connection, just pass data to the upstream.
1475     rv = downstream_->get_upstream()->on_downstream_body(downstream_, data,
1476                                                          datalen, true);
1477     if (rv != 0) {
1478       return rv;
1479     }
1480 
1481     if (downstream_->response_buf_full()) {
1482       downstream_->pause_read(SHRPX_NO_BUFFER);
1483       return 0;
1484     }
1485 
1486     return 0;
1487   }
1488 
1489   auto htperr = llhttp_execute(&response_htp_,
1490                                reinterpret_cast<const char *>(data), datalen);
1491   auto nproc =
1492       htperr == HPE_OK
1493           ? datalen
1494           : static_cast<size_t>(reinterpret_cast<const uint8_t *>(
1495                                     llhttp_get_error_pos(&response_htp_)) -
1496                                 data);
1497 
1498   if (htperr != HPE_OK &&
1499       (!downstream_->get_upgraded() || htperr != HPE_PAUSED_UPGRADE)) {
1500     // Handling early return (in other words, response was hijacked by
1501     // mruby scripting).
1502     if (downstream_->get_response_state() == DownstreamState::MSG_COMPLETE) {
1503       return SHRPX_ERR_DCONN_CANCELED;
1504     }
1505 
1506     if (LOG_ENABLED(INFO)) {
1507       DCLOG(INFO, this) << "HTTP parser failure: "
1508                         << "(" << llhttp_errno_name(htperr) << ") "
1509                         << llhttp_get_error_reason(&response_htp_);
1510     }
1511 
1512     return -1;
1513   }
1514 
1515   if (downstream_->get_upgraded()) {
1516     if (nproc < datalen) {
1517       // Data from data + nproc are for upgraded protocol.
1518       rv = downstream_->get_upstream()->on_downstream_body(
1519           downstream_, data + nproc, datalen - nproc, true);
1520       if (rv != 0) {
1521         return rv;
1522       }
1523 
1524       if (downstream_->response_buf_full()) {
1525         downstream_->pause_read(SHRPX_NO_BUFFER);
1526         return 0;
1527       }
1528     }
1529     return 0;
1530   }
1531 
1532   if (downstream_->response_buf_full()) {
1533     downstream_->pause_read(SHRPX_NO_BUFFER);
1534     return 0;
1535   }
1536 
1537   return 0;
1538 }
1539 
connected()1540 int HttpDownstreamConnection::connected() {
1541   auto &connect_blocker = addr_->connect_blocker;
1542 
1543   auto sock_error = util::get_socket_error(conn_.fd);
1544   if (sock_error != 0) {
1545     conn_.wlimit.stopw();
1546 
1547     DCLOG(WARN, this) << "Backend connect failed; addr="
1548                       << util::to_numeric_addr(raddr_)
1549                       << ": errno=" << sock_error;
1550 
1551     downstream_failure(addr_, raddr_);
1552 
1553     return -1;
1554   }
1555 
1556   if (LOG_ENABLED(INFO)) {
1557     DCLOG(INFO, this) << "Connected to downstream host";
1558   }
1559 
1560   // Reset timeout for write.  Previously, we set timeout for connect.
1561   conn_.wt.repeat = group_->shared_addr->timeout.write;
1562   ev_timer_again(conn_.loop, &conn_.wt);
1563 
1564   conn_.rlimit.startw();
1565   conn_.again_rt();
1566 
1567   ev_set_cb(&conn_.wev, writecb);
1568 
1569   if (conn_.tls.ssl) {
1570     on_read_ = &HttpDownstreamConnection::tls_handshake;
1571     on_write_ = &HttpDownstreamConnection::tls_handshake;
1572 
1573     return 0;
1574   }
1575 
1576   signal_write_ = &HttpDownstreamConnection::actual_signal_write;
1577 
1578   connect_blocker->on_success();
1579 
1580   ev_set_cb(&conn_.rt, timeoutcb);
1581   ev_set_cb(&conn_.wt, timeoutcb);
1582 
1583   on_read_ = &HttpDownstreamConnection::read_clear;
1584   on_write_ = &HttpDownstreamConnection::write_first;
1585 
1586   return 0;
1587 }
1588 
on_read()1589 int HttpDownstreamConnection::on_read() { return on_read_(*this); }
1590 
on_write()1591 int HttpDownstreamConnection::on_write() { return on_write_(*this); }
1592 
on_upstream_change(Upstream * upstream)1593 void HttpDownstreamConnection::on_upstream_change(Upstream *upstream) {}
1594 
signal_write()1595 void HttpDownstreamConnection::signal_write() { signal_write_(*this); }
1596 
actual_signal_write()1597 int HttpDownstreamConnection::actual_signal_write() {
1598   ev_feed_event(conn_.loop, &conn_.wev, EV_WRITE);
1599   return 0;
1600 }
1601 
noop()1602 int HttpDownstreamConnection::noop() { return 0; }
1603 
1604 const std::shared_ptr<DownstreamAddrGroup> &
get_downstream_addr_group() const1605 HttpDownstreamConnection::get_downstream_addr_group() const {
1606   return group_;
1607 }
1608 
get_addr() const1609 DownstreamAddr *HttpDownstreamConnection::get_addr() const { return addr_; }
1610 
poolable() const1611 bool HttpDownstreamConnection::poolable() const {
1612   return !group_->retired && reusable_;
1613 }
1614 
get_raddr() const1615 const Address *HttpDownstreamConnection::get_raddr() const { return raddr_; }
1616 
1617 } // namespace shrpx
1618