]> arthur.barton.de Git - bup.git/commitdiff
web: fix broken GETs and test
authorRob Browning <rlb@defaultvalue.org>
Sat, 12 Mar 2016 17:45:08 +0000 (11:45 -0600)
committerRob Browning <rlb@defaultvalue.org>
Sat, 12 Mar 2016 17:52:47 +0000 (11:52 -0600)
It appears that content GETs, i.e. "curl
http://localhost/foo/latest/bar" may have been broken for a while.  To
addresss that, switch to the apparently preferred upstream coroutine
approach, and add some initial tests.

Signed-off-by: Rob Browning <rlb@defaultvalue.org>
Tested-by: Rob Browning <rlb@defaultvalue.org>
Makefile
cmd/web-cmd.py
t/test-web.sh [new file with mode: 0755]

index 79fbcb36e9e04e41efc03f36e06c5aea67653ac5..1fdac2611147205817c20295f3e7a6c7edd7aa28 100644 (file)
--- a/Makefile
+++ b/Makefile
@@ -145,6 +145,7 @@ runtests-python: all t/tmp
            | tee -a t/tmp/test-log/$$$$.log
 
 cmdline_tests := \
+  t/test-web.sh \
   t/test-rm.sh \
   t/test-gc.sh \
   t/test-main.sh \
index f88bbc7115fc6109f16fa2d1c3ba7037852eb110..47817f0e3cdf82182630303e59034cef071a2f30 100755 (executable)
@@ -13,6 +13,7 @@ from bup.helpers import (chunkyreader, debug1, handle_ctrl_c, log,
                          resource_path, saved_errors)
 
 try:
+    from tornado import gen
     from tornado.httpserver import HTTPServer
     from tornado.ioloop import IOLoop
     from tornado.netutil import bind_unix_socket
@@ -89,7 +90,6 @@ class BupRequestHandler(tornado.web.RequestHandler):
     def head(self, path):
         return self._process_request(path)
     
-    @tornado.web.asynchronous
     def _process_request(self, path):
         path = urllib.unquote(path)
         print 'Handling request for %s' % path
@@ -127,6 +127,7 @@ class BupRequestHandler(tornado.web.RequestHandler):
             hidden_shown=show_hidden,
             dir_contents=_compute_dir_contents(n, path, show_hidden))
 
+    @gen.coroutine
     def _get_file(self, path, n):
         """Process a request on a file.
 
@@ -134,30 +135,21 @@ class BupRequestHandler(tornado.web.RequestHandler):
         In either case, the headers are sent.
         """
         ctype = self._guess_type(path)
-
         self.set_header("Last-Modified", self.date_time_string(n.mtime))
         self.set_header("Content-Type", ctype)
         size = n.size()
         self.set_header("Content-Length", str(size))
         assert(len(n.hash) == 20)
         self.set_header("Etag", n.hash.encode('hex'))
-
         if self.request.method != 'HEAD':
-            self.flush()
             f = n.open()
-            it = chunkyreader(f)
-            def write_more(me):
-                try:
-                    blob = it.next()
-                except StopIteration:
-                    f.close()
-                    self.finish()
-                    return
-                self.request.connection.stream.write(blob,
-                                                     callback=lambda: me(me))
-            write_more(write_more)
-        else:
-            self.finish()
+            try:
+                it = chunkyreader(f)
+                for blob in chunkyreader(f):
+                    self.write(blob)
+            finally:
+                f.close()
+        raise gen.Return()
 
     def _guess_type(self, path):
         """Guess the type of a file.
diff --git a/t/test-web.sh b/t/test-web.sh
new file mode 100755 (executable)
index 0000000..842b6e9
--- /dev/null
@@ -0,0 +1,64 @@
+#!/usr/bin/env bash
+. wvtest-bup.sh || exit $?
+. t/lib.sh || exit $?
+
+set -o pipefail
+
+TOP="$(WVPASS pwd)" || exit $?
+tmpdir="$(WVPASS wvmktempdir)" || exit $?
+export BUP_DIR="$tmpdir/bup"
+
+bup()
+{
+    "$TOP/bup" "$@"
+}
+
+wait-for-server-start()
+{
+    curl --unix-socket ./socket http://localhost/
+    curl_status=$?
+    while test $curl_status -eq 7; do
+        sleep 0.2
+        curl --unix-socket ./socket http://localhost/
+        curl_status=$?
+    done
+    WVPASSEQ $curl_status 0
+}
+
+WVPASS cd "$tmpdir"
+
+# FIXME: add WVSKIP
+run_test=true
+
+if test -z "$(type -p curl)"; then
+    WVSTART 'curl does not appear to be installed; skipping  test'
+    run_test=''
+fi
+    
+WVPASS bup-python -c "import socket as s; s.socket(s.AF_UNIX).bind('socket')"
+curl --unix-socket ./socket http://localhost/foo
+if test $? -ne 7; then
+    WVSTART 'curl does not appear to support --unix-socket; skipping test'
+    run_test=''
+fi
+    
+if test -n "$run_test"; then
+    WVSTART 'web'
+    WVPASS bup init
+    WVPASS mkdir src
+    WVPASS echo excitement > src/data
+    WVPASS bup index src
+    WVPASS bup save -n src --strip src
+
+    "$TOP/bup" web unix://socket &
+    web_pid=$!
+    wait-for-server-start
+
+    WVPASS curl --unix-socket ./socket http://localhost/src/latest/data > result
+
+    WVPASSEQ excitement "$(cat result)"
+    WVPASS kill -s TERM "$web_pid"
+    WVPASS wait "$web_pid"
+fi
+
+WVPASS rm -r "$tmpdir"