Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

push resources specified by 1xx + link: rel=preload sent from application server #916

Merged
merged 5 commits into from
May 31, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions include/h2o/http1client.h
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,8 @@ typedef h2o_http1client_body_cb (*h2o_http1client_head_cb)(h2o_http1client_t *cl
size_t num_headers);
typedef h2o_http1client_head_cb (*h2o_http1client_connect_cb)(h2o_http1client_t *client, const char *errstr, h2o_iovec_t **reqbufs,
size_t *reqbufcnt, int *method_is_head);
typedef int (*h2o_http1client_informational_cb)(h2o_http1client_t *client, int minor_version, int status, h2o_iovec_t msg,
struct phr_header *headers, size_t num_headers);

typedef struct st_h2o_http1client_ctx_t {
h2o_loop_t *loop;
Expand All @@ -60,6 +62,7 @@ struct st_h2o_http1client_t {
} ssl;
h2o_socket_t *sock;
void *data;
h2o_http1client_informational_cb informational_cb;
};

extern const char *const h2o_http1client_error_is_eos;
Expand Down
20 changes: 18 additions & 2 deletions lib/common/http1client.c
Original file line number Diff line number Diff line change
Expand Up @@ -239,11 +239,27 @@ static void on_head(h2o_socket_t *sock, const char *err)
return;
}

/* convert the header names to lowercase */
for (i = 0; i != num_headers; ++i)
h2o_strtolower((char *)headers[i].name, headers[i].name_len);

/* handle 1xx response (except 101, which is handled by on_head callback) */
if (100 <= http_status && http_status <= 199 && http_status != 101) {
if (client->super.informational_cb != NULL &&
client->super.informational_cb(&client->super, minor_version, http_status, h2o_iovec_init(msg, msg_len), headers,
num_headers) != 0) {
close_client(client);
return;
}
h2o_buffer_consume(&client->super.sock->input, rlen);
h2o_timeout_link(client->super.ctx->loop, client->super.ctx->io_timeout, &client->_timeout);
return;
}

/* parse the headers */
reader = on_body_until_close;
client->_can_keepalive = minor_version >= 1;
for (i = 0; i != num_headers; ++i) {
h2o_strtolower((char *)headers[i].name, headers[i].name_len);
if (h2o_memis(headers[i].name, headers[i].name_len, H2O_STRLIT("connection"))) {
if (h2o_contains_token(headers[i].value, headers[i].value_len, H2O_STRLIT("keep-alive"), ',')) {
client->_can_keepalive = 1;
Expand Down Expand Up @@ -273,7 +289,7 @@ static void on_head(h2o_socket_t *sock, const char *err)
}

/* RFC 2616 4.4 */
if (client->_method_is_head || ((100 <= http_status && http_status <= 199) || http_status == 204 || http_status == 304)) {
if (client->_method_is_head || http_status == 101 || http_status == 204 || http_status == 304) {
is_eos = 1;
} else {
is_eos = 0;
Expand Down
15 changes: 15 additions & 0 deletions lib/core/proxy.c
Original file line number Diff line number Diff line change
Expand Up @@ -411,6 +411,20 @@ static h2o_http1client_body_cb on_head(h2o_http1client_t *client, const char *er
return on_body;
}

static int on_1xx(h2o_http1client_t *client, int minor_version, int status, h2o_iovec_t msg, struct phr_header *headers,
size_t num_headers)
{
struct rp_generator_t *self = client->data;
size_t i;

for (i = 0; i != num_headers; ++i) {
if (h2o_memis(headers[i].name, headers[i].name_len, H2O_STRLIT("link")))
h2o_push_path_in_link_header(self->src_req, headers[i].value, headers[i].value_len);
}

return 0;
}

static h2o_http1client_head_cb on_connect(h2o_http1client_t *client, const char *errstr, h2o_iovec_t **reqbufs, size_t *reqbufcnt,
int *method_is_head)
{
Expand All @@ -426,6 +440,7 @@ static h2o_http1client_head_cb on_connect(h2o_http1client_t *client, const char
*reqbufs = self->up_req.bufs;
*reqbufcnt = self->up_req.bufs[1].base != NULL ? 2 : 1;
*method_is_head = self->up_req.is_head;
self->client->informational_cb = on_1xx;
return on_head;
}

Expand Down
10 changes: 10 additions & 0 deletions t/40server-push.t
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,16 @@ EOT
my $resp = `nghttp $opts -n --stat '$proto://127.0.0.1:$port/index.txt?resp:link=</index.txt.gz>\%3b\%20rel=preload'`;
like $resp, qr{\nid\s*responseEnd\s.*\s/index\.txt\?.*\s/index\.txt.gz\n}is;
};
subtest "push-1xx" => sub {
my $out = `nghttp $opts -n --stat '$proto://127.0.0.1:$port/1xx-push/'`;
# index.js arrives < 100ms, and /1xx-push/ arrives > 1sec
$out = (split /^.*?\nid *responseEnd .*?\n/s, $out, 2)[1];
chomp $out;
my @responses = split /\n/, $out;
is scalar(@responses), 2, "2 responses";
like $responses[0], qr{\+[0-9]{1,2}\.[0-9]*ms .* /index.js$}, "index.js arrives < 100ms";
like $responses[1], qr{\+1\.[0-9]*s .* /1xx-push/$}, "/1xx-push/ arrives >= 1sec";
};
subtest 'push-while-sleep' => sub {
my $resp = `nghttp $opts -n --stat '$proto://127.0.0.1:$port/mruby/sleep-and-respond?sleep=1'`;
like $resp, qr{\nid\s*responseEnd\s.*\s/index\.txt\.gz\n.*\s/mruby/sleep-and-respond}is;
Expand Down
13 changes: 13 additions & 0 deletions t/assets/upstream.psgi
Original file line number Diff line number Diff line change
Expand Up @@ -162,4 +162,17 @@ builder {
close $fh;
exit 0;
};
mount "/1xx-push" => sub {
my $env = shift;
my $fh = $env->{"psgix.io"};
print $fh join(
"\r\n",
"HTTP/1.1 100 Continue",
"link: </index.js>; rel=preload",
"",
"",
);
sleep 1.1;
[200, ["content-type" => "text/plain; charset=utf-8", "content-length" => 11], ["hello world"]];
};
};