• 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 = ev_now(conn_.loop);
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 = ev_now(conn_.loop);
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 
934   auto config = get_config();
935   auto &loggingconf = config->logging;
936 
937   resp.http_status = htp->status_code;
938   resp.http_major = htp->http_major;
939   resp.http_minor = htp->http_minor;
940 
941   if (resp.http_major > 1 || req.http_minor > 1) {
942     resp.http_major = 1;
943     resp.http_minor = 1;
944     return -1;
945   }
946 
947   auto dconn = downstream->get_downstream_connection();
948 
949   downstream->set_downstream_addr_group(dconn->get_downstream_addr_group());
950   downstream->set_addr(dconn->get_addr());
951 
952   // Server MUST NOT send Transfer-Encoding with a status code 1xx or
953   // 204.  Also server MUST NOT send Transfer-Encoding with a status
954   // code 2xx to a CONNECT request.  Same holds true with
955   // Content-Length.
956   if (resp.http_status == 204) {
957     if (resp.fs.header(http2::HD_TRANSFER_ENCODING)) {
958       return -1;
959     }
960     // Some server send content-length: 0 for 204.  Until they get
961     // fixed, we accept, but ignore it.
962 
963     // Calling parse_content_length() detects duplicated
964     // content-length header fields.
965     if (resp.fs.parse_content_length() != 0) {
966       return -1;
967     }
968     if (resp.fs.content_length == 0) {
969       resp.fs.erase_content_length_and_transfer_encoding();
970     } else if (resp.fs.content_length != -1) {
971       return -1;
972     }
973   } else if (resp.http_status / 100 == 1 ||
974              (resp.http_status / 100 == 2 && req.method == HTTP_CONNECT)) {
975     // Server MUST NOT send Content-Length and Transfer-Encoding in
976     // these responses.
977     resp.fs.erase_content_length_and_transfer_encoding();
978   } else if (resp.fs.parse_content_length() != 0) {
979     downstream->set_response_state(DownstreamState::MSG_BAD_HEADER);
980     return -1;
981   }
982 
983   // Check upgrade before processing non-final response, since if
984   // upgrade succeeded, 101 response is treated as final in nghttpx.
985   downstream->check_upgrade_fulfilled_http1();
986 
987   if (downstream->get_non_final_response()) {
988     // Reset content-length because we reuse same Downstream for the
989     // next response.
990     resp.fs.content_length = -1;
991     // For non-final response code, we just call
992     // on_downstream_header_complete() without changing response
993     // state.
994     rv = upstream->on_downstream_header_complete(downstream);
995 
996     if (rv != 0) {
997       return -1;
998     }
999 
1000     // Ignore response body for non-final response.
1001     return 1;
1002   }
1003 
1004   resp.connection_close = !llhttp_should_keep_alive(htp);
1005   downstream->set_response_state(DownstreamState::HEADER_COMPLETE);
1006   downstream->inspect_http1_response();
1007   if (downstream->get_upgraded()) {
1008     // content-length must be ignored for upgraded connection.
1009     resp.fs.content_length = -1;
1010     resp.connection_close = true;
1011     // transfer-encoding not applied to upgraded connection
1012     downstream->set_chunked_response(false);
1013   } else if (http2::legacy_http1(req.http_major, req.http_minor)) {
1014     if (resp.fs.content_length == -1) {
1015       resp.connection_close = true;
1016     }
1017     downstream->set_chunked_response(false);
1018   } else if (!downstream->expect_response_body()) {
1019     downstream->set_chunked_response(false);
1020   }
1021 
1022   if (loggingconf.access.write_early && downstream->accesslog_ready()) {
1023     handler->write_accesslog(downstream);
1024     downstream->set_accesslog_written(true);
1025   }
1026 
1027   if (upstream->on_downstream_header_complete(downstream) != 0) {
1028     return -1;
1029   }
1030 
1031   if (downstream->get_upgraded()) {
1032     // Upgrade complete, read until EOF in both ends
1033     if (upstream->resume_read(SHRPX_NO_BUFFER, downstream, 0) != 0) {
1034       return -1;
1035     }
1036     downstream->set_request_state(DownstreamState::HEADER_COMPLETE);
1037     if (LOG_ENABLED(INFO)) {
1038       LOG(INFO) << "HTTP upgrade success. stream_id="
1039                 << downstream->get_stream_id();
1040     }
1041   }
1042 
1043   // Ignore the response body. HEAD response may contain
1044   // Content-Length or Transfer-Encoding: chunked.  Some server send
1045   // 304 status code with nonzero Content-Length, but without response
1046   // body. See
1047   // https://tools.ietf.org/html/rfc7230#section-3.3
1048 
1049   // TODO It seems that the cases other than HEAD are handled by
1050   // llhttp.  Need test.
1051   return !http2::expect_response_body(req.method, resp.http_status);
1052 }
1053 } // namespace
1054 
1055 namespace {
ensure_header_field_buffer(const Downstream * downstream,const HttpConfig & httpconf,size_t len)1056 int ensure_header_field_buffer(const Downstream *downstream,
1057                                const HttpConfig &httpconf, size_t len) {
1058   auto &resp = downstream->response();
1059 
1060   if (resp.fs.buffer_size() + len > httpconf.response_header_field_buffer) {
1061     if (LOG_ENABLED(INFO)) {
1062       DLOG(INFO, downstream) << "Too large header header field size="
1063                              << resp.fs.buffer_size() + len;
1064     }
1065     return -1;
1066   }
1067 
1068   return 0;
1069 }
1070 } // namespace
1071 
1072 namespace {
ensure_max_header_fields(const Downstream * downstream,const HttpConfig & httpconf)1073 int ensure_max_header_fields(const Downstream *downstream,
1074                              const HttpConfig &httpconf) {
1075   auto &resp = downstream->response();
1076 
1077   if (resp.fs.num_fields() >= httpconf.max_response_header_fields) {
1078     if (LOG_ENABLED(INFO)) {
1079       DLOG(INFO, downstream)
1080           << "Too many header field num=" << resp.fs.num_fields() + 1;
1081     }
1082     return -1;
1083   }
1084 
1085   return 0;
1086 }
1087 } // namespace
1088 
1089 namespace {
htp_hdr_keycb(llhttp_t * htp,const char * data,size_t len)1090 int htp_hdr_keycb(llhttp_t *htp, const char *data, size_t len) {
1091   auto downstream = static_cast<Downstream *>(htp->data);
1092   auto &resp = downstream->response();
1093   auto &httpconf = get_config()->http;
1094 
1095   if (ensure_header_field_buffer(downstream, httpconf, len) != 0) {
1096     return -1;
1097   }
1098 
1099   if (downstream->get_response_state() == DownstreamState::INITIAL) {
1100     if (resp.fs.header_key_prev()) {
1101       resp.fs.append_last_header_key(data, len);
1102     } else {
1103       if (ensure_max_header_fields(downstream, httpconf) != 0) {
1104         return -1;
1105       }
1106       resp.fs.alloc_add_header_name(StringRef{data, len});
1107     }
1108   } else {
1109     // trailer part
1110     if (resp.fs.trailer_key_prev()) {
1111       resp.fs.append_last_trailer_key(data, len);
1112     } else {
1113       if (ensure_max_header_fields(downstream, httpconf) != 0) {
1114         // Could not ignore this trailer field easily, since we may
1115         // get its value in htp_hdr_valcb, and it will be added to
1116         // wrong place or crash if trailer fields are currently empty.
1117         return -1;
1118       }
1119       resp.fs.alloc_add_trailer_name(StringRef{data, len});
1120     }
1121   }
1122   return 0;
1123 }
1124 } // namespace
1125 
1126 namespace {
htp_hdr_valcb(llhttp_t * htp,const char * data,size_t len)1127 int htp_hdr_valcb(llhttp_t *htp, const char *data, size_t len) {
1128   auto downstream = static_cast<Downstream *>(htp->data);
1129   auto &resp = downstream->response();
1130   auto &httpconf = get_config()->http;
1131 
1132   if (ensure_header_field_buffer(downstream, httpconf, len) != 0) {
1133     return -1;
1134   }
1135 
1136   if (downstream->get_response_state() == DownstreamState::INITIAL) {
1137     resp.fs.append_last_header_value(data, len);
1138   } else {
1139     resp.fs.append_last_trailer_value(data, len);
1140   }
1141   return 0;
1142 }
1143 } // namespace
1144 
1145 namespace {
htp_bodycb(llhttp_t * htp,const char * data,size_t len)1146 int htp_bodycb(llhttp_t *htp, const char *data, size_t len) {
1147   auto downstream = static_cast<Downstream *>(htp->data);
1148   auto &resp = downstream->response();
1149 
1150   resp.recv_body_length += len;
1151 
1152   return downstream->get_upstream()->on_downstream_body(
1153       downstream, reinterpret_cast<const uint8_t *>(data), len, true);
1154 }
1155 } // namespace
1156 
1157 namespace {
htp_msg_completecb(llhttp_t * htp)1158 int htp_msg_completecb(llhttp_t *htp) {
1159   auto downstream = static_cast<Downstream *>(htp->data);
1160   auto &resp = downstream->response();
1161   auto &balloc = downstream->get_block_allocator();
1162 
1163   for (auto &kv : resp.fs.trailers()) {
1164     kv.value = util::rstrip(balloc, kv.value);
1165   }
1166 
1167   // llhttp does not treat "200 connection established" response
1168   // against CONNECT request, and in that case, this function is not
1169   // called.  But if HTTP Upgrade is made (e.g., WebSocket), this
1170   // function is called, and llhttp_execute() returns just after that.
1171   if (downstream->get_upgraded()) {
1172     return 0;
1173   }
1174 
1175   if (downstream->get_non_final_response()) {
1176     downstream->reset_response();
1177 
1178     return 0;
1179   }
1180 
1181   downstream->set_response_state(DownstreamState::MSG_COMPLETE);
1182   // Block reading another response message from (broken?)
1183   // server. This callback is not called if the connection is
1184   // tunneled.
1185   downstream->pause_read(SHRPX_MSG_BLOCK);
1186   return downstream->get_upstream()->on_downstream_body_complete(downstream);
1187 }
1188 } // namespace
1189 
write_first()1190 int HttpDownstreamConnection::write_first() {
1191   int rv;
1192 
1193   process_blocked_request_buf();
1194 
1195   if (conn_.tls.ssl) {
1196     rv = write_tls();
1197   } else {
1198     rv = write_clear();
1199   }
1200 
1201   if (rv != 0) {
1202     return SHRPX_ERR_RETRY;
1203   }
1204 
1205   if (conn_.tls.ssl) {
1206     on_write_ = &HttpDownstreamConnection::write_tls;
1207   } else {
1208     on_write_ = &HttpDownstreamConnection::write_clear;
1209   }
1210 
1211   first_write_done_ = true;
1212   downstream_->set_request_header_sent(true);
1213 
1214   auto buf = downstream_->get_blocked_request_buf();
1215   buf->reset();
1216 
1217   // upstream->resume_read() might be called in
1218   // write_tls()/write_clear(), but before blocked_request_buf_ is
1219   // reset.  So upstream read might still be blocked.  Let's do it
1220   // again here.
1221   auto input = downstream_->get_request_buf();
1222   if (input->rleft() == 0) {
1223     auto upstream = downstream_->get_upstream();
1224     auto &req = downstream_->request();
1225 
1226     upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1227                           req.unconsumed_body_length);
1228   }
1229 
1230   return 0;
1231 }
1232 
read_clear()1233 int HttpDownstreamConnection::read_clear() {
1234   conn_.last_read = ev_now(conn_.loop);
1235 
1236   std::array<uint8_t, 16_k> buf;
1237   int rv;
1238 
1239   for (;;) {
1240     auto nread = conn_.read_clear(buf.data(), buf.size());
1241     if (nread == 0) {
1242       return 0;
1243     }
1244 
1245     if (nread < 0) {
1246       if (nread == SHRPX_ERR_EOF && !downstream_->get_upgraded()) {
1247         auto htperr = llhttp_finish(&response_htp_);
1248         if (htperr != HPE_OK) {
1249           if (LOG_ENABLED(INFO)) {
1250             DCLOG(INFO, this) << "HTTP response ended prematurely: "
1251                               << llhttp_errno_name(htperr);
1252           }
1253 
1254           return -1;
1255         }
1256       }
1257 
1258       return nread;
1259     }
1260 
1261     rv = process_input(buf.data(), nread);
1262     if (rv != 0) {
1263       return rv;
1264     }
1265 
1266     if (!ev_is_active(&conn_.rev)) {
1267       return 0;
1268     }
1269   }
1270 }
1271 
write_clear()1272 int HttpDownstreamConnection::write_clear() {
1273   conn_.last_read = ev_now(conn_.loop);
1274 
1275   auto upstream = downstream_->get_upstream();
1276   auto input = downstream_->get_request_buf();
1277 
1278   std::array<struct iovec, MAX_WR_IOVCNT> iov;
1279 
1280   while (input->rleft() > 0) {
1281     auto iovcnt = input->riovec(iov.data(), iov.size());
1282 
1283     auto nwrite = conn_.writev_clear(iov.data(), iovcnt);
1284 
1285     if (nwrite == 0) {
1286       return 0;
1287     }
1288 
1289     if (nwrite < 0) {
1290       if (!first_write_done_) {
1291         return nwrite;
1292       }
1293       // We may have pending data in receive buffer which may contain
1294       // part of response body.  So keep reading.  Invoke read event
1295       // to get read(2) error just in case.
1296       ev_feed_event(conn_.loop, &conn_.rev, EV_READ);
1297       on_write_ = &HttpDownstreamConnection::noop;
1298       reusable_ = false;
1299       break;
1300     }
1301 
1302     input->drain(nwrite);
1303   }
1304 
1305   conn_.wlimit.stopw();
1306   ev_timer_stop(conn_.loop, &conn_.wt);
1307 
1308   if (input->rleft() == 0) {
1309     auto &req = downstream_->request();
1310 
1311     upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1312                           req.unconsumed_body_length);
1313   }
1314 
1315   return 0;
1316 }
1317 
tls_handshake()1318 int HttpDownstreamConnection::tls_handshake() {
1319   ERR_clear_error();
1320 
1321   conn_.last_read = ev_now(conn_.loop);
1322 
1323   auto rv = conn_.tls_handshake();
1324   if (rv == SHRPX_ERR_INPROGRESS) {
1325     return 0;
1326   }
1327 
1328   if (rv < 0) {
1329     downstream_failure(addr_, raddr_);
1330 
1331     return rv;
1332   }
1333 
1334   if (LOG_ENABLED(INFO)) {
1335     DCLOG(INFO, this) << "SSL/TLS handshake completed";
1336   }
1337 
1338   if (!get_config()->tls.insecure &&
1339       tls::check_cert(conn_.tls.ssl, addr_, raddr_) != 0) {
1340     downstream_failure(addr_, raddr_);
1341 
1342     return -1;
1343   }
1344 
1345   auto &connect_blocker = addr_->connect_blocker;
1346 
1347   signal_write_ = &HttpDownstreamConnection::actual_signal_write;
1348 
1349   connect_blocker->on_success();
1350 
1351   ev_set_cb(&conn_.rt, timeoutcb);
1352   ev_set_cb(&conn_.wt, timeoutcb);
1353 
1354   on_read_ = &HttpDownstreamConnection::read_tls;
1355   on_write_ = &HttpDownstreamConnection::write_first;
1356 
1357   // TODO Check negotiated ALPN
1358 
1359   return on_write();
1360 }
1361 
read_tls()1362 int HttpDownstreamConnection::read_tls() {
1363   conn_.last_read = ev_now(conn_.loop);
1364 
1365   ERR_clear_error();
1366 
1367   std::array<uint8_t, 16_k> buf;
1368   int rv;
1369 
1370   for (;;) {
1371     auto nread = conn_.read_tls(buf.data(), buf.size());
1372     if (nread == 0) {
1373       return 0;
1374     }
1375 
1376     if (nread < 0) {
1377       if (nread == SHRPX_ERR_EOF && !downstream_->get_upgraded()) {
1378         auto htperr = llhttp_finish(&response_htp_);
1379         if (htperr != HPE_OK) {
1380           if (LOG_ENABLED(INFO)) {
1381             DCLOG(INFO, this) << "HTTP response ended prematurely: "
1382                               << llhttp_errno_name(htperr);
1383           }
1384 
1385           return -1;
1386         }
1387       }
1388 
1389       return nread;
1390     }
1391 
1392     rv = process_input(buf.data(), nread);
1393     if (rv != 0) {
1394       return rv;
1395     }
1396 
1397     if (!ev_is_active(&conn_.rev)) {
1398       return 0;
1399     }
1400   }
1401 }
1402 
write_tls()1403 int HttpDownstreamConnection::write_tls() {
1404   conn_.last_read = ev_now(conn_.loop);
1405 
1406   ERR_clear_error();
1407 
1408   auto upstream = downstream_->get_upstream();
1409   auto input = downstream_->get_request_buf();
1410 
1411   struct iovec iov;
1412 
1413   while (input->rleft() > 0) {
1414     auto iovcnt = input->riovec(&iov, 1);
1415     if (iovcnt != 1) {
1416       assert(0);
1417       return -1;
1418     }
1419     auto nwrite = conn_.write_tls(iov.iov_base, iov.iov_len);
1420 
1421     if (nwrite == 0) {
1422       return 0;
1423     }
1424 
1425     if (nwrite < 0) {
1426       if (!first_write_done_) {
1427         return nwrite;
1428       }
1429       // We may have pending data in receive buffer which may contain
1430       // part of response body.  So keep reading.  Invoke read event
1431       // to get read(2) error just in case.
1432       ev_feed_event(conn_.loop, &conn_.rev, EV_READ);
1433       on_write_ = &HttpDownstreamConnection::noop;
1434       reusable_ = false;
1435       break;
1436     }
1437 
1438     input->drain(nwrite);
1439   }
1440 
1441   conn_.wlimit.stopw();
1442   ev_timer_stop(conn_.loop, &conn_.wt);
1443 
1444   if (input->rleft() == 0) {
1445     auto &req = downstream_->request();
1446 
1447     upstream->resume_read(SHRPX_NO_BUFFER, downstream_,
1448                           req.unconsumed_body_length);
1449   }
1450 
1451   return 0;
1452 }
1453 
process_input(const uint8_t * data,size_t datalen)1454 int HttpDownstreamConnection::process_input(const uint8_t *data,
1455                                             size_t datalen) {
1456   int rv;
1457 
1458   if (downstream_->get_upgraded()) {
1459     // For upgraded connection, just pass data to the upstream.
1460     rv = downstream_->get_upstream()->on_downstream_body(downstream_, data,
1461                                                          datalen, true);
1462     if (rv != 0) {
1463       return rv;
1464     }
1465 
1466     if (downstream_->response_buf_full()) {
1467       downstream_->pause_read(SHRPX_NO_BUFFER);
1468       return 0;
1469     }
1470 
1471     return 0;
1472   }
1473 
1474   auto htperr = llhttp_execute(&response_htp_,
1475                                reinterpret_cast<const char *>(data), datalen);
1476   auto nproc =
1477       htperr == HPE_OK
1478           ? datalen
1479           : static_cast<size_t>(reinterpret_cast<const uint8_t *>(
1480                                     llhttp_get_error_pos(&response_htp_)) -
1481                                 data);
1482 
1483   if (htperr != HPE_OK &&
1484       (!downstream_->get_upgraded() || htperr != HPE_PAUSED_UPGRADE)) {
1485     // Handling early return (in other words, response was hijacked by
1486     // mruby scripting).
1487     if (downstream_->get_response_state() == DownstreamState::MSG_COMPLETE) {
1488       return SHRPX_ERR_DCONN_CANCELED;
1489     }
1490 
1491     if (LOG_ENABLED(INFO)) {
1492       DCLOG(INFO, this) << "HTTP parser failure: "
1493                         << "(" << llhttp_errno_name(htperr) << ") "
1494                         << llhttp_get_error_reason(&response_htp_);
1495     }
1496 
1497     return -1;
1498   }
1499 
1500   if (downstream_->get_upgraded()) {
1501     if (nproc < datalen) {
1502       // Data from data + nproc are for upgraded protocol.
1503       rv = downstream_->get_upstream()->on_downstream_body(
1504           downstream_, data + nproc, datalen - nproc, true);
1505       if (rv != 0) {
1506         return rv;
1507       }
1508 
1509       if (downstream_->response_buf_full()) {
1510         downstream_->pause_read(SHRPX_NO_BUFFER);
1511         return 0;
1512       }
1513     }
1514     return 0;
1515   }
1516 
1517   if (downstream_->response_buf_full()) {
1518     downstream_->pause_read(SHRPX_NO_BUFFER);
1519     return 0;
1520   }
1521 
1522   return 0;
1523 }
1524 
connected()1525 int HttpDownstreamConnection::connected() {
1526   auto &connect_blocker = addr_->connect_blocker;
1527 
1528   auto sock_error = util::get_socket_error(conn_.fd);
1529   if (sock_error != 0) {
1530     conn_.wlimit.stopw();
1531 
1532     DCLOG(WARN, this) << "Backend connect failed; addr="
1533                       << util::to_numeric_addr(raddr_)
1534                       << ": errno=" << sock_error;
1535 
1536     downstream_failure(addr_, raddr_);
1537 
1538     return -1;
1539   }
1540 
1541   if (LOG_ENABLED(INFO)) {
1542     DCLOG(INFO, this) << "Connected to downstream host";
1543   }
1544 
1545   // Reset timeout for write.  Previously, we set timeout for connect.
1546   conn_.wt.repeat = group_->shared_addr->timeout.write;
1547   ev_timer_again(conn_.loop, &conn_.wt);
1548 
1549   conn_.rlimit.startw();
1550   conn_.again_rt();
1551 
1552   ev_set_cb(&conn_.wev, writecb);
1553 
1554   if (conn_.tls.ssl) {
1555     on_read_ = &HttpDownstreamConnection::tls_handshake;
1556     on_write_ = &HttpDownstreamConnection::tls_handshake;
1557 
1558     return 0;
1559   }
1560 
1561   signal_write_ = &HttpDownstreamConnection::actual_signal_write;
1562 
1563   connect_blocker->on_success();
1564 
1565   ev_set_cb(&conn_.rt, timeoutcb);
1566   ev_set_cb(&conn_.wt, timeoutcb);
1567 
1568   on_read_ = &HttpDownstreamConnection::read_clear;
1569   on_write_ = &HttpDownstreamConnection::write_first;
1570 
1571   return 0;
1572 }
1573 
on_read()1574 int HttpDownstreamConnection::on_read() { return on_read_(*this); }
1575 
on_write()1576 int HttpDownstreamConnection::on_write() { return on_write_(*this); }
1577 
on_upstream_change(Upstream * upstream)1578 void HttpDownstreamConnection::on_upstream_change(Upstream *upstream) {}
1579 
signal_write()1580 void HttpDownstreamConnection::signal_write() { signal_write_(*this); }
1581 
actual_signal_write()1582 int HttpDownstreamConnection::actual_signal_write() {
1583   ev_feed_event(conn_.loop, &conn_.wev, EV_WRITE);
1584   return 0;
1585 }
1586 
noop()1587 int HttpDownstreamConnection::noop() { return 0; }
1588 
1589 const std::shared_ptr<DownstreamAddrGroup> &
get_downstream_addr_group() const1590 HttpDownstreamConnection::get_downstream_addr_group() const {
1591   return group_;
1592 }
1593 
get_addr() const1594 DownstreamAddr *HttpDownstreamConnection::get_addr() const { return addr_; }
1595 
poolable() const1596 bool HttpDownstreamConnection::poolable() const {
1597   return !group_->retired && reusable_;
1598 }
1599 
get_raddr() const1600 const Address *HttpDownstreamConnection::get_raddr() const { return raddr_; }
1601 
1602 } // namespace shrpx
1603