chiark / gitweb /
init debug
[hippotat.git] / server
diff --git a/server b/server
index 0d75e0b..3781d56 100755 (executable)
--- a/server
+++ b/server
@@ -12,8 +12,6 @@ from twisted.web.server import NOT_DONE_YET
 
 import syslog
 
-import traceback
-
 clients = { }
 
 #---------- "router" ----------
@@ -49,19 +47,18 @@ class Client():
     # plus from config:
     #  .max_batch_down
     #  .max_queue_time
-    #  .max_request_time
     #  .target_requests_outstanding
 
     if ip not in c.network:
       raise ValueError('client %s not in network' % ip)
 
-    for k in ('max_batch_down','max_queue_time','max_request_time',
+    for k in ('max_batch_down','max_queue_time','http_timeout',
               'target_requests_outstanding'):
       req = cfg.getint(cs, k)
       limit = cfg.getint('limits',k)
       self.__dict__[k] = min(req, limit)
 
-    self._pq = PacketQueue(ip, self.max_queue_time)
+    self._pq = PacketQueue(str(ip), self.max_queue_time)
 
     if ip in clients:
       raise ValueError('multiple client cfg sections for %s' % ip)
@@ -73,7 +70,8 @@ class Client():
     log_debug(dflag, ('client %s: ' % self._ip)+msg, **kwargs)
 
   def process_arriving_data(self, d):
-    self._log(DBG.FLOW, 'req data', d=d)
+    self._log(DBG.FLOW, "req data (enc'd)", d=d)
+    if not len(d): return
     for packet in slip.decode(d):
       (saddr, daddr) = packet_addrs(packet)
       if saddr != self._ip:
@@ -81,11 +79,11 @@ class Client():
       route(packet, self._ip, saddr, daddr)
 
   def _req_cancel(self, request):
-    self._log(DBG.HTTPCTRL, 'cancel', idof=request)
+    self._log(DBG.HTTP_CTRL, 'cancel', idof=request)
     request.finish()
 
   def _req_error(self, err, request):
-    self._log(DBG.HTTPCTRL, 'error %s' % err, idof=request)
+    self._log(DBG.HTTP_CTRL, 'error %s' % err, idof=request)
     self._req_cancel(request)
 
   def queue_outbound(self, packet):
@@ -94,13 +92,17 @@ class Client():
 
   def new_request(self, request):
     request.setHeader('Content-Type','application/octet-stream')
-    reactor.callLater(self.max_request_time, self._req_cancel, request)
+    reactor.callLater(self.http_timeout, self._req_cancel, request)
     request.notifyFinish().addErrback(self._req_error, request)
     self._rq.append(request)
     self._check_outbound()
 
+  def _req_write(self, req, d):
+    self._log(DBG.HTTP, 'req_write ', idof=req, d=d)
+    req.write(d)
+
   def _check_outbound(self):
-    log_debug(DBG.HTTPCTRL, 'CHKO')
+    log_debug(DBG.HTTP_CTRL, 'CHKO')
     while True:
       try: request = self._rq[0]
       except IndexError: request = None
@@ -122,7 +124,7 @@ class Client():
       self._log(DBG.HTTP_CTRL, 'CHKO processing', idof=request)
       # request, and also some non-expired packets
       self._pq.process((lambda: request.sentLength),
-                       request.write,
+                       (lambda d: self._req_write(request, d)),
                        self.max_batch_down)
 
       assert(request.sentLength)
@@ -138,10 +140,13 @@ class Client():
 
 def process_request(request, desca):
   # find client, update config, etc.
-  metadata = request.args['m']
+  metadata = request.args[b'm'][0]
   metadata = metadata.split(b'\r\n')
-  (ci_s, pw, tro) = metadata[0:3]
-  desca['m[0,2]'] = [ci_s, tro]
+  (ci_s, pw, tro, cto) = metadata[0:4]
+  desca['m[0,2:3]'] = [ci_s, tro, cto]
+  ci_s = ci_s.decode('utf-8')
+  tro = int(tro); desca['tro']= tro
+  cto = int(cto); desca['cto']= cto
   ci = ipaddr(ci_s)
   desca['ci'] = ci
   cl = clients[ci]
@@ -151,21 +156,31 @@ def process_request(request, desca):
   if tro != cl.target_requests_outstanding:
     raise ValueError('tro must be %d' % cl.target_requests_outstanding)
 
+  if cto < cl.http_timeout:
+    raise ValueError('cto must be >= %d' % cl.http_timeout)
+
   try:
-    d = request.args['d']
+    d = request.args[b'd'][0]
     desca['d'] = d
-  except KeyError: d = ''
+    desca['dlen'] = len(d)
+  except KeyError:
+    d = b''
+    desca['dlen'] = None
+
+  log_http(desca, 'processing', idof=id(request), d=d)
+
+  d = mime_translate(d)
 
   cl.process_arriving_data(d)
   cl.new_request(request)
 
-def log_http(desca, msg):
+def log_http(desca, msg, **kwargs):
   try:
-    d = desca['d']
+    kwargs['d'] = desca['d']
     del desca['d']
   except KeyError:
-    d = None
-  log_debug(DBG.HTTP, msg + repr(desca), d=d)
+    pass
+  log_debug(DBG.HTTP, msg + repr(desca), **kwargs)
 
 class IphttpResource(twisted.web.resource.Resource):
   isLeaf = True
@@ -177,11 +192,11 @@ class IphttpResource(twisted.web.resource.Resource):
     try: process_request(request, desca)
     except Exception as e:
       emsg = traceback.format_exc()
-      log_http(desca, 'EXCEPTION ' + emsg)
+      log_http(desca, 'RETURNING EXCEPTION ' + emsg)
       request.setHeader('Content-Type','text/plain; charset="utf-8"')
       request.setResponseCode(400)
-      return emsg.encode('utf-8')
-    log_http(desca, '... [%s]' % id(request))
+      return (emsg + ' # ' + repr(desca) + '\r\n').encode('utf-8')
+    log_debug(DBG.HTTP_CTRL, '...', idof=id(request))
     return NOT_DONE_YET
 
   def render_GET(self, request):
@@ -221,6 +236,6 @@ def process_cfg():
 
 common_startup()
 process_cfg()
-start_ipif(c.ipif_command, route)
+start_ipif(c.ipif_command, (lambda p,s,d: route(p,"[ipif]",s,d)))
 start_http()
 common_run()