Frage

I've got a small proxy-server written in Groovy, that remembers user's requests and then loads original content int SocketOutputStream. Everything (html, js and css) is loaded fine - but pictures. All binary content dissapears from the page.

Here is my code:

int host = 3128
def address = Inet4Address.getByName("localhost")
def server = new ServerSocket(host, 25, address)
println "Appication strted on $address, $host"

while (true) {
  server.accept() { socket ->
    socket.withStreams {SocketInputStream input, SocketOutputStream output ->
        def reader = input.newReader()

        String buffer = reader.readLine()
        params = buffer.split(' ')
        String url = params[1]

        URL contentUrl = new URL(url)
        //Doing some hard work with requested content

        output.withWriter { writer ->
            writer << new URL(url).openStream()
        }
    }
  }

}

Maybe someone cpould help me? Thanks.

War es hilfreich?

Lösung

You shouldn't use Readers and Writers if you're expecting binary data...

Try replacing:

    output.withWriter { writer ->
        writer << new URL(url).openStream()
    }

With

    new URL( url ).withInputStream { uin ->
        uin.eachByte( 4096 ) { buffer, nBytes ->
            output.write( buffer, 0, nBytes )
        }
    }
Lizenziert unter: CC-BY-SA mit Zuschreibung
Nicht verbunden mit StackOverflow
scroll top