diff options
| -rw-r--r-- | src/supplemental/http/http_msg.c | 41 | ||||
| -rw-r--r-- | src/supplemental/http/server.c | 9 | ||||
| -rw-r--r-- | src/supplemental/websocket/websocket.c | 7 | ||||
| -rw-r--r-- | tests/trantest.h | 65 |
4 files changed, 94 insertions, 28 deletions
diff --git a/src/supplemental/http/http_msg.c b/src/supplemental/http/http_msg.c index ff931240..da8c70f3 100644 --- a/src/supplemental/http/http_msg.c +++ b/src/supplemental/http/http_msg.c @@ -1,6 +1,6 @@ // -// Copyright 2017 Staysail Systems, Inc. <info@staysail.tech> -// Copyright 2017 Capitar IT Group BV <info@capitar.com> +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -614,25 +614,25 @@ nni_http_res_init(nni_http_res **resp) const char * nni_http_req_get_method(nni_http_req *req) { - return (req->meth); + return (req->meth != NULL ? req->meth : ""); } const char * nni_http_req_get_uri(nni_http_req *req) { - return (req->uri); + return (req->uri != NULL ? req->uri : ""); } const char * nni_http_req_get_version(nni_http_req *req) { - return (req->vers); + return (req->vers != NULL ? req->vers : ""); } const char * nni_http_res_get_version(nni_http_res *res) { - return (res->vers); + return (res->vers != NULL ? res->vers : ""); } int @@ -678,7 +678,7 @@ nni_http_res_get_status(nni_http_res *res) const char * nni_http_res_get_reason(nni_http_res *res) { - return (res->rsn); + return (res->rsn != NULL ? res->rsn : ""); } static int @@ -864,10 +864,11 @@ nni_http_res_init_error(nni_http_res **resp, uint16_t err) char * rsn; char rsnbuf[80]; char html[1024]; + int rv; nni_http_res *res; - if ((nni_http_res_init(&res)) != 0) { - return (NNG_ENOMEM); + if ((rv = nni_http_res_init(&res)) != 0) { + return (rv); } // Note that it is expected that redirect URIs will update the @@ -957,7 +958,7 @@ nni_http_res_init_error(nni_http_res **resp, uint16_t err) } // very simple builtin error page - snprintf(html, sizeof(html), + (void) snprintf(html, sizeof(html), "<head><title>%d %s</title></head>" "<body><p/><h1 align=\"center\">" "<span style=\"font-size: 36px; border-radius: 5px; " @@ -968,14 +969,14 @@ nni_http_res_init_error(nni_http_res **resp, uint16_t err) "%s</span></p></body>", err, rsn, err, rsn); - nni_http_res_set_status(res, err, rsn); - nni_http_res_copy_data(res, html, strlen(html)); - nni_http_res_set_version(res, "HTTP/1.1"); - nni_http_res_set_header( - res, "Content-Type", "text/html; charset=UTF-8"); - // We could set the date, but we don't necessarily have a portable - // way to get the time of day. - - *resp = res; - return (0); + if (((rv = nni_http_res_set_status(res, err, rsn)) != 0) || + ((rv = nni_http_res_set_version(res, "HTTP/1.1")) != 0) || + ((rv = nni_http_res_set_header( + res, "Content-Type", "text/html; charset=UTF-8")) != 0) || + ((rv = nni_http_res_copy_data(res, html, strlen(html))) != 0)) { + nni_http_res_fini(res); + } else { + *resp = res; + } + return (rv); } diff --git a/src/supplemental/http/server.c b/src/supplemental/http/server.c index 64ed9e11..cce73765 100644 --- a/src/supplemental/http/server.c +++ b/src/supplemental/http/server.c @@ -1,6 +1,6 @@ // -// Copyright 2017 Staysail Systems, Inc. <info@staysail.tech> -// Copyright 2017 Capitar IT Group BV <info@capitar.com> +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -298,6 +298,11 @@ http_sconn_error(http_sconn *sc, uint16_t err) return; } + if (sc->close) { + if (nni_http_res_set_header(res, "Connection", "close") != 0) { + http_sconn_close(sc); + } + } sc->res = res; nni_http_write_res(sc->http, res, sc->txaio); } diff --git a/src/supplemental/websocket/websocket.c b/src/supplemental/websocket/websocket.c index d614c58d..effba8d7 100644 --- a/src/supplemental/websocket/websocket.c +++ b/src/supplemental/websocket/websocket.c @@ -541,9 +541,10 @@ ws_write_cb(void *arg) return; } - frame = ws->txframe; - wm = frame->wmsg; - aio = wm->aio; + frame = ws->txframe; + wm = frame->wmsg; + aio = wm->aio; + ws->txframe = NULL; if ((rv = nni_aio_result(ws->txaio)) != 0) { diff --git a/tests/trantest.h b/tests/trantest.h index 324668dd..0346e3ff 100644 --- a/tests/trantest.h +++ b/tests/trantest.h @@ -1,7 +1,6 @@ // -// Copyright 2017 Garrett D'Amore <garrett@damore.org> -// Copyright 2017 Capitar IT Group BV <info@capitar.com> -// Copyright 2017 Staysail Systems, Inc. <info@staysail.tech> +// Copyright 2018 Capitar IT Group BV <info@capitar.com> +// Copyright 2018 Staysail Systems, Inc. <info@staysail.tech> // // This software is supplied under the terms of the MIT License, a // copy of which should be located in the distribution where this @@ -306,6 +305,63 @@ trantest_send_recv(trantest *tt) } void +trantest_send_recv_multi(trantest *tt) +{ + Convey("Send and recv multi", { + nng_listener l; + nng_dialer d; + nng_msg * send; + nng_msg * recv; + size_t len; + nng_pipe p; + char url[NNG_MAXADDRLEN]; + size_t sz; + int i; + char msgbuf[16]; + + So(trantest_listen(tt, &l) == 0); + So(l != 0); + So(trantest_dial(tt, &d) == 0); + So(d != 0); + + nng_msleep(200); // listener may be behind slightly + + for (i = 0; i < 10; i++) { + snprintf(msgbuf, sizeof(msgbuf), "ping%d", i); + send = NULL; + So(nng_msg_alloc(&send, 0) == 0); + So(send != NULL); + So(nng_msg_append(send, msgbuf, strlen(msgbuf) + 1) == + 0); + + So(nng_sendmsg(tt->reqsock, send, 0) == 0); + recv = NULL; + So(nng_recvmsg(tt->repsock, &recv, 0) == 0); + So(recv != NULL); + So(nng_msg_len(recv) == strlen(msgbuf) + 1); + So(strcmp(nng_msg_body(recv), msgbuf) == 0); + nng_msg_free(recv); + + snprintf(msgbuf, sizeof(msgbuf), "pong%d", i); + So(nng_msg_alloc(&send, 0) == 0); + So(nng_msg_append(send, msgbuf, strlen(msgbuf) + 1) == + 0); + So(nng_sendmsg(tt->repsock, send, 0) == 0); + So(nng_recvmsg(tt->reqsock, &recv, 0) == 0); + So(recv != NULL); + So(nng_msg_len(recv) == strlen(msgbuf) + 1); + So(strcmp(nng_msg_body(recv), msgbuf) == 0); + p = nng_msg_get_pipe(recv); + So(p != 0); + sz = sizeof(url); + So(nng_pipe_getopt(p, NNG_OPT_URL, url, &sz) == 0); + So(strcmp(url, tt->addr) == 0); + nng_msg_free(recv); + } + }); +} + +void trantest_check_properties(trantest *tt, trantest_proptest_t f) { Convey("Properties test", { @@ -409,6 +465,7 @@ trantest_test_all(const char *addr) trantest_listen_accept(&tt); trantest_send_recv(&tt); trantest_send_recv_large(&tt); + trantest_send_recv_multi(&tt); }) } @@ -429,6 +486,7 @@ trantest_test_extended(const char *addr, trantest_proptest_t f) trantest_listen_accept(&tt); trantest_send_recv(&tt); trantest_send_recv_large(&tt); + trantest_send_recv_multi(&tt); trantest_check_properties(&tt, f); }) } @@ -457,6 +515,7 @@ trantest_test(trantest *tt) trantest_send_recv(tt); trantest_send_recv_large(tt); + trantest_send_recv_multi(tt); if (tt->proptest != NULL) { trantest_check_properties(tt, tt->proptest); } |
