2012-01-24 08:02:24 -05:00
|
|
|
/*
|
|
|
|
* Spdylay - SPDY Library
|
|
|
|
*
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
#include "spdylay_session.h"
|
|
|
|
|
|
|
|
#include <string.h>
|
|
|
|
#include <stddef.h>
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <arpa/inet.h>
|
|
|
|
|
|
|
|
#include "spdylay_helper.h"
|
|
|
|
|
2012-02-08 09:45:48 -05:00
|
|
|
/*
|
|
|
|
* Returns non-zero if the number of opened streams is larger than or
|
|
|
|
* equal to SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS value.
|
|
|
|
*/
|
|
|
|
static int spdylay_session_get_max_concurrent_streams_reached
|
|
|
|
(spdylay_session *session)
|
|
|
|
{
|
2012-03-09 10:10:11 -05:00
|
|
|
uint32_t local_max, remote_max;
|
|
|
|
local_max = session->local_settings[SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS];
|
|
|
|
remote_max =
|
|
|
|
session->remote_settings[SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS];
|
|
|
|
return spdylay_min(local_max, remote_max)
|
2012-02-08 09:45:48 -05:00
|
|
|
<= spdylay_map_size(&session->streams);
|
|
|
|
}
|
|
|
|
|
2012-03-07 10:18:18 -05:00
|
|
|
/*
|
|
|
|
* Returns non-zero if |error| is non-fatal error.
|
|
|
|
*/
|
|
|
|
static int spdylay_is_non_fatal(int error)
|
|
|
|
{
|
|
|
|
return error < 0 && error > SPDYLAY_ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-03-07 10:40:17 -05:00
|
|
|
/*
|
|
|
|
* Returns non-zero if |error| is fatal error.
|
|
|
|
*/
|
|
|
|
static int spdylay_is_fatal(int error)
|
|
|
|
{
|
|
|
|
return error < SPDYLAY_ERR_FATAL;
|
|
|
|
}
|
|
|
|
|
2012-03-11 06:27:33 -04:00
|
|
|
/*
|
|
|
|
* This function should be called when the session wants to drop
|
|
|
|
* connection after sending GOAWAY. These cases are called as the
|
|
|
|
* session error. For example, when it receives bad zlib data.
|
|
|
|
*/
|
|
|
|
static int spdylay_session_fail_session(spdylay_session *session,
|
|
|
|
uint32_t status_code)
|
|
|
|
{
|
|
|
|
session->goaway_flags |= SPDYLAY_GOAWAY_FAIL_ON_SEND;
|
|
|
|
return spdylay_submit_goaway(session, status_code);
|
|
|
|
}
|
|
|
|
|
2012-02-02 09:19:01 -05:00
|
|
|
int spdylay_session_is_my_stream_id(spdylay_session *session,
|
|
|
|
int32_t stream_id)
|
2012-01-26 11:17:40 -05:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
if(stream_id == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
r = stream_id % 2;
|
2012-02-02 09:19:01 -05:00
|
|
|
return (session->server && r == 0) || (!session->server && r == 1);
|
2012-01-26 11:17:40 -05:00
|
|
|
}
|
|
|
|
|
2012-01-25 11:04:01 -05:00
|
|
|
spdylay_stream* spdylay_session_get_stream(spdylay_session *session,
|
|
|
|
int32_t stream_id)
|
2012-01-25 09:46:07 -05:00
|
|
|
{
|
|
|
|
return (spdylay_stream*)spdylay_map_find(&session->streams, stream_id);
|
|
|
|
}
|
|
|
|
|
2012-02-12 10:48:03 -05:00
|
|
|
static int spdylay_outbound_item_compar(const void *lhsx, const void *rhsx)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
|
|
|
const spdylay_outbound_item *lhs, *rhs;
|
|
|
|
lhs = (const spdylay_outbound_item*)lhsx;
|
|
|
|
rhs = (const spdylay_outbound_item*)rhsx;
|
2012-01-31 08:48:09 -05:00
|
|
|
if(lhs->pri == rhs->pri) {
|
|
|
|
return (lhs->seq < rhs->seq) ? -1 : ((lhs->seq > rhs->seq) ? 1 : 0);
|
|
|
|
} else {
|
|
|
|
return lhs->pri-rhs->pri;
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
|
2012-03-17 10:39:38 -04:00
|
|
|
static void spdylay_inbound_frame_reset(spdylay_inbound_frame *iframe)
|
|
|
|
{
|
|
|
|
iframe->state = SPDYLAY_RECV_HEAD;
|
|
|
|
iframe->len = iframe->off = 0;
|
|
|
|
iframe->headbufoff = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-01 10:19:31 -05:00
|
|
|
static int spdylay_session_new(spdylay_session **session_ptr,
|
2012-02-25 10:12:32 -05:00
|
|
|
uint16_t version,
|
2012-01-24 08:56:26 -05:00
|
|
|
const spdylay_session_callbacks *callbacks,
|
|
|
|
void *user_data)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
|
|
|
int r;
|
2012-02-25 10:12:32 -05:00
|
|
|
if(version != SPDYLAY_PROTO_SPDY2 && version != SPDYLAY_PROTO_SPDY3) {
|
|
|
|
return SPDYLAY_ERR_UNSUPPORTED_VERSION;
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
*session_ptr = malloc(sizeof(spdylay_session));
|
|
|
|
if(*session_ptr == NULL) {
|
2012-02-24 11:41:02 -05:00
|
|
|
r = SPDYLAY_ERR_NOMEM;
|
2012-02-16 08:47:49 -05:00
|
|
|
goto fail_session;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
memset(*session_ptr, 0, sizeof(spdylay_session));
|
2012-01-27 09:05:29 -05:00
|
|
|
|
2012-02-25 10:12:32 -05:00
|
|
|
(*session_ptr)->version = version;
|
|
|
|
|
2012-02-01 10:19:31 -05:00
|
|
|
/* next_stream_id, last_recv_stream_id and next_unique_id are
|
|
|
|
initialized in either spdylay_session_client_new or
|
|
|
|
spdylay_session_server_new */
|
2012-01-27 09:05:29 -05:00
|
|
|
|
2012-02-25 10:12:32 -05:00
|
|
|
(*session_ptr)->flow_control =
|
|
|
|
(*session_ptr)->version == SPDYLAY_PROTO_SPDY3;
|
|
|
|
|
2012-01-27 09:05:29 -05:00
|
|
|
(*session_ptr)->last_ping_unique_id = 0;
|
2012-01-24 08:02:24 -05:00
|
|
|
|
2012-01-31 08:48:09 -05:00
|
|
|
(*session_ptr)->next_seq = 0;
|
|
|
|
|
2012-01-28 05:22:38 -05:00
|
|
|
(*session_ptr)->goaway_flags = SPDYLAY_GOAWAY_NONE;
|
|
|
|
(*session_ptr)->last_good_stream_id = 0;
|
|
|
|
|
2012-02-24 11:17:03 -05:00
|
|
|
r = spdylay_zlib_deflate_hd_init(&(*session_ptr)->hd_deflater,
|
|
|
|
(*session_ptr)->version);
|
2012-01-24 08:02:24 -05:00
|
|
|
if(r != 0) {
|
2012-02-16 08:47:49 -05:00
|
|
|
goto fail_hd_deflater;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-02-24 11:17:03 -05:00
|
|
|
r = spdylay_zlib_inflate_hd_init(&(*session_ptr)->hd_inflater,
|
|
|
|
(*session_ptr)->version);
|
2012-01-24 08:02:24 -05:00
|
|
|
if(r != 0) {
|
2012-02-16 08:47:49 -05:00
|
|
|
goto fail_hd_inflater;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-02-21 09:23:47 -05:00
|
|
|
spdylay_map_init(&(*session_ptr)->streams);
|
2012-01-24 08:02:24 -05:00
|
|
|
r = spdylay_pq_init(&(*session_ptr)->ob_pq, spdylay_outbound_item_compar);
|
|
|
|
if(r != 0) {
|
2012-02-16 08:47:49 -05:00
|
|
|
goto fail_ob_pq;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-02-05 10:14:19 -05:00
|
|
|
r = spdylay_pq_init(&(*session_ptr)->ob_ss_pq, spdylay_outbound_item_compar);
|
|
|
|
if(r != 0) {
|
2012-02-16 08:47:49 -05:00
|
|
|
goto fail_ob_ss_pq;
|
2012-02-05 10:14:19 -05:00
|
|
|
}
|
|
|
|
|
2012-02-16 09:09:06 -05:00
|
|
|
(*session_ptr)->aob.framebuf = malloc
|
|
|
|
(SPDYLAY_INITIAL_OUTBOUND_FRAMEBUF_LENGTH);
|
2012-02-16 06:54:30 -05:00
|
|
|
if((*session_ptr)->aob.framebuf == NULL) {
|
2012-02-24 11:41:02 -05:00
|
|
|
r = SPDYLAY_ERR_NOMEM;
|
2012-02-16 08:47:49 -05:00
|
|
|
goto fail_aob_framebuf;
|
2012-02-16 06:54:30 -05:00
|
|
|
}
|
2012-02-16 09:09:06 -05:00
|
|
|
(*session_ptr)->aob.framebufmax = SPDYLAY_INITIAL_OUTBOUND_FRAMEBUF_LENGTH;
|
2012-02-16 06:54:30 -05:00
|
|
|
|
|
|
|
(*session_ptr)->nvbuf = malloc(SPDYLAY_INITIAL_NV_BUFFER_LENGTH);
|
|
|
|
if((*session_ptr)->nvbuf == NULL) {
|
2012-02-24 11:41:02 -05:00
|
|
|
r = SPDYLAY_ERR_NOMEM;
|
2012-02-16 08:47:49 -05:00
|
|
|
goto fail_nvbuf;
|
2012-02-16 06:54:30 -05:00
|
|
|
}
|
|
|
|
(*session_ptr)->nvbuflen = SPDYLAY_INITIAL_NV_BUFFER_LENGTH;
|
|
|
|
|
2012-02-16 08:01:34 -05:00
|
|
|
spdylay_buffer_init(&(*session_ptr)->inflatebuf, 4096);
|
|
|
|
|
2012-03-09 10:10:11 -05:00
|
|
|
memset((*session_ptr)->remote_settings, 0,
|
|
|
|
sizeof((*session_ptr)->remote_settings));
|
|
|
|
(*session_ptr)->remote_settings[SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS] =
|
2012-03-10 04:49:25 -05:00
|
|
|
SPDYLAY_INITIAL_MAX_CONCURRENT_STREAMS;
|
2012-03-09 10:10:11 -05:00
|
|
|
(*session_ptr)->remote_settings[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE] =
|
|
|
|
SPDYLAY_INITIAL_WINDOW_SIZE;
|
2012-02-05 10:14:19 -05:00
|
|
|
|
2012-03-09 10:10:11 -05:00
|
|
|
memset((*session_ptr)->local_settings, 0,
|
|
|
|
sizeof((*session_ptr)->local_settings));
|
|
|
|
(*session_ptr)->local_settings[SPDYLAY_SETTINGS_MAX_CONCURRENT_STREAMS] =
|
2012-03-10 04:49:25 -05:00
|
|
|
SPDYLAY_INITIAL_MAX_CONCURRENT_STREAMS;
|
2012-03-09 10:10:11 -05:00
|
|
|
(*session_ptr)->local_settings[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE] =
|
2012-02-25 10:12:32 -05:00
|
|
|
SPDYLAY_INITIAL_WINDOW_SIZE;
|
|
|
|
|
2012-01-24 08:02:24 -05:00
|
|
|
(*session_ptr)->callbacks = *callbacks;
|
|
|
|
(*session_ptr)->user_data = user_data;
|
|
|
|
|
2012-02-16 09:09:06 -05:00
|
|
|
(*session_ptr)->iframe.buf = malloc(SPDYLAY_INITIAL_INBOUND_FRAMEBUF_LENGTH);
|
|
|
|
if((*session_ptr)->iframe.buf == NULL) {
|
2012-02-24 11:41:02 -05:00
|
|
|
r = SPDYLAY_ERR_NOMEM;
|
2012-02-16 09:09:06 -05:00
|
|
|
goto fail_iframe_buf;
|
|
|
|
}
|
|
|
|
(*session_ptr)->iframe.bufmax = SPDYLAY_INITIAL_INBOUND_FRAMEBUF_LENGTH;
|
2012-03-17 10:39:38 -04:00
|
|
|
spdylay_inbound_frame_reset(&(*session_ptr)->iframe);
|
2012-02-16 09:09:06 -05:00
|
|
|
|
2012-01-24 08:02:24 -05:00
|
|
|
return 0;
|
2012-02-16 08:47:49 -05:00
|
|
|
|
2012-02-16 09:09:06 -05:00
|
|
|
fail_iframe_buf:
|
|
|
|
free((*session_ptr)->nvbuf);
|
2012-02-16 08:47:49 -05:00
|
|
|
fail_nvbuf:
|
|
|
|
free((*session_ptr)->aob.framebuf);
|
|
|
|
fail_aob_framebuf:
|
|
|
|
spdylay_pq_free(&(*session_ptr)->ob_ss_pq);
|
|
|
|
fail_ob_ss_pq:
|
|
|
|
spdylay_pq_free(&(*session_ptr)->ob_pq);
|
|
|
|
fail_ob_pq:
|
|
|
|
spdylay_map_free(&(*session_ptr)->streams);
|
|
|
|
spdylay_zlib_inflate_free(&(*session_ptr)->hd_inflater);
|
|
|
|
fail_hd_inflater:
|
|
|
|
spdylay_zlib_deflate_free(&(*session_ptr)->hd_deflater);
|
|
|
|
fail_hd_deflater:
|
|
|
|
free(*session_ptr);
|
|
|
|
fail_session:
|
2012-02-24 11:17:03 -05:00
|
|
|
return r;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
|
2012-02-01 10:19:31 -05:00
|
|
|
int spdylay_session_client_new(spdylay_session **session_ptr,
|
2012-02-25 10:12:32 -05:00
|
|
|
uint16_t version,
|
2012-02-01 10:19:31 -05:00
|
|
|
const spdylay_session_callbacks *callbacks,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
int r;
|
2012-02-25 10:12:32 -05:00
|
|
|
r = spdylay_session_new(session_ptr, version, callbacks, user_data);
|
2012-02-01 10:19:31 -05:00
|
|
|
if(r == 0) {
|
|
|
|
/* IDs for use in client */
|
|
|
|
(*session_ptr)->next_stream_id = 1;
|
|
|
|
(*session_ptr)->last_recv_stream_id = 0;
|
|
|
|
(*session_ptr)->next_unique_id = 1;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spdylay_session_server_new(spdylay_session **session_ptr,
|
2012-02-25 10:12:32 -05:00
|
|
|
uint16_t version,
|
2012-02-01 10:19:31 -05:00
|
|
|
const spdylay_session_callbacks *callbacks,
|
|
|
|
void *user_data)
|
|
|
|
{
|
|
|
|
int r;
|
2012-02-25 10:12:32 -05:00
|
|
|
r = spdylay_session_new(session_ptr, version, callbacks, user_data);
|
2012-02-01 10:19:31 -05:00
|
|
|
if(r == 0) {
|
|
|
|
(*session_ptr)->server = 1;
|
|
|
|
/* IDs for use in client */
|
|
|
|
(*session_ptr)->next_stream_id = 2;
|
|
|
|
(*session_ptr)->last_recv_stream_id = 0;
|
|
|
|
(*session_ptr)->next_unique_id = 2;
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-03-09 10:10:11 -05:00
|
|
|
static void spdylay_free_streams(key_type key, void *val, void *ptr)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
|
|
|
spdylay_stream_free((spdylay_stream*)val);
|
|
|
|
free(val);
|
|
|
|
}
|
|
|
|
|
2012-02-05 10:14:19 -05:00
|
|
|
static void spdylay_session_ob_pq_free(spdylay_pq *pq)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
2012-02-05 10:14:19 -05:00
|
|
|
while(!spdylay_pq_empty(pq)) {
|
|
|
|
spdylay_outbound_item *item = (spdylay_outbound_item*)spdylay_pq_top(pq);
|
2012-01-24 08:02:24 -05:00
|
|
|
spdylay_outbound_item_free(item);
|
|
|
|
free(item);
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_pq_pop(pq);
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_pq_free(pq);
|
|
|
|
}
|
|
|
|
|
2012-02-19 09:42:25 -05:00
|
|
|
static void spdylay_active_outbound_item_reset
|
|
|
|
(spdylay_active_outbound_item *aob)
|
|
|
|
{
|
|
|
|
spdylay_outbound_item_free(aob->item);
|
|
|
|
free(aob->item);
|
|
|
|
aob->item = NULL;
|
|
|
|
aob->framebuflen = aob->framebufoff = 0;
|
|
|
|
}
|
|
|
|
|
2012-02-05 10:14:19 -05:00
|
|
|
void spdylay_session_del(spdylay_session *session)
|
|
|
|
{
|
|
|
|
if(session == NULL) {
|
|
|
|
return;
|
|
|
|
}
|
2012-03-09 10:10:11 -05:00
|
|
|
spdylay_map_each(&session->streams, spdylay_free_streams, NULL);
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_map_free(&session->streams);
|
|
|
|
spdylay_session_ob_pq_free(&session->ob_pq);
|
|
|
|
spdylay_session_ob_pq_free(&session->ob_ss_pq);
|
2012-01-24 08:02:24 -05:00
|
|
|
spdylay_zlib_deflate_free(&session->hd_deflater);
|
|
|
|
spdylay_zlib_inflate_free(&session->hd_inflater);
|
2012-02-19 09:42:25 -05:00
|
|
|
spdylay_active_outbound_item_reset(&session->aob);
|
2012-02-16 06:54:30 -05:00
|
|
|
free(session->aob.framebuf);
|
|
|
|
free(session->nvbuf);
|
2012-02-16 08:01:34 -05:00
|
|
|
spdylay_buffer_free(&session->inflatebuf);
|
2012-02-16 09:09:06 -05:00
|
|
|
free(session->iframe.buf);
|
2012-01-24 08:02:24 -05:00
|
|
|
free(session);
|
|
|
|
}
|
|
|
|
|
|
|
|
int spdylay_session_add_frame(spdylay_session *session,
|
|
|
|
spdylay_frame_type frame_type,
|
2012-01-29 05:07:31 -05:00
|
|
|
spdylay_frame *frame,
|
|
|
|
void *aux_data)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
spdylay_outbound_item *item;
|
|
|
|
item = malloc(sizeof(spdylay_outbound_item));
|
|
|
|
if(item == NULL) {
|
|
|
|
return SPDYLAY_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
item->frame_type = frame_type;
|
|
|
|
item->frame = frame;
|
2012-01-29 05:07:31 -05:00
|
|
|
item->aux_data = aux_data;
|
2012-01-31 08:48:09 -05:00
|
|
|
item->seq = session->next_seq++;
|
2012-01-25 09:52:32 -05:00
|
|
|
/* Set priority lowest at the moment. */
|
2012-02-25 10:12:32 -05:00
|
|
|
item->pri = spdylay_session_get_pri_lowest(session);
|
2012-01-24 08:02:24 -05:00
|
|
|
switch(frame_type) {
|
|
|
|
case SPDYLAY_SYN_STREAM:
|
2012-01-25 09:52:32 -05:00
|
|
|
item->pri = frame->syn_stream.pri;
|
2012-01-24 08:02:24 -05:00
|
|
|
break;
|
2012-01-25 09:52:32 -05:00
|
|
|
case SPDYLAY_SYN_REPLY: {
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream
|
|
|
|
(session, frame->syn_reply.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
item->pri = stream->pri;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-25 09:46:07 -05:00
|
|
|
case SPDYLAY_RST_STREAM: {
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream
|
|
|
|
(session, frame->rst_stream.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
stream->state = SPDYLAY_STREAM_CLOSING;
|
|
|
|
item->pri = stream->pri;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-31 11:12:26 -05:00
|
|
|
case SPDYLAY_SETTINGS:
|
|
|
|
/* Should SPDYLAY_SETTINGS have higher priority? */
|
2012-03-10 04:41:01 -05:00
|
|
|
item->pri = -1;
|
2012-01-31 11:12:26 -05:00
|
|
|
break;
|
2012-01-28 03:29:22 -05:00
|
|
|
case SPDYLAY_NOOP:
|
|
|
|
/* We don't have any public API to add NOOP, so here is
|
|
|
|
unreachable. */
|
|
|
|
abort();
|
2012-01-27 09:05:29 -05:00
|
|
|
case SPDYLAY_PING:
|
|
|
|
/* Ping has "height" priority. Give it -1. */
|
|
|
|
item->pri = -1;
|
|
|
|
break;
|
2012-01-28 05:22:38 -05:00
|
|
|
case SPDYLAY_GOAWAY:
|
|
|
|
/* Should GOAWAY have higher priority? */
|
|
|
|
break;
|
2012-02-23 10:02:29 -05:00
|
|
|
case SPDYLAY_HEADERS: {
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream
|
|
|
|
(session, frame->headers.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
item->pri = stream->pri;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-02-25 10:12:32 -05:00
|
|
|
case SPDYLAY_WINDOW_UPDATE: {
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream
|
|
|
|
(session, frame->window_update.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
item->pri = stream->pri;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-26 11:17:40 -05:00
|
|
|
case SPDYLAY_DATA: {
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream
|
|
|
|
(session, frame->data.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
item->pri = stream->pri;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
};
|
2012-02-05 10:14:19 -05:00
|
|
|
if(frame_type == SPDYLAY_SYN_STREAM) {
|
|
|
|
r = spdylay_pq_push(&session->ob_ss_pq, item);
|
|
|
|
} else {
|
|
|
|
r = spdylay_pq_push(&session->ob_pq, item);
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
if(r != 0) {
|
|
|
|
free(item);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-25 07:31:28 -05:00
|
|
|
int spdylay_session_add_rst_stream(spdylay_session *session,
|
|
|
|
int32_t stream_id, uint32_t status_code)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
spdylay_frame *frame;
|
|
|
|
frame = malloc(sizeof(spdylay_frame));
|
|
|
|
if(frame == NULL) {
|
|
|
|
return SPDYLAY_ERR_NOMEM;
|
|
|
|
}
|
2012-02-24 09:33:06 -05:00
|
|
|
spdylay_frame_rst_stream_init(&frame->rst_stream, session->version,
|
|
|
|
stream_id, status_code);
|
2012-01-29 05:07:31 -05:00
|
|
|
r = spdylay_session_add_frame(session, SPDYLAY_RST_STREAM, frame, NULL);
|
2012-01-25 07:31:28 -05:00
|
|
|
if(r != 0) {
|
|
|
|
spdylay_frame_rst_stream_free(&frame->rst_stream);
|
|
|
|
free(frame);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-28 10:16:51 -05:00
|
|
|
spdylay_stream* spdylay_session_open_stream(spdylay_session *session,
|
|
|
|
int32_t stream_id,
|
|
|
|
uint8_t flags, uint8_t pri,
|
2012-02-03 11:37:21 -05:00
|
|
|
spdylay_stream_state initial_state,
|
|
|
|
void *stream_user_data)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
spdylay_stream *stream = malloc(sizeof(spdylay_stream));
|
|
|
|
if(stream == NULL) {
|
2012-01-28 10:16:51 -05:00
|
|
|
return NULL;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-02-03 11:37:21 -05:00
|
|
|
spdylay_stream_init(stream, stream_id, flags, pri, initial_state,
|
2012-03-09 10:10:11 -05:00
|
|
|
session->remote_settings
|
|
|
|
[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE],
|
2012-02-03 11:37:21 -05:00
|
|
|
stream_user_data);
|
2012-01-24 08:02:24 -05:00
|
|
|
r = spdylay_map_insert(&session->streams, stream_id, stream);
|
|
|
|
if(r != 0) {
|
|
|
|
free(stream);
|
2012-01-28 10:16:51 -05:00
|
|
|
stream = NULL;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-01-28 10:16:51 -05:00
|
|
|
return stream;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
|
2012-01-29 09:00:33 -05:00
|
|
|
int spdylay_session_close_stream(spdylay_session *session, int32_t stream_id,
|
|
|
|
spdylay_status_code status_code)
|
2012-01-25 11:04:01 -05:00
|
|
|
{
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
|
|
|
|
if(stream) {
|
2012-01-29 09:00:33 -05:00
|
|
|
if(stream->state != SPDYLAY_STREAM_INITIAL &&
|
|
|
|
session->callbacks.on_stream_close_callback) {
|
|
|
|
session->callbacks.on_stream_close_callback(session, stream_id,
|
|
|
|
status_code,
|
|
|
|
session->user_data);
|
|
|
|
}
|
2012-02-07 17:17:38 -05:00
|
|
|
spdylay_map_erase(&session->streams, stream_id);
|
2012-01-25 11:04:01 -05:00
|
|
|
spdylay_stream_free(stream);
|
|
|
|
free(stream);
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return SPDYLAY_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-28 10:08:51 -05:00
|
|
|
int spdylay_session_close_stream_if_shut_rdwr(spdylay_session *session,
|
|
|
|
spdylay_stream *stream)
|
|
|
|
{
|
|
|
|
if((stream->shut_flags & SPDYLAY_SHUT_RDWR) == SPDYLAY_SHUT_RDWR) {
|
2012-01-29 09:00:33 -05:00
|
|
|
return spdylay_session_close_stream(session, stream->stream_id,
|
|
|
|
SPDYLAY_OK);
|
2012-01-28 10:08:51 -05:00
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-02 09:19:01 -05:00
|
|
|
void spdylay_session_close_pushed_streams(spdylay_session *session,
|
|
|
|
int32_t stream_id,
|
|
|
|
spdylay_status_code status_code)
|
|
|
|
{
|
|
|
|
spdylay_stream *stream;
|
|
|
|
stream = spdylay_session_get_stream(session, stream_id);
|
|
|
|
if(stream) {
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < stream->pushed_streams_length; ++i) {
|
|
|
|
spdylay_session_close_stream(session, stream->pushed_streams[i],
|
|
|
|
status_code);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 10:18:18 -05:00
|
|
|
static int spdylay_predicate_stream_for_send(spdylay_stream *stream)
|
|
|
|
{
|
|
|
|
if(stream == NULL) {
|
2012-03-07 10:37:18 -05:00
|
|
|
return SPDYLAY_ERR_STREAM_CLOSED;
|
2012-03-07 10:18:18 -05:00
|
|
|
} else if(stream->shut_flags & SPDYLAY_SHUT_WR) {
|
|
|
|
return SPDYLAY_ERR_STREAM_SHUT_WR;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-11 05:52:42 -04:00
|
|
|
/*
|
|
|
|
* This function checks SYN_STREAM frame |frame| can be sent at this
|
|
|
|
* time.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* SPDYLAY_ERR_STREAM_CLOSED
|
|
|
|
* The Associated-To-Stream is already closed or does not exist.
|
|
|
|
* SPDYLAY_ERR_STREAM_ID_NOT_AVAILABLE
|
2012-03-15 09:39:26 -04:00
|
|
|
* Stream ID has reached the maximum value. Therefore no stream ID
|
|
|
|
* is available.
|
2012-03-11 05:52:42 -04:00
|
|
|
*/
|
|
|
|
static int spdylay_session_predicate_syn_stream_send
|
|
|
|
(spdylay_session *session,
|
|
|
|
spdylay_syn_stream *frame)
|
|
|
|
{
|
|
|
|
if(session->goaway_flags) {
|
|
|
|
/* When GOAWAY is sent or received, peer must not send new
|
|
|
|
SYN_STREAM. */
|
|
|
|
return SPDYLAY_ERR_SYN_STREAM_NOT_ALLOWED;
|
|
|
|
}
|
|
|
|
/* All 32bit signed stream IDs are spent. */
|
|
|
|
if(session->next_stream_id > INT32_MAX) {
|
|
|
|
return SPDYLAY_ERR_STREAM_ID_NOT_AVAILABLE;
|
|
|
|
}
|
|
|
|
if(frame->assoc_stream_id != 0) {
|
|
|
|
/* Check associated stream is active. */
|
|
|
|
/* We assume here that if frame->assoc_stream_id != 0,
|
|
|
|
session->server is always 1 and frame->assoc_stream_id is
|
|
|
|
odd. */
|
|
|
|
if(spdylay_session_get_stream(session, frame->assoc_stream_id) ==
|
|
|
|
NULL) {
|
|
|
|
return SPDYLAY_ERR_STREAM_CLOSED;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-27 03:09:40 -05:00
|
|
|
/*
|
2012-03-07 10:18:18 -05:00
|
|
|
* This function checks SYN_REPLY with the stream ID |stream_id| can
|
|
|
|
* be sent at this time.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2012-03-07 10:37:18 -05:00
|
|
|
* SPDYLAY_ERR_STREAM_CLOSED
|
2012-03-07 10:18:18 -05:00
|
|
|
* The stream is already closed or does not exist.
|
|
|
|
* SPDYLAY_ERR_STREAM_SHUT_WR
|
|
|
|
* The transmission is not allowed for this stream (e.g., a frame
|
|
|
|
* with FIN flag set has already sent)
|
|
|
|
* SPDYLAY_ERR_INVALID_STREAM_ID
|
|
|
|
* The stream ID is invalid.
|
|
|
|
* SPDYLAY_ERR_STREAM_CLOSING
|
|
|
|
* RST_STREAM was queued for this stream.
|
|
|
|
* SPDYLAY_ERR_INVALID_STREAM_STATE
|
|
|
|
* The state of the stream is not valid (e.g., SYN_REPLY has
|
|
|
|
* already sent).
|
2012-01-27 03:09:40 -05:00
|
|
|
*/
|
2012-03-07 10:18:18 -05:00
|
|
|
static int spdylay_session_predicate_syn_reply_send(spdylay_session *session,
|
|
|
|
int32_t stream_id)
|
2012-01-27 03:09:40 -05:00
|
|
|
{
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
|
2012-03-07 10:18:18 -05:00
|
|
|
int r;
|
|
|
|
r = spdylay_predicate_stream_for_send(stream);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-01-27 03:09:40 -05:00
|
|
|
}
|
|
|
|
if(spdylay_session_is_my_stream_id(session, stream_id)) {
|
2012-03-07 10:18:18 -05:00
|
|
|
return SPDYLAY_ERR_INVALID_STREAM_ID;
|
2012-01-27 03:09:40 -05:00
|
|
|
} else {
|
2012-03-07 10:18:18 -05:00
|
|
|
if(stream->state == SPDYLAY_STREAM_OPENING) {
|
|
|
|
return 0;
|
|
|
|
} else if(stream->state == SPDYLAY_STREAM_CLOSING) {
|
|
|
|
return SPDYLAY_ERR_STREAM_CLOSING;
|
|
|
|
} else {
|
|
|
|
return SPDYLAY_ERR_INVALID_STREAM_STATE;
|
|
|
|
}
|
2012-01-27 03:09:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 10:02:29 -05:00
|
|
|
/*
|
2012-03-07 10:18:18 -05:00
|
|
|
* This function checks HEADERS with the stream ID |stream_id| can
|
|
|
|
* be sent at this time.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2012-03-07 10:37:18 -05:00
|
|
|
* SPDYLAY_ERR_STREAM_CLOSED
|
2012-03-07 10:18:18 -05:00
|
|
|
* The stream is already closed or does not exist.
|
|
|
|
* SPDYLAY_ERR_STREAM_SHUT_WR
|
|
|
|
* The transmission is not allowed for this stream (e.g., a frame
|
|
|
|
* with FIN flag set has already sent)
|
|
|
|
* SPDYLAY_ERR_STREAM_CLOSING
|
|
|
|
* RST_STREAM was queued for this stream.
|
|
|
|
* SPDYLAY_ERR_INVALID_STREAM_STATE
|
|
|
|
* The state of the stream is not valid (e.g., if the local peer
|
|
|
|
* is receiving side and SYN_REPLY has not been sent).
|
2012-02-23 10:02:29 -05:00
|
|
|
*/
|
2012-03-07 10:18:18 -05:00
|
|
|
static int spdylay_session_predicate_headers_send(spdylay_session *session,
|
|
|
|
int32_t stream_id)
|
2012-02-23 10:02:29 -05:00
|
|
|
{
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
|
2012-03-07 10:18:18 -05:00
|
|
|
int r;
|
|
|
|
r = spdylay_predicate_stream_for_send(stream);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-02-23 10:02:29 -05:00
|
|
|
}
|
|
|
|
if(spdylay_session_is_my_stream_id(session, stream_id)) {
|
2012-03-07 10:18:18 -05:00
|
|
|
if(stream->state != SPDYLAY_STREAM_CLOSING) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return SPDYLAY_ERR_STREAM_CLOSING;
|
|
|
|
}
|
2012-02-23 10:02:29 -05:00
|
|
|
} else {
|
2012-03-07 10:18:18 -05:00
|
|
|
if(stream->state == SPDYLAY_STREAM_OPENED) {
|
|
|
|
return 0;
|
|
|
|
} else if(stream->state == SPDYLAY_STREAM_CLOSING) {
|
|
|
|
return SPDYLAY_ERR_STREAM_CLOSING;
|
|
|
|
} else {
|
|
|
|
return SPDYLAY_ERR_INVALID_STREAM_STATE;
|
|
|
|
}
|
2012-02-23 10:02:29 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:12:32 -05:00
|
|
|
/*
|
2012-03-07 10:18:18 -05:00
|
|
|
* This function checks WINDOW_UPDATE with the stream ID |stream_id|
|
|
|
|
* can be sent at this time. Note that FIN flag of the previous frame
|
|
|
|
* does not affect the transmission of the WINDOW_UPDATE frame.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2012-03-07 10:37:18 -05:00
|
|
|
* SPDYLAY_ERR_STREAM_CLOSED
|
2012-03-07 10:18:18 -05:00
|
|
|
* The stream is already closed or does not exist.
|
|
|
|
* SPDYLAY_ERR_STREAM_CLOSING
|
|
|
|
* RST_STREAM was queued for this stream.
|
2012-02-25 10:12:32 -05:00
|
|
|
*/
|
2012-03-07 10:18:18 -05:00
|
|
|
static int spdylay_session_predicate_window_update_send
|
|
|
|
(spdylay_session *session,
|
|
|
|
int32_t stream_id)
|
2012-02-25 10:12:32 -05:00
|
|
|
{
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
|
|
|
|
if(stream == NULL) {
|
2012-03-07 10:37:18 -05:00
|
|
|
return SPDYLAY_ERR_STREAM_CLOSED;
|
2012-03-07 10:18:18 -05:00
|
|
|
}
|
|
|
|
if(stream->state != SPDYLAY_STREAM_CLOSING) {
|
2012-02-25 10:12:32 -05:00
|
|
|
return 0;
|
2012-03-07 10:18:18 -05:00
|
|
|
} else {
|
|
|
|
return SPDYLAY_ERR_STREAM_CLOSING;
|
2012-02-25 10:12:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
2012-02-28 07:40:19 -05:00
|
|
|
* Returns the maximum length of next data read. If the flow control
|
|
|
|
* is enabled, the return value takes into account the current window
|
|
|
|
* size.
|
2012-02-25 10:12:32 -05:00
|
|
|
*/
|
2012-02-28 07:40:19 -05:00
|
|
|
static size_t spdylay_session_next_data_read(spdylay_session *session,
|
|
|
|
spdylay_stream *stream)
|
2012-02-25 10:12:32 -05:00
|
|
|
{
|
|
|
|
if(session->flow_control == 0) {
|
|
|
|
return SPDYLAY_DATA_PAYLOAD_LENGTH;
|
2012-02-28 07:40:19 -05:00
|
|
|
} else if(stream->window_size > 0) {
|
|
|
|
return stream->window_size < SPDYLAY_DATA_PAYLOAD_LENGTH ?
|
|
|
|
stream->window_size : SPDYLAY_DATA_PAYLOAD_LENGTH;
|
2012-02-25 10:12:32 -05:00
|
|
|
} else {
|
2012-02-28 07:40:19 -05:00
|
|
|
return 0;
|
2012-02-25 10:12:32 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-03-07 10:18:18 -05:00
|
|
|
/*
|
|
|
|
* This function checks DATA with the stream ID |stream_id| can be
|
|
|
|
* sent at this time.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
2012-03-07 10:37:18 -05:00
|
|
|
* SPDYLAY_ERR_STREAM_CLOSED
|
2012-03-07 10:18:18 -05:00
|
|
|
* The stream is already closed or does not exist.
|
|
|
|
* SPDYLAY_ERR_STREAM_SHUT_WR
|
|
|
|
* The transmission is not allowed for this stream (e.g., a frame
|
|
|
|
* with FIN flag set has already sent)
|
|
|
|
* SPDYLAY_ERR_DEFERRED_DATA_EXIST
|
|
|
|
* Another DATA frame has already been deferred.
|
|
|
|
* SPDYLAY_ERR_STREAM_CLOSING
|
|
|
|
* RST_STREAM was queued for this stream.
|
|
|
|
* SPDYLAY_ERR_INVALID_STREAM_STATE
|
|
|
|
* The state of the stream is not valid (e.g., if the local peer
|
|
|
|
* is receiving side and SYN_REPLY has not been sent).
|
|
|
|
*/
|
|
|
|
static int spdylay_session_predicate_data_send(spdylay_session *session,
|
|
|
|
int32_t stream_id)
|
2012-01-27 03:09:40 -05:00
|
|
|
{
|
|
|
|
spdylay_stream *stream = spdylay_session_get_stream(session, stream_id);
|
2012-03-07 10:18:18 -05:00
|
|
|
int r;
|
|
|
|
r = spdylay_predicate_stream_for_send(stream);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-01-27 03:09:40 -05:00
|
|
|
}
|
2012-02-19 09:42:25 -05:00
|
|
|
if(stream->deferred_data != NULL) {
|
|
|
|
/* stream->deferred_data != NULL means previously queued DATA
|
|
|
|
frame has not been sent. We don't allow new DATA frame is sent
|
|
|
|
in this case. */
|
2012-03-07 10:18:18 -05:00
|
|
|
return SPDYLAY_ERR_DEFERRED_DATA_EXIST;
|
2012-02-19 09:42:25 -05:00
|
|
|
}
|
2012-01-27 03:09:40 -05:00
|
|
|
if(spdylay_session_is_my_stream_id(session, stream_id)) {
|
2012-02-14 11:07:51 -05:00
|
|
|
/* If stream->state is SPDYLAY_STREAM_CLOSING, RST_STREAM was
|
|
|
|
queued but not yet sent. In this case, we won't send DATA
|
|
|
|
frames. This is because in the current architecture, DATA and
|
|
|
|
RST_STREAM in the same stream have same priority and DATA is
|
|
|
|
small seq number. So RST_STREAM will not be sent until all DATA
|
|
|
|
frames are sent. This is not desirable situation; we want to
|
|
|
|
close stream as soon as possible. To achieve this, we remove
|
|
|
|
DATA frame before RST_STREAM. */
|
2012-03-07 10:18:18 -05:00
|
|
|
if(stream->state != SPDYLAY_STREAM_CLOSING) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
|
|
|
return SPDYLAY_ERR_STREAM_CLOSING;
|
|
|
|
}
|
2012-01-27 03:09:40 -05:00
|
|
|
} else {
|
2012-03-07 10:18:18 -05:00
|
|
|
if(stream->state == SPDYLAY_STREAM_OPENED) {
|
|
|
|
return 0;
|
|
|
|
} else if(stream->state == SPDYLAY_STREAM_CLOSING) {
|
|
|
|
return SPDYLAY_ERR_STREAM_CLOSING;
|
|
|
|
} else {
|
|
|
|
return SPDYLAY_ERR_INVALID_STREAM_STATE;
|
|
|
|
}
|
2012-01-27 03:09:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-24 11:17:03 -05:00
|
|
|
static ssize_t spdylay_session_prep_frame(spdylay_session *session,
|
|
|
|
spdylay_outbound_item *item)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
|
|
|
ssize_t framebuflen;
|
|
|
|
switch(item->frame_type) {
|
|
|
|
case SPDYLAY_SYN_STREAM: {
|
2012-03-07 10:18:18 -05:00
|
|
|
int32_t stream_id;
|
2012-02-03 11:37:21 -05:00
|
|
|
spdylay_syn_stream_aux_data *aux_data;
|
2012-03-11 05:52:42 -04:00
|
|
|
int r;
|
|
|
|
r = spdylay_session_predicate_syn_stream_send(session,
|
|
|
|
&item->frame->syn_stream);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-01-28 05:22:38 -05:00
|
|
|
}
|
2012-01-29 05:07:31 -05:00
|
|
|
stream_id = session->next_stream_id;
|
2012-03-07 10:18:18 -05:00
|
|
|
|
2012-01-29 05:07:31 -05:00
|
|
|
item->frame->syn_stream.stream_id = stream_id;
|
2012-01-24 08:02:24 -05:00
|
|
|
session->next_stream_id += 2;
|
2012-03-06 09:43:45 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_3to2(item->frame->syn_stream.nv);
|
2012-03-14 09:53:14 -04:00
|
|
|
spdylay_frame_nv_sort(item->frame->syn_stream.nv);
|
2012-03-06 09:43:45 -05:00
|
|
|
}
|
2012-02-16 06:54:30 -05:00
|
|
|
framebuflen = spdylay_frame_pack_syn_stream(&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
|
|
|
&session->nvbuf,
|
|
|
|
&session->nvbuflen,
|
2012-01-24 08:02:24 -05:00
|
|
|
&item->frame->syn_stream,
|
|
|
|
&session->hd_deflater);
|
2012-03-06 09:43:45 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_2to3(item->frame->syn_stream.nv);
|
2012-03-14 09:53:14 -04:00
|
|
|
spdylay_frame_nv_sort(item->frame->syn_stream.nv);
|
2012-03-06 09:43:45 -05:00
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
if(framebuflen < 0) {
|
|
|
|
return framebuflen;
|
|
|
|
}
|
2012-02-03 11:37:21 -05:00
|
|
|
aux_data = (spdylay_syn_stream_aux_data*)item->aux_data;
|
2012-01-29 05:07:31 -05:00
|
|
|
if(spdylay_session_open_stream(session, stream_id,
|
2012-01-28 10:16:51 -05:00
|
|
|
item->frame->syn_stream.hd.flags,
|
|
|
|
item->frame->syn_stream.pri,
|
2012-02-03 11:37:21 -05:00
|
|
|
SPDYLAY_STREAM_INITIAL,
|
|
|
|
aux_data->stream_user_data) == NULL) {
|
2012-01-28 10:16:51 -05:00
|
|
|
return SPDYLAY_ERR_NOMEM;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-01-25 11:04:01 -05:00
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SPDYLAY_SYN_REPLY: {
|
2012-03-07 10:18:18 -05:00
|
|
|
int r;
|
|
|
|
r = spdylay_session_predicate_syn_reply_send
|
|
|
|
(session, item->frame->syn_reply.stream_id);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-01-27 03:09:40 -05:00
|
|
|
}
|
2012-03-06 09:43:45 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_3to2(item->frame->syn_reply.nv);
|
2012-03-14 09:53:14 -04:00
|
|
|
spdylay_frame_nv_sort(item->frame->syn_reply.nv);
|
2012-03-06 09:43:45 -05:00
|
|
|
}
|
2012-02-16 06:54:30 -05:00
|
|
|
framebuflen = spdylay_frame_pack_syn_reply(&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
|
|
|
&session->nvbuf,
|
|
|
|
&session->nvbuflen,
|
2012-01-25 11:04:01 -05:00
|
|
|
&item->frame->syn_reply,
|
|
|
|
&session->hd_deflater);
|
2012-03-06 09:43:45 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_2to3(item->frame->syn_reply.nv);
|
2012-03-14 09:53:14 -04:00
|
|
|
spdylay_frame_nv_sort(item->frame->syn_reply.nv);
|
2012-03-06 09:43:45 -05:00
|
|
|
}
|
2012-01-25 11:04:01 -05:00
|
|
|
if(framebuflen < 0) {
|
|
|
|
return framebuflen;
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
break;
|
|
|
|
}
|
2012-01-31 11:12:26 -05:00
|
|
|
case SPDYLAY_RST_STREAM:
|
2012-02-16 06:54:30 -05:00
|
|
|
framebuflen = spdylay_frame_pack_rst_stream(&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
2012-01-31 11:12:26 -05:00
|
|
|
&item->frame->rst_stream);
|
|
|
|
if(framebuflen < 0) {
|
|
|
|
return framebuflen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
case SPDYLAY_SETTINGS:
|
2012-02-16 06:54:30 -05:00
|
|
|
framebuflen = spdylay_frame_pack_settings(&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
2012-01-31 11:12:26 -05:00
|
|
|
&item->frame->settings);
|
|
|
|
if(framebuflen < 0) {
|
|
|
|
return framebuflen;
|
|
|
|
}
|
|
|
|
break;
|
2012-01-28 03:29:22 -05:00
|
|
|
case SPDYLAY_NOOP:
|
|
|
|
/* We don't have any public API to add NOOP, so here is
|
|
|
|
unreachable. */
|
|
|
|
abort();
|
2012-01-27 09:05:29 -05:00
|
|
|
case SPDYLAY_PING:
|
2012-02-16 06:54:30 -05:00
|
|
|
framebuflen = spdylay_frame_pack_ping(&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
|
|
|
&item->frame->ping);
|
2012-01-27 09:05:29 -05:00
|
|
|
if(framebuflen < 0) {
|
|
|
|
return framebuflen;
|
|
|
|
}
|
|
|
|
break;
|
2012-02-23 10:02:29 -05:00
|
|
|
case SPDYLAY_HEADERS: {
|
2012-03-07 10:18:18 -05:00
|
|
|
int r;
|
|
|
|
r = spdylay_session_predicate_headers_send(session,
|
|
|
|
item->frame->headers.stream_id);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-02-23 10:02:29 -05:00
|
|
|
}
|
2012-03-06 09:43:45 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_3to2(item->frame->headers.nv);
|
2012-03-14 09:53:14 -04:00
|
|
|
spdylay_frame_nv_sort(item->frame->headers.nv);
|
2012-03-06 09:43:45 -05:00
|
|
|
}
|
2012-02-23 10:02:29 -05:00
|
|
|
framebuflen = spdylay_frame_pack_headers(&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
|
|
|
&session->nvbuf,
|
|
|
|
&session->nvbuflen,
|
|
|
|
&item->frame->headers,
|
|
|
|
&session->hd_deflater);
|
2012-03-06 09:43:45 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_2to3(item->frame->headers.nv);
|
2012-03-14 09:53:14 -04:00
|
|
|
spdylay_frame_nv_sort(item->frame->headers.nv);
|
2012-03-06 09:43:45 -05:00
|
|
|
}
|
2012-02-23 10:02:29 -05:00
|
|
|
if(framebuflen < 0) {
|
|
|
|
return framebuflen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-02-25 10:12:32 -05:00
|
|
|
case SPDYLAY_WINDOW_UPDATE: {
|
2012-03-07 10:18:18 -05:00
|
|
|
int r;
|
|
|
|
r = spdylay_session_predicate_window_update_send
|
|
|
|
(session, item->frame->window_update.stream_id);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-02-25 10:12:32 -05:00
|
|
|
}
|
|
|
|
framebuflen = spdylay_frame_pack_window_update(&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
|
|
|
&item->frame->window_update);
|
|
|
|
if(framebuflen < 0) {
|
|
|
|
return framebuflen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-28 05:22:38 -05:00
|
|
|
case SPDYLAY_GOAWAY:
|
|
|
|
if(session->goaway_flags & SPDYLAY_GOAWAY_SEND) {
|
|
|
|
/* TODO The spec does not mandate that both endpoints have to
|
|
|
|
exchange GOAWAY. This implementation allows receiver of first
|
|
|
|
GOAWAY can sent its own GOAWAY to tell the remote peer that
|
|
|
|
last-good-stream-id. */
|
2012-03-07 10:18:18 -05:00
|
|
|
return SPDYLAY_ERR_GOAWAY_ALREADY_SENT;
|
2012-01-28 05:22:38 -05:00
|
|
|
}
|
2012-02-16 06:54:30 -05:00
|
|
|
framebuflen = spdylay_frame_pack_goaway(&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
|
|
|
&item->frame->goaway);
|
2012-01-28 05:22:38 -05:00
|
|
|
if(framebuflen < 0) {
|
|
|
|
return framebuflen;
|
|
|
|
}
|
|
|
|
break;
|
2012-01-26 11:17:40 -05:00
|
|
|
case SPDYLAY_DATA: {
|
2012-02-28 07:40:19 -05:00
|
|
|
size_t next_readmax;
|
2012-02-25 10:12:32 -05:00
|
|
|
spdylay_stream *stream;
|
2012-03-07 10:18:18 -05:00
|
|
|
int r;
|
|
|
|
r = spdylay_session_predicate_data_send(session,
|
|
|
|
item->frame->data.stream_id);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-01-27 03:09:40 -05:00
|
|
|
}
|
2012-02-25 10:12:32 -05:00
|
|
|
stream = spdylay_session_get_stream(session, item->frame->data.stream_id);
|
|
|
|
/* Assuming stream is not NULL */
|
|
|
|
assert(stream);
|
2012-02-28 07:40:19 -05:00
|
|
|
next_readmax = spdylay_session_next_data_read(session, stream);
|
|
|
|
if(next_readmax == 0) {
|
2012-02-25 10:12:32 -05:00
|
|
|
spdylay_stream_defer_data(stream, item, SPDYLAY_DEFERRED_FLOW_CONTROL);
|
|
|
|
return SPDYLAY_ERR_DEFERRED;
|
|
|
|
}
|
2012-02-28 07:40:19 -05:00
|
|
|
framebuflen = spdylay_session_pack_data(session,
|
|
|
|
&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
|
|
|
next_readmax,
|
|
|
|
&item->frame->data);
|
2012-02-19 09:42:25 -05:00
|
|
|
if(framebuflen == SPDYLAY_ERR_DEFERRED) {
|
2012-02-25 10:12:32 -05:00
|
|
|
spdylay_stream_defer_data(stream, item, SPDYLAY_DEFERRED_NONE);
|
2012-02-19 09:42:25 -05:00
|
|
|
return SPDYLAY_ERR_DEFERRED;
|
|
|
|
} else if(framebuflen < 0) {
|
2012-01-26 11:17:40 -05:00
|
|
|
return framebuflen;
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
default:
|
|
|
|
framebuflen = SPDYLAY_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
return framebuflen;
|
|
|
|
}
|
|
|
|
|
2012-01-27 09:05:29 -05:00
|
|
|
spdylay_outbound_item* spdylay_session_get_ob_pq_top
|
2012-01-26 11:17:40 -05:00
|
|
|
(spdylay_session *session)
|
|
|
|
{
|
|
|
|
return (spdylay_outbound_item*)spdylay_pq_top(&session->ob_pq);
|
|
|
|
}
|
|
|
|
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_outbound_item* spdylay_session_get_next_ob_item
|
|
|
|
(spdylay_session *session)
|
|
|
|
{
|
|
|
|
if(spdylay_pq_empty(&session->ob_pq)) {
|
|
|
|
if(spdylay_pq_empty(&session->ob_ss_pq)) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* Return item only when concurrent connection limit is not
|
|
|
|
reached */
|
2012-02-08 09:45:48 -05:00
|
|
|
if(spdylay_session_get_max_concurrent_streams_reached(session)) {
|
2012-02-05 10:14:19 -05:00
|
|
|
return NULL;
|
2012-02-08 09:45:48 -05:00
|
|
|
} else {
|
|
|
|
return spdylay_pq_top(&session->ob_ss_pq);
|
2012-02-05 10:14:19 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(spdylay_pq_empty(&session->ob_ss_pq)) {
|
|
|
|
return spdylay_pq_top(&session->ob_pq);
|
|
|
|
} else {
|
|
|
|
spdylay_outbound_item *item, *syn_stream_item;
|
|
|
|
item = spdylay_pq_top(&session->ob_pq);
|
|
|
|
syn_stream_item = spdylay_pq_top(&session->ob_ss_pq);
|
2012-02-08 09:45:48 -05:00
|
|
|
if(spdylay_session_get_max_concurrent_streams_reached(session) ||
|
2012-02-05 10:14:19 -05:00
|
|
|
item->pri < syn_stream_item->pri ||
|
|
|
|
(item->pri == syn_stream_item->pri &&
|
|
|
|
item->seq < syn_stream_item->seq)) {
|
|
|
|
return item;
|
|
|
|
} else {
|
|
|
|
return syn_stream_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
spdylay_outbound_item* spdylay_session_pop_next_ob_item
|
|
|
|
(spdylay_session *session)
|
|
|
|
{
|
|
|
|
if(spdylay_pq_empty(&session->ob_pq)) {
|
|
|
|
if(spdylay_pq_empty(&session->ob_ss_pq)) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
|
|
|
/* Pop item only when concurrent connection limit is not
|
|
|
|
reached */
|
2012-02-08 09:45:48 -05:00
|
|
|
if(spdylay_session_get_max_concurrent_streams_reached(session)) {
|
|
|
|
return NULL;
|
|
|
|
} else {
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_outbound_item *item;
|
|
|
|
item = spdylay_pq_top(&session->ob_ss_pq);
|
|
|
|
spdylay_pq_pop(&session->ob_ss_pq);
|
|
|
|
return item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(spdylay_pq_empty(&session->ob_ss_pq)) {
|
|
|
|
spdylay_outbound_item *item;
|
|
|
|
item = spdylay_pq_top(&session->ob_pq);
|
|
|
|
spdylay_pq_pop(&session->ob_pq);
|
|
|
|
return item;
|
|
|
|
} else {
|
|
|
|
spdylay_outbound_item *item, *syn_stream_item;
|
|
|
|
item = spdylay_pq_top(&session->ob_pq);
|
|
|
|
syn_stream_item = spdylay_pq_top(&session->ob_ss_pq);
|
2012-02-08 09:45:48 -05:00
|
|
|
if(spdylay_session_get_max_concurrent_streams_reached(session) ||
|
2012-02-05 10:14:19 -05:00
|
|
|
item->pri < syn_stream_item->pri ||
|
|
|
|
(item->pri == syn_stream_item->pri &&
|
|
|
|
item->seq < syn_stream_item->seq)) {
|
|
|
|
spdylay_pq_pop(&session->ob_pq);
|
|
|
|
return item;
|
|
|
|
} else {
|
|
|
|
spdylay_pq_pop(&session->ob_ss_pq);
|
|
|
|
return syn_stream_item;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-23 08:49:08 -05:00
|
|
|
/*
|
|
|
|
* Called after a frame is sent.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* SPDYLAY_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
* SPDYLAY_ERR_CALLBACK_FAILURE
|
|
|
|
* The callback function failed.
|
|
|
|
*/
|
2012-01-25 11:04:01 -05:00
|
|
|
static int spdylay_session_after_frame_sent(spdylay_session *session)
|
|
|
|
{
|
2012-01-26 11:17:40 -05:00
|
|
|
/* TODO handle FIN flag. */
|
2012-01-30 09:55:00 -05:00
|
|
|
spdylay_outbound_item *item = session->aob.item;
|
2012-01-25 11:04:01 -05:00
|
|
|
spdylay_frame *frame = session->aob.item->frame;
|
2012-01-26 11:17:40 -05:00
|
|
|
spdylay_frame_type type = session->aob.item->frame_type;
|
2012-01-27 14:28:39 -05:00
|
|
|
if(type == SPDYLAY_DATA) {
|
|
|
|
if(session->callbacks.on_data_send_callback) {
|
|
|
|
session->callbacks.on_data_send_callback
|
2012-02-16 09:38:19 -05:00
|
|
|
(session,
|
|
|
|
frame->data.eof ? frame->data.flags :
|
2012-02-24 07:40:13 -05:00
|
|
|
(frame->data.flags & (~SPDYLAY_DATA_FLAG_FIN)),
|
2012-02-16 09:38:19 -05:00
|
|
|
frame->data.stream_id,
|
2012-02-13 11:42:00 -05:00
|
|
|
session->aob.framebuflen-SPDYLAY_HEAD_LEN, session->user_data);
|
2012-01-27 14:28:39 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(session->callbacks.on_ctrl_send_callback) {
|
|
|
|
session->callbacks.on_ctrl_send_callback
|
|
|
|
(session, type, frame, session->user_data);
|
|
|
|
}
|
|
|
|
}
|
2012-01-26 11:17:40 -05:00
|
|
|
switch(type) {
|
2012-01-25 11:04:01 -05:00
|
|
|
case SPDYLAY_SYN_STREAM: {
|
|
|
|
spdylay_stream *stream =
|
|
|
|
spdylay_session_get_stream(session, frame->syn_stream.stream_id);
|
|
|
|
if(stream) {
|
2012-02-03 11:37:21 -05:00
|
|
|
spdylay_syn_stream_aux_data *aux_data;
|
2012-01-25 11:04:01 -05:00
|
|
|
stream->state = SPDYLAY_STREAM_OPENING;
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2012-01-28 10:08:51 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
|
|
|
|
}
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->syn_stream.hd.flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL) {
|
2012-01-28 10:08:51 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
|
|
|
|
}
|
|
|
|
spdylay_session_close_stream_if_shut_rdwr(session, stream);
|
2012-02-03 11:37:21 -05:00
|
|
|
/* We assume aux_data is a pointer to spdylay_syn_stream_aux_data */
|
|
|
|
aux_data = (spdylay_syn_stream_aux_data*)item->aux_data;
|
|
|
|
if(aux_data->data_prd) {
|
2012-01-30 09:55:00 -05:00
|
|
|
int r;
|
2012-02-03 11:37:21 -05:00
|
|
|
/* spdylay_submit_data() makes a copy of aux_data->data_prd */
|
|
|
|
r = spdylay_submit_data(session, frame->syn_stream.stream_id,
|
2012-02-24 07:40:13 -05:00
|
|
|
SPDYLAY_DATA_FLAG_FIN, aux_data->data_prd);
|
2012-01-30 09:55:00 -05:00
|
|
|
if(r != 0) {
|
2012-02-23 08:49:08 -05:00
|
|
|
/* FATAL error */
|
|
|
|
assert(r < SPDYLAY_ERR_FATAL);
|
2012-01-30 09:55:00 -05:00
|
|
|
/* TODO If r is not FATAL, we should send RST_STREAM. */
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
2012-01-25 11:04:01 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SPDYLAY_SYN_REPLY: {
|
|
|
|
spdylay_stream *stream =
|
|
|
|
spdylay_session_get_stream(session, frame->syn_reply.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
stream->state = SPDYLAY_STREAM_OPENED;
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->syn_reply.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2012-01-28 10:08:51 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
|
|
|
|
}
|
|
|
|
spdylay_session_close_stream_if_shut_rdwr(session, stream);
|
2012-01-30 09:55:00 -05:00
|
|
|
if(item->aux_data) {
|
|
|
|
/* We assume aux_data is a pointer to spdylay_data_provider */
|
|
|
|
spdylay_data_provider *data_prd =
|
|
|
|
(spdylay_data_provider*)item->aux_data;
|
|
|
|
int r;
|
2012-02-15 09:02:51 -05:00
|
|
|
r = spdylay_submit_data(session, frame->syn_reply.stream_id,
|
2012-02-24 07:40:13 -05:00
|
|
|
SPDYLAY_DATA_FLAG_FIN, data_prd);
|
2012-01-30 09:55:00 -05:00
|
|
|
if(r != 0) {
|
2012-02-23 08:49:08 -05:00
|
|
|
/* FATAL error */
|
|
|
|
assert(r < SPDYLAY_ERR_FATAL);
|
2012-01-30 09:55:00 -05:00
|
|
|
/* TODO If r is not FATAL, we should send RST_STREAM. */
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
2012-01-25 11:04:01 -05:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
case SPDYLAY_RST_STREAM:
|
2012-02-02 09:19:01 -05:00
|
|
|
if(!session->server &&
|
|
|
|
spdylay_session_is_my_stream_id(session, frame->rst_stream.stream_id) &&
|
|
|
|
frame->rst_stream.status_code == SPDYLAY_CANCEL) {
|
|
|
|
spdylay_session_close_pushed_streams(session, frame->rst_stream.stream_id,
|
|
|
|
frame->rst_stream.status_code);
|
|
|
|
}
|
2012-01-29 09:00:33 -05:00
|
|
|
spdylay_session_close_stream(session, frame->rst_stream.stream_id,
|
|
|
|
frame->rst_stream.status_code);
|
2012-01-26 11:17:40 -05:00
|
|
|
break;
|
2012-01-31 11:12:26 -05:00
|
|
|
case SPDYLAY_SETTINGS:
|
|
|
|
/* nothing to do */
|
|
|
|
break;
|
2012-01-28 03:29:22 -05:00
|
|
|
case SPDYLAY_NOOP:
|
|
|
|
/* We don't have any public API to add NOOP, so here is
|
|
|
|
unreachable. */
|
|
|
|
abort();
|
2012-01-27 09:05:29 -05:00
|
|
|
case SPDYLAY_PING:
|
|
|
|
/* We record the time now and show application code RTT when
|
|
|
|
reply PING is received. */
|
|
|
|
session->last_ping_unique_id = frame->ping.unique_id;
|
|
|
|
break;
|
2012-01-28 05:22:38 -05:00
|
|
|
case SPDYLAY_GOAWAY:
|
|
|
|
session->goaway_flags |= SPDYLAY_GOAWAY_SEND;
|
|
|
|
break;
|
2012-02-23 10:02:29 -05:00
|
|
|
case SPDYLAY_HEADERS: {
|
|
|
|
spdylay_stream *stream =
|
|
|
|
spdylay_session_get_stream(session, frame->headers.stream_id);
|
|
|
|
if(stream) {
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->headers.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2012-02-23 10:02:29 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
|
|
|
|
}
|
|
|
|
spdylay_session_close_stream_if_shut_rdwr(session, stream);
|
|
|
|
}
|
|
|
|
break;
|
|
|
|
}
|
2012-02-25 10:12:32 -05:00
|
|
|
case SPDYLAY_WINDOW_UPDATE:
|
|
|
|
break;
|
2012-01-26 11:17:40 -05:00
|
|
|
case SPDYLAY_DATA:
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->data.eof && (frame->data.flags & SPDYLAY_DATA_FLAG_FIN)) {
|
2012-01-28 10:08:51 -05:00
|
|
|
spdylay_stream *stream =
|
|
|
|
spdylay_session_get_stream(session, frame->data.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
|
|
|
|
spdylay_session_close_stream_if_shut_rdwr(session, stream);
|
2012-01-27 03:22:17 -05:00
|
|
|
}
|
2012-01-26 11:17:40 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-01-25 11:04:01 -05:00
|
|
|
};
|
2012-01-26 11:17:40 -05:00
|
|
|
if(type == SPDYLAY_DATA) {
|
|
|
|
int r;
|
2012-02-14 11:07:51 -05:00
|
|
|
/* If session is closed or RST_STREAM was queued, we won't send
|
|
|
|
further data. */
|
2012-02-16 09:38:19 -05:00
|
|
|
if(frame->data.eof ||
|
2012-03-07 10:18:18 -05:00
|
|
|
spdylay_session_predicate_data_send(session,
|
|
|
|
frame->data.stream_id) != 0) {
|
2012-01-26 11:17:40 -05:00
|
|
|
spdylay_active_outbound_item_reset(&session->aob);
|
|
|
|
} else {
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_outbound_item* item = spdylay_session_get_next_ob_item(session);
|
2012-02-25 10:12:32 -05:00
|
|
|
/* If priority of this stream is higher or equal to other stream
|
|
|
|
waiting at the top of the queue, we continue to send this
|
|
|
|
data. */
|
2012-02-05 10:14:19 -05:00
|
|
|
if(item == NULL || session->aob.item->pri <= item->pri) {
|
2012-02-28 07:40:19 -05:00
|
|
|
size_t next_readmax;
|
2012-02-25 10:12:32 -05:00
|
|
|
spdylay_stream *stream;
|
|
|
|
stream = spdylay_session_get_stream(session, frame->data.stream_id);
|
|
|
|
/* Assuming stream is not NULL */
|
|
|
|
assert(stream);
|
2012-02-28 07:40:19 -05:00
|
|
|
next_readmax = spdylay_session_next_data_read(session, stream);
|
|
|
|
if(next_readmax == 0) {
|
2012-02-25 10:12:32 -05:00
|
|
|
spdylay_stream_defer_data(stream, session->aob.item,
|
|
|
|
SPDYLAY_DEFERRED_FLOW_CONTROL);
|
|
|
|
session->aob.item = NULL;
|
|
|
|
spdylay_active_outbound_item_reset(&session->aob);
|
|
|
|
return 0;
|
|
|
|
}
|
2012-02-28 07:40:19 -05:00
|
|
|
r = spdylay_session_pack_data(session,
|
|
|
|
&session->aob.framebuf,
|
|
|
|
&session->aob.framebufmax,
|
|
|
|
next_readmax,
|
|
|
|
&frame->data);
|
2012-02-19 09:42:25 -05:00
|
|
|
if(r == SPDYLAY_ERR_DEFERRED) {
|
2012-02-25 10:12:32 -05:00
|
|
|
spdylay_stream_defer_data(stream, session->aob.item,
|
|
|
|
SPDYLAY_DEFERRED_NONE);
|
2012-02-19 09:42:25 -05:00
|
|
|
session->aob.item = NULL;
|
|
|
|
spdylay_active_outbound_item_reset(&session->aob);
|
|
|
|
} else if(r < 0) {
|
2012-02-23 08:49:08 -05:00
|
|
|
/* We don't return other error code other than
|
|
|
|
SPDYLAY_ERR_CALLBACK_FAILURE here. */
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_active_outbound_item_reset(&session->aob);
|
2012-02-23 08:49:08 -05:00
|
|
|
return SPDYLAY_ERR_CALLBACK_FAILURE;
|
2012-02-19 09:42:25 -05:00
|
|
|
} else {
|
|
|
|
session->aob.framebuflen = r;
|
|
|
|
session->aob.framebufoff = 0;
|
2012-02-05 10:14:19 -05:00
|
|
|
}
|
2012-01-26 11:17:40 -05:00
|
|
|
} else {
|
2012-02-05 10:14:19 -05:00
|
|
|
r = spdylay_pq_push(&session->ob_pq, session->aob.item);
|
|
|
|
if(r == 0) {
|
|
|
|
session->aob.item = NULL;
|
|
|
|
spdylay_active_outbound_item_reset(&session->aob);
|
|
|
|
} else {
|
2012-02-23 08:49:08 -05:00
|
|
|
/* FATAL error */
|
|
|
|
assert(r < SPDYLAY_ERR_FATAL);
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_active_outbound_item_reset(&session->aob);
|
|
|
|
return r;
|
|
|
|
}
|
2012-01-26 11:17:40 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
spdylay_active_outbound_item_reset(&session->aob);
|
|
|
|
}
|
|
|
|
return 0;
|
2012-01-25 11:04:01 -05:00
|
|
|
}
|
|
|
|
|
2012-01-24 08:02:24 -05:00
|
|
|
int spdylay_session_send(spdylay_session *session)
|
|
|
|
{
|
2012-01-26 11:17:40 -05:00
|
|
|
int r;
|
2012-02-05 10:14:19 -05:00
|
|
|
while(1) {
|
2012-01-24 08:02:24 -05:00
|
|
|
const uint8_t *data;
|
|
|
|
size_t datalen;
|
|
|
|
ssize_t sentlen;
|
|
|
|
if(session->aob.item == NULL) {
|
2012-02-05 10:14:19 -05:00
|
|
|
spdylay_outbound_item *item;
|
2012-01-24 08:02:24 -05:00
|
|
|
ssize_t framebuflen;
|
2012-02-05 10:14:19 -05:00
|
|
|
item = spdylay_session_pop_next_ob_item(session);
|
|
|
|
if(item == NULL) {
|
|
|
|
break;
|
|
|
|
}
|
2012-02-16 06:54:30 -05:00
|
|
|
framebuflen = spdylay_session_prep_frame(session, item);
|
2012-02-19 09:42:25 -05:00
|
|
|
if(framebuflen == SPDYLAY_ERR_DEFERRED) {
|
|
|
|
continue;
|
|
|
|
} else if(framebuflen < 0) {
|
2012-03-07 10:18:18 -05:00
|
|
|
/* The library is responsible for the transmission of
|
|
|
|
WINDOW_UPDATE frame, so we don't call error callback for
|
|
|
|
it. */
|
|
|
|
if(session->callbacks.on_ctrl_not_send_callback &&
|
|
|
|
spdylay_is_non_fatal(framebuflen) &&
|
|
|
|
item->frame_type != SPDYLAY_WINDOW_UPDATE) {
|
|
|
|
session->callbacks.on_ctrl_not_send_callback(session,
|
|
|
|
item->frame_type,
|
|
|
|
item->frame,
|
|
|
|
framebuflen,
|
|
|
|
session->user_data);
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
spdylay_outbound_item_free(item);
|
|
|
|
free(item);
|
2012-03-07 10:40:17 -05:00
|
|
|
if(spdylay_is_fatal(framebuflen)) {
|
2012-01-28 05:22:38 -05:00
|
|
|
return framebuflen;
|
|
|
|
} else {
|
2012-02-19 09:42:25 -05:00
|
|
|
continue;
|
2012-01-28 05:22:38 -05:00
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
session->aob.item = item;
|
|
|
|
session->aob.framebuflen = framebuflen;
|
2012-01-28 03:25:14 -05:00
|
|
|
/* Call before_send callback */
|
|
|
|
if(item->frame_type != SPDYLAY_DATA &&
|
|
|
|
session->callbacks.before_ctrl_send_callback) {
|
|
|
|
session->callbacks.before_ctrl_send_callback
|
|
|
|
(session, item->frame_type, item->frame, session->user_data);
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
data = session->aob.framebuf + session->aob.framebufoff;
|
|
|
|
datalen = session->aob.framebuflen - session->aob.framebufoff;
|
2012-01-24 08:56:26 -05:00
|
|
|
sentlen = session->callbacks.send_callback(session, data, datalen, 0,
|
2012-01-24 08:02:24 -05:00
|
|
|
session->user_data);
|
|
|
|
if(sentlen < 0) {
|
|
|
|
if(sentlen == SPDYLAY_ERR_WOULDBLOCK) {
|
|
|
|
return 0;
|
|
|
|
} else {
|
2012-02-23 08:49:08 -05:00
|
|
|
return SPDYLAY_ERR_CALLBACK_FAILURE;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
session->aob.framebufoff += sentlen;
|
2012-02-25 10:12:32 -05:00
|
|
|
if(session->flow_control &&
|
|
|
|
session->aob.item->frame_type == SPDYLAY_DATA) {
|
|
|
|
spdylay_frame *frame;
|
|
|
|
spdylay_stream *stream;
|
|
|
|
frame = session->aob.item->frame;
|
|
|
|
stream = spdylay_session_get_stream(session, frame->data.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
stream->window_size -= spdylay_get_uint32(&session->aob.framebuf[4]);
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
if(session->aob.framebufoff == session->aob.framebuflen) {
|
|
|
|
/* Frame has completely sent */
|
2012-01-26 11:17:40 -05:00
|
|
|
r = spdylay_session_after_frame_sent(session);
|
|
|
|
if(r < 0) {
|
2012-02-23 08:49:08 -05:00
|
|
|
/* FATAL */
|
|
|
|
assert(r < SPDYLAY_ERR_FATAL);
|
2012-01-26 11:17:40 -05:00
|
|
|
return r;
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-17 10:39:38 -04:00
|
|
|
static ssize_t spdylay_recv(spdylay_session *session, uint8_t *buf, size_t len)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
|
|
|
ssize_t r;
|
|
|
|
r = session->callbacks.recv_callback
|
2012-03-17 10:39:38 -04:00
|
|
|
(session, buf, len, 0, session->user_data);
|
2012-01-24 08:02:24 -05:00
|
|
|
if(r > 0) {
|
2012-03-17 10:39:38 -04:00
|
|
|
if(r > len) {
|
2012-01-24 08:02:24 -05:00
|
|
|
return SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
} else if(r < 0) {
|
|
|
|
if(r != SPDYLAY_ERR_WOULDBLOCK) {
|
|
|
|
r = SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-02-07 10:10:23 -05:00
|
|
|
static void spdylay_session_call_on_request_recv
|
|
|
|
(spdylay_session *session, int32_t stream_id)
|
|
|
|
{
|
|
|
|
if(session->callbacks.on_request_recv_callback) {
|
|
|
|
session->callbacks.on_request_recv_callback(session, stream_id,
|
|
|
|
session->user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-25 07:31:28 -05:00
|
|
|
static void spdylay_session_call_on_ctrl_frame_received
|
|
|
|
(spdylay_session *session, spdylay_frame_type type, spdylay_frame *frame)
|
|
|
|
{
|
|
|
|
if(session->callbacks.on_ctrl_recv_callback) {
|
|
|
|
session->callbacks.on_ctrl_recv_callback
|
|
|
|
(session, type, frame, session->user_data);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Checks whether received stream_id is valid.
|
|
|
|
* This function returns 1 if it succeeds, or 0.
|
|
|
|
*/
|
|
|
|
static int spdylay_session_is_new_peer_stream_id(spdylay_session *session,
|
|
|
|
int32_t stream_id)
|
|
|
|
{
|
|
|
|
if(stream_id == 0) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(session->server) {
|
|
|
|
return stream_id % 2 == 1 && session->last_recv_stream_id < stream_id;
|
|
|
|
} else {
|
|
|
|
return stream_id % 2 == 0 && session->last_recv_stream_id < stream_id;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-02-15 08:11:42 -05:00
|
|
|
/*
|
2012-02-24 09:33:06 -05:00
|
|
|
* Returns non-zero iff version == session->version
|
2012-02-15 08:11:42 -05:00
|
|
|
*/
|
2012-02-24 09:33:06 -05:00
|
|
|
static int spdylay_session_check_version(spdylay_session *session,
|
|
|
|
uint16_t version)
|
2012-02-15 08:11:42 -05:00
|
|
|
{
|
2012-02-24 09:33:06 -05:00
|
|
|
return session->version == version;
|
2012-02-15 08:11:42 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Returns non-zero iff name/value pairs |nv| are good shape.
|
|
|
|
* Currently, we only checks whether names are lower cased. The spdy/2
|
|
|
|
* spec requires that names must be lower cased.
|
|
|
|
*/
|
|
|
|
static int spdylay_session_check_nv(char **nv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; nv[i]; i += 2) {
|
|
|
|
int j;
|
|
|
|
for(j = 0; nv[i][j] != '\0'; ++j) {
|
|
|
|
if('A' <= nv[i][j] && nv[i][j] <= 'Z') {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 1;
|
|
|
|
}
|
|
|
|
|
2012-01-25 07:31:28 -05:00
|
|
|
/*
|
2012-01-25 09:46:07 -05:00
|
|
|
* Validates SYN_STREAM frame |frame|. This function returns 0 if it
|
2012-01-28 08:46:12 -05:00
|
|
|
* succeeds, or non-zero spdylay_status_code.
|
2012-01-25 07:31:28 -05:00
|
|
|
*/
|
2012-01-25 09:46:07 -05:00
|
|
|
static int spdylay_session_validate_syn_stream(spdylay_session *session,
|
|
|
|
spdylay_syn_stream *frame)
|
2012-01-25 07:31:28 -05:00
|
|
|
{
|
2012-02-24 09:33:06 -05:00
|
|
|
if(!spdylay_session_check_version(session, frame->hd.version)) {
|
2012-01-28 08:46:12 -05:00
|
|
|
return SPDYLAY_UNSUPPORTED_VERSION;
|
|
|
|
}
|
2012-02-01 10:19:31 -05:00
|
|
|
if(session->server) {
|
|
|
|
if(frame->assoc_stream_id != 0) {
|
|
|
|
return SPDYLAY_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
if(frame->assoc_stream_id == 0) {
|
|
|
|
/* spdy/2 spec: When a client receives a SYN_STREAM from the
|
|
|
|
server with an Associated-To-Stream-ID of 0, it must reply with
|
|
|
|
a RST_STREAM with error code INVALID_STREAM. */
|
|
|
|
return SPDYLAY_INVALID_STREAM;
|
|
|
|
}
|
2012-02-24 07:40:13 -05:00
|
|
|
if((frame->hd.flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL) == 0 ||
|
2012-02-01 10:19:31 -05:00
|
|
|
frame->assoc_stream_id % 2 == 0 ||
|
|
|
|
spdylay_session_get_stream(session, frame->assoc_stream_id) == NULL) {
|
|
|
|
/* It seems spdy/2 spec does not say which status code should be
|
|
|
|
returned in these cases. */
|
|
|
|
return SPDYLAY_PROTOCOL_ERROR;
|
|
|
|
}
|
|
|
|
}
|
2012-02-08 09:45:48 -05:00
|
|
|
if(spdylay_session_get_max_concurrent_streams_reached(session)) {
|
|
|
|
/* spdy/2 spec does not clearly say what to do when max concurrent
|
|
|
|
streams number is reached. The mod_spdy sends
|
|
|
|
SPDYLAY_REFUSED_STREAM and we think it is reasonable. So we
|
|
|
|
follow it. */
|
|
|
|
return SPDYLAY_REFUSED_STREAM;
|
|
|
|
}
|
2012-02-15 08:11:42 -05:00
|
|
|
if(!spdylay_session_check_nv(frame->nv)) {
|
|
|
|
return SPDYLAY_PROTOCOL_ERROR;
|
|
|
|
}
|
2012-01-28 08:46:12 -05:00
|
|
|
return 0;
|
2012-01-25 07:31:28 -05:00
|
|
|
}
|
|
|
|
|
2012-02-15 08:11:42 -05:00
|
|
|
|
2012-01-28 08:46:12 -05:00
|
|
|
static int spdylay_session_handle_invalid_stream
|
|
|
|
(spdylay_session *session,
|
|
|
|
int32_t stream_id,
|
|
|
|
spdylay_frame_type type,
|
|
|
|
spdylay_frame *frame,
|
|
|
|
spdylay_status_code status_code)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
|
|
|
int r;
|
2012-01-28 08:46:12 -05:00
|
|
|
r = spdylay_session_add_rst_stream(session, stream_id, status_code);
|
2012-01-25 07:31:28 -05:00
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
if(session->callbacks.on_invalid_ctrl_recv_callback) {
|
|
|
|
session->callbacks.on_invalid_ctrl_recv_callback
|
|
|
|
(session, type, frame, session->user_data);
|
|
|
|
}
|
2012-01-25 09:46:07 -05:00
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spdylay_session_on_syn_stream_received(spdylay_session *session,
|
|
|
|
spdylay_frame *frame)
|
|
|
|
{
|
2012-02-14 10:45:09 -05:00
|
|
|
int r = 0;
|
|
|
|
int status_code;
|
2012-01-28 05:22:38 -05:00
|
|
|
if(session->goaway_flags) {
|
|
|
|
/* We don't accept SYN_STREAM after GOAWAY is sent or received. */
|
|
|
|
return 0;
|
|
|
|
}
|
2012-03-11 06:27:33 -04:00
|
|
|
if(session->last_recv_stream_id == frame->syn_stream.stream_id) {
|
|
|
|
/* SPDY/3 spec says if an endpoint receives same stream ID twice,
|
|
|
|
it MUST issue a stream error with status code
|
|
|
|
PROTOCOL_ERROR. */
|
|
|
|
status_code = SPDYLAY_PROTOCOL_ERROR;
|
|
|
|
} else if(!spdylay_session_is_new_peer_stream_id
|
|
|
|
(session, frame->syn_stream.stream_id)) {
|
|
|
|
/* SPDY/3 spec says if an endpoint receives a SYN_STREAM with a
|
|
|
|
stream ID which is less than any previously received
|
|
|
|
SYN_STREAM, it MUST issue a session error with status
|
|
|
|
PROTOCOL_ERROR */
|
|
|
|
if(session->callbacks.on_invalid_ctrl_recv_callback) {
|
|
|
|
session->callbacks.on_invalid_ctrl_recv_callback(session,
|
|
|
|
SPDYLAY_SYN_STREAM,
|
|
|
|
frame,
|
|
|
|
session->user_data);
|
|
|
|
}
|
|
|
|
return spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
|
|
|
} else {
|
|
|
|
session->last_recv_stream_id = frame->syn_stream.stream_id;
|
|
|
|
status_code = spdylay_session_validate_syn_stream(session,
|
|
|
|
&frame->syn_stream);
|
|
|
|
}
|
2012-02-14 10:45:09 -05:00
|
|
|
if(status_code == 0) {
|
2012-01-27 03:09:40 -05:00
|
|
|
uint8_t flags = frame->syn_stream.hd.flags;
|
2012-02-24 07:40:13 -05:00
|
|
|
if((flags & SPDYLAY_CTRL_FLAG_FIN) &&
|
|
|
|
(flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL)) {
|
2012-01-27 03:09:40 -05:00
|
|
|
/* If the stream is UNIDIRECTIONAL and FIN bit set, we can close
|
|
|
|
stream upon receiving SYN_STREAM. So, the stream needs not to
|
|
|
|
be opened. */
|
|
|
|
} else {
|
2012-01-28 10:16:51 -05:00
|
|
|
spdylay_stream *stream;
|
|
|
|
stream = spdylay_session_open_stream(session, frame->syn_stream.stream_id,
|
|
|
|
frame->syn_stream.hd.flags,
|
|
|
|
frame->syn_stream.pri,
|
2012-02-03 11:37:21 -05:00
|
|
|
SPDYLAY_STREAM_OPENING,
|
|
|
|
NULL);
|
2012-01-28 10:16:51 -05:00
|
|
|
if(stream) {
|
2012-02-24 07:40:13 -05:00
|
|
|
if(flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2012-01-28 10:08:51 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
|
|
|
|
}
|
2012-02-24 07:40:13 -05:00
|
|
|
if(flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL) {
|
2012-01-28 10:08:51 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_WR);
|
|
|
|
}
|
|
|
|
/* We don't call spdylay_session_close_stream_if_shut_rdwr()
|
2012-02-24 07:40:13 -05:00
|
|
|
here because either SPDYLAY_CTRL_FLAG_FIN or
|
|
|
|
SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL is not set here. */
|
2012-01-28 10:08:51 -05:00
|
|
|
}
|
2012-01-27 03:09:40 -05:00
|
|
|
}
|
2012-02-14 10:45:09 -05:00
|
|
|
spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_SYN_STREAM,
|
|
|
|
frame);
|
2012-02-24 07:40:13 -05:00
|
|
|
if(flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2012-02-14 10:45:09 -05:00
|
|
|
spdylay_session_call_on_request_recv(session,
|
|
|
|
frame->syn_stream.stream_id);
|
2012-02-24 07:40:13 -05:00
|
|
|
if(flags & SPDYLAY_CTRL_FLAG_UNIDIRECTIONAL) {
|
2012-02-14 10:45:09 -05:00
|
|
|
/* Note that we call on_stream_close_callback without opening
|
|
|
|
stream. */
|
|
|
|
if(session->callbacks.on_stream_close_callback) {
|
|
|
|
session->callbacks.on_stream_close_callback
|
|
|
|
(session, frame->syn_stream.stream_id, SPDYLAY_OK,
|
|
|
|
session->user_data);
|
|
|
|
}
|
2012-02-07 10:10:23 -05:00
|
|
|
}
|
2012-01-25 09:46:07 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-01-28 08:46:12 -05:00
|
|
|
r = spdylay_session_handle_invalid_stream
|
2012-02-14 10:45:09 -05:00
|
|
|
(session, frame->syn_stream.stream_id, SPDYLAY_SYN_STREAM, frame,
|
|
|
|
status_code);
|
2012-01-25 09:46:07 -05:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spdylay_session_on_syn_reply_received(spdylay_session *session,
|
|
|
|
spdylay_frame *frame)
|
|
|
|
{
|
|
|
|
int r = 0;
|
|
|
|
int valid = 0;
|
2012-03-11 09:42:22 -04:00
|
|
|
int status_code = SPDYLAY_PROTOCOL_ERROR;
|
2012-02-08 10:27:22 -05:00
|
|
|
spdylay_stream *stream;
|
2012-02-24 09:33:06 -05:00
|
|
|
if(!spdylay_session_check_version(session, frame->syn_reply.hd.version)) {
|
2012-02-08 10:27:22 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-15 08:11:42 -05:00
|
|
|
if((stream = spdylay_session_get_stream(session,
|
|
|
|
frame->syn_reply.stream_id)) &&
|
|
|
|
(stream->shut_flags & SPDYLAY_SHUT_RD) == 0 &&
|
|
|
|
spdylay_session_check_nv(frame->syn_reply.nv)) {
|
2012-01-28 10:08:51 -05:00
|
|
|
if(spdylay_session_is_my_stream_id(session, frame->syn_reply.stream_id)) {
|
2012-01-25 09:46:07 -05:00
|
|
|
if(stream->state == SPDYLAY_STREAM_OPENING) {
|
|
|
|
valid = 1;
|
|
|
|
stream->state = SPDYLAY_STREAM_OPENED;
|
|
|
|
spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_SYN_REPLY,
|
|
|
|
frame);
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->syn_reply.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2012-01-28 10:08:51 -05:00
|
|
|
/* This is the last frame of this stream, so disallow
|
|
|
|
further receptions. */
|
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
|
|
|
|
spdylay_session_close_stream_if_shut_rdwr(session, stream);
|
2012-01-27 03:15:24 -05:00
|
|
|
}
|
2012-01-25 09:46:07 -05:00
|
|
|
} else if(stream->state == SPDYLAY_STREAM_CLOSING) {
|
|
|
|
/* This is race condition. SPDYLAY_STREAM_CLOSING indicates
|
|
|
|
that we queued RST_STREAM but it has not been sent. It will
|
|
|
|
eventually sent, so we just ignore this frame. */
|
|
|
|
valid = 1;
|
2012-03-11 09:42:22 -04:00
|
|
|
} else {
|
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY3) {
|
|
|
|
/* SPDY/3 spec says if multiple SYN_REPLY frames for the
|
|
|
|
same active stream ID are received, the receiver must
|
|
|
|
issue a stream error with the status code
|
|
|
|
STREAM_IN_USE. */
|
|
|
|
status_code = SPDYLAY_STREAM_IN_USE;
|
|
|
|
}
|
2012-01-25 09:46:07 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!valid) {
|
2012-01-28 08:46:12 -05:00
|
|
|
r = spdylay_session_handle_invalid_stream
|
|
|
|
(session, frame->syn_reply.stream_id, SPDYLAY_SYN_REPLY, frame,
|
2012-03-11 09:42:22 -04:00
|
|
|
status_code);
|
2012-01-25 09:46:07 -05:00
|
|
|
}
|
|
|
|
return r;
|
2012-01-25 07:31:28 -05:00
|
|
|
}
|
|
|
|
|
2012-01-27 09:22:27 -05:00
|
|
|
int spdylay_session_on_rst_stream_received(spdylay_session *session,
|
|
|
|
spdylay_frame *frame)
|
|
|
|
{
|
2012-02-24 09:33:06 -05:00
|
|
|
if(!spdylay_session_check_version(session, frame->rst_stream.hd.version)) {
|
2012-02-08 10:27:22 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-02 09:19:01 -05:00
|
|
|
if(session->server &&
|
|
|
|
!spdylay_session_is_my_stream_id(session, frame->rst_stream.stream_id) &&
|
|
|
|
frame->rst_stream.status_code == SPDYLAY_CANCEL) {
|
|
|
|
spdylay_session_close_pushed_streams(session, frame->rst_stream.stream_id,
|
|
|
|
frame->rst_stream.status_code);
|
|
|
|
}
|
2012-01-29 09:00:33 -05:00
|
|
|
spdylay_session_close_stream(session, frame->rst_stream.stream_id,
|
|
|
|
frame->rst_stream.status_code);
|
2012-01-27 13:54:53 -05:00
|
|
|
return 0;
|
2012-01-27 09:22:27 -05:00
|
|
|
}
|
|
|
|
|
2012-03-09 10:10:11 -05:00
|
|
|
static void spdylay_update_initial_window_size_func(key_type key, void *value,
|
|
|
|
void *ptr)
|
|
|
|
{
|
|
|
|
int32_t *vals;
|
|
|
|
vals = (int32_t*)ptr;
|
|
|
|
spdylay_stream_update_initial_window_size((spdylay_stream*)value,
|
|
|
|
vals[0], vals[1]);
|
|
|
|
}
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Updates the initial window size of all active streams.
|
|
|
|
*/
|
|
|
|
static void spdylay_session_update_initial_window_size
|
|
|
|
(spdylay_session *session,
|
|
|
|
int32_t new_initial_window_size)
|
|
|
|
{
|
|
|
|
int32_t vals[2];
|
|
|
|
vals[0] = new_initial_window_size;
|
|
|
|
vals[1] = session->remote_settings[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE];
|
|
|
|
spdylay_map_each(&session->streams,
|
|
|
|
spdylay_update_initial_window_size_func,
|
|
|
|
vals);
|
|
|
|
}
|
|
|
|
|
2012-03-10 04:41:01 -05:00
|
|
|
void spdylay_session_update_local_settings(spdylay_session *session,
|
|
|
|
spdylay_settings_entry *iv,
|
|
|
|
size_t niv)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
for(i = 0; i < niv; ++i) {
|
|
|
|
assert(iv[i].settings_id > 0 && iv[i].settings_id <= SPDYLAY_SETTINGS_MAX);
|
|
|
|
session->local_settings[iv[i].settings_id] = iv[i].value;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-01-31 11:12:26 -05:00
|
|
|
int spdylay_session_on_settings_received(spdylay_session *session,
|
|
|
|
spdylay_frame *frame)
|
|
|
|
{
|
2012-03-09 10:10:11 -05:00
|
|
|
int i, check[SPDYLAY_SETTINGS_MAX+1];
|
2012-02-24 09:33:06 -05:00
|
|
|
if(!spdylay_session_check_version(session, frame->settings.hd.version)) {
|
2012-02-08 10:27:22 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-03-09 10:10:11 -05:00
|
|
|
/* Check ID/value pairs and persist them if necessary. */
|
|
|
|
memset(check, 0, sizeof(check));
|
|
|
|
for(i = 0; i < frame->settings.niv; ++i) {
|
|
|
|
const spdylay_settings_entry *entry = &frame->settings.iv[i];
|
|
|
|
/* SPDY/3 spec says if the multiple values for the same ID were
|
|
|
|
found, use the first one and ignore the rest. */
|
|
|
|
if(entry->settings_id > SPDYLAY_SETTINGS_MAX || entry->settings_id == 0 ||
|
|
|
|
check[entry->settings_id] == 1) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
check[entry->settings_id] = 1;
|
|
|
|
if(entry->settings_id == SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE &&
|
|
|
|
session->flow_control) {
|
|
|
|
/* Update the initial window size of the all active streams */
|
|
|
|
/* Check that initial_window_size < (1u << 31) */
|
|
|
|
if(entry->value < (1u << 31)) {
|
|
|
|
spdylay_session_update_initial_window_size(session, entry->value);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
session->remote_settings[entry->settings_id] = entry->value;
|
|
|
|
}
|
2012-01-31 11:12:26 -05:00
|
|
|
spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_SETTINGS, frame);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-27 09:05:29 -05:00
|
|
|
int spdylay_session_on_ping_received(spdylay_session *session,
|
|
|
|
spdylay_frame *frame)
|
|
|
|
{
|
|
|
|
int r = 0;
|
2012-02-24 09:33:06 -05:00
|
|
|
if(!spdylay_session_check_version(session, frame->ping.hd.version)) {
|
2012-02-08 10:27:22 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-01-27 09:05:29 -05:00
|
|
|
if(frame->ping.unique_id != 0) {
|
|
|
|
if(session->last_ping_unique_id == frame->ping.unique_id) {
|
|
|
|
/* This is ping reply from peer */
|
|
|
|
/* Assign 0 to last_ping_unique_id so that we can ignore same
|
|
|
|
ID. */
|
|
|
|
session->last_ping_unique_id = 0;
|
|
|
|
spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_PING, frame);
|
|
|
|
} else if((session->server && frame->ping.unique_id % 2 == 1) ||
|
|
|
|
(!session->server && frame->ping.unique_id % 2 == 0)) {
|
|
|
|
/* Peer sent ping, so ping it back */
|
|
|
|
r = spdylay_session_add_ping(session, frame->ping.unique_id);
|
|
|
|
spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_PING, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-01-28 05:22:38 -05:00
|
|
|
int spdylay_session_on_goaway_received(spdylay_session *session,
|
|
|
|
spdylay_frame *frame)
|
|
|
|
{
|
2012-02-24 09:33:06 -05:00
|
|
|
if(!spdylay_session_check_version(session, frame->goaway.hd.version)) {
|
2012-02-08 10:27:22 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-01-28 05:22:38 -05:00
|
|
|
session->last_good_stream_id = frame->goaway.last_good_stream_id;
|
|
|
|
session->goaway_flags |= SPDYLAY_GOAWAY_RECV;
|
|
|
|
spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_GOAWAY, frame);
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:12:32 -05:00
|
|
|
int spdylay_session_on_window_update_received(spdylay_session *session,
|
|
|
|
spdylay_frame *frame)
|
|
|
|
{
|
|
|
|
spdylay_stream *stream;
|
|
|
|
if(!spdylay_session_check_version(session, frame->window_update.hd.version)) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
if(!session->flow_control) {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
stream = spdylay_session_get_stream(session, frame->window_update.stream_id);
|
|
|
|
if(stream) {
|
|
|
|
if(INT32_MAX-frame->window_update.delta_window_size < stream->window_size) {
|
|
|
|
int r;
|
|
|
|
r = spdylay_session_handle_invalid_stream
|
|
|
|
(session, frame->window_update.stream_id, SPDYLAY_WINDOW_UPDATE, frame,
|
|
|
|
SPDYLAY_FLOW_CONTROL_ERROR);
|
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
stream->window_size += frame->window_update.delta_window_size;
|
|
|
|
if(stream->deferred_data != NULL &&
|
|
|
|
(stream->deferred_flags & SPDYLAY_DEFERRED_FLOW_CONTROL)) {
|
|
|
|
int r;
|
|
|
|
r = spdylay_pq_push(&session->ob_pq, stream->deferred_data);
|
|
|
|
if(r == 0) {
|
|
|
|
spdylay_stream_detach_deferred_data(stream);
|
|
|
|
} else if(r < 0) {
|
|
|
|
/* FATAL */
|
|
|
|
assert(r < SPDYLAY_ERR_FATAL);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
spdylay_session_call_on_ctrl_frame_received(session,
|
|
|
|
SPDYLAY_WINDOW_UPDATE, frame);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-01-27 05:10:13 -05:00
|
|
|
int spdylay_session_on_headers_received(spdylay_session *session,
|
|
|
|
spdylay_frame *frame)
|
|
|
|
{
|
|
|
|
int r = 0;
|
|
|
|
int valid = 0;
|
2012-02-08 10:27:22 -05:00
|
|
|
spdylay_stream *stream;
|
2012-02-24 09:33:06 -05:00
|
|
|
if(!spdylay_session_check_version(session, frame->headers.hd.version)) {
|
2012-02-08 10:27:22 -05:00
|
|
|
return 0;
|
|
|
|
}
|
2012-02-15 08:11:42 -05:00
|
|
|
if((stream = spdylay_session_get_stream(session,
|
|
|
|
frame->headers.stream_id)) &&
|
|
|
|
(stream->shut_flags & SPDYLAY_SHUT_RD) == 0 &&
|
|
|
|
spdylay_session_check_nv(frame->headers.nv)) {
|
2012-01-28 10:08:51 -05:00
|
|
|
if(spdylay_session_is_my_stream_id(session, frame->headers.stream_id)) {
|
2012-01-29 02:27:00 -05:00
|
|
|
if(stream->state == SPDYLAY_STREAM_OPENED) {
|
2012-01-27 05:10:13 -05:00
|
|
|
valid = 1;
|
|
|
|
spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_HEADERS,
|
|
|
|
frame);
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->headers.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2012-01-28 10:08:51 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
|
|
|
|
spdylay_session_close_stream_if_shut_rdwr(session, stream);
|
2012-01-27 05:10:13 -05:00
|
|
|
}
|
|
|
|
} else if(stream->state == SPDYLAY_STREAM_CLOSING) {
|
|
|
|
/* This is race condition. SPDYLAY_STREAM_CLOSING indicates
|
|
|
|
that we queued RST_STREAM but it has not been sent. It will
|
|
|
|
eventually sent, so we just ignore this frame. */
|
|
|
|
valid = 1;
|
|
|
|
}
|
2012-01-28 10:08:51 -05:00
|
|
|
} else {
|
|
|
|
/* If this is remote peer initiated stream, it is OK unless it
|
2012-01-29 02:27:00 -05:00
|
|
|
have sent FIN frame already. But if stream is in
|
|
|
|
SPDYLAY_STREAM_CLOSING, we discard the frame. This is a race
|
|
|
|
condition. */
|
2012-01-28 10:08:51 -05:00
|
|
|
valid = 1;
|
2012-01-29 02:27:00 -05:00
|
|
|
if(stream->state != SPDYLAY_STREAM_CLOSING) {
|
|
|
|
spdylay_session_call_on_ctrl_frame_received(session, SPDYLAY_HEADERS,
|
|
|
|
frame);
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->headers.hd.flags & SPDYLAY_CTRL_FLAG_FIN) {
|
2012-02-07 10:10:23 -05:00
|
|
|
spdylay_session_call_on_request_recv(session,
|
|
|
|
frame->headers.stream_id);
|
2012-01-29 02:27:00 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
|
|
|
|
spdylay_session_close_stream_if_shut_rdwr(session, stream);
|
|
|
|
}
|
2012-01-27 05:10:13 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if(!valid) {
|
2012-01-28 08:46:12 -05:00
|
|
|
r = spdylay_session_handle_invalid_stream
|
|
|
|
(session, frame->headers.stream_id, SPDYLAY_HEADERS, frame,
|
|
|
|
SPDYLAY_PROTOCOL_ERROR);
|
2012-01-27 05:10:13 -05:00
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-02-23 08:49:08 -05:00
|
|
|
/* For errors, this function only returns FATAL error. */
|
2012-01-28 08:46:12 -05:00
|
|
|
static int spdylay_session_process_ctrl_frame(spdylay_session *session)
|
2012-01-25 07:31:28 -05:00
|
|
|
{
|
|
|
|
int r = 0;
|
2012-01-24 08:02:24 -05:00
|
|
|
uint16_t type;
|
2012-01-25 07:31:28 -05:00
|
|
|
spdylay_frame frame;
|
2012-01-24 08:02:24 -05:00
|
|
|
memcpy(&type, &session->iframe.headbuf[2], sizeof(uint16_t));
|
|
|
|
type = ntohs(type);
|
|
|
|
switch(type) {
|
2012-01-25 07:31:28 -05:00
|
|
|
case SPDYLAY_SYN_STREAM:
|
2012-02-16 08:01:34 -05:00
|
|
|
spdylay_buffer_reset(&session->inflatebuf);
|
2012-01-25 07:31:28 -05:00
|
|
|
r = spdylay_frame_unpack_syn_stream(&frame.syn_stream,
|
2012-02-16 08:01:34 -05:00
|
|
|
&session->inflatebuf,
|
|
|
|
&session->nvbuf,
|
|
|
|
&session->nvbuflen,
|
2012-01-25 07:31:28 -05:00
|
|
|
session->iframe.headbuf,
|
2012-01-24 08:02:24 -05:00
|
|
|
sizeof(session->iframe.headbuf),
|
|
|
|
session->iframe.buf,
|
|
|
|
session->iframe.len,
|
|
|
|
&session->hd_inflater);
|
|
|
|
if(r == 0) {
|
2012-03-06 10:42:47 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_2to3(frame.syn_stream.nv);
|
|
|
|
}
|
2012-01-25 09:46:07 -05:00
|
|
|
r = spdylay_session_on_syn_stream_received(session, &frame);
|
2012-01-25 07:31:28 -05:00
|
|
|
spdylay_frame_syn_stream_free(&frame.syn_stream);
|
|
|
|
/* TODO if r indicates mulformed NV pairs (multiple nulls) or
|
|
|
|
invalid frame, send RST_STREAM with PROTOCOL_ERROR. Same for
|
|
|
|
other control frames. */
|
2012-02-18 07:55:40 -05:00
|
|
|
} else if(spdylay_is_non_fatal(r)) {
|
2012-02-26 02:26:38 -05:00
|
|
|
r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-01-25 07:31:28 -05:00
|
|
|
case SPDYLAY_SYN_REPLY:
|
2012-02-16 08:01:34 -05:00
|
|
|
spdylay_buffer_reset(&session->inflatebuf);
|
2012-01-25 07:31:28 -05:00
|
|
|
r = spdylay_frame_unpack_syn_reply(&frame.syn_reply,
|
2012-02-16 08:01:34 -05:00
|
|
|
&session->inflatebuf,
|
|
|
|
&session->nvbuf,
|
|
|
|
&session->nvbuflen,
|
2012-01-25 07:31:28 -05:00
|
|
|
session->iframe.headbuf,
|
2012-01-24 08:02:24 -05:00
|
|
|
sizeof(session->iframe.headbuf),
|
|
|
|
session->iframe.buf,
|
|
|
|
session->iframe.len,
|
|
|
|
&session->hd_inflater);
|
|
|
|
if(r == 0) {
|
2012-03-06 10:42:47 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_2to3(frame.syn_reply.nv);
|
|
|
|
}
|
2012-01-25 09:46:07 -05:00
|
|
|
r = spdylay_session_on_syn_reply_received(session, &frame);
|
2012-01-25 07:31:28 -05:00
|
|
|
spdylay_frame_syn_reply_free(&frame.syn_reply);
|
2012-02-18 07:55:40 -05:00
|
|
|
} else if(spdylay_is_non_fatal(r)) {
|
2012-02-26 02:26:38 -05:00
|
|
|
r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-01-27 09:22:27 -05:00
|
|
|
case SPDYLAY_RST_STREAM:
|
|
|
|
r = spdylay_frame_unpack_rst_stream(&frame.rst_stream,
|
|
|
|
session->iframe.headbuf,
|
|
|
|
sizeof(session->iframe.headbuf),
|
|
|
|
session->iframe.buf,
|
|
|
|
session->iframe.len);
|
|
|
|
if(r == 0) {
|
|
|
|
r = spdylay_session_on_rst_stream_received(session, &frame);
|
|
|
|
spdylay_frame_rst_stream_free(&frame.rst_stream);
|
2012-02-18 07:55:40 -05:00
|
|
|
} else if(spdylay_is_non_fatal(r)) {
|
2012-02-26 02:26:38 -05:00
|
|
|
r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
2012-01-27 09:22:27 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-01-31 11:12:26 -05:00
|
|
|
case SPDYLAY_SETTINGS:
|
|
|
|
r = spdylay_frame_unpack_settings(&frame.settings,
|
|
|
|
session->iframe.headbuf,
|
|
|
|
sizeof(session->iframe.headbuf),
|
|
|
|
session->iframe.buf,
|
|
|
|
session->iframe.len);
|
|
|
|
if(r == 0) {
|
|
|
|
r = spdylay_session_on_settings_received(session, &frame);
|
|
|
|
spdylay_frame_settings_free(&frame.settings);
|
2012-02-18 07:55:40 -05:00
|
|
|
} else if(spdylay_is_non_fatal(r)) {
|
2012-02-26 02:26:38 -05:00
|
|
|
r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
2012-01-31 11:12:26 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-01-28 03:29:22 -05:00
|
|
|
case SPDYLAY_NOOP:
|
|
|
|
break;
|
2012-01-27 09:05:29 -05:00
|
|
|
case SPDYLAY_PING:
|
|
|
|
r = spdylay_frame_unpack_ping(&frame.ping,
|
|
|
|
session->iframe.headbuf,
|
|
|
|
sizeof(session->iframe.headbuf),
|
|
|
|
session->iframe.buf,
|
|
|
|
session->iframe.len);
|
|
|
|
if(r == 0) {
|
|
|
|
r = spdylay_session_on_ping_received(session, &frame);
|
|
|
|
spdylay_frame_ping_free(&frame.ping);
|
2012-02-18 07:55:40 -05:00
|
|
|
} else if(spdylay_is_non_fatal(r)) {
|
2012-02-26 02:26:38 -05:00
|
|
|
r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
2012-01-27 09:05:29 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-01-28 05:22:38 -05:00
|
|
|
case SPDYLAY_GOAWAY:
|
|
|
|
r = spdylay_frame_unpack_goaway(&frame.goaway,
|
|
|
|
session->iframe.headbuf,
|
|
|
|
sizeof(session->iframe.headbuf),
|
|
|
|
session->iframe.buf,
|
|
|
|
session->iframe.len);
|
|
|
|
if(r == 0) {
|
|
|
|
r = spdylay_session_on_goaway_received(session, &frame);
|
|
|
|
spdylay_frame_goaway_free(&frame.goaway);
|
2012-02-18 07:55:40 -05:00
|
|
|
} else if(spdylay_is_non_fatal(r)) {
|
2012-02-26 02:26:38 -05:00
|
|
|
r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
2012-01-28 05:22:38 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-01-27 05:10:13 -05:00
|
|
|
case SPDYLAY_HEADERS:
|
2012-02-16 08:01:34 -05:00
|
|
|
spdylay_buffer_reset(&session->inflatebuf);
|
2012-01-27 05:10:13 -05:00
|
|
|
r = spdylay_frame_unpack_headers(&frame.headers,
|
2012-02-16 08:01:34 -05:00
|
|
|
&session->inflatebuf,
|
|
|
|
&session->nvbuf,
|
|
|
|
&session->nvbuflen,
|
2012-01-27 05:10:13 -05:00
|
|
|
session->iframe.headbuf,
|
|
|
|
sizeof(session->iframe.headbuf),
|
|
|
|
session->iframe.buf,
|
|
|
|
session->iframe.len,
|
|
|
|
&session->hd_inflater);
|
|
|
|
if(r == 0) {
|
2012-03-06 10:42:47 -05:00
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
spdylay_frame_nv_2to3(frame.headers.nv);
|
|
|
|
}
|
2012-01-27 05:10:13 -05:00
|
|
|
r = spdylay_session_on_headers_received(session, &frame);
|
|
|
|
spdylay_frame_headers_free(&frame.headers);
|
2012-02-18 07:55:40 -05:00
|
|
|
} else if(spdylay_is_non_fatal(r)) {
|
2012-02-26 02:26:38 -05:00
|
|
|
r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
2012-01-27 05:10:13 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-02-25 10:12:32 -05:00
|
|
|
case SPDYLAY_WINDOW_UPDATE:
|
|
|
|
r = spdylay_frame_unpack_window_update(&frame.window_update,
|
|
|
|
session->iframe.headbuf,
|
|
|
|
sizeof(session->iframe.headbuf),
|
|
|
|
session->iframe.buf,
|
|
|
|
session->iframe.len);
|
|
|
|
if(r == 0) {
|
|
|
|
r = spdylay_session_on_window_update_received(session, &frame);
|
|
|
|
spdylay_frame_window_update_free(&frame.window_update);
|
|
|
|
} else if(spdylay_is_non_fatal(r)) {
|
2012-02-26 02:26:38 -05:00
|
|
|
r = spdylay_session_fail_session(session, SPDYLAY_GOAWAY_PROTOCOL_ERROR);
|
2012-02-25 10:12:32 -05:00
|
|
|
}
|
|
|
|
break;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-03-07 10:40:17 -05:00
|
|
|
if(spdylay_is_fatal(r)) {
|
2012-01-25 07:31:28 -05:00
|
|
|
return r;
|
2012-01-25 09:46:07 -05:00
|
|
|
} else {
|
|
|
|
return 0;
|
2012-01-25 07:31:28 -05:00
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
|
2012-01-29 02:27:00 -05:00
|
|
|
int spdylay_session_on_data_received(spdylay_session *session,
|
|
|
|
uint8_t flags, int32_t length,
|
|
|
|
int32_t stream_id)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
2012-01-28 08:46:12 -05:00
|
|
|
int r = 0;
|
2012-01-29 02:27:00 -05:00
|
|
|
spdylay_status_code status_code = 0;
|
|
|
|
spdylay_stream *stream;
|
2012-01-28 08:46:12 -05:00
|
|
|
stream = spdylay_session_get_stream(session, stream_id);
|
|
|
|
if(stream) {
|
2012-01-28 10:08:51 -05:00
|
|
|
if((stream->shut_flags & SPDYLAY_SHUT_RD) == 0) {
|
2012-02-07 10:10:23 -05:00
|
|
|
int valid = 0;
|
2012-01-28 10:08:51 -05:00
|
|
|
if(spdylay_session_is_my_stream_id(session, stream_id)) {
|
|
|
|
if(stream->state == SPDYLAY_STREAM_OPENED) {
|
|
|
|
valid = 1;
|
2012-02-07 10:10:23 -05:00
|
|
|
if(session->callbacks.on_data_recv_callback) {
|
|
|
|
session->callbacks.on_data_recv_callback
|
|
|
|
(session, flags, stream_id, length, session->user_data);
|
|
|
|
}
|
2012-01-28 10:08:51 -05:00
|
|
|
} else if(stream->state != SPDYLAY_STREAM_CLOSING) {
|
|
|
|
status_code = SPDYLAY_PROTOCOL_ERROR;
|
|
|
|
}
|
2012-01-29 02:27:00 -05:00
|
|
|
} else if(stream->state != SPDYLAY_STREAM_CLOSING) {
|
2012-01-28 10:08:51 -05:00
|
|
|
/* It is OK if this is remote peer initiated stream and we did
|
2012-01-29 02:27:00 -05:00
|
|
|
not receive FIN unless stream is in SPDYLAY_STREAM_CLOSING
|
|
|
|
state. This is a race condition. */
|
2012-01-28 08:46:12 -05:00
|
|
|
valid = 1;
|
2012-02-07 10:10:23 -05:00
|
|
|
if(session->callbacks.on_data_recv_callback) {
|
|
|
|
session->callbacks.on_data_recv_callback
|
|
|
|
(session, flags, stream_id, length, session->user_data);
|
|
|
|
}
|
2012-02-24 07:40:13 -05:00
|
|
|
if(flags & SPDYLAY_DATA_FLAG_FIN) {
|
2012-02-07 10:10:23 -05:00
|
|
|
spdylay_session_call_on_request_recv(session, stream_id);
|
|
|
|
}
|
2012-01-28 08:46:12 -05:00
|
|
|
}
|
2012-01-29 02:27:00 -05:00
|
|
|
if(valid) {
|
2012-02-24 07:40:13 -05:00
|
|
|
if(flags & SPDYLAY_DATA_FLAG_FIN) {
|
2012-01-29 02:27:00 -05:00
|
|
|
spdylay_stream_shutdown(stream, SPDYLAY_SHUT_RD);
|
|
|
|
spdylay_session_close_stream_if_shut_rdwr(session, stream);
|
|
|
|
}
|
|
|
|
}
|
2012-01-28 08:46:12 -05:00
|
|
|
} else {
|
2012-01-28 10:08:51 -05:00
|
|
|
status_code = SPDYLAY_PROTOCOL_ERROR;
|
2012-01-28 08:46:12 -05:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
status_code = SPDYLAY_INVALID_STREAM;
|
|
|
|
}
|
2012-02-07 10:10:23 -05:00
|
|
|
if(status_code != 0) {
|
2012-01-28 08:46:12 -05:00
|
|
|
r = spdylay_session_add_rst_stream(session, stream_id, status_code);
|
|
|
|
}
|
2012-01-29 02:27:00 -05:00
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-02-23 08:49:08 -05:00
|
|
|
/* For errors, this function only returns FATAL error. */
|
2012-01-29 02:27:00 -05:00
|
|
|
static int spdylay_session_process_data_frame(spdylay_session *session)
|
|
|
|
{
|
|
|
|
uint8_t flags;
|
|
|
|
int32_t length;
|
|
|
|
int32_t stream_id;
|
|
|
|
int r;
|
|
|
|
stream_id = spdylay_get_uint32(session->iframe.headbuf) &
|
|
|
|
SPDYLAY_STREAM_ID_MASK;
|
|
|
|
flags = session->iframe.headbuf[4];
|
|
|
|
length = spdylay_get_uint32(&session->iframe.headbuf[4]) &
|
|
|
|
SPDYLAY_LENGTH_MASK;
|
|
|
|
r = spdylay_session_on_data_received(session, flags, length, stream_id);
|
2012-03-07 10:40:17 -05:00
|
|
|
if(spdylay_is_fatal(r)) {
|
2012-01-28 08:46:12 -05:00
|
|
|
return r;
|
|
|
|
} else {
|
|
|
|
return 0;
|
2012-01-27 13:54:53 -05:00
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
|
2012-02-25 10:12:32 -05:00
|
|
|
/*
|
|
|
|
* Accumulates received bytes |delta_size| and decides whether to send
|
|
|
|
* WINDOW_UPDATE.
|
|
|
|
*
|
|
|
|
* This function returns 0 if it succeeds, or one of the following
|
|
|
|
* negative error codes:
|
|
|
|
*
|
|
|
|
* SPDYLAY_ERR_NOMEM
|
|
|
|
* Out of memory.
|
|
|
|
*/
|
|
|
|
static int spdylay_session_update_recv_window_size(spdylay_session *session,
|
|
|
|
int32_t stream_id,
|
|
|
|
int32_t delta_size)
|
|
|
|
{
|
|
|
|
spdylay_stream *stream;
|
|
|
|
stream = spdylay_session_get_stream(session, stream_id);
|
|
|
|
if(stream) {
|
|
|
|
stream->recv_window_size += delta_size;
|
|
|
|
/* This is just a heuristics. */
|
|
|
|
if(stream->recv_window_size*2 >=
|
2012-03-09 10:10:11 -05:00
|
|
|
session->remote_settings[SPDYLAY_SETTINGS_INITIAL_WINDOW_SIZE]) {
|
2012-02-25 10:12:32 -05:00
|
|
|
int r;
|
|
|
|
r = spdylay_session_add_window_update(session, stream_id,
|
|
|
|
stream->recv_window_size);
|
|
|
|
if(r == 0) {
|
|
|
|
stream->recv_window_size = 0;
|
|
|
|
} else {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2012-03-17 10:39:38 -04:00
|
|
|
ssize_t spdylay_session_mem_recv(spdylay_session *session,
|
|
|
|
const uint8_t *in, size_t inlen)
|
2012-01-24 08:02:24 -05:00
|
|
|
{
|
2012-03-17 10:39:38 -04:00
|
|
|
const uint8_t *inmark, *inlimit;
|
|
|
|
inmark = in;
|
|
|
|
inlimit = in+inlen;
|
2012-01-24 08:02:24 -05:00
|
|
|
while(1) {
|
|
|
|
ssize_t r;
|
|
|
|
if(session->iframe.state == SPDYLAY_RECV_HEAD) {
|
|
|
|
uint32_t payloadlen;
|
2012-03-17 10:39:38 -04:00
|
|
|
size_t remheadbytes;
|
|
|
|
size_t readlen;
|
|
|
|
size_t bufavail = inlimit-inmark;
|
|
|
|
if(bufavail == 0) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
remheadbytes = SPDYLAY_HEAD_LEN-session->iframe.headbufoff;
|
|
|
|
readlen = spdylay_min(remheadbytes, bufavail);
|
|
|
|
memcpy(session->iframe.headbuf+session->iframe.headbufoff,
|
|
|
|
inmark, readlen);
|
|
|
|
inmark += readlen;
|
|
|
|
session->iframe.headbufoff += readlen;
|
|
|
|
if(session->iframe.headbufoff == SPDYLAY_HEAD_LEN) {
|
|
|
|
session->iframe.state = SPDYLAY_RECV_PAYLOAD;
|
|
|
|
payloadlen = spdylay_get_uint32(&session->iframe.headbuf[4]) &
|
|
|
|
SPDYLAY_LENGTH_MASK;
|
|
|
|
if(spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
|
|
|
|
/* control frame */
|
|
|
|
session->iframe.len = payloadlen;
|
|
|
|
r = spdylay_reserve_buffer(&session->iframe.buf,
|
|
|
|
&session->iframe.bufmax,
|
|
|
|
session->iframe.len);
|
|
|
|
if(r != 0) {
|
|
|
|
/* FATAL */
|
|
|
|
assert(r < SPDYLAY_ERR_FATAL);
|
2012-01-24 08:02:24 -05:00
|
|
|
return r;
|
|
|
|
}
|
2012-03-17 10:39:38 -04:00
|
|
|
session->iframe.off = 0;
|
|
|
|
} else {
|
|
|
|
session->iframe.len = payloadlen;
|
|
|
|
session->iframe.off = 0;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
} else {
|
2012-03-17 10:39:38 -04:00
|
|
|
break;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
}
|
|
|
|
if(session->iframe.state == SPDYLAY_RECV_PAYLOAD) {
|
|
|
|
size_t rempayloadlen = session->iframe.len - session->iframe.off;
|
|
|
|
size_t bufavail, readlen;
|
2012-02-25 10:12:32 -05:00
|
|
|
int32_t data_stream_id = 0;
|
|
|
|
uint8_t data_flags = SPDYLAY_DATA_FLAG_NONE;
|
2012-03-17 10:39:38 -04:00
|
|
|
bufavail = inlimit-inmark;
|
|
|
|
if(rempayloadlen > 0 && bufavail == 0) {
|
|
|
|
break;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-03-17 10:39:38 -04:00
|
|
|
readlen = spdylay_min(bufavail, rempayloadlen);
|
2012-01-27 13:54:53 -05:00
|
|
|
if(spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
|
2012-03-17 10:39:38 -04:00
|
|
|
memcpy(session->iframe.buf+session->iframe.off, inmark, readlen);
|
2012-02-25 10:12:32 -05:00
|
|
|
} else {
|
2012-01-27 13:54:53 -05:00
|
|
|
/* For data frame, We don't buffer data. Instead, just pass
|
|
|
|
received data to callback function. */
|
2012-02-25 10:12:32 -05:00
|
|
|
data_stream_id = spdylay_get_uint32(session->iframe.headbuf) &
|
2012-01-27 13:54:53 -05:00
|
|
|
SPDYLAY_STREAM_ID_MASK;
|
2012-02-25 10:12:32 -05:00
|
|
|
data_flags = session->iframe.headbuf[4];
|
|
|
|
if(session->callbacks.on_data_chunk_recv_callback) {
|
|
|
|
session->callbacks.on_data_chunk_recv_callback(session,
|
|
|
|
data_flags,
|
|
|
|
data_stream_id,
|
2012-03-17 10:39:38 -04:00
|
|
|
inmark,
|
2012-02-25 10:12:32 -05:00
|
|
|
readlen,
|
|
|
|
session->user_data);
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
session->iframe.off += readlen;
|
2012-03-17 10:39:38 -04:00
|
|
|
inmark += readlen;
|
2012-02-25 10:12:32 -05:00
|
|
|
|
|
|
|
if(session->flow_control &&
|
|
|
|
!spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
|
|
|
|
if(readlen > 0 &&
|
|
|
|
(session->iframe.len != session->iframe.off ||
|
|
|
|
(data_flags & SPDYLAY_DATA_FLAG_FIN) == 0)) {
|
|
|
|
r = spdylay_session_update_recv_window_size(session,
|
|
|
|
data_stream_id,
|
|
|
|
readlen);
|
|
|
|
if(r < 0) {
|
|
|
|
/* FATAL */
|
|
|
|
assert(r < SPDYLAY_ERR_FATAL);
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
if(session->iframe.len == session->iframe.off) {
|
|
|
|
if(spdylay_frame_is_ctrl_frame(session->iframe.headbuf[0])) {
|
2012-01-25 09:46:07 -05:00
|
|
|
r = spdylay_session_process_ctrl_frame(session);
|
2012-01-24 08:02:24 -05:00
|
|
|
} else {
|
2012-01-28 08:46:12 -05:00
|
|
|
r = spdylay_session_process_data_frame(session);
|
|
|
|
}
|
|
|
|
if(r < 0) {
|
2012-02-23 08:49:08 -05:00
|
|
|
/* FATAL */
|
|
|
|
assert(r < SPDYLAY_ERR_FATAL);
|
2012-01-28 08:46:12 -05:00
|
|
|
return r;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
spdylay_inbound_frame_reset(&session->iframe);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2012-03-17 10:39:38 -04:00
|
|
|
return inmark-in;
|
|
|
|
}
|
|
|
|
|
|
|
|
int spdylay_session_recv(spdylay_session *session)
|
|
|
|
{
|
|
|
|
uint8_t buf[SPDYLAY_INBOUND_BUFFER_LENGTH];
|
|
|
|
while(1) {
|
|
|
|
ssize_t readlen;
|
|
|
|
readlen = spdylay_recv(session, buf, sizeof(buf));
|
|
|
|
if(readlen > 0) {
|
|
|
|
ssize_t proclen = spdylay_session_mem_recv(session, buf, readlen);
|
|
|
|
if(proclen < 0) {
|
|
|
|
return proclen;
|
|
|
|
}
|
|
|
|
assert(proclen == readlen);
|
|
|
|
} else if(readlen == 0 || readlen == SPDYLAY_ERR_WOULDBLOCK) {
|
|
|
|
return 0;
|
|
|
|
} else if(readlen == SPDYLAY_ERR_EOF) {
|
|
|
|
return readlen;
|
|
|
|
} else if(readlen < 0) {
|
|
|
|
return SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
|
|
|
}
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int spdylay_session_want_read(spdylay_session *session)
|
|
|
|
{
|
2012-02-18 07:55:40 -05:00
|
|
|
/* If these flags are set, we don't want to read. The application
|
|
|
|
should drop the connection. */
|
|
|
|
if((session->goaway_flags & SPDYLAY_GOAWAY_FAIL_ON_SEND) &&
|
|
|
|
(session->goaway_flags & SPDYLAY_GOAWAY_SEND)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-29 01:11:10 -05:00
|
|
|
/* Unless GOAWAY is sent or received, we always want to read
|
2012-01-28 05:22:38 -05:00
|
|
|
incoming frames. After GOAWAY is sent or received, we are only
|
2012-01-29 01:11:10 -05:00
|
|
|
interested in active streams. */
|
|
|
|
return !session->goaway_flags || spdylay_map_size(&session->streams) > 0;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
int spdylay_session_want_write(spdylay_session *session)
|
|
|
|
{
|
2012-02-18 07:55:40 -05:00
|
|
|
/* If these flags are set, we don't want to write any data. The
|
|
|
|
application should drop the connection. */
|
|
|
|
if((session->goaway_flags & SPDYLAY_GOAWAY_FAIL_ON_SEND) &&
|
|
|
|
(session->goaway_flags & SPDYLAY_GOAWAY_SEND)) {
|
|
|
|
return 0;
|
|
|
|
}
|
2012-01-29 01:11:10 -05:00
|
|
|
/*
|
|
|
|
* Unless GOAWAY is sent or received, we want to write frames if
|
2012-02-05 10:14:19 -05:00
|
|
|
* there is pending ones. If pending frame is SYN_STREAM and
|
|
|
|
* concurrent stream limit is reached, we don't want to write
|
|
|
|
* SYN_STREAM. After GOAWAY is sent or received, we want to write
|
|
|
|
* frames if there is pending ones AND there are active frames.
|
2012-01-29 01:11:10 -05:00
|
|
|
*/
|
2012-02-05 10:14:19 -05:00
|
|
|
return (session->aob.item != NULL || !spdylay_pq_empty(&session->ob_pq) ||
|
|
|
|
(!spdylay_pq_empty(&session->ob_ss_pq) &&
|
2012-02-08 09:45:48 -05:00
|
|
|
!spdylay_session_get_max_concurrent_streams_reached(session))) &&
|
2012-02-05 10:14:19 -05:00
|
|
|
(!session->goaway_flags || spdylay_map_size(&session->streams) > 0);
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
|
|
|
|
2012-01-27 09:05:29 -05:00
|
|
|
int spdylay_session_add_ping(spdylay_session *session, uint32_t unique_id)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
spdylay_frame *frame;
|
|
|
|
frame = malloc(sizeof(spdylay_frame));
|
|
|
|
if(frame == NULL) {
|
|
|
|
return SPDYLAY_ERR_NOMEM;
|
|
|
|
}
|
2012-02-24 09:33:06 -05:00
|
|
|
spdylay_frame_ping_init(&frame->ping, session->version, unique_id);
|
2012-01-29 05:07:31 -05:00
|
|
|
r = spdylay_session_add_frame(session, SPDYLAY_PING, frame, NULL);
|
2012-01-27 09:05:29 -05:00
|
|
|
if(r != 0) {
|
|
|
|
spdylay_frame_ping_free(&frame->ping);
|
|
|
|
free(frame);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-01-28 05:22:38 -05:00
|
|
|
int spdylay_session_add_goaway(spdylay_session *session,
|
2012-02-26 02:26:38 -05:00
|
|
|
int32_t last_good_stream_id,
|
|
|
|
uint32_t status_code)
|
2012-01-28 05:22:38 -05:00
|
|
|
{
|
|
|
|
int r;
|
|
|
|
spdylay_frame *frame;
|
|
|
|
frame = malloc(sizeof(spdylay_frame));
|
|
|
|
if(frame == NULL) {
|
|
|
|
return SPDYLAY_ERR_NOMEM;
|
|
|
|
}
|
2012-02-24 09:33:06 -05:00
|
|
|
spdylay_frame_goaway_init(&frame->goaway, session->version,
|
2012-02-26 02:26:38 -05:00
|
|
|
last_good_stream_id, status_code);
|
2012-01-29 05:07:31 -05:00
|
|
|
r = spdylay_session_add_frame(session, SPDYLAY_GOAWAY, frame, NULL);
|
2012-01-28 05:22:38 -05:00
|
|
|
if(r != 0) {
|
|
|
|
spdylay_frame_goaway_free(&frame->goaway);
|
|
|
|
free(frame);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-02-25 10:12:32 -05:00
|
|
|
int spdylay_session_add_window_update(spdylay_session *session,
|
|
|
|
int32_t stream_id,
|
|
|
|
int32_t delta_window_size)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
spdylay_frame *frame;
|
|
|
|
frame = malloc(sizeof(spdylay_frame));
|
|
|
|
if(frame == NULL) {
|
|
|
|
return SPDYLAY_ERR_NOMEM;
|
|
|
|
}
|
|
|
|
spdylay_frame_window_update_init(&frame->window_update, session->version,
|
|
|
|
stream_id, delta_window_size);
|
|
|
|
r = spdylay_session_add_frame(session, SPDYLAY_WINDOW_UPDATE, frame, NULL);
|
|
|
|
if(r != 0) {
|
|
|
|
spdylay_frame_window_update_free(&frame->window_update);
|
|
|
|
free(frame);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2012-01-26 11:17:40 -05:00
|
|
|
ssize_t spdylay_session_pack_data(spdylay_session *session,
|
2012-02-16 06:54:30 -05:00
|
|
|
uint8_t **buf_ptr, size_t *buflen_ptr,
|
2012-02-22 09:58:33 -05:00
|
|
|
size_t datamax,
|
2012-02-16 06:54:30 -05:00
|
|
|
spdylay_data *frame)
|
2012-01-26 11:17:40 -05:00
|
|
|
{
|
2012-02-22 09:58:33 -05:00
|
|
|
ssize_t framelen = datamax+8, r;
|
|
|
|
int eof;
|
|
|
|
uint8_t flags;
|
2012-02-16 06:54:30 -05:00
|
|
|
r = spdylay_reserve_buffer(buf_ptr, buflen_ptr, framelen);
|
|
|
|
if(r != 0) {
|
|
|
|
return r;
|
2012-01-26 11:17:40 -05:00
|
|
|
}
|
2012-02-22 09:58:33 -05:00
|
|
|
eof = 0;
|
2012-01-26 11:17:40 -05:00
|
|
|
r = frame->data_prd.read_callback
|
2012-02-22 09:58:33 -05:00
|
|
|
(session, frame->stream_id, (*buf_ptr)+8, datamax,
|
|
|
|
&eof, &frame->data_prd.source, session->user_data);
|
2012-01-26 11:17:40 -05:00
|
|
|
if(r < 0) {
|
|
|
|
return r;
|
2012-02-22 09:58:33 -05:00
|
|
|
} else if(datamax < r) {
|
2012-01-26 11:17:40 -05:00
|
|
|
return SPDYLAY_ERR_CALLBACK_FAILURE;
|
|
|
|
}
|
2012-02-22 09:58:33 -05:00
|
|
|
memset(*buf_ptr, 0, SPDYLAY_HEAD_LEN);
|
|
|
|
spdylay_put_uint32be(&(*buf_ptr)[0], frame->stream_id);
|
|
|
|
spdylay_put_uint32be(&(*buf_ptr)[4], r);
|
|
|
|
flags = 0;
|
2012-01-26 11:17:40 -05:00
|
|
|
if(eof) {
|
2012-02-16 09:38:19 -05:00
|
|
|
frame->eof = 1;
|
2012-02-24 07:40:13 -05:00
|
|
|
if(frame->flags & SPDYLAY_DATA_FLAG_FIN) {
|
|
|
|
flags |= SPDYLAY_DATA_FLAG_FIN;
|
2012-02-16 09:38:19 -05:00
|
|
|
}
|
2012-01-26 11:17:40 -05:00
|
|
|
}
|
2012-02-22 09:58:33 -05:00
|
|
|
(*buf_ptr)[4] = flags;
|
2012-01-26 11:17:40 -05:00
|
|
|
return r+8;
|
2012-01-24 08:02:24 -05:00
|
|
|
}
|
2012-01-27 09:05:29 -05:00
|
|
|
|
|
|
|
uint32_t spdylay_session_get_next_unique_id(spdylay_session *session)
|
|
|
|
{
|
2012-01-28 12:00:03 -05:00
|
|
|
uint32_t ret_id;
|
2012-01-27 09:05:29 -05:00
|
|
|
if(session->next_unique_id > SPDYLAY_MAX_UNIQUE_ID) {
|
|
|
|
if(session->server) {
|
|
|
|
session->next_unique_id = 2;
|
|
|
|
} else {
|
|
|
|
session->next_unique_id = 1;
|
|
|
|
}
|
|
|
|
}
|
2012-01-28 12:00:03 -05:00
|
|
|
ret_id = session->next_unique_id;
|
|
|
|
session->next_unique_id += 2;
|
|
|
|
return ret_id;
|
2012-01-27 09:05:29 -05:00
|
|
|
}
|
2012-02-03 11:37:21 -05:00
|
|
|
|
|
|
|
void* spdylay_session_get_stream_user_data(spdylay_session *session,
|
|
|
|
int32_t stream_id)
|
|
|
|
{
|
|
|
|
spdylay_stream *stream;
|
|
|
|
stream = spdylay_session_get_stream(session, stream_id);
|
|
|
|
if(stream) {
|
|
|
|
return stream->stream_user_data;
|
|
|
|
} else {
|
|
|
|
return NULL;
|
|
|
|
}
|
|
|
|
}
|
2012-02-19 09:42:25 -05:00
|
|
|
|
|
|
|
int spdylay_session_resume_data(spdylay_session *session, int32_t stream_id)
|
|
|
|
{
|
|
|
|
int r;
|
|
|
|
spdylay_stream *stream;
|
|
|
|
stream = spdylay_session_get_stream(session, stream_id);
|
2012-02-25 10:12:32 -05:00
|
|
|
if(stream == NULL || stream->deferred_data == NULL ||
|
|
|
|
(stream->deferred_flags & SPDYLAY_DEFERRED_FLOW_CONTROL)) {
|
2012-02-19 09:42:25 -05:00
|
|
|
return SPDYLAY_ERR_INVALID_ARGUMENT;
|
|
|
|
}
|
|
|
|
r = spdylay_pq_push(&session->ob_pq, stream->deferred_data);
|
|
|
|
if(r == 0) {
|
|
|
|
spdylay_stream_detach_deferred_data(stream);
|
|
|
|
}
|
|
|
|
return r;
|
|
|
|
}
|
2012-02-25 10:12:32 -05:00
|
|
|
|
|
|
|
uint8_t spdylay_session_get_pri_lowest(spdylay_session *session)
|
|
|
|
{
|
|
|
|
if(session->version == SPDYLAY_PROTO_SPDY2) {
|
|
|
|
return SPDYLAY_SPDY2_PRI_LOWEST;
|
|
|
|
} else if(session->version == SPDYLAY_PROTO_SPDY3) {
|
|
|
|
return SPDYLAY_SPDY3_PRI_LOWEST;
|
|
|
|
} else {
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
}
|
2012-03-15 10:06:28 -04:00
|
|
|
|
|
|
|
size_t spdylay_session_get_outbound_queue_size(spdylay_session *session)
|
|
|
|
{
|
|
|
|
return spdylay_pq_size(&session->ob_pq)+spdylay_pq_size(&session->ob_ss_pq);
|
|
|
|
}
|