Bitte helfen Sie mir herauszufinden, was mit diesem Web-Proxy-Code nicht stimmt

Ich möchte einen Web-Proxy für die Übung schreiben, und dies ist der Code, den ich bisher habe:


// returns a map that contains the port and the host
def parseHostAndPort(String data) {
    def objMap // this has host and port as keys
    data.eachLine { line ->
        if(line =~ /^(?i)get|put|post|head|trace|delete/) {
            println line
            def components = line.split(" ")
            def resource = components[1]
            def colon = resource.indexOf(":")
            if(colon != -1) {
                URL u = new URL(resource)
                def pHost = u.host
                def pPort = u.port
                return (objMap = [host:pHost,port:pPort])
            }
            else {
                return (objMap = [host:resource,port:80])
            }
        }
    }
    return objMap
}

// reads a http request from a client
def readClientData(Socket clientSocket) {
    def actualBuffer = new StringBuilder()
    InputStream inStream = clientSocket.inputStream
    while(true) {
        def available = inStream.available()
        if(available == 0)
        break;
        println "available data $available"
        def buffer = new byte[available]
        def bytesRead = inStream.read(buffer,0,available)
        actualBuffer << new String(buffer)
    }
    return actualBuffer.toString()
}

def sock = new ServerSocket(9000)
sock.reuseAddress = true
while(true) {
    sock.accept { cli ->
        println "got a client"
        def data = readClientData(cli)
        def parsed = parseHostAndPort(data)
        def host = parsed["host"]
        def port = parsed["port"]

        println "got from client $data"

        def nsock = new Socket(host,port)
        nsock << data // send data received from client to the socket
        nsock.outputStream.flush() 
        def datax = readClientData(nsock)
        println "got back $datax"
        cli << datax // send the client the response
        cli.outputStream.flush()
        cli.close()
    }
}

Im Moment ist alles was es tut:

Lies die HTTP-Anfrage, die mein Browser sendet

Analysieren Sie den Host und den Port

Stellen Sie eine Verbindung zu diesem Host her und schreiben Sie die vom Client empfangenen Daten

Senden Sie dem Client die vom Host empfangenen Daten zurück

Aber ... es funktioniert nicht immer. Manchmal macht es eine gute Anfrage, manchmal nicht. Ich bin mir nicht sicher, ob es ein Pufferproblem ist. Die Sache ist, fügte ich hinzuflush Anrufe und immer noch nichts.

Kannst du erkennen, was ich falsch mache?

BEARBEITEN:

Das ist mir aufgefallen, wenn ich welche hinzufügesleep Bei Anrufen scheint der Proxy bei einer höheren Anzahl von Anforderungen "zu funktionieren", jedoch nicht bei allen.Um das Kopfgeld einzusammeln, hilf mir herauszufinden, was ich falsch mache. Was ist der normale "Algorithmus" für einen Webproxy? Wo weiche ich davon ab? Vielen Dank!

Antworten auf die Frage(6)

Ihre Antwort auf die Frage