# vim:nowrap:textwidth=0
#
# Sheared -- non-blocking network programming library for Python
# Copyright (C) 2003  Sune Kirkeby <sune@mel.interspace.dk>
# 
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
# 
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
# 
# You should have received a copy of the GNU General Public License
# along with this program; if not, write to the Free Software
# Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
#

from sheared import error
from sheared import reactor
from sheared.python import io

class DumbProxy:
    def __init__(self, host, port):
        self.host = host
        self.port = port

    def handle(self, request, reply):
        try:
            version = request.requestline.version[0]
            transport = reactor.connectTCP((self.host, self.port))
            
            transport.write('%s\r\n' % request.requestline.raw)
            if version == 1:
                for k, v in request.headers.items():
                    transport.write('%s: %s\r\n' % (k, v))
                transport.write('\r\n')
                transport.write(request.body)

            reply.transport.sendfile(transport)

        except OSError:
            pass

        transport.close()

class ReversePassProxy:
    def __init__(self, host, port, location):
        self.host = host
        self.port = port
        self.location = location

    def handle(self, request, reply):
        try:
            version = request.requestline.version[0]
            transport = reactor.connectTCP((self.host, self.port))
            transport = io.BufferedReader(transport)
            
            transport.write('%s\r\n' % request.requestline.raw)
            if version == 1:
                for k, v in request.headers.items():
                    transport.write('%s: %s\r\n' % (k, v))
                transport.write('\r\n')
                transport.write(request.body)

            while 1:
                l = transport.readline()
                if l.startswith('Location: '):
                    l = l.replace('http://%s:%d' % (self.host, self.port),
                              self.location)
                reply.transport.write(l)
                if l == '\r\n':
                    break

            reply.transport.sendfile(transport)

        except OSError:
            pass

        transport.close()

@@ -853,16 +853,33 @@
  USA%0A#%0A%0A
+import urlparse%0A%0A
 from she
@@ -968,24 +968,1010 @@
  DumbProxy:%0A
+    def handle(self, request, reply):%0A        try:%0A            host = request.requestline.uri%5B1%5D%0A            addr = host, 80%0A            %0A            uri = list(request.requestline.uri)%0A            uri%5B0%5D = ''%0A            uri%5B1%5D = ''%0A%0A            transport = reactor.connectTCP(addr)%0A            %0A            transport.write('%25s %25s HTTP/%25d.%25d%5Cr%5Cn' %25 (%0A                                             request.requestline.method,%0A                                             urlparse.urlunsplit(uri),%0A                                             request.requestline.version%5B0%5D,%0A                                             request.requestline.version%5B1%5D))%0A%0A            for k, v in request.headers.items():%0A                transport.write('%25s: %25s%5Cr%5Cn' %25 (k, v))%0A            transport.write('%5Cr%5Cn')%0A            transport.write(request.body)%0A%0A            reply.transport.sendfile(transport)%0A            %0A        except OSError:%0A            pass%0A%0A        transport.close()%0A%0Aclass DumbProxyPass:%0A
     def __in

Rename DumbProxy, it is now DumbProxyPass. New: Add a real HTTP proxy (new DumbProxy).