6 import twisted
.web
.client
12 def set_client(ci
,cs
,pw
):
15 assert(client_cs
is None)
18 c
.max_outstanding
= cfg
.getint(cs
, 'max_requests_outstanding')
19 c
.target_outstanding
= cfg
.getint(cs
, 'target_requests_outstanding')
24 global max_requests_outstanding
26 process_cfg_common_always()
30 c
.url
= cfg
.get('server','url')
33 c
.url
= c
.saddrs
[0].url()
35 process_cfg_clients(set_client
)
37 c
.routes
= cfg
.get('virtual','routes')
38 c
.max_queue_time
= cfg
.getint(client_cs
, 'max_queue_time')
39 c
.max_batch_up
= cfg
.getint(client_cs
, 'max_batch_up')
40 c
.http_timeout
= cfg
.getint(client_cs
, 'http_timeout')
41 c
.http_retry
= cfg
.getint(client_cs
, 'http_retry')
43 process_cfg_ipif(client_cs
,
53 queue
= PacketQueue('up', c
.max_queue_time
)
54 agent
= twisted
.web
.client
.Agent(reactor
, connectTimeout
= c
.http_timeout
)
56 def outbound(packet
, saddr
, daddr
):
57 #print('OUT ', saddr, daddr, repr(packet))
61 class ResponseConsumer(twisted
.internet
.protocol
.Protocol
):
62 def __init__(self
, req
):
64 self
._ssd
= SlipStreamDecoder(queue_inbound
)
65 self
._log(DBG
.HTTP_CTRL
, '__init__')
67 def _log(self
, dflag
, msg
, **kwargs
):
68 log_debug(dflag
, 'RC ' + msg
, idof
=self
._req
, **kwargs
)
70 def dataReceived(self
, data
):
71 self
._log(DBG
.HTTP_CTRL
, 'dataReceived', d
=data
)
73 self
._ssd
.inputdata(mime_translate(data
))
74 except Exception as e
:
77 def connectionMade(self
):
78 self
._log(DBG
.HTTP_CTRL
, 'connectionMade')
80 def connectionLost(self
, reason
):
81 self
._log(DBG
.HTTP_CTRL
, 'connectionLost ' + str(reason
))
82 if not reason
.check(twisted
.web
.client
.ResponseDone
):
83 self
._asyncfailure(reason
)
87 except Exception as e
:
90 def _asyncfailure(self
, reason
):
91 self
._log(DBG
.HTTP_CTRL
, '_asyncFailure ' + str(reason
))
92 req_err(self
._req
, reason
)
94 class ErrorResponseConsumer(twisted
.internet
.protocol
.Protocol
):
95 def __init__(self
, req
, resp
):
100 self
._phrase
= resp
.phrase
.decode('utf-8')
102 self
._phrase
= repr(resp
.phrase
)
104 self
._log(DBG
.HTTP_CTRL
, '__init__ %d %s' %
(resp
.code
, self
._phrase
))
106 def _log(self
, dflag
, msg
, **kwargs
):
107 log_debug(dflag
,'ERROR-RC '+msg
, idof
=self
._req
, **kwargs
)
109 def connectionMade(self
):
112 def dataReceived(self
, data
):
113 self
._log(DBG
.HTTP_CTRL
, 'dataReceived ' + repr(data
))
116 def connectionLost(self
, reason
):
118 mbody
= self
._m
.decode('utf-8')
120 mbody
= repr(self
._m
)
121 if not reason
.check(twisted
.web
.client
.ResponseDone
):
122 mbody
+= ' || ' + str(reason
)
125 %
(self
._resp
.code
, self
._phrase
, mbody
))
127 def req_ok(req
, resp
):
128 log_debug(DBG
.HTTP_CTRL
,
129 'req_ok %d %s %s' %
(resp
.code
, repr(resp
.phrase
), str(resp
)),
132 rc
= ResponseConsumer(req
)
134 rc
= ErrorResponseConsumer(req
, resp
)
138 def req_err(req
, err
):
139 log_debug(DBG
.HTTP_CTRL
, 'req_err ' + str(err
), idof
=req
)
140 if isinstance(err
, twisted
.python
.failure
.Failure
):
141 err
= err
.getTraceback()
142 print(err
, file=sys
.stderr
)
143 reactor
.callLater(c
.http_retry
, (lambda: req_fin(req
)))
148 log_debug(DBG
.HTTP_CTRL
, 'req_fin OS=%d' % outstanding
, idof
=req
)
151 def check_outbound():
155 if outstanding
>= c
.max_outstanding
: break
156 if not queue
.nonempty() and outstanding
>= c
.target_outstanding
: break
159 def moredata(s
): nonlocal d
; d
+= s
160 queue
.process((lambda: len(d
)),
164 d
= mime_translate(d
)
168 mime
= (b
'--b' + crlf
+
169 b
'Content-Type: text/plain; charset="utf-8"' + crlf
+
170 b
'Content-Disposition: form-data; name="m"' + crlf
+ crlf
+
171 str(c
.client
) .encode('ascii') + crlf
+
173 str(c
.target_outstanding
) .encode('ascii') + crlf
+
176 b
'Content-Type: application/octet-stream' + crlf
+
177 b
'Content-Disposition: form-data; name="d"' + crlf
+ crlf
+
179 ) if len(d
) else b
'') +
182 #df = open('data.dump.dbg', mode='wb')
185 # POST -use -c 'multipart/form-data; boundary="b"' http://localhost:8099/ <data.dump.dbg
187 log_debug(DBG
.HTTP_FULL
, 'requesting: ' + str(mime
))
189 hh
= { 'User-Agent': ['hippotat'],
190 'Content-Type': ['multipart/form-data; boundary="b"'],
191 'Content-Length': [str(len(mime
))] }
193 bytesreader
= io
.BytesIO(mime
)
194 producer
= twisted
.web
.client
.FileBodyProducer(bytesreader
)
196 req
= agent
.request(b
'POST',
198 twisted
.web
.client
.Headers(hh
),
202 log_debug(DBG
.HTTP_CTRL
, 'request OS=%d' % outstanding
, idof
=req
, d
=d
)
203 req
.addTimeout(c
.http_timeout
, reactor
)
204 req
.addCallback((lambda resp
: req_ok(req
, resp
)))
205 req
.addErrback((lambda err
: req_err(req
, err
)))
210 start_ipif(c
.ipif_command
, outbound
)