From 57c09bfed20ae535df32534ea6fbbd77139e4682 Mon Sep 17 00:00:00 2001 From: Sergei Selivanov Date: Wed, 29 Jul 2015 10:57:54 +0000 Subject: [PATCH 1/5] fix for issue #6 (truncated responses) --- .../com/googlecode/fcgi4j/FCGIConnection.java | 43 ++++++++++++------- 1 file changed, 28 insertions(+), 15 deletions(-) diff --git a/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java b/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java index 03bee47..1348c3a 100644 --- a/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java +++ b/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java @@ -468,28 +468,41 @@ public long read(ByteBuffer[] dsts) throws IOException { public int read(ByteBuffer dst) throws IOException { readyRead(); - int read = readBufferedData(dst); + int read = 0; - while (dst.hasRemaining()) { - FCGIHeader header = readHeader(); + int available = 0, padding = 0; - if (header.getType() == FCGIHeaderType.FCGI_STDOUT && header.getLength() != 0) { - int currentRead = readStdoutData(dst, header.getLength(), header.getPadding()); + outer: + if (true) { + if (available == 0) { + read += readBufferedData(dst); + } else { + read += readStdoutData(dst, available, padding); + } - if (currentRead < header.getLength()) { - bufferStdoutData(header.getLength() - currentRead, header.getPadding()); - } + while (dst.hasRemaining()) { + FCGIHeader header = readHeader(); - read += currentRead; - } else { - if (header.getType() == FCGIHeaderType.FCGI_END_REQUEST) { - finishRequest(header); - } + if ((header.getType() == FCGIHeaderType.FCGI_STDOUT || header.getType() == FCGIHeaderType.FCGI_STDERR) && header.getLength() != 0) { + int currentRead = readStdoutData(dst, header.getLength(), header.getPadding()); - break; + available = header.getLength() - currentRead; + padding = header.getPadding(); + + read += currentRead; + } else { + if (header.getType() == FCGIHeaderType.FCGI_END_REQUEST) { + finishRequest(header); + break; + } + } } } + if (available != 0) { + bufferStdoutData(available, padding); + } + return read; } @@ -599,4 +612,4 @@ private void finishRequest(FCGIHeader header) throws IOException { endBuffer.rewind(); endRequest = FCGIEndRequest.parse(header, endBuffer); } -} \ No newline at end of file +} From 7a448f256380376da3a53c5f47eed136d14a49df Mon Sep 17 00:00:00 2001 From: root Date: Wed, 29 Jul 2015 11:41:55 +0000 Subject: [PATCH 2/5] removing if(true) --- .../com/googlecode/fcgi4j/FCGIConnection.java | 34 +++++++++---------- 1 file changed, 16 insertions(+), 18 deletions(-) diff --git a/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java b/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java index 1348c3a..e77bdca 100644 --- a/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java +++ b/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java @@ -473,28 +473,26 @@ public int read(ByteBuffer dst) throws IOException { int available = 0, padding = 0; outer: - if (true) { - if (available == 0) { - read += readBufferedData(dst); - } else { - read += readStdoutData(dst, available, padding); - } + if (available == 0) { + read += readBufferedData(dst); + } else { + read += readStdoutData(dst, available, padding); + } - while (dst.hasRemaining()) { - FCGIHeader header = readHeader(); + while (dst.hasRemaining()) { + FCGIHeader header = readHeader(); - if ((header.getType() == FCGIHeaderType.FCGI_STDOUT || header.getType() == FCGIHeaderType.FCGI_STDERR) && header.getLength() != 0) { - int currentRead = readStdoutData(dst, header.getLength(), header.getPadding()); + if ((header.getType() == FCGIHeaderType.FCGI_STDOUT || header.getType() == FCGIHeaderType.FCGI_STDERR) && header.getLength() != 0) { + int currentRead = readStdoutData(dst, header.getLength(), header.getPadding()); - available = header.getLength() - currentRead; - padding = header.getPadding(); + available = header.getLength() - currentRead; + padding = header.getPadding(); - read += currentRead; - } else { - if (header.getType() == FCGIHeaderType.FCGI_END_REQUEST) { - finishRequest(header); - break; - } + read += currentRead; + } else { + if (header.getType() == FCGIHeaderType.FCGI_END_REQUEST) { + finishRequest(header); + break; } } } From e59c8cb7b98905a4511676cabb8f98c121810510 Mon Sep 17 00:00:00 2001 From: Sergei Selivanov Date: Mon, 28 Sep 2015 18:14:09 +0300 Subject: [PATCH 3/5] fix for issue #7 (php5-fpm: Connection reset by peer (read)); readme update --- README.md | 29 +++++++++++++++++++++-------- 1 file changed, 21 insertions(+), 8 deletions(-) diff --git a/README.md b/README.md index ca2a0e7..a9b3ab8 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,27 @@ as expected. FCGIConnection connection = FCGIConnection.open(); connection.connect(new InetSocketAddress("localhost", 5672)); -connection.beginRequest("/var/www/foobar.php"); -connection.setRequestMethod("POST"); - -byte[] postData = "hello=world".getBytes(); - -//set contentLength, it's important -connection.setContentLength(postData.length); -connection.write(ByteBuffer.wrap(postData)); +String requestMethod = "GET" +String targetScript = "/var/www/foobar.php" + +connection.beginRequest(targetScript); +connection.setRequestMethod(requestMethod); +connection.setQueryString("querystring=1"); + +connection.addParams("DOCUMENT_ROOT", "/var/www/"); +connection.addParams("SCRIPT_FILENAME", targetScript); +connection.addParams("SCRIPT_NAME", targetSCript); +connection.addParams("GATEWAY_INTERFACE", "FastCGI/1.0"); +connection.addParams("SERVER_PROTOCOL", "HTTP/1.1"); +connection.addParams("CONTENT_TYPE", "application/x-www-form-urlencoded"); + +if(requestMethod.equalsIgnoreCase("POST")){ + byte[] postData = "hello=world".getBytes(); + + //set contentLength, it's important + connection.setContentLength(postData.length); + connection.write(ByteBuffer.wrap(postData)); +} //print response headers Map responseHeaders = connection.getResponseHeaders(); From 5a264a40f153240983ec679b3c39710d1860f0bc Mon Sep 17 00:00:00 2001 From: Sergei Selivanov Date: Tue, 29 Sep 2015 14:38:27 +0300 Subject: [PATCH 4/5] comment --- README.md | 1 + 1 file changed, 1 insertion(+) diff --git a/README.md b/README.md index a9b3ab8..62f6923 100644 --- a/README.md +++ b/README.md @@ -28,6 +28,7 @@ connection.addParams("GATEWAY_INTERFACE", "FastCGI/1.0"); connection.addParams("SERVER_PROTOCOL", "HTTP/1.1"); connection.addParams("CONTENT_TYPE", "application/x-www-form-urlencoded"); +//add post data only if request method is GET if(requestMethod.equalsIgnoreCase("POST")){ byte[] postData = "hello=world".getBytes(); From 385256602220a85daaf1a66852cf172ef670bd97 Mon Sep 17 00:00:00 2001 From: Sergei Selivanov Date: Sun, 1 Mar 2026 21:03:54 +0200 Subject: [PATCH 5/5] README typo fix --- README.md | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/README.md b/README.md index 62f6923..69f9774 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ connection.setQueryString("querystring=1"); connection.addParams("DOCUMENT_ROOT", "/var/www/"); connection.addParams("SCRIPT_FILENAME", targetScript); -connection.addParams("SCRIPT_NAME", targetSCript); +connection.addParams("SCRIPT_NAME", targetScript); connection.addParams("GATEWAY_INTERFACE", "FastCGI/1.0"); connection.addParams("SERVER_PROTOCOL", "HTTP/1.1"); connection.addParams("CONTENT_TYPE", "application/x-www-form-urlencoded");