diff --git a/src/supplemental/http/http_api.h b/src/supplemental/http/http_api.h index 02b5fbe45..c8d73788c 100644 --- a/src/supplemental/http/http_api.h +++ b/src/supplemental/http/http_api.h @@ -119,7 +119,7 @@ extern int nni_http_req_add_header(nni_http_req *, const char *, const char *); extern int nni_http_req_alloc_data(nni_http_req *, size_t); extern int nni_http_res_alloc_data(nni_http_res *, size_t); -extern bool nni_http_res_is_error(nni_http_res *); +extern bool nni_http_is_error(nng_http *); extern void nni_http_read(nni_http_conn *, nni_aio *); extern void nni_http_read_full(nni_http_conn *, nni_aio *); diff --git a/src/supplemental/http/http_conn.c b/src/supplemental/http/http_conn.c index 824baa96d..65c240f55 100644 --- a/src/supplemental/http/http_conn.c +++ b/src/supplemental/http/http_conn.c @@ -76,12 +76,17 @@ struct nng_http_conn { size_t rd_put; size_t rd_discard; + // some common headers + http_header host_header; // request + http_header location; // response (redirects) + enum read_flavor rd_flavor; enum write_flavor wr_flavor; - bool rd_buffered; + bool buffered; bool client; // true if a client's connection bool res_sent; bool closed; + bool iserr; }; nng_http_req * @@ -220,7 +225,7 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio) // (Note that we get here if we either have not completed // a full transaction on a FULL read, or were not even able // to get *any* data for a partial RAW read.) - conn->rd_buffered = false; + conn->buffered = false; nni_aio_set_iov(&conn->rd_aio, nio, iov); nng_stream_recv(conn->sock, &conn->rd_aio); return (NNG_EAGAIN); @@ -235,9 +240,9 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio) http_buf_pull_up(conn); if (conn->rd_discard > 0) { nni_iov iov1; - iov1.iov_buf = conn->buf + conn->rd_put; - iov1.iov_len = conn->bufsz - conn->rd_put; - conn->rd_buffered = true; + iov1.iov_buf = conn->buf + conn->rd_put; + iov1.iov_len = conn->bufsz - conn->rd_put; + conn->buffered = true; nni_aio_set_iov(&conn->rd_aio, 1, &iov1); nng_stream_recv(conn->sock, &conn->rd_aio); return (NNG_EAGAIN); @@ -255,9 +260,9 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio) if (rv == NNG_EAGAIN) { nni_iov iov1; http_buf_pull_up(conn); - iov1.iov_buf = conn->buf + conn->rd_put; - iov1.iov_len = conn->bufsz - conn->rd_put; - conn->rd_buffered = true; + iov1.iov_buf = conn->buf + conn->rd_put; + iov1.iov_len = conn->bufsz - conn->rd_put; + conn->buffered = true; if (iov1.iov_len == 0) { return (NNG_EMSGSIZE); } @@ -277,9 +282,9 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio) if (rv == NNG_EAGAIN) { nni_iov iov1; http_buf_pull_up(conn); - iov1.iov_buf = conn->buf + conn->rd_put; - iov1.iov_len = conn->bufsz - conn->rd_put; - conn->rd_buffered = true; + iov1.iov_buf = conn->buf + conn->rd_put; + iov1.iov_len = conn->bufsz - conn->rd_put; + conn->buffered = true; if (iov1.iov_len == 0) { return (NNG_EMSGSIZE); } @@ -297,9 +302,9 @@ http_rd_buf(nni_http_conn *conn, nni_aio *aio) } if (rv == NNG_EAGAIN) { nni_iov iov1; - iov1.iov_buf = conn->buf + conn->rd_put; - iov1.iov_len = conn->bufsz - conn->rd_put; - conn->rd_buffered = true; + iov1.iov_buf = conn->buf + conn->rd_put; + iov1.iov_len = conn->bufsz - conn->rd_put; + conn->buffered = true; nni_aio_set_iov(&conn->rd_aio, 1, &iov1); nng_stream_recv(conn->sock, &conn->rd_aio); } @@ -371,7 +376,7 @@ http_rd_cb(void *arg) cnt = nni_aio_count(aio); // If we were reading into the buffer, then advance location(s). - if (conn->rd_buffered) { + if (conn->buffered) { conn->rd_put += cnt; NNI_ASSERT(conn->rd_put <= conn->bufsz); http_rd_start(conn); @@ -1001,6 +1006,12 @@ nni_http_set_status(nng_http *conn, uint16_t status, const char *reason) conn->rsn = dup; } +bool +nni_http_is_error(nng_http *conn) +{ + return (conn->iserr); +} + static int http_conn_set_error(nng_http *conn, uint16_t status, const char *reason, const char *body, const char *redirect) @@ -1023,7 +1034,7 @@ http_conn_set_error(nng_http *conn, uint16_t status, const char *reason, "
"; const char *suffix = "