Preliminary experimental support for SPDY draft 2.

This commit is contained in:
Valentin Bartenev 2013-03-20 10:36:57 +00:00
parent cf64a6c536
commit 2686cb4452
18 changed files with 4852 additions and 16 deletions

View File

@ -100,6 +100,7 @@ fi
# ngx_http_write_filter
# ngx_http_header_filter
# ngx_http_chunked_filter
# ngx_http_spdy_filter
# ngx_http_range_header_filter
# ngx_http_gzip_filter
# ngx_http_postpone_filter
@ -118,8 +119,13 @@ fi
HTTP_FILTER_MODULES="$HTTP_WRITE_FILTER_MODULE \
$HTTP_HEADER_FILTER_MODULE \
$HTTP_CHUNKED_FILTER_MODULE \
$HTTP_RANGE_HEADER_FILTER_MODULE"
$HTTP_CHUNKED_FILTER_MODULE"
if [ $HTTP_SPDY = YES ]; then
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES $HTTP_SPDY_FILTER_MODULE"
fi
HTTP_FILTER_MODULES="$HTTP_FILTER_MODULES $HTTP_RANGE_HEADER_FILTER_MODULE"
if [ $HTTP_GZIP = YES ]; then
have=NGX_HTTP_GZIP . auto/have
@ -179,6 +185,15 @@ if [ $HTTP_USERID = YES ]; then
HTTP_SRCS="$HTTP_SRCS $HTTP_USERID_SRCS"
fi
if [ $HTTP_SPDY = YES ]; then
have=NGX_HTTP_SPDY . auto/have
USE_ZLIB=YES
HTTP_MODULES="$HTTP_MODULES $HTTP_SPDY_MODULE"
HTTP_DEPS="$HTTP_DEPS $HTTP_SPDY_DEPS"
HTTP_SRCS="$HTTP_SRCS $HTTP_SPDY_SRCS"
fi
HTTP_MODULES="$HTTP_MODULES $HTTP_STATIC_MODULE"
if [ $HTTP_GZIP_STATIC = YES ]; then

View File

@ -60,6 +60,7 @@ HTTP_CACHE=YES
HTTP_CHARSET=YES
HTTP_GZIP=YES
HTTP_SSL=NO
HTTP_SPDY=NO
HTTP_SSI=YES
HTTP_POSTPONE=NO
HTTP_REALIP=NO
@ -202,6 +203,7 @@ do
--http-scgi-temp-path=*) NGX_HTTP_SCGI_TEMP_PATH="$value" ;;
--with-http_ssl_module) HTTP_SSL=YES ;;
--with-http_spdy_module) HTTP_SPDY=YES ;;
--with-http_realip_module) HTTP_REALIP=YES ;;
--with-http_addition_module) HTTP_ADDITION=YES ;;
--with-http_xslt_module) HTTP_XSLT=YES ;;
@ -349,6 +351,7 @@ cat << END
--with-ipv6 enable IPv6 support
--with-http_ssl_module enable ngx_http_ssl_module
--with-http_spdy_module enable ngx_http_spdy_module
--with-http_realip_module enable ngx_http_realip_module
--with-http_addition_module enable ngx_http_addition_module
--with-http_xslt_module enable ngx_http_xslt_module

View File

@ -324,6 +324,15 @@ HTTP_POSTPONE_FILTER_SRCS=src/http/ngx_http_postpone_filter_module.c
HTTP_FILE_CACHE_SRCS=src/http/ngx_http_file_cache.c
HTTP_SPDY_MODULE=ngx_http_spdy_module
HTTP_SPDY_FILTER_MODULE=ngx_http_spdy_filter_module
HTTP_SPDY_DEPS="src/http/ngx_http_spdy.h \
src/http/ngx_http_spdy_module.h"
HTTP_SPDY_SRCS="src/http/ngx_http_spdy.c \
src/http/ngx_http_spdy_module.c \
src/http/ngx_http_spdy_filter_module.c"
HTTP_CHARSET_FILTER_MODULE=ngx_http_charset_filter_module
HTTP_CHARSET_SRCS=src/http/modules/ngx_http_charset_filter_module.c

View File

@ -275,13 +275,28 @@ static int
ngx_http_ssl_npn_advertised(ngx_ssl_conn_t *ssl_conn,
const unsigned char **out, unsigned int *outlen, void *arg)
{
#if (NGX_DEBUG)
#if (NGX_HTTP_SPDY || NGX_DEBUG)
ngx_connection_t *c;
c = ngx_ssl_get_connection(ssl_conn);
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "SSL NPN advertised");
#endif
#if (NGX_HTTP_SPDY)
{
ngx_http_connection_t *hc;
hc = c->data;
if (hc->addr_conf->spdy) {
*out = (unsigned char *) NGX_SPDY_NPN_ADVERTISE NGX_HTTP_NPN_ADVERTISE;
*outlen = sizeof(NGX_SPDY_NPN_ADVERTISE NGX_HTTP_NPN_ADVERTISE) - 1;
return SSL_TLSEXT_ERR_OK;
}
}
#endif
*out = (unsigned char *) NGX_HTTP_NPN_ADVERTISE;
*outlen = sizeof(NGX_HTTP_NPN_ADVERTISE) - 1;

View File

@ -1225,6 +1225,9 @@ ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
#if (NGX_HTTP_SSL)
ngx_uint_t ssl;
#endif
#if (NGX_HTTP_SPDY)
ngx_uint_t spdy;
#endif
/*
* we cannot compare whole sockaddr struct's as kernel
@ -1277,6 +1280,9 @@ ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
#if (NGX_HTTP_SSL)
ssl = lsopt->ssl || addr[i].opt.ssl;
#endif
#if (NGX_HTTP_SPDY)
spdy = lsopt->spdy || addr[i].opt.spdy;
#endif
if (lsopt->set) {
@ -1307,6 +1313,9 @@ ngx_http_add_addresses(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
#if (NGX_HTTP_SSL)
addr[i].opt.ssl = ssl;
#endif
#if (NGX_HTTP_SPDY)
addr[i].opt.spdy = spdy;
#endif
return NGX_OK;
}
@ -1337,6 +1346,14 @@ ngx_http_add_address(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
}
}
#if (NGX_HTTP_SPDY && NGX_HTTP_SSL && !defined TLSEXT_TYPE_next_proto_neg)
if (lsopt->spdy && lsopt->ssl) {
ngx_conf_log_error(NGX_LOG_WARN, cf, 0,
"nginx was built without OpenSSL NPN support, "
"SPDY is not enabled for %s", lsopt->addr);
}
#endif
addr = ngx_array_push(&port->addrs);
if (addr == NULL) {
return NGX_ERROR;
@ -1820,6 +1837,9 @@ ngx_http_add_addrs(ngx_conf_t *cf, ngx_http_port_t *hport,
#if (NGX_HTTP_SSL)
addrs[i].conf.ssl = addr[i].opt.ssl;
#endif
#if (NGX_HTTP_SPDY)
addrs[i].conf.spdy = addr[i].opt.spdy;
#endif
if (addr[i].hash.buckets == NULL
&& (addr[i].wc_head == NULL
@ -1881,6 +1901,9 @@ ngx_http_add_addrs6(ngx_conf_t *cf, ngx_http_port_t *hport,
#if (NGX_HTTP_SSL)
addrs6[i].conf.ssl = addr[i].opt.ssl;
#endif
#if (NGX_HTTP_SPDY)
addrs6[i].conf.spdy = addr[i].opt.spdy;
#endif
if (addr[i].hash.buckets == NULL
&& (addr[i].wc_head == NULL

View File

@ -20,6 +20,10 @@ typedef struct ngx_http_file_cache_s ngx_http_file_cache_t;
typedef struct ngx_http_log_ctx_s ngx_http_log_ctx_t;
typedef struct ngx_http_chunked_s ngx_http_chunked_t;
#if (NGX_HTTP_SPDY)
typedef struct ngx_http_spdy_stream_s ngx_http_spdy_stream_t;
#endif
typedef ngx_int_t (*ngx_http_header_handler_pt)(ngx_http_request_t *r,
ngx_table_elt_t *h, ngx_uint_t offset);
typedef u_char *(*ngx_http_log_handler_pt)(ngx_http_request_t *r,
@ -35,6 +39,9 @@ typedef u_char *(*ngx_http_log_handler_pt)(ngx_http_request_t *r,
#include <ngx_http_busy_lock.h>
#include <ngx_http_core_module.h>
#if (NGX_HTTP_SPDY)
#include <ngx_http_spdy.h>
#endif
#if (NGX_HTTP_CACHE)
#include <ngx_http_cache.h>
#endif
@ -80,12 +87,14 @@ ngx_int_t ngx_http_add_listen(ngx_conf_t *cf, ngx_http_core_srv_conf_t *cscf,
void ngx_http_init_connection(ngx_connection_t *c);
void ngx_http_close_connection(ngx_connection_t *c);
#ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
int ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg);
#endif
ngx_int_t ngx_http_parse_request_line(ngx_http_request_t *r, ngx_buf_t *b);
ngx_int_t ngx_http_parse_uri(ngx_http_request_t *r);
ngx_int_t ngx_http_parse_complex_uri(ngx_http_request_t *r,
ngx_uint_t merge_slashes);
ngx_int_t ngx_http_parse_status_line(ngx_http_request_t *r, ngx_buf_t *b,
@ -104,12 +113,17 @@ ngx_int_t ngx_http_parse_chunked(ngx_http_request_t *r, ngx_buf_t *b,
ngx_http_chunked_t *ctx);
ngx_http_request_t *ngx_http_create_request(ngx_connection_t *c);
ngx_int_t ngx_http_process_request_uri(ngx_http_request_t *r);
ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
void ngx_http_process_request(ngx_http_request_t *r);
void ngx_http_update_location_config(ngx_http_request_t *r);
void ngx_http_handler(ngx_http_request_t *r);
void ngx_http_run_posted_requests(ngx_connection_t *c);
ngx_int_t ngx_http_post_request(ngx_http_request_t *r,
ngx_http_posted_request_t *pr);
void ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc);
void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc);
void ngx_http_empty_handler(ngx_event_t *wev);
void ngx_http_request_empty_handler(ngx_http_request_t *r);

View File

@ -2130,6 +2130,13 @@ ngx_http_gzip_ok(ngx_http_request_t *r)
return NGX_DECLINED;
}
#if (NGX_HTTP_SPDY)
if (r->spdy_stream) {
r->gzip_ok = 1;
return NGX_OK;
}
#endif
ae = r->headers_in.accept_encoding;
if (ae == NULL) {
return NGX_DECLINED;
@ -2464,6 +2471,10 @@ ngx_http_subrequest(ngx_http_request_t *r,
sr->request_body = r->request_body;
#if (NGX_HTTP_SPDY)
sr->spdy_stream = r->spdy_stream;
#endif
sr->method = NGX_HTTP_GET;
sr->http_version = r->http_version;
@ -4130,6 +4141,18 @@ ngx_http_core_listen(ngx_conf_t *cf, ngx_command_t *cmd, void *conf)
#endif
}
if (ngx_strcmp(value[n].data, "spdy") == 0) {
#if (NGX_HTTP_SPDY)
lsopt.spdy = 1;
continue;
#else
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the \"spdy\" parameter requires "
"ngx_http_spdy_module");
return NGX_CONF_ERROR;
#endif
}
if (ngx_strncmp(value[n].data, "so_keepalive=", 13) == 0) {
if (ngx_strcmp(&value[n].data[13], "on") == 0) {

View File

@ -75,6 +75,9 @@ typedef struct {
#if (NGX_HTTP_SSL)
unsigned ssl:1;
#endif
#if (NGX_HTTP_SPDY)
unsigned spdy:1;
#endif
#if (NGX_HAVE_INET6 && defined IPV6_V6ONLY)
unsigned ipv6only:1;
#endif
@ -232,7 +235,10 @@ struct ngx_http_addr_conf_s {
ngx_http_virtual_names_t *virtual_names;
#if (NGX_HTTP_SSL)
ngx_uint_t ssl; /* unsigned ssl:1; */
unsigned ssl:1;
#endif
#if (NGX_HTTP_SPDY)
unsigned spdy:1;
#endif
};

View File

@ -1074,6 +1074,154 @@ header_done:
}
ngx_int_t
ngx_http_parse_uri(ngx_http_request_t *r)
{
u_char *p, ch;
enum {
sw_start = 0,
sw_after_slash_in_uri,
sw_check_uri,
sw_uri
} state;
state = sw_start;
for (p = r->uri_start; p != r->uri_end; p++) {
ch = *p;
switch (state) {
case sw_start:
if (ch != '/') {
return NGX_ERROR;
}
state = sw_after_slash_in_uri;
break;
/* check "/.", "//", "%", and "\" (Win32) in URI */
case sw_after_slash_in_uri:
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
state = sw_check_uri;
break;
}
switch (ch) {
case ' ':
r->space_in_uri = 1;
state = sw_check_uri;
break;
case '.':
r->complex_uri = 1;
state = sw_uri;
break;
case '%':
r->quoted_uri = 1;
state = sw_uri;
break;
case '/':
r->complex_uri = 1;
state = sw_uri;
break;
#if (NGX_WIN32)
case '\\':
r->complex_uri = 1;
state = sw_uri;
break;
#endif
case '?':
r->args_start = p + 1;
state = sw_uri;
break;
case '#':
r->complex_uri = 1;
state = sw_uri;
break;
case '+':
r->plus_in_uri = 1;
break;
default:
state = sw_check_uri;
break;
}
break;
/* check "/", "%" and "\" (Win32) in URI */
case sw_check_uri:
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
break;
}
switch (ch) {
case '/':
#if (NGX_WIN32)
if (r->uri_ext == p) {
r->complex_uri = 1;
state = sw_uri;
break;
}
#endif
r->uri_ext = NULL;
state = sw_after_slash_in_uri;
break;
case '.':
r->uri_ext = p + 1;
break;
case ' ':
r->space_in_uri = 1;
break;
#if (NGX_WIN32)
case '\\':
r->complex_uri = 1;
state = sw_after_slash_in_uri;
break;
#endif
case '%':
r->quoted_uri = 1;
state = sw_uri;
break;
case '?':
r->args_start = p + 1;
state = sw_uri;
break;
case '#':
r->complex_uri = 1;
state = sw_uri;
break;
case '+':
r->plus_in_uri = 1;
break;
}
break;
/* URI */
case sw_uri:
if (usual[ch >> 5] & (1 << (ch & 0x1f))) {
break;
}
switch (ch) {
case ' ':
r->space_in_uri = 1;
break;
case '#':
r->complex_uri = 1;
break;
}
break;
}
}
return NGX_OK;
}
ngx_int_t
ngx_http_parse_complex_uri(ngx_http_request_t *r, ngx_uint_t merge_slashes)
{

View File

@ -11,7 +11,6 @@
static void ngx_http_wait_request_handler(ngx_event_t *ev);
static ngx_http_request_t *ngx_http_create_request(ngx_connection_t *c);
static void ngx_http_process_request_line(ngx_event_t *rev);
static void ngx_http_process_request_headers(ngx_event_t *rev);
static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
@ -31,9 +30,6 @@ static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r,
ngx_table_elt_t *h, ngx_uint_t offset);
static ngx_int_t ngx_http_process_request_uri(ngx_http_request_t *r);
static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
static void ngx_http_process_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_validate_host(ngx_str_t *host, ngx_pool_t *pool,
ngx_uint_t alloc);
static ngx_int_t ngx_http_set_virtual_server(ngx_http_request_t *r,
@ -56,9 +52,7 @@ static void ngx_http_set_lingering_close(ngx_http_request_t *r);
static void ngx_http_lingering_close_handler(ngx_event_t *ev);
static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
static void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t error);
static void ngx_http_log_request(ngx_http_request_t *r);
static void ngx_http_close_connection(ngx_connection_t *c);
static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
@ -318,6 +312,12 @@ ngx_http_init_connection(ngx_connection_t *c)
rev->handler = ngx_http_wait_request_handler;
c->write->handler = ngx_http_empty_handler;
#if (NGX_HTTP_SPDY)
if (hc->addr_conf->spdy) {
rev->handler = ngx_http_spdy_init;
}
#endif
#if (NGX_HTTP_SSL)
{
ngx_http_ssl_srv_conf_t *sscf;
@ -487,7 +487,7 @@ ngx_http_wait_request_handler(ngx_event_t *rev)
}
static ngx_http_request_t *
ngx_http_request_t *
ngx_http_create_request(ngx_connection_t *c)
{
ngx_pool_t *pool;
@ -727,6 +727,21 @@ ngx_http_ssl_handshake_handler(ngx_connection_t *c)
c->ssl->no_wait_shutdown = 1;
#if (NGX_HTTP_SPDY && defined TLSEXT_TYPE_next_proto_neg)
{
unsigned int len;
const unsigned char *data;
static const ngx_str_t spdy = ngx_string(NGX_SPDY_NPN_NEGOTIATED);
SSL_get0_next_proto_negotiated(c->ssl->connection, &data, &len);
if (len == spdy.len && ngx_strncmp(data, spdy.data, spdy.len) == 0) {
ngx_http_spdy_init(c->read);
return;
}
}
#endif
c->log->action = "waiting for request";
c->read->handler = ngx_http_wait_request_handler;
@ -985,7 +1000,7 @@ ngx_http_process_request_line(ngx_event_t *rev)
}
static ngx_int_t
ngx_int_t
ngx_http_process_request_uri(ngx_http_request_t *r)
{
ngx_http_core_srv_conf_t *cscf;
@ -1687,7 +1702,7 @@ ngx_http_process_multi_header_lines(ngx_http_request_t *r, ngx_table_elt_t *h,
}
static ngx_int_t
ngx_int_t
ngx_http_process_request_header(ngx_http_request_t *r)
{
if (r->headers_in.server.len == 0
@ -1757,7 +1772,7 @@ ngx_http_process_request_header(ngx_http_request_t *r)
}
static void
void
ngx_http_process_request(ngx_http_request_t *r)
{
ngx_connection_t *c;
@ -2434,6 +2449,13 @@ ngx_http_finalize_connection(ngx_http_request_t *r)
{
ngx_http_core_loc_conf_t *clcf;
#if (NGX_HTTP_SPDY)
if (r->spdy_stream) {
ngx_http_close_request(r, 0);
return;
}
#endif
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (r->main->count != 1) {
@ -2488,6 +2510,12 @@ ngx_http_set_write_handler(ngx_http_request_t *r)
ngx_http_test_reading;
r->write_event_handler = ngx_http_writer;
#if (NGX_HTTP_SPDY)
if (r->spdy_stream) {
return NGX_OK;
}
#endif
wev = r->connection->write;
if (wev->ready && wev->delayed) {
@ -2635,6 +2663,19 @@ ngx_http_test_reading(ngx_http_request_t *r)
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
#if (NGX_HTTP_SPDY)
if (r->spdy_stream) {
if (c->error) {
err = 0;
goto closed;
}
return;
}
#endif
#if (NGX_HAVE_KQUEUE)
if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
@ -3270,12 +3311,19 @@ ngx_http_close_request(ngx_http_request_t *r, ngx_int_t rc)
return;
}
#if (NGX_HTTP_SPDY)
if (r->spdy_stream) {
ngx_http_spdy_close_stream(r->spdy_stream, rc);
return;
}
#endif
ngx_http_free_request(r, rc);
ngx_http_close_connection(c);
}
static void
void
ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc)
{
ngx_log_t *log;
@ -3376,7 +3424,7 @@ ngx_http_log_request(ngx_http_request_t *r)
}
static void
void
ngx_http_close_connection(ngx_connection_t *c)
{
ngx_pool_t *pool;

View File

@ -429,6 +429,9 @@ struct ngx_http_request_s {
ngx_uint_t err_status;
ngx_http_connection_t *http_connection;
#if (NGX_HTTP_SPDY)
ngx_http_spdy_stream_t *spdy_stream;
#endif
ngx_http_log_handler_pt log_handler;

View File

@ -42,6 +42,13 @@ ngx_http_read_client_request_body(ngx_http_request_t *r,
r->main->count++;
#if (NGX_HTTP_SPDY)
if (r->spdy_stream) {
rc = ngx_http_spdy_read_request_body(r, post_handler);
goto done;
}
#endif
if (r->request_body || r->discard_body) {
post_handler(r);
return NGX_OK;
@ -475,6 +482,13 @@ ngx_http_discard_request_body(ngx_http_request_t *r)
ngx_int_t rc;
ngx_event_t *rev;
#if (NGX_HTTP_SPDY)
if (r->spdy_stream && r == r->main) {
r->spdy_stream->skip_data = NGX_SPDY_DATA_DISCARD;
return NGX_OK;
}
#endif
if (r != r->main || r->discard_body || r->request_body) {
return NGX_OK;
}

2881
src/http/ngx_http_spdy.c Normal file

File diff suppressed because it is too large Load Diff

235
src/http/ngx_http_spdy.h Normal file
View File

@ -0,0 +1,235 @@
/*
* Copyright (C) Nginx, Inc.
* Copyright (C) Valentin V. Bartenev
*/
#ifndef _NGX_HTTP_SPDY_H_INCLUDED_
#define _NGX_HTTP_SPDY_H_INCLUDED_
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <zlib.h>
#define NGX_SPDY_VERSION 2
#ifdef TLSEXT_TYPE_next_proto_neg
#define NGX_SPDY_NPN_ADVERTISE "\x06spdy/2"
#define NGX_SPDY_NPN_NEGOTIATED "spdy/2"
#endif
#define NGX_SPDY_STATE_BUFFER_SIZE 16
#define NGX_SPDY_CTL_BIT 1
#define NGX_SPDY_SYN_STREAM 1
#define NGX_SPDY_SYN_REPLY 2
#define NGX_SPDY_RST_STREAM 3
#define NGX_SPDY_SETTINGS 4
#define NGX_SPDY_NOOP 5
#define NGX_SPDY_PING 6
#define NGX_SPDY_GOAWAY 7
#define NGX_SPDY_HEADERS 8
#define NGX_SPDY_FRAME_HEADER_SIZE 8
#define NGX_SPDY_SID_SIZE 4
#define NGX_SPDY_SYN_STREAM_SIZE 10
#define NGX_SPDY_SYN_REPLY_SIZE 6
#define NGX_SPDY_RST_STREAM_SIZE 8
#define NGX_SPDY_PING_SIZE 4
#define NGX_SPDY_GOAWAY_SIZE 4
#define NGX_SPDY_NV_NUM_SIZE 2
#define NGX_SPDY_NV_NLEN_SIZE 2
#define NGX_SPDY_NV_VLEN_SIZE 2
#define NGX_SPDY_SETTINGS_NUM_SIZE 4
#define NGX_SPDY_SETTINGS_IDF_SIZE 4
#define NGX_SPDY_SETTINGS_VAL_SIZE 4
#define NGX_SPDY_SETTINGS_PAIR_SIZE \
(NGX_SPDY_SETTINGS_IDF_SIZE + NGX_SPDY_SETTINGS_VAL_SIZE)
#define NGX_SPDY_HIGHEST_PRIORITY 0
#define NGX_SPDY_LOWEST_PRIORITY 3
#define NGX_SPDY_FLAG_FIN 0x01
#define NGX_SPDY_FLAG_UNIDIRECTIONAL 0x02
#define NGX_SPDY_FLAG_CLEAR_SETTINGS 0x01
#define NGX_SPDY_MAX_FRAME_SIZE ((1 << 24) - 1)
#define NGX_SPDY_DATA_DISCARD 1
#define NGX_SPDY_DATA_ERROR 2
#define NGX_SPDY_DATA_INTERNAL_ERROR 3
typedef struct ngx_http_spdy_connection_s ngx_http_spdy_connection_t;
typedef struct ngx_http_spdy_out_frame_s ngx_http_spdy_out_frame_t;
typedef u_char *(*ngx_http_spdy_handler_pt) (ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
struct ngx_http_spdy_connection_s {
ngx_connection_t *connection;
ngx_http_connection_t *http_connection;
ngx_uint_t processing;
u_char buffer[NGX_SPDY_STATE_BUFFER_SIZE];
size_t buffer_used;
ngx_http_spdy_handler_pt handler;
z_stream zstream_in;
z_stream zstream_out;
ngx_pool_t *pool;
ngx_http_spdy_out_frame_t *free_ctl_frames;
ngx_connection_t *free_fake_connections;
ngx_http_spdy_stream_t **streams_index;
ngx_http_spdy_out_frame_t *last_out;
ngx_http_spdy_stream_t *last_stream;
ngx_http_spdy_stream_t *stream;
ngx_uint_t headers;
size_t length;
u_char flags;
ngx_uint_t last_sid;
unsigned blocked:2;
unsigned waiting:1; /* FIXME better name */
};
struct ngx_http_spdy_stream_s {
ngx_uint_t id;
ngx_http_request_t *request;
ngx_http_spdy_connection_t *connection;
ngx_http_spdy_stream_t *index;
ngx_http_spdy_stream_t *next;
ngx_uint_t header_buffers;
ngx_uint_t waiting;
ngx_http_spdy_out_frame_t *free_frames;
ngx_chain_t *free_data_headers;
unsigned priority:2;
unsigned handled:1;
unsigned in_closed:1;
unsigned out_closed:1;
unsigned skip_data:2;
};
struct ngx_http_spdy_out_frame_s {
ngx_http_spdy_out_frame_t *next;
ngx_chain_t *first;
ngx_chain_t *last;
ngx_int_t (*handler)(ngx_http_spdy_connection_t *sc,
ngx_http_spdy_out_frame_t *frame);
ngx_http_spdy_out_frame_t *free;
ngx_http_spdy_stream_t *stream;
size_t size;
ngx_uint_t priority;
unsigned blocked:1;
unsigned fin:1;
};
static ngx_inline void
ngx_http_spdy_queue_frame(ngx_http_spdy_connection_t *sc,
ngx_http_spdy_out_frame_t *frame)
{
ngx_http_spdy_out_frame_t **out;
for (out = &sc->last_out; *out; out = &(*out)->next)
{
if (frame->priority >= (*out)->priority) {
break;
}
}
frame->next = *out;
*out = frame;
}
static ngx_inline void
ngx_http_spdy_queue_blocked_frame(ngx_http_spdy_connection_t *sc,
ngx_http_spdy_out_frame_t *frame)
{
ngx_http_spdy_out_frame_t **out;
for (out = &sc->last_out; *out && !(*out)->blocked; out = &(*out)->next)
{
if (frame->priority >= (*out)->priority) {
break;
}
}
frame->next = *out;
*out = frame;
}
void ngx_http_spdy_init(ngx_event_t *rev);
void ngx_http_spdy_request_headers_init();
ngx_int_t ngx_http_spdy_read_request_body(ngx_http_request_t *r,
ngx_http_client_body_handler_pt post_handler);
void ngx_http_spdy_close_stream(ngx_http_spdy_stream_t *stream, ngx_int_t rc);
ngx_int_t ngx_http_spdy_send_output_queue(ngx_http_spdy_connection_t *sc);
#define ngx_spdy_frame_aligned_write_uint16(p, s) \
(*(uint16_t *) (p) = htons((uint16_t) (s)), (p) + sizeof(uint16_t))
#define ngx_spdy_frame_aligned_write_uint32(p, s) \
(*(uint32_t *) (p) = htonl((uint32_t) (s)), (p) + sizeof(uint32_t))
#if (NGX_HAVE_NONALIGNED)
#define ngx_spdy_frame_write_uint16 ngx_spdy_frame_aligned_write_uint16
#define ngx_spdy_frame_write_uint32 ngx_spdy_frame_aligned_write_uint32
#else
#define ngx_spdy_frame_write_uint16(p, s) \
((p)[0] = (u_char) (s) >> 8, (p)[1] = (u_char) (s), (p) + sizeof(uint16_t))
#define ngx_spdy_frame_write_uint32(p, s) \
((p)[0] = (u_char) (s) >> 24, \
(p)[1] = (u_char) (s) >> 16, \
(p)[2] = (u_char) (s) >> 8, \
(p)[3] = (u_char) (s), (p) + sizeof(uint32_t))
#endif
#define ngx_spdy_ctl_frame_head(t) \
((uint32_t) NGX_SPDY_CTL_BIT << 31 | NGX_SPDY_VERSION << 16 | (t))
#define ngx_spdy_frame_write_head(p, t) \
ngx_spdy_frame_aligned_write_uint32(p, ngx_spdy_ctl_frame_head(t))
#define ngx_spdy_frame_write_flags_and_len(p, f, l) \
ngx_spdy_frame_aligned_write_uint32(p, (f) << 24 | (l))
#define ngx_spdy_frame_write_sid ngx_spdy_frame_aligned_write_uint32
#endif /* _NGX_HTTP_SPDY_H_INCLUDED_ */

View File

@ -0,0 +1,999 @@
/*
* Copyright (C) Nginx, Inc.
* Copyright (C) Valentin V. Bartenev
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <nginx.h>
#include <ngx_http_spdy_module.h>
#include <zlib.h>
#define NGX_SPDY_WRITE_BUFFERED NGX_HTTP_WRITE_BUFFERED
#define ngx_http_spdy_nv_nsize(h) (NGX_SPDY_NV_NLEN_SIZE + sizeof(h) - 1)
#define ngx_http_spdy_nv_vsize(h) (NGX_SPDY_NV_VLEN_SIZE + sizeof(h) - 1)
#define ngx_http_spdy_nv_write_num ngx_spdy_frame_write_uint16
#define ngx_http_spdy_nv_write_nlen ngx_spdy_frame_write_uint16
#define ngx_http_spdy_nv_write_vlen ngx_spdy_frame_write_uint16
#define ngx_http_spdy_nv_write_name(p, h) \
ngx_cpymem(ngx_http_spdy_nv_write_nlen(p, sizeof(h) - 1), h, sizeof(h) - 1)
#define ngx_http_spdy_nv_write_val(p, h) \
ngx_cpymem(ngx_http_spdy_nv_write_vlen(p, sizeof(h) - 1), h, sizeof(h) - 1)
static ngx_inline ngx_int_t ngx_http_spdy_filter_send(
ngx_connection_t *fc, ngx_http_spdy_stream_t *stream);
static ngx_http_spdy_out_frame_t *ngx_http_spdy_filter_get_data_frame(
ngx_http_spdy_stream_t *stream, size_t len, ngx_uint_t flags,
ngx_chain_t *first, ngx_chain_t *last);
static ngx_int_t ngx_http_spdy_syn_frame_handler(
ngx_http_spdy_connection_t *sc, ngx_http_spdy_out_frame_t *frame);
static ngx_int_t ngx_http_spdy_data_frame_handler(
ngx_http_spdy_connection_t *sc, ngx_http_spdy_out_frame_t *frame);
static ngx_inline void ngx_http_spdy_handle_frame(
ngx_http_spdy_stream_t *stream, ngx_http_spdy_out_frame_t *frame);
static ngx_inline void ngx_http_spdy_handle_stream(
ngx_http_spdy_connection_t *sc, ngx_http_spdy_stream_t *stream);
static void ngx_http_spdy_filter_cleanup(void *data);
static ngx_int_t ngx_http_spdy_filter_init(ngx_conf_t *cf);
static ngx_http_module_t ngx_http_spdy_filter_module_ctx = {
NULL, /* preconfiguration */
ngx_http_spdy_filter_init, /* postconfiguration */
NULL, /* create main configuration */
NULL, /* init main configuration */
NULL, /* create server configuration */
NULL, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_spdy_filter_module = {
NGX_MODULE_V1,
&ngx_http_spdy_filter_module_ctx, /* module context */
NULL, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
NULL, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_http_output_header_filter_pt ngx_http_next_header_filter;
static ngx_http_output_body_filter_pt ngx_http_next_body_filter;
static ngx_int_t
ngx_http_spdy_header_filter(ngx_http_request_t *r)
{
int rc;
size_t len;
u_char *p, *buf, *last;
ngx_buf_t *b;
ngx_str_t host;
ngx_uint_t i, j, count, port;
ngx_chain_t *cl;
ngx_list_part_t *part, *pt;
ngx_table_elt_t *header, *h;
ngx_connection_t *c;
ngx_http_cleanup_t *cln;
ngx_http_core_loc_conf_t *clcf;
ngx_http_core_srv_conf_t *cscf;
ngx_http_spdy_stream_t *stream;
ngx_http_spdy_out_frame_t *frame;
ngx_http_spdy_connection_t *sc;
struct sockaddr_in *sin;
#if (NGX_HAVE_INET6)
struct sockaddr_in6 *sin6;
#endif
u_char addr[NGX_SOCKADDR_STRLEN];
if (!r->spdy_stream) {
return ngx_http_next_header_filter(r);
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"spdy header filter");
if (r->header_sent) {
return NGX_OK;
}
r->header_sent = 1;
if (r != r->main) {
return NGX_OK;
}
c = r->connection;
if (r->method == NGX_HTTP_HEAD) {
r->header_only = 1;
}
switch (r->headers_out.status) {
case NGX_HTTP_OK:
case NGX_HTTP_PARTIAL_CONTENT:
break;
case NGX_HTTP_NOT_MODIFIED:
r->header_only = 1;
break;
case NGX_HTTP_NO_CONTENT:
r->header_only = 1;
ngx_str_null(&r->headers_out.content_type);
r->headers_out.content_length = NULL;
r->headers_out.content_length_n = -1;
/* fall through */
default:
r->headers_out.last_modified_time = -1;
r->headers_out.last_modified = NULL;
}
len = NGX_SPDY_NV_NUM_SIZE
+ ngx_http_spdy_nv_nsize("version")
+ ngx_http_spdy_nv_vsize("HTTP/1.1")
+ ngx_http_spdy_nv_nsize("status")
+ ngx_http_spdy_nv_vsize("418");
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (r->headers_out.server == NULL) {
len += ngx_http_spdy_nv_nsize("server");
len += clcf->server_tokens ? ngx_http_spdy_nv_vsize(NGINX_VER)
: ngx_http_spdy_nv_vsize("nginx");
}
if (r->headers_out.date == NULL) {
len += ngx_http_spdy_nv_nsize("date")
+ ngx_http_spdy_nv_vsize("Wed, 31 Dec 1986 10:00:00 GMT");
}
if (r->headers_out.content_type.len) {
len += ngx_http_spdy_nv_nsize("content-type")
+ NGX_SPDY_NV_VLEN_SIZE + r->headers_out.content_type.len;
if (r->headers_out.content_type_len == r->headers_out.content_type.len
&& r->headers_out.charset.len)
{
len += sizeof("; charset=") - 1 + r->headers_out.charset.len;
}
}
if (r->headers_out.content_length == NULL
&& r->headers_out.content_length_n >= 0)
{
len += ngx_http_spdy_nv_nsize("content-length")
+ NGX_SPDY_NV_VLEN_SIZE + NGX_OFF_T_LEN;
}
if (r->headers_out.last_modified == NULL
&& r->headers_out.last_modified_time != -1)
{
len += ngx_http_spdy_nv_nsize("last-modified")
+ ngx_http_spdy_nv_vsize("Wed, 31 Dec 1986 10:00:00 GMT");
}
if (r->headers_out.location
&& r->headers_out.location->value.len
&& r->headers_out.location->value.data[0] == '/')
{
r->headers_out.location->hash = 0;
if (clcf->server_name_in_redirect) {
cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
host = cscf->server_name;
} else if (r->headers_in.server.len) {
host = r->headers_in.server;
} else {
host.len = NGX_SOCKADDR_STRLEN;
host.data = addr;
if (ngx_connection_local_sockaddr(c, &host, 0) != NGX_OK) {
return NGX_ERROR;
}
}
switch (c->local_sockaddr->sa_family) {
#if (NGX_HAVE_INET6)
case AF_INET6:
sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
port = ntohs(sin6->sin6_port);
break;
#endif
#if (NGX_HAVE_UNIX_DOMAIN)
case AF_UNIX:
port = 0;
break;
#endif
default: /* AF_INET */
sin = (struct sockaddr_in *) c->local_sockaddr;
port = ntohs(sin->sin_port);
break;
}
len += ngx_http_spdy_nv_nsize("location")
+ ngx_http_spdy_nv_vsize("https://")
+ host.len
+ r->headers_out.location->value.len;
if (clcf->port_in_redirect) {
#if (NGX_HTTP_SSL)
if (c->ssl)
port = (port == 443) ? 0 : port;
else
#endif
port = (port == 80) ? 0 : port;
} else {
port = 0;
}
if (port) {
len += sizeof(":65535") - 1;
}
} else {
ngx_str_null(&host);
port = 0;
}
part = &r->headers_out.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
if (header[i].hash == 0) {
continue;
}
len += NGX_SPDY_NV_NLEN_SIZE + header[i].key.len
+ NGX_SPDY_NV_VLEN_SIZE + header[i].value.len;
}
buf = ngx_alloc(len, r->pool->log);
if (buf == NULL) {
return NGX_ERROR;
}
last = buf + NGX_SPDY_NV_NUM_SIZE;
last = ngx_http_spdy_nv_write_name(last, "version");
last = ngx_http_spdy_nv_write_val(last, "HTTP/1.1");
last = ngx_http_spdy_nv_write_name(last, "status");
last = ngx_spdy_frame_write_uint16(last, 3);
last = ngx_sprintf(last, "%03ui", r->headers_out.status);
count = 2;
if (r->headers_out.server == NULL) {
last = ngx_http_spdy_nv_write_name(last, "server");
last = clcf->server_tokens
? ngx_http_spdy_nv_write_val(last, NGINX_VER)
: ngx_http_spdy_nv_write_val(last, "nginx");
count++;
}
if (r->headers_out.date == NULL) {
last = ngx_http_spdy_nv_write_name(last, "date");
last = ngx_http_spdy_nv_write_vlen(last, ngx_cached_http_time.len);
last = ngx_cpymem(last, ngx_cached_http_time.data,
ngx_cached_http_time.len);
count++;
}
if (r->headers_out.content_type.len) {
last = ngx_http_spdy_nv_write_name(last, "content-type");
p = last + NGX_SPDY_NV_VLEN_SIZE;
last = ngx_cpymem(p, r->headers_out.content_type.data,
r->headers_out.content_type.len);
if (r->headers_out.content_type_len == r->headers_out.content_type.len
&& r->headers_out.charset.len)
{
last = ngx_cpymem(last, "; charset=", sizeof("; charset=") - 1);
last = ngx_cpymem(last, r->headers_out.charset.data,
r->headers_out.charset.len);
/* update r->headers_out.content_type for possible logging */
r->headers_out.content_type.len = last - p;
r->headers_out.content_type.data = p;
}
(void) ngx_http_spdy_nv_write_vlen(p - NGX_SPDY_NV_VLEN_SIZE,
r->headers_out.content_type.len);
count++;
}
if (r->headers_out.content_length == NULL
&& r->headers_out.content_length_n >= 0)
{
last = ngx_http_spdy_nv_write_name(last, "content-length");
p = last + NGX_SPDY_NV_VLEN_SIZE;
last = ngx_sprintf(p, "%O", r->headers_out.content_length_n);
(void) ngx_http_spdy_nv_write_vlen(p - NGX_SPDY_NV_VLEN_SIZE,
last - p);
count++;
}
if (r->headers_out.last_modified == NULL
&& r->headers_out.last_modified_time != -1)
{
last = ngx_http_spdy_nv_write_name(last, "last-modified");
p = last + NGX_SPDY_NV_VLEN_SIZE;
last = ngx_http_time(p, r->headers_out.last_modified_time);
(void) ngx_http_spdy_nv_write_vlen(p - NGX_SPDY_NV_VLEN_SIZE,
last - p);
count++;
}
if (host.data) {
last = ngx_http_spdy_nv_write_name(last, "location");
p = last + NGX_SPDY_NV_VLEN_SIZE;
last = ngx_cpymem(p, "http", sizeof("http") - 1);
#if (NGX_HTTP_SSL)
if (c->ssl) {
*last++ ='s';
}
#endif
*last++ = ':'; *last++ = '/'; *last++ = '/';
last = ngx_cpymem(last, host.data, host.len);
if (port) {
last = ngx_sprintf(last, ":%ui", port);
}
last = ngx_cpymem(last, r->headers_out.location->value.data,
r->headers_out.location->value.len);
/* update r->headers_out.location->value for possible logging */
r->headers_out.location->value.len = last - p;
r->headers_out.location->value.data = p;
ngx_str_set(&r->headers_out.location->key, "location");
(void) ngx_http_spdy_nv_write_vlen(p - NGX_SPDY_NV_VLEN_SIZE,
r->headers_out.location->value.len);
count++;
}
part = &r->headers_out.headers.part;
header = part->elts;
for (i = 0; /* void */; i++) {
if (i >= part->nelts) {
if (part->next == NULL) {
break;
}
part = part->next;
header = part->elts;
i = 0;
}
if (header[i].hash == 0 || header[i].hash == 2) {
continue;
}
if ((header[i].key.len == 6
&& ngx_strncasecmp(header[i].key.data,
(u_char *) "status", 6) == 0)
|| (header[i].key.len == 7
&& ngx_strncasecmp(header[i].key.data,
(u_char *) "version", 7) == 0))
{
header[i].hash = 0;
continue;
}
last = ngx_http_spdy_nv_write_nlen(last, header[i].key.len);
ngx_strlow(last, header[i].key.data, header[i].key.len);
last += header[i].key.len;
p = last + NGX_SPDY_NV_VLEN_SIZE;
last = ngx_cpymem(p, header[i].value.data, header[i].value.len);
pt = part;
h = header;
for (j = i + 1; /* void */; j++) {
if (j >= pt->nelts) {
if (pt->next == NULL) {
break;
}
pt = pt->next;
h = pt->elts;
j = 0;
}
if (h[j].hash == 0 || h[j].hash == 2
|| h[j].key.len != header[i].key.len
|| ngx_strncasecmp(header[i].key.data, h[j].key.data,
header[i].key.len))
{
continue;
}
*last++ = '\0';
last = ngx_cpymem(last, h[j].value.data, h[j].value.len);
h[j].hash = 2;
}
(void) ngx_http_spdy_nv_write_vlen(p - NGX_SPDY_NV_VLEN_SIZE,
last - p);
count++;
}
(void) ngx_spdy_frame_write_uint16(buf, count);
stream = r->spdy_stream;
sc = stream->connection;
len = last - buf;
b = ngx_create_temp_buf(r->pool, NGX_SPDY_FRAME_HEADER_SIZE
+ NGX_SPDY_SYN_REPLY_SIZE
+ deflateBound(&sc->zstream_out, len));
if (b == NULL) {
ngx_free(buf);
return NGX_ERROR;
}
b->last += NGX_SPDY_FRAME_HEADER_SIZE + NGX_SPDY_SYN_REPLY_SIZE;
sc->zstream_out.next_in = buf;
sc->zstream_out.avail_in = len;
sc->zstream_out.next_out = b->last;
sc->zstream_out.avail_out = b->end - b->last;
rc = deflate(&sc->zstream_out, Z_SYNC_FLUSH);
ngx_free(buf);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, c->log, 0,
"spdy deflate() failed: %d", rc);
return NGX_ERROR;
}
ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
"spdy deflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
sc->zstream_out.next_in, sc->zstream_out.next_out,
sc->zstream_out.avail_in, sc->zstream_out.avail_out,
rc);
b->last = sc->zstream_out.next_out;
p = b->pos;
p = ngx_spdy_frame_write_head(p, NGX_SPDY_SYN_REPLY);
len = b->last - b->pos;
r->header_size = len;
if (r->header_only) {
b->last_buf = 1;
p = ngx_spdy_frame_write_flags_and_len(p, NGX_SPDY_FLAG_FIN,
len - NGX_SPDY_FRAME_HEADER_SIZE);
} else {
p = ngx_spdy_frame_write_flags_and_len(p, 0,
len - NGX_SPDY_FRAME_HEADER_SIZE);
}
(void) ngx_spdy_frame_write_sid(p, stream->id);
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
cl->buf = b;
cl->next = NULL;
frame = ngx_palloc(r->pool, sizeof(ngx_http_spdy_out_frame_t));
if (frame == NULL) {
return NGX_ERROR;
}
frame->first = cl;
frame->last = cl;
frame->handler = ngx_http_spdy_syn_frame_handler;
frame->free = NULL;
frame->stream = stream;
frame->size = len;
frame->priority = stream->priority;
frame->blocked = 1;
frame->fin = r->header_only;
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, stream->request->connection->log, 0,
"spdy:%ui create SYN_REPLY frame %p: size:%uz",
stream->id, frame, frame->size);
ngx_http_spdy_queue_blocked_frame(sc, frame);
r->blocked++;
cln = ngx_http_cleanup_add(r, 0);
if (cln == NULL) {
return NGX_ERROR;
}
cln->handler = ngx_http_spdy_filter_cleanup;
cln->data = stream;
stream->waiting = 1;
return ngx_http_spdy_filter_send(c, stream);
}
static ngx_int_t
ngx_http_spdy_body_filter(ngx_http_request_t *r, ngx_chain_t *in)
{
off_t size;
ngx_buf_t *b;
ngx_chain_t *cl, *ll, *out, **ln;
ngx_http_spdy_stream_t *stream;
ngx_http_spdy_out_frame_t *frame;
stream = r->spdy_stream;
if (stream == NULL) {
return ngx_http_next_body_filter(r, in);
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"spdy body filter \"%V?%V\"", &r->uri, &r->args);
if (in == NULL || r->header_only) {
if (stream->waiting) {
return NGX_AGAIN;
}
r->connection->buffered &= ~NGX_SPDY_WRITE_BUFFERED;
return NGX_OK;
}
size = 0;
ln = &out;
ll = in;
for ( ;; ) {
b = ll->buf;
#if 1
if (ngx_buf_size(b) == 0 && !ngx_buf_special(b)) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
"zero size buf in spdy body filter "
"t:%d r:%d f:%d %p %p-%p %p %O-%O",
b->temporary,
b->recycled,
b->in_file,
b->start,
b->pos,
b->last,
b->file,
b->file_pos,
b->file_last);
ngx_debug_point();
return NGX_ERROR;
}
#endif
cl = ngx_alloc_chain_link(r->pool);
if (cl == NULL) {
return NGX_ERROR;
}
size += ngx_buf_size(b);
cl->buf = b;
*ln = cl;
ln = &cl->next;
if (ll->next == NULL) {
break;
}
ll = ll->next;
}
if (size > NGX_SPDY_MAX_FRAME_SIZE) {
ngx_log_error(NGX_LOG_ALERT, r->connection->log, 0,
"FIXME: chain too big in spdy filter: %O", size);
return NGX_ERROR;
}
frame = ngx_http_spdy_filter_get_data_frame(stream, (size_t) size,
b->last_buf, out, cl);
if (frame == NULL) {
return NGX_ERROR;
}
ngx_http_spdy_queue_frame(stream->connection, frame);
stream->waiting++;
r->main->blocked++;
return ngx_http_spdy_filter_send(r->connection, stream);
}
static ngx_http_spdy_out_frame_t *
ngx_http_spdy_filter_get_data_frame(ngx_http_spdy_stream_t *stream,
size_t len, ngx_uint_t fin, ngx_chain_t *first, ngx_chain_t *last)
{
u_char *p;
ngx_buf_t *buf;
ngx_uint_t flags;
ngx_chain_t *cl;
ngx_http_spdy_out_frame_t *frame;
frame = stream->free_frames;
if (frame) {
stream->free_frames = frame->free;
} else {
frame = ngx_palloc(stream->request->pool,
sizeof(ngx_http_spdy_out_frame_t));
if (frame == NULL) {
return NULL;
}
}
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, stream->request->connection->log, 0,
"spdy:%ui create DATA frame %p: len:%uz fin:%ui",
stream->id, frame, len, fin);
if (len || fin) {
flags = fin ? NGX_SPDY_FLAG_FIN : 0;
cl = ngx_chain_get_free_buf(stream->request->pool,
&stream->free_data_headers);
if (cl == NULL) {
return NULL;
}
buf = cl->buf;
if (buf->start) {
p = buf->start;
buf->pos = p;
p += sizeof(uint32_t);
(void) ngx_spdy_frame_write_flags_and_len(p, flags, len);
} else {
p = ngx_palloc(stream->request->pool, NGX_SPDY_FRAME_HEADER_SIZE);
if (p == NULL) {
return NULL;
}
buf->pos = p;
buf->start = p;
p = ngx_spdy_frame_write_sid(p, stream->id);
p = ngx_spdy_frame_write_flags_and_len(p, flags, len);
buf->last = p;
buf->end = p;
buf->tag = (ngx_buf_tag_t) &ngx_http_spdy_filter_module;
buf->memory = 1;
}
cl->next = first;
first = cl;
}
frame->first = first;
frame->last = last;
frame->handler = ngx_http_spdy_data_frame_handler;
frame->free = NULL;
frame->stream = stream;
frame->size = NGX_SPDY_FRAME_HEADER_SIZE + len;
frame->priority = stream->priority;
frame->blocked = 0;
frame->fin = fin;
return frame;
}
static ngx_inline ngx_int_t
ngx_http_spdy_filter_send(ngx_connection_t *fc, ngx_http_spdy_stream_t *stream)
{
if (ngx_http_spdy_send_output_queue(stream->connection) == NGX_ERROR) {
fc->error = 1;
return NGX_ERROR;
}
if (stream->waiting) {
fc->buffered |= NGX_SPDY_WRITE_BUFFERED;
fc->write->delayed = 1;
return NGX_AGAIN;
}
fc->buffered &= ~NGX_SPDY_WRITE_BUFFERED;
return NGX_OK;
}
static ngx_int_t
ngx_http_spdy_syn_frame_handler(ngx_http_spdy_connection_t *sc,
ngx_http_spdy_out_frame_t *frame)
{
ngx_buf_t *buf;
ngx_http_spdy_stream_t *stream;
buf = frame->first->buf;
if (buf->pos != buf->last) {
return NGX_AGAIN;
}
stream = frame->stream;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy:%ui SYN_REPLY frame %p was sent", stream->id, frame);
ngx_free_chain(stream->request->pool, frame->first);
ngx_http_spdy_handle_frame(stream, frame);
ngx_http_spdy_handle_stream(sc, stream);
return NGX_OK;
}
static ngx_int_t
ngx_http_spdy_data_frame_handler(ngx_http_spdy_connection_t *sc,
ngx_http_spdy_out_frame_t *frame)
{
ngx_chain_t *cl, *ln;
ngx_http_spdy_stream_t *stream;
stream = frame->stream;
cl = frame->first;
if (cl->buf->tag == (ngx_buf_tag_t) &ngx_http_spdy_filter_module) {
if (cl->buf->pos != cl->buf->last) {
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy:%ui DATA frame %p was sent partially",
stream->id, frame);
return NGX_AGAIN;
}
ln = cl->next;
cl->next = stream->free_data_headers;
stream->free_data_headers = cl;
if (cl == frame->last) {
goto done;
}
cl = ln;
}
for ( ;; ) {
if (ngx_buf_size(cl->buf) != 0) {
if (cl != frame->first) {
frame->first = cl;
ngx_http_spdy_handle_stream(sc, stream);
}
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy:%ui DATA frame %p was sent partially",
stream->id, frame);
return NGX_AGAIN;
}
ln = cl->next;
ngx_free_chain(stream->request->pool, cl);
if (cl == frame->last) {
goto done;
}
cl = ln;
}
done:
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy:%ui DATA frame %p was sent", stream->id, frame);
stream->request->header_size += NGX_SPDY_FRAME_HEADER_SIZE;
ngx_http_spdy_handle_frame(stream, frame);
ngx_http_spdy_handle_stream(sc, stream);
return NGX_OK;
}
static ngx_inline void
ngx_http_spdy_handle_frame(ngx_http_spdy_stream_t *stream,
ngx_http_spdy_out_frame_t *frame)
{
ngx_http_request_t *r;
r = stream->request;
r->connection->sent += frame->size;
r->blocked--;
if (frame->fin) {
stream->out_closed = 1;
}
frame->free = stream->free_frames;
stream->free_frames = frame;
stream->waiting--;
}
static ngx_inline void
ngx_http_spdy_handle_stream(ngx_http_spdy_connection_t *sc,
ngx_http_spdy_stream_t *stream)
{
ngx_connection_t *fc;
fc = stream->request->connection;
fc->write->delayed = 0;
if (stream->handled) {
return;
}
if (sc->blocked == 2) {
stream->handled = 1;
stream->next = sc->last_stream;
sc->last_stream = stream;
}
}
static void
ngx_http_spdy_filter_cleanup(void *data)
{
ngx_http_spdy_stream_t *stream = data;
ngx_http_request_t *r;
ngx_http_spdy_out_frame_t *frame, **fn;
if (stream->waiting == 0) {
return;
}
r = stream->request;
fn = &stream->connection->last_out;
for ( ;; ) {
frame = *fn;
if (frame == NULL) {
break;
}
if (frame->stream == stream && !frame->blocked) {
stream->waiting--;
r->blocked--;
*fn = frame->next;
continue;
}
fn = &frame->next;
}
}
static ngx_int_t
ngx_http_spdy_filter_init(ngx_conf_t *cf)
{
ngx_http_next_header_filter = ngx_http_top_header_filter;
ngx_http_top_header_filter = ngx_http_spdy_header_filter;
ngx_http_next_body_filter = ngx_http_top_body_filter;
ngx_http_top_body_filter = ngx_http_spdy_body_filter;
return NGX_OK;
}

View File

@ -0,0 +1,351 @@
/*
* Copyright (C) Nginx, Inc.
* Copyright (C) Valentin V. Bartenev
*/
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
#include <ngx_http_spdy_module.h>
static ngx_int_t ngx_http_spdy_add_variables(ngx_conf_t *cf);
static ngx_int_t ngx_http_spdy_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_spdy_request_priority_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data);
static ngx_int_t ngx_http_spdy_module_init(ngx_cycle_t *cycle);
static void *ngx_http_spdy_create_main_conf(ngx_conf_t *cf);
static char *ngx_http_spdy_init_main_conf(ngx_conf_t *cf, void *conf);
static void *ngx_http_spdy_create_srv_conf(ngx_conf_t *cf);
static char *ngx_http_spdy_merge_srv_conf(ngx_conf_t *cf, void *parent,
void *child);
static char *ngx_http_spdy_recv_buffer_size(ngx_conf_t *cf, void *post,
void *data);
static char *ngx_http_spdy_pool_size(ngx_conf_t *cf, void *post, void *data);
static char *ngx_http_spdy_streams_index_mask(ngx_conf_t *cf, void *post,
void *data);
static ngx_conf_num_bounds_t ngx_http_spdy_headers_comp_bounds = {
ngx_conf_check_num_bounds, 0, 9
};
static ngx_conf_post_t ngx_http_spdy_recv_buffer_size_post =
{ ngx_http_spdy_recv_buffer_size };
static ngx_conf_post_t ngx_http_spdy_pool_size_post =
{ ngx_http_spdy_pool_size };
static ngx_conf_post_t ngx_http_spdy_streams_index_mask_post =
{ ngx_http_spdy_streams_index_mask };
static ngx_command_t ngx_http_spdy_commands[] = {
{ ngx_string("spdy_recv_buffer_size"),
NGX_HTTP_MAIN_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_MAIN_CONF_OFFSET,
offsetof(ngx_http_spdy_main_conf_t, recv_buffer_size),
&ngx_http_spdy_recv_buffer_size_post },
{ ngx_string("spdy_pool_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_size_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_spdy_srv_conf_t, pool_size),
&ngx_http_spdy_pool_size_post },
{ ngx_string("spdy_max_concurrent_streams"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_spdy_srv_conf_t, concurrent_streams),
NULL },
{ ngx_string("spdy_streams_index_size"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_spdy_srv_conf_t, streams_index_mask),
&ngx_http_spdy_streams_index_mask_post },
{ ngx_string("spdy_recv_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_spdy_srv_conf_t, recv_timeout),
NULL },
{ ngx_string("spdy_keepalive_timeout"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_msec_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_spdy_srv_conf_t, keepalive_timeout),
NULL },
{ ngx_string("spdy_headers_comp"),
NGX_HTTP_MAIN_CONF|NGX_HTTP_SRV_CONF|NGX_CONF_TAKE1,
ngx_conf_set_num_slot,
NGX_HTTP_SRV_CONF_OFFSET,
offsetof(ngx_http_spdy_srv_conf_t, headers_comp),
&ngx_http_spdy_headers_comp_bounds },
ngx_null_command
};
static ngx_http_module_t ngx_http_spdy_module_ctx = {
ngx_http_spdy_add_variables, /* preconfiguration */
NULL, /* postconfiguration */
ngx_http_spdy_create_main_conf, /* create main configuration */
ngx_http_spdy_init_main_conf, /* init main configuration */
ngx_http_spdy_create_srv_conf, /* create server configuration */
ngx_http_spdy_merge_srv_conf, /* merge server configuration */
NULL, /* create location configuration */
NULL /* merge location configuration */
};
ngx_module_t ngx_http_spdy_module = {
NGX_MODULE_V1,
&ngx_http_spdy_module_ctx, /* module context */
ngx_http_spdy_commands, /* module directives */
NGX_HTTP_MODULE, /* module type */
NULL, /* init master */
ngx_http_spdy_module_init, /* init module */
NULL, /* init process */
NULL, /* init thread */
NULL, /* exit thread */
NULL, /* exit process */
NULL, /* exit master */
NGX_MODULE_V1_PADDING
};
static ngx_http_variable_t ngx_http_spdy_vars[] = {
{ ngx_string("spdy"), NULL,
ngx_http_spdy_variable, 0, 0, 0 },
{ ngx_string("spdy_request_priority"), NULL,
ngx_http_spdy_request_priority_variable, 0, 0, 0 },
{ ngx_null_string, NULL, NULL, 0, 0, 0 }
};
static ngx_int_t
ngx_http_spdy_add_variables(ngx_conf_t *cf)
{
ngx_http_variable_t *var, *v;
for (v = ngx_http_spdy_vars; v->name.len; v++) {
var = ngx_http_add_variable(cf, &v->name, v->flags);
if (var == NULL) {
return NGX_ERROR;
}
var->get_handler = v->get_handler;
var->data = v->data;
}
return NGX_OK;
}
static ngx_int_t
ngx_http_spdy_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
if (r->spdy_stream) {
v->len = 1;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = (u_char *) "2";
return NGX_OK;
}
*v = ngx_http_variable_null_value;
return NGX_OK;
}
static ngx_int_t
ngx_http_spdy_request_priority_variable(ngx_http_request_t *r,
ngx_http_variable_value_t *v, uintptr_t data)
{
if (r->spdy_stream) {
v->len = 1;
v->valid = 1;
v->no_cacheable = 0;
v->not_found = 0;
v->data = ngx_pnalloc(r->pool, 1);
if (v->data == NULL) {
return NGX_ERROR;
}
v->data[0] = '0' + (u_char) r->spdy_stream->priority;
return NGX_OK;
}
*v = ngx_http_variable_null_value;
return NGX_OK;
}
static ngx_int_t
ngx_http_spdy_module_init(ngx_cycle_t *cycle)
{
ngx_http_spdy_request_headers_init();
return NGX_OK;
}
static void *
ngx_http_spdy_create_main_conf(ngx_conf_t *cf)
{
ngx_http_spdy_main_conf_t *smcf;
smcf = ngx_pcalloc(cf->pool, sizeof(ngx_http_spdy_main_conf_t));
if (smcf == NULL) {
return NULL;
}
smcf->recv_buffer_size = NGX_CONF_UNSET_SIZE;
return smcf;
}
static char *
ngx_http_spdy_init_main_conf(ngx_conf_t *cf, void *conf)
{
ngx_http_spdy_main_conf_t *smcf = conf;
if (smcf->recv_buffer_size == NGX_CONF_UNSET_SIZE) {
smcf->recv_buffer_size = 256 * 1024;
}
return NGX_CONF_OK;
}
static void *
ngx_http_spdy_create_srv_conf(ngx_conf_t *cf)
{
ngx_http_spdy_srv_conf_t *sscf;
sscf = ngx_pcalloc(cf->pool, sizeof(ngx_http_spdy_srv_conf_t));
if (sscf == NULL) {
return NULL;
}
sscf->pool_size = NGX_CONF_UNSET_SIZE;
sscf->concurrent_streams = NGX_CONF_UNSET_UINT;
sscf->streams_index_mask = NGX_CONF_UNSET_UINT;
sscf->recv_timeout = NGX_CONF_UNSET_MSEC;
sscf->keepalive_timeout = NGX_CONF_UNSET_MSEC;
sscf->headers_comp = NGX_CONF_UNSET;
return sscf;
}
static char *
ngx_http_spdy_merge_srv_conf(ngx_conf_t *cf, void *parent, void *child)
{
ngx_http_spdy_srv_conf_t *prev = parent;
ngx_http_spdy_srv_conf_t *conf = child;
ngx_conf_merge_size_value(conf->pool_size, prev->pool_size, 4096);
ngx_conf_merge_uint_value(conf->concurrent_streams,
prev->concurrent_streams, 100);
ngx_conf_merge_uint_value(conf->streams_index_mask,
prev->streams_index_mask, 32 - 1);
ngx_conf_merge_msec_value(conf->recv_timeout,
prev->recv_timeout, 30000);
ngx_conf_merge_msec_value(conf->keepalive_timeout,
prev->keepalive_timeout, 180000);
ngx_conf_merge_value(conf->headers_comp, prev->headers_comp, 0);
return NGX_CONF_OK;
}
static char *
ngx_http_spdy_recv_buffer_size(ngx_conf_t *cf, void *post, void *data)
{
size_t *sp = data;
if (*sp <= 2 * NGX_SPDY_STATE_BUFFER_SIZE) {
return "value is too small";
}
return NGX_CONF_OK;
}
static char *
ngx_http_spdy_pool_size(ngx_conf_t *cf, void *post, void *data)
{
size_t *sp = data;
if (*sp < NGX_MIN_POOL_SIZE) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the pool size must be no less than %uz",
NGX_MIN_POOL_SIZE);
return NGX_CONF_ERROR;
}
if (*sp % NGX_POOL_ALIGNMENT) {
ngx_conf_log_error(NGX_LOG_EMERG, cf, 0,
"the pool size must be a multiple of %uz",
NGX_POOL_ALIGNMENT);
return NGX_CONF_ERROR;
}
return NGX_CONF_OK;
}
static char *
ngx_http_spdy_streams_index_mask(ngx_conf_t *cf, void *post, void *data)
{
ngx_uint_t *np = data;
ngx_uint_t mask;
mask = *np - 1;
if (*np == 0 || (*np & mask)) {
return "must be a power of two";
}
*np = mask;
return NGX_CONF_OK;
}

View File

@ -0,0 +1,36 @@
/*
* Copyright (C) Nginx, Inc.
* Copyright (C) Valentin V. Bartenev
*/
#ifndef _NGX_HTTP_SPDY_MODULE_H_INCLUDED_
#define _NGX_HTTP_SPDY_MODULE_H_INCLUDED_
#include <ngx_config.h>
#include <ngx_core.h>
#include <ngx_http.h>
typedef struct {
size_t recv_buffer_size;
u_char *recv_buffer;
} ngx_http_spdy_main_conf_t;
typedef struct {
size_t pool_size;
ngx_uint_t concurrent_streams;
ngx_uint_t streams_index_mask;
ngx_msec_t recv_timeout;
ngx_msec_t keepalive_timeout;
ngx_int_t headers_comp;
} ngx_http_spdy_srv_conf_t;
extern ngx_module_t ngx_http_spdy_module;
#endif /* _NGX_HTTP_SPDY_MODULE_H_INCLUDED_ */

View File

@ -440,6 +440,13 @@ ngx_http_upstream_init(ngx_http_request_t *r)
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"http init upstream, client timer: %d", c->read->timer_set);
#if (NGX_HTTP_SPDY)
if (r->spdy_stream) {
ngx_http_upstream_init_request(r);
return;
}
#endif
if (c->read->timer_set) {
ngx_del_timer(c->read);
}
@ -1018,6 +1025,12 @@ ngx_http_upstream_check_broken_connection(ngx_http_request_t *r,
return;
}
#if (NGX_HTTP_SPDY)
if (r->spdy_stream) {
return;
}
#endif
#if (NGX_HAVE_KQUEUE)
if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {