|
Packit |
90a5c9 |
/* Licensed to the Apache Software Foundation (ASF) under one or more
|
|
Packit |
90a5c9 |
* contributor license agreements. See the NOTICE file distributed with
|
|
Packit |
90a5c9 |
* this work for additional information regarding copyright ownership.
|
|
Packit |
90a5c9 |
* The ASF licenses this file to You under the Apache License, Version 2.0
|
|
Packit |
90a5c9 |
* (the "License"); you may not use this file except in compliance with
|
|
Packit |
90a5c9 |
* the License. You may obtain a copy of the License at
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* http://www.apache.org/licenses/LICENSE-2.0
|
|
Packit |
90a5c9 |
*
|
|
Packit |
90a5c9 |
* Unless required by applicable law or agreed to in writing, software
|
|
Packit |
90a5c9 |
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
Packit |
90a5c9 |
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
Packit |
90a5c9 |
* See the License for the specific language governing permissions and
|
|
Packit |
90a5c9 |
* limitations under the License.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include <stddef.h>
|
|
Packit |
90a5c9 |
#include <apr_strings.h>
|
|
Packit |
90a5c9 |
#include <nghttp2/nghttp2.h>
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include <mpm_common.h>
|
|
Packit |
90a5c9 |
#include <httpd.h>
|
|
Packit |
90a5c9 |
#include <mod_proxy.h>
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#include "mod_http2.h"
|
|
Packit |
90a5c9 |
#include "h2.h"
|
|
Packit |
90a5c9 |
#include "h2_proxy_util.h"
|
|
Packit |
90a5c9 |
#include "h2_proxy_session.h"
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
APLOG_USE_MODULE(proxy_http2);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct h2_proxy_stream {
|
|
Packit |
90a5c9 |
int id;
|
|
Packit |
90a5c9 |
apr_pool_t *pool;
|
|
Packit |
90a5c9 |
h2_proxy_session *session;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
const char *url;
|
|
Packit |
90a5c9 |
request_rec *r;
|
|
Packit |
90a5c9 |
h2_proxy_request *req;
|
|
Packit |
90a5c9 |
const char *real_server_uri;
|
|
Packit |
90a5c9 |
const char *p_server_uri;
|
|
Packit |
90a5c9 |
int standalone;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
h2_proxy_stream_state_t state;
|
|
Packit |
90a5c9 |
unsigned int suspended : 1;
|
|
Packit |
90a5c9 |
unsigned int waiting_on_100 : 1;
|
|
Packit |
90a5c9 |
unsigned int waiting_on_ping : 1;
|
|
Packit |
90a5c9 |
uint32_t error_code;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_bucket_brigade *input;
|
|
Packit |
90a5c9 |
apr_off_t data_sent;
|
|
Packit |
90a5c9 |
apr_bucket_brigade *output;
|
|
Packit |
90a5c9 |
apr_off_t data_received;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_table_t *saves;
|
|
Packit |
90a5c9 |
} h2_proxy_stream;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void dispatch_event(h2_proxy_session *session, h2_proxys_event_t ev,
|
|
Packit |
90a5c9 |
int arg, const char *msg);
|
|
Packit |
90a5c9 |
static void ping_arrived(h2_proxy_session *session);
|
|
Packit |
90a5c9 |
static apr_status_t check_suspended(h2_proxy_session *session);
|
|
Packit |
90a5c9 |
static void stream_resume(h2_proxy_stream *stream);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t proxy_session_pre_close(void *theconn)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
proxy_conn_rec *p_conn = (proxy_conn_rec *)theconn;
|
|
Packit |
90a5c9 |
h2_proxy_session *session = p_conn->data;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (session && session->ngh2) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, session->c,
|
|
Packit |
90a5c9 |
"proxy_session(%s): pool cleanup, state=%d, streams=%d",
|
|
Packit |
90a5c9 |
session->id, session->state,
|
|
Packit |
90a5c9 |
(int)h2_proxy_ihash_count(session->streams));
|
|
Packit |
90a5c9 |
session->aborted = 1;
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_PRE_CLOSE, 0, NULL);
|
|
Packit |
90a5c9 |
nghttp2_session_del(session->ngh2);
|
|
Packit |
90a5c9 |
session->ngh2 = NULL;
|
|
Packit |
90a5c9 |
p_conn->data = NULL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int proxy_pass_brigade(apr_bucket_alloc_t *bucket_alloc,
|
|
Packit |
90a5c9 |
proxy_conn_rec *p_conn,
|
|
Packit |
90a5c9 |
conn_rec *origin, apr_bucket_brigade *bb,
|
|
Packit |
90a5c9 |
int flush)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
apr_off_t transferred;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (flush) {
|
|
Packit |
90a5c9 |
apr_bucket *e = apr_bucket_flush_create(bucket_alloc);
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(bb, e);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_brigade_length(bb, 0, &transferred);
|
|
Packit |
90a5c9 |
if (transferred != -1)
|
|
Packit |
90a5c9 |
p_conn->worker->s->transferred += transferred;
|
|
Packit |
90a5c9 |
status = ap_pass_brigade(origin->output_filters, bb);
|
|
Packit |
90a5c9 |
/* Cleanup the brigade now to avoid buckets lifetime
|
|
Packit |
90a5c9 |
* issues in case of error returned below. */
|
|
Packit |
90a5c9 |
apr_brigade_cleanup(bb);
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, status, origin, APLOGNO(03357)
|
|
Packit |
90a5c9 |
"pass output failed to %pI (%s)",
|
|
Packit |
90a5c9 |
p_conn->addr, p_conn->hostname);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static ssize_t raw_send(nghttp2_session *ngh2, const uint8_t *data,
|
|
Packit |
90a5c9 |
size_t length, int flags, void *user_data)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = user_data;
|
|
Packit |
90a5c9 |
apr_bucket *b;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
int flush = 1;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (data) {
|
|
Packit |
90a5c9 |
b = apr_bucket_transient_create((const char*)data, length,
|
|
Packit |
90a5c9 |
session->c->bucket_alloc);
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(session->output, b);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = proxy_pass_brigade(session->c->bucket_alloc,
|
|
Packit |
90a5c9 |
session->p_conn, session->c,
|
|
Packit |
90a5c9 |
session->output, flush);
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, status, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_sesssion(%s): raw_send %d bytes, flush=%d",
|
|
Packit |
90a5c9 |
session->id, (int)length, flush);
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return length;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int on_frame_recv(nghttp2_session *ngh2, const nghttp2_frame *frame,
|
|
Packit |
90a5c9 |
void *user_data)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = user_data;
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
request_rec *r;
|
|
Packit |
90a5c9 |
int n;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (APLOGcdebug(session->c)) {
|
|
Packit |
90a5c9 |
char buffer[256];
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
h2_proxy_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03341)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): recv FRAME[%s]",
|
|
Packit |
90a5c9 |
session->id, buffer);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
session->last_frame_received = apr_time_now();
|
|
Packit |
90a5c9 |
switch (frame->hd.type) {
|
|
Packit |
90a5c9 |
case NGHTTP2_HEADERS:
|
|
Packit |
90a5c9 |
stream = nghttp2_session_get_stream_user_data(ngh2, frame->hd.stream_id);
|
|
Packit |
90a5c9 |
if (!stream) {
|
|
Packit |
90a5c9 |
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
r = stream->r;
|
|
Packit |
90a5c9 |
if (r->status >= 100 && r->status < 200) {
|
|
Packit |
90a5c9 |
/* By default, we will forward all interim responses when
|
|
Packit |
90a5c9 |
* we are sitting on a HTTP/2 connection to the client */
|
|
Packit |
90a5c9 |
int forward = session->h2_front;
|
|
Packit |
90a5c9 |
switch(r->status) {
|
|
Packit |
90a5c9 |
case 100:
|
|
Packit |
90a5c9 |
if (stream->waiting_on_100) {
|
|
Packit |
90a5c9 |
stream->waiting_on_100 = 0;
|
|
Packit |
90a5c9 |
r->status_line = ap_get_status_line(r->status);
|
|
Packit |
90a5c9 |
forward = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case 103:
|
|
Packit |
90a5c9 |
/* workaround until we get this into http protocol base
|
|
Packit |
90a5c9 |
* parts. without this, unknown codes are converted to
|
|
Packit |
90a5c9 |
* 500... */
|
|
Packit |
90a5c9 |
r->status_line = "103 Early Hints";
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
r->status_line = ap_get_status_line(r->status);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03487)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): got interim HEADERS, "
|
|
Packit |
90a5c9 |
"status=%d, will forward=%d",
|
|
Packit |
90a5c9 |
session->id, r->status, forward);
|
|
Packit |
90a5c9 |
if (forward) {
|
|
Packit |
90a5c9 |
ap_send_interim_response(r, 1);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
stream_resume(stream);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case NGHTTP2_PING:
|
|
Packit |
90a5c9 |
if (session->check_ping) {
|
|
Packit |
90a5c9 |
session->check_ping = 0;
|
|
Packit |
90a5c9 |
ping_arrived(session);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case NGHTTP2_PUSH_PROMISE:
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case NGHTTP2_SETTINGS:
|
|
Packit |
90a5c9 |
if (frame->settings.niv > 0) {
|
|
Packit |
90a5c9 |
n = nghttp2_session_get_remote_settings(ngh2, NGHTTP2_SETTINGS_MAX_CONCURRENT_STREAMS);
|
|
Packit |
90a5c9 |
if (n > 0) {
|
|
Packit |
90a5c9 |
session->remote_max_concurrent = n;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case NGHTTP2_GOAWAY:
|
|
Packit |
90a5c9 |
/* we expect the remote server to tell us the highest stream id
|
|
Packit |
90a5c9 |
* that it has started processing. */
|
|
Packit |
90a5c9 |
session->last_stream_id = frame->goaway.last_stream_id;
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_REMOTE_GOAWAY, 0, NULL);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int before_frame_send(nghttp2_session *ngh2,
|
|
Packit |
90a5c9 |
const nghttp2_frame *frame, void *user_data)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = user_data;
|
|
Packit |
90a5c9 |
if (APLOGcdebug(session->c)) {
|
|
Packit |
90a5c9 |
char buffer[256];
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
h2_proxy_util_frame_print(frame, buffer, sizeof(buffer)/sizeof(buffer[0]));
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03343)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): sent FRAME[%s]",
|
|
Packit |
90a5c9 |
session->id, buffer);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int add_header(void *table, const char *n, const char *v)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_table_add(table, n, v);
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void process_proxy_header(h2_proxy_stream *stream, const char *n, const char *v)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
static const struct {
|
|
Packit |
90a5c9 |
const char *name;
|
|
Packit |
90a5c9 |
ap_proxy_header_reverse_map_fn func;
|
|
Packit |
90a5c9 |
} transform_hdrs[] = {
|
|
Packit |
90a5c9 |
{ "Location", ap_proxy_location_reverse_map },
|
|
Packit |
90a5c9 |
{ "Content-Location", ap_proxy_location_reverse_map },
|
|
Packit |
90a5c9 |
{ "URI", ap_proxy_location_reverse_map },
|
|
Packit |
90a5c9 |
{ "Destination", ap_proxy_location_reverse_map },
|
|
Packit |
90a5c9 |
{ "Set-Cookie", ap_proxy_cookie_reverse_map },
|
|
Packit |
90a5c9 |
{ NULL, NULL }
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
request_rec *r = stream->r;
|
|
Packit |
90a5c9 |
proxy_dir_conf *dconf;
|
|
Packit |
90a5c9 |
int i;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
|
|
Packit |
90a5c9 |
if (!dconf->preserve_host) {
|
|
Packit |
90a5c9 |
for (i = 0; transform_hdrs[i].name; ++i) {
|
|
Packit |
90a5c9 |
if (!ap_cstr_casecmp(transform_hdrs[i].name, n)) {
|
|
Packit |
90a5c9 |
apr_table_add(r->headers_out, n,
|
|
Packit |
90a5c9 |
(*transform_hdrs[i].func)(r, dconf, v));
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (!ap_cstr_casecmp("Link", n)) {
|
|
Packit |
90a5c9 |
dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
|
|
Packit |
90a5c9 |
apr_table_add(r->headers_out, n,
|
|
Packit |
90a5c9 |
h2_proxy_link_reverse_map(r, dconf,
|
|
Packit |
90a5c9 |
stream->real_server_uri, stream->p_server_uri, v));
|
|
Packit |
90a5c9 |
return;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_table_add(r->headers_out, n, v);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t h2_proxy_stream_add_header_out(h2_proxy_stream *stream,
|
|
Packit |
90a5c9 |
const char *n, apr_size_t nlen,
|
|
Packit |
90a5c9 |
const char *v, apr_size_t vlen)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (n[0] == ':') {
|
|
Packit |
90a5c9 |
if (!stream->data_received && !strncmp(":status", n, nlen)) {
|
|
Packit |
90a5c9 |
char *s = apr_pstrndup(stream->r->pool, v, vlen);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_table_setn(stream->r->notes, "proxy-status", s);
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, stream->session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%s-%d): got status %s",
|
|
Packit |
90a5c9 |
stream->session->id, stream->id, s);
|
|
Packit |
90a5c9 |
stream->r->status = (int)apr_atoi64(s);
|
|
Packit |
90a5c9 |
if (stream->r->status <= 0) {
|
|
Packit |
90a5c9 |
stream->r->status = 500;
|
|
Packit |
90a5c9 |
return APR_EGENERAL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!h2_proxy_res_ignore_header(n, nlen)) {
|
|
Packit |
90a5c9 |
char *hname, *hvalue;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
hname = apr_pstrndup(stream->pool, n, nlen);
|
|
Packit |
90a5c9 |
h2_proxy_util_camel_case_header(hname, nlen);
|
|
Packit |
90a5c9 |
hvalue = apr_pstrndup(stream->pool, v, vlen);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, stream->session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%s-%d): got header %s: %s",
|
|
Packit |
90a5c9 |
stream->session->id, stream->id, hname, hvalue);
|
|
Packit |
90a5c9 |
process_proxy_header(stream, hname, hvalue);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int log_header(void *ctx, const char *key, const char *value)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream = ctx;
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, stream->r,
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%s-%d), header_out %s: %s",
|
|
Packit |
90a5c9 |
stream->session->id, stream->id, key, value);
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void h2_proxy_stream_end_headers_out(h2_proxy_stream *stream)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = stream->session;
|
|
Packit |
90a5c9 |
request_rec *r = stream->r;
|
|
Packit |
90a5c9 |
apr_pool_t *p = r->pool;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Now, add in the cookies from the response to the ones already saved */
|
|
Packit |
90a5c9 |
apr_table_do(add_header, stream->saves, r->headers_out, "Set-Cookie", NULL);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* and now load 'em all in */
|
|
Packit |
90a5c9 |
if (!apr_is_empty_table(stream->saves)) {
|
|
Packit |
90a5c9 |
apr_table_unset(r->headers_out, "Set-Cookie");
|
|
Packit |
90a5c9 |
r->headers_out = apr_table_overlay(p, r->headers_out, stream->saves);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* handle Via header in response */
|
|
Packit |
90a5c9 |
if (session->conf->viaopt != via_off
|
|
Packit |
90a5c9 |
&& session->conf->viaopt != via_block) {
|
|
Packit |
90a5c9 |
const char *server_name = ap_get_server_name(stream->r);
|
|
Packit |
90a5c9 |
apr_port_t port = ap_get_server_port(stream->r);
|
|
Packit |
90a5c9 |
char portstr[32];
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If USE_CANONICAL_NAME_OFF was configured for the proxy virtual host,
|
|
Packit |
90a5c9 |
* then the server name returned by ap_get_server_name() is the
|
|
Packit |
90a5c9 |
* origin server name (which doesn't make sense with Via: headers)
|
|
Packit |
90a5c9 |
* so we use the proxy vhost's name instead.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if (server_name == stream->r->hostname) {
|
|
Packit |
90a5c9 |
server_name = stream->r->server->server_hostname;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (ap_is_default_port(port, stream->r)) {
|
|
Packit |
90a5c9 |
portstr[0] = '\0';
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
apr_snprintf(portstr, sizeof(portstr), ":%d", port);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* create a "Via:" response header entry and merge it */
|
|
Packit |
90a5c9 |
apr_table_add(r->headers_out, "Via",
|
|
Packit |
90a5c9 |
(session->conf->viaopt == via_full)
|
|
Packit |
90a5c9 |
? apr_psprintf(p, "%d.%d %s%s (%s)",
|
|
Packit |
90a5c9 |
HTTP_VERSION_MAJOR(r->proto_num),
|
|
Packit |
90a5c9 |
HTTP_VERSION_MINOR(r->proto_num),
|
|
Packit |
90a5c9 |
server_name, portstr,
|
|
Packit |
90a5c9 |
AP_SERVER_BASEVERSION)
|
|
Packit |
90a5c9 |
: apr_psprintf(p, "%d.%d %s%s",
|
|
Packit |
90a5c9 |
HTTP_VERSION_MAJOR(r->proto_num),
|
|
Packit |
90a5c9 |
HTTP_VERSION_MINOR(r->proto_num),
|
|
Packit |
90a5c9 |
server_name, portstr)
|
|
Packit |
90a5c9 |
);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (APLOGrtrace2(stream->r)) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, stream->r,
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%s-%d), header_out after merging",
|
|
Packit |
90a5c9 |
stream->session->id, stream->id);
|
|
Packit |
90a5c9 |
apr_table_do(log_header, stream, stream->r->headers_out, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int stream_response_data(nghttp2_session *ngh2, uint8_t flags,
|
|
Packit |
90a5c9 |
int32_t stream_id, const uint8_t *data,
|
|
Packit |
90a5c9 |
size_t len, void *user_data)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = user_data;
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
apr_bucket *b;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
stream = nghttp2_session_get_stream_user_data(ngh2, stream_id);
|
|
Packit |
90a5c9 |
if (!stream) {
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf, APLOGNO(03358)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): recv data chunk for "
|
|
Packit |
90a5c9 |
"unknown stream %d, ignored",
|
|
Packit |
90a5c9 |
session->id, stream_id);
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!stream->data_received) {
|
|
Packit |
90a5c9 |
/* last chance to manipulate response headers.
|
|
Packit |
90a5c9 |
* after this, only trailers */
|
|
Packit |
90a5c9 |
h2_proxy_stream_end_headers_out(stream);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
stream->data_received += len;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
b = apr_bucket_transient_create((const char*)data, len,
|
|
Packit |
90a5c9 |
stream->r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(stream->output, b);
|
|
Packit |
90a5c9 |
/* always flush after a DATA frame, as we have no other indication
|
|
Packit |
90a5c9 |
* of buffer use */
|
|
Packit |
90a5c9 |
b = apr_bucket_flush_create(stream->r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(stream->output, b);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = ap_pass_brigade(stream->r->output_filters, stream->output);
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, stream->r, APLOGNO(03359)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): stream=%d, response DATA %ld, %ld"
|
|
Packit |
90a5c9 |
" total", session->id, stream_id, (long)len,
|
|
Packit |
90a5c9 |
(long)stream->data_received);
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, status, session->c, APLOGNO(03344)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): passing output on stream %d",
|
|
Packit |
90a5c9 |
session->id, stream->id);
|
|
Packit |
90a5c9 |
nghttp2_submit_rst_stream(ngh2, NGHTTP2_FLAG_NONE,
|
|
Packit |
90a5c9 |
stream_id, NGHTTP2_STREAM_CLOSED);
|
|
Packit |
90a5c9 |
return NGHTTP2_ERR_STREAM_CLOSING;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (stream->standalone) {
|
|
Packit |
90a5c9 |
nghttp2_session_consume(ngh2, stream_id, len);
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE2, 0, stream->r,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): stream %d, win_update %d bytes",
|
|
Packit |
90a5c9 |
session->id, stream_id, (int)len);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int on_stream_close(nghttp2_session *ngh2, int32_t stream_id,
|
|
Packit |
90a5c9 |
uint32_t error_code, void *user_data)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = user_data;
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
if (!session->aborted) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03360)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): stream=%d, closed, err=%d",
|
|
Packit |
90a5c9 |
session->id, stream_id, error_code);
|
|
Packit |
90a5c9 |
stream = h2_proxy_ihash_get(session->streams, stream_id);
|
|
Packit |
90a5c9 |
if (stream) {
|
|
Packit |
90a5c9 |
stream->error_code = error_code;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_STREAM_DONE, stream_id, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int on_header(nghttp2_session *ngh2, const nghttp2_frame *frame,
|
|
Packit |
90a5c9 |
const uint8_t *namearg, size_t nlen,
|
|
Packit |
90a5c9 |
const uint8_t *valuearg, size_t vlen, uint8_t flags,
|
|
Packit |
90a5c9 |
void *user_data)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = user_data;
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
const char *n = (const char*)namearg;
|
|
Packit |
90a5c9 |
const char *v = (const char*)valuearg;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
(void)session;
|
|
Packit |
90a5c9 |
if (frame->hd.type == NGHTTP2_HEADERS && nlen) {
|
|
Packit |
90a5c9 |
stream = nghttp2_session_get_stream_user_data(ngh2, frame->hd.stream_id);
|
|
Packit |
90a5c9 |
if (stream) {
|
|
Packit |
90a5c9 |
if (h2_proxy_stream_add_header_out(stream, n, nlen, v, vlen)) {
|
|
Packit |
90a5c9 |
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (frame->hd.type == NGHTTP2_PUSH_PROMISE) {
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static ssize_t stream_request_data(nghttp2_session *ngh2, int32_t stream_id,
|
|
Packit |
90a5c9 |
uint8_t *buf, size_t length,
|
|
Packit |
90a5c9 |
uint32_t *data_flags,
|
|
Packit |
90a5c9 |
nghttp2_data_source *source, void *user_data)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
apr_status_t status = APR_SUCCESS;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*data_flags = 0;
|
|
Packit |
90a5c9 |
stream = nghttp2_session_get_stream_user_data(ngh2, stream_id);
|
|
Packit |
90a5c9 |
if (!stream) {
|
|
Packit |
90a5c9 |
ap_log_error(APLOG_MARK, APLOG_DEBUG, 0, ap_server_conf, APLOGNO(03361)
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%s): data_read, stream %d not found",
|
|
Packit |
90a5c9 |
stream->session->id, stream_id);
|
|
Packit |
90a5c9 |
return NGHTTP2_ERR_CALLBACK_FAILURE;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (stream->session->check_ping) {
|
|
Packit |
90a5c9 |
/* suspend until we hear from the other side */
|
|
Packit |
90a5c9 |
stream->waiting_on_ping = 1;
|
|
Packit |
90a5c9 |
status = APR_EAGAIN;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (stream->r->expecting_100) {
|
|
Packit |
90a5c9 |
/* suspend until the answer comes */
|
|
Packit |
90a5c9 |
stream->waiting_on_100 = 1;
|
|
Packit |
90a5c9 |
status = APR_EAGAIN;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (APR_BRIGADE_EMPTY(stream->input)) {
|
|
Packit |
90a5c9 |
status = ap_get_brigade(stream->r->input_filters, stream->input,
|
|
Packit |
90a5c9 |
AP_MODE_READBYTES, APR_NONBLOCK_READ,
|
|
Packit |
90a5c9 |
H2MAX(APR_BUCKET_BUFF_SIZE, length));
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE2, status, stream->r,
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%s-%d): request body read",
|
|
Packit |
90a5c9 |
stream->session->id, stream->id);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ssize_t readlen = 0;
|
|
Packit |
90a5c9 |
while (status == APR_SUCCESS
|
|
Packit |
90a5c9 |
&& (readlen < length)
|
|
Packit |
90a5c9 |
&& !APR_BRIGADE_EMPTY(stream->input)) {
|
|
Packit |
90a5c9 |
apr_bucket* b = APR_BRIGADE_FIRST(stream->input);
|
|
Packit |
90a5c9 |
if (APR_BUCKET_IS_METADATA(b)) {
|
|
Packit |
90a5c9 |
if (APR_BUCKET_IS_EOS(b)) {
|
|
Packit |
90a5c9 |
*data_flags |= NGHTTP2_DATA_FLAG_EOF;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* we do nothing more regarding any meta here */
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
const char *bdata = NULL;
|
|
Packit |
90a5c9 |
apr_size_t blen = 0;
|
|
Packit |
90a5c9 |
status = apr_bucket_read(b, &bdata, &blen, APR_BLOCK_READ);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS && blen > 0) {
|
|
Packit |
90a5c9 |
ssize_t copylen = H2MIN(length - readlen, blen);
|
|
Packit |
90a5c9 |
memcpy(buf, bdata, copylen);
|
|
Packit |
90a5c9 |
buf += copylen;
|
|
Packit |
90a5c9 |
readlen += copylen;
|
|
Packit |
90a5c9 |
if (copylen < blen) {
|
|
Packit |
90a5c9 |
/* We have data left in the bucket. Split it. */
|
|
Packit |
90a5c9 |
status = apr_bucket_split(b, copylen);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_bucket_delete(b);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
stream->data_sent += readlen;
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, status, stream->r, APLOGNO(03468)
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%d): request DATA %ld, %ld"
|
|
Packit |
90a5c9 |
" total, flags=%d",
|
|
Packit |
90a5c9 |
stream->id, (long)readlen, (long)stream->data_sent,
|
|
Packit |
90a5c9 |
(int)*data_flags);
|
|
Packit |
90a5c9 |
return readlen;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (APR_STATUS_IS_EAGAIN(status)) {
|
|
Packit |
90a5c9 |
/* suspended stream, needs to be re-awakened */
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_TRACE2, status, stream->r,
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%s-%d): suspending",
|
|
Packit |
90a5c9 |
stream->session->id, stream_id);
|
|
Packit |
90a5c9 |
stream->suspended = 1;
|
|
Packit |
90a5c9 |
h2_proxy_iq_add(stream->session->suspended, stream->id, NULL, NULL);
|
|
Packit |
90a5c9 |
return NGHTTP2_ERR_DEFERRED;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
nghttp2_submit_rst_stream(ngh2, NGHTTP2_FLAG_NONE,
|
|
Packit |
90a5c9 |
stream_id, NGHTTP2_STREAM_CLOSED);
|
|
Packit |
90a5c9 |
return NGHTTP2_ERR_STREAM_CLOSING;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
#ifdef H2_NG2_INVALID_HEADER_CB
|
|
Packit |
90a5c9 |
static int on_invalid_header_cb(nghttp2_session *ngh2,
|
|
Packit |
90a5c9 |
const nghttp2_frame *frame,
|
|
Packit |
90a5c9 |
const uint8_t *name, size_t namelen,
|
|
Packit |
90a5c9 |
const uint8_t *value, size_t valuelen,
|
|
Packit |
90a5c9 |
uint8_t flags, void *user_data)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = user_data;
|
|
Packit |
90a5c9 |
if (APLOGcdebug(session->c)) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03469)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s-%d): denying stream with invalid header "
|
|
Packit |
90a5c9 |
"'%s: %s'", session->id, (int)frame->hd.stream_id,
|
|
Packit |
90a5c9 |
apr_pstrndup(session->pool, (const char *)name, namelen),
|
|
Packit |
90a5c9 |
apr_pstrndup(session->pool, (const char *)value, valuelen));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return nghttp2_submit_rst_stream(session->ngh2, NGHTTP2_FLAG_NONE,
|
|
Packit |
90a5c9 |
frame->hd.stream_id,
|
|
Packit |
90a5c9 |
NGHTTP2_PROTOCOL_ERROR);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
h2_proxy_session *h2_proxy_session_setup(const char *id, proxy_conn_rec *p_conn,
|
|
Packit |
90a5c9 |
proxy_server_conf *conf,
|
|
Packit |
90a5c9 |
int h2_front,
|
|
Packit |
90a5c9 |
unsigned char window_bits_connection,
|
|
Packit |
90a5c9 |
unsigned char window_bits_stream,
|
|
Packit |
90a5c9 |
h2_proxy_request_done *done)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (!p_conn->data) {
|
|
Packit |
90a5c9 |
apr_pool_t *pool = p_conn->scpool;
|
|
Packit |
90a5c9 |
h2_proxy_session *session;
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks *cbs;
|
|
Packit |
90a5c9 |
nghttp2_option *option;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
session = apr_pcalloc(pool, sizeof(*session));
|
|
Packit |
90a5c9 |
apr_pool_pre_cleanup_register(pool, p_conn, proxy_session_pre_close);
|
|
Packit |
90a5c9 |
p_conn->data = session;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
session->id = apr_pstrdup(p_conn->scpool, id);
|
|
Packit |
90a5c9 |
session->c = p_conn->connection;
|
|
Packit |
90a5c9 |
session->p_conn = p_conn;
|
|
Packit |
90a5c9 |
session->conf = conf;
|
|
Packit |
90a5c9 |
session->pool = p_conn->scpool;
|
|
Packit |
90a5c9 |
session->state = H2_PROXYS_ST_INIT;
|
|
Packit |
90a5c9 |
session->h2_front = h2_front;
|
|
Packit |
90a5c9 |
session->window_bits_stream = window_bits_stream;
|
|
Packit |
90a5c9 |
session->window_bits_connection = window_bits_connection;
|
|
Packit |
90a5c9 |
session->streams = h2_proxy_ihash_create(pool, offsetof(h2_proxy_stream, id));
|
|
Packit |
90a5c9 |
session->suspended = h2_proxy_iq_create(pool, 5);
|
|
Packit |
90a5c9 |
session->done = done;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
session->input = apr_brigade_create(session->pool, session->c->bucket_alloc);
|
|
Packit |
90a5c9 |
session->output = apr_brigade_create(session->pool, session->c->bucket_alloc);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_new(&cbs);
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_set_on_frame_recv_callback(cbs, on_frame_recv);
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_set_on_data_chunk_recv_callback(cbs, stream_response_data);
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_set_on_stream_close_callback(cbs, on_stream_close);
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_set_on_header_callback(cbs, on_header);
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_set_before_frame_send_callback(cbs, before_frame_send);
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_set_send_callback(cbs, raw_send);
|
|
Packit |
90a5c9 |
#ifdef H2_NG2_INVALID_HEADER_CB
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_set_on_invalid_header_callback(cbs, on_invalid_header_cb);
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
nghttp2_option_new(&option);
|
|
Packit |
90a5c9 |
nghttp2_option_set_peer_max_concurrent_streams(option, 100);
|
|
Packit |
90a5c9 |
nghttp2_option_set_no_auto_window_update(option, 1);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
nghttp2_session_client_new2(&session->ngh2, cbs, session, option);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
nghttp2_option_del(option);
|
|
Packit |
90a5c9 |
nghttp2_session_callbacks_del(cbs);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03362)
|
|
Packit |
90a5c9 |
"setup session for %s", p_conn->hostname);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
h2_proxy_session *session = p_conn->data;
|
|
Packit |
90a5c9 |
apr_interval_time_t age = apr_time_now() - session->last_frame_received;
|
|
Packit |
90a5c9 |
if (age > apr_time_from_sec(1)) {
|
|
Packit |
90a5c9 |
session->check_ping = 1;
|
|
Packit |
90a5c9 |
nghttp2_submit_ping(session->ngh2, 0, (const uint8_t *)"nevergonnagiveyouup");
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return p_conn->data;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t session_start(h2_proxy_session *session)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
nghttp2_settings_entry settings[2];
|
|
Packit |
90a5c9 |
int rv, add_conn_window;
|
|
Packit |
90a5c9 |
apr_socket_t *s;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
s = ap_get_conn_socket(session->c);
|
|
Packit |
90a5c9 |
#if (!defined(WIN32) && !defined(NETWARE)) || defined(DOXYGEN)
|
|
Packit |
90a5c9 |
if (s) {
|
|
Packit |
90a5c9 |
ap_sock_disable_nagle(s);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
#endif
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
settings[0].settings_id = NGHTTP2_SETTINGS_ENABLE_PUSH;
|
|
Packit |
90a5c9 |
settings[0].value = 0;
|
|
Packit |
90a5c9 |
settings[1].settings_id = NGHTTP2_SETTINGS_INITIAL_WINDOW_SIZE;
|
|
Packit |
90a5c9 |
settings[1].value = (1 << session->window_bits_stream) - 1;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = nghttp2_submit_settings(session->ngh2, NGHTTP2_FLAG_NONE, settings,
|
|
Packit |
90a5c9 |
H2_ALEN(settings));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If the connection window is larger than our default, trigger a WINDOW_UPDATE */
|
|
Packit |
90a5c9 |
add_conn_window = ((1 << session->window_bits_connection) - 1 -
|
|
Packit |
90a5c9 |
NGHTTP2_INITIAL_CONNECTION_WINDOW_SIZE);
|
|
Packit |
90a5c9 |
if (!rv && add_conn_window != 0) {
|
|
Packit |
90a5c9 |
rv = nghttp2_submit_window_update(session->ngh2, NGHTTP2_FLAG_NONE, 0, add_conn_window);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return rv? APR_EGENERAL : APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t open_stream(h2_proxy_session *session, const char *url,
|
|
Packit |
90a5c9 |
request_rec *r, int standalone,
|
|
Packit |
90a5c9 |
h2_proxy_stream **pstream)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
apr_uri_t puri;
|
|
Packit |
90a5c9 |
const char *authority, *scheme, *path;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
proxy_dir_conf *dconf;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
stream = apr_pcalloc(r->pool, sizeof(*stream));
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
stream->pool = r->pool;
|
|
Packit |
90a5c9 |
stream->url = url;
|
|
Packit |
90a5c9 |
stream->r = r;
|
|
Packit |
90a5c9 |
stream->standalone = standalone;
|
|
Packit |
90a5c9 |
stream->session = session;
|
|
Packit |
90a5c9 |
stream->state = H2_STREAM_ST_IDLE;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
stream->input = apr_brigade_create(stream->pool, session->c->bucket_alloc);
|
|
Packit |
90a5c9 |
stream->output = apr_brigade_create(stream->pool, session->c->bucket_alloc);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
stream->req = h2_proxy_req_create(1, stream->pool, 0);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = apr_uri_parse(stream->pool, url, &puri);
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS)
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
scheme = (strcmp(puri.scheme, "h2")? "http" : "https");
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
dconf = ap_get_module_config(r->per_dir_config, &proxy_module);
|
|
Packit |
90a5c9 |
if (dconf->preserve_host) {
|
|
Packit |
90a5c9 |
authority = r->hostname;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
authority = puri.hostname;
|
|
Packit |
90a5c9 |
if (!ap_strchr_c(authority, ':') && puri.port
|
|
Packit |
90a5c9 |
&& apr_uri_port_of_scheme(scheme) != puri.port) {
|
|
Packit |
90a5c9 |
/* port info missing and port is not default for scheme: append */
|
|
Packit |
90a5c9 |
authority = apr_psprintf(stream->pool, "%s:%d", authority, puri.port);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* we need this for mapping relative uris in headers ("Link") back
|
|
Packit |
90a5c9 |
* to local uris */
|
|
Packit |
90a5c9 |
stream->real_server_uri = apr_psprintf(stream->pool, "%s://%s", scheme, authority);
|
|
Packit |
90a5c9 |
stream->p_server_uri = apr_psprintf(stream->pool, "%s://%s", puri.scheme, authority);
|
|
Packit |
90a5c9 |
path = apr_uri_unparse(stream->pool, &puri, APR_URI_UNP_OMITSITEPART);
|
|
Packit |
90a5c9 |
h2_proxy_req_make(stream->req, stream->pool, r->method, scheme,
|
|
Packit |
90a5c9 |
authority, path, r->headers_in);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (dconf->add_forwarded_headers) {
|
|
Packit |
90a5c9 |
if (PROXYREQ_REVERSE == r->proxyreq) {
|
|
Packit |
90a5c9 |
const char *buf;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Add X-Forwarded-For: so that the upstream has a chance to
|
|
Packit |
90a5c9 |
* determine, where the original request came from.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
apr_table_mergen(stream->req->headers, "X-Forwarded-For",
|
|
Packit |
90a5c9 |
r->useragent_ip);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Add X-Forwarded-Host: so that upstream knows what the
|
|
Packit |
90a5c9 |
* original request hostname was.
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
if ((buf = apr_table_get(r->headers_in, "Host"))) {
|
|
Packit |
90a5c9 |
apr_table_mergen(stream->req->headers, "X-Forwarded-Host", buf);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Add X-Forwarded-Server: so that upstream knows what the
|
|
Packit |
90a5c9 |
* name of this proxy server is (if there are more than one)
|
|
Packit |
90a5c9 |
* XXX: This duplicates Via: - do we strictly need it?
|
|
Packit |
90a5c9 |
*/
|
|
Packit |
90a5c9 |
apr_table_mergen(stream->req->headers, "X-Forwarded-Server",
|
|
Packit |
90a5c9 |
r->server->server_hostname);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* Tuck away all already existing cookies */
|
|
Packit |
90a5c9 |
stream->saves = apr_table_make(r->pool, 2);
|
|
Packit |
90a5c9 |
apr_table_do(add_header, stream->saves, r->headers_out, "Set-Cookie", NULL);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
*pstream = stream;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t submit_stream(h2_proxy_session *session, h2_proxy_stream *stream)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_ngheader *hd;
|
|
Packit |
90a5c9 |
nghttp2_data_provider *pp = NULL;
|
|
Packit |
90a5c9 |
nghttp2_data_provider provider;
|
|
Packit |
90a5c9 |
int rv, may_have_request_body = 1;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
hd = h2_proxy_util_nghd_make_req(stream->pool, stream->req);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
/* If we expect a 100-continue response, we must refrain from reading
|
|
Packit |
90a5c9 |
any input until we get it. Reading the input will possibly trigger
|
|
Packit |
90a5c9 |
HTTP_IN filter to generate the 100-continue itself. */
|
|
Packit |
90a5c9 |
if (stream->waiting_on_100 || stream->waiting_on_ping) {
|
|
Packit |
90a5c9 |
/* make a small test if we get an EOF/EOS immediately */
|
|
Packit |
90a5c9 |
status = ap_get_brigade(stream->r->input_filters, stream->input,
|
|
Packit |
90a5c9 |
AP_MODE_READBYTES, APR_NONBLOCK_READ,
|
|
Packit |
90a5c9 |
APR_BUCKET_BUFF_SIZE);
|
|
Packit |
90a5c9 |
may_have_request_body = APR_STATUS_IS_EAGAIN(status)
|
|
Packit |
90a5c9 |
|| (status == APR_SUCCESS
|
|
Packit |
90a5c9 |
&& !APR_BUCKET_IS_EOS(APR_BRIGADE_FIRST(stream->input)));
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (may_have_request_body) {
|
|
Packit |
90a5c9 |
provider.source.fd = 0;
|
|
Packit |
90a5c9 |
provider.source.ptr = NULL;
|
|
Packit |
90a5c9 |
provider.read_callback = stream_request_data;
|
|
Packit |
90a5c9 |
pp = &provider;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
rv = nghttp2_submit_request(session->ngh2, NULL,
|
|
Packit |
90a5c9 |
hd->nv, hd->nvlen, pp, stream);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03363)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): submit %s%s -> %d",
|
|
Packit |
90a5c9 |
session->id, stream->req->authority, stream->req->path,
|
|
Packit |
90a5c9 |
rv);
|
|
Packit |
90a5c9 |
if (rv > 0) {
|
|
Packit |
90a5c9 |
stream->id = rv;
|
|
Packit |
90a5c9 |
stream->state = H2_STREAM_ST_OPEN;
|
|
Packit |
90a5c9 |
h2_proxy_ihash_add(session->streams, stream);
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_STREAM_SUBMITTED, rv, NULL);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return APR_EGENERAL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t feed_brigade(h2_proxy_session *session, apr_bucket_brigade *bb)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t status = APR_SUCCESS;
|
|
Packit |
90a5c9 |
apr_size_t readlen = 0;
|
|
Packit |
90a5c9 |
ssize_t n;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
while (status == APR_SUCCESS && !APR_BRIGADE_EMPTY(bb)) {
|
|
Packit |
90a5c9 |
apr_bucket* b = APR_BRIGADE_FIRST(bb);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (APR_BUCKET_IS_METADATA(b)) {
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
const char *bdata = NULL;
|
|
Packit |
90a5c9 |
apr_size_t blen = 0;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = apr_bucket_read(b, &bdata, &blen, APR_BLOCK_READ);
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS && blen > 0) {
|
|
Packit |
90a5c9 |
n = nghttp2_session_mem_recv(session->ngh2, (const uint8_t *)bdata, blen);
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): feeding %ld bytes -> %ld",
|
|
Packit |
90a5c9 |
session->id, (long)blen, (long)n);
|
|
Packit |
90a5c9 |
if (n < 0) {
|
|
Packit |
90a5c9 |
if (nghttp2_is_fatal((int)n)) {
|
|
Packit |
90a5c9 |
status = APR_EGENERAL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
readlen += n;
|
|
Packit |
90a5c9 |
if (n < blen) {
|
|
Packit |
90a5c9 |
apr_bucket_split(b, n);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
apr_bucket_delete(b);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, status, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): fed %ld bytes of input to session",
|
|
Packit |
90a5c9 |
session->id, (long)readlen);
|
|
Packit |
90a5c9 |
if (readlen == 0 && status == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
return APR_EAGAIN;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t h2_proxy_session_read(h2_proxy_session *session, int block,
|
|
Packit |
90a5c9 |
apr_interval_time_t timeout)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t status = APR_SUCCESS;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (APR_BRIGADE_EMPTY(session->input)) {
|
|
Packit |
90a5c9 |
apr_socket_t *socket = NULL;
|
|
Packit |
90a5c9 |
apr_time_t save_timeout = -1;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (block) {
|
|
Packit |
90a5c9 |
socket = ap_get_conn_socket(session->c);
|
|
Packit |
90a5c9 |
if (socket) {
|
|
Packit |
90a5c9 |
apr_socket_timeout_get(socket, &save_timeout);
|
|
Packit |
90a5c9 |
apr_socket_timeout_set(socket, timeout);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* cannot block on timeout */
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_WARNING, 0, session->c, APLOGNO(03379)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): unable to get conn socket",
|
|
Packit |
90a5c9 |
session->id);
|
|
Packit |
90a5c9 |
return APR_ENOTIMPL;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = ap_get_brigade(session->c->input_filters, session->input,
|
|
Packit |
90a5c9 |
AP_MODE_READBYTES,
|
|
Packit |
90a5c9 |
block? APR_BLOCK_READ : APR_NONBLOCK_READ,
|
|
Packit |
90a5c9 |
64 * 1024);
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE3, status, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): read from conn", session->id);
|
|
Packit |
90a5c9 |
if (socket && save_timeout != -1) {
|
|
Packit |
90a5c9 |
apr_socket_timeout_set(socket, save_timeout);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
status = feed_brigade(session, session->input);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (APR_STATUS_IS_TIMEUP(status)) {
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!APR_STATUS_IS_EAGAIN(status)) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, status, session->c, APLOGNO(03380)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): read error", session->id);
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_CONN_ERROR, status, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_status_t h2_proxy_session_submit(h2_proxy_session *session,
|
|
Packit |
90a5c9 |
const char *url, request_rec *r,
|
|
Packit |
90a5c9 |
int standalone)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = open_stream(session, url, r, standalone, &stream);
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
ap_log_rerror(APLOG_MARK, APLOG_DEBUG, 0, r, APLOGNO(03381)
|
|
Packit |
90a5c9 |
"process stream(%d): %s %s%s, original: %s",
|
|
Packit |
90a5c9 |
stream->id, stream->req->method,
|
|
Packit |
90a5c9 |
stream->req->authority, stream->req->path,
|
|
Packit |
90a5c9 |
r->the_request);
|
|
Packit |
90a5c9 |
status = submit_stream(session, stream);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void stream_resume(h2_proxy_stream *stream)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_session *session = stream->session;
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_stream(%s-%d): resuming",
|
|
Packit |
90a5c9 |
session->id, stream->id);
|
|
Packit |
90a5c9 |
stream->suspended = 0;
|
|
Packit |
90a5c9 |
h2_proxy_iq_remove(session->suspended, stream->id);
|
|
Packit |
90a5c9 |
nghttp2_session_resume_data(session->ngh2, stream->id);
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_STREAM_RESUMED, 0, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t check_suspended(h2_proxy_session *session)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
int i, stream_id;
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
for (i = 0; i < session->suspended->nelts; ++i) {
|
|
Packit |
90a5c9 |
stream_id = session->suspended->elts[i];
|
|
Packit |
90a5c9 |
stream = nghttp2_session_get_stream_user_data(session->ngh2, stream_id);
|
|
Packit |
90a5c9 |
if (stream) {
|
|
Packit |
90a5c9 |
if (stream->waiting_on_100 || stream->waiting_on_ping) {
|
|
Packit |
90a5c9 |
status = APR_EAGAIN;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
status = ap_get_brigade(stream->r->input_filters, stream->input,
|
|
Packit |
90a5c9 |
AP_MODE_READBYTES, APR_NONBLOCK_READ,
|
|
Packit |
90a5c9 |
APR_BUCKET_BUFF_SIZE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS && !APR_BRIGADE_EMPTY(stream->input)) {
|
|
Packit |
90a5c9 |
stream_resume(stream);
|
|
Packit |
90a5c9 |
check_suspended(session);
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (status != APR_SUCCESS && !APR_STATUS_IS_EAGAIN(status)) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_WARNING, status, session->c,
|
|
Packit |
90a5c9 |
APLOGNO(03382) "h2_proxy_stream(%s-%d): check input",
|
|
Packit |
90a5c9 |
session->id, stream_id);
|
|
Packit |
90a5c9 |
stream_resume(stream);
|
|
Packit |
90a5c9 |
check_suspended(session);
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* gone? */
|
|
Packit |
90a5c9 |
h2_proxy_iq_remove(session->suspended, stream_id);
|
|
Packit |
90a5c9 |
check_suspended(session);
|
|
Packit |
90a5c9 |
return APR_SUCCESS;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return APR_EAGAIN;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static apr_status_t session_shutdown(h2_proxy_session *session, int reason,
|
|
Packit |
90a5c9 |
const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t status = APR_SUCCESS;
|
|
Packit |
90a5c9 |
const char *err = msg;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_assert(session);
|
|
Packit |
90a5c9 |
if (!err && reason) {
|
|
Packit |
90a5c9 |
err = nghttp2_strerror(reason);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
nghttp2_submit_goaway(session->ngh2, NGHTTP2_FLAG_NONE, 0,
|
|
Packit |
90a5c9 |
reason, (uint8_t*)err, err? strlen(err):0);
|
|
Packit |
90a5c9 |
status = nghttp2_session_send(session->ngh2);
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_LOCAL_GOAWAY, reason, err);
|
|
Packit |
90a5c9 |
return status;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *StateNames[] = {
|
|
Packit |
90a5c9 |
"INIT", /* H2_PROXYS_ST_INIT */
|
|
Packit |
90a5c9 |
"DONE", /* H2_PROXYS_ST_DONE */
|
|
Packit |
90a5c9 |
"IDLE", /* H2_PROXYS_ST_IDLE */
|
|
Packit |
90a5c9 |
"BUSY", /* H2_PROXYS_ST_BUSY */
|
|
Packit |
90a5c9 |
"WAIT", /* H2_PROXYS_ST_WAIT */
|
|
Packit |
90a5c9 |
"LSHUTDOWN", /* H2_PROXYS_ST_LOCAL_SHUTDOWN */
|
|
Packit |
90a5c9 |
"RSHUTDOWN", /* H2_PROXYS_ST_REMOTE_SHUTDOWN */
|
|
Packit |
90a5c9 |
};
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static const char *state_name(h2_proxys_state state)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (state >= (sizeof(StateNames)/sizeof(StateNames[0]))) {
|
|
Packit |
90a5c9 |
return "unknown";
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return StateNames[state];
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int is_accepting_streams(h2_proxy_session *session)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_IDLE:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_BUSY:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_WAIT:
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void transit(h2_proxy_session *session, const char *action,
|
|
Packit |
90a5c9 |
h2_proxys_state nstate)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03345)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): transit [%s] -- %s --> [%s]", session->id,
|
|
Packit |
90a5c9 |
state_name(session->state), action, state_name(nstate));
|
|
Packit |
90a5c9 |
session->state = nstate;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_init(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_INIT:
|
|
Packit |
90a5c9 |
if (h2_proxy_ihash_empty(session->streams)) {
|
|
Packit |
90a5c9 |
transit(session, "init", H2_PROXYS_ST_IDLE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
transit(session, "init", H2_PROXYS_ST_BUSY);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_local_goaway(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
|
|
Packit |
90a5c9 |
/* already did that? */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_IDLE:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_REMOTE_SHUTDOWN:
|
|
Packit |
90a5c9 |
/* all done */
|
|
Packit |
90a5c9 |
transit(session, "local goaway", H2_PROXYS_ST_DONE);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
transit(session, "local goaway", H2_PROXYS_ST_LOCAL_SHUTDOWN);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_remote_goaway(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_REMOTE_SHUTDOWN:
|
|
Packit |
90a5c9 |
/* already received that? */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_IDLE:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
|
|
Packit |
90a5c9 |
/* all done */
|
|
Packit |
90a5c9 |
transit(session, "remote goaway", H2_PROXYS_ST_DONE);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
transit(session, "remote goaway", H2_PROXYS_ST_REMOTE_SHUTDOWN);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_conn_error(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_INIT:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_DONE:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
|
|
Packit |
90a5c9 |
/* just leave */
|
|
Packit |
90a5c9 |
transit(session, "conn error", H2_PROXYS_ST_DONE);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, arg, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): conn error -> shutdown", session->id);
|
|
Packit |
90a5c9 |
session_shutdown(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_proto_error(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_DONE:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
|
|
Packit |
90a5c9 |
/* just leave */
|
|
Packit |
90a5c9 |
transit(session, "proto error", H2_PROXYS_ST_DONE);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): proto error -> shutdown", session->id);
|
|
Packit |
90a5c9 |
session_shutdown(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_conn_timeout(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
|
|
Packit |
90a5c9 |
transit(session, "conn timeout", H2_PROXYS_ST_DONE);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
session_shutdown(session, arg, msg);
|
|
Packit |
90a5c9 |
transit(session, "conn timeout", H2_PROXYS_ST_DONE);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_no_io(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_BUSY:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_REMOTE_SHUTDOWN:
|
|
Packit |
90a5c9 |
/* nothing for input and output to do. If we remain
|
|
Packit |
90a5c9 |
* in this state, we go into a tight loop and suck up
|
|
Packit |
90a5c9 |
* CPU cycles. Ideally, we'd like to do a blocking read, but that
|
|
Packit |
90a5c9 |
* is not possible if we have scheduled tasks and wait
|
|
Packit |
90a5c9 |
* for them to produce something. */
|
|
Packit |
90a5c9 |
if (h2_proxy_ihash_empty(session->streams)) {
|
|
Packit |
90a5c9 |
if (!is_accepting_streams(session)) {
|
|
Packit |
90a5c9 |
/* We are no longer accepting new streams and have
|
|
Packit |
90a5c9 |
* finished processing existing ones. Time to leave. */
|
|
Packit |
90a5c9 |
session_shutdown(session, arg, msg);
|
|
Packit |
90a5c9 |
transit(session, "no io", H2_PROXYS_ST_DONE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* When we have no streams, no task events are possible,
|
|
Packit |
90a5c9 |
* switch to blocking reads */
|
|
Packit |
90a5c9 |
transit(session, "no io", H2_PROXYS_ST_IDLE);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
/* Unable to do blocking reads, as we wait on events from
|
|
Packit |
90a5c9 |
* task processing in other threads. Do a busy wait with
|
|
Packit |
90a5c9 |
* backoff timer. */
|
|
Packit |
90a5c9 |
transit(session, "no io", H2_PROXYS_ST_WAIT);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_stream_submitted(h2_proxy_session *session, int stream_id,
|
|
Packit |
90a5c9 |
const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_IDLE:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_WAIT:
|
|
Packit |
90a5c9 |
transit(session, "stream submitted", H2_PROXYS_ST_BUSY);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_stream_done(h2_proxy_session *session, int stream_id,
|
|
Packit |
90a5c9 |
const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
stream = nghttp2_session_get_stream_user_data(session->ngh2, stream_id);
|
|
Packit |
90a5c9 |
if (stream) {
|
|
Packit |
90a5c9 |
int touched = (stream->data_sent ||
|
|
Packit |
90a5c9 |
stream_id <= session->last_stream_id);
|
|
Packit |
90a5c9 |
apr_status_t status = (stream->error_code == 0)? APR_SUCCESS : APR_EINVAL;
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03364)
|
|
Packit |
90a5c9 |
"h2_proxy_sesssion(%s): stream(%d) closed "
|
|
Packit |
90a5c9 |
"(touched=%d, error=%d)",
|
|
Packit |
90a5c9 |
session->id, stream_id, touched, stream->error_code);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (status != APR_SUCCESS) {
|
|
Packit |
90a5c9 |
stream->r->status = 500;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (!stream->data_received) {
|
|
Packit |
90a5c9 |
apr_bucket *b;
|
|
Packit |
90a5c9 |
/* if the response had no body, this is the time to flush
|
|
Packit |
90a5c9 |
* an empty brigade which will also write the resonse
|
|
Packit |
90a5c9 |
* headers */
|
|
Packit |
90a5c9 |
h2_proxy_stream_end_headers_out(stream);
|
|
Packit |
90a5c9 |
stream->data_received = 1;
|
|
Packit |
90a5c9 |
b = apr_bucket_flush_create(stream->r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(stream->output, b);
|
|
Packit |
90a5c9 |
b = apr_bucket_eos_create(stream->r->connection->bucket_alloc);
|
|
Packit |
90a5c9 |
APR_BRIGADE_INSERT_TAIL(stream->output, b);
|
|
Packit |
90a5c9 |
ap_pass_brigade(stream->r->output_filters, stream->output);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
stream->state = H2_STREAM_ST_CLOSED;
|
|
Packit |
90a5c9 |
h2_proxy_ihash_remove(session->streams, stream_id);
|
|
Packit |
90a5c9 |
h2_proxy_iq_remove(session->suspended, stream_id);
|
|
Packit |
90a5c9 |
if (session->done) {
|
|
Packit |
90a5c9 |
session->done(session, stream->r, status, touched);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_stream_resumed(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_WAIT:
|
|
Packit |
90a5c9 |
transit(session, "stream resumed", H2_PROXYS_ST_BUSY);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_data_read(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_IDLE:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_WAIT:
|
|
Packit |
90a5c9 |
transit(session, "data read", H2_PROXYS_ST_BUSY);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_ngh2_done(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_DONE:
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
transit(session, "nghttp2 done", H2_PROXYS_ST_DONE);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ev_pre_close(h2_proxy_session *session, int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_DONE:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
|
|
Packit |
90a5c9 |
/* nop */
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
session_shutdown(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void dispatch_event(h2_proxy_session *session, h2_proxys_event_t ev,
|
|
Packit |
90a5c9 |
int arg, const char *msg)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
switch (ev) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_INIT:
|
|
Packit |
90a5c9 |
ev_init(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_LOCAL_GOAWAY:
|
|
Packit |
90a5c9 |
ev_local_goaway(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_REMOTE_GOAWAY:
|
|
Packit |
90a5c9 |
ev_remote_goaway(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_CONN_ERROR:
|
|
Packit |
90a5c9 |
ev_conn_error(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_PROTO_ERROR:
|
|
Packit |
90a5c9 |
ev_proto_error(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_CONN_TIMEOUT:
|
|
Packit |
90a5c9 |
ev_conn_timeout(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_NO_IO:
|
|
Packit |
90a5c9 |
ev_no_io(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_STREAM_SUBMITTED:
|
|
Packit |
90a5c9 |
ev_stream_submitted(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_STREAM_DONE:
|
|
Packit |
90a5c9 |
ev_stream_done(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_STREAM_RESUMED:
|
|
Packit |
90a5c9 |
ev_stream_resumed(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_DATA_READ:
|
|
Packit |
90a5c9 |
ev_data_read(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_NGH2_DONE:
|
|
Packit |
90a5c9 |
ev_ngh2_done(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
case H2_PROXYS_EV_PRE_CLOSE:
|
|
Packit |
90a5c9 |
ev_pre_close(session, arg, msg);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): unknown event %d",
|
|
Packit |
90a5c9 |
session->id, ev);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int send_loop(h2_proxy_session *session)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
while (nghttp2_session_want_write(session->ngh2)) {
|
|
Packit |
90a5c9 |
int rv = nghttp2_session_send(session->ngh2);
|
|
Packit |
90a5c9 |
if (rv < 0 && nghttp2_is_fatal(rv)) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): write, rv=%d", session->id, rv);
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_CONN_ERROR, rv, NULL);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
apr_status_t h2_proxy_session_process(h2_proxy_session *session)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
apr_status_t status;
|
|
Packit |
90a5c9 |
int have_written = 0, have_read = 0;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): process", session->id);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
run_loop:
|
|
Packit |
90a5c9 |
switch (session->state) {
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_INIT:
|
|
Packit |
90a5c9 |
status = session_start(session);
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_INIT, 0, NULL);
|
|
Packit |
90a5c9 |
goto run_loop;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_CONN_ERROR, status, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_BUSY:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_LOCAL_SHUTDOWN:
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_REMOTE_SHUTDOWN:
|
|
Packit |
90a5c9 |
have_written = send_loop(session);
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (nghttp2_session_want_read(session->ngh2)) {
|
|
Packit |
90a5c9 |
status = h2_proxy_session_read(session, 0, 0);
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
have_read = 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!have_written && !have_read
|
|
Packit |
90a5c9 |
&& !nghttp2_session_want_write(session->ngh2)) {
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_NO_IO, 0, NULL);
|
|
Packit |
90a5c9 |
goto run_loop;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_WAIT:
|
|
Packit |
90a5c9 |
if (check_suspended(session) == APR_EAGAIN) {
|
|
Packit |
90a5c9 |
/* no stream has become resumed. Do a blocking read with
|
|
Packit |
90a5c9 |
* ever increasing timeouts... */
|
|
Packit |
90a5c9 |
if (session->wait_timeout < 25) {
|
|
Packit |
90a5c9 |
session->wait_timeout = 25;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else {
|
|
Packit |
90a5c9 |
session->wait_timeout = H2MIN(apr_time_from_msec(100),
|
|
Packit |
90a5c9 |
2*session->wait_timeout);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
status = h2_proxy_session_read(session, 1, session->wait_timeout);
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE3, status, session->c,
|
|
Packit |
90a5c9 |
APLOGNO(03365)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): WAIT read, timeout=%fms",
|
|
Packit |
90a5c9 |
session->id, (float)session->wait_timeout/1000.0);
|
|
Packit |
90a5c9 |
if (status == APR_SUCCESS) {
|
|
Packit |
90a5c9 |
have_read = 1;
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_DATA_READ, 0, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
else if (APR_STATUS_IS_TIMEUP(status)
|
|
Packit |
90a5c9 |
|| APR_STATUS_IS_EAGAIN(status)) {
|
|
Packit |
90a5c9 |
/* go back to checking all inputs again */
|
|
Packit |
90a5c9 |
transit(session, "wait cycle", H2_PROXYS_ST_BUSY);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_IDLE:
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
case H2_PROXYS_ST_DONE: /* done, session terminated */
|
|
Packit |
90a5c9 |
return APR_EOF;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
default:
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_ERR, APR_EGENERAL, session->c,
|
|
Packit |
90a5c9 |
APLOGNO(03346)"h2_proxy_session(%s): unknown state %d",
|
|
Packit |
90a5c9 |
session->id, session->state);
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_PROTO_ERROR, 0, NULL);
|
|
Packit |
90a5c9 |
break;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (have_read || have_written) {
|
|
Packit |
90a5c9 |
session->wait_timeout = 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!nghttp2_session_want_read(session->ngh2)
|
|
Packit |
90a5c9 |
&& !nghttp2_session_want_write(session->ngh2)) {
|
|
Packit |
90a5c9 |
dispatch_event(session, H2_PROXYS_EV_NGH2_DONE, 0, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
return APR_SUCCESS; /* needs to be called again */
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
h2_proxy_session *session;
|
|
Packit |
90a5c9 |
h2_proxy_request_done *done;
|
|
Packit |
90a5c9 |
} cleanup_iter_ctx;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int cancel_iter(void *udata, void *val)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
cleanup_iter_ctx *ctx = udata;
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream = val;
|
|
Packit |
90a5c9 |
nghttp2_submit_rst_stream(ctx->session->ngh2, NGHTTP2_FLAG_NONE,
|
|
Packit |
90a5c9 |
stream->id, 0);
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
void h2_proxy_session_cancel_all(h2_proxy_session *session)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (!h2_proxy_ihash_empty(session->streams)) {
|
|
Packit |
90a5c9 |
cleanup_iter_ctx ctx;
|
|
Packit |
90a5c9 |
ctx.session = session;
|
|
Packit |
90a5c9 |
ctx.done = session->done;
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03366)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): cancel %d streams",
|
|
Packit |
90a5c9 |
session->id, (int)h2_proxy_ihash_count(session->streams));
|
|
Packit |
90a5c9 |
h2_proxy_ihash_iter(session->streams, cancel_iter, &ctx;;
|
|
Packit |
90a5c9 |
session_shutdown(session, 0, NULL);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int done_iter(void *udata, void *val)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
cleanup_iter_ctx *ctx = udata;
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream = val;
|
|
Packit |
90a5c9 |
int touched = (stream->data_sent ||
|
|
Packit |
90a5c9 |
stream->id <= ctx->session->last_stream_id);
|
|
Packit |
90a5c9 |
ctx->done(ctx->session, stream->r, APR_ECONNABORTED, touched);
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
void h2_proxy_session_cleanup(h2_proxy_session *session,
|
|
Packit |
90a5c9 |
h2_proxy_request_done *done)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (!h2_proxy_ihash_empty(session->streams)) {
|
|
Packit |
90a5c9 |
cleanup_iter_ctx ctx;
|
|
Packit |
90a5c9 |
ctx.session = session;
|
|
Packit |
90a5c9 |
ctx.done = done;
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03519)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): terminated, %d streams unfinished",
|
|
Packit |
90a5c9 |
session->id, (int)h2_proxy_ihash_count(session->streams));
|
|
Packit |
90a5c9 |
h2_proxy_ihash_iter(session->streams, done_iter, &ctx;;
|
|
Packit |
90a5c9 |
h2_proxy_ihash_clear(session->streams);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int ping_arrived_iter(void *udata, void *val)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream = val;
|
|
Packit |
90a5c9 |
if (stream->waiting_on_ping) {
|
|
Packit |
90a5c9 |
stream->waiting_on_ping = 0;
|
|
Packit |
90a5c9 |
stream_resume(stream);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static void ping_arrived(h2_proxy_session *session)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (!h2_proxy_ihash_empty(session->streams)) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_DEBUG, 0, session->c, APLOGNO(03470)
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): ping arrived, unblocking streams",
|
|
Packit |
90a5c9 |
session->id);
|
|
Packit |
90a5c9 |
h2_proxy_ihash_iter(session->streams, ping_arrived_iter, &session);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
typedef struct {
|
|
Packit |
90a5c9 |
h2_proxy_session *session;
|
|
Packit |
90a5c9 |
conn_rec *c;
|
|
Packit |
90a5c9 |
apr_off_t bytes;
|
|
Packit |
90a5c9 |
int updated;
|
|
Packit |
90a5c9 |
} win_update_ctx;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
static int win_update_iter(void *udata, void *val)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
win_update_ctx *ctx = udata;
|
|
Packit |
90a5c9 |
h2_proxy_stream *stream = val;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (stream->r && stream->r->connection == ctx->c) {
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, ctx->session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s-%d): win_update %ld bytes",
|
|
Packit |
90a5c9 |
ctx->session->id, (int)stream->id, (long)ctx->bytes);
|
|
Packit |
90a5c9 |
nghttp2_session_consume(ctx->session->ngh2, stream->id, ctx->bytes);
|
|
Packit |
90a5c9 |
ctx->updated = 1;
|
|
Packit |
90a5c9 |
return 0;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
return 1;
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
void h2_proxy_session_update_window(h2_proxy_session *session,
|
|
Packit |
90a5c9 |
conn_rec *c, apr_off_t bytes)
|
|
Packit |
90a5c9 |
{
|
|
Packit |
90a5c9 |
if (!h2_proxy_ihash_empty(session->streams)) {
|
|
Packit |
90a5c9 |
win_update_ctx ctx;
|
|
Packit |
90a5c9 |
ctx.session = session;
|
|
Packit |
90a5c9 |
ctx.c = c;
|
|
Packit |
90a5c9 |
ctx.bytes = bytes;
|
|
Packit |
90a5c9 |
ctx.updated = 0;
|
|
Packit |
90a5c9 |
h2_proxy_ihash_iter(session->streams, win_update_iter, &ctx;;
|
|
Packit |
90a5c9 |
|
|
Packit |
90a5c9 |
if (!ctx.updated) {
|
|
Packit |
90a5c9 |
/* could not find the stream any more, possibly closed, update
|
|
Packit |
90a5c9 |
* the connection window at least */
|
|
Packit |
90a5c9 |
ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, session->c,
|
|
Packit |
90a5c9 |
"h2_proxy_session(%s): win_update conn %ld bytes",
|
|
Packit |
90a5c9 |
session->id, (long)bytes);
|
|
Packit |
90a5c9 |
nghttp2_session_consume_connection(session->ngh2, (size_t)bytes);
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
}
|
|
Packit |
90a5c9 |
|