/*
* 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>
#include <zlib.h>
#if (NGX_HAVE_LITTLE_ENDIAN && NGX_HAVE_NONALIGNED)
#define ngx_str5cmp(m, c0, c1, c2, c3, c4) \
*(uint32_t *) m == (c3 << 24 | c2 << 16 | c1 << 8 | c0) \
&& m[4] == c4
#else
#define ngx_str5cmp(m, c0, c1, c2, c3, c4) \
m[0] == c0 && m[1] == c1 && m[2] == c2 && m[3] == c3 && m[4] == c4
#endif
#if (NGX_HAVE_NONALIGNED)
#define ngx_spdy_frame_parse_uint16(p) ntohs(*(uint16_t *) (p))
#define ngx_spdy_frame_parse_uint32(p) ntohl(*(uint32_t *) (p))
#else
#define ngx_spdy_frame_parse_uint16(p) ((p)[0] << 8 | (p)[1])
#define ngx_spdy_frame_parse_uint32(p) \
((p)[0] << 24 | (p)[1] << 16 | (p)[2] << 8 | (p)[3])
#endif
#define ngx_spdy_frame_parse_sid(p) \
(ngx_spdy_frame_parse_uint32(p) & 0x7fffffff)
#define ngx_spdy_ctl_frame_check(h) \
(((h) & 0xffffff00) == ngx_spdy_ctl_frame_head(0))
#define ngx_spdy_data_frame_check(h) \
(!((h) & (uint32_t) NGX_SPDY_CTL_BIT << 31))
#define ngx_spdy_ctl_frame_type(h) ((h) & 0x000000ff)
#define ngx_spdy_frame_flags(p) ((p) >> 24)
#define ngx_spdy_frame_length(p) ((p) & 0x00ffffff)
#define NGX_SPDY_SKIP_HEADERS_BUFFER_SIZE 4096
#define NGX_SPDY_CTL_FRAME_BUFFER_SIZE 16
#define NGX_SPDY_PROTOCOL_ERROR 1
#define NGX_SPDY_INVALID_STREAM 2
#define NGX_SPDY_REFUSED_STREAM 3
#define NGX_SPDY_UNSUPPORTED_VERSION 4
#define NGX_SPDY_CANCEL 5
#define NGX_SPDY_INTERNAL_ERROR 6
#define NGX_SPDY_FLOW_CONTROL_ERROR 7
#define NGX_SPDY_SETTINGS_MAX_STREAMS 4
#define NGX_SPDY_SETTINGS_FLAG_PERSIST 0x01
typedef struct {
ngx_uint_t hash;
u_char len;
u_char header[7];
ngx_int_t (*handler)(ngx_http_request_t *r);
} ngx_http_spdy_request_header_t;
static void ngx_http_spdy_read_handler(ngx_event_t *rev);
static void ngx_http_spdy_write_handler(ngx_event_t *wev);
static void ngx_http_spdy_handle_connection(ngx_http_spdy_connection_t *sc);
static u_char *ngx_http_spdy_state_detect_settings(
ngx_http_spdy_connection_t *sc, u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_head(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_syn_stream(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_headers(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_headers_error(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_headers_skip(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_data(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_rst_stream(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_ping(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_skip(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_settings(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_noop(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_complete(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end);
static u_char *ngx_http_spdy_state_save(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end, ngx_http_spdy_handler_pt handler);
static u_char *ngx_http_spdy_state_protocol_error(
ngx_http_spdy_connection_t *sc);
static u_char *ngx_http_spdy_state_internal_error(
ngx_http_spdy_connection_t *sc);
static ngx_int_t ngx_http_spdy_send_rst_stream(ngx_http_spdy_connection_t *sc,
ngx_uint_t sid, ngx_uint_t status, ngx_uint_t priority);
static ngx_int_t ngx_http_spdy_send_settings(ngx_http_spdy_connection_t *sc);
static ngx_int_t ngx_http_spdy_settings_frame_handler(
ngx_http_spdy_connection_t *sc, ngx_http_spdy_out_frame_t *frame);
static ngx_http_spdy_out_frame_t *ngx_http_spdy_get_ctl_frame(
ngx_http_spdy_connection_t *sc, size_t size, ngx_uint_t priority);
static ngx_int_t ngx_http_spdy_ctl_frame_handler(
ngx_http_spdy_connection_t *sc, ngx_http_spdy_out_frame_t *frame);
static ngx_http_spdy_stream_t *ngx_http_spdy_create_stream(
ngx_http_spdy_connection_t *sc, ngx_uint_t id, ngx_uint_t priority);
static ngx_http_spdy_stream_t *ngx_http_spdy_get_stream_by_id(
ngx_http_spdy_connection_t *sc, ngx_uint_t sid);
#define ngx_http_spdy_streams_index_size(sscf) (sscf->streams_index_mask + 1)
#define ngx_http_spdy_stream_index(sscf, sid) \
((sid >> 1) & sscf->streams_index_mask)
static ngx_int_t ngx_http_spdy_parse_header(ngx_http_request_t *r);
static ngx_int_t ngx_http_spdy_alloc_large_header_buffer(ngx_http_request_t *r);
static ngx_int_t ngx_http_spdy_handle_request_header(ngx_http_request_t *r);
static ngx_int_t ngx_http_spdy_parse_method(ngx_http_request_t *r);
static ngx_int_t ngx_http_spdy_parse_scheme(ngx_http_request_t *r);
static ngx_int_t ngx_http_spdy_parse_url(ngx_http_request_t *r);
static ngx_int_t ngx_http_spdy_parse_version(ngx_http_request_t *r);
static ngx_int_t ngx_http_spdy_construct_request_line(ngx_http_request_t *r);
static void ngx_http_spdy_run_request(ngx_http_request_t *r);
static ngx_int_t ngx_http_spdy_init_request_body(ngx_http_request_t *r);
static void ngx_http_spdy_handle_connection_handler(ngx_event_t *rev);
static void ngx_http_spdy_keepalive_handler(ngx_event_t *rev);
static void ngx_http_spdy_finalize_connection(ngx_http_spdy_connection_t *sc,
ngx_int_t rc);
static void ngx_http_spdy_pool_cleanup(void *data);
static void *ngx_http_spdy_zalloc(void *opaque, u_int items, u_int size);
static void ngx_http_spdy_zfree(void *opaque, void *address);
static const u_char ngx_http_spdy_dict[] =
"options" "get" "head" "post" "put" "delete" "trace"
"accept" "accept-charset" "accept-encoding" "accept-language"
"authorization" "expect" "from" "host"
"if-modified-since" "if-match" "if-none-match" "if-range"
"if-unmodifiedsince" "max-forwards" "proxy-authorization"
"range" "referer" "te" "user-agent"
"100" "101" "200" "201" "202" "203" "204" "205" "206"
"300" "301" "302" "303" "304" "305" "306" "307"
"400" "401" "402" "403" "404" "405" "406" "407" "408" "409" "410"
"411" "412" "413" "414" "415" "416" "417"
"500" "501" "502" "503" "504" "505"
"accept-ranges" "age" "etag" "location" "proxy-authenticate" "public"
"retry-after" "server" "vary" "warning" "www-authenticate" "allow"
"content-base" "content-encoding" "cache-control" "connection" "date"
"trailer" "transfer-encoding" "upgrade" "via" "warning"
"content-language" "content-length" "content-location"
"content-md5" "content-range" "content-type" "etag" "expires"
"last-modified" "set-cookie"
"Monday" "Tuesday" "Wednesday" "Thursday" "Friday" "Saturday" "Sunday"
"Jan" "Feb" "Mar" "Apr" "May" "Jun" "Jul" "Aug" "Sep" "Oct" "Nov" "Dec"
"chunked" "text/html" "image/png" "image/jpg" "image/gif"
"application/xml" "application/xhtml" "text/plain" "public" "max-age"
"charset=iso-8859-1" "utf-8" "gzip" "deflate" "HTTP/1.1" "status"
"version" "url";
static ngx_http_spdy_request_header_t ngx_http_spdy_request_headers[] = {
{ 0, 6, "method", ngx_http_spdy_parse_method },
{ 0, 6, "scheme", ngx_http_spdy_parse_scheme },
{ 0, 3, "url", ngx_http_spdy_parse_url },
{ 0, 7, "version", ngx_http_spdy_parse_version },
};
#define NGX_SPDY_REQUEST_HEADERS \
(sizeof(ngx_http_spdy_request_headers) \
/ sizeof(ngx_http_spdy_request_header_t))
void
ngx_http_spdy_init(ngx_event_t *rev)
{
int rc;
ngx_connection_t *c;
ngx_pool_cleanup_t *cln;
ngx_http_connection_t *hc;
ngx_http_spdy_srv_conf_t *sscf;
ngx_http_spdy_main_conf_t *smcf;
ngx_http_spdy_connection_t *sc;
c = rev->data;
hc = c->data;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"init spdy request");
c->log->action = "processing SPDY";
smcf = ngx_http_get_module_main_conf(hc->conf_ctx, ngx_http_spdy_module);
if (smcf->recv_buffer == NULL) {
smcf->recv_buffer = ngx_palloc(ngx_cycle->pool, smcf->recv_buffer_size);
if (smcf->recv_buffer == NULL) {
ngx_http_close_connection(c);
return;
}
}
sc = ngx_pcalloc(c->pool, sizeof(ngx_http_spdy_connection_t));
if (sc == NULL) {
ngx_http_close_connection(c);
return;
}
sc->connection = c;
sc->http_connection = hc;
sc->handler = ngx_http_spdy_state_detect_settings;
sc->zstream_in.zalloc = ngx_http_spdy_zalloc;
sc->zstream_in.zfree = ngx_http_spdy_zfree;
sc->zstream_in.opaque = sc;
rc = inflateInit(&sc->zstream_in);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, c->log, 0,
"inflateInit() failed: %d", rc);
ngx_http_close_connection(c);
return;
}
sc->zstream_out.zalloc = ngx_http_spdy_zalloc;
sc->zstream_out.zfree = ngx_http_spdy_zfree;
sc->zstream_out.opaque = sc;
sscf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_spdy_module);
rc = deflateInit2(&sc->zstream_out, (int) sscf->headers_comp,
Z_DEFLATED, 11, 4, Z_DEFAULT_STRATEGY);
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, c->log, 0,
"deflateInit2() failed: %d", rc);
ngx_http_close_connection(c);
return;
}
rc = deflateSetDictionary(&sc->zstream_out, ngx_http_spdy_dict,
sizeof(ngx_http_spdy_dict));
if (rc != Z_OK) {
ngx_log_error(NGX_LOG_ALERT, c->log, 0,
"deflateSetDictionary() failed: %d", rc);
ngx_http_close_connection(c);
return;
}
sc->pool = ngx_create_pool(sscf->pool_size, sc->connection->log);
if (sc->pool == NULL) {
ngx_http_close_connection(c);
return;
}
cln = ngx_pool_cleanup_add(c->pool, sizeof(ngx_pool_cleanup_file_t));
if (cln == NULL) {
ngx_http_close_connection(c);
return;
}
cln->handler = ngx_http_spdy_pool_cleanup;
cln->data = sc;
sc->streams_index = ngx_pcalloc(sc->pool,
ngx_http_spdy_streams_index_size(sscf)
* sizeof(ngx_http_spdy_stream_t *));
if (sc->streams_index == NULL) {
ngx_http_close_connection(c);
return;
}
c->data = sc;
rev->handler = ngx_http_spdy_read_handler;
c->write->handler = ngx_http_spdy_write_handler;
ngx_http_spdy_read_handler(rev);
}
static void
ngx_http_spdy_read_handler(ngx_event_t *rev)
{
u_char *p, *end;
size_t available;
ssize_t n;
ngx_connection_t *c;
ngx_http_spdy_main_conf_t *smcf;
ngx_http_spdy_connection_t *sc;
c = rev->data;
sc = c->data;
if (rev->timedout) {
ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
ngx_http_spdy_finalize_connection(sc, NGX_HTTP_REQUEST_TIME_OUT);
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "spdy read handler");
sc->blocked = 1;
smcf = ngx_http_get_module_main_conf(sc->http_connection->conf_ctx,
ngx_http_spdy_module);
available = smcf->recv_buffer_size - 2 * NGX_SPDY_STATE_BUFFER_SIZE;
do {
p = smcf->recv_buffer;
ngx_memcpy(p, sc->buffer, NGX_SPDY_STATE_BUFFER_SIZE);
end = p + sc->buffer_used;
n = c->recv(c, end, available);
if (n == NGX_AGAIN) {
break;
}
if (n == 0 && (sc->waiting || sc->processing)) {
ngx_log_error(NGX_LOG_INFO, c->log, 0,
"client closed prematurely connection");
}
if (n == 0 || n == NGX_ERROR) {
ngx_http_spdy_finalize_connection(sc,
NGX_HTTP_CLIENT_CLOSED_REQUEST);
return;
}
end += n;
sc->buffer_used = 0;
sc->waiting = 0;
do {
p = sc->handler(sc, p, end);
if (p == NULL) {
return;
}
} while (p != end);
} while (rev->ready);
if (ngx_handle_read_event(rev, 0) != NGX_OK) {
ngx_http_spdy_finalize_connection(sc, NGX_HTTP_INTERNAL_SERVER_ERROR);
return;
}
sc->blocked = 0;
if (sc->processing) {
if (rev->timer_set) {
ngx_del_timer(rev);
}
return;
}
ngx_http_spdy_handle_connection(sc);
}
static void
ngx_http_spdy_write_handler(ngx_event_t *wev)
{
ngx_int_t rc;
ngx_connection_t *c;
ngx_http_spdy_stream_t *stream, *s, *sn;
ngx_http_spdy_connection_t *sc;
c = wev->data;
sc = c->data;
if (wev->timedout) {
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
"spdy write event timed out");
ngx_http_spdy_finalize_connection(sc, NGX_HTTP_CLIENT_CLOSED_REQUEST);
return;
}
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "spdy write handler");
sc->blocked = 2;
rc = ngx_http_spdy_send_output_queue(sc);
if (rc == NGX_ERROR) {
ngx_http_spdy_finalize_connection(sc, NGX_HTTP_CLIENT_CLOSED_REQUEST);
return;
}
stream = NULL;
for (s = sc->last_stream; s; s = sn) {
sn = s->next;
s->next = stream;
stream = s;
}
sc->last_stream = NULL;
sc->blocked = 1;
for ( /* void */ ; stream; stream = sn) {
sn = stream->next;
stream->handled = 0;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
"spdy run stream %ui", stream->id);
wev = stream->request->connection->write;
wev->handler(wev);
}
sc->blocked = 0;
if (rc == NGX_AGAIN) {
return;
}
ngx_http_spdy_handle_connection(sc);
}
ngx_int_t
ngx_http_spdy_send_output_queue(ngx_http_spdy_connection_t *sc)
{
ngx_chain_t *cl;
ngx_event_t *wev;
ngx_connection_t *c;
ngx_http_core_loc_conf_t *clcf;
ngx_http_spdy_out_frame_t *out, *frame, *fn;
c = sc->connection;
if (c->error) {
return NGX_ERROR;
}
wev = c->write;
if (!wev->ready) {
return NGX_OK;
}
cl = NULL;
out = NULL;
for (frame = sc->last_out; frame; frame = fn) {
frame->last->next = cl;
cl = frame->first;
fn = frame->next;
frame->next = out;
out = frame;
ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
"spdy frame out: %p sid:%ui prio:%ui bl:%ui size:%uz",
out, out->stream ? out->stream->id : 0, out->priority,
out->blocked, out->size);
}
cl = c->send_chain(c, cl, 0);
if (cl == NGX_CHAIN_ERROR) {
c->error = 1;
if (!sc->blocked) {
ngx_post_event(wev, &ngx_posted_events);
}
return NGX_ERROR;
}
clcf = ngx_http_get_module_loc_conf(sc->http_connection->conf_ctx,
ngx_http_core_module);
if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
return NGX_ERROR; /* FIXME */
}
if (cl) {
ngx_add_timer(wev, clcf->send_timeout);
} else {
if (wev->timer_set) {
ngx_del_timer(wev);
}
}
for ( /* void */ ; out; out = out->next) {
if (out->handler(sc, out) != NGX_OK) {
out->blocked = 1;
out->priority = NGX_SPDY_HIGHEST_PRIORITY;
break;
}
ngx_log_debug4(NGX_LOG_DEBUG_HTTP, c->log, 0,
"spdy frame sent: %p sid:%ui bl:%ui size:%uz",
out, out->stream ? out->stream->id : 0,
out->blocked, out->size);
}
frame = NULL;
for ( /* void */ ; out; out = fn) {
fn = out->next;
out->next = frame;
frame = out;
}
sc->last_out = frame;
return NGX_OK;
}
static void
ngx_http_spdy_handle_connection(ngx_http_spdy_connection_t *sc)
{
ngx_connection_t *c;
ngx_http_spdy_srv_conf_t *sscf;
if (sc->last_out || sc->processing) {
return;
}
c = sc->connection;
if (c->error) {
ngx_http_close_connection(c);
return;
}
if (c->buffered) {
return;
}
sscf = ngx_http_get_module_srv_conf(sc->http_connection->conf_ctx,
ngx_http_spdy_module);
if (sc->waiting) {
ngx_add_timer(c->read, sscf->recv_timeout);
return;
}
if (ngx_terminate || ngx_exiting) {
ngx_http_close_connection(c);
return;
}
ngx_destroy_pool(sc->pool);
sc->pool = NULL;
sc->free_ctl_frames = NULL;
sc->free_fake_connections = NULL;
#if (NGX_HTTP_SSL)
if (c->ssl) {
ngx_ssl_free_buffer(c);
}
#endif
c->destroyed = 1;
c->idle = 1;
ngx_reusable_connection(c, 1);
c->write->handler = ngx_http_empty_handler;
c->read->handler = ngx_http_spdy_keepalive_handler;
if (c->write->timer_set) {
ngx_del_timer(c->write);
}
ngx_add_timer(c->read, sscf->keepalive_timeout);
}
static u_char *
ngx_http_spdy_state_detect_settings(ngx_http_spdy_connection_t *sc,
u_char *pos, u_char *end)
{
if (end - pos < NGX_SPDY_FRAME_HEADER_SIZE) {
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_detect_settings);
}
/*
* Since this is the first frame in a buffer,
* then it is properly aligned
*/
if (*(uint32_t *) pos == htonl(ngx_spdy_ctl_frame_head(NGX_SPDY_SETTINGS)))
{
sc->length = ngx_spdy_frame_length(htonl(((uint32_t *) pos)[1]));
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy SETTINGS frame received, size: %uz", sc->length);
pos += NGX_SPDY_FRAME_HEADER_SIZE;
return ngx_http_spdy_state_settings(sc, pos, end);
}
ngx_http_spdy_send_settings(sc);
return ngx_http_spdy_state_head(sc, pos, end);
}
static u_char *
ngx_http_spdy_state_head(ngx_http_spdy_connection_t *sc, u_char *pos,
u_char *end)
{
uint32_t head, flen;
if (end - pos < NGX_SPDY_FRAME_HEADER_SIZE) {
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_head);
}
head = ngx_spdy_frame_parse_uint32(pos);
pos += sizeof(uint32_t);
flen = ngx_spdy_frame_parse_uint32(pos);
sc->flags = ngx_spdy_frame_flags(flen);
sc->length = ngx_spdy_frame_length(flen);
pos += sizeof(uint32_t);
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy process frame head:%08Xd f:%ui l:%ui",
head, sc->flags, sc->length);
if (ngx_spdy_ctl_frame_check(head)) {
switch (ngx_spdy_ctl_frame_type(head)) {
case NGX_SPDY_SYN_STREAM:
return ngx_http_spdy_state_syn_stream(sc, pos, end);
case NGX_SPDY_SYN_REPLY:
return ngx_http_spdy_state_protocol_error(sc);
case NGX_SPDY_RST_STREAM:
return ngx_http_spdy_state_rst_stream(sc, pos, end);
case NGX_SPDY_SETTINGS:
return ngx_http_spdy_state_skip(sc, pos, end);
case NGX_SPDY_NOOP:
return ngx_http_spdy_state_noop(sc, pos, end);
case NGX_SPDY_PING:
return ngx_http_spdy_state_ping(sc, pos, end);
case NGX_SPDY_GOAWAY:
return ngx_http_spdy_state_skip(sc, pos, end); /* TODO */
case NGX_SPDY_HEADERS:
return ngx_http_spdy_state_protocol_error(sc);
default: /* TODO logging */
return ngx_http_spdy_state_skip(sc, pos, end);
}
}
if (ngx_spdy_data_frame_check(head)) {
sc->stream = ngx_http_spdy_get_stream_by_id(sc, head);
return ngx_http_spdy_state_data(sc, pos, end);
}
/* TODO version & type check */
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy unknown frame");
return ngx_http_spdy_state_protocol_error(sc);
}
static u_char *
ngx_http_spdy_state_syn_stream(ngx_http_spdy_connection_t *sc, u_char *pos,
u_char *end)
{
ngx_uint_t sid, prio;
ngx_http_spdy_stream_t *stream;
ngx_http_spdy_srv_conf_t *sscf;
if (end - pos < NGX_SPDY_SYN_STREAM_SIZE) {
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_syn_stream);
}
if (sc->length <= NGX_SPDY_SYN_STREAM_SIZE) {
/* TODO logging */
return ngx_http_spdy_state_protocol_error(sc);
}
sc->length -= NGX_SPDY_SYN_STREAM_SIZE;
sid = ngx_spdy_frame_parse_sid(pos);
prio = pos[8] >> 6;
pos += NGX_SPDY_SYN_STREAM_SIZE;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy SYN_STREAM frame sid:%ui prio:%ui", sid, prio);
sscf = ngx_http_get_module_srv_conf(sc->http_connection->conf_ctx,
ngx_http_spdy_module);
if (sc->processing >= sscf->concurrent_streams) {
ngx_log_error(NGX_LOG_INFO, sc->connection->log, 0,
"spdy concurrent streams excessed %ui", sc->processing);
if (ngx_http_spdy_send_rst_stream(sc, sid, NGX_SPDY_REFUSED_STREAM,
prio)
!= NGX_OK)
{
return ngx_http_spdy_state_internal_error(sc);
}
return ngx_http_spdy_state_headers_skip(sc, pos, end);
}
stream = ngx_http_spdy_create_stream(sc, sid, prio);
if (stream == NULL) {
return ngx_http_spdy_state_internal_error(sc);
}
stream->in_closed = (sc->flags & NGX_SPDY_FLAG_FIN) ? 1 : 0;
stream->request->request_length = NGX_SPDY_FRAME_HEADER_SIZE
+ NGX_SPDY_SYN_STREAM_SIZE
+ sc->length;
sc->stream = stream;
sc->last_sid = sid;
return ngx_http_spdy_state_headers(sc, pos, end);
}
static u_char *
ngx_http_spdy_state_headers(ngx_http_spdy_connection_t *sc, u_char *pos,
u_char *end)
{
int z;
size_t size;
ngx_buf_t *buf;
ngx_int_t rc;
ngx_uint_t complete;
ngx_http_request_t *r;
size = end - pos;
if (size == 0) {
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_headers);
}
if (size >= sc->length) {
size = sc->length;
complete = 1;
} else {
complete = 0;
}
r = sc->stream->request;
ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"spdy process HEADERS %uz of %uz", size, sc->length);
buf = r->header_in;
sc->zstream_in.next_in = pos;
sc->zstream_in.avail_in = size;
sc->zstream_in.next_out = buf->last;
/* one byte is reserved for null-termination of the last header value */
sc->zstream_in.avail_out = buf->end - buf->last - 1;
z = inflate(&sc->zstream_in, Z_NO_FLUSH);
if (z == Z_NEED_DICT) {
z = inflateSetDictionary(&sc->zstream_in, ngx_http_spdy_dict,
sizeof(ngx_http_spdy_dict));
if (z != Z_OK) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"spdy inflateSetDictionary() failed: %d", z);
ngx_http_spdy_close_stream(sc->stream, 0);
return ngx_http_spdy_state_protocol_error(sc);
}
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"spdy inflateSetDictionary(): %d", z);
z = sc->zstream_in.avail_in ? inflate(&sc->zstream_in, Z_NO_FLUSH)
: Z_OK;
}
if (z != Z_OK) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"spdy inflate() failed: %d", z);
ngx_http_spdy_close_stream(sc->stream, 0);
return ngx_http_spdy_state_protocol_error(sc);
}
ngx_log_debug5(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"spdy inflate out: ni:%p no:%p ai:%ud ao:%ud rc:%d",
sc->zstream_in.next_in, sc->zstream_in.next_out,
sc->zstream_in.avail_in, sc->zstream_in.avail_out,
z);
sc->length -= sc->zstream_in.next_in - pos;
pos = sc->zstream_in.next_in;
buf->last = sc->zstream_in.next_out;
if (r->headers_in.headers.part.elts == NULL) {
if (buf->last - buf->pos < NGX_SPDY_NV_NUM_SIZE) {
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_headers);
}
sc->headers = ngx_spdy_frame_parse_uint16(buf->pos);
buf->pos += NGX_SPDY_NV_NUM_SIZE;
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"spdy headers count: %ui", sc->headers);
if (ngx_list_init(&r->headers_in.headers, r->pool, sc->headers + 3,
sizeof(ngx_table_elt_t))
!= NGX_OK)
{
ngx_http_spdy_close_stream(sc->stream,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return ngx_http_spdy_state_headers_error(sc, pos, end);
}
if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
sizeof(ngx_table_elt_t *))
!= NGX_OK)
{
ngx_http_spdy_close_stream(sc->stream,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return ngx_http_spdy_state_headers_error(sc, pos, end);
}
}
while (sc->headers) {
rc = ngx_http_spdy_parse_header(r);
switch (rc) {
case NGX_DONE:
sc->headers--;
case NGX_OK:
break;
case NGX_AGAIN:
if (sc->zstream_in.avail_in) {
rc = ngx_http_spdy_alloc_large_header_buffer(r);
if (rc == NGX_DECLINED) {
/* TODO logging */
ngx_http_finalize_request(r,
NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
return ngx_http_spdy_state_headers_error(sc, pos, end);
}
if (rc != NGX_OK) {
ngx_http_spdy_close_stream(sc->stream,
NGX_HTTP_INTERNAL_SERVER_ERROR);
return ngx_http_spdy_state_headers_error(sc, pos, end);
}
/* null-terminate the last processed header name or value */
*buf->pos = '\0';
buf = r->header_in;
sc->zstream_in.next_out = buf->last;
/* one byte is reserved for null-termination */
sc->zstream_in.avail_out = buf->end - buf->last - 1;
z = inflate(&sc->zstream_in, Z_NO_FLUSH);
if (z != Z_OK) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"spdy inflate() failed: %d", z);
ngx_http_spdy_close_stream(sc->stream, 0);
return ngx_http_spdy_state_protocol_error(sc);
}
sc->length -= sc->zstream_in.next_in - pos;
pos = sc->zstream_in.next_in;
buf->last = sc->zstream_in.next_out;
continue;
}
if (complete) {
/* TODO: improve error message */
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"spdy again while last chunk");
ngx_http_spdy_close_stream(sc->stream, 0);
return ngx_http_spdy_state_protocol_error(sc);
}
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_headers);
case NGX_HTTP_PARSE_INVALID_REQUEST:
/* TODO: improve error message */
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent invalid header line");
ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
return ngx_http_spdy_state_headers_error(sc, pos, end);
default: /* NGX_HTTP_PARSE_INVALID_HEADER */
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent invalid HEADERS spdy frame");
ngx_http_spdy_close_stream(sc->stream, NGX_HTTP_BAD_REQUEST);
return ngx_http_spdy_state_protocol_error(sc);
}
/* a header line has been parsed successfully */
rc = ngx_http_spdy_handle_request_header(r);
if (rc != NGX_OK) {
if (rc == NGX_HTTP_PARSE_INVALID_HEADER) {
ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
"client sent invalid HEADERS spdy frame");
ngx_http_spdy_close_stream(sc->stream, NGX_HTTP_BAD_REQUEST);
return ngx_http_spdy_state_protocol_error(sc);
}
if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
}
return ngx_http_spdy_state_headers_error(sc, pos, end);
}
}
if (buf->pos != buf->last) {
/* TODO: improve error message */
ngx_log_debug3(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
"end %ui %p %p", complete, buf->pos, buf->last);
ngx_http_spdy_close_stream(sc->stream, NGX_HTTP_BAD_REQUEST);
return ngx_http_spdy_state_protocol_error(sc);
}
if (!complete) {
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_headers);
}
/* null-terminate the last header value */
*buf->pos = '\0';
ngx_http_spdy_run_request(r);
return ngx_http_spdy_state_complete(sc, pos, end);
}
static u_char *
ngx_http_spdy_state_headers_error(ngx_http_spdy_connection_t *sc, u_char *pos,
u_char *end)
{
if (sc->connection->error) {
return ngx_http_spdy_state_internal_error(sc);
}
return ngx_http_spdy_state_headers_skip(sc, pos, end);
}
static u_char *
ngx_http_spdy_state_headers_skip(ngx_http_spdy_connection_t *sc, u_char *pos,
u_char *end)
{
int n;
size_t size;
u_char buffer[NGX_SPDY_SKIP_HEADERS_BUFFER_SIZE];
if (sc->length == 0) {
return ngx_http_spdy_state_complete(sc, pos, end);
}
size = end - pos;
if (size == 0) {
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_headers_skip);
}
sc->zstream_in.next_in = pos;
sc->zstream_in.avail_in = (size < sc->length) ? size : sc->length;
while (sc->zstream_in.avail_in) {
sc->zstream_in.next_out = buffer;
sc->zstream_in.avail_out = NGX_SPDY_SKIP_HEADERS_BUFFER_SIZE;
n = inflate(&sc->zstream_in, Z_NO_FLUSH);
ngx_log_debug1(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy inflate(): %d", n);
if (n != Z_OK) {
/* TODO: logging */
return ngx_http_spdy_state_protocol_error(sc);
}
}
pos = sc->zstream_in.next_in;
if (size < sc->length) {
sc->length -= size;
return ngx_http_spdy_state_save(sc, pos, end,
ngx_http_spdy_state_headers_skip);
}
return ngx_http_spdy_state_complete(sc, pos, end);
}
static u_char *
ngx_http_spdy_state_data(ngx_http_spdy_connection_t *sc, u_char *pos,
u_char *end)
{
size_t size;
ssize_t n;
ngx_buf_t *buf;
ngx_int_t rc;
ngx_uint_t complete;
ngx_temp_file_t *tf;
ngx_http_request_t *r;
ngx_http_spdy_stream_t *stream;
ngx_http_request_body_t *rb;
ngx_http_core_loc_conf_t *clcf;
stream = sc->stream;
ngx_log_debug0(NGX_LOG_DEBUG_HTTP, sc->connection->log, 0,
"spdy DATA frame");
if (stream == NULL) {
return ngx_http_spdy_state_skip(sc, pos, end);
}
if (stream->in_closed) {
/* TODO log */
return ngx_http_spdy_state_protocol_error(sc);
}
if (stream->skip_data) {
if (sc->flags & NGX_SPDY_FLAG_FIN) {
stream->in_closed = 1;
}
/* TODO log and accounting */
return ngx_http_spdy_state_skip(sc, pos, end);
}
size = end - pos;
if (size >= sc->length) {
size = sc->length;
complete = 1;
} else {
sc->length -= size;
complete = 0;
}
r = stream->request;
if (r->request_body == NULL
&& ngx_http_spdy_init_request_body(r) != NGX_OK)
{
stream->skip_data = NGX_SPDY_DATA_INTERNAL_ERROR;
return ngx_http_spdy_state_skip(sc, pos, end);
}
rb = r->request_body;
tf = rb->temp_file;
buf = rb->buf;
if (size) {
rb->rest += size;
if (r->headers_in.content_length_n != -1
&& r->headers_in.content_length_n < rb->rest)
{
/* TODO logging */
stream->skip_data = NGX_SPDY_DATA_ERROR;
goto error;
} else {
clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
if (clcf->client_max_body_size
&& clcf->client_max_body_size < rb->rest)
{
ngx_log_error(NGX_LOG_ERR, r->connection->log, 0,
"client intended to send too large chunked "
"body: %O bytes",
rb->rest);
stream->skip_data = NGX_SPDY_DATA_ERROR;
goto error;
}
}
if (tf) {
buf->start = pos;
buf->pos = pos;
pos += size;
buf->