Imported Upstream version 1.0.0
[platform/upstream/nghttp2.git] / src / shrpx_spdy_upstream.cc
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_spdy_upstream.h"
26
27 #include <netinet/tcp.h>
28 #include <assert.h>
29 #include <cerrno>
30 #include <sstream>
31
32 #include <nghttp2/nghttp2.h>
33
34 #include "shrpx_client_handler.h"
35 #include "shrpx_downstream.h"
36 #include "shrpx_downstream_connection.h"
37 #include "shrpx_config.h"
38 #include "shrpx_http.h"
39 #include "http2.h"
40 #include "util.h"
41 #include "template.h"
42
43 using namespace nghttp2;
44
45 namespace shrpx {
46
47 namespace {
48 ssize_t send_callback(spdylay_session *session, const uint8_t *data, size_t len,
49                       int flags, void *user_data) {
50   auto upstream = static_cast<SpdyUpstream *>(user_data);
51   auto handler = upstream->get_client_handler();
52   auto wb = handler->get_wb();
53
54   if (wb->wleft() == 0) {
55     return SPDYLAY_ERR_WOULDBLOCK;
56   }
57
58   auto nread = wb->write(data, len);
59
60   return nread;
61 }
62 } // namespace
63
64 namespace {
65 ssize_t recv_callback(spdylay_session *session, uint8_t *buf, size_t len,
66                       int flags, void *user_data) {
67   auto upstream = static_cast<SpdyUpstream *>(user_data);
68   auto handler = upstream->get_client_handler();
69   auto rb = handler->get_rb();
70   auto rlimit = handler->get_rlimit();
71
72   if (rb->rleft() == 0) {
73     return SPDYLAY_ERR_WOULDBLOCK;
74   }
75
76   auto nread = std::min(rb->rleft(), len);
77
78   memcpy(buf, rb->pos, nread);
79   rb->drain(nread);
80   rlimit->startw();
81
82   return nread;
83 }
84 } // namespace
85
86 namespace {
87 void on_stream_close_callback(spdylay_session *session, int32_t stream_id,
88                               spdylay_status_code status_code,
89                               void *user_data) {
90   auto upstream = static_cast<SpdyUpstream *>(user_data);
91   if (LOG_ENABLED(INFO)) {
92     ULOG(INFO, upstream) << "Stream stream_id=" << stream_id
93                          << " is being closed";
94   }
95   auto downstream = static_cast<Downstream *>(
96       spdylay_session_get_stream_user_data(session, stream_id));
97   if (!downstream) {
98     return;
99   }
100
101   upstream->consume(stream_id, downstream->get_request_datalen());
102
103   downstream->reset_request_datalen();
104
105   if (downstream->get_request_state() == Downstream::CONNECT_FAIL) {
106     upstream->remove_downstream(downstream);
107     // downstrea was deleted
108
109     return;
110   }
111
112   downstream->set_request_state(Downstream::STREAM_CLOSED);
113   if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
114     // At this point, downstream response was read
115     if (!downstream->get_upgraded() &&
116         !downstream->get_response_connection_close()) {
117       // Keep-alive
118       downstream->detach_downstream_connection();
119     }
120     upstream->remove_downstream(downstream);
121     // downstrea was deleted
122
123     return;
124   }
125
126   // At this point, downstream read may be paused.
127
128   // If shrpx_downstream::push_request_headers() failed, the
129   // error is handled here.
130   upstream->remove_downstream(downstream);
131   // downstrea was deleted
132
133   // How to test this case? Request sufficient large download
134   // and make client send RST_STREAM after it gets first DATA
135   // frame chunk.
136 }
137 } // namespace
138
139 namespace {
140 void on_ctrl_recv_callback(spdylay_session *session, spdylay_frame_type type,
141                            spdylay_frame *frame, void *user_data) {
142   auto upstream = static_cast<SpdyUpstream *>(user_data);
143   switch (type) {
144   case SPDYLAY_SYN_STREAM: {
145     if (LOG_ENABLED(INFO)) {
146       ULOG(INFO, upstream) << "Received upstream SYN_STREAM stream_id="
147                            << frame->syn_stream.stream_id;
148     }
149
150     auto downstream = upstream->add_pending_downstream(
151         frame->syn_stream.stream_id, frame->syn_stream.pri);
152
153     downstream->reset_upstream_rtimer();
154
155     auto nv = frame->syn_stream.nv;
156
157     if (LOG_ENABLED(INFO)) {
158       std::stringstream ss;
159       for (size_t i = 0; nv[i]; i += 2) {
160         ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i + 1] << "\n";
161       }
162       ULOG(INFO, upstream) << "HTTP request headers. stream_id="
163                            << downstream->get_stream_id() << "\n" << ss.str();
164     }
165
166     size_t num_headers = 0;
167     size_t header_buffer = 0;
168     for (size_t i = 0; nv[i]; i += 2) {
169       ++num_headers;
170       header_buffer += strlen(nv[i]) + strlen(nv[i + 1]);
171     }
172
173     if (header_buffer > get_config()->header_field_buffer ||
174         num_headers > get_config()->max_header_fields) {
175       upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
176       return;
177     }
178
179     for (size_t i = 0; nv[i]; i += 2) {
180       downstream->add_request_header(nv[i], nv[i + 1]);
181     }
182
183     if (downstream->index_request_headers() != 0) {
184       if (upstream->error_reply(downstream, 400) != 0) {
185         ULOG(FATAL, upstream) << "error_reply failed";
186       }
187       return;
188     }
189
190     auto path = downstream->get_request_header(http2::HD__PATH);
191     auto scheme = downstream->get_request_header(http2::HD__SCHEME);
192     auto host = downstream->get_request_header(http2::HD__HOST);
193     auto method = downstream->get_request_header(http2::HD__METHOD);
194
195     bool is_connect = method && "CONNECT" == method->value;
196     if (!path || !host || !method || !http2::non_empty_value(host) ||
197         !http2::non_empty_value(path) || !http2::non_empty_value(method) ||
198         (!is_connect && (!scheme || !http2::non_empty_value(scheme)))) {
199       upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
200       return;
201     }
202
203     downstream->set_request_method(method->value);
204     if (is_connect) {
205       downstream->set_request_http2_authority(path->value);
206     } else {
207       downstream->set_request_http2_scheme(scheme->value);
208       downstream->set_request_http2_authority(host->value);
209       downstream->set_request_path(path->value);
210     }
211
212     if (!(frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN)) {
213       downstream->set_request_http2_expect_body(true);
214     }
215
216     downstream->inspect_http2_request();
217
218     downstream->set_request_state(Downstream::HEADER_COMPLETE);
219     if (frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
220       if (!downstream->validate_request_bodylen()) {
221         upstream->rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR);
222         return;
223       }
224
225       downstream->disable_upstream_rtimer();
226       downstream->set_request_state(Downstream::MSG_COMPLETE);
227     }
228
229     upstream->start_downstream(downstream);
230
231     break;
232   }
233   default:
234     break;
235   }
236 }
237 } // namespace
238
239 void SpdyUpstream::start_downstream(Downstream *downstream) {
240   if (downstream_queue_.can_activate(
241           downstream->get_request_http2_authority())) {
242     initiate_downstream(downstream);
243     return;
244   }
245
246   downstream_queue_.mark_blocked(downstream);
247 }
248
249 void SpdyUpstream::initiate_downstream(Downstream *downstream) {
250   int rv = downstream->attach_downstream_connection(
251       handler_->get_downstream_connection());
252   if (rv != 0) {
253     // If downstream connection fails, issue RST_STREAM.
254     rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
255     downstream->set_request_state(Downstream::CONNECT_FAIL);
256
257     downstream_queue_.mark_failure(downstream);
258
259     return;
260   }
261   rv = downstream->push_request_headers();
262   if (rv != 0) {
263     rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
264
265     downstream_queue_.mark_failure(downstream);
266
267     return;
268   }
269
270   downstream_queue_.mark_active(downstream);
271 }
272
273 namespace {
274 void on_data_chunk_recv_callback(spdylay_session *session, uint8_t flags,
275                                  int32_t stream_id, const uint8_t *data,
276                                  size_t len, void *user_data) {
277   auto upstream = static_cast<SpdyUpstream *>(user_data);
278   auto downstream = static_cast<Downstream *>(
279       spdylay_session_get_stream_user_data(session, stream_id));
280
281   if (!downstream) {
282     upstream->consume(stream_id, len);
283
284     return;
285   }
286
287   downstream->reset_upstream_rtimer();
288
289   if (downstream->push_upload_data_chunk(data, len) != 0) {
290     upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
291
292     upstream->consume(stream_id, len);
293
294     return;
295   }
296
297   if (!upstream->get_flow_control()) {
298     return;
299   }
300
301   // If connection-level window control is not enabled (e.g,
302   // spdy/3), spdylay_session_get_recv_data_length() is always
303   // returns 0.
304   if (spdylay_session_get_recv_data_length(session) >
305       std::max(SPDYLAY_INITIAL_WINDOW_SIZE,
306                1 << get_config()->http2_upstream_connection_window_bits)) {
307     if (LOG_ENABLED(INFO)) {
308       ULOG(INFO, upstream)
309           << "Flow control error on connection: "
310           << "recv_window_size="
311           << spdylay_session_get_recv_data_length(session) << ", window_size="
312           << (1 << get_config()->http2_upstream_connection_window_bits);
313     }
314     spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
315     return;
316   }
317   if (spdylay_session_get_stream_recv_data_length(session, stream_id) >
318       std::max(SPDYLAY_INITIAL_WINDOW_SIZE,
319                1 << get_config()->http2_upstream_window_bits)) {
320     if (LOG_ENABLED(INFO)) {
321       ULOG(INFO, upstream) << "Flow control error: recv_window_size="
322                            << spdylay_session_get_stream_recv_data_length(
323                                   session, stream_id)
324                            << ", initial_window_size="
325                            << (1 << get_config()->http2_upstream_window_bits);
326     }
327     upstream->rst_stream(downstream, SPDYLAY_FLOW_CONTROL_ERROR);
328     return;
329   }
330 }
331 } // namespace
332
333 namespace {
334 void on_data_recv_callback(spdylay_session *session, uint8_t flags,
335                            int32_t stream_id, int32_t length, void *user_data) {
336   auto upstream = static_cast<SpdyUpstream *>(user_data);
337   auto downstream = static_cast<Downstream *>(
338       spdylay_session_get_stream_user_data(session, stream_id));
339   if (downstream && (flags & SPDYLAY_DATA_FLAG_FIN)) {
340     if (!downstream->validate_request_bodylen()) {
341       upstream->rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR);
342       return;
343     }
344
345     downstream->disable_upstream_rtimer();
346     downstream->end_upload_data();
347     downstream->set_request_state(Downstream::MSG_COMPLETE);
348   }
349 }
350 } // namespace
351
352 namespace {
353 void on_ctrl_not_send_callback(spdylay_session *session,
354                                spdylay_frame_type type, spdylay_frame *frame,
355                                int error_code, void *user_data) {
356   auto upstream = static_cast<SpdyUpstream *>(user_data);
357   if (LOG_ENABLED(INFO)) {
358     ULOG(INFO, upstream) << "Failed to send control frame type=" << type
359                          << ", error_code=" << error_code << ":"
360                          << spdylay_strerror(error_code);
361   }
362   if (type == SPDYLAY_SYN_REPLY && error_code != SPDYLAY_ERR_STREAM_CLOSED &&
363       error_code != SPDYLAY_ERR_STREAM_CLOSING) {
364     // To avoid stream hanging around, issue RST_STREAM.
365     auto stream_id = frame->syn_reply.stream_id;
366     // TODO Could be always nullptr
367     auto downstream = static_cast<Downstream *>(
368         spdylay_session_get_stream_user_data(session, stream_id));
369     if (downstream) {
370       upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
371     }
372   }
373 }
374 } // namespace
375
376 namespace {
377 void on_ctrl_recv_parse_error_callback(spdylay_session *session,
378                                        spdylay_frame_type type,
379                                        const uint8_t *head, size_t headlen,
380                                        const uint8_t *payload,
381                                        size_t payloadlen, int error_code,
382                                        void *user_data) {
383   auto upstream = static_cast<SpdyUpstream *>(user_data);
384   if (LOG_ENABLED(INFO)) {
385     ULOG(INFO, upstream) << "Failed to parse received control frame. type="
386                          << type << ", error_code=" << error_code << ":"
387                          << spdylay_strerror(error_code);
388   }
389 }
390 } // namespace
391
392 namespace {
393 void on_unknown_ctrl_recv_callback(spdylay_session *session,
394                                    const uint8_t *head, size_t headlen,
395                                    const uint8_t *payload, size_t payloadlen,
396                                    void *user_data) {
397   auto upstream = static_cast<SpdyUpstream *>(user_data);
398   if (LOG_ENABLED(INFO)) {
399     ULOG(INFO, upstream) << "Received unknown control frame.";
400   }
401 }
402 } // namespace
403
404 namespace {
405 // Infer upstream RST_STREAM status code from downstream HTTP/2
406 // error code.
407 uint32_t infer_upstream_rst_stream_status_code(uint32_t downstream_error_code) {
408   // Only propagate *_REFUSED_STREAM so that upstream client can
409   // resend request.
410   if (downstream_error_code == NGHTTP2_REFUSED_STREAM) {
411     return SPDYLAY_REFUSED_STREAM;
412   } else {
413     return SPDYLAY_INTERNAL_ERROR;
414   }
415 }
416 } // namespace
417
418 SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler)
419     : downstream_queue_(
420           get_config()->http2_proxy
421               ? get_config()->downstream_connections_per_host
422               : get_config()->downstream_proto == PROTO_HTTP
423                     ? get_config()->downstream_connections_per_frontend
424                     : 0,
425           !get_config()->http2_proxy),
426       handler_(handler), session_(nullptr) {
427   spdylay_session_callbacks callbacks;
428   memset(&callbacks, 0, sizeof(callbacks));
429   callbacks.send_callback = send_callback;
430   callbacks.recv_callback = recv_callback;
431   callbacks.on_stream_close_callback = on_stream_close_callback;
432   callbacks.on_ctrl_recv_callback = on_ctrl_recv_callback;
433   callbacks.on_data_chunk_recv_callback = on_data_chunk_recv_callback;
434   callbacks.on_data_recv_callback = on_data_recv_callback;
435   callbacks.on_ctrl_not_send_callback = on_ctrl_not_send_callback;
436   callbacks.on_ctrl_recv_parse_error_callback =
437       on_ctrl_recv_parse_error_callback;
438   callbacks.on_unknown_ctrl_recv_callback = on_unknown_ctrl_recv_callback;
439
440   int rv;
441   rv = spdylay_session_server_new(&session_, version, &callbacks, this);
442   assert(rv == 0);
443
444   uint32_t max_buffer = 65536;
445   rv = spdylay_session_set_option(session_,
446                                   SPDYLAY_OPT_MAX_RECV_CTRL_FRAME_BUFFER,
447                                   &max_buffer, sizeof(max_buffer));
448   assert(rv == 0);
449
450   if (version >= SPDYLAY_PROTO_SPDY3) {
451     int val = 1;
452     flow_control_ = true;
453     initial_window_size_ = 1 << get_config()->http2_upstream_window_bits;
454     rv = spdylay_session_set_option(
455         session_, SPDYLAY_OPT_NO_AUTO_WINDOW_UPDATE2, &val, sizeof(val));
456     assert(rv == 0);
457   } else {
458     flow_control_ = false;
459     initial_window_size_ = 0;
460   }
461   // TODO Maybe call from outside?
462   std::array<spdylay_settings_entry, 2> entry;
463   entry[0].settings_id = SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS;
464   entry[0].value = get_config()->http2_max_concurrent_streams;
465   entry[0].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
466
467   entry[1].settings_id = SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE;
468   entry[1].value = initial_window_size_;
469   entry[1].flags = SPDYLAY_ID_FLAG_SETTINGS_NONE;
470
471   rv = spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE,
472                                entry.data(), entry.size());
473   assert(rv == 0);
474
475   if (version >= SPDYLAY_PROTO_SPDY3_1 &&
476       get_config()->http2_upstream_connection_window_bits > 16) {
477     int32_t delta = (1 << get_config()->http2_upstream_connection_window_bits) -
478                     SPDYLAY_INITIAL_WINDOW_SIZE;
479     rv = spdylay_submit_window_update(session_, 0, delta);
480     assert(rv == 0);
481   }
482
483   handler_->reset_upstream_read_timeout(
484       get_config()->http2_upstream_read_timeout);
485
486   handler_->signal_write();
487 }
488
489 SpdyUpstream::~SpdyUpstream() { spdylay_session_del(session_); }
490
491 int SpdyUpstream::on_read() {
492   int rv = 0;
493
494   rv = spdylay_session_recv(session_);
495   if (rv < 0) {
496     if (rv != SPDYLAY_ERR_EOF) {
497       ULOG(ERROR, this) << "spdylay_session_recv() returned error: "
498                         << spdylay_strerror(rv);
499     }
500     return rv;
501   }
502
503   handler_->signal_write();
504
505   return 0;
506 }
507
508 // After this function call, downstream may be deleted.
509 int SpdyUpstream::on_write() {
510   int rv = 0;
511
512   rv = spdylay_session_send(session_);
513   if (rv != 0) {
514     ULOG(ERROR, this) << "spdylay_session_send() returned error: "
515                       << spdylay_strerror(rv);
516     return rv;
517   }
518
519   if (spdylay_session_want_read(session_) == 0 &&
520       spdylay_session_want_write(session_) == 0 &&
521       handler_->get_wb()->rleft() == 0) {
522     if (LOG_ENABLED(INFO)) {
523       ULOG(INFO, this) << "No more read/write for this SPDY session";
524     }
525     return -1;
526   }
527   return 0;
528 }
529
530 ClientHandler *SpdyUpstream::get_client_handler() const { return handler_; }
531
532 int SpdyUpstream::downstream_read(DownstreamConnection *dconn) {
533   auto downstream = dconn->get_downstream();
534
535   if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
536     // If upstream SPDY stream was closed, we just close downstream,
537     // because there is no consumer now. Downstream connection is also
538     // closed in this case.
539     remove_downstream(downstream);
540     // downstrea was deleted
541
542     return 0;
543   }
544
545   if (downstream->get_response_state() == Downstream::MSG_RESET) {
546     // The downstream stream was reset (canceled). In this case,
547     // RST_STREAM to the upstream and delete downstream connection
548     // here. Deleting downstream will be taken place at
549     // on_stream_close_callback.
550     rst_stream(downstream,
551                infer_upstream_rst_stream_status_code(
552                    downstream->get_response_rst_stream_error_code()));
553     downstream->pop_downstream_connection();
554     dconn = nullptr;
555   } else if (downstream->get_response_state() == Downstream::MSG_BAD_HEADER) {
556     if (error_reply(downstream, 502) != 0) {
557       return -1;
558     }
559     downstream->pop_downstream_connection();
560     // dconn was deleted
561     dconn = nullptr;
562   } else {
563     auto rv = downstream->on_read();
564     if (rv == SHRPX_ERR_EOF) {
565       return downstream_eof(dconn);
566     }
567     if (rv != 0) {
568       if (rv != SHRPX_ERR_NETWORK) {
569         if (LOG_ENABLED(INFO)) {
570           DCLOG(INFO, dconn) << "HTTP parser failure";
571         }
572       }
573       return downstream_error(dconn, Downstream::EVENT_ERROR);
574     }
575     // Detach downstream connection early so that it could be reused
576     // without hitting server's request timeout.
577     if (downstream->get_response_state() == Downstream::MSG_COMPLETE &&
578         !downstream->get_response_connection_close()) {
579       // Keep-alive
580       downstream->detach_downstream_connection();
581     }
582   }
583
584   handler_->signal_write();
585   // At this point, downstream may be deleted.
586
587   return 0;
588 }
589
590 int SpdyUpstream::downstream_write(DownstreamConnection *dconn) {
591   int rv;
592   rv = dconn->on_write();
593   if (rv == SHRPX_ERR_NETWORK) {
594     return downstream_error(dconn, Downstream::EVENT_ERROR);
595   }
596   if (rv != 0) {
597     return -1;
598   }
599   return 0;
600 }
601
602 int SpdyUpstream::downstream_eof(DownstreamConnection *dconn) {
603   auto downstream = dconn->get_downstream();
604
605   if (LOG_ENABLED(INFO)) {
606     DCLOG(INFO, dconn) << "EOF. stream_id=" << downstream->get_stream_id();
607   }
608   if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
609     // If stream was closed already, we don't need to send reply at
610     // the first place. We can delete downstream.
611     remove_downstream(downstream);
612     // downstream was deleted
613
614     return 0;
615   }
616
617   // Delete downstream connection. If we don't delete it here, it will
618   // be pooled in on_stream_close_callback.
619   downstream->pop_downstream_connection();
620   // dconn was deleted
621   dconn = nullptr;
622   // downstream wil be deleted in on_stream_close_callback.
623   if (downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
624     // Server may indicate the end of the request by EOF
625     if (LOG_ENABLED(INFO)) {
626       ULOG(INFO, this) << "Downstream body was ended by EOF";
627     }
628     downstream->set_response_state(Downstream::MSG_COMPLETE);
629
630     // For tunneled connection, MSG_COMPLETE signals
631     // downstream_data_read_callback to send RST_STREAM after pending
632     // response body is sent. This is needed to ensure that RST_STREAM
633     // is sent after all pending data are sent.
634     on_downstream_body_complete(downstream);
635   } else if (downstream->get_response_state() != Downstream::MSG_COMPLETE) {
636     // If stream was not closed, then we set MSG_COMPLETE and let
637     // on_stream_close_callback delete downstream.
638     if (error_reply(downstream, 502) != 0) {
639       return -1;
640     }
641   }
642   handler_->signal_write();
643   // At this point, downstream may be deleted.
644   return 0;
645 }
646
647 int SpdyUpstream::downstream_error(DownstreamConnection *dconn, int events) {
648   auto downstream = dconn->get_downstream();
649
650   if (LOG_ENABLED(INFO)) {
651     if (events & Downstream::EVENT_ERROR) {
652       DCLOG(INFO, dconn) << "Downstream network/general error";
653     } else {
654       DCLOG(INFO, dconn) << "Timeout";
655     }
656     if (downstream->get_upgraded()) {
657       DCLOG(INFO, dconn) << "Note: this is tunnel connection";
658     }
659   }
660
661   if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
662     remove_downstream(downstream);
663     // downstream was deleted
664
665     return 0;
666   }
667
668   // Delete downstream connection. If we don't delete it here, it will
669   // be pooled in on_stream_close_callback.
670   downstream->pop_downstream_connection();
671   // dconn was deleted
672   dconn = nullptr;
673
674   if (downstream->get_response_state() == Downstream::MSG_COMPLETE) {
675     // For SSL tunneling, we issue RST_STREAM. For other types of
676     // stream, we don't have to do anything since response was
677     // complete.
678     if (downstream->get_upgraded()) {
679       // We want "NO_ERROR" error code but SPDY does not have such
680       // code for RST_STREAM.
681       rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
682     }
683   } else {
684     if (downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
685       if (downstream->get_upgraded()) {
686         on_downstream_body_complete(downstream);
687       } else {
688         rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
689       }
690     } else {
691       unsigned int status;
692       if (events & Downstream::EVENT_TIMEOUT) {
693         status = 504;
694       } else {
695         status = 502;
696       }
697       if (error_reply(downstream, status) != 0) {
698         return -1;
699       }
700     }
701     downstream->set_response_state(Downstream::MSG_COMPLETE);
702   }
703   handler_->signal_write();
704   // At this point, downstream may be deleted.
705   return 0;
706 }
707
708 int SpdyUpstream::rst_stream(Downstream *downstream, int status_code) {
709   if (LOG_ENABLED(INFO)) {
710     ULOG(INFO, this) << "RST_STREAM stream_id=" << downstream->get_stream_id();
711   }
712   int rv;
713   rv = spdylay_submit_rst_stream(session_, downstream->get_stream_id(),
714                                  status_code);
715   if (rv < SPDYLAY_ERR_FATAL) {
716     ULOG(FATAL, this) << "spdylay_submit_rst_stream() failed: "
717                       << spdylay_strerror(rv);
718     DIE();
719   }
720   return 0;
721 }
722
723 namespace {
724 ssize_t spdy_data_read_callback(spdylay_session *session, int32_t stream_id,
725                                 uint8_t *buf, size_t length, int *eof,
726                                 spdylay_data_source *source, void *user_data) {
727   auto downstream = static_cast<Downstream *>(source->ptr);
728   auto upstream = static_cast<SpdyUpstream *>(downstream->get_upstream());
729   auto body = downstream->get_response_buf();
730   assert(body);
731
732   auto dconn = downstream->get_downstream_connection();
733
734   if (body->rleft() == 0 && dconn &&
735       downstream->get_response_state() != Downstream::MSG_COMPLETE) {
736     // Try to read more if buffer is empty.  This will help small
737     // buffer and make priority handling a bit better.
738     if (upstream->downstream_read(dconn) != 0) {
739       return SPDYLAY_ERR_CALLBACK_FAILURE;
740     }
741   }
742
743   auto nread = body->remove(buf, length);
744   auto body_empty = body->rleft() == 0;
745
746   if (nread == 0 &&
747       downstream->get_response_state() == Downstream::MSG_COMPLETE) {
748     if (!downstream->get_upgraded()) {
749       *eof = 1;
750     } else {
751       // For tunneling, issue RST_STREAM to finish the stream.
752       if (LOG_ENABLED(INFO)) {
753         ULOG(INFO, upstream)
754             << "RST_STREAM to tunneled stream stream_id=" << stream_id;
755       }
756       upstream->rst_stream(
757           downstream, infer_upstream_rst_stream_status_code(
758                           downstream->get_response_rst_stream_error_code()));
759     }
760   }
761
762   if (body_empty) {
763     downstream->disable_upstream_wtimer();
764   } else {
765     downstream->reset_upstream_wtimer();
766   }
767
768   if (nread > 0 && downstream->resume_read(SHRPX_NO_BUFFER, nread) != 0) {
769     return SPDYLAY_ERR_CALLBACK_FAILURE;
770   }
771
772   if (nread == 0 && *eof != 1) {
773     return SPDYLAY_ERR_DEFERRED;
774   }
775
776   if (nread > 0) {
777     downstream->add_response_sent_bodylen(nread);
778   }
779
780   return nread;
781 }
782 } // namespace
783
784 int SpdyUpstream::error_reply(Downstream *downstream,
785                               unsigned int status_code) {
786   int rv;
787   auto html = http::create_error_html(status_code);
788   downstream->set_response_http_status(status_code);
789   auto body = downstream->get_response_buf();
790   body->append(html.c_str(), html.size());
791   downstream->set_response_state(Downstream::MSG_COMPLETE);
792
793   spdylay_data_provider data_prd;
794   data_prd.source.ptr = downstream;
795   data_prd.read_callback = spdy_data_read_callback;
796
797   std::string content_length = util::utos(html.size());
798   std::string status_string = http2::get_status_string(status_code);
799   const char *nv[] = {":status",        status_string.c_str(),
800                       ":version",       "http/1.1",
801                       "content-type",   "text/html; charset=UTF-8",
802                       "server",         get_config()->server_name,
803                       "content-length", content_length.c_str(),
804                       nullptr};
805
806   rv = spdylay_submit_response(session_, downstream->get_stream_id(), nv,
807                                &data_prd);
808   if (rv < SPDYLAY_ERR_FATAL) {
809     ULOG(FATAL, this) << "spdylay_submit_response() failed: "
810                       << spdylay_strerror(rv);
811     return -1;
812   }
813
814   return 0;
815 }
816
817 Downstream *SpdyUpstream::add_pending_downstream(int32_t stream_id,
818                                                  int32_t priority) {
819   auto downstream = make_unique<Downstream>(this, handler_->get_mcpool(),
820                                             stream_id, priority);
821   spdylay_session_set_stream_user_data(session_, stream_id, downstream.get());
822   auto res = downstream.get();
823
824   downstream_queue_.add_pending(std::move(downstream));
825
826   return res;
827 }
828
829 void SpdyUpstream::remove_downstream(Downstream *downstream) {
830   if (downstream->accesslog_ready()) {
831     handler_->write_accesslog(downstream);
832   }
833
834   spdylay_session_set_stream_user_data(session_, downstream->get_stream_id(),
835                                        nullptr);
836
837   auto next_downstream = downstream_queue_.remove_and_get_blocked(downstream);
838
839   if (next_downstream) {
840     initiate_downstream(next_downstream);
841   }
842 }
843
844 // WARNING: Never call directly or indirectly spdylay_session_send or
845 // spdylay_session_recv. These calls may delete downstream.
846 int SpdyUpstream::on_downstream_header_complete(Downstream *downstream) {
847   if (downstream->get_non_final_response()) {
848     // SPDY does not support non-final response.  We could send it
849     // with HEADERS and final response in SYN_REPLY, but it is not
850     // official way.
851     downstream->clear_response_headers();
852
853     return 0;
854   }
855
856   if (LOG_ENABLED(INFO)) {
857     DLOG(INFO, downstream) << "HTTP response header completed";
858   }
859
860   if (!get_config()->http2_proxy && !get_config()->client_proxy &&
861       !get_config()->no_location_rewrite) {
862     downstream->rewrite_location_response_header(
863         downstream->get_request_http2_scheme());
864   }
865   size_t nheader = downstream->get_response_headers().size();
866   // 8 means server, :status, :version and possible via header field.
867   auto nv = make_unique<const char *[]>(
868       nheader * 2 + 8 + get_config()->add_response_headers.size() * 2 + 1);
869
870   size_t hdidx = 0;
871   std::string via_value;
872   std::string status_string =
873       http2::get_status_string(downstream->get_response_http_status());
874   nv[hdidx++] = ":status";
875   nv[hdidx++] = status_string.c_str();
876   nv[hdidx++] = ":version";
877   nv[hdidx++] = "HTTP/1.1";
878   for (auto &hd : downstream->get_response_headers()) {
879     if (hd.name.empty() || hd.name.c_str()[0] == ':') {
880       continue;
881     }
882     switch (hd.token) {
883     case http2::HD_CONNECTION:
884     case http2::HD_KEEP_ALIVE:
885     case http2::HD_PROXY_CONNECTION:
886     case http2::HD_TRANSFER_ENCODING:
887     case http2::HD_VIA:
888     case http2::HD_SERVER:
889       continue;
890     }
891
892     nv[hdidx++] = hd.name.c_str();
893     nv[hdidx++] = hd.value.c_str();
894   }
895
896   if (!get_config()->http2_proxy && !get_config()->client_proxy) {
897     nv[hdidx++] = "server";
898     nv[hdidx++] = get_config()->server_name;
899   } else {
900     auto server = downstream->get_response_header(http2::HD_SERVER);
901     if (server) {
902       nv[hdidx++] = "server";
903       nv[hdidx++] = server->value.c_str();
904     }
905   }
906
907   auto via = downstream->get_response_header(http2::HD_VIA);
908   if (get_config()->no_via) {
909     if (via) {
910       nv[hdidx++] = "via";
911       nv[hdidx++] = via->value.c_str();
912     }
913   } else {
914     if (via) {
915       via_value = via->value;
916       via_value += ", ";
917     }
918     via_value += http::create_via_header_value(
919         downstream->get_response_major(), downstream->get_response_minor());
920     nv[hdidx++] = "via";
921     nv[hdidx++] = via_value.c_str();
922   }
923
924   for (auto &p : get_config()->add_response_headers) {
925     nv[hdidx++] = p.first.c_str();
926     nv[hdidx++] = p.second.c_str();
927   }
928
929   nv[hdidx++] = 0;
930   if (LOG_ENABLED(INFO)) {
931     std::stringstream ss;
932     for (size_t i = 0; nv[i]; i += 2) {
933       ss << TTY_HTTP_HD << nv[i] << TTY_RST << ": " << nv[i + 1] << "\n";
934     }
935     ULOG(INFO, this) << "HTTP response headers. stream_id="
936                      << downstream->get_stream_id() << "\n" << ss.str();
937   }
938   spdylay_data_provider data_prd;
939   data_prd.source.ptr = downstream;
940   data_prd.read_callback = spdy_data_read_callback;
941
942   int rv;
943   rv = spdylay_submit_response(session_, downstream->get_stream_id(), nv.get(),
944                                &data_prd);
945   if (rv != 0) {
946     ULOG(FATAL, this) << "spdylay_submit_response() failed";
947     return -1;
948   }
949
950   return 0;
951 }
952
953 // WARNING: Never call directly or indirectly spdylay_session_send or
954 // spdylay_session_recv. These calls may delete downstream.
955 int SpdyUpstream::on_downstream_body(Downstream *downstream,
956                                      const uint8_t *data, size_t len,
957                                      bool flush) {
958   auto body = downstream->get_response_buf();
959   body->append(data, len);
960
961   if (flush) {
962     spdylay_session_resume_data(session_, downstream->get_stream_id());
963
964     downstream->ensure_upstream_wtimer();
965   }
966
967   return 0;
968 }
969
970 // WARNING: Never call directly or indirectly spdylay_session_send or
971 // spdylay_session_recv. These calls may delete downstream.
972 int SpdyUpstream::on_downstream_body_complete(Downstream *downstream) {
973   if (LOG_ENABLED(INFO)) {
974     DLOG(INFO, downstream) << "HTTP response completed";
975   }
976
977   if (!downstream->validate_response_bodylen()) {
978     rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR);
979     downstream->set_response_connection_close(true);
980     return 0;
981   }
982
983   spdylay_session_resume_data(session_, downstream->get_stream_id());
984   downstream->ensure_upstream_wtimer();
985
986   return 0;
987 }
988
989 bool SpdyUpstream::get_flow_control() const { return flow_control_; }
990
991 void SpdyUpstream::pause_read(IOCtrlReason reason) {}
992
993 int SpdyUpstream::resume_read(IOCtrlReason reason, Downstream *downstream,
994                               size_t consumed) {
995   if (get_flow_control()) {
996     assert(downstream->get_request_datalen() >= consumed);
997
998     if (consume(downstream->get_stream_id(), consumed) != 0) {
999       return -1;
1000     }
1001
1002     downstream->dec_request_datalen(consumed);
1003   }
1004
1005   handler_->signal_write();
1006   return 0;
1007 }
1008
1009 int SpdyUpstream::on_downstream_abort_request(Downstream *downstream,
1010                                               unsigned int status_code) {
1011   int rv;
1012
1013   rv = error_reply(downstream, status_code);
1014
1015   if (rv != 0) {
1016     return -1;
1017   }
1018
1019   handler_->signal_write();
1020   return 0;
1021 }
1022
1023 int SpdyUpstream::consume(int32_t stream_id, size_t len) {
1024   int rv;
1025
1026   rv = spdylay_session_consume(session_, stream_id, len);
1027
1028   if (rv != 0) {
1029     ULOG(WARN, this) << "spdylay_session_consume() returned error: "
1030                      << spdylay_strerror(rv);
1031     return -1;
1032   }
1033
1034   return 0;
1035 }
1036
1037 int SpdyUpstream::on_timeout(Downstream *downstream) {
1038   if (LOG_ENABLED(INFO)) {
1039     ULOG(INFO, this) << "Stream timeout stream_id="
1040                      << downstream->get_stream_id();
1041   }
1042
1043   rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
1044
1045   return 0;
1046 }
1047
1048 void SpdyUpstream::on_handler_delete() {
1049   for (auto d = downstream_queue_.get_downstreams(); d; d = d->dlnext) {
1050     if (d->get_dispatch_state() == Downstream::DISPATCH_ACTIVE &&
1051         d->accesslog_ready()) {
1052       handler_->write_accesslog(d);
1053     }
1054   }
1055 }
1056
1057 int SpdyUpstream::on_downstream_reset(bool no_retry) {
1058   int rv;
1059
1060   for (auto downstream = downstream_queue_.get_downstreams(); downstream;
1061        downstream = downstream->dlnext) {
1062     if (downstream->get_dispatch_state() != Downstream::DISPATCH_ACTIVE) {
1063       continue;
1064     }
1065
1066     if (!downstream->request_submission_ready()) {
1067       rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
1068       downstream->pop_downstream_connection();
1069       continue;
1070     }
1071
1072     downstream->pop_downstream_connection();
1073
1074     downstream->add_retry();
1075
1076     if (no_retry || downstream->no_more_retry()) {
1077       goto fail;
1078     }
1079
1080     // downstream connection is clean; we can retry with new
1081     // downstream connection.
1082
1083     rv = downstream->attach_downstream_connection(
1084         handler_->get_downstream_connection());
1085     if (rv != 0) {
1086       goto fail;
1087     }
1088
1089     continue;
1090
1091   fail:
1092     if (on_downstream_abort_request(downstream, 503) != 0) {
1093       return -1;
1094     }
1095     downstream->pop_downstream_connection();
1096   }
1097
1098   handler_->signal_write();
1099
1100   return 0;
1101 }
1102
1103 } // namespace shrpx