From e367ad643b4a74e7e3feb0ecc65e908e68c0e3d2 Mon Sep 17 00:00:00 2001 From: Stefan Eissing Date: Mon, 14 Dec 2015 11:09:31 +0000 Subject: [PATCH] saving some bytes and cycles by not create h2_ctx for every connection and checking for c->master in the hooks git-svn-id: https://svn.apache.org/repos/asf/httpd/httpd/trunk@1719881 13f79535-47bb-0310-9956-ffa450edef68 --- modules/http2/h2_alt_svc.c | 4 +--- modules/http2/h2_config.c | 16 +++++++++------- modules/http2/h2_ctx.c | 25 +++++++++++++++---------- modules/http2/h2_ctx.h | 16 +++++++++++++--- modules/http2/h2_h2.c | 12 ++++++++++-- modules/http2/h2_switch.c | 2 +- modules/http2/h2_task.c | 14 ++++++++++++-- 7 files changed, 61 insertions(+), 28 deletions(-) diff --git a/modules/http2/h2_alt_svc.c b/modules/http2/h2_alt_svc.c index 6bc3cd0dff..dea16af7eb 100644 --- a/modules/http2/h2_alt_svc.c +++ b/modules/http2/h2_alt_svc.c @@ -73,7 +73,6 @@ h2_alt_svc *h2_alt_svc_parse(const char *s, apr_pool_t *pool) { static int h2_alt_svc_handler(request_rec *r) { - h2_ctx *ctx; const h2_config *cfg; int i; @@ -82,8 +81,7 @@ static int h2_alt_svc_handler(request_rec *r) return DECLINED; } - ctx = h2_ctx_rget(r); - if (h2_ctx_is_active(ctx) || h2_ctx_is_task(ctx)) { + if (h2_ctx_rget(r)) { return DECLINED; } diff --git a/modules/http2/h2_config.c b/modules/http2/h2_config.c index 0471424705..5e7af79a69 100644 --- a/modules/http2/h2_config.c +++ b/modules/http2/h2_config.c @@ -561,14 +561,16 @@ const h2_config *h2_config_rget(request_rec *r) const h2_config *h2_config_get(conn_rec *c) { - h2_ctx *ctx = h2_ctx_get(c); + h2_ctx *ctx = h2_ctx_get(c, 0); - if (ctx->config) { - return ctx->config; - } - else if (ctx->server) { - ctx->config = h2_config_sget(ctx->server); - return ctx->config; + if (ctx) { + if (ctx->config) { + return ctx->config; + } + else if (ctx->server) { + ctx->config = h2_config_sget(ctx->server); + return ctx->config; + } } return h2_config_sget(c->base_server); diff --git a/modules/http2/h2_ctx.c b/modules/http2/h2_ctx.c index 08bdd8612d..cedab8a09c 100644 --- a/modules/http2/h2_ctx.c +++ b/modules/http2/h2_ctx.c @@ -20,6 +20,7 @@ #include #include "h2_private.h" +#include "h2_session.h" #include "h2_task.h" #include "h2_ctx.h" #include "h2_private.h" @@ -41,10 +42,10 @@ h2_ctx *h2_ctx_create_for(const conn_rec *c, h2_task *task) return ctx; } -h2_ctx *h2_ctx_get(const conn_rec *c) +h2_ctx *h2_ctx_get(const conn_rec *c, int create) { h2_ctx *ctx = (h2_ctx*)ap_get_module_config(c->conn_config, &http2_module); - if (ctx == NULL) { + if (ctx == NULL && create) { ctx = h2_ctx_create(c); } return ctx; @@ -52,7 +53,7 @@ h2_ctx *h2_ctx_get(const conn_rec *c) h2_ctx *h2_ctx_rget(const request_rec *r) { - return h2_ctx_get(r->connection); + return h2_ctx_get(r->connection, 0); } const char *h2_ctx_protocol_get(const conn_rec *c) @@ -64,10 +65,19 @@ const char *h2_ctx_protocol_get(const conn_rec *c) h2_ctx *h2_ctx_protocol_set(h2_ctx *ctx, const char *proto) { ctx->protocol = proto; - ctx->is_h2 = (proto != NULL); return ctx; } +h2_session *h2_ctx_session_get(h2_ctx *ctx) +{ + return ctx? ctx->session : NULL; +} + +void h2_ctx_session_set(h2_ctx *ctx, struct h2_session *session) +{ + ctx->session = session; +} + h2_ctx *h2_ctx_server_set(h2_ctx *ctx, server_rec *s) { ctx->server = s; @@ -79,12 +89,7 @@ int h2_ctx_is_task(h2_ctx *ctx) return ctx && !!ctx->task; } -int h2_ctx_is_active(h2_ctx *ctx) -{ - return ctx && ctx->is_h2; -} - struct h2_task *h2_ctx_get_task(h2_ctx *ctx) { - return ctx->task; + return ctx? ctx->task : NULL; } diff --git a/modules/http2/h2_ctx.h b/modules/http2/h2_ctx.h index 7f8f2b5949..09b9766d74 100644 --- a/modules/http2/h2_ctx.h +++ b/modules/http2/h2_ctx.h @@ -16,6 +16,7 @@ #ifndef __mod_h2__h2_ctx__ #define __mod_h2__h2_ctx__ +struct h2_session; struct h2_task; struct h2_config; @@ -28,15 +29,22 @@ struct h2_config; * - those created by ourself to perform work on HTTP/2 streams */ typedef struct h2_ctx { - int is_h2; /* h2 engine is used */ const char *protocol; /* the protocol negotiated */ + struct h2_session *session; /* the session established */ struct h2_task *task; /* the h2_task executing or NULL */ const char *hostname; /* hostname negotiated via SNI, optional */ server_rec *server; /* httpd server config selected. */ const struct h2_config *config; /* effective config in this context */ } h2_ctx; -h2_ctx *h2_ctx_get(const conn_rec *c); +/** + * Get (or create) a h2 context record for this connection. + * @param c the connection to look at + * @param create != 0 iff missing context shall be created + * @return h2 context of this connection + */ +h2_ctx *h2_ctx_get(const conn_rec *c, int create); + h2_ctx *h2_ctx_rget(const request_rec *r); h2_ctx *h2_ctx_create_for(const conn_rec *c, struct h2_task *task); @@ -50,13 +58,15 @@ h2_ctx *h2_ctx_protocol_set(h2_ctx *ctx, const char *proto); */ h2_ctx *h2_ctx_server_set(h2_ctx *ctx, server_rec *s); +struct h2_session *h2_ctx_session_get(h2_ctx *ctx); +void h2_ctx_session_set(h2_ctx *ctx, struct h2_session *session); + /** * Get the h2 protocol negotiated for this connection, or NULL. */ const char *h2_ctx_protocol_get(const conn_rec *c); int h2_ctx_is_task(h2_ctx *ctx); -int h2_ctx_is_active(h2_ctx *ctx); struct h2_task *h2_ctx_get_task(h2_ctx *ctx); diff --git a/modules/http2/h2_h2.c b/modules/http2/h2_h2.c index 20fbe30d1e..912b7a88f3 100644 --- a/modules/http2/h2_h2.c +++ b/modules/http2/h2_h2.c @@ -574,8 +574,13 @@ void h2_h2_register_hooks(void) int h2_h2_process_conn(conn_rec* c) { - h2_ctx *ctx = h2_ctx_get(c); + h2_ctx *ctx; + if (c->master) { + return DECLINED; + } + + ctx = h2_ctx_get(c, 0); ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, c, "h2_h2, process_conn"); if (h2_ctx_is_task(ctx)) { /* our stream pseudo connection */ @@ -612,6 +617,9 @@ int h2_h2_process_conn(conn_rec* c) if ((slen >= 24) && !memcmp(H2_MAGIC_TOKEN, s, 24)) { ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, c, "h2_h2, direct mode detected"); + if (!ctx) { + ctx = h2_ctx_get(c, 1); + } h2_ctx_protocol_set(ctx, h2_h2_is_tls(c)? "h2" : "h2c"); } else { @@ -630,7 +638,7 @@ int h2_h2_process_conn(conn_rec* c) /* If "h2" was selected as protocol (by whatever mechanism), take over * the connection. */ - if (h2_ctx_is_active(ctx)) { + if (ctx) { ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, c, "h2_h2, connection, h2 active"); diff --git a/modules/http2/h2_switch.c b/modules/http2/h2_switch.c index ab8b90f7cc..daddb8d310 100644 --- a/modules/http2/h2_switch.c +++ b/modules/http2/h2_switch.c @@ -136,7 +136,7 @@ static int h2_protocol_switch(conn_rec *c, request_rec *r, server_rec *s, } if (found) { - h2_ctx *ctx = h2_ctx_get(c); + h2_ctx *ctx = h2_ctx_get(c, 1); ap_log_cerror(APLOG_MARK, APLOG_TRACE1, 0, c, "switching protocol to '%s'", protocol); diff --git a/modules/http2/h2_task.c b/modules/http2/h2_task.c index 55bcec7293..f4aa068760 100644 --- a/modules/http2/h2_task.c +++ b/modules/http2/h2_task.c @@ -118,8 +118,13 @@ void h2_task_register_hooks(void) static int h2_task_pre_conn(conn_rec* c, void *arg) { - h2_ctx *ctx = h2_ctx_get(c); + h2_ctx *ctx; + if (!c->master) { + return OK; + } + + ctx = h2_ctx_get(c, 0); (void)arg; if (h2_ctx_is_task(ctx)) { h2_task *task = h2_ctx_get_task(ctx); @@ -246,8 +251,13 @@ static apr_status_t h2_task_process_request(const h2_request *req, conn_rec *c) static int h2_task_process_conn(conn_rec* c) { - h2_ctx *ctx = h2_ctx_get(c); + h2_ctx *ctx; + + if (!c->master) { + return DECLINED; + } + ctx = h2_ctx_get(c, 0); if (h2_ctx_is_task(ctx)) { if (!ctx->task->serialize_headers) { ap_log_cerror(APLOG_MARK, APLOG_TRACE2, 0, c, -- 2.40.0