2012-01-24 14:02:24 +01:00
|
|
|
/*
|
2014-03-30 12:09:21 +02:00
|
|
|
* nghttp2 - HTTP/2 C Library
|
2012-01-24 14:02:24 +01:00
|
|
|
*
|
|
|
|
* Copyright (c) 2012 Tatsuhiro Tsujikawa
|
|
|
|
*
|
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
* the following conditions:
|
|
|
|
*
|
|
|
|
* The above copyright notice and this permission notice shall be
|
|
|
|
* included in all copies or substantial portions of the Software.
|
|
|
|
*
|
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
|
|
|
|
* NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
|
|
|
|
* LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
|
|
|
|
* OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
|
|
|
|
* WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
|
|
|
*/
|
2013-07-12 17:19:03 +02:00
|
|
|
#include "nghttp2_session.h"
|
2012-01-24 14:02:24 +01:00
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
2016-03-13 10:21:07 +01:00
|
|
|
#include <stdarg.h>
|
2012-01-24 14:02:24 +01:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
#include "nghttp2_helper.h"
|
|
|
|
#include "nghttp2_net.h"
|
2014-03-25 18:04:24 +01:00
|
|
|
#include "nghttp2_priority_spec.h"
|
2014-04-04 14:57:47 +02:00
|
|
|
#include "nghttp2_option.h"
|
2015-02-19 16:01:15 +01:00
|
|
|
#include "nghttp2_http.h"
|
2015-08-16 12:01:10 +02:00
|
|
|
#include "nghttp2_pq.h"
|
2016-10-14 15:31:46 +02:00
|
|
|
#include "nghttp2_debug.h"
|
2012-01-24 14:02:24 +01:00
|
|
|
|
2012-02-08 15:45:48 +01:00
|
|
|
/*
|
2012-05-07 17:59:26 +02:00
|
|
|
* Returns non-zero if the number of outgoing opened streams is larger
|
|
|
|
* than or equal to
|
2014-06-10 14:29:19 +02:00
|
|
|
* remote_settings.max_concurrent_streams.
|
2012-02-08 15:45:48 +01:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int
|
|
|
|
session_is_outgoing_concurrent_streams_max(nghttp2_session *session) {
|
|
|
|
return session->remote_settings.max_concurrent_streams <=
|
|
|
|
session->num_outgoing_streams;
|
2012-05-07 17:59:26 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns non-zero if the number of incoming opened streams is larger
|
|
|
|
* than or equal to
|
2014-06-10 14:29:19 +02:00
|
|
|
* local_settings.max_concurrent_streams.
|
2012-05-07 17:59:26 +02:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int
|
|
|
|
session_is_incoming_concurrent_streams_max(nghttp2_session *session) {
|
|
|
|
return session->local_settings.max_concurrent_streams <=
|
|
|
|
session->num_incoming_streams;
|
2012-02-08 15:45:48 +01:00
|
|
|
}
|
|
|
|
|
2014-02-20 14:59:29 +01:00
|
|
|
/*
|
|
|
|
* Returns non-zero if the number of incoming opened streams is larger
|
|
|
|
* than or equal to
|
|
|
|
* session->pending_local_max_concurrent_stream.
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int
|
|
|
|
session_is_incoming_concurrent_streams_pending_max(nghttp2_session *session) {
|
|
|
|
return session->pending_local_max_concurrent_stream <=
|
|
|
|
session->num_incoming_streams;
|
2014-02-20 14:59:29 +01:00
|
|
|
}
|
|
|
|
|
2012-03-07 16:18:18 +01:00
|
|
|
/*
|
2014-01-18 08:25:44 +01:00
|
|
|
* Returns non-zero if |lib_error| is non-fatal error.
|
2012-03-07 16:18:18 +01:00
|
|
|
*/
|
2015-03-23 13:18:55 +01:00
|
|
|
static int is_non_fatal(int lib_error_code) {
|
|
|
|
return lib_error_code < 0 && lib_error_code > NGHTTP2_ERR_FATAL;
|
2012-03-07 16:18:18 +01:00
|
|
|
}
|
|
|
|
|
2015-03-23 13:18:55 +01:00
|
|
|
int nghttp2_is_fatal(int lib_error_code) {
|
|
|
|
return lib_error_code < NGHTTP2_ERR_FATAL;
|
|
|
|
}
|
2012-03-07 16:40:17 +01:00
|
|
|
|
2015-02-20 15:07:48 +01:00
|
|
|
static int session_enforce_http_messaging(nghttp2_session *session) {
|
|
|
|
return (session->opt_flags & NGHTTP2_OPTMASK_NO_HTTP_MESSAGING) == 0;
|
2015-02-19 16:01:15 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns nonzero if |frame| is trailer headers.
|
|
|
|
*/
|
|
|
|
static int session_trailer_headers(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream,
|
|
|
|
nghttp2_frame *frame) {
|
|
|
|
if (!stream || frame->hd.type != NGHTTP2_HEADERS) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if (session->server) {
|
|
|
|
return frame->headers.cat == NGHTTP2_HCAT_HEADERS;
|
|
|
|
}
|
|
|
|
|
|
|
|
return frame->headers.cat == NGHTTP2_HCAT_HEADERS &&
|
|
|
|
(stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) == 0;
|
|
|
|
}
|
|
|
|
|
2014-01-09 14:06:38 +01:00
|
|
|
/* Returns nonzero if the |stream| is in reserved(remote) state */
|
|
|
|
static int state_reserved_remote(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-01-09 14:06:38 +01:00
|
|
|
return stream->state == NGHTTP2_STREAM_RESERVED &&
|
2014-11-27 15:39:04 +01:00
|
|
|
!nghttp2_session_is_my_stream_id(session, stream->stream_id);
|
2014-01-09 14:06:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/* Returns nonzero if the |stream| is in reserved(local) state */
|
|
|
|
static int state_reserved_local(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-01-09 14:06:38 +01:00
|
|
|
return stream->state == NGHTTP2_STREAM_RESERVED &&
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_session_is_my_stream_id(session, stream->stream_id);
|
2014-01-09 14:06:38 +01:00
|
|
|
}
|
|
|
|
|
2014-06-07 11:36:58 +02:00
|
|
|
/*
|
2014-11-29 09:24:49 +01:00
|
|
|
* Checks whether received stream_id is valid. This function returns
|
|
|
|
* 1 if it succeeds, or 0.
|
2014-06-07 11:36:58 +02:00
|
|
|
*/
|
|
|
|
static int session_is_new_peer_stream_id(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t stream_id) {
|
2014-11-29 09:24:49 +01:00
|
|
|
return stream_id != 0 &&
|
|
|
|
!nghttp2_session_is_my_stream_id(session, stream_id) &&
|
|
|
|
session->last_recv_stream_id < stream_id;
|
2014-06-07 11:36:58 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
static int session_detect_idle_stream(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t stream_id) {
|
2014-06-07 11:36:58 +02:00
|
|
|
/* Assume that stream object with stream_id does not exist */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_is_my_stream_id(session, stream_id)) {
|
2016-01-23 06:47:39 +01:00
|
|
|
if (session->last_sent_stream_id < stream_id) {
|
2014-06-07 11:36:58 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session_is_new_peer_stream_id(session, stream_id)) {
|
2014-06-07 11:36:58 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
static int check_ext_type_set(const uint8_t *ext_types, uint8_t type) {
|
|
|
|
return (ext_types[type / 8] & (1 << (type & 0x7))) > 0;
|
|
|
|
}
|
|
|
|
|
2016-03-13 10:21:07 +01:00
|
|
|
static int session_call_error_callback(nghttp2_session *session,
|
2017-11-19 08:41:10 +01:00
|
|
|
int lib_error_code, const char *fmt,
|
|
|
|
...) {
|
2016-03-13 10:21:07 +01:00
|
|
|
size_t bufsize;
|
|
|
|
va_list ap;
|
|
|
|
char *buf;
|
|
|
|
int rv;
|
|
|
|
nghttp2_mem *mem;
|
|
|
|
|
2017-11-19 08:41:10 +01:00
|
|
|
if (!session->callbacks.error_callback &&
|
|
|
|
!session->callbacks.error_callback2) {
|
2016-03-13 10:21:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
mem = &session->mem;
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rv = vsnprintf(NULL, 0, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
bufsize = (size_t)(rv + 1);
|
|
|
|
|
|
|
|
buf = nghttp2_mem_malloc(mem, bufsize);
|
|
|
|
if (buf == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
va_start(ap, fmt);
|
|
|
|
rv = vsnprintf(buf, bufsize, fmt, ap);
|
|
|
|
va_end(ap);
|
|
|
|
|
|
|
|
if (rv < 0) {
|
|
|
|
nghttp2_mem_free(mem, buf);
|
|
|
|
/* vsnprintf may return error because of various things we can
|
|
|
|
imagine, but typically we don't want to drop session just for
|
|
|
|
debug callback. */
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("error_callback: vsnprintf failed. The template was %s\n", fmt);
|
2016-03-13 10:21:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2017-11-19 08:41:10 +01:00
|
|
|
if (session->callbacks.error_callback2) {
|
|
|
|
rv = session->callbacks.error_callback2(session, lib_error_code, buf,
|
|
|
|
(size_t)rv, session->user_data);
|
|
|
|
} else {
|
|
|
|
rv = session->callbacks.error_callback(session, buf, (size_t)rv,
|
|
|
|
session->user_data);
|
|
|
|
}
|
2016-03-13 10:21:07 +01:00
|
|
|
|
|
|
|
nghttp2_mem_free(mem, buf);
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_terminate_session(nghttp2_session *session,
|
|
|
|
int32_t last_stream_id,
|
|
|
|
uint32_t error_code, const char *reason) {
|
2014-11-28 14:59:13 +01:00
|
|
|
int rv;
|
2014-07-12 15:57:17 +02:00
|
|
|
const uint8_t *debug_data;
|
|
|
|
size_t debug_datalen;
|
|
|
|
|
2014-11-29 08:28:08 +01:00
|
|
|
if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
|
2014-07-12 15:57:17 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
/* Ignore all incoming frames because we are going to tear down the
|
|
|
|
session. */
|
|
|
|
session->iframe.state = NGHTTP2_IB_IGN_ALL;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (reason == NULL) {
|
2014-07-12 15:57:17 +02:00
|
|
|
debug_data = NULL;
|
|
|
|
debug_datalen = 0;
|
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
debug_data = (const uint8_t *)reason;
|
2014-07-12 15:57:17 +02:00
|
|
|
debug_datalen = strlen(reason);
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:59:13 +01:00
|
|
|
rv = nghttp2_session_add_goaway(session, last_stream_id, error_code,
|
2015-01-21 17:43:56 +01:00
|
|
|
debug_data, debug_datalen,
|
|
|
|
NGHTTP2_GOAWAY_AUX_TERM_ON_SEND);
|
2014-11-28 14:59:13 +01:00
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-11-29 08:28:08 +01:00
|
|
|
session->goaway_flags |= NGHTTP2_GOAWAY_TERM_ON_SEND;
|
2014-11-28 14:59:13 +01:00
|
|
|
|
|
|
|
return 0;
|
2014-07-12 15:57:17 +02:00
|
|
|
}
|
|
|
|
|
2013-12-25 16:23:07 +01:00
|
|
|
int nghttp2_session_terminate_session(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
uint32_t error_code) {
|
2014-07-12 15:57:17 +02:00
|
|
|
return session_terminate_session(session, session->last_proc_stream_id,
|
|
|
|
error_code, NULL);
|
2014-06-18 04:32:07 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_session_terminate_session2(nghttp2_session *session,
|
|
|
|
int32_t last_stream_id,
|
2014-11-27 15:39:04 +01:00
|
|
|
uint32_t error_code) {
|
2014-07-12 15:57:17 +02:00
|
|
|
return session_terminate_session(session, last_stream_id, error_code, NULL);
|
|
|
|
}
|
2014-06-18 03:51:33 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_terminate_session_with_reason(nghttp2_session *session,
|
|
|
|
uint32_t error_code,
|
|
|
|
const char *reason) {
|
2014-07-12 15:57:17 +02:00
|
|
|
return session_terminate_session(session, session->last_proc_stream_id,
|
|
|
|
error_code, reason);
|
2012-03-11 11:27:33 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_is_my_stream_id(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t stream_id) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rem;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream_id == 0) {
|
2012-01-26 17:17:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-18 16:45:20 +01:00
|
|
|
rem = stream_id & 0x1;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->server) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rem == 0;
|
|
|
|
}
|
|
|
|
return rem == 1;
|
2012-01-26 17:17:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *nghttp2_session_get_stream(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2014-03-30 10:41:54 +02:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
stream = (nghttp2_stream *)nghttp2_map_find(&session->streams, stream_id);
|
2014-03-30 10:41:54 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == NULL || (stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) ||
|
|
|
|
stream->state == NGHTTP2_STREAM_IDLE) {
|
2014-03-30 10:41:54 +02:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
return stream;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *nghttp2_session_get_stream_raw(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
|
|
|
return (nghttp2_stream *)nghttp2_map_find(&session->streams, stream_id);
|
2012-01-25 15:46:07 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void session_inbound_frame_reset(nghttp2_session *session) {
|
2013-09-28 10:59:24 +02:00
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem = &session->mem;
|
2014-01-22 16:28:23 +01:00
|
|
|
/* A bit risky code, since if this function is called from
|
|
|
|
nghttp2_session_new(), we rely on the fact that
|
|
|
|
iframe->frame.hd.type is 0, so that no free is performed. */
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (iframe->frame.hd.type) {
|
2016-02-24 16:45:24 +01:00
|
|
|
case NGHTTP2_DATA:
|
|
|
|
break;
|
2014-01-22 16:28:23 +01:00
|
|
|
case NGHTTP2_HEADERS:
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_frame_headers_free(&iframe->frame.headers, mem);
|
2014-01-22 16:28:23 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PRIORITY:
|
|
|
|
nghttp2_frame_priority_free(&iframe->frame.priority);
|
|
|
|
break;
|
|
|
|
case NGHTTP2_RST_STREAM:
|
|
|
|
nghttp2_frame_rst_stream_free(&iframe->frame.rst_stream);
|
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS:
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_frame_settings_free(&iframe->frame.settings, mem);
|
2016-04-10 09:36:04 +02:00
|
|
|
|
|
|
|
nghttp2_mem_free(mem, iframe->iv);
|
|
|
|
|
|
|
|
iframe->iv = NULL;
|
|
|
|
iframe->niv = 0;
|
|
|
|
iframe->max_niv = 0;
|
|
|
|
|
2014-01-22 16:28:23 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_frame_push_promise_free(&iframe->frame.push_promise, mem);
|
2014-01-22 16:28:23 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PING:
|
|
|
|
nghttp2_frame_ping_free(&iframe->frame.ping);
|
|
|
|
break;
|
|
|
|
case NGHTTP2_GOAWAY:
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_frame_goaway_free(&iframe->frame.goaway, mem);
|
2014-01-22 16:28:23 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_WINDOW_UPDATE:
|
|
|
|
nghttp2_frame_window_update_free(&iframe->frame.window_update);
|
|
|
|
break;
|
2016-02-24 16:45:24 +01:00
|
|
|
default:
|
|
|
|
/* extension frame */
|
2016-04-03 15:58:25 +02:00
|
|
|
if (check_ext_type_set(session->user_recv_ext_types,
|
|
|
|
iframe->frame.hd.type)) {
|
|
|
|
nghttp2_frame_extension_free(&iframe->frame.ext);
|
2016-04-09 11:23:15 +02:00
|
|
|
} else {
|
2016-04-03 15:58:25 +02:00
|
|
|
switch (iframe->frame.hd.type) {
|
|
|
|
case NGHTTP2_ALTSVC:
|
2016-04-09 11:23:15 +02:00
|
|
|
if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ALTSVC) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
2016-04-03 15:58:25 +02:00
|
|
|
nghttp2_frame_altsvc_free(&iframe->frame.ext, mem);
|
|
|
|
break;
|
2018-05-10 03:57:02 +02:00
|
|
|
case NGHTTP2_ORIGIN:
|
|
|
|
if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ORIGIN) == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
nghttp2_frame_origin_free(&iframe->frame.ext, mem);
|
|
|
|
break;
|
2016-04-03 15:58:25 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2016-02-24 16:45:24 +01:00
|
|
|
break;
|
2013-09-28 10:59:24 +02:00
|
|
|
}
|
2014-06-09 16:16:54 +02:00
|
|
|
|
2014-01-23 13:25:49 +01:00
|
|
|
memset(&iframe->frame, 0, sizeof(nghttp2_frame));
|
2014-06-09 16:16:54 +02:00
|
|
|
memset(&iframe->ext_frame_payload, 0, sizeof(nghttp2_ext_frame_payload));
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_HEAD;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&iframe->sbuf, iframe->raw_sbuf,
|
|
|
|
sizeof(iframe->raw_sbuf));
|
|
|
|
iframe->sbuf.mark += NGHTTP2_FRAME_HDLEN;
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_buf_free(&iframe->lbuf, mem);
|
2014-03-22 10:27:38 +01:00
|
|
|
nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
iframe->raw_lbuf = NULL;
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->payloadleft = 0;
|
2014-02-08 16:35:21 +01:00
|
|
|
iframe->padlen = 0;
|
2012-05-25 03:46:40 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static void init_settings(nghttp2_settings_storage *settings) {
|
2014-06-10 14:29:19 +02:00
|
|
|
settings->header_table_size = NGHTTP2_HD_DEFAULT_MAX_BUFFER_SIZE;
|
|
|
|
settings->enable_push = 1;
|
More strict stream state handling
Previously, in server side, we used closed streams to detect the error
that the misbehaving client sends a frame on the incoming stream it
explicitly closed. With this commit, we make a further step, and
detect one more error case. Since we retain closed streams as long as
the sum of its size and the number of opened streams are equal or less
than max concurrent streams, we can safely say that if we get a frame
which is sent on the stream that is not found in either closed or
opened stream, it is already closed or has not existed. Then we can
send GOAWAY.
The previous code shrinks closed streams when we closed another
stream, but now it is removed. It is enough to adjust closed streams
when new incoming stream is created.
While creating this commit, we noticed that
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS is defined as INT32_MAX. But
since SETTINGS can contain value up to UINT32_MAX, it is not enough.
However, since the stream ID space is limited to INT32_MAX, it is high
enough. We could keep this value, but this time we deprecate
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS macro. While it is in public
header, the effect of deprecating it is negligible because of the
reason we wrote above, and usually application sets much smaller value
(say, 100) as SETTINGS_MAX_CONCURRENT_STREAMS.
2016-08-07 07:34:13 +02:00
|
|
|
settings->max_concurrent_streams = NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS;
|
2014-06-10 14:29:19 +02:00
|
|
|
settings->initial_window_size = NGHTTP2_INITIAL_WINDOW_SIZE;
|
2014-07-27 09:58:04 +02:00
|
|
|
settings->max_frame_size = NGHTTP2_MAX_FRAME_SIZE_MIN;
|
2014-10-28 15:40:47 +01:00
|
|
|
settings->max_header_list_size = UINT32_MAX;
|
2013-11-02 08:23:56 +01:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
static void active_outbound_item_reset(nghttp2_active_outbound_item *aob,
|
|
|
|
nghttp2_mem *mem) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: reset nghttp2_active_outbound_item\n");
|
|
|
|
DEBUGF("send: aob->item = %p\n", aob->item);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_outbound_item_free(aob->item, mem);
|
|
|
|
nghttp2_mem_free(mem, aob->item);
|
2014-02-18 15:23:11 +01:00
|
|
|
aob->item = NULL;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs_reset(&aob->framebufs);
|
2014-02-18 15:23:11 +01:00
|
|
|
aob->state = NGHTTP2_OB_POP_ITEM;
|
|
|
|
}
|
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
int nghttp2_enable_strict_preface = 1;
|
2014-10-09 14:37:18 +02:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
static int session_new(nghttp2_session **session_ptr,
|
|
|
|
const nghttp2_session_callbacks *callbacks,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *user_data, int server,
|
2014-12-07 15:07:13 +01:00
|
|
|
const nghttp2_option *option, nghttp2_mem *mem) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2016-06-14 17:00:30 +02:00
|
|
|
size_t nbuffer;
|
2016-09-11 15:13:59 +02:00
|
|
|
size_t max_deflate_dynamic_table_size =
|
|
|
|
NGHTTP2_HD_DEFAULT_MAX_DEFLATE_BUFFER_SIZE;
|
2014-02-13 15:22:52 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
if (mem == NULL) {
|
|
|
|
mem = nghttp2_mem_default();
|
|
|
|
}
|
|
|
|
|
|
|
|
*session_ptr = nghttp2_mem_calloc(mem, 1, sizeof(nghttp2_session));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (*session_ptr == NULL) {
|
2014-02-18 16:45:20 +01:00
|
|
|
rv = NGHTTP2_ERR_NOMEM;
|
2012-02-16 14:47:49 +01:00
|
|
|
goto fail_session;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2012-01-27 15:05:29 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
(*session_ptr)->mem = *mem;
|
2014-12-08 13:22:20 +01:00
|
|
|
mem = &(*session_ptr)->mem;
|
2014-12-07 15:07:13 +01:00
|
|
|
|
2013-11-28 15:26:34 +01:00
|
|
|
/* next_stream_id is initialized in either
|
|
|
|
nghttp2_session_client_new2 or nghttp2_session_server_new2 */
|
2012-01-24 14:02:24 +01:00
|
|
|
|
2015-08-13 16:32:13 +02:00
|
|
|
nghttp2_stream_init(&(*session_ptr)->root, 0, NGHTTP2_STREAM_FLAG_NONE,
|
2015-08-18 15:41:50 +02:00
|
|
|
NGHTTP2_STREAM_IDLE, NGHTTP2_DEFAULT_WEIGHT, 0, 0, NULL,
|
|
|
|
mem);
|
2014-04-17 14:15:14 +02:00
|
|
|
|
2013-08-08 18:31:15 +02:00
|
|
|
(*session_ptr)->remote_window_size = NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
|
2013-07-15 14:45:59 +02:00
|
|
|
(*session_ptr)->recv_window_size = 0;
|
2014-07-25 14:26:03 +02:00
|
|
|
(*session_ptr)->consumed_size = 0;
|
2013-10-12 10:02:37 +02:00
|
|
|
(*session_ptr)->recv_reduction = 0;
|
2013-08-08 17:58:52 +02:00
|
|
|
(*session_ptr)->local_window_size = NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE;
|
2013-07-15 14:45:59 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
(*session_ptr)->goaway_flags = NGHTTP2_GOAWAY_NONE;
|
2014-06-18 04:03:55 +02:00
|
|
|
(*session_ptr)->local_last_stream_id = (1u << 31) - 1;
|
2014-06-28 03:46:05 +02:00
|
|
|
(*session_ptr)->remote_last_stream_id = (1u << 31) - 1;
|
2012-01-28 11:22:38 +01:00
|
|
|
|
2014-02-20 14:59:29 +01:00
|
|
|
(*session_ptr)->pending_local_max_concurrent_stream =
|
More strict stream state handling
Previously, in server side, we used closed streams to detect the error
that the misbehaving client sends a frame on the incoming stream it
explicitly closed. With this commit, we make a further step, and
detect one more error case. Since we retain closed streams as long as
the sum of its size and the number of opened streams are equal or less
than max concurrent streams, we can safely say that if we get a frame
which is sent on the stream that is not found in either closed or
opened stream, it is already closed or has not existed. Then we can
send GOAWAY.
The previous code shrinks closed streams when we closed another
stream, but now it is removed. It is enough to adjust closed streams
when new incoming stream is created.
While creating this commit, we noticed that
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS is defined as INT32_MAX. But
since SETTINGS can contain value up to UINT32_MAX, it is not enough.
However, since the stream ID space is limited to INT32_MAX, it is high
enough. We could keep this value, but this time we deprecate
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS macro. While it is in public
header, the effect of deprecating it is negligible because of the
reason we wrote above, and usually application sets much smaller value
(say, 100) as SETTINGS_MAX_CONCURRENT_STREAMS.
2016-08-07 07:34:13 +02:00
|
|
|
NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS;
|
2015-03-07 08:17:40 +01:00
|
|
|
(*session_ptr)->pending_enable_push = 1;
|
2014-02-20 14:59:29 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (server) {
|
2013-10-20 17:44:39 +02:00
|
|
|
(*session_ptr)->server = 1;
|
|
|
|
}
|
2012-02-05 16:14:19 +01:00
|
|
|
|
2014-06-10 14:29:19 +02:00
|
|
|
init_settings(&(*session_ptr)->remote_settings);
|
|
|
|
init_settings(&(*session_ptr)->local_settings);
|
2012-02-25 16:12:32 +01:00
|
|
|
|
2015-08-23 14:22:25 +02:00
|
|
|
(*session_ptr)->max_incoming_reserved_streams =
|
|
|
|
NGHTTP2_MAX_INCOMING_RESERVED_STREAMS;
|
|
|
|
|
2015-12-03 15:40:37 +01:00
|
|
|
/* Limit max outgoing concurrent streams to sensible value */
|
|
|
|
(*session_ptr)->remote_settings.max_concurrent_streams = 100;
|
|
|
|
|
2016-06-14 17:00:30 +02:00
|
|
|
(*session_ptr)->max_send_header_block_length = NGHTTP2_MAX_HEADERSLEN;
|
2019-06-25 15:33:35 +02:00
|
|
|
(*session_ptr)->max_outbound_ack = NGHTTP2_DEFAULT_MAX_OBQ_FLOOD_ITEM;
|
2016-06-14 17:00:30 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (option) {
|
|
|
|
if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE) &&
|
|
|
|
option->no_auto_window_update) {
|
2014-04-04 14:57:47 +02:00
|
|
|
|
2014-07-25 14:26:03 +02:00
|
|
|
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE;
|
2014-04-04 14:57:47 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (option->opt_set_mask & NGHTTP2_OPT_PEER_MAX_CONCURRENT_STREAMS) {
|
2014-04-04 14:57:47 +02:00
|
|
|
|
2014-06-10 14:29:19 +02:00
|
|
|
(*session_ptr)->remote_settings.max_concurrent_streams =
|
2014-11-27 15:39:04 +01:00
|
|
|
option->peer_max_concurrent_streams;
|
2014-04-04 14:57:47 +02:00
|
|
|
}
|
2014-09-13 12:50:44 +02:00
|
|
|
|
2015-08-23 14:22:25 +02:00
|
|
|
if (option->opt_set_mask & NGHTTP2_OPT_MAX_RESERVED_REMOTE_STREAMS) {
|
|
|
|
|
|
|
|
(*session_ptr)->max_incoming_reserved_streams =
|
|
|
|
option->max_reserved_remote_streams;
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:35:40 +02:00
|
|
|
if ((option->opt_set_mask & NGHTTP2_OPT_NO_RECV_CLIENT_MAGIC) &&
|
|
|
|
option->no_recv_client_magic) {
|
2014-09-13 12:50:44 +02:00
|
|
|
|
2015-04-05 15:35:40 +02:00
|
|
|
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC;
|
2014-09-13 12:50:44 +02:00
|
|
|
}
|
2015-02-20 15:26:56 +01:00
|
|
|
|
|
|
|
if ((option->opt_set_mask & NGHTTP2_OPT_NO_HTTP_MESSAGING) &&
|
|
|
|
option->no_http_messaging) {
|
|
|
|
|
|
|
|
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_HTTP_MESSAGING;
|
|
|
|
}
|
2015-10-14 17:17:07 +02:00
|
|
|
|
|
|
|
if (option->opt_set_mask & NGHTTP2_OPT_USER_RECV_EXT_TYPES) {
|
|
|
|
memcpy((*session_ptr)->user_recv_ext_types, option->user_recv_ext_types,
|
|
|
|
sizeof((*session_ptr)->user_recv_ext_types));
|
|
|
|
}
|
2016-02-29 15:39:50 +01:00
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
if (option->opt_set_mask & NGHTTP2_OPT_BUILTIN_RECV_EXT_TYPES) {
|
2016-04-09 11:23:15 +02:00
|
|
|
(*session_ptr)->builtin_recv_ext_types = option->builtin_recv_ext_types;
|
2016-04-03 15:58:25 +02:00
|
|
|
}
|
|
|
|
|
2016-02-29 15:39:50 +01:00
|
|
|
if ((option->opt_set_mask & NGHTTP2_OPT_NO_AUTO_PING_ACK) &&
|
|
|
|
option->no_auto_ping_ack) {
|
|
|
|
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_AUTO_PING_ACK;
|
|
|
|
}
|
2016-06-14 17:00:30 +02:00
|
|
|
|
|
|
|
if (option->opt_set_mask & NGHTTP2_OPT_MAX_SEND_HEADER_BLOCK_LENGTH) {
|
|
|
|
(*session_ptr)->max_send_header_block_length =
|
|
|
|
option->max_send_header_block_length;
|
|
|
|
}
|
2016-09-11 15:13:59 +02:00
|
|
|
|
|
|
|
if (option->opt_set_mask & NGHTTP2_OPT_MAX_DEFLATE_DYNAMIC_TABLE_SIZE) {
|
|
|
|
max_deflate_dynamic_table_size = option->max_deflate_dynamic_table_size;
|
|
|
|
}
|
2017-02-13 14:15:42 +01:00
|
|
|
|
|
|
|
if ((option->opt_set_mask & NGHTTP2_OPT_NO_CLOSED_STREAMS) &&
|
|
|
|
option->no_closed_streams) {
|
|
|
|
(*session_ptr)->opt_flags |= NGHTTP2_OPTMASK_NO_CLOSED_STREAMS;
|
|
|
|
}
|
2019-06-25 15:33:35 +02:00
|
|
|
|
|
|
|
if (option->opt_set_mask & NGHTTP2_OPT_MAX_OUTBOUND_ACK) {
|
|
|
|
(*session_ptr)->max_outbound_ack = option->max_outbound_ack;
|
|
|
|
}
|
2016-09-11 15:13:59 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_hd_deflate_init2(&(*session_ptr)->hd_deflater,
|
|
|
|
max_deflate_dynamic_table_size, mem);
|
|
|
|
if (rv != 0) {
|
|
|
|
goto fail_hd_deflater;
|
|
|
|
}
|
|
|
|
rv = nghttp2_hd_inflate_init(&(*session_ptr)->hd_inflater, mem);
|
|
|
|
if (rv != 0) {
|
|
|
|
goto fail_hd_inflater;
|
|
|
|
}
|
|
|
|
rv = nghttp2_map_init(&(*session_ptr)->streams, mem);
|
|
|
|
if (rv != 0) {
|
|
|
|
goto fail_map;
|
2013-11-07 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
2016-06-14 17:00:30 +02:00
|
|
|
nbuffer = ((*session_ptr)->max_send_header_block_length +
|
|
|
|
NGHTTP2_FRAMEBUF_CHUNKLEN - 1) /
|
|
|
|
NGHTTP2_FRAMEBUF_CHUNKLEN;
|
|
|
|
|
|
|
|
if (nbuffer == 0) {
|
|
|
|
nbuffer = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* 1 for Pad Field. */
|
|
|
|
rv = nghttp2_bufs_init3(&(*session_ptr)->aob.framebufs,
|
|
|
|
NGHTTP2_FRAMEBUF_CHUNKLEN, nbuffer, 1,
|
|
|
|
NGHTTP2_FRAME_HDLEN + 1, mem);
|
|
|
|
if (rv != 0) {
|
|
|
|
goto fail_aob_framebuf;
|
|
|
|
}
|
|
|
|
|
|
|
|
active_outbound_item_reset(&(*session_ptr)->aob, mem);
|
|
|
|
|
2012-01-24 14:02:24 +01:00
|
|
|
(*session_ptr)->callbacks = *callbacks;
|
|
|
|
(*session_ptr)->user_data = user_data;
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(*session_ptr);
|
2012-02-16 15:09:06 +01:00
|
|
|
|
2015-04-05 15:35:40 +02:00
|
|
|
if (nghttp2_enable_strict_preface) {
|
2014-10-09 14:37:18 +02:00
|
|
|
nghttp2_inbound_frame *iframe = &(*session_ptr)->iframe;
|
|
|
|
|
2017-11-23 06:19:12 +01:00
|
|
|
if (server && ((*session_ptr)->opt_flags &
|
|
|
|
NGHTTP2_OPTMASK_NO_RECV_CLIENT_MAGIC) == 0) {
|
2015-04-05 15:35:40 +02:00
|
|
|
iframe->state = NGHTTP2_IB_READ_CLIENT_MAGIC;
|
|
|
|
iframe->payloadleft = NGHTTP2_CLIENT_MAGIC_LEN;
|
2015-03-22 16:12:27 +01:00
|
|
|
} else {
|
|
|
|
iframe->state = NGHTTP2_IB_READ_FIRST_SETTINGS;
|
|
|
|
}
|
2015-04-05 15:35:40 +02:00
|
|
|
|
|
|
|
if (!server) {
|
|
|
|
(*session_ptr)->aob.state = NGHTTP2_OB_SEND_CLIENT_MAGIC;
|
|
|
|
nghttp2_bufs_add(&(*session_ptr)->aob.framebufs, NGHTTP2_CLIENT_MAGIC,
|
|
|
|
NGHTTP2_CLIENT_MAGIC_LEN);
|
|
|
|
}
|
2014-09-13 12:50:44 +02:00
|
|
|
}
|
|
|
|
|
2012-01-24 14:02:24 +01:00
|
|
|
return 0;
|
2012-02-16 14:47:49 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
fail_aob_framebuf:
|
2013-12-04 16:41:42 +01:00
|
|
|
nghttp2_map_free(&(*session_ptr)->streams);
|
2014-11-27 15:39:04 +01:00
|
|
|
fail_map:
|
2013-07-19 17:08:14 +02:00
|
|
|
nghttp2_hd_inflate_free(&(*session_ptr)->hd_inflater);
|
2014-11-27 15:39:04 +01:00
|
|
|
fail_hd_inflater:
|
2013-07-19 17:08:14 +02:00
|
|
|
nghttp2_hd_deflate_free(&(*session_ptr)->hd_deflater);
|
2014-11-27 15:39:04 +01:00
|
|
|
fail_hd_deflater:
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, *session_ptr);
|
2014-11-27 15:39:04 +01:00
|
|
|
fail_session:
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_client_new(nghttp2_session **session_ptr,
|
|
|
|
const nghttp2_session_callbacks *callbacks,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *user_data) {
|
2014-12-07 15:07:13 +01:00
|
|
|
return nghttp2_session_client_new3(session_ptr, callbacks, user_data, NULL,
|
|
|
|
NULL);
|
2013-11-07 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_session_client_new2(nghttp2_session **session_ptr,
|
|
|
|
const nghttp2_session_callbacks *callbacks,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *user_data, const nghttp2_option *option) {
|
2014-12-07 15:07:13 +01:00
|
|
|
return nghttp2_session_client_new3(session_ptr, callbacks, user_data, option,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_session_client_new3(nghttp2_session **session_ptr,
|
|
|
|
const nghttp2_session_callbacks *callbacks,
|
|
|
|
void *user_data, const nghttp2_option *option,
|
|
|
|
nghttp2_mem *mem) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-06-25 14:19:12 +02:00
|
|
|
nghttp2_session *session;
|
2014-06-07 11:51:20 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
rv = session_new(&session, callbacks, user_data, 0, option, mem);
|
2014-04-04 14:57:47 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-02-01 16:19:31 +01:00
|
|
|
}
|
2014-02-18 16:45:20 +01:00
|
|
|
/* IDs for use in client */
|
2014-06-25 14:19:12 +02:00
|
|
|
session->next_stream_id = 1;
|
|
|
|
|
|
|
|
*session_ptr = session;
|
|
|
|
|
2014-02-18 16:45:20 +01:00
|
|
|
return 0;
|
2012-02-01 16:19:31 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_server_new(nghttp2_session **session_ptr,
|
|
|
|
const nghttp2_session_callbacks *callbacks,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *user_data) {
|
2014-12-07 15:07:13 +01:00
|
|
|
return nghttp2_session_server_new3(session_ptr, callbacks, user_data, NULL,
|
|
|
|
NULL);
|
2013-11-07 16:12:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_session_server_new2(nghttp2_session **session_ptr,
|
|
|
|
const nghttp2_session_callbacks *callbacks,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *user_data, const nghttp2_option *option) {
|
2014-12-07 15:07:13 +01:00
|
|
|
return nghttp2_session_server_new3(session_ptr, callbacks, user_data, option,
|
|
|
|
NULL);
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_session_server_new3(nghttp2_session **session_ptr,
|
|
|
|
const nghttp2_session_callbacks *callbacks,
|
|
|
|
void *user_data, const nghttp2_option *option,
|
|
|
|
nghttp2_mem *mem) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-06-25 14:19:12 +02:00
|
|
|
nghttp2_session *session;
|
2014-06-07 11:51:20 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
rv = session_new(&session, callbacks, user_data, 1, option, mem);
|
2014-04-04 14:57:47 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-02-01 16:19:31 +01:00
|
|
|
}
|
2014-02-18 16:45:20 +01:00
|
|
|
/* IDs for use in client */
|
2014-06-25 14:19:12 +02:00
|
|
|
session->next_stream_id = 2;
|
|
|
|
|
|
|
|
*session_ptr = session;
|
|
|
|
|
2014-02-18 16:45:20 +01:00
|
|
|
return 0;
|
2012-02-01 16:19:31 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int free_streams(nghttp2_map_entry *entry, void *ptr) {
|
2014-09-17 16:16:00 +02:00
|
|
|
nghttp2_session *session;
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
nghttp2_outbound_item *item;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-09-17 16:16:00 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
session = (nghttp2_session *)ptr;
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
2014-11-27 15:39:04 +01:00
|
|
|
stream = (nghttp2_stream *)entry;
|
2014-12-20 11:53:57 +01:00
|
|
|
item = stream->item;
|
2014-09-17 16:16:00 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item && !item->queued && item != session->aob.item) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_outbound_item_free(item, mem);
|
|
|
|
nghttp2_mem_free(mem, item);
|
2014-09-17 16:16:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_stream_free(stream);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
static void ob_q_free(nghttp2_outbound_queue *q, nghttp2_mem *mem) {
|
|
|
|
nghttp2_outbound_item *item, *next;
|
|
|
|
for (item = q->head; item;) {
|
|
|
|
next = item->qnext;
|
|
|
|
nghttp2_outbound_item_free(item, mem);
|
|
|
|
nghttp2_mem_free(mem, item);
|
|
|
|
item = next;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-07-22 17:36:00 +02:00
|
|
|
static int inflight_settings_new(nghttp2_inflight_settings **settings_ptr,
|
|
|
|
const nghttp2_settings_entry *iv, size_t niv,
|
|
|
|
nghttp2_mem *mem) {
|
|
|
|
*settings_ptr = nghttp2_mem_malloc(mem, sizeof(nghttp2_inflight_settings));
|
|
|
|
if (!*settings_ptr) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (niv > 0) {
|
|
|
|
(*settings_ptr)->iv = nghttp2_frame_iv_copy(iv, niv, mem);
|
|
|
|
if (!(*settings_ptr)->iv) {
|
2017-04-13 16:00:15 +02:00
|
|
|
nghttp2_mem_free(mem, *settings_ptr);
|
2015-07-22 17:36:00 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
(*settings_ptr)->iv = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
(*settings_ptr)->niv = niv;
|
|
|
|
(*settings_ptr)->next = NULL;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void inflight_settings_del(nghttp2_inflight_settings *settings,
|
|
|
|
nghttp2_mem *mem) {
|
|
|
|
if (!settings) {
|
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_mem_free(mem, settings->iv);
|
|
|
|
nghttp2_mem_free(mem, settings);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void nghttp2_session_del(nghttp2_session *session) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2015-07-22 17:36:00 +02:00
|
|
|
nghttp2_inflight_settings *settings;
|
2014-12-07 15:07:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session == NULL) {
|
2012-02-05 16:14:19 +01:00
|
|
|
return;
|
|
|
|
}
|
2014-12-07 15:07:13 +01:00
|
|
|
|
|
|
|
mem = &session->mem;
|
|
|
|
|
2015-07-22 17:36:00 +02:00
|
|
|
for (settings = session->inflight_settings_head; settings;) {
|
|
|
|
nghttp2_inflight_settings *next = settings->next;
|
|
|
|
inflight_settings_del(settings, mem);
|
|
|
|
settings = next;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-08-13 16:32:13 +02:00
|
|
|
nghttp2_stream_free(&session->root);
|
2014-04-17 14:15:14 +02:00
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
/* Have to free streams first, so that we can check
|
2014-12-20 11:53:57 +01:00
|
|
|
stream->item->queued */
|
2014-09-17 16:16:00 +02:00
|
|
|
nghttp2_map_each_free(&session->streams, free_streams, session);
|
2013-12-04 16:41:42 +01:00
|
|
|
nghttp2_map_free(&session->streams);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
ob_q_free(&session->ob_urgent, mem);
|
|
|
|
ob_q_free(&session->ob_reg, mem);
|
|
|
|
ob_q_free(&session->ob_syn, mem);
|
2015-08-16 12:01:10 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
active_outbound_item_reset(&session->aob, mem);
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2013-07-19 17:08:14 +02:00
|
|
|
nghttp2_hd_deflate_free(&session->hd_deflater);
|
|
|
|
nghttp2_hd_inflate_free(&session->hd_inflater);
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs_free(&session->aob.framebufs);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, session);
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2015-11-12 16:53:29 +01:00
|
|
|
int nghttp2_session_reprioritize_stream(
|
|
|
|
nghttp2_session *session, nghttp2_stream *stream,
|
|
|
|
const nghttp2_priority_spec *pri_spec_in) {
|
2014-03-25 18:04:24 +01:00
|
|
|
int rv;
|
2014-11-26 18:18:00 +01:00
|
|
|
nghttp2_stream *dep_stream = NULL;
|
2014-11-08 08:12:13 +01:00
|
|
|
nghttp2_priority_spec pri_spec_default;
|
|
|
|
const nghttp2_priority_spec *pri_spec = pri_spec_in;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-06-21 09:31:30 +02:00
|
|
|
assert(pri_spec->stream_id != stream->stream_id);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!nghttp2_stream_in_dep_tree(stream)) {
|
2014-11-08 02:31:53 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (pri_spec->stream_id != 0) {
|
2014-11-08 08:12:13 +01:00
|
|
|
dep_stream = nghttp2_session_get_stream_raw(session, pri_spec->stream_id);
|
|
|
|
|
2015-12-24 12:53:08 +01:00
|
|
|
if (!dep_stream &&
|
2014-11-27 15:39:04 +01:00
|
|
|
session_detect_idle_stream(session, pri_spec->stream_id)) {
|
2014-11-12 16:17:28 +01:00
|
|
|
|
|
|
|
nghttp2_priority_spec_default_init(&pri_spec_default);
|
|
|
|
|
2014-12-12 16:03:19 +01:00
|
|
|
dep_stream = nghttp2_session_open_stream(
|
|
|
|
session, pri_spec->stream_id, NGHTTP2_FLAG_NONE, &pri_spec_default,
|
|
|
|
NGHTTP2_STREAM_IDLE, NULL);
|
2014-11-12 16:17:28 +01:00
|
|
|
|
2014-12-12 16:03:19 +01:00
|
|
|
if (dep_stream == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2014-11-12 16:17:28 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (!dep_stream || !nghttp2_stream_in_dep_tree(dep_stream)) {
|
2014-11-08 08:12:13 +01:00
|
|
|
nghttp2_priority_spec_default_init(&pri_spec_default);
|
|
|
|
pri_spec = &pri_spec_default;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (pri_spec->stream_id == 0) {
|
2015-08-13 16:32:13 +02:00
|
|
|
dep_stream = &session->root;
|
2015-08-16 12:01:10 +02:00
|
|
|
} else if (nghttp2_stream_dep_find_ancestor(dep_stream, stream)) {
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("stream: cycle detected, dep_stream(%p)=%d stream(%p)=%d\n",
|
2016-10-14 15:33:08 +02:00
|
|
|
dep_stream, dep_stream->stream_id, stream, stream->stream_id);
|
2014-04-24 16:44:34 +02:00
|
|
|
|
|
|
|
nghttp2_stream_dep_remove_subtree(dep_stream);
|
2016-08-04 15:20:07 +02:00
|
|
|
rv = nghttp2_stream_dep_add_subtree(stream->dep_prev, dep_stream);
|
2015-08-13 16:32:13 +02:00
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-04-14 16:53:54 +02:00
|
|
|
}
|
2013-08-09 16:40:41 +02:00
|
|
|
|
2015-11-27 16:50:29 +01:00
|
|
|
assert(dep_stream);
|
|
|
|
|
2015-11-19 16:19:02 +01:00
|
|
|
if (dep_stream == stream->dep_prev && !pri_spec->exclusive) {
|
2015-12-07 15:08:54 +01:00
|
|
|
/* This is minor optimization when just weight is changed. */
|
|
|
|
nghttp2_stream_change_weight(stream, pri_spec->weight);
|
|
|
|
|
2015-11-19 16:19:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_stream_dep_remove_subtree(stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
/* We have to update weight after removing stream from tree */
|
|
|
|
stream->weight = pri_spec->weight;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-08-13 16:32:13 +02:00
|
|
|
if (pri_spec->exclusive) {
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_dep_insert_subtree(dep_stream, stream);
|
2014-04-14 16:53:54 +02:00
|
|
|
} else {
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_dep_add_subtree(dep_stream, stream);
|
2014-04-14 16:53:54 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-04-14 16:53:54 +02:00
|
|
|
return rv;
|
2013-08-09 16:40:41 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
return 0;
|
2013-08-09 16:40:41 +02:00
|
|
|
}
|
|
|
|
|
2014-10-02 17:59:44 +02:00
|
|
|
int nghttp2_session_add_item(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_outbound_item *item) {
|
2014-10-02 17:59:44 +02:00
|
|
|
/* TODO Return error if stream is not found for the frame requiring
|
|
|
|
stream presence. */
|
|
|
|
int rv = 0;
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
nghttp2_frame *frame;
|
|
|
|
|
|
|
|
frame = &item->frame;
|
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
switch (frame->hd.type) {
|
|
|
|
case NGHTTP2_DATA:
|
|
|
|
if (!stream) {
|
|
|
|
return NGHTTP2_ERR_STREAM_CLOSED;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
if (stream->item) {
|
|
|
|
return NGHTTP2_ERR_DATA_EXIST;
|
|
|
|
}
|
2014-11-11 16:26:23 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
rv = nghttp2_stream_attach_item(stream, item);
|
2015-12-02 13:16:30 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-12-02 13:16:30 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
return 0;
|
|
|
|
case NGHTTP2_HEADERS:
|
|
|
|
/* We push request HEADERS and push response HEADERS to
|
|
|
|
dedicated queue because their transmission is affected by
|
|
|
|
SETTINGS_MAX_CONCURRENT_STREAMS */
|
|
|
|
/* TODO If 2 HEADERS are submitted for reserved stream, then
|
|
|
|
both of them are queued into ob_syn, which is not
|
|
|
|
desirable. */
|
|
|
|
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST ||
|
|
|
|
(stream && stream->state == NGHTTP2_STREAM_RESERVED)) {
|
|
|
|
nghttp2_outbound_queue_push(&session->ob_syn, item);
|
|
|
|
item->queued = 1;
|
|
|
|
return 0;
|
|
|
|
;
|
|
|
|
}
|
2015-12-02 13:16:30 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
nghttp2_outbound_queue_push(&session->ob_reg, item);
|
|
|
|
item->queued = 1;
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_SETTINGS:
|
|
|
|
case NGHTTP2_PING:
|
|
|
|
nghttp2_outbound_queue_push(&session->ob_urgent, item);
|
|
|
|
item->queued = 1;
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_RST_STREAM:
|
|
|
|
if (stream) {
|
|
|
|
stream->state = NGHTTP2_STREAM_CLOSING;
|
|
|
|
}
|
|
|
|
nghttp2_outbound_queue_push(&session->ob_reg, item);
|
|
|
|
item->queued = 1;
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
|
|
|
nghttp2_headers_aux_data *aux_data;
|
|
|
|
nghttp2_priority_spec pri_spec;
|
2015-12-02 13:16:30 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
aux_data = &item->aux_data.headers;
|
2015-12-02 13:16:30 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
if (!stream) {
|
|
|
|
return NGHTTP2_ERR_STREAM_CLOSED;
|
|
|
|
}
|
2015-12-23 08:38:30 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
nghttp2_priority_spec_init(&pri_spec, stream->stream_id,
|
|
|
|
NGHTTP2_DEFAULT_WEIGHT, 0);
|
2015-12-02 13:16:30 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
if (!nghttp2_session_open_stream(
|
|
|
|
session, frame->push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_STREAM_FLAG_NONE, &pri_spec, NGHTTP2_STREAM_RESERVED,
|
|
|
|
aux_data->stream_user_data)) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2014-10-02 17:59:44 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
/* We don't have to call nghttp2_session_adjust_closed_stream()
|
|
|
|
here, since stream->stream_id is local stream_id, and it does
|
|
|
|
not affect closed stream count. */
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
nghttp2_outbound_queue_push(&session->ob_reg, item);
|
|
|
|
item->queued = 1;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
return 0;
|
2012-02-05 16:14:19 +01:00
|
|
|
}
|
2016-09-11 09:55:15 +02:00
|
|
|
case NGHTTP2_WINDOW_UPDATE:
|
|
|
|
if (stream) {
|
|
|
|
stream->window_update_queued = 1;
|
|
|
|
} else if (frame->hd.stream_id == 0) {
|
|
|
|
session->window_update_queued = 1;
|
|
|
|
}
|
|
|
|
nghttp2_outbound_queue_push(&session->ob_reg, item);
|
|
|
|
item->queued = 1;
|
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
nghttp2_outbound_queue_push(&session->ob_reg, item);
|
|
|
|
item->queued = 1;
|
|
|
|
return 0;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_add_rst_stream(nghttp2_session *session, int32_t stream_id,
|
|
|
|
uint32_t error_code) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_outbound_item *item;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_frame *frame;
|
2014-03-21 16:51:40 +01:00
|
|
|
nghttp2_stream *stream;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-03-21 16:51:40 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
2014-03-21 16:51:40 +01:00
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream && stream->state == NGHTTP2_STREAM_CLOSING) {
|
2014-03-21 16:51:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
/* Cancel pending request HEADERS in ob_syn if this RST_STREAM
|
2015-02-13 15:41:48 +01:00
|
|
|
refers to that stream. */
|
|
|
|
if (!session->server && nghttp2_session_is_my_stream_id(session, stream_id) &&
|
2015-04-24 15:30:32 +02:00
|
|
|
nghttp2_outbound_queue_top(&session->ob_syn)) {
|
|
|
|
nghttp2_headers_aux_data *aux_data;
|
2015-02-13 15:41:48 +01:00
|
|
|
nghttp2_frame *headers_frame;
|
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
headers_frame = &nghttp2_outbound_queue_top(&session->ob_syn)->frame;
|
2015-02-13 15:41:48 +01:00
|
|
|
assert(headers_frame->hd.type == NGHTTP2_HEADERS);
|
|
|
|
|
|
|
|
if (headers_frame->hd.stream_id <= stream_id &&
|
|
|
|
(uint32_t)stream_id < session->next_stream_id) {
|
2015-04-24 15:30:32 +02:00
|
|
|
|
|
|
|
for (item = session->ob_syn.head; item; item = item->qnext) {
|
|
|
|
aux_data = &item->aux_data.headers;
|
|
|
|
|
|
|
|
if (item->frame.hd.stream_id < stream_id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* stream_id in ob_syn queue must be strictly increasing. If
|
|
|
|
we found larger ID, then we can break here. */
|
|
|
|
if (item->frame.hd.stream_id > stream_id || aux_data->canceled) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
aux_data->error_code = error_code;
|
|
|
|
aux_data->canceled = 1;
|
|
|
|
|
2015-02-13 15:41:48 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item == NULL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2015-04-24 19:23:01 +02:00
|
|
|
nghttp2_outbound_item_init(item);
|
2014-10-02 17:59:44 +02:00
|
|
|
|
|
|
|
frame = &item->frame;
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_rst_stream_init(&frame->rst_stream, stream_id, error_code);
|
2014-10-02 17:59:44 +02:00
|
|
|
rv = nghttp2_session_add_item(session, item);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_frame_rst_stream_free(&frame->rst_stream);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, item);
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *nghttp2_session_open_stream(nghttp2_session *session,
|
|
|
|
int32_t stream_id, uint8_t flags,
|
2014-11-08 03:23:18 +01:00
|
|
|
nghttp2_priority_spec *pri_spec_in,
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_stream_state initial_state,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *stream_user_data) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_stream *stream;
|
2014-11-08 02:18:10 +01:00
|
|
|
nghttp2_stream *dep_stream = NULL;
|
2014-11-07 16:36:47 +01:00
|
|
|
int stream_alloc = 0;
|
2014-11-08 03:23:18 +01:00
|
|
|
nghttp2_priority_spec pri_spec_default;
|
|
|
|
nghttp2_priority_spec *pri_spec = pri_spec_in;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
2014-11-07 16:36:47 +01:00
|
|
|
stream = nghttp2_session_get_stream_raw(session, stream_id);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream) {
|
2014-11-07 16:36:47 +01:00
|
|
|
assert(stream->state == NGHTTP2_STREAM_IDLE);
|
|
|
|
assert(nghttp2_stream_in_dep_tree(stream));
|
2014-12-12 16:03:19 +01:00
|
|
|
nghttp2_session_detach_idle_stream(session, stream);
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_dep_remove(stream);
|
|
|
|
if (rv != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-07 16:36:47 +01:00
|
|
|
} else {
|
2014-12-07 15:07:13 +01:00
|
|
|
stream = nghttp2_mem_malloc(mem, sizeof(nghttp2_stream));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == NULL) {
|
2014-11-07 16:36:47 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream_alloc = 1;
|
2014-03-30 10:41:54 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (pri_spec->stream_id != 0) {
|
2014-11-07 16:36:47 +01:00
|
|
|
dep_stream = nghttp2_session_get_stream_raw(session, pri_spec->stream_id);
|
|
|
|
|
2015-12-24 12:53:08 +01:00
|
|
|
if (!dep_stream &&
|
2014-11-27 15:39:04 +01:00
|
|
|
session_detect_idle_stream(session, pri_spec->stream_id)) {
|
2014-12-12 16:03:19 +01:00
|
|
|
/* Depends on idle stream, which does not exist in memory.
|
|
|
|
Assign default priority for it. */
|
2014-11-12 16:17:28 +01:00
|
|
|
nghttp2_priority_spec_default_init(&pri_spec_default);
|
|
|
|
|
2014-12-12 16:03:19 +01:00
|
|
|
dep_stream = nghttp2_session_open_stream(
|
|
|
|
session, pri_spec->stream_id, NGHTTP2_FLAG_NONE, &pri_spec_default,
|
|
|
|
NGHTTP2_STREAM_IDLE, NULL);
|
2014-11-12 16:17:28 +01:00
|
|
|
|
2014-12-12 16:03:19 +01:00
|
|
|
if (dep_stream == NULL) {
|
|
|
|
if (stream_alloc) {
|
|
|
|
nghttp2_mem_free(mem, stream);
|
2014-11-12 16:17:28 +01:00
|
|
|
}
|
2014-12-12 16:03:19 +01:00
|
|
|
|
|
|
|
return NULL;
|
2014-11-12 16:17:28 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (!dep_stream || !nghttp2_stream_in_dep_tree(dep_stream)) {
|
2014-11-12 16:17:28 +01:00
|
|
|
/* If dep_stream is not part of dependency tree, stream will get
|
2015-10-20 16:00:18 +02:00
|
|
|
default priority. This handles the case when
|
|
|
|
pri_spec->stream_id == stream_id. This happens because we
|
|
|
|
don't check pri_spec->stream_id against new stream ID in
|
|
|
|
nghttp2_submit_request. This also handles the case when idle
|
|
|
|
stream created by PRIORITY frame was opened. Somehow we
|
|
|
|
first remove the idle stream from dependency tree. This is
|
|
|
|
done to simplify code base, but ideally we should retain old
|
|
|
|
dependency. But I'm not sure this adds values. */
|
2014-11-07 16:36:47 +01:00
|
|
|
nghttp2_priority_spec_default_init(&pri_spec_default);
|
|
|
|
pri_spec = &pri_spec_default;
|
|
|
|
}
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-02-09 13:45:55 +01:00
|
|
|
if (initial_state == NGHTTP2_STREAM_RESERVED) {
|
|
|
|
flags |= NGHTTP2_STREAM_FLAG_PUSH;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream_alloc) {
|
2017-01-24 14:59:01 +01:00
|
|
|
nghttp2_stream_init(stream, stream_id, flags, initial_state,
|
|
|
|
pri_spec->weight,
|
|
|
|
(int32_t)session->remote_settings.initial_window_size,
|
|
|
|
(int32_t)session->local_settings.initial_window_size,
|
|
|
|
stream_user_data, mem);
|
|
|
|
|
2014-11-07 16:36:47 +01:00
|
|
|
rv = nghttp2_map_insert(&session->streams, &stream->map_entry);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2017-01-24 14:59:01 +01:00
|
|
|
nghttp2_stream_free(stream);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, stream);
|
2014-11-07 16:36:47 +01:00
|
|
|
return NULL;
|
|
|
|
}
|
2017-01-24 14:59:01 +01:00
|
|
|
} else {
|
|
|
|
stream->flags = flags;
|
|
|
|
stream->state = initial_state;
|
|
|
|
stream->weight = pri_spec->weight;
|
|
|
|
stream->stream_user_data = stream_user_data;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (initial_state) {
|
2014-11-07 16:36:47 +01:00
|
|
|
case NGHTTP2_STREAM_RESERVED:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_is_my_stream_id(session, stream_id)) {
|
2015-08-23 14:22:25 +02:00
|
|
|
/* reserved (local) */
|
2013-07-24 18:49:05 +02:00
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
|
|
|
|
} else {
|
2015-08-23 14:22:25 +02:00
|
|
|
/* reserved (remote) */
|
2013-07-24 18:49:05 +02:00
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
|
2015-08-23 14:22:25 +02:00
|
|
|
++session->num_incoming_reserved_streams;
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
|
|
|
/* Reserved stream does not count in the concurrent streams
|
|
|
|
limit. That is one of the DOS vector. */
|
2014-11-07 16:36:47 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_STREAM_IDLE:
|
|
|
|
/* Idle stream does not count toward the concurrent streams limit.
|
|
|
|
This is used as anchor node in dependency tree. */
|
2015-12-23 08:38:30 +01:00
|
|
|
nghttp2_session_keep_idle_stream(session, stream);
|
2014-11-07 16:36:47 +01:00
|
|
|
break;
|
|
|
|
default:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_is_my_stream_id(session, stream_id)) {
|
2013-07-24 18:49:05 +02:00
|
|
|
++session->num_outgoing_streams;
|
|
|
|
} else {
|
|
|
|
++session->num_incoming_streams;
|
|
|
|
}
|
2012-05-07 17:59:26 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-08-13 16:32:13 +02:00
|
|
|
if (pri_spec->stream_id == 0) {
|
|
|
|
dep_stream = &session->root;
|
|
|
|
}
|
2014-11-08 15:13:14 +01:00
|
|
|
|
2015-08-13 16:32:13 +02:00
|
|
|
assert(dep_stream);
|
2014-03-29 15:17:39 +01:00
|
|
|
|
2015-08-13 16:32:13 +02:00
|
|
|
if (pri_spec->exclusive) {
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_dep_insert(dep_stream, stream);
|
|
|
|
if (rv != 0) {
|
|
|
|
return NULL;
|
|
|
|
}
|
2014-11-04 16:32:16 +01:00
|
|
|
} else {
|
2015-08-13 16:32:13 +02:00
|
|
|
nghttp2_stream_dep_add(dep_stream, stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2012-01-28 16:16:51 +01:00
|
|
|
return stream;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_close_stream(nghttp2_session *session, int32_t stream_id,
|
2014-11-27 15:39:04 +01:00
|
|
|
uint32_t error_code) {
|
2014-03-25 18:04:24 +01:00
|
|
|
int rv;
|
|
|
|
nghttp2_stream *stream;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2015-12-03 14:48:41 +01:00
|
|
|
int is_my_stream_id;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
2014-03-25 18:04:24 +01:00
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2013-09-05 16:17:16 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("stream: stream(%p)=%d close\n", stream, stream->stream_id);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
if (stream->item) {
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_outbound_item *item;
|
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
item = stream->item;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_detach_item(stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* If item is queued, it will be deleted when it is popped
|
|
|
|
(nghttp2_session_prep_frame() will fail). If session->aob.item
|
2014-05-08 16:37:56 +02:00
|
|
|
points to this item, let active_outbound_item_reset()
|
2014-03-25 18:04:24 +01:00
|
|
|
free the item. */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!item->queued && item != session->aob.item) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_outbound_item_free(item, mem);
|
|
|
|
nghttp2_mem_free(mem, item);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:17:16 +02:00
|
|
|
/* We call on_stream_close_callback even if stream->state is
|
|
|
|
NGHTTP2_STREAM_INITIAL. This will happen while sending request
|
|
|
|
HEADERS, a local endpoint receives RST_STREAM for that stream. It
|
|
|
|
may be PROTOCOL_ERROR, but without notifying stream closure will
|
|
|
|
hang the stream in a local endpoint.
|
|
|
|
*/
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-12 16:03:19 +01:00
|
|
|
if (session->callbacks.on_stream_close_callback) {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.on_stream_close_callback(
|
|
|
|
session, stream_id, error_code, session->user_data) != 0) {
|
2014-03-29 15:57:27 +01:00
|
|
|
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-01-29 15:00:33 +01:00
|
|
|
}
|
2014-03-29 15:57:27 +01:00
|
|
|
}
|
|
|
|
|
2015-12-03 14:48:41 +01:00
|
|
|
is_my_stream_id = nghttp2_session_is_my_stream_id(session, stream_id);
|
|
|
|
|
2015-02-09 13:45:55 +01:00
|
|
|
/* pushed streams which is not opened yet is not counted toward max
|
|
|
|
concurrent limits */
|
2015-08-23 14:22:25 +02:00
|
|
|
if ((stream->flags & NGHTTP2_STREAM_FLAG_PUSH)) {
|
2015-12-03 14:48:41 +01:00
|
|
|
if (!is_my_stream_id) {
|
2015-08-23 14:22:25 +02:00
|
|
|
--session->num_incoming_reserved_streams;
|
|
|
|
}
|
|
|
|
} else {
|
2015-12-03 14:48:41 +01:00
|
|
|
if (is_my_stream_id) {
|
2013-09-05 16:17:16 +02:00
|
|
|
--session->num_outgoing_streams;
|
|
|
|
} else {
|
|
|
|
--session->num_incoming_streams;
|
2012-05-07 17:59:26 +02:00
|
|
|
}
|
2012-01-25 17:04:01 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-03-30 10:41:54 +02:00
|
|
|
/* Closes both directions just in case they are not closed yet */
|
|
|
|
stream->flags |= NGHTTP2_STREAM_FLAG_CLOSED;
|
|
|
|
|
2017-02-13 14:15:42 +01:00
|
|
|
if ((session->opt_flags & NGHTTP2_OPTMASK_NO_CLOSED_STREAMS) == 0 &&
|
|
|
|
session->server && !is_my_stream_id &&
|
2015-12-03 14:48:41 +01:00
|
|
|
nghttp2_stream_in_dep_tree(stream)) {
|
2014-11-07 16:36:47 +01:00
|
|
|
/* On server side, retain stream at most MAX_CONCURRENT_STREAMS
|
|
|
|
combined with the current active incoming streams to make
|
|
|
|
dependency tree work better. */
|
2015-12-23 08:38:30 +01:00
|
|
|
nghttp2_session_keep_closed_stream(session, stream);
|
2014-03-30 10:41:54 +02:00
|
|
|
} else {
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_session_destroy_stream(session, stream);
|
More strict stream state handling
Previously, in server side, we used closed streams to detect the error
that the misbehaving client sends a frame on the incoming stream it
explicitly closed. With this commit, we make a further step, and
detect one more error case. Since we retain closed streams as long as
the sum of its size and the number of opened streams are equal or less
than max concurrent streams, we can safely say that if we get a frame
which is sent on the stream that is not found in either closed or
opened stream, it is already closed or has not existed. Then we can
send GOAWAY.
The previous code shrinks closed streams when we closed another
stream, but now it is removed. It is enough to adjust closed streams
when new incoming stream is created.
While creating this commit, we noticed that
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS is defined as INT32_MAX. But
since SETTINGS can contain value up to UINT32_MAX, it is not enough.
However, since the stream ID space is limited to INT32_MAX, it is high
enough. We could keep this value, but this time we deprecate
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS macro. While it is in public
header, the effect of deprecating it is negligible because of the
reason we wrote above, and usually application sets much smaller value
(say, 100) as SETTINGS_MAX_CONCURRENT_STREAMS.
2016-08-07 07:34:13 +02:00
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-03-30 10:41:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
int nghttp2_session_destroy_stream(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2015-08-16 12:01:10 +02:00
|
|
|
int rv;
|
2014-12-07 15:07:13 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("stream: destroy closed stream(%p)=%d\n", stream, stream->stream_id);
|
2014-03-30 10:41:54 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
|
|
|
|
2015-08-13 16:32:13 +02:00
|
|
|
if (nghttp2_stream_in_dep_tree(stream)) {
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_dep_remove(stream);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-08-13 16:32:13 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-03-30 10:41:54 +02:00
|
|
|
nghttp2_map_remove(&session->streams, stream->stream_id);
|
2013-09-05 16:17:16 +02:00
|
|
|
nghttp2_stream_free(stream);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, stream);
|
2015-08-16 12:01:10 +02:00
|
|
|
|
|
|
|
return 0;
|
2014-03-30 10:41:54 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-12-23 08:38:30 +01:00
|
|
|
void nghttp2_session_keep_closed_stream(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream) {
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("stream: keep closed stream(%p)=%d, state=%d\n", stream,
|
|
|
|
stream->stream_id, stream->state);
|
2014-03-30 10:41:54 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->closed_stream_tail) {
|
2014-03-30 10:41:54 +02:00
|
|
|
session->closed_stream_tail->closed_next = stream;
|
2014-11-07 16:36:47 +01:00
|
|
|
stream->closed_prev = session->closed_stream_tail;
|
2014-03-30 10:41:54 +02:00
|
|
|
} else {
|
|
|
|
session->closed_stream_head = stream;
|
|
|
|
}
|
|
|
|
session->closed_stream_tail = stream;
|
|
|
|
|
|
|
|
++session->num_closed_streams;
|
|
|
|
}
|
|
|
|
|
2015-12-23 08:38:30 +01:00
|
|
|
void nghttp2_session_keep_idle_stream(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("stream: keep idle stream(%p)=%d, state=%d\n", stream,
|
|
|
|
stream->stream_id, stream->state);
|
2014-12-12 16:03:19 +01:00
|
|
|
|
|
|
|
if (session->idle_stream_tail) {
|
|
|
|
session->idle_stream_tail->closed_next = stream;
|
|
|
|
stream->closed_prev = session->idle_stream_tail;
|
|
|
|
} else {
|
|
|
|
session->idle_stream_head = stream;
|
|
|
|
}
|
|
|
|
session->idle_stream_tail = stream;
|
|
|
|
|
|
|
|
++session->num_idle_streams;
|
|
|
|
}
|
|
|
|
|
|
|
|
void nghttp2_session_detach_idle_stream(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream) {
|
2014-11-07 16:36:47 +01:00
|
|
|
nghttp2_stream *prev_stream, *next_stream;
|
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("stream: detach idle stream(%p)=%d, state=%d\n", stream,
|
|
|
|
stream->stream_id, stream->state);
|
2014-11-07 16:36:47 +01:00
|
|
|
|
|
|
|
prev_stream = stream->closed_prev;
|
|
|
|
next_stream = stream->closed_next;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (prev_stream) {
|
2014-11-07 16:36:47 +01:00
|
|
|
prev_stream->closed_next = next_stream;
|
|
|
|
} else {
|
2014-12-12 16:03:19 +01:00
|
|
|
session->idle_stream_head = next_stream;
|
2014-11-07 16:36:47 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (next_stream) {
|
2014-11-07 16:36:47 +01:00
|
|
|
next_stream->closed_prev = prev_stream;
|
|
|
|
} else {
|
2014-12-12 16:03:19 +01:00
|
|
|
session->idle_stream_tail = prev_stream;
|
2014-11-07 16:36:47 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
stream->closed_prev = NULL;
|
|
|
|
stream->closed_next = NULL;
|
|
|
|
|
2014-12-12 16:03:19 +01:00
|
|
|
--session->num_idle_streams;
|
2014-11-07 16:36:47 +01:00
|
|
|
}
|
|
|
|
|
2015-12-23 08:38:30 +01:00
|
|
|
int nghttp2_session_adjust_closed_stream(nghttp2_session *session) {
|
2014-05-16 15:32:08 +02:00
|
|
|
size_t num_stream_max;
|
2015-08-16 12:01:10 +02:00
|
|
|
int rv;
|
2014-05-16 15:32:08 +02:00
|
|
|
|
More strict stream state handling
Previously, in server side, we used closed streams to detect the error
that the misbehaving client sends a frame on the incoming stream it
explicitly closed. With this commit, we make a further step, and
detect one more error case. Since we retain closed streams as long as
the sum of its size and the number of opened streams are equal or less
than max concurrent streams, we can safely say that if we get a frame
which is sent on the stream that is not found in either closed or
opened stream, it is already closed or has not existed. Then we can
send GOAWAY.
The previous code shrinks closed streams when we closed another
stream, but now it is removed. It is enough to adjust closed streams
when new incoming stream is created.
While creating this commit, we noticed that
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS is defined as INT32_MAX. But
since SETTINGS can contain value up to UINT32_MAX, it is not enough.
However, since the stream ID space is limited to INT32_MAX, it is high
enough. We could keep this value, but this time we deprecate
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS macro. While it is in public
header, the effect of deprecating it is negligible because of the
reason we wrote above, and usually application sets much smaller value
(say, 100) as SETTINGS_MAX_CONCURRENT_STREAMS.
2016-08-07 07:34:13 +02:00
|
|
|
if (session->local_settings.max_concurrent_streams ==
|
|
|
|
NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS) {
|
|
|
|
num_stream_max = session->pending_local_max_concurrent_stream;
|
|
|
|
} else {
|
|
|
|
num_stream_max = session->local_settings.max_concurrent_streams;
|
|
|
|
}
|
2014-05-16 15:32:08 +02:00
|
|
|
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("stream: adjusting kept closed streams num_closed_streams=%zu, "
|
|
|
|
"num_incoming_streams=%zu, max_concurrent_streams=%zu\n",
|
2016-10-12 18:11:59 +02:00
|
|
|
session->num_closed_streams, session->num_incoming_streams,
|
|
|
|
num_stream_max);
|
2014-03-30 10:41:54 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
while (session->num_closed_streams > 0 &&
|
2015-12-23 08:38:30 +01:00
|
|
|
session->num_closed_streams + session->num_incoming_streams >
|
2014-11-27 15:39:04 +01:00
|
|
|
num_stream_max) {
|
2014-03-30 10:41:54 +02:00
|
|
|
nghttp2_stream *head_stream;
|
2015-08-16 12:01:10 +02:00
|
|
|
nghttp2_stream *next;
|
2014-03-30 10:41:54 +02:00
|
|
|
|
|
|
|
head_stream = session->closed_stream_head;
|
|
|
|
|
2014-11-30 11:52:05 +01:00
|
|
|
assert(head_stream);
|
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
next = head_stream->closed_next;
|
|
|
|
|
|
|
|
rv = nghttp2_session_destroy_stream(session, head_stream);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* head_stream is now freed */
|
|
|
|
|
|
|
|
session->closed_stream_head = next;
|
2014-03-30 10:41:54 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->closed_stream_head) {
|
2014-11-07 16:36:47 +01:00
|
|
|
session->closed_stream_head->closed_prev = NULL;
|
|
|
|
} else {
|
2014-03-30 10:41:54 +02:00
|
|
|
session->closed_stream_tail = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
--session->num_closed_streams;
|
|
|
|
}
|
2015-08-16 12:01:10 +02:00
|
|
|
|
|
|
|
return 0;
|
2012-01-25 17:04:01 +01:00
|
|
|
}
|
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
int nghttp2_session_adjust_idle_stream(nghttp2_session *session) {
|
2014-12-12 16:03:19 +01:00
|
|
|
size_t max;
|
2015-08-16 12:01:10 +02:00
|
|
|
int rv;
|
2014-12-12 16:03:19 +01:00
|
|
|
|
2016-02-22 17:09:45 +01:00
|
|
|
/* Make minimum number of idle streams 16, and maximum 100, which
|
|
|
|
are arbitrary chosen numbers. */
|
|
|
|
max = nghttp2_min(
|
|
|
|
100, nghttp2_max(
|
|
|
|
16, nghttp2_min(session->local_settings.max_concurrent_streams,
|
|
|
|
session->pending_local_max_concurrent_stream)));
|
2014-12-12 16:03:19 +01:00
|
|
|
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("stream: adjusting kept idle streams num_idle_streams=%zu, max=%zu\n",
|
2016-10-12 18:11:59 +02:00
|
|
|
session->num_idle_streams, max);
|
2014-12-12 16:03:19 +01:00
|
|
|
|
|
|
|
while (session->num_idle_streams > max) {
|
|
|
|
nghttp2_stream *head;
|
2015-08-16 12:01:10 +02:00
|
|
|
nghttp2_stream *next;
|
2014-12-12 16:03:19 +01:00
|
|
|
|
|
|
|
head = session->idle_stream_head;
|
|
|
|
assert(head);
|
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
next = head->closed_next;
|
|
|
|
|
|
|
|
rv = nghttp2_session_destroy_stream(session, head);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* head is now destroyed */
|
|
|
|
|
|
|
|
session->idle_stream_head = next;
|
2014-12-12 16:03:19 +01:00
|
|
|
|
|
|
|
if (session->idle_stream_head) {
|
|
|
|
session->idle_stream_head->closed_prev = NULL;
|
|
|
|
} else {
|
|
|
|
session->idle_stream_tail = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
--session->num_idle_streams;
|
|
|
|
}
|
2015-08-16 12:01:10 +02:00
|
|
|
|
|
|
|
return 0;
|
2014-12-12 16:03:19 +01:00
|
|
|
}
|
|
|
|
|
2013-08-29 15:58:05 +02:00
|
|
|
/*
|
|
|
|
* Closes stream with stream ID |stream_id| if both transmission and
|
|
|
|
* reception of the stream were disallowed. The |error_code| indicates
|
|
|
|
* the reason of the closure.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_INVALID_ARGUMENT
|
|
|
|
* The stream is not found.
|
|
|
|
* NGHTTP2_ERR_CALLBACK_FAILURE
|
|
|
|
* The callback function failed.
|
|
|
|
*/
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_close_stream_if_shut_rdwr(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
|
|
|
if ((stream->shut_flags & NGHTTP2_SHUT_RDWR) == NGHTTP2_SHUT_RDWR) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return nghttp2_session_close_stream(session, stream->stream_id,
|
2013-07-15 14:45:59 +02:00
|
|
|
NGHTTP2_NO_ERROR);
|
2012-01-28 16:08:51 +01:00
|
|
|
}
|
2013-10-04 15:42:34 +02:00
|
|
|
return 0;
|
2012-01-28 16:08:51 +01:00
|
|
|
}
|
|
|
|
|
2015-12-11 13:23:49 +01:00
|
|
|
/*
|
|
|
|
* Returns nonzero if local endpoint allows reception of new stream
|
|
|
|
* from remote.
|
|
|
|
*/
|
|
|
|
static int session_allow_incoming_new_stream(nghttp2_session *session) {
|
|
|
|
return (session->goaway_flags &
|
|
|
|
(NGHTTP2_GOAWAY_TERM_ON_SEND | NGHTTP2_GOAWAY_SENT)) == 0;
|
|
|
|
}
|
|
|
|
|
2015-01-15 14:32:29 +01:00
|
|
|
/*
|
|
|
|
* This function returns nonzero if session is closing.
|
|
|
|
*/
|
|
|
|
static int session_is_closing(nghttp2_session *session) {
|
2016-02-02 16:25:11 +01:00
|
|
|
return (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) != 0 ||
|
|
|
|
(nghttp2_session_want_read(session) == 0 &&
|
|
|
|
nghttp2_session_want_write(session) == 0);
|
2015-01-15 14:32:29 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
/*
|
|
|
|
* Check that we can send a frame to the |stream|. This function
|
|
|
|
* returns 0 if we can send a frame to the |frame|, or one of the
|
|
|
|
* following negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_STREAM_CLOSED
|
|
|
|
* The stream is already closed.
|
|
|
|
* NGHTTP2_ERR_STREAM_SHUT_WR
|
|
|
|
* The stream is half-closed for transmission.
|
2015-01-15 14:32:29 +01:00
|
|
|
* NGHTTP2_ERR_SESSION_CLOSING
|
|
|
|
* This session is closing.
|
2013-07-15 14:45:59 +02:00
|
|
|
*/
|
2015-01-15 14:32:29 +01:00
|
|
|
static int session_predicate_for_stream_send(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream) {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == NULL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_CLOSED;
|
2013-10-04 15:42:34 +02:00
|
|
|
}
|
2015-01-15 14:32:29 +01:00
|
|
|
if (session_is_closing(session)) {
|
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->shut_flags & NGHTTP2_SHUT_WR) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_SHUT_WR;
|
2012-03-07 16:18:18 +01:00
|
|
|
}
|
2013-10-04 15:42:34 +02:00
|
|
|
return 0;
|
2012-03-07 16:18:18 +01:00
|
|
|
}
|
|
|
|
|
2015-10-30 14:48:27 +01:00
|
|
|
int nghttp2_session_check_request_allowed(nghttp2_session *session) {
|
2015-10-29 14:49:27 +01:00
|
|
|
return !session->server && session->next_stream_id <= INT32_MAX &&
|
2016-02-02 16:25:11 +01:00
|
|
|
(session->goaway_flags & NGHTTP2_GOAWAY_RECV) == 0 &&
|
|
|
|
!session_is_closing(session);
|
2015-10-29 14:49:27 +01:00
|
|
|
}
|
|
|
|
|
2012-03-11 10:52:42 +01:00
|
|
|
/*
|
2015-02-13 15:41:48 +01:00
|
|
|
* This function checks request HEADERS frame, which opens stream, can
|
2013-07-15 14:45:59 +02:00
|
|
|
* be sent at this time.
|
2012-03-11 10:52:42 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2013-07-15 14:45:59 +02:00
|
|
|
* NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
|
2014-11-28 14:59:13 +01:00
|
|
|
* New stream cannot be created because of GOAWAY: session is
|
|
|
|
* going down or received last_stream_id is strictly less than
|
|
|
|
* frame->hd.stream_id.
|
2015-02-13 15:41:48 +01:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSING
|
|
|
|
* request HEADERS was canceled by RST_STREAM while it is in queue.
|
2012-03-11 10:52:42 +01:00
|
|
|
*/
|
2015-02-13 15:41:48 +01:00
|
|
|
static int session_predicate_request_headers_send(nghttp2_session *session,
|
|
|
|
nghttp2_outbound_item *item) {
|
|
|
|
if (item->aux_data.headers.canceled) {
|
|
|
|
return NGHTTP2_ERR_STREAM_CLOSING;
|
|
|
|
}
|
2016-02-02 16:25:11 +01:00
|
|
|
/* If we are terminating session (NGHTTP2_GOAWAY_TERM_ON_SEND),
|
|
|
|
GOAWAY was received from peer, or session is about to close, new
|
|
|
|
request is not allowed. */
|
|
|
|
if ((session->goaway_flags & NGHTTP2_GOAWAY_RECV) ||
|
|
|
|
session_is_closing(session)) {
|
2013-07-15 14:45:59 +02:00
|
|
|
return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
|
2012-03-11 10:52:42 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-27 09:09:40 +01:00
|
|
|
/*
|
2013-07-15 14:45:59 +02:00
|
|
|
* This function checks HEADERS, which is the first frame from the
|
2014-11-12 13:47:26 +01:00
|
|
|
* server, with the |stream| can be sent at this time. The |stream|
|
|
|
|
* can be NULL.
|
2012-03-07 16:18:18 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSED
|
2012-03-07 16:18:18 +01:00
|
|
|
* The stream is already closed or does not exist.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_SHUT_WR
|
2012-03-07 16:18:18 +01:00
|
|
|
* The transmission is not allowed for this stream (e.g., a frame
|
2013-07-25 14:18:13 +02:00
|
|
|
* with END_STREAM flag set has already sent)
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_INVALID_STREAM_ID
|
2012-03-07 16:18:18 +01:00
|
|
|
* The stream ID is invalid.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSING
|
2012-03-07 16:18:18 +01:00
|
|
|
* RST_STREAM was queued for this stream.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_INVALID_STREAM_STATE
|
2013-07-25 14:18:13 +02:00
|
|
|
* The state of the stream is not valid.
|
2015-01-15 14:32:29 +01:00
|
|
|
* NGHTTP2_ERR_SESSION_CLOSING
|
2016-01-10 16:31:52 +01:00
|
|
|
* This session is closing.
|
|
|
|
* NGHTTP2_ERR_PROTO
|
|
|
|
* Client side attempted to send response.
|
2012-01-27 09:09:40 +01:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_predicate_response_headers_send(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2015-01-15 14:32:29 +01:00
|
|
|
rv = session_predicate_for_stream_send(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-27 09:09:40 +01:00
|
|
|
}
|
2014-11-12 13:47:26 +01:00
|
|
|
assert(stream);
|
2016-01-10 16:31:52 +01:00
|
|
|
if (!session->server) {
|
|
|
|
return NGHTTP2_ERR_PROTO;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_STREAM_ID;
|
2013-10-04 15:42:34 +02:00
|
|
|
}
|
2017-04-26 15:31:43 +02:00
|
|
|
switch (stream->state) {
|
|
|
|
case NGHTTP2_STREAM_OPENING:
|
2013-09-05 16:17:16 +02:00
|
|
|
return 0;
|
2017-04-26 15:31:43 +02:00
|
|
|
case NGHTTP2_STREAM_CLOSING:
|
2013-09-05 16:17:16 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_CLOSING;
|
2017-04-26 15:31:43 +02:00
|
|
|
default:
|
|
|
|
return NGHTTP2_ERR_INVALID_STREAM_STATE;
|
2012-01-27 09:09:40 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 18:49:05 +02:00
|
|
|
/*
|
|
|
|
* This function checks HEADERS for reserved stream can be sent. The
|
2014-11-12 13:47:26 +01:00
|
|
|
* |stream| must be reserved state and the |session| is server side.
|
|
|
|
* The |stream| can be NULL.
|
2013-07-24 18:49:05 +02:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_STREAM_CLOSED
|
|
|
|
* The stream is already closed.
|
|
|
|
* NGHTTP2_ERR_STREAM_SHUT_WR
|
|
|
|
* The stream is half-closed for transmission.
|
|
|
|
* NGHTTP2_ERR_PROTO
|
2014-01-09 13:39:29 +01:00
|
|
|
* The stream is not reserved state
|
2013-07-24 18:49:05 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSED
|
|
|
|
* RST_STREAM was queued for this stream.
|
2015-01-15 14:32:29 +01:00
|
|
|
* NGHTTP2_ERR_SESSION_CLOSING
|
|
|
|
* This session is closing.
|
2015-11-16 14:47:12 +01:00
|
|
|
* NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
|
|
|
|
* New stream cannot be created because GOAWAY is already sent or
|
|
|
|
* received.
|
2016-01-10 16:31:52 +01:00
|
|
|
* NGHTTP2_ERR_PROTO
|
|
|
|
* Client side attempted to send push response.
|
2013-07-24 18:49:05 +02:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int
|
2015-01-15 14:32:29 +01:00
|
|
|
session_predicate_push_response_headers_send(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2013-07-24 18:49:05 +02:00
|
|
|
/* TODO Should disallow HEADERS if GOAWAY has already been issued? */
|
2015-01-15 14:32:29 +01:00
|
|
|
rv = session_predicate_for_stream_send(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
2014-11-12 13:47:26 +01:00
|
|
|
assert(stream);
|
2016-01-10 16:31:52 +01:00
|
|
|
if (!session->server) {
|
|
|
|
return NGHTTP2_ERR_PROTO;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state != NGHTTP2_STREAM_RESERVED) {
|
2013-07-24 18:49:05 +02:00
|
|
|
return NGHTTP2_ERR_PROTO;
|
|
|
|
}
|
2015-11-16 14:47:12 +01:00
|
|
|
if (session->goaway_flags & NGHTTP2_GOAWAY_RECV) {
|
|
|
|
return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
|
|
|
|
}
|
2013-07-24 18:49:05 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-23 16:02:29 +01:00
|
|
|
/*
|
2015-01-15 14:32:29 +01:00
|
|
|
* This function checks HEADERS, which is neither stream-opening nor
|
|
|
|
* first response header, with the |stream| can be sent at this time.
|
2014-11-12 13:47:26 +01:00
|
|
|
* The |stream| can be NULL.
|
2012-03-07 16:18:18 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSED
|
2012-03-07 16:18:18 +01:00
|
|
|
* The stream is already closed or does not exist.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_SHUT_WR
|
2012-03-07 16:18:18 +01:00
|
|
|
* The transmission is not allowed for this stream (e.g., a frame
|
2013-07-25 14:18:13 +02:00
|
|
|
* with END_STREAM flag set has already sent)
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSING
|
2012-03-07 16:18:18 +01:00
|
|
|
* RST_STREAM was queued for this stream.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_INVALID_STREAM_STATE
|
2013-07-25 14:18:13 +02:00
|
|
|
* The state of the stream is not valid.
|
2015-01-15 14:32:29 +01:00
|
|
|
* NGHTTP2_ERR_SESSION_CLOSING
|
|
|
|
* This session is closing.
|
2012-02-23 16:02:29 +01:00
|
|
|
*/
|
2015-01-15 14:32:29 +01:00
|
|
|
static int session_predicate_headers_send(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2015-01-15 14:32:29 +01:00
|
|
|
rv = session_predicate_for_stream_send(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-02-23 16:02:29 +01:00
|
|
|
}
|
2014-11-12 13:47:26 +01:00
|
|
|
assert(stream);
|
2017-04-26 15:31:43 +02:00
|
|
|
|
|
|
|
switch (stream->state) {
|
|
|
|
case NGHTTP2_STREAM_OPENED:
|
2013-10-04 15:42:34 +02:00
|
|
|
return 0;
|
2017-04-26 15:31:43 +02:00
|
|
|
case NGHTTP2_STREAM_CLOSING:
|
2013-09-05 16:17:16 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_CLOSING;
|
2017-04-26 15:31:43 +02:00
|
|
|
default:
|
|
|
|
if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return NGHTTP2_ERR_INVALID_STREAM_STATE;
|
2012-02-23 16:02:29 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-07-24 18:49:05 +02:00
|
|
|
/*
|
2014-11-12 13:47:26 +01:00
|
|
|
* This function checks PUSH_PROMISE frame |frame| with the |stream|
|
|
|
|
* can be sent at this time. The |stream| can be NULL.
|
2013-07-24 18:49:05 +02:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_START_STREAM_NOT_ALLOWED
|
|
|
|
* New stream cannot be created because GOAWAY is already sent or
|
|
|
|
* received.
|
|
|
|
* NGHTTP2_ERR_PROTO
|
|
|
|
* The client side attempts to send PUSH_PROMISE, or the server
|
|
|
|
* sends PUSH_PROMISE for the stream not initiated by the client.
|
|
|
|
* NGHTTP2_ERR_STREAM_CLOSED
|
|
|
|
* The stream is already closed or does not exist.
|
|
|
|
* NGHTTP2_ERR_STREAM_CLOSING
|
|
|
|
* RST_STREAM was queued for this stream.
|
|
|
|
* NGHTTP2_ERR_STREAM_SHUT_WR
|
|
|
|
* The transmission is not allowed for this stream (e.g., a frame
|
|
|
|
* with END_STREAM flag set has already sent)
|
2013-11-02 08:53:06 +01:00
|
|
|
* NGHTTP2_ERR_PUSH_DISABLED
|
|
|
|
* The remote peer disabled reception of PUSH_PROMISE.
|
2015-01-15 14:32:29 +01:00
|
|
|
* NGHTTP2_ERR_SESSION_CLOSING
|
|
|
|
* This session is closing.
|
2013-07-24 18:49:05 +02:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_predicate_push_promise_send(nghttp2_session *session,
|
2015-01-07 17:33:09 +01:00
|
|
|
nghttp2_stream *stream) {
|
2013-07-24 18:49:05 +02:00
|
|
|
int rv;
|
2014-11-12 13:47:26 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!session->server) {
|
2014-02-14 08:12:04 +01:00
|
|
|
return NGHTTP2_ERR_PROTO;
|
|
|
|
}
|
2014-11-12 13:47:26 +01:00
|
|
|
|
2015-01-15 14:32:29 +01:00
|
|
|
rv = session_predicate_for_stream_send(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-24 18:49:05 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-11-12 13:47:26 +01:00
|
|
|
|
|
|
|
assert(stream);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->remote_settings.enable_push == 0) {
|
2013-11-02 08:53:06 +01:00
|
|
|
return NGHTTP2_ERR_PUSH_DISABLED;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_CLOSING) {
|
2013-07-24 18:49:05 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_CLOSING;
|
|
|
|
}
|
2015-01-15 14:32:29 +01:00
|
|
|
if (session->goaway_flags & NGHTTP2_GOAWAY_RECV) {
|
2013-07-24 18:49:05 +02:00
|
|
|
return NGHTTP2_ERR_START_STREAM_NOT_ALLOWED;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-25 16:12:32 +01:00
|
|
|
/*
|
2012-03-07 16:18:18 +01:00
|
|
|
* This function checks WINDOW_UPDATE with the stream ID |stream_id|
|
2013-07-25 14:18:13 +02:00
|
|
|
* can be sent at this time. Note that END_STREAM flag of the previous
|
|
|
|
* frame does not affect the transmission of the WINDOW_UPDATE frame.
|
2012-03-07 16:18:18 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSED
|
2012-03-07 16:18:18 +01:00
|
|
|
* The stream is already closed or does not exist.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSING
|
2012-03-07 16:18:18 +01:00
|
|
|
* RST_STREAM was queued for this stream.
|
2013-10-04 14:59:44 +02:00
|
|
|
* NGHTTP2_ERR_INVALID_STREAM_STATE
|
|
|
|
* The state of the stream is not valid.
|
2015-01-15 14:32:29 +01:00
|
|
|
* NGHTTP2_ERR_SESSION_CLOSING
|
|
|
|
* This session is closing.
|
2012-02-25 16:12:32 +01:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_predicate_window_update_send(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_stream *stream;
|
2016-03-29 16:30:55 +02:00
|
|
|
|
|
|
|
if (session_is_closing(session)) {
|
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream_id == 0) {
|
2013-07-15 14:45:59 +02:00
|
|
|
/* Connection-level window update */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == NULL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_CLOSED;
|
2012-03-07 16:18:18 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_CLOSING) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_CLOSING;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (state_reserved_local(session, stream)) {
|
2013-10-04 14:59:44 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_STREAM_STATE;
|
|
|
|
}
|
|
|
|
return 0;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
|
|
|
|
2016-03-29 16:29:26 +02:00
|
|
|
static int session_predicate_altsvc_send(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
if (session_is_closing(session)) {
|
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream_id == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
if (stream == NULL) {
|
|
|
|
return NGHTTP2_ERR_STREAM_CLOSED;
|
|
|
|
}
|
2016-04-14 17:59:05 +02:00
|
|
|
if (stream->state == NGHTTP2_STREAM_CLOSING) {
|
|
|
|
return NGHTTP2_ERR_STREAM_CLOSING;
|
|
|
|
}
|
2016-03-29 16:29:26 +02:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:57:02 +02:00
|
|
|
static int session_predicate_origin_send(nghttp2_session *session) {
|
|
|
|
if (session_is_closing(session)) {
|
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-25 15:41:34 +02:00
|
|
|
/* Take into account settings max frame size and both connection-level
|
|
|
|
flow control here */
|
2014-11-27 15:39:04 +01:00
|
|
|
static ssize_t
|
|
|
|
nghttp2_session_enforce_flow_control_limits(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream,
|
|
|
|
ssize_t requested_window_size) {
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("send: remote windowsize connection=%d, remote maxframsize=%u, "
|
|
|
|
"stream(id %d)=%d\n",
|
2016-10-14 15:33:08 +02:00
|
|
|
session->remote_window_size, session->remote_settings.max_frame_size,
|
|
|
|
stream->stream_id, stream->remote_window_size);
|
2014-02-11 13:35:41 +01:00
|
|
|
|
2014-08-25 15:41:34 +02:00
|
|
|
return nghttp2_min(nghttp2_min(nghttp2_min(requested_window_size,
|
|
|
|
stream->remote_window_size),
|
|
|
|
session->remote_window_size),
|
2014-10-19 15:40:39 +02:00
|
|
|
(int32_t)session->remote_settings.max_frame_size);
|
2014-08-24 17:32:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns the maximum length of next data read. If the
|
|
|
|
* connection-level and/or stream-wise flow control are enabled, the
|
|
|
|
* return value takes into account those current window sizes. The remote
|
|
|
|
* settings for max frame size is also taken into account.
|
|
|
|
*/
|
|
|
|
static size_t nghttp2_session_next_data_read(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-08-25 15:41:34 +02:00
|
|
|
ssize_t window_size;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
window_size = nghttp2_session_enforce_flow_control_limits(
|
|
|
|
session, stream, NGHTTP2_DATA_PAYLOADLEN);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: available window=%zd\n", window_size);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
return window_size > 0 ? (size_t)window_size : 0;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
|
|
|
|
2012-03-07 16:18:18 +01:00
|
|
|
/*
|
2014-11-12 13:47:26 +01:00
|
|
|
* This function checks DATA with the |stream| can be sent at this
|
|
|
|
* time. The |stream| can be NULL.
|
2012-03-07 16:18:18 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSED
|
2012-03-07 16:18:18 +01:00
|
|
|
* The stream is already closed or does not exist.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_SHUT_WR
|
2012-03-07 16:18:18 +01:00
|
|
|
* The transmission is not allowed for this stream (e.g., a frame
|
2013-07-25 14:18:13 +02:00
|
|
|
* with END_STREAM flag set has already sent)
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_STREAM_CLOSING
|
2012-03-07 16:18:18 +01:00
|
|
|
* RST_STREAM was queued for this stream.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_INVALID_STREAM_STATE
|
2013-07-25 14:18:13 +02:00
|
|
|
* The state of the stream is not valid.
|
2015-01-15 14:32:29 +01:00
|
|
|
* NGHTTP2_ERR_SESSION_CLOSING
|
|
|
|
* This session is closing.
|
2012-03-07 16:18:18 +01:00
|
|
|
*/
|
2013-07-12 17:19:03 +02:00
|
|
|
static int nghttp2_session_predicate_data_send(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2015-01-15 14:32:29 +01:00
|
|
|
rv = session_predicate_for_stream_send(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-27 09:09:40 +01:00
|
|
|
}
|
2014-11-12 13:47:26 +01:00
|
|
|
assert(stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
|
2013-10-04 14:59:44 +02:00
|
|
|
/* Request body data */
|
2013-07-12 17:19:03 +02:00
|
|
|
/* If stream->state is NGHTTP2_STREAM_CLOSING, RST_STREAM was
|
2012-02-14 17:07:51 +01:00
|
|
|
queued but not yet sent. In this case, we won't send DATA
|
2013-10-04 14:59:44 +02:00
|
|
|
frames. */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_CLOSING) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_CLOSING;
|
2012-03-07 16:18:18 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_RESERVED) {
|
2013-10-04 14:59:44 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_STREAM_STATE;
|
|
|
|
}
|
2013-09-05 16:17:16 +02:00
|
|
|
return 0;
|
2013-10-04 14:59:44 +02:00
|
|
|
}
|
|
|
|
/* Response body data */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_OPENED) {
|
2013-10-04 14:59:44 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_CLOSING) {
|
2013-09-05 16:17:16 +02:00
|
|
|
return NGHTTP2_ERR_STREAM_CLOSING;
|
2012-01-27 09:09:40 +01:00
|
|
|
}
|
2013-10-04 14:59:44 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_STREAM_STATE;
|
2012-01-27 09:09:40 +01:00
|
|
|
}
|
|
|
|
|
2014-02-11 07:28:44 +01:00
|
|
|
static ssize_t session_call_select_padding(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame,
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t max_payloadlen) {
|
2014-02-11 07:28:44 +01:00
|
|
|
ssize_t rv;
|
2014-06-07 11:15:36 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.length >= max_payloadlen) {
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)frame->hd.length;
|
2014-06-07 11:15:36 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.select_padding_callback) {
|
2014-06-07 11:15:36 +02:00
|
|
|
size_t max_paddedlen;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
max_paddedlen =
|
|
|
|
nghttp2_min(frame->hd.length + NGHTTP2_MAX_PADLEN, max_payloadlen);
|
2014-06-07 11:15:36 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = session->callbacks.select_padding_callback(
|
|
|
|
session, frame, max_paddedlen, session->user_data);
|
|
|
|
if (rv < (ssize_t)frame->hd.length || rv > (ssize_t)max_paddedlen) {
|
2014-02-11 07:28:44 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)frame->hd.length;
|
2014-02-11 07:28:44 +01:00
|
|
|
}
|
|
|
|
|
2014-02-15 08:30:43 +01:00
|
|
|
/* Add padding to HEADERS or PUSH_PROMISE. We use
|
|
|
|
frame->headers.padlen in this function to use the fact that
|
|
|
|
frame->push_promise has also padlen in the same position. */
|
2014-03-13 14:11:02 +01:00
|
|
|
static int session_headers_add_pad(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2014-02-15 08:30:43 +01:00
|
|
|
int rv;
|
|
|
|
ssize_t padded_payloadlen;
|
2014-03-10 17:47:38 +01:00
|
|
|
nghttp2_active_outbound_item *aob;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs *framebufs;
|
|
|
|
size_t padlen;
|
2014-06-25 14:17:03 +02:00
|
|
|
size_t max_payloadlen;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
|
|
|
aob = &session->aob;
|
2014-03-13 14:11:02 +01:00
|
|
|
framebufs = &aob->framebufs;
|
2014-02-15 08:30:43 +01:00
|
|
|
|
2014-08-25 15:41:34 +02:00
|
|
|
max_payloadlen = nghttp2_min(NGHTTP2_MAX_PAYLOADLEN,
|
|
|
|
frame->hd.length + NGHTTP2_MAX_PADLEN);
|
2014-06-25 14:17:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
padded_payloadlen =
|
|
|
|
session_call_select_padding(session, frame, max_payloadlen);
|
2014-06-25 14:17:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal((int)padded_payloadlen)) {
|
2014-06-11 16:37:16 +02:00
|
|
|
return (int)padded_payloadlen;
|
2014-02-15 08:30:43 +01:00
|
|
|
}
|
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
padlen = (size_t)padded_payloadlen - frame->hd.length;
|
2014-02-15 08:30:43 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: padding selected: payloadlen=%zd, padlen=%zu\n",
|
|
|
|
padded_payloadlen, padlen);
|
2014-02-15 08:30:43 +01:00
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
rv = nghttp2_frame_add_pad(framebufs, &frame->hd, padlen, 0);
|
2014-06-07 11:15:36 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return rv;
|
2014-02-15 08:30:43 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
frame->headers.padlen = padlen;
|
|
|
|
|
|
|
|
return 0;
|
2014-02-15 08:30:43 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static size_t session_estimate_headers_payload(nghttp2_session *session,
|
|
|
|
const nghttp2_nv *nva,
|
|
|
|
size_t nvlen,
|
|
|
|
size_t additional) {
|
|
|
|
return nghttp2_hd_deflate_bound(&session->hd_deflater, nva, nvlen) +
|
|
|
|
additional;
|
2014-07-02 15:45:38 +02:00
|
|
|
}
|
|
|
|
|
2015-10-10 15:27:48 +02:00
|
|
|
static int session_pack_extension(nghttp2_session *session, nghttp2_bufs *bufs,
|
|
|
|
nghttp2_frame *frame) {
|
|
|
|
ssize_t rv;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
size_t buflen;
|
|
|
|
size_t framelen;
|
|
|
|
|
|
|
|
assert(session->callbacks.pack_extension_callback);
|
|
|
|
|
|
|
|
buf = &bufs->head->buf;
|
|
|
|
buflen = nghttp2_min(nghttp2_buf_avail(buf), NGHTTP2_MAX_PAYLOADLEN);
|
|
|
|
|
2015-11-17 13:29:21 +01:00
|
|
|
rv = session->callbacks.pack_extension_callback(session, buf->last, buflen,
|
|
|
|
frame, session->user_data);
|
2015-10-10 15:27:48 +02:00
|
|
|
if (rv == NGHTTP2_ERR_CANCEL) {
|
|
|
|
return (int)rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv < 0 || (size_t)rv > buflen) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
framelen = (size_t)rv;
|
|
|
|
|
|
|
|
frame->hd.length = framelen;
|
|
|
|
|
|
|
|
assert(buf->pos == buf->last);
|
|
|
|
buf->last += framelen;
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
|
|
|
|
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-03-10 18:15:05 +01:00
|
|
|
/*
|
|
|
|
* This function serializes frame for transmission.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of negative error
|
|
|
|
* codes, including both fatal and non-fatal ones.
|
|
|
|
*/
|
2014-05-08 16:37:56 +02:00
|
|
|
static int session_prep_frame(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_outbound_item *item) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_frame *frame;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
2014-10-02 17:59:44 +02:00
|
|
|
frame = &item->frame;
|
2014-03-10 18:15:05 +01:00
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
switch (frame->hd.type) {
|
|
|
|
case NGHTTP2_DATA: {
|
2012-02-28 13:40:19 +01:00
|
|
|
size_t next_readmax;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_stream *stream;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-10-02 17:59:44 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream) {
|
2014-12-20 11:53:57 +01:00
|
|
|
assert(stream->item == item);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-12 13:47:26 +01:00
|
|
|
rv = nghttp2_session_predicate_data_send(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2016-01-05 14:14:54 +01:00
|
|
|
// If stream was already closed, nghttp2_session_get_stream()
|
2015-07-31 14:11:16 +02:00
|
|
|
// returns NULL, but item is still attached to the stream.
|
|
|
|
// Search stream including closed again.
|
|
|
|
stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream) {
|
2014-10-30 15:31:28 +01:00
|
|
|
int rv2;
|
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
rv2 = nghttp2_stream_detach_item(stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv2)) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return rv2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-27 09:09:40 +01:00
|
|
|
}
|
2012-02-25 16:12:32 +01:00
|
|
|
/* Assuming stream is not NULL */
|
|
|
|
assert(stream);
|
2013-07-12 17:19:03 +02:00
|
|
|
next_readmax = nghttp2_session_next_data_read(session, stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (next_readmax == 0) {
|
2014-09-25 15:48:28 +02:00
|
|
|
|
|
|
|
/* This must be true since we only pop DATA frame item from
|
|
|
|
queue when session->remote_window_size > 0 */
|
|
|
|
assert(session->remote_window_size > 0);
|
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_defer_item(stream,
|
|
|
|
NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
|
2014-04-03 08:48:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-04-03 08:48:19 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-11 10:43:45 +01:00
|
|
|
session->aob.item = NULL;
|
2014-12-07 15:07:13 +01:00
|
|
|
active_outbound_item_reset(&session->aob, mem);
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2015-02-12 14:58:43 +01:00
|
|
|
rv = nghttp2_session_pack_data(session, &session->aob.framebufs,
|
2015-04-16 14:38:13 +02:00
|
|
|
next_readmax, frame, &item->aux_data.data,
|
|
|
|
stream);
|
2016-08-28 11:50:19 +02:00
|
|
|
if (rv == NGHTTP2_ERR_PAUSE) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-02-12 14:58:43 +01:00
|
|
|
if (rv == NGHTTP2_ERR_DEFERRED) {
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_defer_item(stream, NGHTTP2_STREAM_FLAG_DEFERRED_USER);
|
2014-04-03 08:48:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-04-03 08:48:19 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-02-11 10:43:45 +01:00
|
|
|
session->aob.item = NULL;
|
2014-12-07 15:07:13 +01:00
|
|
|
active_outbound_item_reset(&session->aob, mem);
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_DEFERRED;
|
2014-02-18 16:45:20 +01:00
|
|
|
}
|
2015-02-12 14:58:43 +01:00
|
|
|
if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_detach_item(stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-10-02 17:59:44 +02:00
|
|
|
rv = nghttp2_session_add_rst_stream(session, frame->hd.stream_id,
|
2014-02-18 16:45:20 +01:00
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
2015-02-12 14:58:43 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-05-12 11:19:05 +02:00
|
|
|
}
|
2015-02-12 14:58:43 +01:00
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
2012-01-26 17:17:40 +01:00
|
|
|
}
|
2015-02-12 14:58:43 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
int rv2;
|
2014-08-10 05:20:36 +02:00
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
rv2 = nghttp2_stream_detach_item(stream);
|
2015-02-12 14:58:43 +01:00
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv2)) {
|
|
|
|
return rv2;
|
2014-08-10 05:20:36 +02:00
|
|
|
}
|
|
|
|
|
2015-02-12 14:58:43 +01:00
|
|
|
return rv;
|
2014-03-10 18:15:05 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2016-09-11 09:55:15 +02:00
|
|
|
case NGHTTP2_HEADERS: {
|
|
|
|
nghttp2_headers_aux_data *aux_data;
|
|
|
|
size_t estimated_payloadlen;
|
|
|
|
|
|
|
|
aux_data = &item->aux_data.headers;
|
|
|
|
|
|
|
|
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
|
|
|
|
/* initial HEADERS, which opens stream */
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
stream = nghttp2_session_open_stream(
|
|
|
|
session, frame->hd.stream_id, NGHTTP2_STREAM_FLAG_NONE,
|
|
|
|
&frame->headers.pri_spec, NGHTTP2_STREAM_INITIAL,
|
|
|
|
aux_data->stream_user_data);
|
|
|
|
|
|
|
|
if (stream == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't call nghttp2_session_adjust_closed_stream() here,
|
|
|
|
since we don't keep closed stream in client side */
|
|
|
|
|
|
|
|
rv = session_predicate_request_headers_send(session, item);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session_enforce_http_messaging(session)) {
|
|
|
|
nghttp2_http_record_request_method(stream, frame);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
|
|
|
|
if (stream && stream->state == NGHTTP2_STREAM_RESERVED) {
|
|
|
|
rv = session_predicate_push_response_headers_send(session, stream);
|
|
|
|
if (rv == 0) {
|
|
|
|
frame->headers.cat = NGHTTP2_HCAT_PUSH_RESPONSE;
|
|
|
|
|
|
|
|
if (aux_data->stream_user_data) {
|
|
|
|
stream->stream_user_data = aux_data->stream_user_data;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else if (session_predicate_response_headers_send(session, stream) ==
|
|
|
|
0) {
|
|
|
|
frame->headers.cat = NGHTTP2_HCAT_RESPONSE;
|
|
|
|
rv = 0;
|
|
|
|
} else {
|
|
|
|
frame->headers.cat = NGHTTP2_HCAT_HEADERS;
|
|
|
|
|
|
|
|
rv = session_predicate_headers_send(session, stream);
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 15:15:53 +02:00
|
|
|
estimated_payloadlen = session_estimate_headers_payload(
|
|
|
|
session, frame->headers.nva, frame->headers.nvlen,
|
|
|
|
NGHTTP2_PRIORITY_SPECLEN);
|
|
|
|
|
|
|
|
if (estimated_payloadlen > session->max_send_header_block_length) {
|
|
|
|
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
rv = nghttp2_frame_pack_headers(&session->aob.framebufs, &frame->headers,
|
|
|
|
&session->hd_deflater);
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: before padding, HEADERS serialized in %zd bytes\n",
|
|
|
|
nghttp2_bufs_len(&session->aob.framebufs));
|
2016-09-11 09:55:15 +02:00
|
|
|
|
|
|
|
rv = session_headers_add_pad(session, frame);
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: HEADERS finally serialized in %zd bytes\n",
|
|
|
|
nghttp2_bufs_len(&session->aob.framebufs));
|
2016-09-11 09:55:15 +02:00
|
|
|
|
|
|
|
if (frame->headers.cat == NGHTTP2_HCAT_REQUEST) {
|
|
|
|
assert(session->last_sent_stream_id < frame->hd.stream_id);
|
|
|
|
session->last_sent_stream_id = frame->hd.stream_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case NGHTTP2_PRIORITY: {
|
|
|
|
if (session_is_closing(session)) {
|
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
|
|
|
/* PRIORITY frame can be sent at any time and to any stream
|
|
|
|
ID. */
|
|
|
|
nghttp2_frame_pack_priority(&session->aob.framebufs, &frame->priority);
|
|
|
|
|
|
|
|
/* Peer can send PRIORITY frame against idle stream to create
|
|
|
|
"anchor" in dependency tree. Only client can do this in
|
|
|
|
nghttp2. In nghttp2, only server retains non-active (closed
|
|
|
|
or idle) streams in memory, so we don't open stream here. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case NGHTTP2_RST_STREAM:
|
|
|
|
if (session_is_closing(session)) {
|
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
|
|
|
nghttp2_frame_pack_rst_stream(&session->aob.framebufs, &frame->rst_stream);
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_SETTINGS: {
|
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
|
|
|
|
assert(session->obq_flood_counter_ > 0);
|
|
|
|
--session->obq_flood_counter_;
|
|
|
|
/* When session is about to close, don't send SETTINGS ACK.
|
|
|
|
We are required to send SETTINGS without ACK though; for
|
|
|
|
example, we have to send SETTINGS as a part of connection
|
|
|
|
preface. */
|
|
|
|
if (session_is_closing(session)) {
|
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_frame_pack_settings(&session->aob.framebufs, &frame->settings);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case NGHTTP2_PUSH_PROMISE: {
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
size_t estimated_payloadlen;
|
|
|
|
|
|
|
|
/* stream could be NULL if associated stream was already
|
|
|
|
closed. */
|
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
|
2017-10-28 15:25:42 +02:00
|
|
|
/* predicate should fail if stream is NULL. */
|
2016-09-11 09:55:15 +02:00
|
|
|
rv = session_predicate_push_promise_send(session, stream);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(stream);
|
|
|
|
|
2017-04-26 15:15:53 +02:00
|
|
|
estimated_payloadlen = session_estimate_headers_payload(
|
|
|
|
session, frame->push_promise.nva, frame->push_promise.nvlen, 0);
|
|
|
|
|
|
|
|
if (estimated_payloadlen > session->max_send_header_block_length) {
|
|
|
|
return NGHTTP2_ERR_FRAME_SIZE_ERROR;
|
|
|
|
}
|
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
rv = nghttp2_frame_pack_push_promise(
|
|
|
|
&session->aob.framebufs, &frame->push_promise, &session->hd_deflater);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = session_headers_add_pad(session, frame);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
assert(session->last_sent_stream_id + 2 <=
|
|
|
|
frame->push_promise.promised_stream_id);
|
|
|
|
session->last_sent_stream_id = frame->push_promise.promised_stream_id;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case NGHTTP2_PING:
|
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
|
|
|
|
assert(session->obq_flood_counter_ > 0);
|
|
|
|
--session->obq_flood_counter_;
|
|
|
|
}
|
2018-01-08 09:03:48 +01:00
|
|
|
/* PING frame is allowed to be sent unless termination GOAWAY is
|
|
|
|
sent */
|
|
|
|
if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_ON_SEND) {
|
2016-09-11 09:55:15 +02:00
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
|
|
|
nghttp2_frame_pack_ping(&session->aob.framebufs, &frame->ping);
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_GOAWAY:
|
|
|
|
rv = nghttp2_frame_pack_goaway(&session->aob.framebufs, &frame->goaway);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
session->local_last_stream_id = frame->goaway.last_stream_id;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_WINDOW_UPDATE:
|
|
|
|
rv = session_predicate_window_update_send(session, frame->hd.stream_id);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
nghttp2_frame_pack_window_update(&session->aob.framebufs,
|
|
|
|
&frame->window_update);
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_CONTINUATION:
|
|
|
|
/* We never handle CONTINUATION here. */
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
default: {
|
|
|
|
nghttp2_ext_aux_data *aux_data;
|
|
|
|
|
|
|
|
/* extension frame */
|
|
|
|
|
|
|
|
aux_data = &item->aux_data.ext;
|
|
|
|
|
|
|
|
if (aux_data->builtin == 0) {
|
|
|
|
if (session_is_closing(session)) {
|
|
|
|
return NGHTTP2_ERR_SESSION_CLOSING;
|
|
|
|
}
|
|
|
|
|
|
|
|
return session_pack_extension(session, &session->aob.framebufs, frame);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (frame->hd.type) {
|
|
|
|
case NGHTTP2_ALTSVC:
|
|
|
|
rv = session_predicate_altsvc_send(session, frame->hd.stream_id);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_frame_pack_altsvc(&session->aob.framebufs, &frame->ext);
|
|
|
|
|
2018-05-10 03:57:02 +02:00
|
|
|
return 0;
|
|
|
|
case NGHTTP2_ORIGIN:
|
|
|
|
rv = session_predicate_origin_send(session);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = nghttp2_frame_pack_origin(&session->aob.framebufs, &frame->ext);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2016-09-11 09:55:15 +02:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
/* Unreachable here */
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_outbound_item *
|
|
|
|
nghttp2_session_get_next_ob_item(nghttp2_session *session) {
|
2015-04-24 15:30:32 +02:00
|
|
|
if (nghttp2_outbound_queue_top(&session->ob_urgent)) {
|
|
|
|
return nghttp2_outbound_queue_top(&session->ob_urgent);
|
2012-02-05 16:14:19 +01:00
|
|
|
}
|
2014-09-25 15:48:28 +02:00
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
if (nghttp2_outbound_queue_top(&session->ob_reg)) {
|
|
|
|
return nghttp2_outbound_queue_top(&session->ob_reg);
|
2014-09-25 15:48:28 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
if (!session_is_outgoing_concurrent_streams_max(session)) {
|
|
|
|
if (nghttp2_outbound_queue_top(&session->ob_syn)) {
|
|
|
|
return nghttp2_outbound_queue_top(&session->ob_syn);
|
|
|
|
}
|
|
|
|
}
|
2014-09-25 15:48:28 +02:00
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
if (session->remote_window_size > 0) {
|
|
|
|
return nghttp2_stream_next_outbound_item(&session->root);
|
2014-09-25 15:48:28 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
return NULL;
|
2012-02-05 16:14:19 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_outbound_item *
|
|
|
|
nghttp2_session_pop_next_ob_item(nghttp2_session *session) {
|
2015-04-24 15:30:32 +02:00
|
|
|
nghttp2_outbound_item *item;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
item = nghttp2_outbound_queue_top(&session->ob_urgent);
|
|
|
|
if (item) {
|
|
|
|
nghttp2_outbound_queue_pop(&session->ob_urgent);
|
2014-09-25 15:48:28 +02:00
|
|
|
item->queued = 0;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
item = nghttp2_outbound_queue_top(&session->ob_reg);
|
|
|
|
if (item) {
|
|
|
|
nghttp2_outbound_queue_pop(&session->ob_reg);
|
2014-09-25 15:48:28 +02:00
|
|
|
item->queued = 0;
|
|
|
|
return item;
|
2012-02-05 16:14:19 +01:00
|
|
|
}
|
2014-09-25 15:48:28 +02:00
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
if (!session_is_outgoing_concurrent_streams_max(session)) {
|
|
|
|
item = nghttp2_outbound_queue_top(&session->ob_syn);
|
|
|
|
if (item) {
|
|
|
|
nghttp2_outbound_queue_pop(&session->ob_syn);
|
|
|
|
item->queued = 0;
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
2014-09-25 15:48:28 +02:00
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
if (session->remote_window_size > 0) {
|
|
|
|
return nghttp2_stream_next_outbound_item(&session->root);
|
2014-09-25 15:48:28 +02:00
|
|
|
}
|
|
|
|
|
2015-04-24 15:30:32 +02:00
|
|
|
return NULL;
|
2012-02-05 16:14:19 +01:00
|
|
|
}
|
|
|
|
|
2014-01-27 15:28:45 +01:00
|
|
|
static int session_call_before_frame_send(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2014-01-27 15:28:45 +01:00
|
|
|
int rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.before_frame_send_callback) {
|
2014-01-27 15:28:45 +01:00
|
|
|
rv = session->callbacks.before_frame_send_callback(session, frame,
|
|
|
|
session->user_data);
|
2016-07-02 12:21:08 +02:00
|
|
|
if (rv == NGHTTP2_ERR_CANCEL) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-01-27 15:28:45 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-27 14:13:41 +01:00
|
|
|
static int session_call_on_frame_send(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2014-01-27 14:13:41 +01:00
|
|
|
int rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.on_frame_send_callback) {
|
2014-01-27 14:13:41 +01:00
|
|
|
rv = session->callbacks.on_frame_send_callback(session, frame,
|
|
|
|
session->user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-01-27 14:13:41 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-28 14:59:13 +01:00
|
|
|
static int find_stream_on_goaway_func(nghttp2_map_entry *entry, void *ptr) {
|
|
|
|
nghttp2_close_stream_on_goaway_arg *arg;
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
arg = (nghttp2_close_stream_on_goaway_arg *)ptr;
|
|
|
|
stream = (nghttp2_stream *)entry;
|
|
|
|
|
|
|
|
if (nghttp2_session_is_my_stream_id(arg->session, stream->stream_id)) {
|
|
|
|
if (arg->incoming) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
} else if (!arg->incoming) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->state != NGHTTP2_STREAM_IDLE &&
|
|
|
|
(stream->flags & NGHTTP2_STREAM_FLAG_CLOSED) == 0 &&
|
|
|
|
stream->stream_id > arg->last_stream_id) {
|
|
|
|
/* We are collecting streams to close because we cannot call
|
|
|
|
nghttp2_session_close_stream() inside nghttp2_map_each().
|
|
|
|
Reuse closed_next member.. bad choice? */
|
|
|
|
assert(stream->closed_next == NULL);
|
|
|
|
assert(stream->closed_prev == NULL);
|
|
|
|
|
|
|
|
if (arg->head) {
|
|
|
|
stream->closed_next = arg->head;
|
|
|
|
arg->head = stream;
|
|
|
|
} else {
|
|
|
|
arg->head = stream;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Closes non-idle and non-closed streams whose stream ID >
|
|
|
|
last_stream_id. If incoming is nonzero, we are going to close
|
|
|
|
incoming streams. Otherwise, close outgoing streams. */
|
|
|
|
static int session_close_stream_on_goaway(nghttp2_session *session,
|
|
|
|
int32_t last_stream_id,
|
|
|
|
int incoming) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_stream *stream, *next_stream;
|
|
|
|
nghttp2_close_stream_on_goaway_arg arg = {session, NULL, last_stream_id,
|
|
|
|
incoming};
|
|
|
|
|
|
|
|
rv = nghttp2_map_each(&session->streams, find_stream_on_goaway_func, &arg);
|
|
|
|
assert(rv == 0);
|
|
|
|
|
|
|
|
stream = arg.head;
|
|
|
|
while (stream) {
|
|
|
|
next_stream = stream->closed_next;
|
|
|
|
stream->closed_next = NULL;
|
2017-11-30 15:10:57 +01:00
|
|
|
rv = nghttp2_session_close_stream(session, stream->stream_id,
|
|
|
|
NGHTTP2_REFUSED_STREAM);
|
2014-11-28 14:59:13 +01:00
|
|
|
|
|
|
|
/* stream may be deleted here */
|
|
|
|
|
|
|
|
stream = next_stream;
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
/* Clean up closed_next member just in case */
|
|
|
|
while (stream) {
|
|
|
|
next_stream = stream->closed_next;
|
|
|
|
stream->closed_next = NULL;
|
|
|
|
stream = next_stream;
|
|
|
|
}
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
static void reschedule_stream(nghttp2_stream *stream) {
|
|
|
|
stream->last_writelen = stream->item->frame.hd.length;
|
2015-04-16 15:41:00 +02:00
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
nghttp2_stream_reschedule(stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2015-09-30 18:19:57 +02:00
|
|
|
static int session_update_stream_consumed_size(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream,
|
|
|
|
size_t delta_size);
|
|
|
|
|
|
|
|
static int session_update_connection_consumed_size(nghttp2_session *session,
|
|
|
|
size_t delta_size);
|
|
|
|
|
|
|
|
static int session_update_recv_connection_window_size(nghttp2_session *session,
|
|
|
|
size_t delta_size);
|
|
|
|
|
|
|
|
static int session_update_recv_stream_window_size(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream,
|
|
|
|
size_t delta_size,
|
|
|
|
int send_window_update);
|
|
|
|
|
2012-02-23 14:49:08 +01:00
|
|
|
/*
|
2014-12-18 13:52:17 +01:00
|
|
|
* Called after a frame is sent. This function runs
|
|
|
|
* on_frame_send_callback and handles stream closure upon END_STREAM
|
|
|
|
* or RST_STREAM. This function does not reset session->aob. It is a
|
|
|
|
* responsibility of session_after_frame_sent2.
|
2012-02-23 14:49:08 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_NOMEM
|
2012-02-23 14:49:08 +01:00
|
|
|
* Out of memory.
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_CALLBACK_FAILURE
|
2012-02-23 14:49:08 +01:00
|
|
|
* The callback function failed.
|
|
|
|
*/
|
2014-12-18 13:52:17 +01:00
|
|
|
static int session_after_frame_sent1(nghttp2_session *session) {
|
2014-01-27 15:00:08 +01:00
|
|
|
int rv;
|
2014-03-10 17:47:38 +01:00
|
|
|
nghttp2_active_outbound_item *aob = &session->aob;
|
|
|
|
nghttp2_outbound_item *item = aob->item;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs *framebufs = &aob->framebufs;
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_frame *frame;
|
2016-09-11 10:18:08 +02:00
|
|
|
nghttp2_stream *stream;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-10-02 17:59:44 +02:00
|
|
|
frame = &item->frame;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (frame->hd.type == NGHTTP2_DATA) {
|
|
|
|
nghttp2_data_aux_data *aux_data;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
aux_data = &item->aux_data.data;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
/* We update flow control window after a frame was completely
|
|
|
|
sent. This is possible because we choose payload length not to
|
|
|
|
exceed the window */
|
|
|
|
session->remote_window_size -= (int32_t)frame->hd.length;
|
|
|
|
if (stream) {
|
|
|
|
stream->remote_window_size -= (int32_t)frame->hd.length;
|
2012-01-27 20:28:39 +01:00
|
|
|
}
|
2014-12-18 13:52:17 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (stream && aux_data->eof) {
|
|
|
|
rv = nghttp2_stream_detach_item(stream);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2014-01-27 15:00:08 +01:00
|
|
|
}
|
2014-11-11 16:26:23 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
/* Call on_frame_send_callback after
|
|
|
|
nghttp2_stream_detach_item(), so that application can issue
|
|
|
|
nghttp2_submit_data() in the callback. */
|
|
|
|
if (session->callbacks.on_frame_send_callback) {
|
|
|
|
rv = session_call_on_frame_send(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-27 15:00:08 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
|
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
|
|
|
int stream_closed;
|
|
|
|
|
|
|
|
stream_closed =
|
|
|
|
(stream->shut_flags & NGHTTP2_SHUT_RDWR) == NGHTTP2_SHUT_RDWR;
|
|
|
|
|
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
|
|
|
|
|
2014-01-27 15:00:08 +01:00
|
|
|
rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-27 15:00:08 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
/* stream may be NULL if it was closed */
|
|
|
|
if (stream_closed) {
|
|
|
|
stream = NULL;
|
2014-01-27 15:00:08 +01:00
|
|
|
}
|
2012-01-28 16:08:51 +01:00
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
return 0;
|
2012-03-29 16:59:51 +02:00
|
|
|
}
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (session->callbacks.on_frame_send_callback) {
|
|
|
|
rv = session_call_on_frame_send(session, frame);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2014-11-07 16:36:47 +01:00
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
}
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
/* non-DATA frame */
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (frame->hd.type == NGHTTP2_HEADERS ||
|
|
|
|
frame->hd.type == NGHTTP2_PUSH_PROMISE) {
|
|
|
|
if (nghttp2_bufs_next_present(framebufs)) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: CONTINUATION exists, just return\n");
|
2016-09-11 10:18:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
rv = session_call_on_frame_send(session, frame);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
switch (frame->hd.type) {
|
|
|
|
case NGHTTP2_HEADERS: {
|
|
|
|
nghttp2_headers_aux_data *aux_data;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
if (!stream) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
switch (frame->headers.cat) {
|
|
|
|
case NGHTTP2_HCAT_REQUEST: {
|
|
|
|
stream->state = NGHTTP2_STREAM_OPENING;
|
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
|
|
|
|
}
|
|
|
|
rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-11-07 16:36:47 +01:00
|
|
|
return rv;
|
2014-01-09 15:30:45 +01:00
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
/* We assume aux_data is a pointer to nghttp2_headers_aux_data */
|
|
|
|
aux_data = &item->aux_data.headers;
|
|
|
|
if (aux_data->data_prd.read_callback) {
|
|
|
|
/* nghttp2_submit_data() makes a copy of aux_data->data_prd */
|
|
|
|
rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM,
|
|
|
|
frame->hd.stream_id, &aux_data->data_prd);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
/* TODO nghttp2_submit_data() may fail if stream has already
|
|
|
|
DATA frame item. We might have to handle it here. */
|
|
|
|
}
|
|
|
|
return 0;
|
2014-01-09 15:30:45 +01:00
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
case NGHTTP2_HCAT_PUSH_RESPONSE:
|
|
|
|
stream->flags = (uint8_t)(stream->flags & ~NGHTTP2_STREAM_FLAG_PUSH);
|
|
|
|
++session->num_outgoing_streams;
|
|
|
|
/* Fall through */
|
|
|
|
case NGHTTP2_HCAT_RESPONSE:
|
|
|
|
stream->state = NGHTTP2_STREAM_OPENED;
|
|
|
|
/* Fall through */
|
|
|
|
case NGHTTP2_HCAT_HEADERS:
|
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
|
|
|
|
}
|
|
|
|
rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-27 15:00:08 +01:00
|
|
|
return rv;
|
2013-08-29 15:58:05 +02:00
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
/* We assume aux_data is a pointer to nghttp2_headers_aux_data */
|
|
|
|
aux_data = &item->aux_data.headers;
|
|
|
|
if (aux_data->data_prd.read_callback) {
|
|
|
|
rv = nghttp2_submit_data(session, NGHTTP2_FLAG_END_STREAM,
|
|
|
|
frame->hd.stream_id, &aux_data->data_prd);
|
2015-01-21 17:43:56 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
/* TODO nghttp2_submit_data() may fail if stream has already
|
|
|
|
DATA frame item. We might have to handle it here. */
|
2014-11-28 14:59:13 +01:00
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
return 0;
|
|
|
|
default:
|
|
|
|
/* Unreachable */
|
|
|
|
assert(0);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case NGHTTP2_PRIORITY:
|
|
|
|
if (session->server) {
|
|
|
|
return 0;
|
|
|
|
;
|
2014-11-28 14:59:13 +01:00
|
|
|
}
|
2015-09-30 18:19:57 +02:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
|
2015-09-30 18:19:57 +02:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (!stream) {
|
|
|
|
if (!session_detect_idle_stream(session, frame->hd.stream_id)) {
|
|
|
|
return 0;
|
2015-09-30 18:19:57 +02:00
|
|
|
}
|
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
stream = nghttp2_session_open_stream(
|
|
|
|
session, frame->hd.stream_id, NGHTTP2_FLAG_NONE,
|
|
|
|
&frame->priority.pri_spec, NGHTTP2_STREAM_IDLE, NULL);
|
|
|
|
if (!stream) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
rv = nghttp2_session_reprioritize_stream(session, stream,
|
|
|
|
&frame->priority.pri_spec);
|
2015-09-30 18:19:57 +02:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
}
|
2015-09-30 18:19:57 +02:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
rv = nghttp2_session_adjust_idle_stream(session);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2012-02-23 16:02:29 +01:00
|
|
|
}
|
2014-12-18 13:52:17 +01:00
|
|
|
|
2014-01-27 15:00:08 +01:00
|
|
|
return 0;
|
2016-09-11 10:18:08 +02:00
|
|
|
case NGHTTP2_RST_STREAM:
|
|
|
|
rv = nghttp2_session_close_stream(session, frame->hd.stream_id,
|
|
|
|
frame->rst_stream.error_code);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
case NGHTTP2_GOAWAY: {
|
|
|
|
nghttp2_goaway_aux_data *aux_data;
|
2014-01-27 14:13:41 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
aux_data = &item->aux_data.goaway;
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if ((aux_data->flags & NGHTTP2_GOAWAY_AUX_SHUTDOWN_NOTICE) == 0) {
|
2014-02-07 15:22:17 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (aux_data->flags & NGHTTP2_GOAWAY_AUX_TERM_ON_SEND) {
|
|
|
|
session->goaway_flags |= NGHTTP2_GOAWAY_TERM_SENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
session->goaway_flags |= NGHTTP2_GOAWAY_SENT;
|
|
|
|
|
|
|
|
rv = session_close_stream_on_goaway(session, frame->goaway.last_stream_id,
|
|
|
|
1);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-07 15:22:17 +01:00
|
|
|
return rv;
|
2012-01-27 09:22:17 +01:00
|
|
|
}
|
2016-09-11 10:18:08 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
case NGHTTP2_WINDOW_UPDATE:
|
|
|
|
if (frame->hd.stream_id == 0) {
|
|
|
|
session->window_update_queued = 0;
|
|
|
|
if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
|
|
|
|
rv = session_update_connection_consumed_size(session, 0);
|
|
|
|
} else {
|
|
|
|
rv = session_update_recv_connection_window_size(session, 0);
|
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2014-08-25 14:53:40 +02:00
|
|
|
}
|
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-08-09 11:41:08 +02:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
if (!stream) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
stream->window_update_queued = 0;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
/* We don't have to send WINDOW_UPDATE if END_STREAM from peer
|
|
|
|
is seen. */
|
|
|
|
if (stream->shut_flags & NGHTTP2_SHUT_RD) {
|
2014-12-18 13:52:17 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
|
|
|
|
rv = session_update_stream_consumed_size(session, stream, 0);
|
|
|
|
} else {
|
|
|
|
rv = session_update_recv_stream_window_size(session, stream, 0, 1);
|
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2012-01-26 17:17:40 +01:00
|
|
|
}
|
2014-08-25 14:53:40 +02:00
|
|
|
|
2016-09-11 10:18:08 +02:00
|
|
|
return 0;
|
|
|
|
default:
|
2014-12-18 13:52:17 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Called after a frame is sent and session_after_frame_sent1. This
|
|
|
|
* function is responsible to reset session->aob.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
* NGHTTP2_ERR_CALLBACK_FAILURE
|
|
|
|
* The callback function failed.
|
|
|
|
*/
|
|
|
|
static int session_after_frame_sent2(nghttp2_session *session) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_active_outbound_item *aob = &session->aob;
|
|
|
|
nghttp2_outbound_item *item = aob->item;
|
|
|
|
nghttp2_bufs *framebufs = &aob->framebufs;
|
|
|
|
nghttp2_frame *frame;
|
|
|
|
nghttp2_mem *mem;
|
2015-12-10 15:18:02 +01:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
nghttp2_data_aux_data *aux_data;
|
2014-12-18 13:52:17 +01:00
|
|
|
|
|
|
|
mem = &session->mem;
|
|
|
|
frame = &item->frame;
|
|
|
|
|
|
|
|
if (frame->hd.type != NGHTTP2_DATA) {
|
|
|
|
|
|
|
|
if (frame->hd.type == NGHTTP2_HEADERS ||
|
|
|
|
frame->hd.type == NGHTTP2_PUSH_PROMISE) {
|
|
|
|
|
|
|
|
if (nghttp2_bufs_next_present(framebufs)) {
|
|
|
|
framebufs->cur = framebufs->cur->next;
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: next CONTINUATION frame, %zu bytes\n",
|
|
|
|
nghttp2_buf_len(&framebufs->cur->buf));
|
2014-12-18 13:52:17 +01:00
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
active_outbound_item_reset(&session->aob, mem);
|
|
|
|
|
|
|
|
return 0;
|
2015-12-10 15:18:02 +01:00
|
|
|
}
|
2014-12-18 13:52:17 +01:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
/* DATA frame */
|
2014-12-18 13:52:17 +01:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
aux_data = &item->aux_data.data;
|
2014-08-25 16:46:17 +02:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
/* On EOF, we have already detached data. Please note that
|
|
|
|
application may issue nghttp2_submit_data() in
|
|
|
|
on_frame_send_callback (call from session_after_frame_sent1),
|
|
|
|
which attach data to stream. We don't want to detach it. */
|
|
|
|
if (aux_data->eof) {
|
|
|
|
active_outbound_item_reset(aob, mem);
|
2014-08-25 16:46:17 +02:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2015-04-03 16:10:51 +02:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
/* Reset no_copy here because next write may not use this. */
|
|
|
|
aux_data->no_copy = 0;
|
2014-12-18 13:52:17 +01:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
2014-08-09 11:41:08 +02:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
/* If session is closed or RST_STREAM was queued, we won't send
|
|
|
|
further data. */
|
|
|
|
if (nghttp2_session_predicate_data_send(session, stream) != 0) {
|
|
|
|
if (stream) {
|
|
|
|
rv = nghttp2_stream_detach_item(stream);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-01-27 15:00:08 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-12-10 15:18:02 +01:00
|
|
|
active_outbound_item_reset(aob, mem);
|
2015-08-16 12:01:10 +02:00
|
|
|
|
2014-01-27 15:00:08 +01:00
|
|
|
return 0;
|
2012-01-26 17:17:40 +01:00
|
|
|
}
|
2015-12-10 15:18:02 +01:00
|
|
|
|
|
|
|
aob->item = NULL;
|
|
|
|
active_outbound_item_reset(&session->aob, mem);
|
|
|
|
|
2014-07-06 16:32:08 +02:00
|
|
|
return 0;
|
2012-01-25 17:04:01 +01:00
|
|
|
}
|
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
static int session_call_send_data(nghttp2_session *session,
|
|
|
|
nghttp2_outbound_item *item,
|
|
|
|
nghttp2_bufs *framebufs) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
size_t length;
|
|
|
|
nghttp2_frame *frame;
|
|
|
|
nghttp2_data_aux_data *aux_data;
|
|
|
|
|
|
|
|
buf = &framebufs->cur->buf;
|
|
|
|
frame = &item->frame;
|
|
|
|
length = frame->hd.length - frame->data.padlen;
|
|
|
|
aux_data = &item->aux_data.data;
|
|
|
|
|
|
|
|
rv = session->callbacks.send_data_callback(session, frame, buf->pos, length,
|
|
|
|
&aux_data->data_prd.source,
|
|
|
|
session->user_data);
|
|
|
|
|
2015-10-03 10:00:16 +02:00
|
|
|
switch (rv) {
|
|
|
|
case 0:
|
|
|
|
case NGHTTP2_ERR_WOULDBLOCK:
|
|
|
|
case NGHTTP2_ERR_PAUSE:
|
|
|
|
case NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE:
|
2015-04-03 16:10:51 +02:00
|
|
|
return rv;
|
2015-10-03 10:00:16 +02:00
|
|
|
default:
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2015-04-03 16:10:51 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 13:52:17 +01:00
|
|
|
static ssize_t nghttp2_session_mem_send_internal(nghttp2_session *session,
|
|
|
|
const uint8_t **data_ptr,
|
|
|
|
int fast_cb) {
|
2014-02-18 15:23:11 +01:00
|
|
|
int rv;
|
2014-03-10 17:47:38 +01:00
|
|
|
nghttp2_active_outbound_item *aob;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs *framebufs;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
2014-03-10 17:47:38 +01:00
|
|
|
aob = &session->aob;
|
2014-03-13 14:11:02 +01:00
|
|
|
framebufs = &aob->framebufs;
|
2014-02-18 15:23:11 +01:00
|
|
|
|
2015-12-23 08:38:30 +01:00
|
|
|
/* We may have idle streams more than we expect (e.g.,
|
|
|
|
nghttp2_session_change_stream_priority() or
|
|
|
|
nghttp2_session_create_idle_stream()). Adjust them here. */
|
|
|
|
rv = nghttp2_session_adjust_idle_stream(session);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
|
|
|
switch (aob->state) {
|
2014-02-18 15:23:11 +01:00
|
|
|
case NGHTTP2_OB_POP_ITEM: {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_outbound_item *item;
|
2014-02-18 15:23:11 +01:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
item = nghttp2_session_pop_next_ob_item(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item == NULL) {
|
2014-02-18 15:23:11 +01:00
|
|
|
return 0;
|
2012-02-05 16:14:19 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
rv = session_prep_frame(session, item);
|
2016-08-28 11:50:19 +02:00
|
|
|
if (rv == NGHTTP2_ERR_PAUSE) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_DEFERRED) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: frame transmission deferred\n");
|
2014-02-18 15:23:11 +01:00
|
|
|
break;
|
2014-01-26 14:01:27 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2015-02-13 15:41:48 +01:00
|
|
|
int32_t opened_stream_id = 0;
|
|
|
|
uint32_t error_code = NGHTTP2_INTERNAL_ERROR;
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: frame preparation failed with %s\n",
|
|
|
|
nghttp2_strerror(rv));
|
2013-07-19 17:21:07 +02:00
|
|
|
/* TODO If the error comes from compressor, the connection
|
|
|
|
must be closed. */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item->frame.hd.type != NGHTTP2_DATA &&
|
|
|
|
session->callbacks.on_frame_not_send_callback && is_non_fatal(rv)) {
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_frame *frame = &item->frame;
|
2012-03-29 16:59:51 +02:00
|
|
|
/* The library is responsible for the transmission of
|
|
|
|
WINDOW_UPDATE frame, so we don't call error callback for
|
|
|
|
it. */
|
2015-02-12 15:02:17 +01:00
|
|
|
if (frame->hd.type != NGHTTP2_WINDOW_UPDATE &&
|
|
|
|
session->callbacks.on_frame_not_send_callback(
|
|
|
|
session, frame, rv, session->user_data) != 0) {
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-02-12 15:02:17 +01:00
|
|
|
nghttp2_outbound_item_free(item, mem);
|
|
|
|
nghttp2_mem_free(mem, item);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-02-12 15:02:17 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-03-29 16:59:51 +02:00
|
|
|
}
|
2015-02-13 15:41:48 +01:00
|
|
|
}
|
|
|
|
/* We have to close stream opened by failed request HEADERS
|
|
|
|
or PUSH_PROMISE. */
|
|
|
|
switch (item->frame.hd.type) {
|
|
|
|
case NGHTTP2_HEADERS:
|
|
|
|
if (item->frame.headers.cat == NGHTTP2_HCAT_REQUEST) {
|
|
|
|
opened_stream_id = item->frame.hd.stream_id;
|
|
|
|
if (item->aux_data.headers.canceled) {
|
|
|
|
error_code = item->aux_data.headers.error_code;
|
2015-10-29 14:49:27 +01:00
|
|
|
} else {
|
|
|
|
/* Set error_code to REFUSED_STREAM so that application
|
|
|
|
can send request again. */
|
|
|
|
error_code = NGHTTP2_REFUSED_STREAM;
|
2015-02-12 14:41:57 +01:00
|
|
|
}
|
|
|
|
}
|
2015-02-13 15:41:48 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
|
|
|
opened_stream_id = item->frame.push_promise.promised_stream_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (opened_stream_id) {
|
|
|
|
/* careful not to override rv */
|
|
|
|
int rv2;
|
|
|
|
rv2 = nghttp2_session_close_stream(session, opened_stream_id,
|
|
|
|
error_code);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv2)) {
|
|
|
|
return rv2;
|
2015-02-12 14:41:57 +01:00
|
|
|
}
|
2012-03-07 16:18:18 +01:00
|
|
|
}
|
2015-02-13 15:41:48 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_outbound_item_free(item, mem);
|
|
|
|
nghttp2_mem_free(mem, item);
|
|
|
|
active_outbound_item_reset(aob, mem);
|
2013-07-15 14:45:59 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_HEADER_COMP) {
|
2013-08-19 17:12:29 +02:00
|
|
|
/* If header compression error occurred, should terminiate
|
|
|
|
connection. */
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_terminate_session(session,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
2013-07-23 15:47:15 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-03-10 18:15:05 +01:00
|
|
|
return rv;
|
2012-01-28 11:22:38 +01:00
|
|
|
}
|
2014-02-18 15:23:11 +01:00
|
|
|
break;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-03-10 17:47:38 +01:00
|
|
|
aob->item = item;
|
2014-01-26 14:01:27 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs_rewind(framebufs);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item->frame.hd.type != NGHTTP2_DATA) {
|
2014-03-10 17:47:38 +01:00
|
|
|
nghttp2_frame *frame;
|
|
|
|
|
2014-10-02 17:59:44 +02:00
|
|
|
frame = &item->frame;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("send: next frame: payloadlen=%zu, type=%u, flags=0x%02x, "
|
|
|
|
"stream_id=%d\n",
|
2016-10-12 18:11:59 +02:00
|
|
|
frame->hd.length, frame->hd.type, frame->hd.flags,
|
|
|
|
frame->hd.stream_id);
|
2014-03-19 16:27:39 +01:00
|
|
|
|
2014-02-18 15:23:11 +01:00
|
|
|
rv = session_call_before_frame_send(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-18 15:23:11 +01:00
|
|
|
return rv;
|
2013-08-29 14:45:10 +02:00
|
|
|
}
|
2016-07-02 12:21:08 +02:00
|
|
|
|
|
|
|
if (rv == NGHTTP2_ERR_CANCEL) {
|
|
|
|
int32_t opened_stream_id = 0;
|
|
|
|
uint32_t error_code = NGHTTP2_INTERNAL_ERROR;
|
|
|
|
|
|
|
|
if (session->callbacks.on_frame_not_send_callback) {
|
|
|
|
if (session->callbacks.on_frame_not_send_callback(
|
|
|
|
session, frame, rv, session->user_data) != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have to close stream opened by canceled request
|
|
|
|
HEADERS or PUSH_PROMISE. */
|
|
|
|
switch (item->frame.hd.type) {
|
|
|
|
case NGHTTP2_HEADERS:
|
|
|
|
if (item->frame.headers.cat == NGHTTP2_HCAT_REQUEST) {
|
|
|
|
opened_stream_id = item->frame.hd.stream_id;
|
|
|
|
/* We don't have to check
|
|
|
|
item->aux_data.headers.canceled since it has already
|
|
|
|
been checked. */
|
|
|
|
/* Set error_code to REFUSED_STREAM so that application
|
|
|
|
can send request again. */
|
|
|
|
error_code = NGHTTP2_REFUSED_STREAM;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
|
|
|
opened_stream_id = item->frame.push_promise.promised_stream_id;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
if (opened_stream_id) {
|
|
|
|
/* careful not to override rv */
|
|
|
|
int rv2;
|
|
|
|
rv2 = nghttp2_session_close_stream(session, opened_stream_id,
|
|
|
|
error_code);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv2)) {
|
|
|
|
return rv2;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
active_outbound_item_reset(aob, mem);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-03-19 16:27:39 +01:00
|
|
|
} else {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: next frame: DATA\n");
|
2015-04-03 16:10:51 +02:00
|
|
|
|
|
|
|
if (item->aux_data.data.no_copy) {
|
|
|
|
aob->state = NGHTTP2_OB_SEND_NO_COPY;
|
|
|
|
break;
|
|
|
|
}
|
2012-01-28 09:25:14 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: start transmitting frame type=%u, length=%zd\n",
|
|
|
|
framebufs->cur->buf.pos[3],
|
|
|
|
framebufs->cur->buf.last - framebufs->cur->buf.pos);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
|
|
|
aob->state = NGHTTP2_OB_SEND_DATA;
|
|
|
|
|
2014-02-18 15:23:11 +01:00
|
|
|
break;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-02-18 15:23:11 +01:00
|
|
|
case NGHTTP2_OB_SEND_DATA: {
|
|
|
|
size_t datalen;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
|
|
|
|
|
|
|
buf = &framebufs->cur->buf;
|
2014-02-18 15:23:11 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (buf->pos == buf->last) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: end transmission of a frame\n");
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-02-18 15:23:11 +01:00
|
|
|
/* Frame has completely sent */
|
2014-12-18 13:52:17 +01:00
|
|
|
if (fast_cb) {
|
|
|
|
rv = session_after_frame_sent2(session);
|
|
|
|
} else {
|
|
|
|
rv = session_after_frame_sent1(session);
|
2015-01-02 06:49:36 +01:00
|
|
|
if (rv < 0) {
|
|
|
|
/* FATAL */
|
|
|
|
assert(nghttp2_is_fatal(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
2014-12-18 13:52:17 +01:00
|
|
|
rv = session_after_frame_sent2(session);
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
2014-02-18 15:23:11 +01:00
|
|
|
/* FATAL */
|
|
|
|
assert(nghttp2_is_fatal(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
/* We have already adjusted the next state */
|
|
|
|
break;
|
|
|
|
}
|
2014-02-07 15:22:17 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
*data_ptr = buf->pos;
|
|
|
|
datalen = nghttp2_buf_len(buf);
|
|
|
|
|
2014-02-18 15:23:11 +01:00
|
|
|
/* We increment the offset here. If send_callback does not send
|
|
|
|
everything, we will adjust it. */
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->pos += datalen;
|
2014-02-18 15:23:11 +01:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)datalen;
|
2014-02-18 15:23:11 +01:00
|
|
|
}
|
2015-04-05 05:40:21 +02:00
|
|
|
case NGHTTP2_OB_SEND_NO_COPY: {
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
nghttp2_frame *frame;
|
2015-10-03 10:00:16 +02:00
|
|
|
int pause;
|
2015-04-05 05:40:21 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: no copy DATA\n");
|
2015-04-03 16:10:51 +02:00
|
|
|
|
2015-04-05 05:40:21 +02:00
|
|
|
frame = &aob->item->frame;
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
if (stream == NULL) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: no copy DATA cancelled because stream was closed\n");
|
2015-04-05 05:40:21 +02:00
|
|
|
|
|
|
|
active_outbound_item_reset(aob, mem);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
rv = session_call_send_data(session, aob->item, framebufs);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_detach_item(stream);
|
2015-04-03 16:10:51 +02:00
|
|
|
|
2015-04-05 05:40:21 +02:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2015-04-03 16:10:51 +02:00
|
|
|
|
2015-04-05 05:40:21 +02:00
|
|
|
rv = nghttp2_session_add_rst_stream(session, frame->hd.stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2015-04-03 16:10:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
active_outbound_item_reset(aob, mem);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == NGHTTP2_ERR_WOULDBLOCK) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-03 10:00:16 +02:00
|
|
|
pause = (rv == NGHTTP2_ERR_PAUSE);
|
2015-04-03 16:10:51 +02:00
|
|
|
|
|
|
|
rv = session_after_frame_sent1(session);
|
|
|
|
if (rv < 0) {
|
|
|
|
assert(nghttp2_is_fatal(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
rv = session_after_frame_sent2(session);
|
|
|
|
if (rv < 0) {
|
|
|
|
assert(nghttp2_is_fatal(rv));
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We have already adjusted the next state */
|
|
|
|
|
2015-10-03 10:00:16 +02:00
|
|
|
if (pause) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
break;
|
2014-02-18 15:23:11 +01:00
|
|
|
}
|
2015-04-05 15:35:40 +02:00
|
|
|
case NGHTTP2_OB_SEND_CLIENT_MAGIC: {
|
|
|
|
size_t datalen;
|
|
|
|
nghttp2_buf *buf;
|
|
|
|
|
|
|
|
buf = &framebufs->cur->buf;
|
|
|
|
|
|
|
|
if (buf->pos == buf->last) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: end transmission of client magic\n");
|
2015-04-05 15:35:40 +02:00
|
|
|
active_outbound_item_reset(aob, mem);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
*data_ptr = buf->pos;
|
|
|
|
datalen = nghttp2_buf_len(buf);
|
|
|
|
|
|
|
|
buf->pos += datalen;
|
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)datalen;
|
2015-04-05 15:35:40 +02:00
|
|
|
}
|
2015-04-05 05:40:21 +02:00
|
|
|
}
|
2014-02-18 15:23:11 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-12-18 13:52:17 +01:00
|
|
|
ssize_t nghttp2_session_mem_send(nghttp2_session *session,
|
|
|
|
const uint8_t **data_ptr) {
|
|
|
|
int rv;
|
|
|
|
ssize_t len;
|
|
|
|
|
2016-04-22 17:32:23 +02:00
|
|
|
*data_ptr = NULL;
|
|
|
|
|
2014-12-18 13:52:17 +01:00
|
|
|
len = nghttp2_session_mem_send_internal(session, data_ptr, 1);
|
|
|
|
if (len <= 0) {
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2015-04-05 15:35:40 +02:00
|
|
|
if (session->aob.item) {
|
|
|
|
/* We have to call session_after_frame_sent1 here to handle stream
|
|
|
|
closure upon transmission of frames. Otherwise, END_STREAM may
|
|
|
|
be reached to client before we call nghttp2_session_mem_send
|
|
|
|
again and we may get exceeding number of incoming streams. */
|
|
|
|
rv = session_after_frame_sent1(session);
|
|
|
|
if (rv < 0) {
|
|
|
|
assert(nghttp2_is_fatal(rv));
|
|
|
|
return (ssize_t)rv;
|
|
|
|
}
|
2014-12-18 13:52:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return len;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_send(nghttp2_session *session) {
|
2016-04-22 00:53:19 +02:00
|
|
|
const uint8_t *data = NULL;
|
2014-02-18 15:23:11 +01:00
|
|
|
ssize_t datalen;
|
|
|
|
ssize_t sentlen;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_bufs *framebufs;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
framebufs = &session->aob.framebufs;
|
2014-02-18 15:23:11 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2014-12-18 13:52:17 +01:00
|
|
|
datalen = nghttp2_session_mem_send_internal(session, &data, 0);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (datalen <= 0) {
|
2014-06-11 16:37:16 +02:00
|
|
|
return (int)datalen;
|
2014-02-18 15:23:11 +01:00
|
|
|
}
|
2015-09-23 07:41:53 +02:00
|
|
|
sentlen = session->callbacks.send_callback(session, data, (size_t)datalen,
|
|
|
|
0, session->user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (sentlen < 0) {
|
|
|
|
if (sentlen == NGHTTP2_ERR_WOULDBLOCK) {
|
2014-02-18 15:23:11 +01:00
|
|
|
/* Transmission canceled. Rewind the offset */
|
2014-03-13 14:11:02 +01:00
|
|
|
framebufs->cur->buf.pos -= datalen;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2012-01-24 14:02:24 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-02-18 15:23:11 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-02-18 15:23:11 +01:00
|
|
|
/* Rewind the offset to the amount of unsent bytes */
|
2014-03-13 14:11:02 +01:00
|
|
|
framebufs->cur->buf.pos -= datalen - sentlen;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static ssize_t session_recv(nghttp2_session *session, uint8_t *buf,
|
|
|
|
size_t len) {
|
2014-02-18 16:45:20 +01:00
|
|
|
ssize_t rv;
|
|
|
|
rv = session->callbacks.recv_callback(session, buf, len, 0,
|
|
|
|
session->user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv > 0) {
|
|
|
|
if ((size_t)rv > len) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (rv < 0 && rv != NGHTTP2_ERR_WOULDBLOCK && rv != NGHTTP2_ERR_EOF) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_call_on_begin_frame(nghttp2_session *session,
|
|
|
|
const nghttp2_frame_hd *hd) {
|
2014-08-23 14:08:35 +02:00
|
|
|
int rv;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.on_begin_frame_callback) {
|
2014-08-23 14:08:35 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = session->callbacks.on_begin_frame_callback(session, hd,
|
|
|
|
session->user_data);
|
2014-08-23 14:08:35 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-08-23 14:08:35 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_call_on_frame_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame) {
|
2013-08-29 14:03:39 +02:00
|
|
|
int rv;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.on_frame_recv_callback) {
|
2013-08-29 14:03:39 +02:00
|
|
|
rv = session->callbacks.on_frame_recv_callback(session, frame,
|
|
|
|
session->user_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-29 13:23:13 +01:00
|
|
|
static int session_call_on_begin_headers(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2014-01-29 13:23:13 +01:00
|
|
|
int rv;
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: call on_begin_headers callback stream_id=%d\n",
|
|
|
|
frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.on_begin_headers_callback) {
|
2014-01-29 13:23:13 +01:00
|
|
|
rv = session->callbacks.on_begin_headers_callback(session, frame,
|
|
|
|
session->user_data);
|
2015-04-23 16:43:30 +02:00
|
|
|
if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-01-29 13:23:13 +01:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
static int session_call_on_header(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame,
|
2016-03-12 05:23:12 +01:00
|
|
|
const nghttp2_hd_nv *nv) {
|
2016-03-12 07:05:20 +01:00
|
|
|
int rv = 0;
|
|
|
|
if (session->callbacks.on_header_callback2) {
|
|
|
|
rv = session->callbacks.on_header_callback2(
|
|
|
|
session, frame, nv->name, nv->value, nv->flags, session->user_data);
|
|
|
|
} else if (session->callbacks.on_header_callback) {
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = session->callbacks.on_header_callback(
|
2016-03-12 05:23:12 +01:00
|
|
|
session, frame, nv->name->base, nv->name->len, nv->value->base,
|
|
|
|
nv->value->len, nv->flags, session->user_data);
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
2016-03-12 07:05:20 +01:00
|
|
|
|
|
|
|
if (rv == NGHTTP2_ERR_PAUSE || rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (rv != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
2013-08-29 14:03:39 +02:00
|
|
|
return 0;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
|
|
|
|
2016-08-03 16:49:49 +02:00
|
|
|
static int session_call_on_invalid_header(nghttp2_session *session,
|
|
|
|
const nghttp2_frame *frame,
|
|
|
|
const nghttp2_hd_nv *nv) {
|
|
|
|
int rv;
|
|
|
|
if (session->callbacks.on_invalid_header_callback2) {
|
|
|
|
rv = session->callbacks.on_invalid_header_callback2(
|
|
|
|
session, frame, nv->name, nv->value, nv->flags, session->user_data);
|
|
|
|
} else if (session->callbacks.on_invalid_header_callback) {
|
|
|
|
rv = session->callbacks.on_invalid_header_callback(
|
|
|
|
session, frame, nv->name->base, nv->name->len, nv->value->base,
|
|
|
|
nv->value->len, nv->flags, session->user_data);
|
|
|
|
} else {
|
2017-04-28 16:33:58 +02:00
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
2016-08-03 16:49:49 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (rv == NGHTTP2_ERR_PAUSE || rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (rv != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-10-10 15:27:48 +02:00
|
|
|
static int
|
|
|
|
session_call_on_extension_chunk_recv_callback(nghttp2_session *session,
|
|
|
|
const uint8_t *data, size_t len) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
|
|
|
|
if (session->callbacks.on_extension_chunk_recv_callback) {
|
|
|
|
rv = session->callbacks.on_extension_chunk_recv_callback(
|
|
|
|
session, &frame->hd, data, len, session->user_data);
|
|
|
|
if (rv == NGHTTP2_ERR_CANCEL) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (rv != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static int session_call_unpack_extension_callback(nghttp2_session *session) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
void *payload = NULL;
|
|
|
|
|
|
|
|
rv = session->callbacks.unpack_extension_callback(
|
|
|
|
session, &payload, &frame->hd, session->user_data);
|
|
|
|
if (rv == NGHTTP2_ERR_CANCEL) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
if (rv != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
frame->ext.payload = payload;
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
/*
|
|
|
|
* Handles frame size error.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
*/
|
2017-03-11 09:46:39 +01:00
|
|
|
static int session_handle_frame_size_error(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
/* TODO Currently no callback is called for this error, because we
|
|
|
|
call this callback before reading any payload */
|
|
|
|
return nghttp2_session_terminate_session(session, NGHTTP2_FRAME_SIZE_ERROR);
|
2013-07-21 11:23:50 +02:00
|
|
|
}
|
2012-02-15 14:11:42 +01:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
static uint32_t get_error_code_from_lib_error_code(int lib_error_code) {
|
2015-03-23 13:18:55 +01:00
|
|
|
switch (lib_error_code) {
|
|
|
|
case NGHTTP2_ERR_STREAM_CLOSED:
|
|
|
|
return NGHTTP2_STREAM_CLOSED;
|
|
|
|
case NGHTTP2_ERR_HEADER_COMP:
|
|
|
|
return NGHTTP2_COMPRESSION_ERROR;
|
|
|
|
case NGHTTP2_ERR_FRAME_SIZE_ERROR:
|
|
|
|
return NGHTTP2_FRAME_SIZE_ERROR;
|
|
|
|
case NGHTTP2_ERR_FLOW_CONTROL:
|
|
|
|
return NGHTTP2_FLOW_CONTROL_ERROR;
|
|
|
|
case NGHTTP2_ERR_REFUSED_STREAM:
|
|
|
|
return NGHTTP2_REFUSED_STREAM;
|
|
|
|
case NGHTTP2_ERR_PROTO:
|
2015-04-05 15:46:05 +02:00
|
|
|
case NGHTTP2_ERR_HTTP_HEADER:
|
|
|
|
case NGHTTP2_ERR_HTTP_MESSAGING:
|
2015-03-23 13:18:55 +01:00
|
|
|
return NGHTTP2_PROTOCOL_ERROR;
|
|
|
|
default:
|
|
|
|
return NGHTTP2_INTERNAL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-04-26 17:03:20 +02:00
|
|
|
/*
|
|
|
|
* Calls on_invalid_frame_recv_callback if it is set to |session|.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_CALLBACK_FAILURE
|
|
|
|
* User defined callback function fails.
|
|
|
|
*/
|
|
|
|
static int session_call_on_invalid_frame_recv_callback(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
|
|
|
int lib_error_code) {
|
|
|
|
if (session->callbacks.on_invalid_frame_recv_callback) {
|
|
|
|
if (session->callbacks.on_invalid_frame_recv_callback(
|
|
|
|
session, frame, lib_error_code, session->user_data) != 0) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-26 14:59:07 +01:00
|
|
|
static int session_handle_invalid_stream2(nghttp2_session *session,
|
|
|
|
int32_t stream_id,
|
|
|
|
nghttp2_frame *frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
int lib_error_code) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2015-03-23 13:18:55 +01:00
|
|
|
rv = nghttp2_session_add_rst_stream(
|
|
|
|
session, stream_id, get_error_code_from_lib_error_code(lib_error_code));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.on_invalid_frame_recv_callback) {
|
|
|
|
if (session->callbacks.on_invalid_frame_recv_callback(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, lib_error_code, session->user_data) != 0) {
|
2013-08-29 14:12:43 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
2012-01-25 15:46:07 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-26 14:59:07 +01:00
|
|
|
static int session_handle_invalid_stream(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
int lib_error_code) {
|
2015-02-26 14:59:07 +01:00
|
|
|
return session_handle_invalid_stream2(session, frame->hd.stream_id, frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
lib_error_code);
|
2015-02-26 14:59:07 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_inflate_handle_invalid_stream(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
int lib_error_code) {
|
2014-01-16 15:41:13 +01:00
|
|
|
int rv;
|
2015-03-23 13:18:55 +01:00
|
|
|
rv = session_handle_invalid_stream(session, frame, lib_error_code);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
|
2013-07-21 11:20:23 +02:00
|
|
|
/*
|
|
|
|
* Handles invalid frame which causes connection error.
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_handle_invalid_connection(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
int lib_error_code,
|
2014-11-27 15:39:04 +01:00
|
|
|
const char *reason) {
|
|
|
|
if (session->callbacks.on_invalid_frame_recv_callback) {
|
|
|
|
if (session->callbacks.on_invalid_frame_recv_callback(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, lib_error_code, session->user_data) != 0) {
|
2013-08-29 14:12:43 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2013-07-21 11:20:23 +02:00
|
|
|
}
|
2015-03-23 13:18:55 +01:00
|
|
|
return nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, get_error_code_from_lib_error_code(lib_error_code), reason);
|
2013-07-21 11:20:23 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_inflate_handle_invalid_connection(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
int lib_error_code,
|
2014-11-27 15:39:04 +01:00
|
|
|
const char *reason) {
|
2014-01-16 15:41:13 +01:00
|
|
|
int rv;
|
2015-03-23 13:18:55 +01:00
|
|
|
rv =
|
|
|
|
session_handle_invalid_connection(session, frame, lib_error_code, reason);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
|
2014-03-10 17:47:38 +01:00
|
|
|
/*
|
|
|
|
* Inflates header block in the memory pointed by |in| with |inlen|
|
|
|
|
* bytes. If this function returns NGHTTP2_ERR_PAUSE, the caller must
|
|
|
|
* call this function again, until it returns 0 or one of negative
|
|
|
|
* error code. If |call_header_cb| is zero, the on_header_callback
|
|
|
|
* are not invoked and the function never return NGHTTP2_ERR_PAUSE. If
|
|
|
|
* the given |in| is the last chunk of header block, the |final| must
|
|
|
|
* be nonzero. If header block is successfully processed (which is
|
|
|
|
* indicated by the return value 0, NGHTTP2_ERR_PAUSE or
|
|
|
|
* NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE), the number of processed
|
|
|
|
* input bytes is assigned to the |*readlen_ptr|.
|
|
|
|
*
|
|
|
|
* This function return 0 if it succeeds, or one of the negative error
|
|
|
|
* codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_CALLBACK_FAILURE
|
|
|
|
* The callback function failed.
|
|
|
|
* NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE
|
|
|
|
* The callback returns this error code, indicating that this
|
|
|
|
* stream should be RST_STREAMed.
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
* NGHTTP2_ERR_PAUSE
|
|
|
|
* The callback function returned NGHTTP2_ERR_PAUSE
|
|
|
|
* NGHTTP2_ERR_HEADER_COMP
|
|
|
|
* Header decompression failed
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int inflate_header_block(nghttp2_session *session, nghttp2_frame *frame,
|
|
|
|
size_t *readlen_ptr, uint8_t *in, size_t inlen,
|
|
|
|
int final, int call_header_cb) {
|
2014-06-11 16:37:16 +02:00
|
|
|
ssize_t proclen;
|
|
|
|
int rv;
|
2014-03-10 17:47:38 +01:00
|
|
|
int inflate_flags;
|
2016-03-12 05:23:12 +01:00
|
|
|
nghttp2_hd_nv nv;
|
2014-03-13 15:02:33 +01:00
|
|
|
nghttp2_stream *stream;
|
2015-02-19 16:01:15 +01:00
|
|
|
nghttp2_stream *subject_stream;
|
|
|
|
int trailer = 0;
|
2014-03-13 15:02:33 +01:00
|
|
|
|
2014-03-10 17:47:38 +01:00
|
|
|
*readlen_ptr = 0;
|
2015-02-19 16:01:15 +01:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
|
|
|
|
if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
|
|
|
|
subject_stream = nghttp2_session_get_stream(
|
|
|
|
session, frame->push_promise.promised_stream_id);
|
|
|
|
} else {
|
|
|
|
subject_stream = stream;
|
|
|
|
trailer = session_trailer_headers(session, stream, frame);
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: decoding header block %zu bytes\n", inlen);
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
2014-03-10 17:47:38 +01:00
|
|
|
inflate_flags = 0;
|
2016-05-14 11:25:20 +02:00
|
|
|
proclen = nghttp2_hd_inflate_hd_nv(&session->hd_inflater, &nv,
|
|
|
|
&inflate_flags, in, inlen, final);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal((int)proclen)) {
|
2014-06-11 16:37:16 +02:00
|
|
|
return (int)proclen;
|
2014-03-10 17:47:38 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (proclen < 0) {
|
|
|
|
if (session->iframe.state == NGHTTP2_IB_READ_HEADER_BLOCK) {
|
2015-04-28 14:38:52 +02:00
|
|
|
if (subject_stream && subject_stream->state != NGHTTP2_STREAM_CLOSING) {
|
2014-03-13 15:02:33 +01:00
|
|
|
/* Adding RST_STREAM here is very important. It prevents
|
|
|
|
from invoking subsequent callbacks for the same stream
|
|
|
|
ID. */
|
2015-04-28 14:38:52 +02:00
|
|
|
rv = nghttp2_session_add_rst_stream(
|
|
|
|
session, subject_stream->stream_id, NGHTTP2_COMPRESSION_ERROR);
|
2014-03-13 15:02:33 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-03-13 15:02:33 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
rv =
|
|
|
|
nghttp2_session_terminate_session(session, NGHTTP2_COMPRESSION_ERROR);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-03-10 17:47:38 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-13 15:02:33 +01:00
|
|
|
|
2014-03-10 17:47:38 +01:00
|
|
|
return NGHTTP2_ERR_HEADER_COMP;
|
|
|
|
}
|
2014-06-11 16:37:16 +02:00
|
|
|
in += proclen;
|
2015-09-23 07:41:53 +02:00
|
|
|
inlen -= (size_t)proclen;
|
|
|
|
*readlen_ptr += (size_t)proclen;
|
2014-08-23 17:50:55 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: proclen=%zd\n", proclen);
|
2014-08-23 17:50:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (call_header_cb && (inflate_flags & NGHTTP2_HD_INFLATE_EMIT)) {
|
2015-02-22 15:13:27 +01:00
|
|
|
rv = 0;
|
2019-04-11 03:14:52 +02:00
|
|
|
if (subject_stream) {
|
|
|
|
if (session_enforce_http_messaging(session)) {
|
|
|
|
rv = nghttp2_http_on_header(session, subject_stream, frame, &nv,
|
|
|
|
trailer);
|
2017-04-28 16:33:58 +02:00
|
|
|
|
2019-04-11 03:14:52 +02:00
|
|
|
if (rv == NGHTTP2_ERR_IGN_HTTP_HEADER) {
|
|
|
|
/* Don't overwrite rv here */
|
|
|
|
int rv2;
|
2017-04-28 16:33:58 +02:00
|
|
|
|
2019-04-11 03:14:52 +02:00
|
|
|
rv2 = session_call_on_invalid_header(session, frame, &nv);
|
|
|
|
if (rv2 == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
|
|
|
rv = NGHTTP2_ERR_HTTP_HEADER;
|
|
|
|
} else {
|
|
|
|
if (rv2 != 0) {
|
|
|
|
return rv2;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* header is ignored */
|
|
|
|
DEBUGF("recv: HTTP ignored: type=%u, id=%d, header %.*s: %.*s\n",
|
|
|
|
frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
|
|
|
|
nv.name->base, (int)nv.value->len, nv.value->base);
|
|
|
|
|
|
|
|
rv2 = session_call_error_callback(
|
|
|
|
session, NGHTTP2_ERR_HTTP_HEADER,
|
|
|
|
"Ignoring received invalid HTTP header field: frame type: "
|
|
|
|
"%u, stream: %d, name: [%.*s], value: [%.*s]",
|
|
|
|
frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
|
|
|
|
nv.name->base, (int)nv.value->len, nv.value->base);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv2)) {
|
|
|
|
return rv2;
|
|
|
|
}
|
2017-04-28 16:33:58 +02:00
|
|
|
}
|
2019-04-11 03:14:52 +02:00
|
|
|
}
|
2017-04-28 16:33:58 +02:00
|
|
|
|
2019-04-11 03:14:52 +02:00
|
|
|
if (rv == NGHTTP2_ERR_HTTP_HEADER) {
|
|
|
|
DEBUGF("recv: HTTP error: type=%u, id=%d, header %.*s: %.*s\n",
|
2017-04-28 16:33:58 +02:00
|
|
|
frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
|
|
|
|
nv.name->base, (int)nv.value->len, nv.value->base);
|
|
|
|
|
2019-04-11 03:14:52 +02:00
|
|
|
rv = session_call_error_callback(
|
2017-11-19 08:41:10 +01:00
|
|
|
session, NGHTTP2_ERR_HTTP_HEADER,
|
2019-04-11 03:14:52 +02:00
|
|
|
"Invalid HTTP header field was received: frame type: "
|
2017-04-28 16:33:58 +02:00
|
|
|
"%u, stream: %d, name: [%.*s], value: [%.*s]",
|
|
|
|
frame->hd.type, frame->hd.stream_id, (int)nv.name->len,
|
|
|
|
nv.name->base, (int)nv.value->len, nv.value->base);
|
|
|
|
|
2019-04-11 03:14:52 +02:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2017-04-28 16:33:58 +02:00
|
|
|
}
|
2016-03-13 10:21:07 +01:00
|
|
|
|
2019-04-11 03:14:52 +02:00
|
|
|
rv = session_handle_invalid_stream2(session,
|
|
|
|
subject_stream->stream_id,
|
|
|
|
frame, NGHTTP2_ERR_HTTP_HEADER);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE;
|
2016-03-13 10:21:07 +01:00
|
|
|
}
|
2019-04-11 03:14:52 +02:00
|
|
|
}
|
|
|
|
if (rv == 0) {
|
|
|
|
rv = session_call_on_header(session, frame, &nv);
|
|
|
|
/* This handles NGHTTP2_ERR_PAUSE and
|
|
|
|
NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE as well */
|
|
|
|
if (rv != 0) {
|
2015-02-19 16:01:15 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (inflate_flags & NGHTTP2_HD_INFLATE_FINAL) {
|
2014-03-10 17:47:38 +01:00
|
|
|
nghttp2_hd_inflate_end_headers(&session->hd_inflater);
|
|
|
|
break;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((inflate_flags & NGHTTP2_HD_INFLATE_EMIT) == 0 && inlen == 0) {
|
2014-03-10 17:47:38 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-01-17 14:52:30 +01:00
|
|
|
/*
|
2015-11-27 14:50:13 +01:00
|
|
|
* Call this function when HEADERS frame was completely received.
|
2014-01-17 14:52:30 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of negative error
|
|
|
|
* codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_CALLBACK_FAILURE
|
|
|
|
* The callback function failed.
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
*/
|
2015-11-27 14:50:13 +01:00
|
|
|
static int session_end_stream_headers_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
|
|
|
nghttp2_stream *stream) {
|
2014-01-16 15:41:13 +01:00
|
|
|
int rv;
|
2015-11-27 14:50:13 +01:00
|
|
|
if ((frame->hd.flags & NGHTTP2_FLAG_END_STREAM) == 0) {
|
|
|
|
return 0;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
|
|
|
|
2015-11-27 14:50:13 +01:00
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
|
|
|
|
rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2014-01-16 15:41:13 +01:00
|
|
|
}
|
2015-11-27 14:50:13 +01:00
|
|
|
|
2014-01-16 15:41:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_after_header_block_received(nghttp2_session *session) {
|
2015-02-19 16:01:15 +01:00
|
|
|
int rv = 0;
|
|
|
|
int call_cb = 1;
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_frame *frame = &session->iframe.frame;
|
2014-01-29 13:23:13 +01:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
2014-03-13 14:49:37 +01:00
|
|
|
/* We don't call on_frame_recv_callback if stream has been closed
|
|
|
|
already or being closed. */
|
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream || stream->state == NGHTTP2_STREAM_CLOSING) {
|
2014-03-13 14:49:37 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2015-02-20 15:07:48 +01:00
|
|
|
if (session_enforce_http_messaging(session)) {
|
2015-02-19 16:01:15 +01:00
|
|
|
if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
|
|
|
|
nghttp2_stream *subject_stream;
|
|
|
|
|
|
|
|
subject_stream = nghttp2_session_get_stream(
|
|
|
|
session, frame->push_promise.promised_stream_id);
|
|
|
|
if (subject_stream) {
|
2018-10-04 04:56:13 +02:00
|
|
|
rv = nghttp2_http_on_request_headers(subject_stream, frame);
|
2015-02-19 16:01:15 +01:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
assert(frame->hd.type == NGHTTP2_HEADERS);
|
|
|
|
switch (frame->headers.cat) {
|
|
|
|
case NGHTTP2_HCAT_REQUEST:
|
2018-10-04 04:56:13 +02:00
|
|
|
rv = nghttp2_http_on_request_headers(stream, frame);
|
2015-02-19 16:01:15 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_HCAT_RESPONSE:
|
|
|
|
case NGHTTP2_HCAT_PUSH_RESPONSE:
|
|
|
|
rv = nghttp2_http_on_response_headers(stream);
|
|
|
|
break;
|
|
|
|
case NGHTTP2_HCAT_HEADERS:
|
|
|
|
if (stream->http_flags & NGHTTP2_HTTP_FLAG_EXPECT_FINAL_RESPONSE) {
|
|
|
|
assert(!session->server);
|
|
|
|
rv = nghttp2_http_on_response_headers(stream);
|
|
|
|
} else {
|
|
|
|
rv = nghttp2_http_on_trailer_headers(stream, frame);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
assert(0);
|
|
|
|
}
|
|
|
|
if (rv == 0 && (frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
|
|
|
|
rv = nghttp2_http_on_remote_end_stream(stream);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if (rv != 0) {
|
|
|
|
int32_t stream_id;
|
|
|
|
|
|
|
|
if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
|
|
|
|
stream_id = frame->push_promise.promised_stream_id;
|
|
|
|
} else {
|
|
|
|
stream_id = frame->hd.stream_id;
|
|
|
|
}
|
|
|
|
|
|
|
|
call_cb = 0;
|
|
|
|
|
2015-02-26 14:59:07 +01:00
|
|
|
rv = session_handle_invalid_stream2(session, stream_id, frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
NGHTTP2_ERR_HTTP_MESSAGING);
|
2015-02-19 16:01:15 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call_cb) {
|
|
|
|
rv = session_call_on_frame_received(session, frame);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-01-29 13:23:13 +01:00
|
|
|
}
|
2014-03-13 14:49:37 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.type != NGHTTP2_HEADERS) {
|
2014-01-29 13:23:13 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-03-13 14:49:37 +01:00
|
|
|
|
2015-11-27 14:50:13 +01:00
|
|
|
return session_end_stream_headers_received(session, frame, stream);
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
|
|
|
|
2013-07-25 14:07:38 +02:00
|
|
|
int nghttp2_session_on_request_headers_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2013-09-05 16:17:16 +02:00
|
|
|
int rv = 0;
|
|
|
|
nghttp2_stream *stream;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id == 0) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "request HEADERS: stream_id == 0");
|
2013-07-21 11:40:47 +02:00
|
|
|
}
|
2014-11-27 17:22:57 +01:00
|
|
|
|
2017-10-28 15:25:42 +02:00
|
|
|
/* If client receives idle stream from server, it is invalid
|
2014-11-27 17:22:57 +01:00
|
|
|
regardless stream ID is even or odd. This is because client is
|
|
|
|
not expected to receive request from server. */
|
2014-11-27 17:35:12 +01:00
|
|
|
if (!session->server) {
|
|
|
|
if (session_detect_idle_stream(session, frame->hd.stream_id)) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 17:35:12 +01:00
|
|
|
"request HEADERS: client received request");
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
2014-11-27 17:22:57 +01:00
|
|
|
}
|
|
|
|
|
2015-12-20 05:19:29 +01:00
|
|
|
assert(session->server);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!session_is_new_peer_stream_id(session, frame->hd.stream_id)) {
|
2015-12-20 05:19:29 +01:00
|
|
|
if (frame->hd.stream_id == 0 ||
|
|
|
|
nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
|
2014-11-27 17:01:33 +01:00
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 17:01:33 +01:00
|
|
|
"request HEADERS: invalid stream_id");
|
|
|
|
}
|
|
|
|
|
More strict stream state handling
Previously, in server side, we used closed streams to detect the error
that the misbehaving client sends a frame on the incoming stream it
explicitly closed. With this commit, we make a further step, and
detect one more error case. Since we retain closed streams as long as
the sum of its size and the number of opened streams are equal or less
than max concurrent streams, we can safely say that if we get a frame
which is sent on the stream that is not found in either closed or
opened stream, it is already closed or has not existed. Then we can
send GOAWAY.
The previous code shrinks closed streams when we closed another
stream, but now it is removed. It is enough to adjust closed streams
when new incoming stream is created.
While creating this commit, we noticed that
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS is defined as INT32_MAX. But
since SETTINGS can contain value up to UINT32_MAX, it is not enough.
However, since the stream ID space is limited to INT32_MAX, it is high
enough. We could keep this value, but this time we deprecate
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS macro. While it is in public
header, the effect of deprecating it is negligible because of the
reason we wrote above, and usually application sets much smaller value
(say, 100) as SETTINGS_MAX_CONCURRENT_STREAMS.
2016-08-07 07:34:13 +02:00
|
|
|
/* RFC 7540 says if an endpoint receives a HEADERS with invalid
|
|
|
|
* stream ID (e.g, numerically smaller than previous), it MUST
|
|
|
|
* issue connection error with error code PROTOCOL_ERROR. It is a
|
|
|
|
* bit hard to detect this, since we cannot remember all streams
|
|
|
|
* we observed so far.
|
|
|
|
*
|
|
|
|
* You might imagine this is really easy. But no. HTTP/2 is
|
|
|
|
* asynchronous protocol, and usually client and server do not
|
|
|
|
* share the complete picture of open/closed stream status. For
|
|
|
|
* example, after server sends RST_STREAM for a stream, client may
|
|
|
|
* send trailer HEADERS for that stream. If naive server detects
|
|
|
|
* that, and issued connection error, then it is a bug of server
|
|
|
|
* implementation since client is not wrong if it did not get
|
|
|
|
* RST_STREAM when it issued trailer HEADERS.
|
|
|
|
*
|
2016-09-09 15:08:34 +02:00
|
|
|
* At the moment, we are very conservative here. We only use
|
|
|
|
* connection error if stream ID refers idle stream, or we are
|
|
|
|
* sure that stream is half-closed(remote) or closed. Otherwise
|
|
|
|
* we just ignore HEADERS for now.
|
More strict stream state handling
Previously, in server side, we used closed streams to detect the error
that the misbehaving client sends a frame on the incoming stream it
explicitly closed. With this commit, we make a further step, and
detect one more error case. Since we retain closed streams as long as
the sum of its size and the number of opened streams are equal or less
than max concurrent streams, we can safely say that if we get a frame
which is sent on the stream that is not found in either closed or
opened stream, it is already closed or has not existed. Then we can
send GOAWAY.
The previous code shrinks closed streams when we closed another
stream, but now it is removed. It is enough to adjust closed streams
when new incoming stream is created.
While creating this commit, we noticed that
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS is defined as INT32_MAX. But
since SETTINGS can contain value up to UINT32_MAX, it is not enough.
However, since the stream ID space is limited to INT32_MAX, it is high
enough. We could keep this value, but this time we deprecate
NGHTTP2_INITIAL_MAX_CONCURRENT_STREAMS macro. While it is in public
header, the effect of deprecating it is negligible because of the
reason we wrote above, and usually application sets much smaller value
(say, 100) as SETTINGS_MAX_CONCURRENT_STREAMS.
2016-08-07 07:34:13 +02:00
|
|
|
*/
|
2015-12-17 13:30:30 +01:00
|
|
|
stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
|
2016-09-09 15:08:34 +02:00
|
|
|
if (stream && (stream->shut_flags & NGHTTP2_SHUT_RD)) {
|
2015-12-17 13:30:30 +01:00
|
|
|
return session_inflate_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
|
|
|
|
}
|
2014-01-16 15:41:13 +01:00
|
|
|
|
2014-11-28 14:59:13 +01:00
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
|
|
|
}
|
2013-09-05 16:17:16 +02:00
|
|
|
session->last_recv_stream_id = frame->hd.stream_id;
|
2014-11-28 14:59:13 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session_is_incoming_concurrent_streams_max(session)) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 15:39:04 +01:00
|
|
|
"request HEADERS: max concurrent streams exceeded");
|
2014-02-20 14:59:29 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-12-11 13:23:49 +01:00
|
|
|
if (!session_allow_incoming_new_stream(session)) {
|
|
|
|
/* We just ignore stream after GOAWAY was sent */
|
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->headers.pri_spec.stream_id == frame->hd.stream_id) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "request HEADERS: depend on itself");
|
2014-03-25 18:04:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session_is_incoming_concurrent_streams_pending_max(session)) {
|
|
|
|
return session_inflate_handle_invalid_stream(session, frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
NGHTTP2_ERR_REFUSED_STREAM);
|
2012-01-25 15:46:07 +01:00
|
|
|
}
|
2013-09-05 16:17:16 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
stream = nghttp2_session_open_stream(
|
|
|
|
session, frame->hd.stream_id, NGHTTP2_STREAM_FLAG_NONE,
|
|
|
|
&frame->headers.pri_spec, NGHTTP2_STREAM_OPENING, NULL);
|
|
|
|
if (!stream) {
|
2013-09-05 16:17:16 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2015-12-23 08:38:30 +01:00
|
|
|
|
|
|
|
rv = nghttp2_session_adjust_closed_stream(session);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2013-11-28 15:26:34 +01:00
|
|
|
session->last_proc_stream_id = session->last_recv_stream_id;
|
2015-12-23 08:38:30 +01:00
|
|
|
|
2014-01-29 13:23:13 +01:00
|
|
|
rv = session_call_on_begin_headers(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-09-05 16:17:16 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return 0;
|
2012-01-25 15:46:07 +01:00
|
|
|
}
|
|
|
|
|
2013-07-25 14:07:38 +02:00
|
|
|
int nghttp2_session_on_response_headers_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2013-08-29 14:03:39 +02:00
|
|
|
int rv;
|
2013-07-24 18:49:05 +02:00
|
|
|
/* This function is only called if stream->state ==
|
|
|
|
NGHTTP2_STREAM_OPENING and stream_id is local side initiated. */
|
|
|
|
assert(stream->state == NGHTTP2_STREAM_OPENING &&
|
|
|
|
nghttp2_session_is_my_stream_id(session, frame->hd.stream_id));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id == 0) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "response HEADERS: stream_id == 0");
|
2013-07-21 11:40:47 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->shut_flags & NGHTTP2_SHUT_RD) {
|
2013-07-15 14:45:59 +02:00
|
|
|
/* half closed (remote): from the spec:
|
|
|
|
|
|
|
|
If an endpoint receives additional frames for a stream that is
|
|
|
|
in this state it MUST respond with a stream error (Section
|
|
|
|
5.4.2) of type STREAM_CLOSED.
|
2015-12-20 15:29:24 +01:00
|
|
|
|
|
|
|
We go further, and make it connection error.
|
2013-07-15 14:45:59 +02:00
|
|
|
*/
|
2015-12-20 15:29:24 +01:00
|
|
|
return session_inflate_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
|
2013-12-25 15:38:55 +01:00
|
|
|
}
|
2013-09-05 16:17:16 +02:00
|
|
|
stream->state = NGHTTP2_STREAM_OPENED;
|
2014-01-29 13:23:13 +01:00
|
|
|
rv = session_call_on_begin_headers(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-09-05 16:17:16 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return 0;
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
|
|
|
|
2013-07-25 14:07:38 +02:00
|
|
|
int nghttp2_session_on_push_response_headers_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2013-07-24 18:49:05 +02:00
|
|
|
int rv = 0;
|
2014-01-09 13:39:29 +01:00
|
|
|
assert(stream->state == NGHTTP2_STREAM_RESERVED);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id == 0) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 15:39:04 +01:00
|
|
|
"push response HEADERS: stream_id == 0");
|
2012-01-25 15:46:07 +01:00
|
|
|
}
|
2014-02-20 14:59:29 +01:00
|
|
|
|
2015-12-20 15:47:16 +01:00
|
|
|
if (session->server) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"HEADERS: no HEADERS allowed from client in reserved state");
|
2013-12-25 15:38:55 +01:00
|
|
|
}
|
2014-02-20 14:59:29 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session_is_incoming_concurrent_streams_max(session)) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 15:39:04 +01:00
|
|
|
"push response HEADERS: max concurrent streams exceeded");
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
2015-12-11 13:23:49 +01:00
|
|
|
|
|
|
|
if (!session_allow_incoming_new_stream(session)) {
|
|
|
|
/* We don't accept new stream after GOAWAY was sent. */
|
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session_is_incoming_concurrent_streams_pending_max(session)) {
|
|
|
|
return session_inflate_handle_invalid_stream(session, frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
NGHTTP2_ERR_REFUSED_STREAM);
|
2014-02-20 14:59:29 +01:00
|
|
|
}
|
|
|
|
|
2013-07-24 18:49:05 +02:00
|
|
|
nghttp2_stream_promise_fulfilled(stream);
|
2015-08-23 14:22:25 +02:00
|
|
|
if (!nghttp2_session_is_my_stream_id(session, stream->stream_id)) {
|
|
|
|
--session->num_incoming_reserved_streams;
|
|
|
|
}
|
2013-07-24 18:49:05 +02:00
|
|
|
++session->num_incoming_streams;
|
2014-01-29 13:23:13 +01:00
|
|
|
rv = session_call_on_begin_headers(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-09-28 10:59:24 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return 0;
|
2012-01-25 13:31:28 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
int nghttp2_session_on_headers_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_stream *stream) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv = 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id == 0) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "HEADERS: stream_id == 0");
|
2013-07-21 11:40:47 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((stream->shut_flags & NGHTTP2_SHUT_RD)) {
|
2013-07-15 14:45:59 +02:00
|
|
|
/* half closed (remote): from the spec:
|
|
|
|
|
|
|
|
If an endpoint receives additional frames for a stream that is
|
|
|
|
in this state it MUST respond with a stream error (Section
|
|
|
|
5.4.2) of type STREAM_CLOSED.
|
2015-12-20 15:29:24 +01:00
|
|
|
|
|
|
|
we go further, and make it connection error.
|
2013-07-15 14:45:59 +02:00
|
|
|
*/
|
2015-12-20 15:29:24 +01:00
|
|
|
return session_inflate_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_STREAM_CLOSED, "HEADERS: stream closed");
|
2013-12-25 15:38:55 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
|
|
|
|
if (stream->state == NGHTTP2_STREAM_OPENED) {
|
2014-02-18 16:45:20 +01:00
|
|
|
rv = session_call_on_begin_headers(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2013-09-05 16:17:16 +02:00
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return 0;
|
2013-09-05 16:17:16 +02:00
|
|
|
}
|
2015-12-20 15:20:14 +01:00
|
|
|
|
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
|
|
|
/* If this is remote peer initiated stream, it is OK unless it
|
|
|
|
has sent END_STREAM frame already. But if stream is in
|
|
|
|
NGHTTP2_STREAM_CLOSING, we discard the frame. This is a race
|
|
|
|
condition. */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state != NGHTTP2_STREAM_CLOSING) {
|
2014-02-18 16:45:20 +01:00
|
|
|
rv = session_call_on_begin_headers(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_headers_frame(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
int rv;
|
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
rv = nghttp2_frame_unpack_headers_payload(&frame->headers, iframe->sbuf.pos);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR, "HEADERS: could not unpack");
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-01-26 07:44:43 +01:00
|
|
|
frame->headers.cat = NGHTTP2_HCAT_REQUEST;
|
|
|
|
return nghttp2_session_on_request_headers_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_RESERVED) {
|
2014-01-26 07:44:43 +01:00
|
|
|
frame->headers.cat = NGHTTP2_HCAT_PUSH_RESPONSE;
|
|
|
|
return nghttp2_session_on_push_response_headers_received(session, frame,
|
|
|
|
stream);
|
|
|
|
}
|
2015-12-20 15:47:16 +01:00
|
|
|
|
|
|
|
if (stream->state == NGHTTP2_STREAM_OPENING &&
|
|
|
|
nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
|
|
|
|
frame->headers.cat = NGHTTP2_HCAT_RESPONSE;
|
|
|
|
return nghttp2_session_on_response_headers_received(session, frame, stream);
|
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
frame->headers.cat = NGHTTP2_HCAT_HEADERS;
|
|
|
|
return nghttp2_session_on_headers_received(session, frame, stream);
|
2013-07-15 14:45:59 +02:00
|
|
|
}
|
|
|
|
|
2013-07-22 17:11:39 +02:00
|
|
|
int nghttp2_session_on_priority_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2014-03-25 18:04:24 +01:00
|
|
|
int rv;
|
2013-07-22 17:11:39 +02:00
|
|
|
nghttp2_stream *stream;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id == 0) {
|
2015-03-23 13:18:55 +01:00
|
|
|
return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"PRIORITY: stream_id == 0");
|
2013-07-22 17:11:39 +02:00
|
|
|
}
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2015-06-21 09:02:32 +02:00
|
|
|
if (frame->priority.pri_spec.stream_id == frame->hd.stream_id) {
|
|
|
|
return nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR, "depend on itself");
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!session->server) {
|
2014-11-07 16:36:47 +01:00
|
|
|
/* Re-prioritization works only in server */
|
|
|
|
return session_call_on_frame_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2014-06-07 11:36:58 +02:00
|
|
|
stream = nghttp2_session_get_stream_raw(session, frame->hd.stream_id);
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-11-07 16:36:47 +01:00
|
|
|
/* PRIORITY against idle stream can create anchor node in
|
|
|
|
dependency tree. */
|
2014-12-12 16:03:19 +01:00
|
|
|
if (!session_detect_idle_stream(session, frame->hd.stream_id)) {
|
2014-11-07 16:36:47 +01:00
|
|
|
return 0;
|
2014-02-01 08:05:58 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
stream = nghttp2_session_open_stream(
|
|
|
|
session, frame->hd.stream_id, NGHTTP2_STREAM_FLAG_NONE,
|
|
|
|
&frame->priority.pri_spec, NGHTTP2_STREAM_IDLE, NULL);
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == NULL) {
|
2014-11-07 16:36:47 +01:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2015-12-23 08:38:30 +01:00
|
|
|
|
|
|
|
rv = nghttp2_session_adjust_idle_stream(session);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-11-07 16:36:47 +01:00
|
|
|
} else {
|
2014-03-25 18:04:24 +01:00
|
|
|
rv = nghttp2_session_reprioritize_stream(session, stream,
|
|
|
|
&frame->priority.pri_spec);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2015-12-23 08:38:30 +01:00
|
|
|
|
|
|
|
rv = nghttp2_session_adjust_idle_stream(session);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2013-09-05 16:17:16 +02:00
|
|
|
}
|
2014-11-07 16:36:47 +01:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
return session_call_on_frame_received(session, frame);
|
2013-07-22 17:11:39 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_priority_frame(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
nghttp2_frame_unpack_priority_payload(&frame->priority, iframe->sbuf.pos);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return nghttp2_session_on_priority_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
int nghttp2_session_on_rst_stream_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2013-08-29 14:03:39 +02:00
|
|
|
int rv;
|
2014-02-01 08:05:58 +01:00
|
|
|
nghttp2_stream *stream;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id == 0) {
|
2015-03-23 13:18:55 +01:00
|
|
|
return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"RST_STREAM: stream_id == 0");
|
2013-07-21 11:40:47 +02:00
|
|
|
}
|
2016-01-10 16:31:52 +01:00
|
|
|
|
|
|
|
if (session_detect_idle_stream(session, frame->hd.stream_id)) {
|
|
|
|
return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"RST_STREAM: stream in idle");
|
|
|
|
}
|
|
|
|
|
2014-02-01 08:05:58 +01:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
2016-01-10 16:31:52 +01:00
|
|
|
if (stream) {
|
2015-12-17 14:22:22 +01:00
|
|
|
/* We may use stream->shut_flags for strict error checking. */
|
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
|
2014-02-01 08:05:58 +01:00
|
|
|
}
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
rv = session_call_on_frame_received(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-08-29 14:03:39 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2013-08-29 15:58:05 +02:00
|
|
|
rv = nghttp2_session_close_stream(session, frame->hd.stream_id,
|
|
|
|
frame->rst_stream.error_code);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2013-08-29 15:58:05 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2012-01-27 19:54:53 +01:00
|
|
|
return 0;
|
2012-01-27 15:22:27 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_rst_stream_frame(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
nghttp2_frame_unpack_rst_stream_payload(&frame->rst_stream, iframe->sbuf.pos);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return nghttp2_session_on_rst_stream_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int update_remote_initial_window_size_func(nghttp2_map_entry *entry,
|
|
|
|
void *ptr) {
|
2013-08-08 14:12:49 +02:00
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_update_window_size_arg *arg;
|
|
|
|
nghttp2_stream *stream;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
arg = (nghttp2_update_window_size_arg *)ptr;
|
|
|
|
stream = (nghttp2_stream *)entry;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_stream_update_remote_initial_window_size(
|
|
|
|
stream, arg->new_window_size, arg->old_window_size);
|
|
|
|
if (rv != 0) {
|
2016-01-22 16:24:12 +01:00
|
|
|
return nghttp2_session_add_rst_stream(arg->session, stream->stream_id,
|
|
|
|
NGHTTP2_FLOW_CONTROL_ERROR);
|
2013-08-08 14:12:49 +02:00
|
|
|
}
|
2014-04-24 17:35:48 +02:00
|
|
|
|
2012-05-11 15:58:12 +02:00
|
|
|
/* If window size gets positive, push deferred DATA frame to
|
|
|
|
outbound queue. */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->remote_window_size > 0 &&
|
|
|
|
nghttp2_stream_check_deferred_by_flow_control(stream)) {
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
rv = nghttp2_stream_resume_deferred_item(
|
2015-08-16 12:01:10 +02:00
|
|
|
stream, NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2012-05-11 15:58:12 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
2012-03-09 16:10:11 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2013-08-08 17:58:52 +02:00
|
|
|
* Updates the remote initial window size of all active streams. If
|
|
|
|
* error occurs, all streams may not be updated.
|
2012-05-11 15:58:12 +02:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_NOMEM
|
2012-05-11 15:58:12 +02:00
|
|
|
* Out of memory.
|
2012-03-09 16:10:11 +01:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int
|
|
|
|
session_update_remote_initial_window_size(nghttp2_session *session,
|
|
|
|
int32_t new_initial_window_size) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_update_window_size_arg arg;
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2012-05-11 15:58:12 +02:00
|
|
|
arg.session = session;
|
|
|
|
arg.new_window_size = new_initial_window_size;
|
2015-09-23 07:41:53 +02:00
|
|
|
arg.old_window_size = (int32_t)session->remote_settings.initial_window_size;
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
return nghttp2_map_each(&session->streams,
|
2014-11-27 15:39:04 +01:00
|
|
|
update_remote_initial_window_size_func, &arg);
|
2013-08-08 17:58:52 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int update_local_initial_window_size_func(nghttp2_map_entry *entry,
|
|
|
|
void *ptr) {
|
2013-08-08 17:58:52 +02:00
|
|
|
int rv;
|
|
|
|
nghttp2_update_window_size_arg *arg;
|
|
|
|
nghttp2_stream *stream;
|
2014-11-27 15:39:04 +01:00
|
|
|
arg = (nghttp2_update_window_size_arg *)ptr;
|
|
|
|
stream = (nghttp2_stream *)entry;
|
|
|
|
rv = nghttp2_stream_update_local_initial_window_size(
|
|
|
|
stream, arg->new_window_size, arg->old_window_size);
|
|
|
|
if (rv != 0) {
|
2016-01-22 16:24:12 +01:00
|
|
|
return nghttp2_session_add_rst_stream(arg->session, stream->stream_id,
|
|
|
|
NGHTTP2_FLOW_CONTROL_ERROR);
|
2013-08-08 17:58:52 +02:00
|
|
|
}
|
2015-09-30 18:19:57 +02:00
|
|
|
if (!(arg->session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) &&
|
|
|
|
stream->window_update_queued == 0 &&
|
|
|
|
nghttp2_should_send_window_update(stream->local_window_size,
|
|
|
|
stream->recv_window_size)) {
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2015-09-30 18:19:57 +02:00
|
|
|
rv = nghttp2_session_add_window_update(arg->session, NGHTTP2_FLAG_NONE,
|
|
|
|
stream->stream_id,
|
|
|
|
stream->recv_window_size);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
2013-08-08 17:58:52 +02:00
|
|
|
}
|
2015-09-30 18:19:57 +02:00
|
|
|
|
|
|
|
stream->recv_window_size = 0;
|
2013-08-08 17:58:52 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Updates the local initial window size of all active streams. If
|
|
|
|
* error occurs, all streams may not be updated.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int
|
|
|
|
session_update_local_initial_window_size(nghttp2_session *session,
|
|
|
|
int32_t new_initial_window_size,
|
|
|
|
int32_t old_initial_window_size) {
|
2013-08-08 17:58:52 +02:00
|
|
|
nghttp2_update_window_size_arg arg;
|
|
|
|
arg.session = session;
|
|
|
|
arg.new_window_size = new_initial_window_size;
|
|
|
|
arg.old_window_size = old_initial_window_size;
|
|
|
|
return nghttp2_map_each(&session->streams,
|
2014-11-27 15:39:04 +01:00
|
|
|
update_local_initial_window_size_func, &arg);
|
2012-03-09 16:10:11 +01:00
|
|
|
}
|
|
|
|
|
2013-10-27 11:22:51 +01:00
|
|
|
/*
|
|
|
|
* Apply SETTINGS values |iv| having |niv| elements to the local
|
2014-05-12 14:28:49 +02:00
|
|
|
* settings. We assumes that all values in |iv| is correct, since we
|
|
|
|
* validated them in nghttp2_session_add_settings() already.
|
2013-10-27 11:22:51 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_HEADER_COMP
|
|
|
|
* The header table size is out of range
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory
|
|
|
|
*/
|
2013-08-08 17:58:52 +02:00
|
|
|
int nghttp2_session_update_local_settings(nghttp2_session *session,
|
|
|
|
nghttp2_settings_entry *iv,
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t niv) {
|
2013-08-08 17:58:52 +02:00
|
|
|
int rv;
|
2012-03-22 18:17:48 +01:00
|
|
|
size_t i;
|
2013-08-08 17:58:52 +02:00
|
|
|
int32_t new_initial_window_size = -1;
|
2015-09-23 07:41:53 +02:00
|
|
|
uint32_t header_table_size = 0;
|
2015-10-24 06:41:47 +02:00
|
|
|
uint32_t min_header_table_size = UINT32_MAX;
|
2013-10-27 11:22:51 +01:00
|
|
|
uint8_t header_table_size_seen = 0;
|
2015-10-24 06:41:47 +02:00
|
|
|
/* For NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE, use the value last
|
|
|
|
seen. For NGHTTP2_SETTINGS_HEADER_TABLE_SIZE, use both minimum
|
|
|
|
value and last seen value. */
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < niv; ++i) {
|
|
|
|
switch (iv[i].settings_id) {
|
2013-10-27 11:22:51 +01:00
|
|
|
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
|
|
|
header_table_size_seen = 1;
|
|
|
|
header_table_size = iv[i].value;
|
2015-10-24 06:41:47 +02:00
|
|
|
min_header_table_size = nghttp2_min(min_header_table_size, iv[i].value);
|
2013-10-27 11:22:51 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
|
2015-09-23 07:41:53 +02:00
|
|
|
new_initial_window_size = (int32_t)iv[i].value;
|
2013-10-27 11:22:51 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (header_table_size_seen) {
|
2015-10-24 06:41:47 +02:00
|
|
|
if (min_header_table_size < header_table_size) {
|
|
|
|
rv = nghttp2_hd_inflate_change_table_size(&session->hd_inflater,
|
|
|
|
min_header_table_size);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-13 15:22:52 +01:00
|
|
|
rv = nghttp2_hd_inflate_change_table_size(&session->hd_inflater,
|
|
|
|
header_table_size);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-10-27 11:22:51 +01:00
|
|
|
return rv;
|
2013-08-08 17:58:52 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (new_initial_window_size != -1) {
|
|
|
|
rv = session_update_local_initial_window_size(
|
|
|
|
session, new_initial_window_size,
|
2015-09-23 07:41:53 +02:00
|
|
|
(int32_t)session->local_settings.initial_window_size);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-08-08 17:58:52 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < niv; ++i) {
|
|
|
|
switch (iv[i].settings_id) {
|
2014-06-10 14:29:19 +02:00
|
|
|
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
|
|
|
session->local_settings.header_table_size = iv[i].value;
|
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS_ENABLE_PUSH:
|
|
|
|
session->local_settings.enable_push = iv[i].value;
|
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
|
|
|
|
session->local_settings.max_concurrent_streams = iv[i].value;
|
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
|
|
|
|
session->local_settings.initial_window_size = iv[i].value;
|
|
|
|
break;
|
2014-07-27 09:58:04 +02:00
|
|
|
case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
|
|
|
|
session->local_settings.max_frame_size = iv[i].value;
|
|
|
|
break;
|
2014-07-31 14:14:14 +02:00
|
|
|
case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
|
2014-10-28 15:40:47 +01:00
|
|
|
session->local_settings.max_header_list_size = iv[i].value;
|
2014-07-27 09:58:04 +02:00
|
|
|
break;
|
2018-03-10 09:19:45 +01:00
|
|
|
case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
|
|
|
|
session->local_settings.enable_connect_protocol = iv[i].value;
|
|
|
|
break;
|
2013-08-17 16:41:04 +02:00
|
|
|
}
|
2012-03-10 10:41:01 +01:00
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2013-08-08 17:58:52 +02:00
|
|
|
return 0;
|
2012-03-10 10:41:01 +01:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_on_settings_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame, int noack) {
|
2012-05-11 15:58:12 +02:00
|
|
|
int rv;
|
2014-07-14 17:25:31 +02:00
|
|
|
size_t i;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2015-07-22 17:36:00 +02:00
|
|
|
nghttp2_inflight_settings *settings;
|
2014-12-07 15:07:13 +01:00
|
|
|
|
|
|
|
mem = &session->mem;
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id != 0) {
|
2015-03-23 13:18:55 +01:00
|
|
|
return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"SETTINGS: stream_id != 0");
|
2013-07-21 11:40:47 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_ACK) {
|
|
|
|
if (frame->settings.niv != 0) {
|
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_FRAME_SIZE_ERROR,
|
2014-11-27 15:39:04 +01:00
|
|
|
"SETTINGS: ACK and payload != 0");
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
2015-07-22 17:36:00 +02:00
|
|
|
|
|
|
|
settings = session->inflight_settings_head;
|
|
|
|
|
|
|
|
if (!settings) {
|
2014-11-27 15:39:04 +01:00
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "SETTINGS: unexpected ACK");
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
2015-07-22 17:36:00 +02:00
|
|
|
|
|
|
|
rv = nghttp2_session_update_local_settings(session, settings->iv,
|
|
|
|
settings->niv);
|
|
|
|
|
|
|
|
session->inflight_settings_head = settings->next;
|
|
|
|
|
|
|
|
inflight_settings_del(settings, mem);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2013-10-27 11:22:51 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2015-03-23 13:18:55 +01:00
|
|
|
return session_handle_invalid_connection(session, frame, rv, NULL);
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
2014-05-08 16:37:56 +02:00
|
|
|
return session_call_on_frame_received(session, frame);
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2018-05-31 05:24:00 +02:00
|
|
|
if (!session->remote_settings_received) {
|
|
|
|
session->remote_settings.max_concurrent_streams =
|
|
|
|
NGHTTP2_DEFAULT_MAX_CONCURRENT_STREAMS;
|
|
|
|
session->remote_settings_received = 1;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < frame->settings.niv; ++i) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_settings_entry *entry = &frame->settings.iv[i];
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (entry->settings_id) {
|
2013-10-27 11:22:51 +01:00
|
|
|
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-02-13 15:22:52 +01:00
|
|
|
rv = nghttp2_hd_deflate_change_table_size(&session->hd_deflater,
|
|
|
|
entry->value);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2013-10-27 11:22:51 +01:00
|
|
|
return rv;
|
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_HEADER_COMP, NULL);
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
|
|
|
session->remote_settings.header_table_size = entry->value;
|
|
|
|
|
2013-10-27 11:22:51 +01:00
|
|
|
break;
|
2014-02-06 13:38:16 +01:00
|
|
|
case NGHTTP2_SETTINGS_ENABLE_PUSH:
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (entry->value != 0 && entry->value != 1) {
|
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 15:39:04 +01:00
|
|
|
"SETTINGS: invalid SETTINGS_ENBLE_PUSH");
|
2014-02-06 13:38:16 +01:00
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!session->server && entry->value != 0) {
|
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 15:39:04 +01:00
|
|
|
"SETTINGS: server attempted to enable push");
|
2014-04-04 13:36:09 +02:00
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
|
|
|
session->remote_settings.enable_push = entry->value;
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
|
|
|
|
|
|
|
|
session->remote_settings.max_concurrent_streams = entry->value;
|
|
|
|
|
2014-02-06 13:38:16 +01:00
|
|
|
break;
|
2013-07-16 11:26:57 +02:00
|
|
|
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2012-03-09 16:10:11 +01:00
|
|
|
/* Update the initial window size of the all active streams */
|
|
|
|
/* Check that initial_window_size < (1u << 31) */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (entry->value > NGHTTP2_MAX_WINDOW_SIZE) {
|
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_FLOW_CONTROL,
|
2014-11-27 15:39:04 +01:00
|
|
|
"SETTINGS: too large SETTINGS_INITIAL_WINDOW_SIZE");
|
2014-02-06 13:38:16 +01:00
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
rv = session_update_remote_initial_window_size(session,
|
|
|
|
(int32_t)entry->value);
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-06 13:38:16 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_FLOW_CONTROL, NULL);
|
2012-03-09 16:10:11 +01:00
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
|
|
|
session->remote_settings.initial_window_size = entry->value;
|
|
|
|
|
2014-07-27 09:58:04 +02:00
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (entry->value < NGHTTP2_MAX_FRAME_SIZE_MIN ||
|
|
|
|
entry->value > NGHTTP2_MAX_FRAME_SIZE_MAX) {
|
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 15:39:04 +01:00
|
|
|
"SETTINGS: invalid SETTINGS_MAX_FRAME_SIZE");
|
2014-07-27 09:58:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
session->remote_settings.max_frame_size = entry->value;
|
|
|
|
|
|
|
|
break;
|
2014-07-31 14:14:14 +02:00
|
|
|
case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
|
2014-07-27 09:58:04 +02:00
|
|
|
|
2014-10-28 15:40:47 +01:00
|
|
|
session->remote_settings.max_header_list_size = entry->value;
|
2014-07-27 09:58:04 +02:00
|
|
|
|
2018-03-10 09:19:45 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
|
|
|
|
|
|
|
|
if (entry->value != 0 && entry->value != 1) {
|
|
|
|
return session_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"SETTINGS: invalid SETTINGS_ENABLE_CONNECT_PROTOCOL");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!session->server &&
|
|
|
|
session->remote_settings.enable_connect_protocol &&
|
|
|
|
entry->value == 0) {
|
|
|
|
return session_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"SETTINGS: server attempted to disable "
|
|
|
|
"SETTINGS_ENABLE_CONNECT_PROTOCOL");
|
|
|
|
}
|
|
|
|
|
|
|
|
session->remote_settings.enable_connect_protocol = entry->value;
|
|
|
|
|
2013-07-16 11:26:57 +02:00
|
|
|
break;
|
2012-03-09 16:10:11 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2015-01-15 14:32:29 +01:00
|
|
|
if (!noack && !session_is_closing(session)) {
|
2013-10-27 11:22:51 +01:00
|
|
|
rv = nghttp2_session_add_settings(session, NGHTTP2_FLAG_ACK, NULL, 0);
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2013-10-27 11:22:51 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
return session_handle_invalid_connection(session, frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
NGHTTP2_ERR_INTERNAL, NULL);
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
|
|
|
}
|
2014-06-10 14:29:19 +02:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
return session_call_on_frame_received(session, frame);
|
2012-01-31 17:12:26 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_settings_frame(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
2014-07-14 17:25:31 +02:00
|
|
|
size_t i;
|
|
|
|
nghttp2_settings_entry min_header_size_entry;
|
|
|
|
|
2016-04-10 09:36:04 +02:00
|
|
|
if (iframe->max_niv) {
|
|
|
|
min_header_size_entry = iframe->iv[iframe->max_niv - 1];
|
2014-07-14 17:25:31 +02:00
|
|
|
|
2016-04-10 09:36:04 +02:00
|
|
|
if (min_header_size_entry.value < UINT32_MAX) {
|
|
|
|
/* If we have less value, then we must have
|
|
|
|
SETTINGS_HEADER_TABLE_SIZE in i < iframe->niv */
|
|
|
|
for (i = 0; i < iframe->niv; ++i) {
|
|
|
|
if (iframe->iv[i].settings_id == NGHTTP2_SETTINGS_HEADER_TABLE_SIZE) {
|
|
|
|
break;
|
|
|
|
}
|
2014-07-14 17:25:31 +02:00
|
|
|
}
|
|
|
|
|
2016-04-10 09:36:04 +02:00
|
|
|
assert(i < iframe->niv);
|
2014-07-14 17:25:31 +02:00
|
|
|
|
2016-04-10 09:36:04 +02:00
|
|
|
if (min_header_size_entry.value != iframe->iv[i].value) {
|
|
|
|
iframe->iv[iframe->niv++] = iframe->iv[i];
|
|
|
|
iframe->iv[i] = min_header_size_entry;
|
|
|
|
}
|
2014-07-14 17:25:31 +02:00
|
|
|
}
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
|
2016-04-10 09:36:04 +02:00
|
|
|
nghttp2_frame_unpack_settings_payload(&frame->settings, iframe->iv,
|
|
|
|
iframe->niv);
|
|
|
|
|
|
|
|
iframe->iv = NULL;
|
|
|
|
iframe->niv = 0;
|
|
|
|
iframe->max_niv = 0;
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return nghttp2_session_on_settings_received(session, frame, 0 /* ACK */);
|
|
|
|
}
|
|
|
|
|
2013-07-24 18:49:05 +02:00
|
|
|
int nghttp2_session_on_push_promise_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2014-01-16 15:41:13 +01:00
|
|
|
int rv;
|
2013-07-24 18:49:05 +02:00
|
|
|
nghttp2_stream *stream;
|
2013-09-05 16:17:16 +02:00
|
|
|
nghttp2_stream *promised_stream;
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_priority_spec pri_spec;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id == 0) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: stream_id == 0");
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->server || session->local_settings.enable_push == 0) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: push disabled");
|
2013-11-02 08:53:06 +01:00
|
|
|
}
|
2014-07-12 15:57:17 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!nghttp2_session_is_my_stream_id(session, frame->hd.stream_id)) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: invalid stream_id");
|
2014-07-12 15:57:17 +02:00
|
|
|
}
|
|
|
|
|
2015-12-11 13:23:49 +01:00
|
|
|
if (!session_allow_incoming_new_stream(session)) {
|
|
|
|
/* We just discard PUSH_PROMISE after GOAWAY was sent */
|
2015-12-10 15:54:54 +01:00
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!session_is_new_peer_stream_id(session,
|
|
|
|
frame->push_promise.promised_stream_id)) {
|
2013-08-17 15:34:57 +02:00
|
|
|
/* The spec says if an endpoint receives a PUSH_PROMISE with
|
|
|
|
illegal stream ID is subject to a connection error of type
|
|
|
|
PROTOCOL_ERROR. */
|
2014-11-27 15:39:04 +01:00
|
|
|
return session_inflate_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 15:39:04 +01:00
|
|
|
"PUSH_PROMISE: invalid promised_stream_id");
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
2016-01-10 16:31:52 +01:00
|
|
|
|
|
|
|
if (session_detect_idle_stream(session, frame->hd.stream_id)) {
|
|
|
|
return session_inflate_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "PUSH_PROMISE: stream in idle");
|
|
|
|
}
|
|
|
|
|
2013-07-24 18:49:05 +02:00
|
|
|
session->last_recv_stream_id = frame->push_promise.promised_stream_id;
|
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
2015-03-07 08:17:40 +01:00
|
|
|
if (!stream || stream->state == NGHTTP2_STREAM_CLOSING ||
|
2015-08-23 14:22:25 +02:00
|
|
|
!session->pending_enable_push ||
|
|
|
|
session->num_incoming_reserved_streams >=
|
|
|
|
session->max_incoming_reserved_streams) {
|
2016-01-10 16:31:52 +01:00
|
|
|
/* Currently, client does not retain closed stream, so we don't
|
|
|
|
check NGHTTP2_SHUT_RD condition here. */
|
2015-12-17 13:30:30 +01:00
|
|
|
|
2015-11-16 14:20:27 +01:00
|
|
|
rv = nghttp2_session_add_rst_stream(
|
|
|
|
session, frame->push_promise.promised_stream_id, NGHTTP2_CANCEL);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return NGHTTP2_ERR_IGN_HEADER_BLOCK;
|
|
|
|
}
|
2015-12-17 13:30:30 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->shut_flags & NGHTTP2_SHUT_RD) {
|
2015-12-17 13:30:30 +01:00
|
|
|
return session_inflate_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_STREAM_CLOSED,
|
|
|
|
"PUSH_PROMISE: stream closed");
|
2013-09-05 16:17:16 +02:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_priority_spec_init(&pri_spec, stream->stream_id,
|
|
|
|
NGHTTP2_DEFAULT_WEIGHT, 0);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
promised_stream = nghttp2_session_open_stream(
|
2015-02-09 13:45:55 +01:00
|
|
|
session, frame->push_promise.promised_stream_id, NGHTTP2_STREAM_FLAG_NONE,
|
2014-11-27 15:39:04 +01:00
|
|
|
&pri_spec, NGHTTP2_STREAM_RESERVED, NULL);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!promised_stream) {
|
2013-09-05 16:17:16 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-12-23 08:38:30 +01:00
|
|
|
/* We don't call nghttp2_session_adjust_closed_stream(), since we
|
|
|
|
don't keep closed stream in client side */
|
|
|
|
|
2013-11-28 15:26:34 +01:00
|
|
|
session->last_proc_stream_id = session->last_recv_stream_id;
|
2014-01-29 13:23:13 +01:00
|
|
|
rv = session_call_on_begin_headers(session, frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-01-16 15:41:13 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_push_promise_frame(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
int rv;
|
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
rv = nghttp2_frame_unpack_push_promise_payload(&frame->push_promise,
|
|
|
|
iframe->sbuf.pos);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
|
|
|
return nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR, "PUSH_PROMISE: could not unpack");
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return nghttp2_session_on_push_promise_received(session, frame);
|
2013-07-24 18:49:05 +02:00
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_on_ping_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv = 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id != 0) {
|
2015-03-23 13:18:55 +01:00
|
|
|
return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"PING: stream_id != 0");
|
2013-07-21 11:40:47 +02:00
|
|
|
}
|
2016-02-29 15:39:50 +01:00
|
|
|
if ((session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_PING_ACK) == 0 &&
|
|
|
|
(frame->hd.flags & NGHTTP2_FLAG_ACK) == 0 &&
|
2015-01-15 14:32:29 +01:00
|
|
|
!session_is_closing(session)) {
|
2013-07-15 14:45:59 +02:00
|
|
|
/* Peer sent ping, so ping it back */
|
2014-02-18 16:45:20 +01:00
|
|
|
rv = nghttp2_session_add_ping(session, NGHTTP2_FLAG_ACK,
|
|
|
|
frame->ping.opaque_data);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2013-08-29 14:03:39 +02:00
|
|
|
}
|
2012-01-27 15:05:29 +01:00
|
|
|
}
|
2014-05-08 16:37:56 +02:00
|
|
|
return session_call_on_frame_received(session, frame);
|
2012-01-27 15:05:29 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_ping_frame(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
nghttp2_frame_unpack_ping_payload(&frame->ping, iframe->sbuf.pos);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return nghttp2_session_on_ping_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_on_goaway_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2014-11-28 14:59:13 +01:00
|
|
|
int rv;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.stream_id != 0) {
|
2015-03-23 13:18:55 +01:00
|
|
|
return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"GOAWAY: stream_id != 0");
|
2014-01-18 16:37:45 +01:00
|
|
|
}
|
2014-11-28 14:59:13 +01:00
|
|
|
/* Spec says Endpoints MUST NOT increase the value they send in the
|
2014-06-28 03:46:05 +02:00
|
|
|
last stream identifier. */
|
2014-11-28 14:59:13 +01:00
|
|
|
if ((frame->goaway.last_stream_id > 0 &&
|
|
|
|
!nghttp2_session_is_my_stream_id(session,
|
|
|
|
frame->goaway.last_stream_id)) ||
|
|
|
|
session->remote_last_stream_id < frame->goaway.last_stream_id) {
|
2015-03-23 13:18:55 +01:00
|
|
|
return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
|
2014-11-27 15:39:04 +01:00
|
|
|
"GOAWAY: invalid last_stream_id");
|
2014-06-28 03:46:05 +02:00
|
|
|
}
|
|
|
|
|
2015-01-07 14:53:43 +01:00
|
|
|
session->goaway_flags |= NGHTTP2_GOAWAY_RECV;
|
|
|
|
|
2014-06-18 03:56:32 +02:00
|
|
|
session->remote_last_stream_id = frame->goaway.last_stream_id;
|
2014-11-28 14:59:13 +01:00
|
|
|
|
|
|
|
rv = session_call_on_frame_received(session, frame);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return session_close_stream_on_goaway(session, frame->goaway.last_stream_id,
|
|
|
|
0);
|
2012-01-28 11:22:38 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_goaway_frame(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
nghttp2_frame_unpack_goaway_payload(&frame->goaway, iframe->sbuf.pos,
|
|
|
|
iframe->lbuf.pos,
|
|
|
|
nghttp2_buf_len(&iframe->lbuf));
|
2014-03-22 10:59:59 +01:00
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return nghttp2_session_on_goaway_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int
|
|
|
|
session_on_connection_window_update_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame) {
|
2013-09-05 16:21:00 +02:00
|
|
|
/* Handle connection-level flow control */
|
2014-12-09 17:02:30 +01:00
|
|
|
if (frame->window_update.window_size_increment == 0) {
|
2015-12-20 15:29:24 +01:00
|
|
|
return session_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"WINDOW_UPDATE: window_size_increment == 0");
|
2014-12-09 17:02:30 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (NGHTTP2_MAX_WINDOW_SIZE - frame->window_update.window_size_increment <
|
|
|
|
session->remote_window_size) {
|
2014-11-27 15:39:04 +01:00
|
|
|
return session_handle_invalid_connection(session, frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
NGHTTP2_ERR_FLOW_CONTROL, NULL);
|
2013-09-05 16:21:00 +02:00
|
|
|
}
|
|
|
|
session->remote_window_size += frame->window_update.window_size_increment;
|
2014-04-24 17:35:48 +02:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
return session_call_on_frame_received(session, frame);
|
2013-09-05 16:21:00 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_on_stream_window_update_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame) {
|
2013-09-05 16:21:00 +02:00
|
|
|
int rv;
|
|
|
|
nghttp2_stream *stream;
|
2016-01-10 16:31:52 +01:00
|
|
|
|
|
|
|
if (session_detect_idle_stream(session, frame->hd.stream_id)) {
|
|
|
|
return session_handle_invalid_connection(session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"WINDOW_UPDATE to idle stream");
|
|
|
|
}
|
|
|
|
|
2013-09-05 16:21:00 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2013-09-05 16:21:00 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (state_reserved_remote(session, stream)) {
|
|
|
|
return session_handle_invalid_connection(
|
2015-03-23 13:18:55 +01:00
|
|
|
session, frame, NGHTTP2_ERR_PROTO, "WINDOW_UPADATE to reserved stream");
|
2013-09-05 16:21:00 +02:00
|
|
|
}
|
2014-12-09 17:02:30 +01:00
|
|
|
if (frame->window_update.window_size_increment == 0) {
|
2015-12-20 15:29:24 +01:00
|
|
|
return session_handle_invalid_connection(
|
|
|
|
session, frame, NGHTTP2_ERR_PROTO,
|
|
|
|
"WINDOW_UPDATE: window_size_increment == 0");
|
2014-12-09 17:02:30 +01:00
|
|
|
}
|
|
|
|
if (NGHTTP2_MAX_WINDOW_SIZE - frame->window_update.window_size_increment <
|
|
|
|
stream->remote_window_size) {
|
2014-05-08 16:37:56 +02:00
|
|
|
return session_handle_invalid_stream(session, frame,
|
2015-03-23 13:18:55 +01:00
|
|
|
NGHTTP2_ERR_FLOW_CONTROL);
|
2013-09-05 16:21:00 +02:00
|
|
|
}
|
|
|
|
stream->remote_window_size += frame->window_update.window_size_increment;
|
2014-04-24 17:35:48 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->remote_window_size > 0 &&
|
|
|
|
nghttp2_stream_check_deferred_by_flow_control(stream)) {
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-12-20 11:53:57 +01:00
|
|
|
rv = nghttp2_stream_resume_deferred_item(
|
2015-08-16 12:01:10 +02:00
|
|
|
stream, NGHTTP2_STREAM_FLAG_DEFERRED_FLOW_CONTROL);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2013-08-29 14:03:39 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2013-09-05 16:21:00 +02:00
|
|
|
}
|
2014-05-08 16:37:56 +02:00
|
|
|
return session_call_on_frame_received(session, frame);
|
2013-09-05 16:21:00 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_session_on_window_update_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
|
|
|
if (frame->hd.stream_id == 0) {
|
2013-09-05 16:21:00 +02:00
|
|
|
return session_on_connection_window_update_received(session, frame);
|
2013-07-15 14:45:59 +02:00
|
|
|
} else {
|
2013-09-05 16:21:00 +02:00
|
|
|
return session_on_stream_window_update_received(session, frame);
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_window_update_frame(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
2012-05-25 06:49:18 +02:00
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
nghttp2_frame_unpack_window_update_payload(&frame->window_update,
|
|
|
|
iframe->sbuf.pos);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return nghttp2_session_on_window_update_received(session, frame);
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
int nghttp2_session_on_altsvc_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame) {
|
|
|
|
nghttp2_ext_altsvc *altsvc;
|
2016-04-09 12:14:15 +02:00
|
|
|
nghttp2_stream *stream;
|
2016-04-03 15:58:25 +02:00
|
|
|
|
|
|
|
altsvc = frame->ext.payload;
|
|
|
|
|
2016-04-08 16:24:51 +02:00
|
|
|
/* session->server case has been excluded */
|
2016-04-03 15:58:25 +02:00
|
|
|
|
|
|
|
if (frame->hd.stream_id == 0) {
|
|
|
|
if (altsvc->origin_len == 0) {
|
2017-04-26 17:03:20 +02:00
|
|
|
return session_call_on_invalid_frame_recv_callback(session, frame,
|
|
|
|
NGHTTP2_ERR_PROTO);
|
2016-04-03 15:58:25 +02:00
|
|
|
}
|
2016-04-09 12:14:15 +02:00
|
|
|
} else {
|
|
|
|
if (altsvc->origin_len > 0) {
|
2017-04-26 17:03:20 +02:00
|
|
|
return session_call_on_invalid_frame_recv_callback(session, frame,
|
|
|
|
NGHTTP2_ERR_PROTO);
|
2016-04-09 12:14:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
|
|
|
if (!stream) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (stream->state == NGHTTP2_STREAM_CLOSING) {
|
|
|
|
return 0;
|
|
|
|
}
|
2016-04-03 15:58:25 +02:00
|
|
|
}
|
|
|
|
|
2017-04-26 17:03:20 +02:00
|
|
|
if (altsvc->field_value_len == 0) {
|
|
|
|
return session_call_on_invalid_frame_recv_callback(session, frame,
|
|
|
|
NGHTTP2_ERR_PROTO);
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
return session_call_on_frame_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:57:02 +02:00
|
|
|
int nghttp2_session_on_origin_received(nghttp2_session *session,
|
|
|
|
nghttp2_frame *frame) {
|
|
|
|
return session_call_on_frame_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
static int session_process_altsvc_frame(nghttp2_session *session) {
|
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
|
|
|
|
nghttp2_frame_unpack_altsvc_payload(
|
|
|
|
&frame->ext, nghttp2_get_uint16(iframe->sbuf.pos), iframe->lbuf.pos,
|
|
|
|
nghttp2_buf_len(&iframe->lbuf));
|
|
|
|
|
|
|
|
/* nghttp2_frame_unpack_altsvc_payload steals buffer from
|
|
|
|
iframe->lbuf */
|
|
|
|
nghttp2_buf_wrap_init(&iframe->lbuf, NULL, 0);
|
|
|
|
|
|
|
|
return nghttp2_session_on_altsvc_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:57:02 +02:00
|
|
|
static int session_process_origin_frame(nghttp2_session *session) {
|
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
nghttp2_mem *mem = &session->mem;
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
rv = nghttp2_frame_unpack_origin_payload(&frame->ext, iframe->lbuf.pos,
|
|
|
|
nghttp2_buf_len(&iframe->lbuf), mem);
|
|
|
|
if (rv != 0) {
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
/* Ignore ORIGIN frame which cannot be parsed. */
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nghttp2_session_on_origin_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2015-10-10 15:27:48 +02:00
|
|
|
static int session_process_extension_frame(nghttp2_session *session) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
nghttp2_frame *frame = &iframe->frame;
|
|
|
|
|
|
|
|
rv = session_call_unpack_extension_callback(session);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* This handles the case where rv == NGHTTP2_ERR_CANCEL as well */
|
|
|
|
if (rv != 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return session_call_on_frame_received(session, frame);
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
int nghttp2_session_on_data_received(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame *frame) {
|
2013-09-05 16:17:16 +02:00
|
|
|
int rv = 0;
|
2015-02-19 16:01:15 +01:00
|
|
|
int call_cb = 1;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_stream *stream;
|
2014-01-26 07:44:43 +01:00
|
|
|
|
2014-03-13 14:49:37 +01:00
|
|
|
/* We don't call on_frame_recv_callback if stream has been closed
|
|
|
|
already or being closed. */
|
2014-01-27 14:13:41 +01:00
|
|
|
stream = nghttp2_session_get_stream(session, frame->hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream || stream->state == NGHTTP2_STREAM_CLOSING) {
|
2013-09-05 16:17:16 +02:00
|
|
|
/* This should be treated as stream error, but it results in lots
|
|
|
|
of RST_STREAM. So just ignore frame against nonexistent stream
|
|
|
|
for now. */
|
|
|
|
return 0;
|
|
|
|
}
|
2014-03-13 14:49:37 +01:00
|
|
|
|
2015-02-20 15:07:48 +01:00
|
|
|
if (session_enforce_http_messaging(session) &&
|
2015-02-19 16:01:15 +01:00
|
|
|
(frame->hd.flags & NGHTTP2_FLAG_END_STREAM)) {
|
|
|
|
if (nghttp2_http_on_remote_end_stream(stream) != 0) {
|
|
|
|
call_cb = 0;
|
|
|
|
rv = nghttp2_session_add_rst_stream(session, stream->stream_id,
|
|
|
|
NGHTTP2_PROTOCOL_ERROR);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (call_cb) {
|
|
|
|
rv = session_call_on_frame_received(session, frame);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-03-13 14:49:37 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (frame->hd.flags & NGHTTP2_FLAG_END_STREAM) {
|
2013-09-05 16:17:16 +02:00
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
|
|
|
|
rv = nghttp2_session_close_stream_if_shut_rdwr(session, stream);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2013-09-05 16:17:16 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2012-01-28 14:46:12 +01:00
|
|
|
}
|
2013-09-05 16:17:16 +02:00
|
|
|
return 0;
|
2012-01-29 08:27:00 +01:00
|
|
|
}
|
|
|
|
|
2012-02-23 14:49:08 +01:00
|
|
|
/* For errors, this function only returns FATAL error. */
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_process_data_frame(nghttp2_session *session) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-01-27 14:13:41 +01:00
|
|
|
nghttp2_frame *public_data_frame = &session->iframe.frame;
|
2014-02-18 16:45:20 +01:00
|
|
|
rv = nghttp2_session_on_data_received(session, public_data_frame);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-27 19:54:53 +01:00
|
|
|
}
|
2014-02-18 16:45:20 +01:00
|
|
|
return 0;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-08-08 17:58:52 +02:00
|
|
|
/*
|
2013-10-29 13:53:50 +01:00
|
|
|
* Now we have SETTINGS synchronization, flow control error can be
|
|
|
|
* detected strictly. If DATA frame is received with length > 0 and
|
2014-01-20 11:50:11 +01:00
|
|
|
* current received window size + delta length is strictly larger than
|
|
|
|
* local window size, it is subject to FLOW_CONTROL_ERROR, so return
|
|
|
|
* -1. Note that local_window_size is calculated after SETTINGS ACK is
|
|
|
|
* received from peer, so peer must honor this limit. If the resulting
|
2013-10-29 13:53:50 +01:00
|
|
|
* recv_window_size is strictly larger than NGHTTP2_MAX_WINDOW_SIZE,
|
|
|
|
* return -1 too.
|
2013-08-08 17:58:52 +02:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int adjust_recv_window_size(int32_t *recv_window_size_ptr, size_t delta,
|
|
|
|
int32_t local_window_size) {
|
|
|
|
if (*recv_window_size_ptr > local_window_size - (int32_t)delta ||
|
|
|
|
*recv_window_size_ptr > NGHTTP2_MAX_WINDOW_SIZE - (int32_t)delta) {
|
2013-10-29 13:53:50 +01:00
|
|
|
return -1;
|
2013-07-16 13:54:24 +02:00
|
|
|
}
|
2015-09-23 11:07:38 +02:00
|
|
|
*recv_window_size_ptr += (int32_t)delta;
|
2013-08-08 17:58:52 +02:00
|
|
|
return 0;
|
2013-07-16 13:54:24 +02:00
|
|
|
}
|
|
|
|
|
2012-02-25 16:12:32 +01:00
|
|
|
/*
|
2013-08-07 15:02:30 +02:00
|
|
|
* Accumulates received bytes |delta_size| for stream-level flow
|
2014-07-25 14:26:03 +02:00
|
|
|
* control and decides whether to send WINDOW_UPDATE to that stream.
|
|
|
|
* If NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE is set, WINDOW_UPDATE will not
|
|
|
|
* be sent.
|
2012-02-25 16:12:32 +01:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2013-07-12 17:19:03 +02:00
|
|
|
* NGHTTP2_ERR_NOMEM
|
2012-02-25 16:12:32 +01:00
|
|
|
* Out of memory.
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_update_recv_stream_window_size(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream,
|
|
|
|
size_t delta_size,
|
|
|
|
int send_window_update) {
|
2013-08-08 17:58:52 +02:00
|
|
|
int rv;
|
2013-10-29 13:53:50 +01:00
|
|
|
rv = adjust_recv_window_size(&stream->recv_window_size, delta_size,
|
|
|
|
stream->local_window_size);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-08-08 17:58:52 +02:00
|
|
|
return nghttp2_session_add_rst_stream(session, stream->stream_id,
|
2013-10-29 13:53:50 +01:00
|
|
|
NGHTTP2_FLOW_CONTROL_ERROR);
|
2013-08-08 17:58:52 +02:00
|
|
|
}
|
2014-02-11 13:30:44 +01:00
|
|
|
/* We don't have to send WINDOW_UPDATE if the data received is the
|
|
|
|
last chunk in the incoming stream. */
|
2015-09-30 18:19:57 +02:00
|
|
|
/* We have to use local_settings here because it is the constraint
|
|
|
|
the remote endpoint should honor. */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (send_window_update &&
|
2015-09-30 18:19:57 +02:00
|
|
|
!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) &&
|
|
|
|
stream->window_update_queued == 0 &&
|
|
|
|
nghttp2_should_send_window_update(stream->local_window_size,
|
|
|
|
stream->recv_window_size)) {
|
|
|
|
rv = nghttp2_session_add_window_update(session, NGHTTP2_FLAG_NONE,
|
|
|
|
stream->stream_id,
|
|
|
|
stream->recv_window_size);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
2015-09-30 18:19:57 +02:00
|
|
|
|
|
|
|
stream->recv_window_size = 0;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
2013-08-07 15:02:30 +02:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Accumulates received bytes |delta_size| for connection-level flow
|
|
|
|
* control and decides whether to send WINDOW_UPDATE to the
|
2014-07-25 14:26:03 +02:00
|
|
|
* connection. If NGHTTP2_OPT_NO_AUTO_WINDOW_UPDATE is set,
|
|
|
|
* WINDOW_UPDATE will not be sent.
|
2013-08-07 15:02:30 +02:00
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_update_recv_connection_window_size(nghttp2_session *session,
|
|
|
|
size_t delta_size) {
|
2013-08-08 17:58:52 +02:00
|
|
|
int rv;
|
2013-10-29 13:53:50 +01:00
|
|
|
rv = adjust_recv_window_size(&session->recv_window_size, delta_size,
|
|
|
|
session->local_window_size);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-12-25 16:23:07 +01:00
|
|
|
return nghttp2_session_terminate_session(session,
|
|
|
|
NGHTTP2_FLOW_CONTROL_ERROR);
|
2013-08-08 17:58:52 +02:00
|
|
|
}
|
2015-09-30 18:19:57 +02:00
|
|
|
if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) &&
|
|
|
|
session->window_update_queued == 0 &&
|
|
|
|
nghttp2_should_send_window_update(session->local_window_size,
|
|
|
|
session->recv_window_size)) {
|
|
|
|
/* Use stream ID 0 to update connection-level flow control
|
|
|
|
window */
|
|
|
|
rv = nghttp2_session_add_window_update(session, NGHTTP2_FLAG_NONE, 0,
|
|
|
|
session->recv_window_size);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
2014-07-25 14:26:03 +02:00
|
|
|
}
|
2015-09-30 18:19:57 +02:00
|
|
|
|
|
|
|
session->recv_window_size = 0;
|
2014-07-25 14:26:03 +02:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_update_consumed_size(nghttp2_session *session,
|
|
|
|
int32_t *consumed_size_ptr,
|
|
|
|
int32_t *recv_window_size_ptr,
|
2015-09-30 18:19:57 +02:00
|
|
|
uint8_t window_update_queued,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t stream_id, size_t delta_size,
|
|
|
|
int32_t local_window_size) {
|
2014-07-25 14:26:03 +02:00
|
|
|
int32_t recv_size;
|
|
|
|
int rv;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((size_t)*consumed_size_ptr > NGHTTP2_MAX_WINDOW_SIZE - delta_size) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return nghttp2_session_terminate_session(session,
|
|
|
|
NGHTTP2_FLOW_CONTROL_ERROR);
|
|
|
|
}
|
|
|
|
|
2015-09-23 11:07:38 +02:00
|
|
|
*consumed_size_ptr += (int32_t)delta_size;
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2015-09-30 18:19:57 +02:00
|
|
|
if (window_update_queued == 0) {
|
|
|
|
/* recv_window_size may be smaller than consumed_size, because it
|
|
|
|
may be decreased by negative value with
|
|
|
|
nghttp2_submit_window_update(). */
|
|
|
|
recv_size = nghttp2_min(*consumed_size_ptr, *recv_window_size_ptr);
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2015-09-30 18:19:57 +02:00
|
|
|
if (nghttp2_should_send_window_update(local_window_size, recv_size)) {
|
|
|
|
rv = nghttp2_session_add_window_update(session, NGHTTP2_FLAG_NONE,
|
|
|
|
stream_id, recv_size);
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2015-09-30 18:19:57 +02:00
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2015-09-30 18:19:57 +02:00
|
|
|
*recv_window_size_ptr -= recv_size;
|
|
|
|
*consumed_size_ptr -= recv_size;
|
|
|
|
}
|
2014-07-25 14:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_update_stream_consumed_size(nghttp2_session *session,
|
|
|
|
nghttp2_stream *stream,
|
|
|
|
size_t delta_size) {
|
|
|
|
return session_update_consumed_size(
|
|
|
|
session, &stream->consumed_size, &stream->recv_window_size,
|
2015-09-30 18:19:57 +02:00
|
|
|
stream->window_update_queued, stream->stream_id, delta_size,
|
|
|
|
stream->local_window_size);
|
2014-08-23 11:56:04 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_update_connection_consumed_size(nghttp2_session *session,
|
|
|
|
size_t delta_size) {
|
2015-09-30 18:19:57 +02:00
|
|
|
return session_update_consumed_size(
|
|
|
|
session, &session->consumed_size, &session->recv_window_size,
|
|
|
|
session->window_update_queued, 0, delta_size, session->local_window_size);
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
|
|
|
|
2012-06-14 15:39:44 +02:00
|
|
|
/*
|
2014-01-26 07:44:43 +01:00
|
|
|
* Checks that we can receive the DATA frame for stream, which is
|
|
|
|
* indicated by |session->iframe.frame.hd.stream_id|. If it is a
|
|
|
|
* connection error situation, GOAWAY frame will be issued by this
|
|
|
|
* function.
|
|
|
|
*
|
|
|
|
* If the DATA frame is allowed, returns 0.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* NGHTTP2_ERR_IGN_PAYLOAD
|
|
|
|
* The reception of DATA frame is connection error; or should be
|
|
|
|
* ignored.
|
|
|
|
* NGHTTP2_ERR_NOMEM
|
|
|
|
* Out of memory.
|
2012-06-14 15:39:44 +02:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static int session_on_data_received_fail_fast(nghttp2_session *session) {
|
2014-01-26 07:44:43 +01:00
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_stream *stream;
|
2014-04-24 18:27:18 +02:00
|
|
|
nghttp2_inbound_frame *iframe;
|
|
|
|
int32_t stream_id;
|
2014-07-12 15:57:17 +02:00
|
|
|
const char *failure_reason;
|
2015-01-14 15:31:21 +01:00
|
|
|
uint32_t error_code = NGHTTP2_PROTOCOL_ERROR;
|
2014-04-24 18:27:18 +02:00
|
|
|
|
|
|
|
iframe = &session->iframe;
|
|
|
|
stream_id = iframe->frame.hd.stream_id;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream_id == 0) {
|
2014-01-26 07:44:43 +01:00
|
|
|
/* The spec says that if a DATA frame is received whose stream ID
|
|
|
|
is 0, the recipient MUST respond with a connection error of
|
|
|
|
type PROTOCOL_ERROR. */
|
2014-07-12 15:57:17 +02:00
|
|
|
failure_reason = "DATA: stream_id == 0";
|
2014-01-26 07:44:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2016-01-10 16:31:52 +01:00
|
|
|
|
|
|
|
if (session_detect_idle_stream(session, stream_id)) {
|
|
|
|
failure_reason = "DATA: stream in idle";
|
|
|
|
error_code = NGHTTP2_PROTOCOL_ERROR;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2013-07-12 17:19:03 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2015-12-17 13:30:30 +01:00
|
|
|
stream = nghttp2_session_get_stream_raw(session, stream_id);
|
2016-09-09 15:08:34 +02:00
|
|
|
if (stream && (stream->shut_flags & NGHTTP2_SHUT_RD)) {
|
2015-12-17 13:30:30 +01:00
|
|
|
failure_reason = "DATA: stream closed";
|
|
|
|
error_code = NGHTTP2_STREAM_CLOSED;
|
|
|
|
goto fail;
|
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return NGHTTP2_ERR_IGN_PAYLOAD;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->shut_flags & NGHTTP2_SHUT_RD) {
|
|
|
|
failure_reason = "DATA: stream in half-closed(remote)";
|
2015-01-14 15:31:21 +01:00
|
|
|
error_code = NGHTTP2_STREAM_CLOSED;
|
2014-01-26 07:44:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-04-24 18:27:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_session_is_my_stream_id(session, stream_id)) {
|
|
|
|
if (stream->state == NGHTTP2_STREAM_CLOSING) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return NGHTTP2_ERR_IGN_PAYLOAD;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state != NGHTTP2_STREAM_OPENED) {
|
2014-07-12 15:57:17 +02:00
|
|
|
failure_reason = "DATA: stream not opened";
|
2014-01-26 07:44:43 +01:00
|
|
|
goto fail;
|
2012-06-14 15:39:44 +02:00
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_RESERVED) {
|
2014-07-12 15:57:17 +02:00
|
|
|
failure_reason = "DATA: stream in reserved";
|
2014-01-26 07:44:43 +01:00
|
|
|
goto fail;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream->state == NGHTTP2_STREAM_CLOSING) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return NGHTTP2_ERR_IGN_PAYLOAD;
|
2012-06-14 15:39:44 +02:00
|
|
|
}
|
|
|
|
return 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
fail:
|
2015-01-14 15:31:21 +01:00
|
|
|
rv = nghttp2_session_terminate_session_with_reason(session, error_code,
|
|
|
|
failure_reason);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
return NGHTTP2_ERR_IGN_PAYLOAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t inbound_frame_payload_readlen(nghttp2_inbound_frame *iframe,
|
2014-11-27 15:39:04 +01:00
|
|
|
const uint8_t *in,
|
|
|
|
const uint8_t *last) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return nghttp2_min((size_t)(last - in), iframe->payloadleft);
|
|
|
|
}
|
|
|
|
|
2014-02-11 08:33:07 +01:00
|
|
|
/*
|
2014-03-22 10:27:38 +01:00
|
|
|
* Resets iframe->sbuf and advance its mark pointer by |left| bytes.
|
2014-02-11 08:33:07 +01:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static void inbound_frame_set_mark(nghttp2_inbound_frame *iframe, size_t left) {
|
2014-03-22 10:27:38 +01:00
|
|
|
nghttp2_buf_reset(&iframe->sbuf);
|
|
|
|
iframe->sbuf.mark += left;
|
2014-02-11 08:33:07 +01:00
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
static size_t inbound_frame_buf_read(nghttp2_inbound_frame *iframe,
|
2014-11-27 15:39:04 +01:00
|
|
|
const uint8_t *in, const uint8_t *last) {
|
2014-03-22 10:27:38 +01:00
|
|
|
size_t readlen;
|
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
readlen =
|
|
|
|
nghttp2_min((size_t)(last - in), nghttp2_buf_mark_avail(&iframe->sbuf));
|
2014-03-22 10:27:38 +01:00
|
|
|
|
|
|
|
iframe->sbuf.last = nghttp2_cpymem(iframe->sbuf.last, in, readlen);
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return readlen;
|
2012-06-14 15:39:44 +02:00
|
|
|
}
|
|
|
|
|
2014-01-17 14:52:30 +01:00
|
|
|
/*
|
2014-03-22 10:27:38 +01:00
|
|
|
* Unpacks SETTINGS entry in iframe->sbuf.
|
2014-01-17 14:52:30 +01:00
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static void inbound_frame_set_settings_entry(nghttp2_inbound_frame *iframe) {
|
2014-01-26 07:44:43 +01:00
|
|
|
nghttp2_settings_entry iv;
|
2016-04-10 09:36:04 +02:00
|
|
|
nghttp2_settings_entry *min_header_table_size_entry;
|
2014-01-26 07:44:43 +01:00
|
|
|
size_t i;
|
|
|
|
|
2014-03-22 10:27:38 +01:00
|
|
|
nghttp2_frame_unpack_settings_entry(&iv, iframe->sbuf.pos);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (iv.settings_id) {
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
|
|
|
case NGHTTP2_SETTINGS_ENABLE_PUSH:
|
|
|
|
case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
|
|
|
|
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
|
2014-07-27 09:58:04 +02:00
|
|
|
case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
|
2014-07-31 14:14:14 +02:00
|
|
|
case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
|
2018-03-10 09:19:45 +01:00
|
|
|
case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
|
2013-09-28 10:59:24 +02:00
|
|
|
break;
|
|
|
|
default:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: unknown settings id=0x%02x\n", iv.settings_id);
|
2016-04-10 09:36:04 +02:00
|
|
|
|
|
|
|
iframe->iv[iframe->niv++] = iv;
|
|
|
|
|
2014-06-10 14:32:20 +02:00
|
|
|
return;
|
2013-09-28 10:59:24 +02:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = 0; i < iframe->niv; ++i) {
|
|
|
|
if (iframe->iv[i].settings_id == iv.settings_id) {
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->iv[i] = iv;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (i == iframe->niv) {
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->iv[iframe->niv++] = iv;
|
2014-01-17 15:49:40 +01:00
|
|
|
}
|
2014-07-14 17:25:31 +02:00
|
|
|
|
2016-04-10 09:36:04 +02:00
|
|
|
if (iv.settings_id == NGHTTP2_SETTINGS_HEADER_TABLE_SIZE) {
|
|
|
|
/* Keep track of minimum value of SETTINGS_HEADER_TABLE_SIZE */
|
|
|
|
min_header_table_size_entry = &iframe->iv[iframe->max_niv - 1];
|
2014-07-14 17:25:31 +02:00
|
|
|
|
2016-04-10 09:36:04 +02:00
|
|
|
if (iv.value < min_header_table_size_entry->value) {
|
|
|
|
min_header_table_size_entry->value = iv.value;
|
|
|
|
}
|
2014-07-14 17:25:31 +02:00
|
|
|
}
|
2013-09-28 10:59:24 +02:00
|
|
|
}
|
|
|
|
|
2014-02-08 10:02:57 +01:00
|
|
|
/*
|
2014-06-07 11:15:36 +02:00
|
|
|
* Checks PADDED flags and set iframe->sbuf to read them accordingly.
|
|
|
|
* If padding is set, this function returns 1. If no padding is set,
|
|
|
|
* this function returns 0. On error, returns -1.
|
2014-02-08 10:02:57 +01:00
|
|
|
*/
|
2014-02-08 16:35:21 +01:00
|
|
|
static int inbound_frame_handle_pad(nghttp2_inbound_frame *iframe,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame_hd *hd) {
|
|
|
|
if (hd->flags & NGHTTP2_FLAG_PADDED) {
|
|
|
|
if (hd->length < 1) {
|
2014-02-15 08:30:43 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
inbound_frame_set_mark(iframe, 1);
|
2014-02-08 10:02:57 +01:00
|
|
|
return 1;
|
|
|
|
}
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: no padding in payload\n");
|
2014-02-08 10:02:57 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-02-08 16:35:21 +01:00
|
|
|
/*
|
|
|
|
* Computes number of padding based on flags. This function returns
|
|
|
|
* the calculated length if it succeeds, or -1.
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static ssize_t inbound_frame_compute_pad(nghttp2_inbound_frame *iframe) {
|
2014-02-08 16:35:21 +01:00
|
|
|
size_t padlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-06-07 11:15:36 +02:00
|
|
|
/* 1 for Pad Length field */
|
2015-09-23 11:07:38 +02:00
|
|
|
padlen = (size_t)(iframe->sbuf.pos[0] + 1);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: padlen=%zu\n", padlen);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-11 07:28:44 +01:00
|
|
|
/* We cannot use iframe->frame.hd.length because of CONTINUATION */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (padlen - 1 > iframe->payloadleft) {
|
2014-02-08 16:35:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-08 16:35:21 +01:00
|
|
|
iframe->padlen = padlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)padlen;
|
2014-02-08 16:35:21 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* This function returns the effective payload length in the data of
|
|
|
|
* length |readlen| when the remaning payload is |payloadleft|. The
|
|
|
|
* |payloadleft| does not include |readlen|. If padding was started
|
|
|
|
* strictly before this data chunk, this function returns -1.
|
|
|
|
*/
|
|
|
|
static ssize_t inbound_frame_effective_readlen(nghttp2_inbound_frame *iframe,
|
|
|
|
size_t payloadleft,
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t readlen) {
|
|
|
|
size_t trail_padlen =
|
|
|
|
nghttp2_frame_trail_padlen(&iframe->frame, iframe->padlen);
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (trail_padlen > payloadleft) {
|
2014-02-08 16:35:21 +01:00
|
|
|
size_t padlen;
|
|
|
|
padlen = trail_padlen - payloadleft;
|
2014-11-27 15:39:04 +01:00
|
|
|
if (readlen < padlen) {
|
2014-02-08 16:35:21 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)(readlen - padlen);
|
2014-02-08 16:35:21 +01:00
|
|
|
}
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)(readlen);
|
2014-02-08 16:35:21 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
ssize_t nghttp2_session_mem_recv(nghttp2_session *session, const uint8_t *in,
|
|
|
|
size_t inlen) {
|
2014-01-26 07:44:43 +01:00
|
|
|
const uint8_t *first = in, *last = in + inlen;
|
|
|
|
nghttp2_inbound_frame *iframe = &session->iframe;
|
|
|
|
size_t readlen;
|
2014-06-11 16:37:16 +02:00
|
|
|
ssize_t padlen;
|
2014-01-26 07:44:43 +01:00
|
|
|
int rv;
|
|
|
|
int busy = 0;
|
2014-01-26 12:31:28 +01:00
|
|
|
nghttp2_frame_hd cont_hd;
|
2014-02-11 13:30:44 +01:00
|
|
|
nghttp2_stream *stream;
|
2014-03-25 18:04:24 +01:00
|
|
|
size_t pri_fieldlen;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-01-26 07:44:43 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: connection recv_window_size=%d, local_window=%d\n",
|
|
|
|
session->recv_window_size, session->local_window_size);
|
2014-02-11 13:35:41 +01:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
|
|
|
|
2015-12-23 08:38:30 +01:00
|
|
|
/* We may have idle streams more than we expect (e.g.,
|
|
|
|
nghttp2_session_change_stream_priority() or
|
|
|
|
nghttp2_session_create_idle_stream()). Adjust them here. */
|
|
|
|
rv = nghttp2_session_adjust_idle_stream(session);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2017-03-07 14:55:39 +01:00
|
|
|
if (!nghttp2_session_want_read(session)) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (;;) {
|
|
|
|
switch (iframe->state) {
|
2015-04-05 15:35:40 +02:00
|
|
|
case NGHTTP2_IB_READ_CLIENT_MAGIC:
|
2014-09-13 12:50:44 +02:00
|
|
|
readlen = nghttp2_min(inlen, iframe->payloadleft);
|
|
|
|
|
2019-06-22 10:35:13 +02:00
|
|
|
if (memcmp(&NGHTTP2_CLIENT_MAGIC[NGHTTP2_CLIENT_MAGIC_LEN -
|
|
|
|
iframe->payloadleft],
|
2014-11-27 15:39:04 +01:00
|
|
|
in, readlen) != 0) {
|
2015-04-05 15:35:40 +02:00
|
|
|
return NGHTTP2_ERR_BAD_CLIENT_MAGIC;
|
2014-09-13 12:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft == 0) {
|
2014-09-13 12:50:44 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-10-09 14:37:18 +02:00
|
|
|
iframe->state = NGHTTP2_IB_READ_FIRST_SETTINGS;
|
2014-09-13 12:50:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
break;
|
2014-10-09 14:37:18 +02:00
|
|
|
case NGHTTP2_IB_READ_FIRST_SETTINGS:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_FIRST_SETTINGS]\n");
|
2014-10-09 14:37:18 +02:00
|
|
|
|
|
|
|
readlen = inbound_frame_buf_read(iframe, in, last);
|
|
|
|
in += readlen;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
|
2014-10-09 14:37:18 +02:00
|
|
|
return in - first;
|
|
|
|
}
|
|
|
|
|
2015-02-05 17:22:41 +01:00
|
|
|
if (iframe->sbuf.pos[3] != NGHTTP2_SETTINGS ||
|
|
|
|
(iframe->sbuf.pos[4] & NGHTTP2_FLAG_ACK)) {
|
2016-04-02 11:19:46 +02:00
|
|
|
rv = session_call_error_callback(
|
2017-11-19 08:41:10 +01:00
|
|
|
session, NGHTTP2_ERR_SETTINGS_EXPECTED,
|
|
|
|
"Remote peer returned unexpected data while we expected "
|
|
|
|
"SETTINGS frame. Perhaps, peer does not support HTTP/2 "
|
|
|
|
"properly.");
|
2016-04-02 11:19:46 +02:00
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR, "SETTINGS expected");
|
2014-10-09 14:37:18 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-10-09 14:37:18 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-10-09 14:37:18 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_READ_HEAD;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
/* Fall through */
|
2014-08-23 14:08:35 +02:00
|
|
|
case NGHTTP2_IB_READ_HEAD: {
|
|
|
|
int on_begin_frame_called = 0;
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_HEAD]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
readlen = inbound_frame_buf_read(iframe, in, last);
|
|
|
|
in += readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return in - first;
|
|
|
|
}
|
2014-01-26 12:31:28 +01:00
|
|
|
|
2014-03-22 10:27:38 +01:00
|
|
|
nghttp2_frame_unpack_frame_hd(&iframe->frame.hd, iframe->sbuf.pos);
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->payloadleft = iframe->frame.hd.length;
|
|
|
|
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("recv: payloadlen=%zu, type=%u, flags=0x%02x, stream_id=%d\n",
|
2016-10-12 18:11:59 +02:00
|
|
|
iframe->frame.hd.length, iframe->frame.hd.type,
|
|
|
|
iframe->frame.hd.flags, iframe->frame.hd.stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
|
|
|
|
if (iframe->frame.hd.length > session->local_settings.max_frame_size) {
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: length is too large %zu > %u\n", iframe->frame.hd.length,
|
2016-10-12 18:11:59 +02:00
|
|
|
session->local_settings.max_frame_size);
|
2014-07-27 09:58:04 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_FRAME_SIZE_ERROR, "too large frame size");
|
2014-07-27 09:58:04 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-07-27 09:58:04 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-07-27 09:58:04 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (iframe->frame.hd.type) {
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_DATA: {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: DATA\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
iframe->frame.hd.flags &=
|
|
|
|
(NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_PADDED);
|
2014-01-26 07:44:43 +01:00
|
|
|
/* Check stream is open. If it is not open or closing,
|
|
|
|
ignore payload. */
|
|
|
|
busy = 1;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
rv = session_on_data_received_fail_fast(session);
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_IGN_PAYLOAD) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: DATA not allowed stream_id=%d\n",
|
|
|
|
iframe->frame.hd.stream_id);
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_IGN_DATA;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-08 16:35:21 +01:00
|
|
|
rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR,
|
|
|
|
"DATA: insufficient padding space");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-08 10:02:57 +01:00
|
|
|
return rv;
|
2014-02-07 15:22:17 +01:00
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-02-07 15:22:17 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == 1) {
|
2014-02-12 13:35:40 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_PAD_DATA;
|
2014-02-07 15:22:17 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-11 08:24:21 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_DATA;
|
2012-03-17 15:39:38 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_HEADERS:
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: HEADERS\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
iframe->frame.hd.flags &=
|
|
|
|
(NGHTTP2_FLAG_END_STREAM | NGHTTP2_FLAG_END_HEADERS |
|
|
|
|
NGHTTP2_FLAG_PADDED | NGHTTP2_FLAG_PRIORITY);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-08 16:35:21 +01:00
|
|
|
rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR,
|
|
|
|
"HEADERS: insufficient padding space");
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-08 16:35:21 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-02-08 16:35:21 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == 1) {
|
2014-02-11 08:24:21 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
2014-02-08 16:35:21 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
pri_fieldlen = nghttp2_frame_priority_len(iframe->frame.hd.flags);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (pri_fieldlen > 0) {
|
|
|
|
if (iframe->payloadleft < pri_fieldlen) {
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-03-25 18:04:24 +01:00
|
|
|
inbound_frame_set_mark(iframe, pri_fieldlen);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-08-23 14:08:35 +02:00
|
|
|
/* Call on_begin_frame_callback here because
|
|
|
|
session_process_headers_frame() may call
|
|
|
|
on_begin_headers_callback */
|
|
|
|
rv = session_call_on_begin_frame(session, &iframe->frame.hd);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-08-23 14:08:35 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
on_begin_frame_called = 1;
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
rv = session_process_headers_frame(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:43:30 +02:00
|
|
|
if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
|
|
|
rv = nghttp2_session_add_rst_stream(
|
|
|
|
session, iframe->frame.hd.stream_id, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
|
2014-01-30 13:26:18 +01:00
|
|
|
iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PRIORITY:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: PRIORITY\n");
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft != NGHTTP2_PRIORITY_SPECLEN) {
|
2014-03-25 18:04:24 +01:00
|
|
|
busy = 1;
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
|
|
|
|
2014-05-22 14:41:43 +02:00
|
|
|
inbound_frame_set_mark(iframe, NGHTTP2_PRIORITY_SPECLEN);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
|
|
|
break;
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_RST_STREAM:
|
|
|
|
case NGHTTP2_WINDOW_UPDATE:
|
2014-02-11 13:35:41 +01:00
|
|
|
#ifdef DEBUGBUILD
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (iframe->frame.hd.type) {
|
2014-02-11 13:35:41 +01:00
|
|
|
case NGHTTP2_RST_STREAM:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: RST_STREAM\n");
|
2014-02-11 13:35:41 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_WINDOW_UPDATE:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: WINDOW_UPDATE\n");
|
2014-02-11 13:35:41 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
#endif /* DEBUGBUILD */
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-03-05 15:25:42 +01:00
|
|
|
iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft != 4) {
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
|
|
|
inbound_frame_set_mark(iframe, 4);
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_SETTINGS:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: SETTINGS\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-03-05 15:25:42 +01:00
|
|
|
iframe->frame.hd.flags &= NGHTTP2_FLAG_ACK;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((iframe->frame.hd.length % NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) ||
|
|
|
|
((iframe->frame.hd.flags & NGHTTP2_FLAG_ACK) &&
|
|
|
|
iframe->payloadleft > 0)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_SETTINGS;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft) {
|
2016-04-10 09:36:04 +02:00
|
|
|
nghttp2_settings_entry *min_header_table_size_entry;
|
|
|
|
|
2017-10-28 15:25:42 +02:00
|
|
|
/* We allocate iv with additional one entry, to store the
|
2016-04-10 09:36:04 +02:00
|
|
|
minimum header table size. */
|
|
|
|
iframe->max_niv =
|
|
|
|
iframe->frame.hd.length / NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH + 1;
|
|
|
|
|
2017-11-23 06:19:12 +01:00
|
|
|
iframe->iv = nghttp2_mem_malloc(mem, sizeof(nghttp2_settings_entry) *
|
|
|
|
iframe->max_niv);
|
2016-04-10 09:36:04 +02:00
|
|
|
|
|
|
|
if (!iframe->iv) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
min_header_table_size_entry = &iframe->iv[iframe->max_niv - 1];
|
|
|
|
min_header_table_size_entry->settings_id =
|
|
|
|
NGHTTP2_SETTINGS_HEADER_TABLE_SIZE;
|
|
|
|
min_header_table_size_entry->value = UINT32_MAX;
|
|
|
|
|
2014-03-22 10:27:38 +01:00
|
|
|
inbound_frame_set_mark(iframe, NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH);
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
|
|
|
inbound_frame_set_mark(iframe, 0);
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: PUSH_PROMISE\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
iframe->frame.hd.flags &=
|
|
|
|
(NGHTTP2_FLAG_END_HEADERS | NGHTTP2_FLAG_PADDED);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-15 08:30:43 +01:00
|
|
|
rv = inbound_frame_handle_pad(iframe, &iframe->frame.hd);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv < 0) {
|
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR,
|
|
|
|
"PUSH_PROMISE: insufficient padding space");
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-15 08:30:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-02-15 08:30:43 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == 1) {
|
2014-02-15 08:30:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft < 4) {
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
|
|
|
inbound_frame_set_mark(iframe, 4);
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PING:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: PING\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-03-05 15:25:42 +01:00
|
|
|
iframe->frame.hd.flags &= NGHTTP2_FLAG_ACK;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft != 8) {
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
2014-03-22 10:27:38 +01:00
|
|
|
inbound_frame_set_mark(iframe, 8);
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_GOAWAY:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: GOAWAY\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-03-05 15:25:42 +01:00
|
|
|
iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft < 8) {
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
2014-03-22 10:27:38 +01:00
|
|
|
inbound_frame_set_mark(iframe, 8);
|
|
|
|
|
2014-06-07 09:30:55 +02:00
|
|
|
break;
|
|
|
|
case NGHTTP2_CONTINUATION:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: unexpected CONTINUATION\n");
|
2014-06-07 09:30:55 +02:00
|
|
|
|
|
|
|
/* Receiving CONTINUATION in this state are subject to
|
|
|
|
connection error of type PROTOCOL_ERROR */
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR, "CONTINUATION: unexpected");
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-06-07 09:30:55 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-02-06 13:42:49 +01:00
|
|
|
default:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: extension frame\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
if (check_ext_type_set(session->user_recv_ext_types,
|
|
|
|
iframe->frame.hd.type)) {
|
|
|
|
if (!session->callbacks.unpack_extension_callback) {
|
|
|
|
/* Silently ignore unknown frame type. */
|
|
|
|
|
|
|
|
busy = 1;
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2015-10-10 15:27:48 +02:00
|
|
|
|
|
|
|
busy = 1;
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
iframe->state = NGHTTP2_IB_READ_EXTENSION_PAYLOAD;
|
2015-10-10 15:27:48 +02:00
|
|
|
|
|
|
|
break;
|
2016-04-09 11:23:15 +02:00
|
|
|
} else {
|
2016-04-03 15:58:25 +02:00
|
|
|
switch (iframe->frame.hd.type) {
|
|
|
|
case NGHTTP2_ALTSVC:
|
2016-04-09 11:23:15 +02:00
|
|
|
if ((session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ALTSVC) ==
|
|
|
|
0) {
|
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: ALTSVC\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
|
2016-04-06 14:56:01 +02:00
|
|
|
iframe->frame.ext.payload = &iframe->ext_frame_payload.altsvc;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-04-08 16:24:51 +02:00
|
|
|
if (session->server) {
|
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
if (iframe->payloadleft < 2) {
|
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
busy = 1;
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
|
|
|
inbound_frame_set_mark(iframe, 2);
|
|
|
|
|
2018-05-10 03:57:02 +02:00
|
|
|
break;
|
|
|
|
case NGHTTP2_ORIGIN:
|
|
|
|
if (!(session->builtin_recv_ext_types & NGHTTP2_TYPEMASK_ORIGIN)) {
|
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGF("recv: ORIGIN\n");
|
|
|
|
|
|
|
|
iframe->frame.ext.payload = &iframe->ext_frame_payload.origin;
|
|
|
|
|
|
|
|
if (session->server || iframe->frame.hd.stream_id ||
|
|
|
|
(iframe->frame.hd.flags & 0xf0)) {
|
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe->frame.hd.flags = NGHTTP2_FLAG_NONE;
|
|
|
|
|
|
|
|
if (iframe->payloadleft) {
|
|
|
|
iframe->raw_lbuf = nghttp2_mem_malloc(mem, iframe->payloadleft);
|
|
|
|
|
|
|
|
if (iframe->raw_lbuf == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf,
|
|
|
|
iframe->payloadleft);
|
|
|
|
} else {
|
|
|
|
busy = 1;
|
|
|
|
}
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_READ_ORIGIN_PAYLOAD;
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
busy = 1;
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
2014-08-23 14:08:35 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!on_begin_frame_called) {
|
|
|
|
switch (iframe->state) {
|
2014-08-23 14:08:35 +02:00
|
|
|
case NGHTTP2_IB_IGN_HEADER_BLOCK:
|
|
|
|
case NGHTTP2_IB_IGN_PAYLOAD:
|
|
|
|
case NGHTTP2_IB_FRAME_SIZE_ERROR:
|
|
|
|
case NGHTTP2_IB_IGN_DATA:
|
2018-04-22 06:48:56 +02:00
|
|
|
case NGHTTP2_IB_IGN_ALL:
|
2014-08-23 14:08:35 +02:00
|
|
|
break;
|
|
|
|
default:
|
|
|
|
rv = session_call_on_begin_frame(session, &iframe->frame.hd);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-08-23 14:08:35 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
2014-08-23 14:08:35 +02:00
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_IB_READ_NBYTE:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_NBYTE]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
readlen = inbound_frame_buf_read(iframe, in, last);
|
|
|
|
in += readlen;
|
|
|
|
iframe->payloadleft -= readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu, left=%zd\n", readlen,
|
|
|
|
iframe->payloadleft, nghttp2_buf_mark_avail(&iframe->sbuf));
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return in - first;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (iframe->frame.hd.type) {
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_HEADERS:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->padlen == 0 &&
|
|
|
|
(iframe->frame.hd.flags & NGHTTP2_FLAG_PADDED)) {
|
2018-04-22 05:00:55 +02:00
|
|
|
pri_fieldlen = nghttp2_frame_priority_len(iframe->frame.hd.flags);
|
2014-06-11 16:37:16 +02:00
|
|
|
padlen = inbound_frame_compute_pad(iframe);
|
2018-04-22 05:00:55 +02:00
|
|
|
if (padlen < 0 ||
|
|
|
|
(size_t)padlen + pri_fieldlen > 1 + iframe->payloadleft) {
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR, "HEADERS: invalid padding");
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-08 16:35:21 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-02-08 16:35:21 +01:00
|
|
|
}
|
2015-09-23 07:41:53 +02:00
|
|
|
iframe->frame.headers.padlen = (size_t)padlen;
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (pri_fieldlen > 0) {
|
|
|
|
if (iframe->payloadleft < pri_fieldlen) {
|
2014-02-08 16:35:21 +01:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
2014-03-25 18:04:24 +01:00
|
|
|
inbound_frame_set_mark(iframe, pri_fieldlen);
|
2014-02-08 16:35:21 +01:00
|
|
|
break;
|
2014-04-14 16:53:54 +02:00
|
|
|
} else {
|
|
|
|
/* Truncate buffers used for padding spec */
|
|
|
|
inbound_frame_set_mark(iframe, 0);
|
2014-02-08 16:35:21 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
rv = session_process_headers_frame(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:43:30 +02:00
|
|
|
if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
|
|
|
rv = nghttp2_session_add_rst_stream(
|
|
|
|
session, iframe->frame.hd.stream_id, NGHTTP2_INTERNAL_ERROR);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PRIORITY:
|
|
|
|
rv = session_process_priority_frame(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_RST_STREAM:
|
|
|
|
rv = session_process_rst_stream_frame(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->padlen == 0 &&
|
|
|
|
(iframe->frame.hd.flags & NGHTTP2_FLAG_PADDED)) {
|
2014-06-11 16:37:16 +02:00
|
|
|
padlen = inbound_frame_compute_pad(iframe);
|
2018-04-22 05:00:55 +02:00
|
|
|
if (padlen < 0 || (size_t)padlen + 4 /* promised stream id */
|
|
|
|
> 1 + iframe->payloadleft) {
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR,
|
|
|
|
"PUSH_PROMISE: invalid padding");
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-15 08:30:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-02-15 08:30:43 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
iframe->frame.push_promise.padlen = (size_t)padlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft < 4) {
|
2014-02-15 08:30:43 +01:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-15 08:30:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_NBYTE;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
|
|
|
inbound_frame_set_mark(iframe, 4);
|
|
|
|
|
2014-02-15 08:30:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
rv = session_process_push_promise_frame(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2015-04-23 16:43:30 +02:00
|
|
|
if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
|
|
|
rv = nghttp2_session_add_rst_stream(
|
|
|
|
session, iframe->frame.push_promise.promised_stream_id,
|
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_IGN_HEADER_BLOCK) {
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_PING:
|
|
|
|
rv = session_process_ping_frame(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
2014-03-22 10:59:59 +01:00
|
|
|
case NGHTTP2_GOAWAY: {
|
|
|
|
size_t debuglen;
|
|
|
|
|
|
|
|
/* 8 is Last-stream-ID + Error Code */
|
|
|
|
debuglen = iframe->frame.hd.length - 8;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (debuglen > 0) {
|
2014-12-07 15:07:13 +01:00
|
|
|
iframe->raw_lbuf = nghttp2_mem_malloc(mem, debuglen);
|
2014-03-22 10:59:59 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->raw_lbuf == NULL) {
|
2014-03-22 10:59:59 +01:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf, debuglen);
|
|
|
|
}
|
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 1;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_GOAWAY_DEBUG;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
2014-03-22 10:59:59 +01:00
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_WINDOW_UPDATE:
|
|
|
|
rv = session_process_window_update_frame(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
2016-04-03 15:58:25 +02:00
|
|
|
case NGHTTP2_ALTSVC: {
|
|
|
|
size_t origin_len;
|
|
|
|
|
|
|
|
origin_len = nghttp2_get_uint16(iframe->sbuf.pos);
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: origin_len=%zu\n", origin_len);
|
2016-04-03 15:58:25 +02:00
|
|
|
|
2017-04-26 17:03:20 +02:00
|
|
|
if (origin_len > iframe->payloadleft) {
|
2016-04-03 15:58:25 +02:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_FRAME_SIZE_ERROR;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iframe->frame.hd.length > 2) {
|
|
|
|
iframe->raw_lbuf =
|
|
|
|
nghttp2_mem_malloc(mem, iframe->frame.hd.length - 2);
|
|
|
|
|
|
|
|
if (iframe->raw_lbuf == NULL) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_buf_wrap_init(&iframe->lbuf, iframe->raw_lbuf,
|
|
|
|
iframe->frame.hd.length);
|
|
|
|
}
|
|
|
|
|
|
|
|
busy = 1;
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_READ_ALTSVC_PAYLOAD;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
default:
|
|
|
|
/* This is unknown frame */
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case NGHTTP2_IB_READ_HEADER_BLOCK:
|
2014-02-08 16:35:21 +01:00
|
|
|
case NGHTTP2_IB_IGN_HEADER_BLOCK: {
|
|
|
|
ssize_t data_readlen;
|
2015-09-25 15:53:35 +02:00
|
|
|
size_t trail_padlen;
|
|
|
|
int final;
|
2014-02-07 16:32:50 +01:00
|
|
|
#ifdef DEBUGBUILD
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_HEADER_BLOCK]\n");
|
2014-02-07 16:32:50 +01:00
|
|
|
} else {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_IGN_HEADER_BLOCK]\n");
|
2014-02-07 16:32:50 +01:00
|
|
|
}
|
|
|
|
#endif /* DEBUGBUILD */
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
readlen = inbound_frame_payload_readlen(iframe, in, last);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft - readlen);
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
data_readlen = inbound_frame_effective_readlen(
|
|
|
|
iframe, iframe->payloadleft - readlen, readlen);
|
2018-04-22 05:00:55 +02:00
|
|
|
|
|
|
|
if (data_readlen == -1) {
|
|
|
|
/* everything is padding */
|
|
|
|
data_readlen = 0;
|
|
|
|
}
|
|
|
|
|
2015-09-25 15:53:35 +02:00
|
|
|
trail_padlen = nghttp2_frame_trail_padlen(&iframe->frame, iframe->padlen);
|
|
|
|
|
|
|
|
final = (iframe->frame.hd.flags & NGHTTP2_FLAG_END_HEADERS) &&
|
|
|
|
iframe->payloadleft - (size_t)data_readlen == trail_padlen;
|
|
|
|
|
|
|
|
if (data_readlen > 0 || (data_readlen == 0 && final)) {
|
2014-02-08 16:35:21 +01:00
|
|
|
size_t hd_proclen = 0;
|
2015-09-25 15:53:35 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: block final=%d\n", final);
|
2015-09-25 15:53:35 +02:00
|
|
|
|
|
|
|
rv =
|
|
|
|
inflate_header_block(session, &iframe->frame, &hd_proclen,
|
|
|
|
(uint8_t *)in, (size_t)data_readlen, final,
|
|
|
|
iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK);
|
2014-11-27 15:39:04 +01:00
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-27 17:17:23 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_PAUSE) {
|
2014-02-08 16:35:21 +01:00
|
|
|
in += hd_proclen;
|
|
|
|
iframe->payloadleft -= hd_proclen;
|
|
|
|
|
|
|
|
return in - first;
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE) {
|
2014-02-08 16:35:21 +01:00
|
|
|
/* The application says no more headers. We decompress the
|
|
|
|
rest of the header block but not invoke on_header_callback
|
|
|
|
and on_frame_recv_callback. */
|
2014-08-23 17:50:55 +02:00
|
|
|
in += hd_proclen;
|
|
|
|
iframe->payloadleft -= hd_proclen;
|
|
|
|
|
2015-04-23 16:57:39 +02:00
|
|
|
/* Use promised stream ID for PUSH_PROMISE */
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_add_rst_stream(
|
2017-04-30 08:45:53 +02:00
|
|
|
session,
|
|
|
|
iframe->frame.hd.type == NGHTTP2_PUSH_PROMISE
|
|
|
|
? iframe->frame.push_promise.promised_stream_id
|
|
|
|
: iframe->frame.hd.stream_id,
|
2015-04-23 16:57:39 +02:00
|
|
|
NGHTTP2_INTERNAL_ERROR);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-08 16:35:21 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
|
|
|
|
break;
|
|
|
|
}
|
2014-08-23 17:50:55 +02:00
|
|
|
|
|
|
|
in += readlen;
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv == NGHTTP2_ERR_HEADER_COMP) {
|
2014-02-08 16:35:21 +01:00
|
|
|
/* GOAWAY is already issued */
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft == 0) {
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-02-08 16:35:21 +01:00
|
|
|
} else {
|
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
in += readlen;
|
|
|
|
iframe->payloadleft -= readlen;
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft) {
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-14 13:53:03 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((iframe->frame.hd.flags & NGHTTP2_FLAG_END_HEADERS) == 0) {
|
2014-03-22 10:27:38 +01:00
|
|
|
|
|
|
|
inbound_frame_set_mark(iframe, NGHTTP2_FRAME_HDLEN);
|
|
|
|
|
2014-02-08 16:35:21 +01:00
|
|
|
iframe->padlen = 0;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
|
2014-01-26 12:31:28 +01:00
|
|
|
iframe->state = NGHTTP2_IB_EXPECT_CONTINUATION;
|
|
|
|
} else {
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_CONTINUATION;
|
|
|
|
}
|
|
|
|
} else {
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->state == NGHTTP2_IB_READ_HEADER_BLOCK) {
|
2014-02-08 16:35:21 +01:00
|
|
|
rv = session_after_header_block_received(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-08 16:35:21 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-01-26 12:31:28 +01:00
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
2014-02-08 16:35:21 +01:00
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_IB_IGN_PAYLOAD:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_IGN_PAYLOAD]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
readlen = inbound_frame_payload_readlen(iframe, in, last);
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft) {
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
switch (iframe->frame.hd.type) {
|
2014-03-29 15:53:53 +01:00
|
|
|
case NGHTTP2_HEADERS:
|
|
|
|
case NGHTTP2_PUSH_PROMISE:
|
2014-06-07 09:30:55 +02:00
|
|
|
case NGHTTP2_CONTINUATION:
|
2014-03-29 15:53:53 +01:00
|
|
|
/* Mark inflater bad so that we won't perform further decoding */
|
|
|
|
session->hd_inflater.ctx.bad = 1;
|
|
|
|
break;
|
|
|
|
default:
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_IB_FRAME_SIZE_ERROR:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_FRAME_SIZE_ERROR]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2017-03-11 09:46:39 +01:00
|
|
|
rv = session_handle_frame_size_error(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
assert(iframe->state == NGHTTP2_IB_IGN_ALL);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_IB_READ_SETTINGS:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_SETTINGS]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
readlen = inbound_frame_buf_read(iframe, in, last);
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (readlen > 0) {
|
2014-06-07 09:41:36 +02:00
|
|
|
inbound_frame_set_settings_entry(iframe);
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft) {
|
2014-03-22 10:27:38 +01:00
|
|
|
inbound_frame_set_mark(iframe, NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH);
|
2012-03-17 15:39:38 +01:00
|
|
|
break;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
rv = session_process_settings_frame(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_IB_READ_GOAWAY_DEBUG:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_GOAWAY_DEBUG]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
readlen = inbound_frame_payload_readlen(iframe, in, last);
|
2014-03-22 10:59:59 +01:00
|
|
|
|
2016-02-12 14:31:47 +01:00
|
|
|
if (readlen > 0) {
|
|
|
|
iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
|
2014-03-22 10:59:59 +01:00
|
|
|
|
2016-02-12 14:31:47 +01:00
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft) {
|
2014-03-22 10:59:59 +01:00
|
|
|
assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
|
|
|
|
|
2012-03-17 15:39:38 +01:00
|
|
|
break;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-12-02 15:18:51 +01:00
|
|
|
rv = session_process_goaway_frame(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
2014-01-26 12:31:28 +01:00
|
|
|
case NGHTTP2_IB_EXPECT_CONTINUATION:
|
2014-02-11 08:24:21 +01:00
|
|
|
case NGHTTP2_IB_IGN_CONTINUATION:
|
2014-01-26 12:31:28 +01:00
|
|
|
#ifdef DEBUGBUILD
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->state == NGHTTP2_IB_EXPECT_CONTINUATION) {
|
2014-03-19 16:27:39 +01:00
|
|
|
fprintf(stderr, "recv: [IB_EXPECT_CONTINUATION]\n");
|
2014-01-26 12:31:28 +01:00
|
|
|
} else {
|
2014-03-19 16:27:39 +01:00
|
|
|
fprintf(stderr, "recv: [IB_IGN_CONTINUATION]\n");
|
2014-01-26 12:31:28 +01:00
|
|
|
}
|
|
|
|
#endif /* DEBUGBUILD */
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 12:31:28 +01:00
|
|
|
readlen = inbound_frame_buf_read(iframe, in, last);
|
|
|
|
in += readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
|
2014-01-26 12:31:28 +01:00
|
|
|
return in - first;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
|
|
|
nghttp2_frame_unpack_frame_hd(&cont_hd, iframe->sbuf.pos);
|
2014-01-26 12:31:28 +01:00
|
|
|
iframe->payloadleft = cont_hd.length;
|
2014-02-14 17:34:04 +01:00
|
|
|
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("recv: payloadlen=%zu, type=%u, flags=0x%02x, stream_id=%d\n",
|
2016-10-14 15:33:08 +02:00
|
|
|
cont_hd.length, cont_hd.type, cont_hd.flags, cont_hd.stream_id);
|
2014-02-14 17:34:04 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (cont_hd.type != NGHTTP2_CONTINUATION ||
|
|
|
|
cont_hd.stream_id != iframe->frame.hd.stream_id) {
|
2016-10-14 15:49:29 +02:00
|
|
|
DEBUGF("recv: expected stream_id=%d, type=%d, but got stream_id=%d, "
|
|
|
|
"type=%u\n",
|
2016-10-12 18:11:59 +02:00
|
|
|
iframe->frame.hd.stream_id, NGHTTP2_CONTINUATION,
|
|
|
|
cont_hd.stream_id, cont_hd.type);
|
2014-11-27 15:39:04 +01:00
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR,
|
|
|
|
"unexpected non-CONTINUATION frame or stream_id is invalid");
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 12:31:28 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-07-02 15:25:32 +02:00
|
|
|
}
|
|
|
|
|
2014-06-07 11:15:36 +02:00
|
|
|
/* CONTINUATION won't bear NGHTTP2_PADDED flag */
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2015-09-23 11:07:38 +02:00
|
|
|
iframe->frame.hd.flags = (uint8_t)(
|
|
|
|
iframe->frame.hd.flags | (cont_hd.flags & NGHTTP2_FLAG_END_HEADERS));
|
2014-06-07 11:15:36 +02:00
|
|
|
iframe->frame.hd.length += cont_hd.length;
|
2014-02-08 16:35:21 +01:00
|
|
|
|
2014-01-26 12:31:28 +01:00
|
|
|
busy = 1;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->state == NGHTTP2_IB_EXPECT_CONTINUATION) {
|
2014-01-26 12:31:28 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_HEADER_BLOCK;
|
2014-08-23 14:08:35 +02:00
|
|
|
|
|
|
|
rv = session_call_on_begin_frame(session, &cont_hd);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-08-23 14:08:35 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-01-26 12:31:28 +01:00
|
|
|
} else {
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_HEADER_BLOCK;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-08 16:35:21 +01:00
|
|
|
break;
|
2014-02-12 13:35:40 +01:00
|
|
|
case NGHTTP2_IB_READ_PAD_DATA:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_PAD_DATA]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-12 13:35:40 +01:00
|
|
|
readlen = inbound_frame_buf_read(iframe, in, last);
|
|
|
|
in += readlen;
|
|
|
|
iframe->payloadleft -= readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu, left=%zu\n", readlen,
|
|
|
|
iframe->payloadleft, nghttp2_buf_mark_avail(&iframe->sbuf));
|
2014-02-12 13:35:40 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_buf_mark_avail(&iframe->sbuf)) {
|
2014-08-24 15:29:05 +02:00
|
|
|
return in - first;
|
|
|
|
}
|
|
|
|
|
2014-06-07 11:15:36 +02:00
|
|
|
/* Pad Length field is subject to flow control */
|
2014-05-08 16:37:56 +02:00
|
|
|
rv = session_update_recv_connection_window_size(session, readlen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-12 13:35:40 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-07-25 14:26:03 +02:00
|
|
|
/* Pad Length field is consumed immediately */
|
2014-11-27 15:39:04 +01:00
|
|
|
rv =
|
|
|
|
nghttp2_session_consume(session, iframe->frame.hd.stream_id, readlen);
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
stream = nghttp2_session_get_stream(session, iframe->frame.hd.stream_id);
|
|
|
|
if (stream) {
|
|
|
|
rv = session_update_recv_stream_window_size(
|
|
|
|
session, stream, readlen,
|
|
|
|
iframe->payloadleft ||
|
|
|
|
(iframe->frame.hd.flags & NGHTTP2_FLAG_END_STREAM) == 0);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-12 13:35:40 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
busy = 1;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-06-11 16:37:16 +02:00
|
|
|
padlen = inbound_frame_compute_pad(iframe);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (padlen < 0) {
|
|
|
|
rv = nghttp2_session_terminate_session_with_reason(
|
|
|
|
session, NGHTTP2_PROTOCOL_ERROR, "DATA: invalid padding");
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-12 13:35:40 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
return (ssize_t)inlen;
|
2014-02-12 13:35:40 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
iframe->frame.data.padlen = (size_t)padlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-12 13:35:40 +01:00
|
|
|
iframe->state = NGHTTP2_IB_READ_DATA;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-02-12 13:35:40 +01:00
|
|
|
break;
|
2014-01-26 07:44:43 +01:00
|
|
|
case NGHTTP2_IB_READ_DATA:
|
2015-07-08 15:27:04 +02:00
|
|
|
stream = nghttp2_session_get_stream(session, iframe->frame.hd.stream_id);
|
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_DATA;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_DATA]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
readlen = inbound_frame_payload_readlen(iframe, in, last);
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (readlen > 0) {
|
2014-02-08 16:35:21 +01:00
|
|
|
ssize_t data_readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
rv = session_update_recv_connection_window_size(session, readlen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-02-05 16:23:20 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2015-07-08 15:27:04 +02:00
|
|
|
rv = session_update_recv_stream_window_size(
|
|
|
|
session, stream, readlen,
|
|
|
|
iframe->payloadleft ||
|
|
|
|
(iframe->frame.hd.flags & NGHTTP2_FLAG_END_STREAM) == 0);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2015-07-08 15:27:04 +02:00
|
|
|
data_readlen = inbound_frame_effective_readlen(
|
|
|
|
iframe, iframe->payloadleft, readlen);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
if (data_readlen == -1) {
|
|
|
|
/* everything is padding */
|
|
|
|
data_readlen = 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
padlen = (ssize_t)readlen - data_readlen;
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2015-07-08 15:27:04 +02:00
|
|
|
if (padlen > 0) {
|
|
|
|
/* Padding is considered as "consumed" immediately */
|
|
|
|
rv = nghttp2_session_consume(session, iframe->frame.hd.stream_id,
|
2015-09-23 07:41:53 +02:00
|
|
|
(size_t)padlen);
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2015-07-08 15:27:04 +02:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2014-07-25 14:26:03 +02:00
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
|
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
2015-07-08 15:27:04 +02:00
|
|
|
}
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: data_readlen=%zd\n", data_readlen);
|
2015-07-08 15:27:04 +02:00
|
|
|
|
|
|
|
if (data_readlen > 0) {
|
|
|
|
if (session_enforce_http_messaging(session)) {
|
2015-09-23 07:41:53 +02:00
|
|
|
if (nghttp2_http_on_data_chunk(stream, (size_t)data_readlen) != 0) {
|
2015-07-08 15:27:04 +02:00
|
|
|
if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
|
|
|
|
/* Consume all data for connection immediately here */
|
2015-09-23 07:41:53 +02:00
|
|
|
rv = session_update_connection_consumed_size(
|
|
|
|
session, (size_t)data_readlen);
|
2015-06-11 16:34:30 +02:00
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
|
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_DATA) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
2015-06-11 16:34:30 +02:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2015-07-08 15:27:04 +02:00
|
|
|
rv = nghttp2_session_add_rst_stream(
|
|
|
|
session, iframe->frame.hd.stream_id, NGHTTP2_PROTOCOL_ERROR);
|
2015-02-19 16:01:15 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2015-07-08 15:27:04 +02:00
|
|
|
return rv;
|
2015-02-19 16:01:15 +01:00
|
|
|
}
|
2015-07-08 15:27:04 +02:00
|
|
|
busy = 1;
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_DATA;
|
|
|
|
break;
|
2015-02-19 16:01:15 +01:00
|
|
|
}
|
2013-09-28 10:59:24 +02:00
|
|
|
}
|
2015-07-08 15:27:04 +02:00
|
|
|
if (session->callbacks.on_data_chunk_recv_callback) {
|
|
|
|
rv = session->callbacks.on_data_chunk_recv_callback(
|
|
|
|
session, iframe->frame.hd.flags, iframe->frame.hd.stream_id,
|
2015-09-23 07:41:53 +02:00
|
|
|
in - readlen, (size_t)data_readlen, session->user_data);
|
2015-07-08 15:27:04 +02:00
|
|
|
if (rv == NGHTTP2_ERR_PAUSE) {
|
|
|
|
return in - first;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2015-07-08 15:27:04 +02:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2013-08-27 19:13:57 +02:00
|
|
|
}
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft) {
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-14 13:53:03 +01:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
rv = session_process_data_frame(session);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
case NGHTTP2_IB_IGN_DATA:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_IGN_DATA]\n");
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
readlen = inbound_frame_payload_readlen(iframe, in, last);
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (readlen > 0) {
|
2014-01-26 07:44:43 +01:00
|
|
|
/* Update connection-level flow control window for ignored
|
|
|
|
DATA frame too */
|
2014-05-08 16:37:56 +02:00
|
|
|
rv = session_update_recv_connection_window_size(session, readlen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-01-26 07:44:43 +01:00
|
|
|
return rv;
|
|
|
|
}
|
2014-07-02 14:20:40 +02:00
|
|
|
|
2018-04-22 06:48:56 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE) {
|
2014-07-02 14:20:40 +02:00
|
|
|
|
2014-07-25 14:26:03 +02:00
|
|
|
/* Ignored DATA is considered as "consumed" immediately. */
|
|
|
|
rv = session_update_connection_consumed_size(session, readlen);
|
2014-07-02 14:20:40 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-07-02 14:20:40 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2018-04-22 06:48:56 +02:00
|
|
|
|
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
2014-07-02 14:20:40 +02:00
|
|
|
}
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iframe->payloadleft) {
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-05-08 16:37:56 +02:00
|
|
|
session_inbound_frame_reset(session);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
2015-02-14 17:20:10 +01:00
|
|
|
case NGHTTP2_IB_IGN_ALL:
|
2015-09-23 07:41:53 +02:00
|
|
|
return (ssize_t)inlen;
|
2015-10-10 15:27:48 +02:00
|
|
|
case NGHTTP2_IB_READ_EXTENSION_PAYLOAD:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_EXTENSION_PAYLOAD]\n");
|
2015-10-10 15:27:48 +02:00
|
|
|
|
|
|
|
readlen = inbound_frame_payload_readlen(iframe, in, last);
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft);
|
2015-10-10 15:27:48 +02:00
|
|
|
|
|
|
|
if (readlen > 0) {
|
|
|
|
rv = session_call_on_extension_chunk_recv_callback(
|
|
|
|
session, in - readlen, readlen);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (rv != 0) {
|
|
|
|
busy = 1;
|
|
|
|
|
|
|
|
iframe->state = NGHTTP2_IB_IGN_PAYLOAD;
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (iframe->payloadleft > 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = session_process_extension_frame(session);
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
session_inbound_frame_reset(session);
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
break;
|
|
|
|
case NGHTTP2_IB_READ_ALTSVC_PAYLOAD:
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("recv: [IB_READ_ALTSVC_PAYLOAD]\n");
|
2016-04-03 15:58:25 +02:00
|
|
|
|
|
|
|
readlen = inbound_frame_payload_readlen(iframe, in, last);
|
|
|
|
if (readlen > 0) {
|
|
|
|
iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
|
|
|
|
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
|
|
|
}
|
|
|
|
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft);
|
2016-04-03 15:58:25 +02:00
|
|
|
|
|
|
|
if (iframe->payloadleft) {
|
|
|
|
assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = session_process_altsvc_frame(session);
|
2018-05-10 03:57:02 +02:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
session_inbound_frame_reset(session);
|
|
|
|
|
|
|
|
break;
|
|
|
|
case NGHTTP2_IB_READ_ORIGIN_PAYLOAD:
|
|
|
|
DEBUGF("recv: [IB_READ_ORIGIN_PAYLOAD]\n");
|
|
|
|
|
|
|
|
readlen = inbound_frame_payload_readlen(iframe, in, last);
|
|
|
|
|
|
|
|
if (readlen > 0) {
|
|
|
|
iframe->lbuf.last = nghttp2_cpymem(iframe->lbuf.last, in, readlen);
|
|
|
|
|
|
|
|
iframe->payloadleft -= readlen;
|
|
|
|
in += readlen;
|
|
|
|
}
|
|
|
|
|
|
|
|
DEBUGF("recv: readlen=%zu, payloadleft=%zu\n", readlen,
|
|
|
|
iframe->payloadleft);
|
|
|
|
|
|
|
|
if (iframe->payloadleft) {
|
|
|
|
assert(nghttp2_buf_avail(&iframe->lbuf) > 0);
|
|
|
|
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = session_process_origin_frame(session);
|
2016-04-03 15:58:25 +02:00
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2018-05-10 03:57:02 +02:00
|
|
|
if (iframe->state == NGHTTP2_IB_IGN_ALL) {
|
|
|
|
return (ssize_t)inlen;
|
|
|
|
}
|
|
|
|
|
2016-04-03 15:58:25 +02:00
|
|
|
session_inbound_frame_reset(session);
|
|
|
|
|
2015-10-10 15:27:48 +02:00
|
|
|
break;
|
2014-01-26 07:44:43 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!busy && in == last) {
|
2014-01-26 07:44:43 +01:00
|
|
|
break;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
busy = 0;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
assert(in == last);
|
2014-03-22 10:27:38 +01:00
|
|
|
|
2014-01-26 07:44:43 +01:00
|
|
|
return in - first;
|
2012-03-17 15:39:38 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_recv(nghttp2_session *session) {
|
2013-07-12 17:19:03 +02:00
|
|
|
uint8_t buf[NGHTTP2_INBOUND_BUFFER_LENGTH];
|
2014-11-27 15:39:04 +01:00
|
|
|
while (1) {
|
2012-03-17 15:39:38 +01:00
|
|
|
ssize_t readlen;
|
2014-05-08 16:37:56 +02:00
|
|
|
readlen = session_recv(session, buf, sizeof(buf));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (readlen > 0) {
|
2015-09-23 07:41:53 +02:00
|
|
|
ssize_t proclen = nghttp2_session_mem_recv(session, buf, (size_t)readlen);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (proclen < 0) {
|
2014-06-11 16:37:16 +02:00
|
|
|
return (int)proclen;
|
2012-03-17 15:39:38 +01:00
|
|
|
}
|
|
|
|
assert(proclen == readlen);
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (readlen == 0 || readlen == NGHTTP2_ERR_WOULDBLOCK) {
|
2012-03-17 15:39:38 +01:00
|
|
|
return 0;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (readlen == NGHTTP2_ERR_EOF) {
|
2014-06-11 16:37:16 +02:00
|
|
|
return NGHTTP2_ERR_EOF;
|
2014-11-27 15:39:04 +01:00
|
|
|
} else if (readlen < 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-03-17 15:39:38 +01:00
|
|
|
}
|
|
|
|
}
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2014-05-23 15:23:38 +02:00
|
|
|
/*
|
|
|
|
* Returns the number of active streams, which includes streams in
|
|
|
|
* reserved state.
|
|
|
|
*/
|
2014-11-27 15:39:04 +01:00
|
|
|
static size_t session_get_num_active_streams(nghttp2_session *session) {
|
2015-03-28 16:58:41 +01:00
|
|
|
return nghttp2_map_size(&session->streams) - session->num_closed_streams -
|
|
|
|
session->num_idle_streams;
|
2014-05-23 15:23:38 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_want_read(nghttp2_session *session) {
|
2014-05-23 15:23:38 +02:00
|
|
|
size_t num_active_streams;
|
|
|
|
|
2015-01-07 14:53:43 +01:00
|
|
|
/* If this flag is set, we don't want to read. The application
|
2012-02-18 13:55:40 +01:00
|
|
|
should drop the connection. */
|
2015-01-07 14:53:43 +01:00
|
|
|
if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_SENT) {
|
2012-02-18 13:55:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-23 15:23:38 +02:00
|
|
|
|
|
|
|
num_active_streams = session_get_num_active_streams(session);
|
|
|
|
|
2014-11-29 15:12:55 +01:00
|
|
|
/* Unless termination GOAWAY is sent or received, we always want to
|
|
|
|
read incoming frames. */
|
2014-07-12 16:16:25 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (num_active_streams > 0) {
|
2014-07-12 16:16:25 +02:00
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2015-01-07 14:53:43 +01:00
|
|
|
/* If there is no active streams and GOAWAY has been sent or
|
|
|
|
received, we are done with this session. */
|
|
|
|
return (session->goaway_flags &
|
|
|
|
(NGHTTP2_GOAWAY_SENT | NGHTTP2_GOAWAY_RECV)) == 0;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_want_write(nghttp2_session *session) {
|
2015-01-07 14:53:43 +01:00
|
|
|
/* If these flag is set, we don't want to write any data. The
|
2012-02-18 13:55:40 +01:00
|
|
|
application should drop the connection. */
|
2015-01-07 14:53:43 +01:00
|
|
|
if (session->goaway_flags & NGHTTP2_GOAWAY_TERM_SENT) {
|
2012-02-18 13:55:40 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2014-05-23 15:23:38 +02:00
|
|
|
|
2012-01-29 07:11:10 +01:00
|
|
|
/*
|
2014-11-29 15:12:55 +01:00
|
|
|
* Unless termination GOAWAY is sent or received, we want to write
|
|
|
|
* frames if there is pending ones. If pending frame is request/push
|
|
|
|
* response HEADERS and concurrent stream limit is reached, we don't
|
|
|
|
* want to write them.
|
2012-01-29 07:11:10 +01:00
|
|
|
*/
|
2017-03-07 14:55:39 +01:00
|
|
|
return session->aob.item || nghttp2_outbound_queue_top(&session->ob_urgent) ||
|
|
|
|
nghttp2_outbound_queue_top(&session->ob_reg) ||
|
|
|
|
(!nghttp2_pq_empty(&session->root.obq) &&
|
|
|
|
session->remote_window_size > 0) ||
|
|
|
|
(nghttp2_outbound_queue_top(&session->ob_syn) &&
|
|
|
|
!session_is_outgoing_concurrent_streams_max(session));
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
int nghttp2_session_add_ping(nghttp2_session *session, uint8_t flags,
|
2014-11-27 15:39:04 +01:00
|
|
|
const uint8_t *opaque_data) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_outbound_item *item;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_frame *frame;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
2015-09-29 17:44:08 +02:00
|
|
|
|
|
|
|
if ((flags & NGHTTP2_FLAG_ACK) &&
|
2019-06-25 15:33:35 +02:00
|
|
|
session->obq_flood_counter_ >= session->max_outbound_ack) {
|
2015-09-30 15:16:03 +02:00
|
|
|
return NGHTTP2_ERR_FLOODED;
|
2015-09-29 17:44:08 +02:00
|
|
|
}
|
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item == NULL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2012-01-27 15:05:29 +01:00
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2015-04-24 19:23:01 +02:00
|
|
|
nghttp2_outbound_item_init(item);
|
2014-10-02 17:59:44 +02:00
|
|
|
|
|
|
|
frame = &item->frame;
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_ping_init(&frame->ping, flags, opaque_data);
|
2014-10-02 17:59:44 +02:00
|
|
|
|
|
|
|
rv = nghttp2_session_add_item(session, item);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_frame_ping_free(&frame->ping);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, item);
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-27 15:05:29 +01:00
|
|
|
}
|
2015-09-29 17:44:08 +02:00
|
|
|
|
|
|
|
if (flags & NGHTTP2_FLAG_ACK) {
|
|
|
|
++session->obq_flood_counter_;
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:45:20 +01:00
|
|
|
return 0;
|
2012-01-27 15:05:29 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_add_goaway(nghttp2_session *session, int32_t last_stream_id,
|
|
|
|
uint32_t error_code, const uint8_t *opaque_data,
|
2015-01-21 17:43:56 +01:00
|
|
|
size_t opaque_data_len, uint8_t aux_flags) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_outbound_item *item;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_frame *frame;
|
2013-07-15 14:45:59 +02:00
|
|
|
uint8_t *opaque_data_copy = NULL;
|
2014-11-28 14:59:13 +01:00
|
|
|
nghttp2_goaway_aux_data *aux_data;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
|
|
|
|
|
|
|
mem = &session->mem;
|
2014-11-28 14:59:13 +01:00
|
|
|
|
|
|
|
if (nghttp2_session_is_my_stream_id(session, last_stream_id)) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (opaque_data_len) {
|
|
|
|
if (opaque_data_len + 8 > NGHTTP2_MAX_PAYLOADLEN) {
|
2013-07-15 14:45:59 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2014-12-07 15:07:13 +01:00
|
|
|
opaque_data_copy = nghttp2_mem_malloc(mem, opaque_data_len);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (opaque_data_copy == NULL) {
|
2013-07-15 14:45:59 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
memcpy(opaque_data_copy, opaque_data, opaque_data_len);
|
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item == NULL) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, opaque_data_copy);
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2012-01-28 11:22:38 +01:00
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2015-04-24 19:23:01 +02:00
|
|
|
nghttp2_outbound_item_init(item);
|
2014-10-02 17:59:44 +02:00
|
|
|
|
|
|
|
frame = &item->frame;
|
|
|
|
|
2014-11-28 14:59:13 +01:00
|
|
|
/* last_stream_id must not be increased from the value previously
|
|
|
|
sent */
|
|
|
|
last_stream_id = nghttp2_min(last_stream_id, session->local_last_stream_id);
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
nghttp2_frame_goaway_init(&frame->goaway, last_stream_id, error_code,
|
|
|
|
opaque_data_copy, opaque_data_len);
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2014-11-28 14:59:13 +01:00
|
|
|
aux_data = &item->aux_data.goaway;
|
2015-01-21 17:43:56 +01:00
|
|
|
aux_data->flags = aux_flags;
|
2014-11-28 14:59:13 +01:00
|
|
|
|
2014-10-02 17:59:44 +02:00
|
|
|
rv = nghttp2_session_add_item(session, item);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_frame_goaway_free(&frame->goaway, mem);
|
|
|
|
nghttp2_mem_free(mem, item);
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-01-28 11:22:38 +01:00
|
|
|
}
|
2014-02-18 16:45:20 +01:00
|
|
|
return 0;
|
2012-01-28 11:22:38 +01:00
|
|
|
}
|
|
|
|
|
2013-07-15 14:45:59 +02:00
|
|
|
int nghttp2_session_add_window_update(nghttp2_session *session, uint8_t flags,
|
2012-02-25 16:12:32 +01:00
|
|
|
int32_t stream_id,
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t window_size_increment) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_outbound_item *item;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_frame *frame;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
mem = &session->mem;
|
|
|
|
item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item == NULL) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2015-04-24 19:23:01 +02:00
|
|
|
nghttp2_outbound_item_init(item);
|
2014-10-02 17:59:44 +02:00
|
|
|
|
|
|
|
frame = &item->frame;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_frame_window_update_init(&frame->window_update, flags, stream_id,
|
|
|
|
window_size_increment);
|
2014-10-02 17:59:44 +02:00
|
|
|
|
|
|
|
rv = nghttp2_session_add_item(session, item);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_frame_window_update_free(&frame->window_update);
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, item);
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
2014-02-18 16:45:20 +01:00
|
|
|
return 0;
|
2012-02-25 16:12:32 +01:00
|
|
|
}
|
|
|
|
|
2015-07-22 17:36:00 +02:00
|
|
|
static void
|
|
|
|
session_append_inflight_settings(nghttp2_session *session,
|
|
|
|
nghttp2_inflight_settings *settings) {
|
2016-06-21 15:30:21 +02:00
|
|
|
nghttp2_inflight_settings **i;
|
2015-07-22 17:36:00 +02:00
|
|
|
|
2016-06-21 15:30:21 +02:00
|
|
|
for (i = &session->inflight_settings_head; *i; i = &(*i)->next)
|
2015-07-22 17:36:00 +02:00
|
|
|
;
|
|
|
|
|
2016-06-21 15:30:21 +02:00
|
|
|
*i = settings;
|
2015-07-22 17:36:00 +02:00
|
|
|
}
|
|
|
|
|
2013-10-27 11:22:51 +01:00
|
|
|
int nghttp2_session_add_settings(nghttp2_session *session, uint8_t flags,
|
2014-11-27 15:39:04 +01:00
|
|
|
const nghttp2_settings_entry *iv, size_t niv) {
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_outbound_item *item;
|
2013-10-27 11:22:51 +01:00
|
|
|
nghttp2_frame *frame;
|
|
|
|
nghttp2_settings_entry *iv_copy;
|
2014-03-30 11:07:52 +02:00
|
|
|
size_t i;
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
2015-07-22 17:36:00 +02:00
|
|
|
nghttp2_inflight_settings *inflight_settings = NULL;
|
2014-12-07 15:07:13 +01:00
|
|
|
|
|
|
|
mem = &session->mem;
|
2014-03-30 11:07:52 +02:00
|
|
|
|
2015-09-29 17:44:08 +02:00
|
|
|
if (flags & NGHTTP2_FLAG_ACK) {
|
|
|
|
if (niv != 0) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2019-06-25 15:33:35 +02:00
|
|
|
if (session->obq_flood_counter_ >= session->max_outbound_ack) {
|
2015-09-30 15:16:03 +02:00
|
|
|
return NGHTTP2_ERR_FLOODED;
|
2015-09-29 17:44:08 +02:00
|
|
|
}
|
2014-03-30 11:07:52 +02:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!nghttp2_iv_check(iv, niv)) {
|
2013-10-27 11:22:51 +01:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2014-10-02 17:59:44 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
item = nghttp2_mem_malloc(mem, sizeof(nghttp2_outbound_item));
|
2014-11-27 15:39:04 +01:00
|
|
|
if (item == NULL) {
|
2013-10-27 11:22:51 +01:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2014-04-30 15:40:43 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (niv > 0) {
|
2014-12-07 15:07:13 +01:00
|
|
|
iv_copy = nghttp2_frame_iv_copy(iv, niv, mem);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (iv_copy == NULL) {
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, item);
|
2014-04-30 15:40:43 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
iv_copy = NULL;
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
2014-03-30 11:07:52 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((flags & NGHTTP2_FLAG_ACK) == 0) {
|
2015-07-22 17:36:00 +02:00
|
|
|
rv = inflight_settings_new(&inflight_settings, iv, niv, mem);
|
|
|
|
if (rv != 0) {
|
|
|
|
assert(nghttp2_is_fatal(rv));
|
|
|
|
nghttp2_mem_free(mem, iv_copy);
|
|
|
|
nghttp2_mem_free(mem, item);
|
|
|
|
return rv;
|
2014-03-30 11:07:52 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-24 19:23:01 +02:00
|
|
|
nghttp2_outbound_item_init(item);
|
2014-10-02 17:59:44 +02:00
|
|
|
|
|
|
|
frame = &item->frame;
|
|
|
|
|
2013-10-27 11:22:51 +01:00
|
|
|
nghttp2_frame_settings_init(&frame->settings, flags, iv_copy, niv);
|
2014-10-02 17:59:44 +02:00
|
|
|
rv = nghttp2_session_add_item(session, item);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-10-27 11:22:51 +01:00
|
|
|
/* The only expected error is fatal one */
|
2014-02-18 16:45:20 +01:00
|
|
|
assert(nghttp2_is_fatal(rv));
|
2014-03-30 11:07:52 +02:00
|
|
|
|
2015-07-22 17:36:00 +02:00
|
|
|
inflight_settings_del(inflight_settings, mem);
|
2014-03-30 11:07:52 +02:00
|
|
|
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_frame_settings_free(&frame->settings, mem);
|
|
|
|
nghttp2_mem_free(mem, item);
|
2014-03-30 11:07:52 +02:00
|
|
|
|
2014-02-18 16:45:20 +01:00
|
|
|
return rv;
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
2014-03-30 11:07:52 +02:00
|
|
|
|
2015-09-29 17:44:08 +02:00
|
|
|
if (flags & NGHTTP2_FLAG_ACK) {
|
|
|
|
++session->obq_flood_counter_;
|
2016-06-21 15:32:08 +02:00
|
|
|
} else {
|
|
|
|
session_append_inflight_settings(session, inflight_settings);
|
2015-09-29 17:44:08 +02:00
|
|
|
}
|
|
|
|
|
2015-03-07 08:17:40 +01:00
|
|
|
/* Extract NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS and ENABLE_PUSH
|
|
|
|
here. We use it to refuse the incoming stream and PUSH_PROMISE
|
|
|
|
with RST_STREAM. */
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
for (i = niv; i > 0; --i) {
|
|
|
|
if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS) {
|
2014-03-30 11:07:52 +02:00
|
|
|
session->pending_local_max_concurrent_stream = iv[i - 1].value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-07 08:17:40 +01:00
|
|
|
for (i = niv; i > 0; --i) {
|
|
|
|
if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_ENABLE_PUSH) {
|
2015-09-23 07:41:53 +02:00
|
|
|
session->pending_enable_push = (uint8_t)iv[i - 1].value;
|
2015-03-07 08:17:40 +01:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2018-09-27 17:11:33 +02:00
|
|
|
for (i = niv; i > 0; --i) {
|
|
|
|
if (iv[i - 1].settings_id == NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL) {
|
|
|
|
session->pending_enable_connect_protocol = (uint8_t)iv[i - 1].value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-02-18 16:45:20 +01:00
|
|
|
return 0;
|
2013-10-27 11:22:51 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_pack_data(nghttp2_session *session, nghttp2_bufs *bufs,
|
|
|
|
size_t datamax, nghttp2_frame *frame,
|
2015-04-16 14:38:13 +02:00
|
|
|
nghttp2_data_aux_data *aux_data,
|
|
|
|
nghttp2_stream *stream) {
|
2014-06-11 16:37:16 +02:00
|
|
|
int rv;
|
2014-04-05 10:59:24 +02:00
|
|
|
uint32_t data_flags;
|
2014-02-08 09:46:21 +01:00
|
|
|
ssize_t payloadlen;
|
2014-02-11 07:28:44 +01:00
|
|
|
ssize_t padded_payloadlen;
|
2014-03-13 14:11:02 +01:00
|
|
|
nghttp2_buf *buf;
|
2014-06-25 14:17:03 +02:00
|
|
|
size_t max_payloadlen;
|
2014-02-11 07:28:44 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
assert(bufs->head == bufs->cur);
|
2014-02-08 09:46:21 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf = &bufs->cur->buf;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->callbacks.read_length_callback) {
|
2014-08-24 17:32:44 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
payloadlen = session->callbacks.read_length_callback(
|
|
|
|
session, frame->hd.type, stream->stream_id, session->remote_window_size,
|
|
|
|
stream->remote_window_size, session->remote_settings.max_frame_size,
|
|
|
|
session->user_data);
|
2014-08-25 15:41:34 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: read_length_callback=%zd\n", payloadlen);
|
2014-08-25 15:41:34 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
payloadlen = nghttp2_session_enforce_flow_control_limits(session, stream,
|
|
|
|
payloadlen);
|
2014-08-25 15:41:34 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: read_length_callback after flow control=%zd\n", payloadlen);
|
2014-08-25 15:41:34 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (payloadlen <= 0) {
|
2014-08-24 17:32:44 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2014-08-25 15:46:55 +02:00
|
|
|
}
|
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
if ((size_t)payloadlen > nghttp2_buf_avail(buf)) {
|
2014-08-25 15:46:55 +02:00
|
|
|
/* Resize the current buffer(s). The reason why we do +1 for
|
|
|
|
buffer size is for possible padding field. */
|
|
|
|
rv = nghttp2_bufs_realloc(&session->aob.framebufs,
|
2015-09-23 07:41:53 +02:00
|
|
|
(size_t)(NGHTTP2_FRAME_HDLEN + 1 + payloadlen));
|
2014-08-25 15:46:55 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: realloc buffer failed rv=%d", rv);
|
2014-08-25 15:46:55 +02:00
|
|
|
/* If reallocation failed, old buffers are still in tact. So
|
|
|
|
use safe limit. */
|
2015-09-23 07:41:53 +02:00
|
|
|
payloadlen = (ssize_t)datamax;
|
2014-08-25 15:46:55 +02:00
|
|
|
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("send: use safe limit payloadlen=%zd", payloadlen);
|
2014-08-25 16:05:39 +02:00
|
|
|
} else {
|
|
|
|
assert(&session->aob.framebufs == bufs);
|
|
|
|
|
|
|
|
buf = &bufs->cur->buf;
|
2014-08-24 17:32:44 +02:00
|
|
|
}
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
datamax = (size_t)payloadlen;
|
2014-08-24 17:32:44 +02:00
|
|
|
}
|
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
/* Current max DATA length is less then buffer chunk size */
|
2015-09-23 07:41:53 +02:00
|
|
|
assert(nghttp2_buf_avail(buf) >= datamax);
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-04-05 10:59:24 +02:00
|
|
|
data_flags = NGHTTP2_DATA_FLAG_NONE;
|
2014-11-27 15:39:04 +01:00
|
|
|
payloadlen = aux_data->data_prd.read_callback(
|
|
|
|
session, frame->hd.stream_id, buf->pos, datamax, &data_flags,
|
|
|
|
&aux_data->data_prd.source, session->user_data);
|
2014-02-08 09:46:21 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (payloadlen == NGHTTP2_ERR_DEFERRED ||
|
2016-08-28 11:50:19 +02:00
|
|
|
payloadlen == NGHTTP2_ERR_TEMPORAL_CALLBACK_FAILURE ||
|
|
|
|
payloadlen == NGHTTP2_ERR_PAUSE) {
|
2016-10-14 15:33:08 +02:00
|
|
|
DEBUGF("send: DATA postponed due to %s\n",
|
|
|
|
nghttp2_strerror((int)payloadlen));
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-06-11 16:37:16 +02:00
|
|
|
return (int)payloadlen;
|
2014-02-08 09:46:21 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (payloadlen < 0 || datamax < (size_t)payloadlen) {
|
2012-05-11 16:23:46 +02:00
|
|
|
/* This is the error code when callback is failed. */
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
2012-01-26 17:17:40 +01:00
|
|
|
}
|
2014-02-07 15:22:17 +01:00
|
|
|
|
2014-03-13 14:11:02 +01:00
|
|
|
buf->last = buf->pos + payloadlen;
|
|
|
|
buf->pos -= NGHTTP2_FRAME_HDLEN;
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2014-02-07 15:22:17 +01:00
|
|
|
/* Clear flags, because this may contain previous flags of previous
|
|
|
|
DATA */
|
2014-10-02 17:59:44 +02:00
|
|
|
frame->hd.flags = NGHTTP2_FLAG_NONE;
|
2014-02-07 15:22:17 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (data_flags & NGHTTP2_DATA_FLAG_EOF) {
|
2014-10-02 17:59:44 +02:00
|
|
|
aux_data->eof = 1;
|
2015-03-07 09:02:00 +01:00
|
|
|
/* If NGHTTP2_DATA_FLAG_NO_END_STREAM is set, don't set
|
|
|
|
NGHTTP2_FLAG_END_STREAM */
|
|
|
|
if ((aux_data->flags & NGHTTP2_FLAG_END_STREAM) &&
|
|
|
|
(data_flags & NGHTTP2_DATA_FLAG_NO_END_STREAM) == 0) {
|
2014-10-02 17:59:44 +02:00
|
|
|
frame->hd.flags |= NGHTTP2_FLAG_END_STREAM;
|
2014-02-07 15:22:17 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
if (data_flags & NGHTTP2_DATA_FLAG_NO_COPY) {
|
|
|
|
if (session->callbacks.send_data_callback == NULL) {
|
2016-10-12 18:11:59 +02:00
|
|
|
DEBUGF("NGHTTP2_DATA_FLAG_NO_COPY requires send_data_callback set\n");
|
2015-04-03 16:10:51 +02:00
|
|
|
|
|
|
|
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
aux_data->no_copy = 1;
|
|
|
|
}
|
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
frame->hd.length = (size_t)payloadlen;
|
2014-10-02 17:59:44 +02:00
|
|
|
frame->data.padlen = 0;
|
2014-02-11 07:28:44 +01:00
|
|
|
|
2014-10-02 17:59:44 +02:00
|
|
|
max_payloadlen = nghttp2_min(datamax, frame->hd.length + NGHTTP2_MAX_PADLEN);
|
2014-06-25 14:17:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
padded_payloadlen =
|
|
|
|
session_call_select_padding(session, frame, max_payloadlen);
|
2014-06-25 14:17:03 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal((int)padded_payloadlen)) {
|
2014-06-11 16:37:16 +02:00
|
|
|
return (int)padded_payloadlen;
|
2014-02-11 07:28:44 +01:00
|
|
|
}
|
2014-03-10 17:47:38 +01:00
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
frame->data.padlen = (size_t)(padded_payloadlen - payloadlen);
|
2014-02-07 15:22:17 +01:00
|
|
|
|
2014-10-02 17:59:44 +02:00
|
|
|
nghttp2_frame_pack_frame_hd(buf->pos, &frame->hd);
|
2014-02-07 15:22:17 +01:00
|
|
|
|
2015-04-03 16:10:51 +02:00
|
|
|
rv = nghttp2_frame_add_pad(bufs, &frame->hd, frame->data.padlen,
|
|
|
|
aux_data->no_copy);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2014-03-13 14:11:02 +01:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
reschedule_stream(stream);
|
2015-04-16 14:38:13 +02:00
|
|
|
|
2016-04-27 15:57:19 +02:00
|
|
|
if (frame->hd.length == 0 && (data_flags & NGHTTP2_DATA_FLAG_EOF) &&
|
|
|
|
(data_flags & NGHTTP2_DATA_FLAG_NO_END_STREAM)) {
|
|
|
|
/* DATA payload length is 0, and DATA frame does not bear
|
|
|
|
END_STREAM. In this case, there is no point to send 0 length
|
|
|
|
DATA frame. */
|
|
|
|
return NGHTTP2_ERR_CANCEL;
|
|
|
|
}
|
|
|
|
|
2014-04-01 14:59:26 +02:00
|
|
|
return 0;
|
2012-01-24 14:02:24 +01:00
|
|
|
}
|
2012-01-27 15:05:29 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
void *nghttp2_session_get_stream_user_data(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream) {
|
2012-02-03 17:37:21 +01:00
|
|
|
return stream->stream_user_data;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 15:42:25 +01:00
|
|
|
|
2013-12-23 09:40:34 +01:00
|
|
|
int nghttp2_session_set_stream_user_data(nghttp2_session *session,
|
|
|
|
int32_t stream_id,
|
2014-11-27 15:39:04 +01:00
|
|
|
void *stream_user_data) {
|
2013-12-23 09:40:34 +01:00
|
|
|
nghttp2_stream *stream;
|
2018-08-18 10:38:58 +02:00
|
|
|
nghttp2_frame *frame;
|
|
|
|
nghttp2_outbound_item *item;
|
|
|
|
|
2013-12-23 09:40:34 +01:00
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2018-08-18 10:38:58 +02:00
|
|
|
if (stream) {
|
|
|
|
stream->stream_user_data = stream_user_data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (session->server || !nghttp2_session_is_my_stream_id(session, stream_id) ||
|
|
|
|
!nghttp2_outbound_queue_top(&session->ob_syn)) {
|
2013-12-23 09:40:34 +01:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2018-08-18 10:38:58 +02:00
|
|
|
|
|
|
|
frame = &nghttp2_outbound_queue_top(&session->ob_syn)->frame;
|
|
|
|
assert(frame->hd.type == NGHTTP2_HEADERS);
|
|
|
|
|
|
|
|
if (frame->hd.stream_id > stream_id ||
|
|
|
|
(uint32_t)stream_id >= session->next_stream_id) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (item = session->ob_syn.head; item; item = item->qnext) {
|
|
|
|
if (item->frame.hd.stream_id < stream_id) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (item->frame.hd.stream_id > stream_id) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
|
|
|
|
item->aux_data.headers.stream_user_data = stream_user_data;
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
2013-12-23 09:40:34 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_resume_data(nghttp2_session *session, int32_t stream_id) {
|
2014-02-18 16:45:20 +01:00
|
|
|
int rv;
|
2013-07-12 17:19:03 +02:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2014-12-20 11:53:57 +01:00
|
|
|
if (stream == NULL || !nghttp2_stream_check_deferred_item(stream)) {
|
2013-07-12 17:19:03 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
2012-02-19 15:42:25 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2015-08-16 12:01:10 +02:00
|
|
|
rv = nghttp2_stream_resume_deferred_item(stream,
|
|
|
|
NGHTTP2_STREAM_FLAG_DEFERRED_USER);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-03-25 18:04:24 +01:00
|
|
|
return rv;
|
2012-02-19 15:42:25 +01:00
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2017-01-07 18:46:00 +01:00
|
|
|
return 0;
|
2012-02-19 15:42:25 +01:00
|
|
|
}
|
2012-02-25 16:12:32 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t nghttp2_session_get_outbound_queue_size(nghttp2_session *session) {
|
2015-04-24 15:30:32 +02:00
|
|
|
return nghttp2_outbound_queue_size(&session->ob_urgent) +
|
|
|
|
nghttp2_outbound_queue_size(&session->ob_reg) +
|
2015-08-16 12:01:10 +02:00
|
|
|
nghttp2_outbound_queue_size(&session->ob_syn);
|
|
|
|
/* TODO account for item attached to stream */
|
2012-03-15 15:06:28 +01:00
|
|
|
}
|
2012-04-05 18:45:39 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t
|
|
|
|
nghttp2_session_get_stream_effective_recv_data_length(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2013-10-29 16:07:35 +01:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == NULL) {
|
2013-10-29 16:07:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2013-10-29 17:58:38 +01:00
|
|
|
return stream->recv_window_size < 0 ? 0 : stream->recv_window_size;
|
2013-10-29 16:07:35 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t
|
|
|
|
nghttp2_session_get_stream_effective_local_window_size(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2013-10-29 16:07:35 +01:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == NULL) {
|
2013-10-29 16:07:35 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
return stream->local_window_size;
|
|
|
|
}
|
|
|
|
|
2016-08-21 12:01:51 +02:00
|
|
|
int32_t nghttp2_session_get_stream_local_window_size(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
int32_t size;
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
if (stream == NULL) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
size = stream->local_window_size - stream->recv_window_size;
|
|
|
|
|
|
|
|
/* size could be negative if local endpoint reduced
|
|
|
|
SETTINGS_INITIAL_WINDOW_SIZE */
|
|
|
|
if (size < 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
return size;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t
|
|
|
|
nghttp2_session_get_effective_recv_data_length(nghttp2_session *session) {
|
2013-10-29 17:58:38 +01:00
|
|
|
return session->recv_window_size < 0 ? 0 : session->recv_window_size;
|
2013-10-29 16:51:01 +01:00
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t
|
|
|
|
nghttp2_session_get_effective_local_window_size(nghttp2_session *session) {
|
2013-10-29 16:51:01 +01:00
|
|
|
return session->local_window_size;
|
|
|
|
}
|
|
|
|
|
2016-08-21 12:01:51 +02:00
|
|
|
int32_t nghttp2_session_get_local_window_size(nghttp2_session *session) {
|
|
|
|
return session->local_window_size - session->recv_window_size;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t nghttp2_session_get_stream_remote_window_size(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2014-03-21 16:34:25 +01:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream == NULL) {
|
2014-03-21 16:34:25 +01:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-08-23 11:41:34 +02:00
|
|
|
/* stream->remote_window_size can be negative when
|
|
|
|
SETTINGS_INITIAL_WINDOW_SIZE is changed. */
|
|
|
|
return nghttp2_max(0, stream->remote_window_size);
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int32_t nghttp2_session_get_remote_window_size(nghttp2_session *session) {
|
2014-08-23 11:41:34 +02:00
|
|
|
return session->remote_window_size;
|
2014-03-21 16:34:25 +01:00
|
|
|
}
|
|
|
|
|
2014-05-02 16:34:57 +02:00
|
|
|
uint32_t nghttp2_session_get_remote_settings(nghttp2_session *session,
|
2014-11-27 15:39:04 +01:00
|
|
|
nghttp2_settings_id id) {
|
|
|
|
switch (id) {
|
2014-06-10 14:29:19 +02:00
|
|
|
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
|
|
|
return session->remote_settings.header_table_size;
|
|
|
|
case NGHTTP2_SETTINGS_ENABLE_PUSH:
|
|
|
|
return session->remote_settings.enable_push;
|
|
|
|
case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
|
|
|
|
return session->remote_settings.max_concurrent_streams;
|
|
|
|
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
|
|
|
|
return session->remote_settings.initial_window_size;
|
2014-07-27 09:58:04 +02:00
|
|
|
case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
|
|
|
|
return session->remote_settings.max_frame_size;
|
2014-07-31 14:14:14 +02:00
|
|
|
case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
|
2014-10-28 15:40:47 +01:00
|
|
|
return session->remote_settings.max_header_list_size;
|
2018-03-10 09:19:45 +01:00
|
|
|
case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
|
|
|
|
return session->remote_settings.enable_connect_protocol;
|
2014-05-02 16:34:57 +02:00
|
|
|
}
|
|
|
|
|
2014-06-10 14:29:19 +02:00
|
|
|
assert(0);
|
2017-05-17 06:43:06 +02:00
|
|
|
abort(); /* if NDEBUG is set */
|
2014-05-02 16:34:57 +02:00
|
|
|
}
|
|
|
|
|
2016-08-21 12:33:01 +02:00
|
|
|
uint32_t nghttp2_session_get_local_settings(nghttp2_session *session,
|
|
|
|
nghttp2_settings_id id) {
|
|
|
|
switch (id) {
|
|
|
|
case NGHTTP2_SETTINGS_HEADER_TABLE_SIZE:
|
|
|
|
return session->local_settings.header_table_size;
|
|
|
|
case NGHTTP2_SETTINGS_ENABLE_PUSH:
|
|
|
|
return session->local_settings.enable_push;
|
|
|
|
case NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS:
|
|
|
|
return session->local_settings.max_concurrent_streams;
|
|
|
|
case NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE:
|
|
|
|
return session->local_settings.initial_window_size;
|
|
|
|
case NGHTTP2_SETTINGS_MAX_FRAME_SIZE:
|
|
|
|
return session->local_settings.max_frame_size;
|
|
|
|
case NGHTTP2_SETTINGS_MAX_HEADER_LIST_SIZE:
|
|
|
|
return session->local_settings.max_header_list_size;
|
2018-03-10 09:19:45 +01:00
|
|
|
case NGHTTP2_SETTINGS_ENABLE_CONNECT_PROTOCOL:
|
|
|
|
return session->local_settings.enable_connect_protocol;
|
2016-08-21 12:33:01 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
assert(0);
|
2017-05-17 06:43:06 +02:00
|
|
|
abort(); /* if NDEBUG is set */
|
2016-08-21 12:33:01 +02:00
|
|
|
}
|
|
|
|
|
2015-11-07 03:30:15 +01:00
|
|
|
static int nghttp2_session_upgrade_internal(nghttp2_session *session,
|
|
|
|
const uint8_t *settings_payload,
|
|
|
|
size_t settings_payloadlen,
|
|
|
|
void *stream_user_data) {
|
2013-08-03 11:05:14 +02:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
nghttp2_frame frame;
|
|
|
|
nghttp2_settings_entry *iv;
|
|
|
|
size_t niv;
|
|
|
|
int rv;
|
2014-03-25 18:04:24 +01:00
|
|
|
nghttp2_priority_spec pri_spec;
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem *mem;
|
|
|
|
|
|
|
|
mem = &session->mem;
|
2013-08-03 11:05:14 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if ((!session->server && session->next_stream_id != 1) ||
|
|
|
|
(session->server && session->last_recv_stream_id >= 1)) {
|
2013-08-03 11:05:14 +02:00
|
|
|
return NGHTTP2_ERR_PROTO;
|
|
|
|
}
|
2014-11-27 15:39:04 +01:00
|
|
|
if (settings_payloadlen % NGHTTP2_FRAME_SETTINGS_ENTRY_LENGTH) {
|
2013-08-03 11:05:14 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
2014-01-26 08:46:18 +01:00
|
|
|
rv = nghttp2_frame_unpack_settings_payload2(&iv, &niv, settings_payload,
|
2014-12-07 15:07:13 +01:00
|
|
|
settings_payloadlen, mem);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-08-03 11:05:14 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-04-04 13:23:46 +02:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->server) {
|
2014-11-15 03:30:01 +01:00
|
|
|
nghttp2_frame_hd_init(&frame.hd, settings_payloadlen, NGHTTP2_SETTINGS,
|
|
|
|
NGHTTP2_FLAG_NONE, 0);
|
2013-08-03 11:05:14 +02:00
|
|
|
frame.settings.iv = iv;
|
|
|
|
frame.settings.niv = niv;
|
2013-10-27 11:22:51 +01:00
|
|
|
rv = nghttp2_session_on_settings_received(session, &frame, 1 /* No ACK */);
|
2013-08-03 11:05:14 +02:00
|
|
|
} else {
|
2013-10-25 15:50:24 +02:00
|
|
|
rv = nghttp2_submit_settings(session, NGHTTP2_FLAG_NONE, iv, niv);
|
2013-08-03 11:05:14 +02:00
|
|
|
}
|
2014-12-07 15:07:13 +01:00
|
|
|
nghttp2_mem_free(mem, iv);
|
2014-11-27 15:39:04 +01:00
|
|
|
if (rv != 0) {
|
2013-08-03 11:05:14 +02:00
|
|
|
return rv;
|
|
|
|
}
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-04-14 16:53:54 +02:00
|
|
|
nghttp2_priority_spec_default_init(&pri_spec);
|
2014-03-25 18:04:24 +01:00
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
stream = nghttp2_session_open_stream(
|
|
|
|
session, 1, NGHTTP2_STREAM_FLAG_NONE, &pri_spec, NGHTTP2_STREAM_OPENING,
|
|
|
|
session->server ? NULL : stream_user_data);
|
|
|
|
if (stream == NULL) {
|
2013-08-03 11:05:14 +02:00
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
2015-12-23 08:38:30 +01:00
|
|
|
|
|
|
|
/* We don't call nghttp2_session_adjust_closed_stream(), since this
|
|
|
|
should be the first stream open. */
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (session->server) {
|
2013-08-03 11:05:14 +02:00
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_RD);
|
|
|
|
session->last_recv_stream_id = 1;
|
2013-11-28 15:26:34 +01:00
|
|
|
session->last_proc_stream_id = 1;
|
2013-08-03 11:05:14 +02:00
|
|
|
} else {
|
|
|
|
nghttp2_stream_shutdown(stream, NGHTTP2_SHUT_WR);
|
2016-01-23 06:47:39 +01:00
|
|
|
session->last_sent_stream_id = 1;
|
2013-08-03 11:05:14 +02:00
|
|
|
session->next_stream_id += 2;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-03 14:44:29 +02:00
|
|
|
|
2015-11-07 03:30:15 +01:00
|
|
|
int nghttp2_session_upgrade(nghttp2_session *session,
|
|
|
|
const uint8_t *settings_payload,
|
|
|
|
size_t settings_payloadlen,
|
|
|
|
void *stream_user_data) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
rv = nghttp2_session_upgrade_internal(session, settings_payload,
|
|
|
|
settings_payloadlen, stream_user_data);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, 1);
|
|
|
|
assert(stream);
|
|
|
|
|
2015-11-07 02:56:40 +01:00
|
|
|
/* We have no information about request header fields when Upgrade
|
|
|
|
was happened. So we don't know the request method here. If
|
|
|
|
request method is HEAD, we have a trouble because we may have
|
|
|
|
nonzero content-length header field in response headers, and we
|
|
|
|
will going to check it against the actual DATA frames, but we may
|
2015-11-07 03:30:15 +01:00
|
|
|
get mismatch because HEAD response body must be empty. Because
|
|
|
|
of this reason, nghttp2_session_upgrade() was deprecated in favor
|
|
|
|
of nghttp2_session_upgrade2(), which has |head_request| parameter
|
|
|
|
to indicate that request method is HEAD or not. */
|
2015-11-07 02:56:40 +01:00
|
|
|
stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_UPGRADE_WORKAROUND;
|
2013-08-03 11:05:14 +02:00
|
|
|
return 0;
|
|
|
|
}
|
2014-07-03 14:44:29 +02:00
|
|
|
|
2015-11-07 03:30:15 +01:00
|
|
|
int nghttp2_session_upgrade2(nghttp2_session *session,
|
|
|
|
const uint8_t *settings_payload,
|
|
|
|
size_t settings_payloadlen, int head_request,
|
|
|
|
void *stream_user_data) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
rv = nghttp2_session_upgrade_internal(session, settings_payload,
|
|
|
|
settings_payloadlen, stream_user_data);
|
|
|
|
if (rv != 0) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, 1);
|
|
|
|
assert(stream);
|
|
|
|
|
|
|
|
if (head_request) {
|
|
|
|
stream->http_flags |= NGHTTP2_HTTP_FLAG_METH_HEAD;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_get_stream_local_close(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2014-07-03 14:44:29 +02:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-07-03 14:44:29 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (stream->shut_flags & NGHTTP2_SHUT_WR) != 0;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
int nghttp2_session_get_stream_remote_close(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2014-07-03 14:44:29 +02:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!stream) {
|
2014-07-03 14:44:29 +02:00
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
return (stream->shut_flags & NGHTTP2_SHUT_RD) != 0;
|
|
|
|
}
|
2014-07-25 14:26:03 +02:00
|
|
|
|
|
|
|
int nghttp2_session_consume(nghttp2_session *session, int32_t stream_id,
|
2014-11-27 15:39:04 +01:00
|
|
|
size_t size) {
|
2014-07-25 14:26:03 +02:00
|
|
|
int rv;
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (stream_id == 0) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return NGHTTP2_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = session_update_connection_consumed_size(session, size);
|
|
|
|
|
2014-11-27 15:39:04 +01:00
|
|
|
if (nghttp2_is_fatal(rv)) {
|
2014-07-25 14:26:03 +02:00
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
|
2015-04-06 17:14:18 +02:00
|
|
|
if (!stream) {
|
|
|
|
return 0;
|
|
|
|
}
|
2014-07-25 14:26:03 +02:00
|
|
|
|
2015-04-06 17:14:18 +02:00
|
|
|
rv = session_update_stream_consumed_size(session, stream, size);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_session_consume_connection(nghttp2_session *session, size_t size) {
|
|
|
|
int rv;
|
|
|
|
|
|
|
|
if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
|
|
|
|
return NGHTTP2_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = session_update_connection_consumed_size(session, size);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int nghttp2_session_consume_stream(nghttp2_session *session, int32_t stream_id,
|
|
|
|
size_t size) {
|
|
|
|
int rv;
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
|
|
|
|
if (stream_id == 0) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (!(session->opt_flags & NGHTTP2_OPTMASK_NO_AUTO_WINDOW_UPDATE)) {
|
|
|
|
return NGHTTP2_ERR_INVALID_STATE;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream(session, stream_id);
|
|
|
|
|
|
|
|
if (!stream) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
rv = session_update_stream_consumed_size(session, stream, size);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
2014-07-25 14:26:03 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2014-12-12 17:18:15 +01:00
|
|
|
|
|
|
|
int nghttp2_session_set_next_stream_id(nghttp2_session *session,
|
|
|
|
int32_t next_stream_id) {
|
2015-04-16 17:12:47 +02:00
|
|
|
if (next_stream_id <= 0 ||
|
2014-12-12 17:18:15 +01:00
|
|
|
session->next_stream_id > (uint32_t)next_stream_id) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2015-04-16 17:12:47 +02:00
|
|
|
if (session->server) {
|
|
|
|
if (next_stream_id % 2) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
} else if (next_stream_id % 2 == 0) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
2015-09-23 07:41:53 +02:00
|
|
|
session->next_stream_id = (uint32_t)next_stream_id;
|
2014-12-12 17:18:15 +01:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t nghttp2_session_get_next_stream_id(nghttp2_session *session) {
|
|
|
|
return session->next_stream_id;
|
|
|
|
}
|
2015-01-21 17:43:56 +01:00
|
|
|
|
|
|
|
int32_t nghttp2_session_get_last_proc_stream_id(nghttp2_session *session) {
|
|
|
|
return session->last_proc_stream_id;
|
|
|
|
}
|
2015-08-18 15:41:50 +02:00
|
|
|
|
|
|
|
nghttp2_stream *nghttp2_session_find_stream(nghttp2_session *session,
|
|
|
|
int32_t stream_id) {
|
2015-11-24 14:30:12 +01:00
|
|
|
if (stream_id == 0) {
|
|
|
|
return &session->root;
|
|
|
|
}
|
|
|
|
|
2015-08-18 15:41:50 +02:00
|
|
|
return nghttp2_session_get_stream_raw(session, stream_id);
|
|
|
|
}
|
|
|
|
|
|
|
|
nghttp2_stream *nghttp2_session_get_root_stream(nghttp2_session *session) {
|
|
|
|
return &session->root;
|
|
|
|
}
|
2015-11-21 07:07:55 +01:00
|
|
|
|
|
|
|
int nghttp2_session_check_server_session(nghttp2_session *session) {
|
|
|
|
return session->server;
|
|
|
|
}
|
2015-11-21 10:22:28 +01:00
|
|
|
|
|
|
|
int nghttp2_session_change_stream_priority(
|
|
|
|
nghttp2_session *session, int32_t stream_id,
|
|
|
|
const nghttp2_priority_spec *pri_spec) {
|
2015-12-23 08:38:30 +01:00
|
|
|
int rv;
|
2015-11-21 10:22:28 +01:00
|
|
|
nghttp2_stream *stream;
|
|
|
|
nghttp2_priority_spec pri_spec_copy;
|
|
|
|
|
2015-11-24 14:33:06 +01:00
|
|
|
if (stream_id == 0 || stream_id == pri_spec->stream_id) {
|
2015-11-21 10:22:28 +01:00
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream_raw(session, stream_id);
|
|
|
|
if (!stream) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
pri_spec_copy = *pri_spec;
|
|
|
|
nghttp2_priority_spec_normalize_weight(&pri_spec_copy);
|
|
|
|
|
2015-12-23 08:38:30 +01:00
|
|
|
rv = nghttp2_session_reprioritize_stream(session, stream, &pri_spec_copy);
|
|
|
|
|
|
|
|
if (nghttp2_is_fatal(rv)) {
|
|
|
|
return rv;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* We don't intentionally call nghttp2_session_adjust_idle_stream()
|
|
|
|
so that idle stream created by this function, and existing ones
|
|
|
|
are kept for application. We will adjust number of idle stream
|
|
|
|
in nghttp2_session_mem_send or nghttp2_session_mem_recv is
|
|
|
|
called. */
|
|
|
|
return 0;
|
2015-11-21 10:22:28 +01:00
|
|
|
}
|
2015-11-28 07:04:39 +01:00
|
|
|
|
|
|
|
int nghttp2_session_create_idle_stream(nghttp2_session *session,
|
|
|
|
int32_t stream_id,
|
|
|
|
const nghttp2_priority_spec *pri_spec) {
|
|
|
|
nghttp2_stream *stream;
|
|
|
|
nghttp2_priority_spec pri_spec_copy;
|
|
|
|
|
|
|
|
if (stream_id == 0 || stream_id == pri_spec->stream_id ||
|
|
|
|
!session_detect_idle_stream(session, stream_id)) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
stream = nghttp2_session_get_stream_raw(session, stream_id);
|
|
|
|
if (stream) {
|
|
|
|
return NGHTTP2_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
|
|
|
|
pri_spec_copy = *pri_spec;
|
|
|
|
nghttp2_priority_spec_normalize_weight(&pri_spec_copy);
|
|
|
|
|
|
|
|
stream =
|
|
|
|
nghttp2_session_open_stream(session, stream_id, NGHTTP2_STREAM_FLAG_NONE,
|
|
|
|
&pri_spec_copy, NGHTTP2_STREAM_IDLE, NULL);
|
|
|
|
if (!stream) {
|
|
|
|
return NGHTTP2_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
|
2015-12-23 08:38:30 +01:00
|
|
|
/* We don't intentionally call nghttp2_session_adjust_idle_stream()
|
|
|
|
so that idle stream created by this function, and existing ones
|
|
|
|
are kept for application. We will adjust number of idle stream
|
|
|
|
in nghttp2_session_mem_send or nghttp2_session_mem_recv is
|
|
|
|
called. */
|
2015-11-28 07:04:39 +01:00
|
|
|
return 0;
|
|
|
|
}
|
2016-08-26 16:02:51 +02:00
|
|
|
|
|
|
|
size_t
|
|
|
|
nghttp2_session_get_hd_inflate_dynamic_table_size(nghttp2_session *session) {
|
|
|
|
return nghttp2_hd_inflate_get_dynamic_table_size(&session->hd_inflater);
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t
|
|
|
|
nghttp2_session_get_hd_deflate_dynamic_table_size(nghttp2_session *session) {
|
|
|
|
return nghttp2_hd_deflate_get_dynamic_table_size(&session->hd_deflater);
|
|
|
|
}
|
2018-02-22 13:02:16 +01:00
|
|
|
|
|
|
|
void nghttp2_session_set_user_data(nghttp2_session *session, void *user_data) {
|
|
|
|
session->user_data = user_data;
|
|
|
|
}
|