MINOR: http: factor out the content-type checks

Let's only look up the content-type header once. This involves
inverting the condition which is not dramatic.

Also, we now always check the value length before comparing it, and we
always reset the ctx.idx before looking a header up. Otherwise that
could make header lookups depend on their on-wire order. It would be
a minor issue however since at worst it would cause some responses not
to be compressed.
This commit is contained in:
Willy Tarreau 2012-11-26 16:33:37 +01:00
parent 5730c68b46
commit 0a80a8dbb2

View File

@ -2064,44 +2064,43 @@ int select_compression_response_header(struct session *s, struct buffer *res)
if (txn->status != 200) if (txn->status != 200)
goto fail; goto fail;
ctx.idx = 0;
/* Content-Length is null */ /* Content-Length is null */
if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0) if (!(msg->flags & HTTP_MSGF_TE_CHNK) && msg->body_len == 0)
goto fail; goto fail;
/* content is already compressed */ /* content is already compressed */
ctx.idx = 0;
if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx)) if (http_find_header2("Content-Encoding", 16, res->p, &txn->hdr_idx, &ctx))
goto fail; goto fail;
comp_type = NULL; comp_type = NULL;
/* if there was a compression content-type option in the backend or the frontend /* we don't want to compress multipart content-types, nor content-types that are
* The backend have priority. * not listed in the "compression type" directive if any. If no content-type was
* found but configuration requires one, we don't compress either. Backend has
* the priority.
*/ */
if ((s->be->comp && (comp_type = s->be->comp->types)) || (s->fe->comp && (comp_type = s->fe->comp->types))) { ctx.idx = 0;
if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) { if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
if (ctx.vlen >= 9 && strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
goto fail;
if ((s->be->comp && (comp_type = s->be->comp->types)) ||
(s->fe->comp && (comp_type = s->fe->comp->types))) {
for (; comp_type; comp_type = comp_type->next) { for (; comp_type; comp_type = comp_type->next) {
if (strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0) if (ctx.vlen >= comp_type->name_len &&
strncasecmp(ctx.line+ctx.val, comp_type->name, comp_type->name_len) == 0)
/* this Content-Type should be compressed */ /* this Content-Type should be compressed */
break; break;
} }
} else {
/* there is no Content-Type header */
goto fail;
}
/* this Content-Type should not be compressed */ /* this Content-Type should not be compressed */
if (comp_type == NULL) if (comp_type == NULL)
goto fail; goto fail;
} }
}
ctx.idx = 0; else { /* no content-type header */
if ((s->be->comp && s->be->comp->types) || (s->fe->comp && s->fe->comp->types))
/* Don't compress multipart */ goto fail; /* a content-type was required */
if (http_find_header2("Content-Type", 12, res->p, &txn->hdr_idx, &ctx)) {
if (strncasecmp("multipart", ctx.line+ctx.val, 9) == 0)
goto fail;
} }
/* limit compression rate */ /* limit compression rate */
@ -2120,6 +2119,7 @@ int select_compression_response_header(struct session *s, struct buffer *res)
s->flags |= SN_COMP_READY; s->flags |= SN_COMP_READY;
/* remove Content-Length header */ /* remove Content-Length header */
ctx.idx = 0;
if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx)) if ((msg->flags & HTTP_MSGF_CNT_LEN) && http_find_header2("Content-Length", 14, res->p, &txn->hdr_idx, &ctx))
http_remove_header2(msg, &txn->hdr_idx, &ctx); http_remove_header2(msg, &txn->hdr_idx, &ctx);