[Twisted-Python] fixes for web.proxy

hi, twisted.web.proxy didn't work for me. i've included two fixes.. a) makes Proxy a factory which current twisted can use. and b) is a neccesary change to deal with pages sending just \n i dunno how to implemented this best, but proxy or HTTPClient are unusable if they depend on \r\n being sent. maybe lineReceiver needs a new mode to accept \r\n AND \n as delimiters, but removing both? here are my diffs: diff -u -r1.9 proxy.py --- web/proxy.py 15 Nov 2002 20:47:36 -0000 1.9 +++ web/proxy.py 3 Dec 2002 03:05:18 -0000 @@ -27,6 +27,7 @@ class ProxyClient(http.HTTPClient): + delimiter = '\n' def __init__(self, command, rest, version, headers, data, father): self.father = father @@ -106,9 +107,15 @@ s, self) reactor.connectTCP(host, port, clientFactory) -class Proxy(http.HTTPChannel): - - requestFactory = ProxyRequest +class Proxy(http.HTTPFactory): + def buildProtocol(self, addr): + """Generate a channel attached to this site. + """ + channel = http.HTTPChannel() + channel.requestFactory = ProxyRequest + channel.site = self + channel.factory = self + return channel class ReverseProxyRequest(http.Request): Index: protocols/http.py =================================================================== RCS file: /cvs/Twisted/twisted/protocols/http.py,v retrieving revision 1.63 diff -u -r1.63 http.py --- protocols/http.py 15 Nov 2002 20:57:58 -0000 1.63 +++ protocols/http.py 3 Dec 2002 03:05:20 -0000 @@ -268,6 +268,7 @@ length = None firstLine = 1 __buffer = '' + delimiter = '\n' def sendCommand(self, command, path): self.transport.write('%s %s HTTP/1.0\r\n' % (command, path)) @@ -279,6 +280,8 @@ self.transport.write('\r\n') def lineReceived(self, line): + if line[-1] == '\r': + line = line[0:-1] if self.firstLine: self.firstLine = 0 try:

this is a more generic fix, that adds special handling for services where crlf and lf are interchangeable. comments, please! also i'd like to get feedback on proxy maintainership. if noone disagrees i'll make a plugin for proxy, and try to polish it up, so that it becomes actually useable. paul Index: protocols/basic.py =================================================================== RCS file: /cvs/Twisted/twisted/protocols/basic.py,v retrieving revision 1.27 diff -u -r1.27 basic.py --- protocols/basic.py 15 Nov 2002 14:56:18 -0000 1.27 +++ protocols/basic.py 3 Dec 2002 13:57:30 -0000 @@ -147,6 +147,7 @@ line_mode = 1 __buffer = '' delimiter = '\r\n' + crlfhack = 0 MAX_LENGTH = 16384 def dataReceived(self, data): @@ -157,7 +158,16 @@ self.__buffer = self.__buffer+data while self.line_mode: try: - line, self.__buffer = self.__buffer.split(self.delimiter, 1) + if self.crlfhack: + delimiter = '\n' + else: + delimiter = self.delimiter + + line, self.__buffer = self.__buffer.split(delimiter, 1) + + if self.crlfhack: + line = line.replace('\r', '') + except ValueError: if len(self.__buffer) > self.MAX_LENGTH: self.transport.loseConnection() Index: protocols/http.py =================================================================== RCS file: /cvs/Twisted/twisted/protocols/http.py,v retrieving revision 1.63 diff -u -r1.63 http.py --- protocols/http.py 15 Nov 2002 20:57:58 -0000 1.63 +++ protocols/http.py 3 Dec 2002 13:57:32 -0000 @@ -268,6 +268,8 @@ length = None firstLine = 1 __buffer = '' + delimiter = '\r\n' + crlfhack = 1 def sendCommand(self, command, path): self.transport.write('%s %s HTTP/1.0\r\n' % (command, path)) Index: web/proxy.py =================================================================== RCS file: /cvs/Twisted/twisted/web/proxy.py,v retrieving revision 1.9 diff -u -r1.9 proxy.py --- web/proxy.py 15 Nov 2002 20:47:36 -0000 1.9 +++ web/proxy.py 3 Dec 2002 13:57:32 -0000 @@ -106,9 +106,15 @@ s, self) reactor.connectTCP(host, port, clientFactory) -class Proxy(http.HTTPChannel): - - requestFactory = ProxyRequest +class Proxy(http.HTTPFactory): + def buildProtocol(self, addr): + """Generate a channel attached to this site. + """ + channel = http.HTTPChannel() + channel.requestFactory = ProxyRequest + channel.site = self + channel.factory = self + return channel class ReverseProxyRequest(http.Request):

this is a more generic fix, that adds special handling for services where crlf and lf are interchangeable. comments, please! also i'd like to get feedback on proxy maintainership. if noone disagrees i'll make a plugin for proxy, and try to polish it up, so that it becomes actually useable. paul Index: protocols/basic.py =================================================================== RCS file: /cvs/Twisted/twisted/protocols/basic.py,v retrieving revision 1.27 diff -u -r1.27 basic.py --- protocols/basic.py 15 Nov 2002 14:56:18 -0000 1.27 +++ protocols/basic.py 3 Dec 2002 13:57:30 -0000 @@ -147,6 +147,7 @@ line_mode = 1 __buffer = '' delimiter = '\r\n' + crlfhack = 0 MAX_LENGTH = 16384 def dataReceived(self, data): @@ -157,7 +158,16 @@ self.__buffer = self.__buffer+data while self.line_mode: try: - line, self.__buffer = self.__buffer.split(self.delimiter, 1) + if self.crlfhack: + delimiter = '\n' + else: + delimiter = self.delimiter + + line, self.__buffer = self.__buffer.split(delimiter, 1) + + if self.crlfhack: + line = line.replace('\r', '') + except ValueError: if len(self.__buffer) > self.MAX_LENGTH: self.transport.loseConnection() Index: protocols/http.py =================================================================== RCS file: /cvs/Twisted/twisted/protocols/http.py,v retrieving revision 1.63 diff -u -r1.63 http.py --- protocols/http.py 15 Nov 2002 20:57:58 -0000 1.63 +++ protocols/http.py 3 Dec 2002 13:57:32 -0000 @@ -268,6 +268,8 @@ length = None firstLine = 1 __buffer = '' + delimiter = '\r\n' + crlfhack = 1 def sendCommand(self, command, path): self.transport.write('%s %s HTTP/1.0\r\n' % (command, path)) Index: web/proxy.py =================================================================== RCS file: /cvs/Twisted/twisted/web/proxy.py,v retrieving revision 1.9 diff -u -r1.9 proxy.py --- web/proxy.py 15 Nov 2002 20:47:36 -0000 1.9 +++ web/proxy.py 3 Dec 2002 13:57:32 -0000 @@ -106,9 +106,15 @@ s, self) reactor.connectTCP(host, port, clientFactory) -class Proxy(http.HTTPChannel): - - requestFactory = ProxyRequest +class Proxy(http.HTTPFactory): + def buildProtocol(self, addr): + """Generate a channel attached to this site. + """ + channel = http.HTTPChannel() + channel.requestFactory = ProxyRequest + channel.site = self + channel.factory = self + return channel class ReverseProxyRequest(http.Request):
participants (1)
-
Paul Boehm