chiark / gitweb /
wip
[hippotat.git] / client
diff --git a/client b/client
index 931cb1f4c5fdcdd983407a2d9d7835d4e7bf6baf..856c6742b6827b00c362359e3fd0855361a152c5 100755 (executable)
--- a/client
+++ b/client
@@ -5,6 +5,8 @@ from hippotat import *
 import twisted.web
 import twisted.web.client
 
+import io
+
 client_cs = None
 
 def set_client(ci,cs,pw):
@@ -48,7 +50,7 @@ outstanding = 0
 def start_client():
   global queue
   global agent
-  queue = PacketQueue(c.max_queue_time)
+  queue = PacketQueue('up', c.max_queue_time)
   agent = twisted.web.client.Agent(reactor, connectTimeout = c.http_timeout)
 
 def outbound(packet, saddr, daddr):
@@ -102,23 +104,39 @@ def check_outbound():
                   c.max_batch_up)
     
     crlf = b'\r\n'
-    mime = (b'--b'                                      + crlf +
-            b'Content-Disposition: form-data; name="m"' + crlf +
-            password                                    + crlf +
-            str(c.client)             .encode('ascii')  + crlf +
-            str(c.target_outstanding) .encode('ascii')  + crlf +
-            b'--b'                                      + crlf +
-            b'Content-Disposition: form-data; name="d"' + crlf +
-            mime_translate(d)                           + crlf +
-            b'--b--'                                    + crlf)
-
-    print('REQUESTING ', mime, file=sys.stderr)
+    lf   =   b'\n'
+    mime = (b'--b'                                        + crlf +
+            b'Content-Type: text/plain; charset="utf-8"'  + crlf +
+            b'Content-Disposition: form-data; name="m"'   + crlf + crlf +
+            str(c.client)             .encode('ascii')    + crlf +
+            password                                      + crlf +
+            str(c.target_outstanding) .encode('ascii')    + crlf +
+          ((
+            b'--b'                                        + crlf +
+            b'Content-Type: application/octet-stream'     + crlf +
+            b'Content-Disposition: form-data; name="d"'   + crlf + crlf +
+            mime_translate(d)                             + crlf
+           ) if len(d) else b'')                               +
+            b'--b--'                                      + crlf)
+
+    #df = open('data.dump.dbg', mode='wb')
+    #df.write(mime)
+    #df.close()
+    # POST -use -c 'multipart/form-data; boundary="b"' http://localhost:8099/ <data.dump.dbg
+
+    log_debug(DBG.HTTP_FULL, 'requesting: ' + str(mime))
 
     hh = { 'User-Agent': ['hippotat'],
-           'Content-Type': ['multipart/form-data; boundary="b"'] }
+           'Content-Type': ['multipart/form-data; boundary="b"'],
+           'Content-Length': [str(len(mime))] }
+
+    bytesreader = io.BytesIO(mime)
+    producer = twisted.web.client.FileBodyProducer(bytesreader)
+
     req = agent.request(b'POST',
                         c.url,
-                        twisted.web.client.Headers(hh))
+                        twisted.web.client.Headers(hh),
+                        producer)
     req.addTimeout(c.http_timeout, reactor)
     req.addCallbacks(req_ok, req_err)
     outstanding += 1