2 * nghttp2 - HTTP/2 C Library
4 * Copyright (c) 2012 Tatsuhiro Tsujikawa
6 * Permission is hereby granted, free of charge, to any person obtaining
7 * a copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sublicense, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
14 * The above copyright notice and this permission notice shall be
15 * included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
21 * LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
22 * OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
23 * WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 #include "shrpx_spdy_upstream.h"
27 #include <netinet/tcp.h>
32 #include <nghttp2/nghttp2.h>
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"
43 using namespace nghttp2;
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();
54 if (wb->wleft() == 0) {
55 return SPDYLAY_ERR_WOULDBLOCK;
58 auto nread = wb->write(data, len);
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();
72 if (rb->rleft() == 0) {
73 return SPDYLAY_ERR_WOULDBLOCK;
76 auto nread = std::min(rb->rleft(), len);
78 memcpy(buf, rb->pos, nread);
87 void on_stream_close_callback(spdylay_session *session, int32_t stream_id,
88 spdylay_status_code status_code,
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";
95 auto downstream = static_cast<Downstream *>(
96 spdylay_session_get_stream_user_data(session, stream_id));
101 upstream->consume(stream_id, downstream->get_request_datalen());
103 downstream->reset_request_datalen();
105 if (downstream->get_request_state() == Downstream::CONNECT_FAIL) {
106 upstream->remove_downstream(downstream);
107 // downstrea was deleted
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()) {
118 downstream->detach_downstream_connection();
120 upstream->remove_downstream(downstream);
121 // downstrea was deleted
126 // At this point, downstream read may be paused.
128 // If shrpx_downstream::push_request_headers() failed, the
129 // error is handled here.
130 upstream->remove_downstream(downstream);
131 // downstrea was deleted
133 // How to test this case? Request sufficient large download
134 // and make client send RST_STREAM after it gets first DATA
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);
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;
150 auto downstream = upstream->add_pending_downstream(
151 frame->syn_stream.stream_id, frame->syn_stream.pri);
153 downstream->reset_upstream_rtimer();
155 auto nv = frame->syn_stream.nv;
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";
162 ULOG(INFO, upstream) << "HTTP request headers. stream_id="
163 << downstream->get_stream_id() << "\n" << ss.str();
166 size_t num_headers = 0;
167 size_t header_buffer = 0;
168 for (size_t i = 0; nv[i]; i += 2) {
170 header_buffer += strlen(nv[i]) + strlen(nv[i + 1]);
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);
179 for (size_t i = 0; nv[i]; i += 2) {
180 downstream->add_request_header(nv[i], nv[i + 1]);
183 if (downstream->index_request_headers() != 0) {
184 if (upstream->error_reply(downstream, 400) != 0) {
185 ULOG(FATAL, upstream) << "error_reply failed";
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);
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);
203 downstream->set_request_method(method->value);
205 downstream->set_request_http2_authority(path->value);
207 downstream->set_request_http2_scheme(scheme->value);
208 downstream->set_request_http2_authority(host->value);
209 downstream->set_request_path(path->value);
212 if (!(frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN)) {
213 downstream->set_request_http2_expect_body(true);
216 downstream->inspect_http2_request();
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);
225 downstream->disable_upstream_rtimer();
226 downstream->set_request_state(Downstream::MSG_COMPLETE);
229 upstream->start_downstream(downstream);
239 void SpdyUpstream::start_downstream(Downstream *downstream) {
240 if (downstream_queue_.can_activate(
241 downstream->get_request_http2_authority())) {
242 initiate_downstream(downstream);
246 downstream_queue_.mark_blocked(downstream);
249 void SpdyUpstream::initiate_downstream(Downstream *downstream) {
250 int rv = downstream->attach_downstream_connection(
251 handler_->get_downstream_connection());
253 // If downstream connection fails, issue RST_STREAM.
254 rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
255 downstream->set_request_state(Downstream::CONNECT_FAIL);
257 downstream_queue_.mark_failure(downstream);
261 rv = downstream->push_request_headers();
263 rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
265 downstream_queue_.mark_failure(downstream);
270 downstream_queue_.mark_active(downstream);
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));
282 upstream->consume(stream_id, len);
287 downstream->reset_upstream_rtimer();
289 if (downstream->push_upload_data_chunk(data, len) != 0) {
290 upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
292 upstream->consume(stream_id, len);
297 if (!upstream->get_flow_control()) {
301 // If connection-level window control is not enabled (e.g,
302 // spdy/3), spdylay_session_get_recv_data_length() is always
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)) {
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);
314 spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
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(
324 << ", initial_window_size="
325 << (1 << get_config()->http2_upstream_window_bits);
327 upstream->rst_stream(downstream, SPDYLAY_FLOW_CONTROL_ERROR);
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);
345 downstream->disable_upstream_rtimer();
346 downstream->end_upload_data();
347 downstream->set_request_state(Downstream::MSG_COMPLETE);
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);
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));
370 upstream->rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
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,
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);
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,
397 auto upstream = static_cast<SpdyUpstream *>(user_data);
398 if (LOG_ENABLED(INFO)) {
399 ULOG(INFO, upstream) << "Received unknown control frame.";
405 // Infer upstream RST_STREAM status code from downstream HTTP/2
407 uint32_t infer_upstream_rst_stream_status_code(uint32_t downstream_error_code) {
408 // Only propagate *_REFUSED_STREAM so that upstream client can
410 if (downstream_error_code == NGHTTP2_REFUSED_STREAM) {
411 return SPDYLAY_REFUSED_STREAM;
413 return SPDYLAY_INTERNAL_ERROR;
418 SpdyUpstream::SpdyUpstream(uint16_t version, ClientHandler *handler)
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
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;
441 rv = spdylay_session_server_new(&session_, version, &callbacks, this);
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));
450 if (version >= SPDYLAY_PROTO_SPDY3) {
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));
458 flow_control_ = false;
459 initial_window_size_ = 0;
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;
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;
471 rv = spdylay_submit_settings(session_, SPDYLAY_FLAG_SETTINGS_NONE,
472 entry.data(), entry.size());
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);
483 handler_->reset_upstream_read_timeout(
484 get_config()->http2_upstream_read_timeout);
486 handler_->signal_write();
489 SpdyUpstream::~SpdyUpstream() { spdylay_session_del(session_); }
491 int SpdyUpstream::on_read() {
494 rv = spdylay_session_recv(session_);
496 if (rv != SPDYLAY_ERR_EOF) {
497 ULOG(ERROR, this) << "spdylay_session_recv() returned error: "
498 << spdylay_strerror(rv);
503 handler_->signal_write();
508 // After this function call, downstream may be deleted.
509 int SpdyUpstream::on_write() {
512 rv = spdylay_session_send(session_);
514 ULOG(ERROR, this) << "spdylay_session_send() returned error: "
515 << spdylay_strerror(rv);
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";
530 ClientHandler *SpdyUpstream::get_client_handler() const { return handler_; }
532 int SpdyUpstream::downstream_read(DownstreamConnection *dconn) {
533 auto downstream = dconn->get_downstream();
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
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();
555 } else if (downstream->get_response_state() == Downstream::MSG_BAD_HEADER) {
556 if (error_reply(downstream, 502) != 0) {
559 downstream->pop_downstream_connection();
563 auto rv = downstream->on_read();
564 if (rv == SHRPX_ERR_EOF) {
565 return downstream_eof(dconn);
568 if (rv != SHRPX_ERR_NETWORK) {
569 if (LOG_ENABLED(INFO)) {
570 DCLOG(INFO, dconn) << "HTTP parser failure";
573 return downstream_error(dconn, Downstream::EVENT_ERROR);
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()) {
580 downstream->detach_downstream_connection();
584 handler_->signal_write();
585 // At this point, downstream may be deleted.
590 int SpdyUpstream::downstream_write(DownstreamConnection *dconn) {
592 rv = dconn->on_write();
593 if (rv == SHRPX_ERR_NETWORK) {
594 return downstream_error(dconn, Downstream::EVENT_ERROR);
602 int SpdyUpstream::downstream_eof(DownstreamConnection *dconn) {
603 auto downstream = dconn->get_downstream();
605 if (LOG_ENABLED(INFO)) {
606 DCLOG(INFO, dconn) << "EOF. stream_id=" << downstream->get_stream_id();
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
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();
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";
628 downstream->set_response_state(Downstream::MSG_COMPLETE);
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) {
642 handler_->signal_write();
643 // At this point, downstream may be deleted.
647 int SpdyUpstream::downstream_error(DownstreamConnection *dconn, int events) {
648 auto downstream = dconn->get_downstream();
650 if (LOG_ENABLED(INFO)) {
651 if (events & Downstream::EVENT_ERROR) {
652 DCLOG(INFO, dconn) << "Downstream network/general error";
654 DCLOG(INFO, dconn) << "Timeout";
656 if (downstream->get_upgraded()) {
657 DCLOG(INFO, dconn) << "Note: this is tunnel connection";
661 if (downstream->get_request_state() == Downstream::STREAM_CLOSED) {
662 remove_downstream(downstream);
663 // downstream was deleted
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();
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
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);
684 if (downstream->get_response_state() == Downstream::HEADER_COMPLETE) {
685 if (downstream->get_upgraded()) {
686 on_downstream_body_complete(downstream);
688 rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
692 if (events & Downstream::EVENT_TIMEOUT) {
697 if (error_reply(downstream, status) != 0) {
701 downstream->set_response_state(Downstream::MSG_COMPLETE);
703 handler_->signal_write();
704 // At this point, downstream may be deleted.
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();
713 rv = spdylay_submit_rst_stream(session_, downstream->get_stream_id(),
715 if (rv < SPDYLAY_ERR_FATAL) {
716 ULOG(FATAL, this) << "spdylay_submit_rst_stream() failed: "
717 << spdylay_strerror(rv);
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();
732 auto dconn = downstream->get_downstream_connection();
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;
743 auto nread = body->remove(buf, length);
744 auto body_empty = body->rleft() == 0;
747 downstream->get_response_state() == Downstream::MSG_COMPLETE) {
748 if (!downstream->get_upgraded()) {
751 // For tunneling, issue RST_STREAM to finish the stream.
752 if (LOG_ENABLED(INFO)) {
754 << "RST_STREAM to tunneled stream stream_id=" << stream_id;
756 upstream->rst_stream(
757 downstream, infer_upstream_rst_stream_status_code(
758 downstream->get_response_rst_stream_error_code()));
763 downstream->disable_upstream_wtimer();
765 downstream->reset_upstream_wtimer();
768 if (nread > 0 && downstream->resume_read(SHRPX_NO_BUFFER, nread) != 0) {
769 return SPDYLAY_ERR_CALLBACK_FAILURE;
772 if (nread == 0 && *eof != 1) {
773 return SPDYLAY_ERR_DEFERRED;
777 downstream->add_response_sent_bodylen(nread);
784 int SpdyUpstream::error_reply(Downstream *downstream,
785 unsigned int status_code) {
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);
793 spdylay_data_provider data_prd;
794 data_prd.source.ptr = downstream;
795 data_prd.read_callback = spdy_data_read_callback;
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(),
806 rv = spdylay_submit_response(session_, downstream->get_stream_id(), nv,
808 if (rv < SPDYLAY_ERR_FATAL) {
809 ULOG(FATAL, this) << "spdylay_submit_response() failed: "
810 << spdylay_strerror(rv);
817 Downstream *SpdyUpstream::add_pending_downstream(int32_t stream_id,
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();
824 downstream_queue_.add_pending(std::move(downstream));
829 void SpdyUpstream::remove_downstream(Downstream *downstream) {
830 if (downstream->accesslog_ready()) {
831 handler_->write_accesslog(downstream);
834 spdylay_session_set_stream_user_data(session_, downstream->get_stream_id(),
837 auto next_downstream = downstream_queue_.remove_and_get_blocked(downstream);
839 if (next_downstream) {
840 initiate_downstream(next_downstream);
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
851 downstream->clear_response_headers();
856 if (LOG_ENABLED(INFO)) {
857 DLOG(INFO, downstream) << "HTTP response header completed";
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());
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);
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] == ':') {
883 case http2::HD_CONNECTION:
884 case http2::HD_KEEP_ALIVE:
885 case http2::HD_PROXY_CONNECTION:
886 case http2::HD_TRANSFER_ENCODING:
888 case http2::HD_SERVER:
892 nv[hdidx++] = hd.name.c_str();
893 nv[hdidx++] = hd.value.c_str();
896 if (!get_config()->http2_proxy && !get_config()->client_proxy) {
897 nv[hdidx++] = "server";
898 nv[hdidx++] = get_config()->server_name;
900 auto server = downstream->get_response_header(http2::HD_SERVER);
902 nv[hdidx++] = "server";
903 nv[hdidx++] = server->value.c_str();
907 auto via = downstream->get_response_header(http2::HD_VIA);
908 if (get_config()->no_via) {
911 nv[hdidx++] = via->value.c_str();
915 via_value = via->value;
918 via_value += http::create_via_header_value(
919 downstream->get_response_major(), downstream->get_response_minor());
921 nv[hdidx++] = via_value.c_str();
924 for (auto &p : get_config()->add_response_headers) {
925 nv[hdidx++] = p.first.c_str();
926 nv[hdidx++] = p.second.c_str();
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";
935 ULOG(INFO, this) << "HTTP response headers. stream_id="
936 << downstream->get_stream_id() << "\n" << ss.str();
938 spdylay_data_provider data_prd;
939 data_prd.source.ptr = downstream;
940 data_prd.read_callback = spdy_data_read_callback;
943 rv = spdylay_submit_response(session_, downstream->get_stream_id(), nv.get(),
946 ULOG(FATAL, this) << "spdylay_submit_response() failed";
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,
958 auto body = downstream->get_response_buf();
959 body->append(data, len);
962 spdylay_session_resume_data(session_, downstream->get_stream_id());
964 downstream->ensure_upstream_wtimer();
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";
977 if (!downstream->validate_response_bodylen()) {
978 rst_stream(downstream, SPDYLAY_PROTOCOL_ERROR);
979 downstream->set_response_connection_close(true);
983 spdylay_session_resume_data(session_, downstream->get_stream_id());
984 downstream->ensure_upstream_wtimer();
989 bool SpdyUpstream::get_flow_control() const { return flow_control_; }
991 void SpdyUpstream::pause_read(IOCtrlReason reason) {}
993 int SpdyUpstream::resume_read(IOCtrlReason reason, Downstream *downstream,
995 if (get_flow_control()) {
996 assert(downstream->get_request_datalen() >= consumed);
998 if (consume(downstream->get_stream_id(), consumed) != 0) {
1002 downstream->dec_request_datalen(consumed);
1005 handler_->signal_write();
1009 int SpdyUpstream::on_downstream_abort_request(Downstream *downstream,
1010 unsigned int status_code) {
1013 rv = error_reply(downstream, status_code);
1019 handler_->signal_write();
1023 int SpdyUpstream::consume(int32_t stream_id, size_t len) {
1026 rv = spdylay_session_consume(session_, stream_id, len);
1029 ULOG(WARN, this) << "spdylay_session_consume() returned error: "
1030 << spdylay_strerror(rv);
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();
1043 rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
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);
1057 int SpdyUpstream::on_downstream_reset(bool no_retry) {
1060 for (auto downstream = downstream_queue_.get_downstreams(); downstream;
1061 downstream = downstream->dlnext) {
1062 if (downstream->get_dispatch_state() != Downstream::DISPATCH_ACTIVE) {
1066 if (!downstream->request_submission_ready()) {
1067 rst_stream(downstream, SPDYLAY_INTERNAL_ERROR);
1068 downstream->pop_downstream_connection();
1072 downstream->pop_downstream_connection();
1074 downstream->add_retry();
1076 if (no_retry || downstream->no_more_retry()) {
1080 // downstream connection is clean; we can retry with new
1081 // downstream connection.
1083 rv = downstream->attach_downstream_connection(
1084 handler_->get_downstream_connection());
1092 if (on_downstream_abort_request(downstream, 503) != 0) {
1095 downstream->pop_downstream_connection();
1098 handler_->signal_write();
1103 } // namespace shrpx