Add expect 100 feature to the web client

parent 8b1d0a8e
......@@ -74,10 +74,14 @@ class HTTPPageGetter(client.HTTPPageGetter):
rectimeout = 15
checkTimeout = False
expect100 = False
def sendCommand(self, command, path):
version=b' HTTP/1.0\r\n'
if self.expect100:
version=b' HTTP/1.1\r\n'
self.transport.writeSequence([command, b' ', path, version])
# XXX Serve per farlo diventare 1.1??
#def sendCommand(self, command, path):
# self.transport.write('%s %s HTTP/1.1\r\n' % (command, path))
def lineReceived(self, *args, **kwargs):
self.lastrec = time.time()
......@@ -100,9 +104,45 @@ class HTTPPageGetter(client.HTTPPageGetter):
auth = "Basic " + base64.encodestring(cred).replace('\012','')
#self.sendHeader('Authorization', auth)
self.factory.headers['Authorization'] = auth
if self.expect100:
self.factory.headers['Expect'] = '100-continue'
self.checkTimeout = task.LoopingCall(self.timeoutCheck)
self.checkTimeout.start(1)
return client.HTTPPageGetter.connectionMade(self, *args, **kwargs)
method = getattr(self.factory, 'method', b'GET')
self.sendCommand(method, self.factory.path)
if self.factory.scheme == b'http' and self.factory.port != 80:
host = self.factory.host + b':' + intToBytes(self.factory.port)
elif self.factory.scheme == b'https' and self.factory.port != 443:
host = self.factory.host + b':' + intToBytes(self.factory.port)
else:
host = self.factory.host
self.sendHeader(b'Host', self.factory.headers.get(b"host", host))
self.sendHeader(b'User-Agent', self.factory.agent)
data = getattr(self.factory, 'postdata', None)
if data is not None:
self.sendHeader(b"Content-Length", intToBytes(len(data)))
cookieData = []
for (key, value) in self.factory.headers.items():
if key.lower() not in self._specialHeaders:
# we calculated it on our own
self.sendHeader(key, value)
if key.lower() == b'cookie':
cookieData.append(value)
for cookie, cookval in self.factory.cookies.items():
cookieData.append(cookie + b'=' + cookval)
if cookieData:
self.sendHeader(b'Cookie', b'; '.join(cookieData))
self.endHeaders()
self.headers = {}
if not self.expect100:
self._writeData()
def _writeData(self):
data = getattr(self.factory, 'postdata', None)
if data is not None:
self.transport.write(data)
def timeoutCheck(self):
......@@ -121,6 +161,12 @@ class HTTPPageGetter(client.HTTPPageGetter):
except:
pass
def handleStatus_100(self):
if self.expect100:
self._writeData()
else:
self.handleStatus_400()
def handleStatus_404(self):
self.handleStatusDefault()
self.factory.noPage(
......@@ -231,8 +277,8 @@ class HTTPPageGetterProgressStream(HTTPPageGetterProgress):
class HTTPClientFactory(client.HTTPClientFactory):
protocol = HTTPPageGetter
def __init__(self, url, method='GET', postdata=None, headers=None,
agent="Domotika Client (http://www.unixmedia.it)", timeout=0, cookies=None,
followRedirect=1, proxy_host = None, proxy_port = 80, headerscb=None):
agent="Nexlab Client (https://www.nexlab.net)", timeout=0, cookies=None,
followRedirect=1, proxy_host = None, proxy_port = 80, headerscb=None, expect100=False):
if proxy_host:
self.has_proxy = True
else:
......@@ -241,6 +287,7 @@ class HTTPClientFactory(client.HTTPClientFactory):
self.proxy_port = proxy_port
self.myurl = url
self.headerscb = headerscb
self.expect100 = expect100
client.HTTPClientFactory.__init__(self, url, method, postdata, headers,
agent, timeout, cookies,
followRedirect)
......@@ -271,6 +318,18 @@ class HTTPClientFactory(client.HTTPClientFactory):
self.headerscb(headers)
return client.HTTPClientFactory.gotHeaders(self, headers)
def buildProtocol(self, addr):
p = protocol.ClientFactory.buildProtocol(self, addr)
p.followRedirect = self.followRedirect
p.afterFoundGet = self.afterFoundGet
p.expect100 = self.expect100
if self.timeout:
timeoutCall = reactor.callLater(self.timeout, p.timeout)
self.deferred.addBoth(self._cancelTimeout, timeoutCall)
return p
class HTTPClientFactoryProgress(HTTPClientFactory):
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment