1

在运行此代码时,我在最后一个 while 循环中遇到了 OutputStream.write 异常(它在代码中的其他地方工作正常) - 这是在 java 中的代理服务器的植入,当在主机响应中搜索时内容长度并将结果转发到它可以工作的浏览器,但是当尝试处理“Transfer-Encoding:chunked”策略时,相同的方法不起作用'并抛出此异常:

 java.net.SocketException: Connection reset by peer: socket write error
at java.net.SocketOutputStream.socketWrite0(Native Method)
at java.net.SocketOutputStream.socketWrite(Unknown Source)
at java.net.SocketOutputStream.write(Unknown Source)
at IncomingRequest.run(IncomingRequest.java:250)

 is = hostSocket.getInputStream();
            OutputStream os = client.getOutputStream();

            System.out.println("Forwarding request from server");

            byte[] currByte = new byte[1];
            StringBuilder responseHeader = new StringBuilder();
            int crlfCounter = 4;

            // Separates the response header by looking for 2 consecutive \r\n

            while (crlfCounter > 0){
                is.read(currByte);
                os.write(currByte);
                System.out.print((char)currByte[0]);
                responseHeader.append((char)currByte[0]);
                if ((char)currByte[0] == '\r' || (char)currByte[0] == '\n'){
                    crlfCounter--;
                }else{
                    crlfCounter = 4;
                }
            }

            StringBuilder chuckSize = new StringBuilder();
            int contentLength = 0;
            int contentIndex = responseHeader.toString().indexOf("Content-Length: ");
            int chunkedIndex = responseHeader.toString().indexOf("Transfer-Encoding: chunked");
            if (contentIndex != -1) {
                contentIndex += 16;
                int conEnd = responseHeader.toString().indexOf('\n', contentIndex);
                contentLength = Integer.parseInt(responseHeader.toString().substring(contentIndex,conEnd).trim());
                System.out.println("Content Length is : " + contentLength);
                while (contentLength > 0){
                    is.read(currByte);
                    os.write(currByte);
                    contentLength--;
                }
                os.write('\r');
                os.write('\n');
                os.write('\r');
                os.write('\n');
            } else  if (chunkedIndex != -1){
                boolean lastChunk = false;
                while (!lastChunk) {

                    do {
                        is.read(currByte);
                        chuckSize.append((char) currByte[0]);
                    } while ((char)currByte[0] != '\n');

                    contentLength = Integer.parseInt(chuckSize.toString().trim(), 16);
                    System.out.println("Hex " + chuckSize.toString());
                    System.out.println(contentLength + "     the number");

                    if (contentLength == 0) {
                        lastChunk = true;
                    }

                    while (contentLength > 0){
                        is.read(currByte);
                        os.write(currByte);
                        contentLength--;
                    }
                }
            }
4

1 回答 1

2

我可能误读了您的代码,但似乎您将所有标头(包括“Transfer-Encoding:chunked”)写入os,但您没有写入实际的块大小,因此接收端可能会因为非法输入而关闭连接(期望块大小,获取其他数据)

于 2012-01-20T19:52:27.730 回答