diff --git a/README.md b/README.md index ca2a0e7..69f9774 100644 --- a/README.md +++ b/README.md @@ -14,14 +14,28 @@ 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"); + +//add post data only if request method is GET +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(); diff --git a/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java b/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java index 03bee47..e77bdca 100644 --- a/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java +++ b/src/main/java/com/googlecode/fcgi4j/FCGIConnection.java @@ -468,28 +468,39 @@ public long read(ByteBuffer[] dsts) throws IOException { public int read(ByteBuffer dst) throws IOException { readyRead(); - int read = readBufferedData(dst); + int read = 0; + + int available = 0, padding = 0; + + outer: + if (available == 0) { + read += readBufferedData(dst); + } else { + read += readStdoutData(dst, available, padding); + } while (dst.hasRemaining()) { FCGIHeader header = readHeader(); - if (header.getType() == FCGIHeaderType.FCGI_STDOUT && header.getLength() != 0) { + if ((header.getType() == FCGIHeaderType.FCGI_STDOUT || header.getType() == FCGIHeaderType.FCGI_STDERR) && header.getLength() != 0) { int currentRead = readStdoutData(dst, header.getLength(), header.getPadding()); - if (currentRead < header.getLength()) { - bufferStdoutData(header.getLength() - currentRead, header.getPadding()); - } + available = header.getLength() - currentRead; + padding = header.getPadding(); read += currentRead; } else { if (header.getType() == FCGIHeaderType.FCGI_END_REQUEST) { finishRequest(header); + break; } - - break; } } + if (available != 0) { + bufferStdoutData(available, padding); + } + return read; } @@ -599,4 +610,4 @@ private void finishRequest(FCGIHeader header) throws IOException { endBuffer.rewind(); endRequest = FCGIEndRequest.parse(header, endBuffer); } -} \ No newline at end of file +}