chiark / gitweb /
wip
authorIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 1 Apr 2017 20:33:52 +0000 (21:33 +0100)
committerIan Jackson <ijackson@chiark.greenend.org.uk>
Sat, 1 Apr 2017 20:33:52 +0000 (21:33 +0100)
client

diff --git a/client b/client
index ec28fd0..cda0ac8 100755 (executable)
--- a/client
+++ b/client
@@ -88,25 +88,44 @@ class ResponseConsumer(twisted.internet.protocol.Protocol):
 
   def _asyncfailure(self, reason):
     self._log(DBG.HTTP_CTRL, '_asyncFailure ' + str(reason))
-    global outstanding
-    outstanding += 1
     req_err(self._req, reason)
 
+class ErrorResponseConsumer(twisted.internet.protocol.Protocol):
+  def __init__(self, req, resp):
+    self._req = req
+    self._resp = resp
+
+    try:
+      self._phrase = resp.phrase.decode('utf-8')
+    except Exception:
+      self._phrase = repr(resp.phrase)
+
+    self._log(DBG.HTTP_CTRL, '__init__ %d %s' % (resp.code, self._phrase))
+
+  def _log(self, dflag, msg, **kwargs):
+    log_debug(dflag,'ERROR-RC '+msg, idof=self._req, **kwargs)
+
+  def connectionMade(self): self._m = ''
+  def dataReceived(self, data): self._m += data
+  def connectionLost(self, reason):
+    try:
+      mbody = self._m.decode('utf-8')
+    except Exception:
+      mbody = repr(self._m)
+    req_err(req,
+            "FAILED %d %s | %s | %s"
+            % (self._resp.code, self.phrase, mbody, str(reason)))
+
 def req_ok(req, resp):
   log_debug(DBG.HTTP_CTRL,
             'req_ok %d %s %s' % (resp.code, repr(resp.phrase), str(resp)),
             idof=req)
-  if resp.code != 200:
-    try:
-      phrase = resp.phrase.decode('utf-8')
-    except UnicodeDecodeError:
-      phrase = repr(resp.phrase)
-    req_err(req, "FAILED %d %s" % (resp.code, phrase))
-    return
+  if resp.code == 200:
+    rc = ResponseConsumer(req)
+  else:
+    rc = ErrorResponseConsumer(req, resp)
 
-  rc = ResponseConsumer(req)
   resp.deliverBody(rc)
-  req_fin(req)
 
 def req_err(req, err):
   log_debug(DBG.HTTP_CTRL, 'req_err ' + str(err), idof=req)
@@ -114,9 +133,9 @@ def req_err(req, err):
   reactor.callLater(c.http_retry, (lambda: req_fin(req)))
 
 def req_fin(req):
-  log_debug(DBG.HTTP_CTRL, 'req_fin', idof=req)
   global outstanding
   outstanding -= 1
+  log_debug(DBG.HTTP_CTRL, 'req_fin OS=%d' % outstanding, idof=req)
   check_outbound()
 
 def check_outbound():
@@ -169,11 +188,11 @@ def check_outbound():
                         twisted.web.client.Headers(hh),
                         producer)
 
-    log_debug(DBG.HTTP_CTRL, 'request', idof=req, d=d)
+    outstanding += 1
+    log_debug(DBG.HTTP_CTRL, 'request OS=%d' % outstanding, idof=req, d=d)
     req.addTimeout(c.http_timeout, reactor)
     req.addCallback((lambda resp: req_ok(req, resp)))
     req.addErrback((lambda err: req_err(req, err)))
-    outstanding += 1
 
 common_startup()
 process_cfg()