Repository: nanomsg/nng (c)

Issue:
http server directory handler
The directory handler created via `nng_http_handler_alloc_directory(nng_http_handler **hp, const char *path, const char *dirname);` cannot accept uri "/" (the `path` parameter), causing 404 error. Is this behaviour by design or a mistake?

Pull request description:
Fix URI test that fails on all non-base URIs
fixes #751 http server directory handler

The test `(uri[len] != '/')` fails on all URIs other than "/". This PR fixes this.


Patch:
diff --git a/src/protocol/reqrep0/rep.c b/src/protocol/reqrep0/rep.c
index 3cc1802a..b546044d 100644
--- a/src/protocol/reqrep0/rep.c
+++ b/src/protocol/reqrep0/rep.c
@@ -491,6 +491,7 @@ rep0_pipe_recv_cb(void *arg)
 	nni_aio *  aio;
 	size_t     len;
 	int        hops;
+	int        ttl;
 
 	if (nni_aio_result(&p->aio_recv) != 0) {
 		nni_pipe_close(p->pipe);
@@ -498,6 +499,7 @@ rep0_pipe_recv_cb(void *arg)
 	}
 
 	msg = nni_aio_get_msg(&p->aio_recv);
+	ttl = nni_atomic_get(&s->ttl);
 
 	nni_msg_set_pipe(msg, p->id);
 
@@ -506,7 +508,7 @@ rep0_pipe_recv_cb(void *arg)
 	for (;;) {
 		bool end;
 
-		if (hops > nni_atomic_get(&s->ttl)) {
+		if (hops > ttl) {
 			// This isn't malformed, but it has gone
 			// through too many hops.  Do not disconnect,
 			// because we can legitimately receive messages
@@ -577,8 +579,8 @@ static int
 rep0_sock_set_max_ttl(void *arg, const void *buf, size_t sz, nni_opt_type t)
 {
 	rep0_sock *s = arg;
-	int ttl;
-	int rv;
+	int        ttl;
+	int        rv;
 
 	if ((rv = nni_copyin_int(&ttl, buf, sz, 1, 255, t)) == 0) {
 		nni_atomic_set(&s->ttl, ttl);
diff --git a/src/protocol/reqrep0/xrep.c b/src/protocol/reqrep0/xrep.c
index e5d96f02..a036e6f6 100644
--- a/src/protocol/reqrep0/xrep.c
+++ b/src/protocol/reqrep0/xrep.c
@@ -283,12 +283,15 @@ xrep0_pipe_recv_cb(void *arg)
 	xrep0_sock *s = p->rep;
 	nni_msg *   msg;
 	int         hops;
+	int         ttl;
 
 	if (nni_aio_result(&p->aio_recv) != 0) {
 		nni_pipe_close(p->pipe);
 		return;
 	}
 
+	ttl = nni_atomic_get(&s->ttl);
+
 	msg = nni_aio_get_msg(&p->aio_recv);
 	nni_aio_set_msg(&p->aio_recv, NULL);
 
@@ -302,7 +305,7 @@ xrep0_pipe_recv_cb(void *arg)
 	for (;;) {
 		bool     end = 0;
 		uint8_t *body;
-		if (hops > (int)nni_atomic_get(&s->ttl)) {
+		if (hops > ttl) {
 			// This isn't malformed, but it has gone through
 			// too many hops.  Do not disconnect, because we
 			// can legitimately receive messages with too many
diff --git a/src/protocol/survey0/CMakeLists.txt b/src/protocol/survey0/CMakeLists.txt
index eeec4ee1..a180a196 100644
--- a/src/protocol/survey0/CMakeLists.txt
+++ b/src/protocol/survey0/CMakeLists.txt
@@ -24,4 +24,5 @@ nng_headers_if(NNG_PROTO_RESPONDENT0 nng/protocol/survey0/respond.h)
 nng_defines_if(NNG_PROTO_RESPONDENT0 NNG_HAVE_RESPONDENT0)
 
 nng_test(respond_test)
+nng_test(xrespond_test)
 nng_test(xsurvey_test)
\ No newline at end of file
diff --git a/src/protocol/survey0/respond.c b/src/protocol/survey0/respond.c
index 201811de..19dedef0 100644
--- a/src/protocol/survey0/respond.c
+++ b/src/protocol/survey0/respond.c
@@ -480,12 +480,14 @@ resp0_pipe_recv_cb(void *arg)
 	nni_aio *   aio;
 	int         hops;
 	size_t      len;
+	int         ttl;
 
 	if (nni_aio_result(&p->aio_recv) != 0) {
 		nni_pipe_close(p->npipe);
 		return;
 	}
 
+	ttl = nni_atomic_get(&s->ttl);
 	msg = nni_aio_get_msg(&p->aio_recv);
 	nni_msg_set_pipe(msg, p->id);
 
@@ -495,7 +497,7 @@ resp0_pipe_recv_cb(void *arg)
 		bool     end = 0;
 		uint8_t *body;
 
-		if (hops > nni_atomic_get(&s->ttl)) {
+		if (hops > ttl) {
 			goto drop;
 		}
 		hops++;
diff --git a/src/protocol/survey0/xrespond.c b/src/protocol/survey0/xrespond.c
index 6318fe8b..73e541d3 100644
--- a/src/protocol/survey0/xrespond.c
+++ b/src/protocol/survey0/xrespond.c
@@ -37,12 +37,12 @@ static void xresp0_pipe_fini(void *);
 
 // resp0_sock is our per-socket protocol private structure.
 struct xresp0_sock {
-	nni_msgq *  urq;
-	nni_msgq *  uwq;
-	int         ttl;
-	nni_idhash *pipes;
-	nni_aio *   aio_getq;
-	nni_mtx     mtx;
+	nni_msgq *     urq;
+	nni_msgq *     uwq;
+	nni_atomic_int ttl;
+	nni_idhash *   pipes;
+	nni_aio *      aio_getq;
+	nni_mtx        mtx;
 };
 
 // resp0_pipe is our per-pipe protocol private structure.
@@ -74,13 +74,15 @@ xresp0_sock_init(void *arg, nni_sock *nsock)
 	int          rv;
 
 	nni_mtx_init(&s->mtx);
+	nni_atomic_init(&s->ttl);
+	nni_atomic_set(&s->ttl, 8); // Per RFC
 	if (((rv = nni_idhash_init(&s->pipes)) != 0) ||
-	    ((rv = nni_aio_alloc(&s->aio_getq, xresp0_sock_getq_cb, s)) != 0)) {
+	    ((rv = nni_aio_alloc(&s->aio_getq, xresp0_sock_getq_cb, s)) !=
+	        0)) {
 		xresp0_sock_fini(s);
 		return (rv);
 	}
 
-	s->ttl = 8; // Per RFC
 	s->urq = nni_sock_recvq(nsock);
 	s->uwq = nni_sock_sendq(nsock);
 
@@ -269,12 +271,14 @@ xresp0_recv_cb(void *arg)
 	nni_msgq *   urq = s->urq;
 	nni_msg *    msg;
 	int          hops;
+	int          ttl;
 
 	if (nni_aio_result(p->aio_recv) != 0) {
 		nni_pipe_close(p->npipe);
 		return;
 	}
 
+	ttl = nni_atomic_get(&s->ttl);
 	msg = nni_aio_get_msg(p->aio_recv);
 	nni_aio_set_msg(p->aio_recv, NULL);
 	nni_msg_set_pipe(msg, p->id);
@@ -290,7 +294,7 @@ xresp0_recv_cb(void *arg)
 		bool     end;
 		uint8_t *body;
 
-		if (hops > s->ttl) {
+		if (hops > ttl) {
 			goto drop;
 		}
 		hops++;
@@ -340,14 +344,19 @@ static int
 xresp0_sock_set_maxttl(void *arg, const void *buf, size_t sz, nni_opt_type t)
 {
 	xresp0_sock *s = arg;
-	return (nni_copyin_int(&s->ttl, buf, sz, 1, 255, t));
+	int ttl;
+	int rv;
+	if ((rv = nni_copyin_int(&ttl, buf, sz, 1, 255, t)) == 0) {
+		nni_atomic_set(&s->ttl, ttl);
+	}
+	return (rv);
 }
 
 static int
 xresp0_sock_get_maxttl(void *arg, void *buf, size_t *szp, nni_opt_type t)
 {
 	xresp0_sock *s = arg;
-	return (nni_copyout_int(s->ttl, buf, szp, t));
+	return (nni_copyout_int(nni_atomic_get(&s->ttl), buf, szp, t));
 }
 
 static void
diff --git a/src/supplemental/http/http_server.c b/src/supplemental/http/http_server.c
index 9a4ff2af..da12c20d 100644
--- a/src/supplemental/http/http_server.c
+++ b/src/supplemental/http/http_server.c
@@ -676,7 +676,7 @@ http_sconn_cbdone(void *arg)
 	// Get the handler.  It may be set regardless of success or
 	// failure.  Clear it, and drop our reference, since we're
 	// done with the handler for now.
-	h   = nni_aio_get_data(aio, 1);
+	h = nni_aio_get_data(aio, 1);
 	nni_aio_set_data(aio, 1, NULL);
 
 	if (h != NULL) {
@@ -1402,8 +1402,9 @@ http_handle_dir(nni_aio *aio)
 	char *            pn;
 
 	len = strlen(base);
-	if ((strncmp(uri, base, len) != 0) ||
-	    ((uri[len] != 0) && (uri[len] != '/'))) {
+	if (base[1] != '\0' && // Allows "/" as base
+	    ((strncmp(uri, base, len) != 0) ||
+	        ((uri[len] != 0) && (uri[len] != '/')))) {
 		// This should never happen!
 		nni_aio_finish_error(aio, NNG_EINVAL);
 		return;
diff --git a/src/transport/inproc/inproc.c b/src/transport/inproc/inproc.c
index 9aef40a5..ffc6f002 100644
--- a/src/transport/inproc/inproc.c
+++ b/src/transport/inproc/inproc.c
@@ -1,5 +1,5 @@
 //
-// Copyright 2019 Staysail Systems, Inc. <info@staysail.tech>
+// Copyright 2020 Staysail Systems, Inc. <info@staysail.tech>
 // Copyright 2018 Capitar IT Group BV <info@capitar.com>
 // Copyright 2018 Devolutions <info@devolutions.net>
 //
@@ -172,7 +172,7 @@ inproc_pipe_send(void *arg, nni_aio *aio)
 		nni_aio_finish(aio, rv, nni_aio_count(aio));
 		return;
 	}
-	nni_msg_header_chop(msg, l);
+	nni_msg_header_clear(msg);
 	nni_msgq_aio_put(pipe->wq, aio);
 }
 


Test patch:
diff --git a/src/protocol/survey0/xrespond_test.c b/src/protocol/survey0/xrespond_test.c
new file mode 100644
index 00000000..7b8196b6
--- /dev/null
+++ b/src/protocol/survey0/xrespond_test.c
@@ -0,0 +1,443 @@
+//
+// Copyright 2020 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
+// file was obtained (LICENSE.txt).  A copy of the license may also be
+// found online at https://opensource.org/licenses/MIT.
+//
+
+#include <string.h>
+
+#include <nng/nng.h>
+#include <nng/protocol/survey0/respond.h>
+#include <nng/protocol/survey0/survey.h>
+
+#include <acutest.h>
+#include <testutil.h>
+
+#ifndef NNI_PROTO
+#define NNI_PROTO(x, y) (((x) << 4u) | (y))
+#endif
+
+static void
+test_xresp_identity(void)
+{
+	nng_socket s;
+	int        p;
+	char *     n;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&s));
+	TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PROTO, &p));
+	TEST_CHECK(p == NNI_PROTO(6u, 3u));
+	TEST_NNG_PASS(nng_getopt_int(s, NNG_OPT_PEER, &p));
+	TEST_CHECK(p == NNI_PROTO(6u, 2u));
+	TEST_NNG_PASS(nng_getopt_string(s, NNG_OPT_PROTONAME, &n));
+	TEST_CHECK(strcmp(n, "respondent") == 0);
+	nng_strfree(n);
+	TEST_CHECK(nng_getopt_string(s, NNG_OPT_PEERNAME, &n) == 0);
+	TEST_CHECK(strcmp(n, "surveyor") == 0);
+	nng_strfree(n);
+	TEST_CHECK(nng_close(s) == 0);
+}
+
+static void
+test_xresp_raw(void)
+{
+	nng_socket s;
+	bool       b;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&s));
+	TEST_NNG_PASS(nng_getopt_bool(s, NNG_OPT_RAW, &b));
+	TEST_CHECK(b);
+	TEST_NNG_PASS(nng_close(s));
+}
+
+static void
+test_xresp_no_context(void)
+{
+	nng_socket s;
+	nng_ctx    ctx;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&s));
+	TEST_NNG_FAIL(nng_ctx_open(&ctx, s), NNG_ENOTSUP);
+	TEST_NNG_PASS(nng_close(s));
+}
+
+static void
+test_xresp_poll_writeable(void)
+{
+	int        fd;
+	nng_socket surv;
+	nng_socket resp;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+	TEST_NNG_PASS(nng_surveyor0_open(&surv));
+	TEST_NNG_PASS(nng_getopt_int(resp, NNG_OPT_SENDFD, &fd));
+	TEST_CHECK(fd >= 0);
+
+	// We are always writeable, even before connect.  This is so that
+	// back-pressure from a bad peer can't trash others.  We assume
+	// that peers won't send us requests faster than they can consume
+	// the answers.  If they do, they will lose their answers.
+	TEST_CHECK(testutil_pollfd(fd) == true);
+
+	TEST_NNG_PASS(testutil_marry(surv, resp));
+
+	// Now it's writable.
+	TEST_CHECK(testutil_pollfd(fd) == true);
+
+	TEST_NNG_PASS(nng_close(surv));
+	TEST_NNG_PASS(nng_close(resp));
+}
+
+static void
+test_xresp_poll_readable(void)
+{
+	int        fd;
+	nng_socket surv;
+	nng_socket resp;
+	nng_msg *  msg;
+
+	TEST_NNG_PASS(nng_surveyor0_open(&surv));
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+	TEST_NNG_PASS(nng_getopt_int(resp, NNG_OPT_RECVFD, &fd));
+	TEST_CHECK(fd >= 0);
+
+	// Not readable if not connected!
+	TEST_CHECK(testutil_pollfd(fd) == false);
+
+	// Even after connect (no message yet)
+	TEST_NNG_PASS(testutil_marry(surv, resp));
+	TEST_CHECK(testutil_pollfd(fd) == false);
+
+	// But once we send messages, it is.
+	// We have to send a request, in order to send a reply.
+	TEST_NNG_SEND_STR(surv, "abc");
+	testutil_sleep(100);
+
+	TEST_CHECK(testutil_pollfd(fd) == true);
+
+	// and receiving makes it no longer ready
+	TEST_NNG_PASS(nng_recvmsg(resp, &msg, 0));
+	nng_msg_free(msg);
+	TEST_CHECK(testutil_pollfd(fd) == false);
+
+	TEST_NNG_PASS(nng_close(surv));
+	TEST_NNG_PASS(nng_close(resp));
+}
+
+static void
+test_xresp_validate_peer(void)
+{
+	nng_socket s1, s2;
+	nng_stat * stats;
+	nng_stat * reject;
+	char       addr[64];
+
+	testutil_scratch_addr("inproc", sizeof(addr), addr);
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&s1));
+	TEST_NNG_PASS(nng_respondent0_open(&s2));
+
+	TEST_NNG_PASS(nng_listen(s1, addr, NULL, 0));
+	TEST_NNG_PASS(nng_dial(s2, addr, NULL, NNG_FLAG_NONBLOCK));
+
+	testutil_sleep(100);
+	TEST_NNG_PASS(nng_stats_get(&stats));
+
+	TEST_CHECK(stats != NULL);
+	TEST_CHECK((reject = nng_stat_find_socket(stats, s1)) != NULL);
+	TEST_CHECK((reject = nng_stat_find(reject, "reject")) != NULL);
+
+	TEST_CHECK(nng_stat_type(reject) == NNG_STAT_COUNTER);
+	TEST_CHECK(nng_stat_value(reject) > 0);
+
+	TEST_NNG_PASS(nng_close(s1));
+	TEST_NNG_PASS(nng_close(s2));
+	nng_stats_free(stats);
+}
+
+static void
+test_xresp_close_pipe_before_send(void)
+{
+	nng_socket resp;
+	nng_socket surv;
+	nng_pipe   p;
+	nng_aio *  aio1;
+	nng_msg *  m;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+	TEST_NNG_PASS(nng_surveyor0_open(&surv));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 1000));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 1000));
+	TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 1000));
+	TEST_NNG_PASS(nng_aio_alloc(&aio1, NULL, NULL));
+
+	TEST_NNG_PASS(testutil_marry(surv, resp));
+	TEST_NNG_SEND_STR(surv, "test");
+
+	nng_recv_aio(resp, aio1);
+	nng_aio_wait(aio1);
+	TEST_NNG_PASS(nng_aio_result(aio1));
+	TEST_CHECK((m = nng_aio_get_msg(aio1)) != NULL);
+	p = nng_msg_get_pipe(m);
+	TEST_NNG_PASS(nng_pipe_close(p));
+	TEST_NNG_PASS(nng_sendmsg(resp, m, 0));
+
+	TEST_NNG_PASS(nng_close(surv));
+	TEST_NNG_PASS(nng_close(resp));
+	nng_aio_free(aio1);
+}
+
+static void
+test_xresp_close_pipe_during_send(void)
+{
+	nng_socket resp;
+	nng_socket surv;
+	nng_pipe   p;
+	nng_msg *  m;
+
+	TEST_NNG_PASS(nng_respondent_open_raw(&resp));
+	TEST_NNG_PASS(nng_surveyor0_open_raw(&surv));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 1000));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 200));
+	TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 1000));
+	TEST_NNG_PASS(nng_setopt_int(resp, NNG_OPT_SENDBUF, 20));
+	TEST_NNG_PASS(nng_setopt_int(resp, NNG_OPT_RECVBUF, 20));
+	TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_SENDBUF, 20));
+	TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_RECVBUF, 1));
+
+	TEST_NNG_PASS(testutil_marry(surv, resp));
+
+	TEST_NNG_PASS(nng_msg_alloc(&m, 4));
+	TEST_NNG_PASS(nng_msg_append_u32(m, (unsigned) 0x81000000u));
+	TEST_NNG_PASS(nng_sendmsg(surv, m, 0));
+	TEST_NNG_PASS(nng_recvmsg(resp, &m, 0));
+	p = nng_msg_get_pipe(m);
+	nng_msg_free(m);
+
+	for (int i = 0; i < 100; i++) {
+		TEST_NNG_PASS(nng_msg_alloc(&m, 4));
+		TEST_NNG_PASS(nng_msg_header_append_u32(m, nng_pipe_id(p)));
+		TEST_NNG_PASS(
+		    nng_msg_header_append_u32(m, (unsigned) i | 0x80000000u));
+		// xsrep does not exert back-pressure
+		TEST_NNG_PASS(nng_sendmsg(resp, m, 0));
+	}
+	TEST_NNG_PASS(nng_pipe_close(p));
+
+	TEST_NNG_PASS(nng_close(surv));
+	TEST_NNG_PASS(nng_close(resp));
+}
+
+static void
+test_xresp_close_during_recv(void)
+{
+	nng_socket resp;
+	nng_socket surv;
+	nng_msg *  m;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+	TEST_NNG_PASS(nng_surveyor0_open_raw(&surv));
+	TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 1000));
+	TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 100));
+	TEST_NNG_PASS(nng_setopt_int(resp, NNG_OPT_RECVBUF, 5));
+	TEST_NNG_PASS(nng_setopt_int(surv, NNG_OPT_SENDBUF, 20));
+
+	TEST_NNG_PASS(testutil_marry(surv, resp));
+
+	for (unsigned i = 0; i < 100; i++) {
+		int rv;
+		TEST_NNG_PASS(nng_msg_alloc(&m, 4));
+		TEST_NNG_PASS(nng_msg_header_append_u32(m, i | 0x80000000u));
+		rv = nng_sendmsg(surv, m, 0);
+		if (rv == NNG_ETIMEDOUT) {
+			nng_msg_free(m);
+			break;
+		}
+	}
+	TEST_NNG_PASS(nng_close(surv));
+	TEST_NNG_PASS(nng_close(resp));
+}
+
+static void
+test_xresp_recv_aio_stopped(void)
+{
+	nng_socket resp;
+	nng_aio *  aio;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+	TEST_NNG_PASS(nng_aio_alloc(&aio, NULL, NULL));
+
+	nng_aio_stop(aio);
+	nng_recv_aio(resp, aio);
+	nng_aio_wait(aio);
+	TEST_NNG_FAIL(nng_aio_result(aio), NNG_ECANCELED);
+	TEST_NNG_PASS(nng_close(resp));
+	nng_aio_free(aio);
+}
+
+static void
+test_xresp_send_no_header(void)
+{
+	nng_socket resp;
+	nng_socket surv;
+	nng_msg *  m;
+
+	TEST_NNG_PASS(nng_surveyor0_open_raw(&surv));
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 100));
+	TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_RECVTIMEO, 100));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 1000));
+	TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 1000));
+
+	TEST_NNG_PASS(testutil_marry(surv, resp));
+
+	TEST_NNG_PASS(nng_msg_alloc(&m, 4));
+	TEST_NNG_PASS(nng_sendmsg(resp, m, 0));
+	TEST_NNG_FAIL(nng_recvmsg(resp, &m, 0), NNG_ETIMEDOUT);
+
+	TEST_NNG_PASS(nng_close(surv));
+	TEST_NNG_PASS(nng_close(resp));
+}
+
+static void
+test_xresp_recv_garbage(void)
+{
+	nng_socket resp;
+	nng_socket surv;
+	nng_msg *  m;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+	TEST_NNG_PASS(nng_surveyor0_open_raw(&surv));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 100));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_SENDTIMEO, 100));
+	TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 1000));
+
+	TEST_NNG_PASS(testutil_marry(surv, resp));
+
+	TEST_NNG_PASS(nng_msg_alloc(&m, 4));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 1u));
+	TEST_NNG_PASS(nng_sendmsg(surv, m, 0));
+	TEST_NNG_FAIL(nng_recvmsg(resp, &m, 0), NNG_ETIMEDOUT);
+
+	TEST_NNG_PASS(nng_close(surv));
+	TEST_NNG_PASS(nng_close(resp));
+}
+
+static void
+test_xresp_ttl_option(void)
+{
+	nng_socket  resp;
+	int         v;
+	bool        b;
+	size_t      sz;
+	const char *opt = NNG_OPT_MAXTTL;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+
+	TEST_NNG_PASS(nng_setopt_int(resp, opt, 1));
+	TEST_NNG_FAIL(nng_setopt_int(resp, opt, 0), NNG_EINVAL);
+	TEST_NNG_FAIL(nng_setopt_int(resp, opt, -1), NNG_EINVAL);
+	TEST_NNG_FAIL(nng_setopt_int(resp, opt, 256), NNG_EINVAL);
+	TEST_NNG_PASS(nng_setopt_int(resp, opt, 3));
+	TEST_NNG_PASS(nng_getopt_int(resp, opt, &v));
+	TEST_CHECK(v == 3);
+	v  = 0;
+	sz = sizeof(v);
+	TEST_NNG_PASS(nng_getopt(resp, opt, &v, &sz));
+	TEST_CHECK(v == 3);
+	TEST_CHECK(sz == sizeof(v));
+
+	TEST_CHECK(nng_setopt(resp, opt, "", 1) == NNG_EINVAL);
+	sz = 1;
+	TEST_CHECK(nng_getopt(resp, opt, &v, &sz) == NNG_EINVAL);
+	TEST_CHECK(nng_setopt_bool(resp, opt, true) == NNG_EBADTYPE);
+	TEST_CHECK(nng_getopt_bool(resp, opt, &b) == NNG_EBADTYPE);
+
+	TEST_CHECK(nng_close(resp) == 0);
+}
+
+static void
+test_xresp_ttl_drop(void)
+{
+	nng_socket resp;
+	nng_socket surv;
+	nng_msg *  m;
+
+	TEST_NNG_PASS(nng_respondent0_open_raw(&resp));
+	TEST_NNG_PASS(nng_surveyor0_open_raw(&surv));
+	TEST_NNG_PASS(nng_setopt_int(resp, NNG_OPT_MAXTTL, 3));
+	TEST_NNG_PASS(nng_setopt_ms(resp, NNG_OPT_RECVTIMEO, 200));
+	TEST_NNG_PASS(nng_setopt_ms(surv, NNG_OPT_SENDTIMEO, 1000));
+
+	TEST_NNG_PASS(testutil_marry(surv, resp));
+
+	// Send messages.  Note that xresp implicitly adds a hop on receive.
+
+	TEST_NNG_PASS(nng_msg_alloc(&m, 0));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 1u)); // 2 hops
+	TEST_NNG_PASS(nng_msg_append_u32(m, 0x80000001u));
+	TEST_NNG_PASS(nng_msg_append(m, "PASS1", 6));
+	TEST_NNG_PASS(nng_sendmsg(surv, m, 0));
+
+	TEST_NNG_PASS(nng_msg_alloc(&m, 0));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 1u)); // 4 hops -- discard!
+	TEST_NNG_PASS(nng_msg_append_u32(m, 2u));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 3u));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 0x80000002u));
+	TEST_NNG_PASS(nng_msg_append(m, "FAIL2", 6));
+	TEST_NNG_PASS(nng_sendmsg(surv, m, 0));
+
+	TEST_NNG_PASS(nng_msg_alloc(&m, 0));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 1u)); // 3 hops - passes
+	TEST_NNG_PASS(nng_msg_append_u32(m, 2u));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 0x80000003u));
+	TEST_NNG_PASS(nng_msg_append(m, "PASS3", 6));
+	TEST_NNG_PASS(nng_sendmsg(surv, m, 0));
+
+	TEST_NNG_PASS(nng_msg_alloc(&m, 0));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 1u)); // 4 hops -- discard!
+	TEST_NNG_PASS(nng_msg_append_u32(m, 2u));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 3u));
+	TEST_NNG_PASS(nng_msg_append_u32(m, 0x80000003u));
+	TEST_NNG_PASS(nng_msg_append(m, "FAIL4", 6));
+	TEST_NNG_PASS(nng_sendmsg(surv, m, 0));
+
+	// So on receive we should see 80000001 and 80000003.
+	TEST_NNG_PASS(nng_recvmsg(resp, &m, 0));
+	TEST_CHECK(nng_msg_header_len(m) == 12);
+	TEST_CHECK(nng_msg_len(m) == 6);
+	TEST_CHECK(strcmp(nng_msg_body(m), "PASS1") == 0);
+	nng_msg_free(m);
+
+	TEST_NNG_PASS(nng_recvmsg(resp, &m, 0));
+	TEST_CHECK(nng_msg_header_len(m) == 16); // 3 hops + ID
+	TEST_CHECK(nng_msg_len(m) == 6);
+	TEST_CHECK(strcmp(nng_msg_body(m), "PASS3") == 0);
+	nng_msg_free(m);
+
+	TEST_NNG_FAIL(nng_recvmsg(resp, &m, 0), NNG_ETIMEDOUT);
+
+	TEST_NNG_PASS(nng_close(surv));
+	TEST_NNG_PASS(nng_close(resp));
+}
+
+TEST_LIST = {
+	{ "xrespond identity", test_xresp_identity },
+	{ "xrespond raw", test_xresp_raw },
+	{ "xrespond no context", test_xresp_no_context },
+	{ "xrespond poll readable", test_xresp_poll_readable },
+	{ "xrespond poll writable", test_xresp_poll_writeable },
+	{ "xrespond validate peer", test_xresp_validate_peer },
+	{ "xrespond close pipe before send", test_xresp_close_pipe_before_send },
+	{ "xrespond close pipe during send", test_xresp_close_pipe_during_send },
+	{ "xrespond close during recv", test_xresp_close_during_recv },
+	{ "xrespond recv aio stopped", test_xresp_recv_aio_stopped },
+	{ "xrespond send no header", test_xresp_send_no_header },
+	{ "xrespond recv garbage", test_xresp_recv_garbage },
+	{ "xrespond ttl option", test_xresp_ttl_option },
+	{ "xrespond ttl drop", test_xresp_ttl_drop },
+	{ NULL, NULL },
+};
diff --git a/src/protocol/survey0/xsurvey_test.c b/src/protocol/survey0/xsurvey_test.c
index 0d436dfa..2c17525a 100644
--- a/src/protocol/survey0/xsurvey_test.c
+++ b/src/protocol/survey0/xsurvey_test.c
@@ -10,8 +10,6 @@
 #include <string.h>
 
 #include <nng/nng.h>
-#include <nng/protocol/reqrep0/rep.h>
-#include <nng/protocol/reqrep0/req.h>
 #include <nng/protocol/survey0/respond.h>
 #include <nng/protocol/survey0/survey.h>
 
diff --git a/tests/httpserver.c b/tests/httpserver.c
index a69889a5..9b7ed0e3 100644
--- a/tests/httpserver.c
+++ b/tests/httpserver.c
@@ -289,6 +289,209 @@ TestMain("HTTP Server", {
 			});
 		});
 	});
+
+	Convey("Directory serving works (root)", {
+		char     urlstr[32];
+		nng_url *url;
+		char *   tmpdir;
+		char *   workdir;
+		char *   file1;
+		char *   file2;
+		char *   file3;
+		char *   subdir1;
+		char *   subdir2;
+
+		trantest_next_address(urlstr, "http://127.0.0.1:%u");
+		So(nng_url_parse(&url, urlstr) == 0);
+		So(nng_http_server_hold(&s, url) == 0);
+		So((tmpdir = nni_plat_temp_dir()) != NULL);
+		So((workdir = nni_file_join(tmpdir, "httptest")) != NULL);
+		So((subdir1 = nni_file_join(workdir, "subdir1")) != NULL);
+		So((subdir2 = nni_file_join(workdir, "subdir2")) != NULL);
+		So((file1 = nni_file_join(subdir1, "index.html")) != NULL);
+		So((file2 = nni_file_join(workdir, "file.txt")) != NULL);
+		So((file3 = nni_file_join(subdir2, "index.htm")) != NULL);
+
+		So(nni_file_put(file1, doc1, strlen(doc1)) == 0);
+		So(nni_file_put(file2, doc2, strlen(doc2)) == 0);
+		So(nni_file_put(file3, doc3, strlen(doc3)) == 0);
+
+		Reset({
+			nng_http_server_release(s);
+			free(tmpdir);
+			nni_file_delete(file1);
+			nni_file_delete(file2);
+			nni_file_delete(file3);
+			nni_file_delete(subdir1);
+			nni_file_delete(subdir2);
+			nni_file_delete(workdir);
+			free(workdir);
+			free(file1);
+			free(file2);
+			free(file3);
+			free(subdir1);
+			free(subdir2);
+			nng_url_free(url);
+		});
+
+		So(nng_http_handler_alloc_directory(&h, "/", workdir) == 0);
+		So(nng_http_server_add_handler(s, h) == 0);
+		So(nng_http_server_start(s) == 0);
+		nng_msleep(100);
+
+		Convey("Index.html works", {
+			char     fullurl[256];
+			void *   data;
+			size_t   size;
+			uint16_t stat;
+			char *   ctype;
+
+			snprintf(fullurl, sizeof(fullurl),
+			    "%s/subdir1/index.html", urlstr);
+			So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
+			So(stat == NNG_HTTP_STATUS_OK);
+			So(size == strlen(doc1));
+			So(memcmp(data, doc1, size) == 0);
+			So(strcmp(ctype, "text/html") == 0);
+			nng_strfree(ctype);
+			nng_free(data, size);
+		});
+
+		Convey("Index.htm works", {
+			char     fullurl[256];
+			void *   data;
+			size_t   size;
+			uint16_t stat;
+			char *   ctype;
+
+			snprintf(
+			    fullurl, sizeof(fullurl), "%s/subdir2", urlstr);
+			So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
+			So(stat == NNG_HTTP_STATUS_OK);
+			So(size == strlen(doc3));
+			So(memcmp(data, doc3, size) == 0);
+			So(strcmp(ctype, "text/html") == 0);
+			nni_strfree(ctype);
+			nng_free(data, size);
+		});
+
+		Convey("Named file works", {
+			char     fullurl[256];
+			void *   data;
+			size_t   size;
+			uint16_t stat;
+			char *   ctype;
+
+			snprintf(
+			    fullurl, sizeof(fullurl), "%s/file.txt", urlstr);
+			So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
+			So(stat == NNG_HTTP_STATUS_OK);
+			So(size == strlen(doc2));
+			So(memcmp(data, doc2, size) == 0);
+			So(strcmp(ctype, "text/plain") == 0);
+			nni_strfree(ctype);
+			nng_free(data, size);
+		});
+
+		Convey("Missing index gives 404", {
+			char     fullurl[256];
+			void *   data;
+			size_t   size;
+			uint16_t stat;
+			char *   ctype;
+
+			snprintf(fullurl, sizeof(fullurl), "%s/", urlstr);
+			So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
+			So(stat == NNG_HTTP_STATUS_NOT_FOUND);
+			nng_strfree(ctype);
+			nng_free(data, size);
+		});
+
+		Convey("Custom error page works", {
+			char     fullurl[256];
+			void *   data;
+			size_t   size;
+			uint16_t stat;
+			char *   ctype;
+
+			So(nng_http_server_set_error_page(s, 404, doc4) == 0);
+			snprintf(fullurl, sizeof(fullurl), "%s/", urlstr);
+			So(httpget(fullurl, &data, &size, &stat, &ctype) == 0);
+			So(stat == NNG_HTTP_STATUS_NOT_FOUND);
+			So(size == strlen(doc4));
+			So(memcmp(data, doc4, size) == 0);
+			nng_strfree(ctype);
+			nng_free(data, size);
+		});
+
+		Convey("Bad method gives 405", {
+			char          fullurl[256];
+			void *        data;
+			size_t        size;
+			nng_http_req *req;
+			nng_http_res *res;
+			nng_url *     curl;
+
+			So(nng_http_res_alloc(&res) == 0);
+			snprintf(fullurl, sizeof(fullurl), "%s/", urlstr);
+			So(nng_url_parse(&curl, fullurl) == 0);
+			So(nng_http_req_alloc(&req, curl) == 0);
+			So(nng_http_req_set_method(req, "POST") == 0);
+
+			So(httpdo(curl, req, res, &data, &size) == 0);
+			So(nng_http_res_get_status(res) ==
+			    NNG_HTTP_STATUS_METHOD_NOT_ALLOWED);
+			nng_http_req_free(req);
+			nng_http_res_free(res);
+			nng_url_free(curl);
+			nng_free(data, size);
+		});
+		Convey("Version 0.9 gives 505", {
+			char          fullurl[256];
+			void *        data;
+			size_t        size;
+			nng_http_req *req;
+			nng_http_res *res;
+			nng_url *     curl;
+
+			So(nng_http_res_alloc(&res) == 0);
+			snprintf(fullurl, sizeof(fullurl), "%s/", urlstr);
+			So(nng_url_parse(&curl, fullurl) == 0);
+			So(nng_http_req_alloc(&req, curl) == 0);
+			So(nng_http_req_set_version(req, "HTTP/0.9") == 0);
+
+			So(httpdo(curl, req, res, &data, &size) == 0);
+			So(nng_http_res_get_status(res) ==
+			    NNG_HTTP_STATUS_HTTP_VERSION_NOT_SUPP);
+			nng_http_req_free(req);
+			nng_http_res_free(res);
+			nng_url_free(curl);
+			nng_free(data, size);
+		});
+		Convey("Missing Host gives 400", {
+			char          fullurl[256];
+			void *        data;
+			size_t        size;
+			nng_http_req *req;
+			nng_http_res *res;
+			nng_url *     curl;
+
+			So(nng_http_res_alloc(&res) == 0);
+			snprintf(fullurl, sizeof(fullurl), "%s/", urlstr);
+			So(nng_url_parse(&curl, fullurl) == 0);
+			So(nng_http_req_alloc(&req, curl) == 0);
+			So(nng_http_req_del_header(req, "Host") == 0);
+
+			So(httpdo(curl, req, res, &data, &size) == 0);
+			So(nng_http_res_get_status(res) ==
+			    NNG_HTTP_STATUS_BAD_REQUEST);
+			nng_http_req_free(req);
+			nng_http_res_free(res);
+			nng_url_free(curl);
+			nng_free(data, size);
+		});
+	});
+
 	Convey("Directory serving works", {
 		char     urlstr[32];
 		nng_url *url;