Skip to content

Commit

Permalink
http_client.c: fix tracing error response message bodies
Browse files Browse the repository at this point in the history
  • Loading branch information
DDvO committed Oct 9, 2024
1 parent 4bb9108 commit b3f032b
Showing 1 changed file with 13 additions and 12 deletions.
25 changes: 13 additions & 12 deletions crypto/http/http_client.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,14 +80,15 @@ struct ossl_http_req_ctx_st {
#define OHS_WRITE_HDR (4 | OHS_NOREAD) /* Request header being sent */
#define OHS_WRITE_REQ (5 | OHS_NOREAD) /* Request content being sent */
#define OHS_FLUSH (6 | OHS_NOREAD) /* Request being flushed */
#define OHS_ASN1_DONE (7 | OHS_NOREAD) /* ASN1 content read completed */
#define OHS_STREAM (8 | OHS_NOREAD) /* HTTP content stream to be read */
#define OHS_FIRSTLINE 1 /* First line of response being read */
#define OHS_HEADERS 2 /* MIME headers of response being read */
#define OHS_HEADERS_ERROR 3 /* MIME headers of resp. being read after error */
#define OHS_REDIRECT 4 /* MIME headers being read, expecting Location */
#define OHS_ASN1_HEADER 5 /* ASN1 sequence header (tag+length) being read */
#define OHS_ASN1_CONTENT 6 /* ASN1 content octets being read */
#define OHS_ASN1_DONE (7 | OHS_NOREAD) /* ASN1 content read completed */
#define OHS_STREAM (8 | OHS_NOREAD) /* HTTP content stream to be read */
#define OHS_BODY_ERROR 7 /* response body being read after error */

/* Low-level HTTP API implementation */

Expand Down Expand Up @@ -577,6 +578,8 @@ int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx)
}
}
if (n <= 0) {
if (rctx->state == OHS_BODY_ERROR && OSSL_TRACE_ENABLED(HTTP))
OSSL_TRACE(HTTP, "]\n"); /* end of error response body */
if (BIO_should_retry(rctx->rbio))
return -1;
ERR_raise(ERR_LIB_HTTP, HTTP_R_FAILED_READING_DATA);
Expand Down Expand Up @@ -696,6 +699,12 @@ int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx)
return 0;
}

if (rctx->state == OHS_BODY_ERROR) {
if (OSSL_TRACE_ENABLED(HTTP)) /* dump response body line */
OSSL_TRACE_STRING(HTTP, got_text, 1, (unsigned char *)buf, n);
goto next_line;
}

resp_hdr_lines++;
if (rctx->max_hdr_lines != 0 && rctx->max_hdr_lines < resp_hdr_lines) {
ERR_raise(ERR_LIB_HTTP, HTTP_R_RESPONSE_TOO_MANY_HDRLINES);
Expand Down Expand Up @@ -818,18 +827,10 @@ int OSSL_HTTP_REQ_CTX_nbio(OSSL_HTTP_REQ_CTX *rctx)
}

if (rctx->state == OHS_HEADERS_ERROR) {
rctx->state = OHS_BODY_ERROR;
if (OSSL_TRACE_ENABLED(HTTP)) {
int printed_final_nl = 0;

OSSL_TRACE(HTTP, "Received error response body: [\n");
while ((n = BIO_read(rctx->rbio, rctx->buf, rctx->buf_size)) > 0
|| (OSSL_sleep(100), BIO_should_retry(rctx->rbio))) {
OSSL_TRACE_STRING(HTTP, got_text, 1, rctx->buf, n);
if (n > 0)
printed_final_nl = rctx->buf[n - 1] == '\n';
}
OSSL_TRACE1(HTTP, "%s]\n", printed_final_nl ? "" : "\n");
(void)printed_final_nl; /* avoid warning unless enable-trace */
goto next_line;
}
return 0;
}
Expand Down

0 comments on commit b3f032b

Please sign in to comment.