MINOR: http-ana: Add a function to write an http reply in an HTX message

When an error response is sent to a client, the write of the http reply in the
channel buffer and its sending are performed in different functions. The
http_reply_to_htx() function is used to write an http reply in HTX message. This
way, it could be possible to use the http replies in a different context.
This commit is contained in:
Christopher Faulet 2020-05-15 15:12:47 +02:00
parent 963ce5bc06
commit 97e466c9e3
1 changed files with 26 additions and 16 deletions

View File

@ -4654,25 +4654,19 @@ struct http_reply *http_error_message(struct stream *s)
return &http_err_replies[msgnum]; return &http_err_replies[msgnum];
} }
/* Produces a response from an http reply. Depending on the http reply type, a, /* Produces an HTX message from an http reply. Depending on the http reply type, a,
* errorfile, an raw file or a log-format string is used. On success, it returns * errorfile, an raw file or a log-format string is used. On success, it returns
* 0. If an error occurs -1 is returned. * 0. If an error occurs -1 is returned.
*/ */
int http_reply_message(struct stream *s, struct http_reply *reply) static int http_reply_to_htx(struct stream *s, struct htx *htx, struct http_reply *reply)
{ {
struct channel *res = &s->res;
struct buffer *errmsg; struct buffer *errmsg;
struct htx *htx = htx_from_buf(&res->buf);
struct htx_sl *sl; struct htx_sl *sl;
struct buffer *body = NULL; struct buffer *body = NULL;
const char *status, *reason, *clen, *ctype; const char *status, *reason, *clen, *ctype;
unsigned int slflags; unsigned int slflags;
int ret = 0; int ret = 0;
if (s->txn->status == -1)
s->txn->status = reply->status;
channel_htx_truncate(res, htx);
/* /*
* - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so * - HTTP_REPLY_ERRFILES unexpected here. handled as no payload if so
* *
@ -4759,10 +4753,6 @@ int http_reply_message(struct stream *s, struct http_reply *reply)
goto fail; goto fail;
} }
htx_to_buf(htx, &s->res.buf);
if (!http_forward_proxy_resp(s, 1))
goto fail;
leave: leave:
if (reply->type == HTTP_REPLY_LOGFMT) if (reply->type == HTTP_REPLY_LOGFMT)
free_trash_chunk(body); free_trash_chunk(body);
@ -4771,17 +4761,37 @@ int http_reply_message(struct stream *s, struct http_reply *reply)
fail_alloc: fail_alloc:
if (!(s->flags & SF_ERR_MASK)) if (!(s->flags & SF_ERR_MASK))
s->flags |= SF_ERR_RESOURCE; s->flags |= SF_ERR_RESOURCE;
/* fall through */
fail:
ret = -1; ret = -1;
goto leave; goto leave;
}
/* Send an http reply to the client. On success, it returns 0. If an error
* occurs -1 is returned.
*/
int http_reply_message(struct stream *s, struct http_reply *reply)
{
struct channel *res = &s->res;
struct htx *htx = htx_from_buf(&res->buf);
if (s->txn->status == -1)
s->txn->status = reply->status;
channel_htx_truncate(res, htx);
if (http_reply_to_htx(s, htx, reply) == -1)
goto fail;
htx_to_buf(htx, &s->res.buf);
if (!http_forward_proxy_resp(s, 1))
goto fail;
return 0;
fail: fail:
/* If an error occurred, remove the incomplete HTTP response from the
* buffer */
channel_htx_truncate(res, htx); channel_htx_truncate(res, htx);
ret = -1;
if (!(s->flags & SF_ERR_MASK)) if (!(s->flags & SF_ERR_MASK))
s->flags |= SF_ERR_PRXCOND; s->flags |= SF_ERR_PRXCOND;
goto leave; return -1;
} }
/* Return the error message corresponding to si->err_type. It is assumed /* Return the error message corresponding to si->err_type. It is assumed