Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 22 additions & 8 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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<String, String> responseHeaders = connection.getResponseHeaders();
Expand Down
27 changes: 19 additions & 8 deletions src/main/java/com/googlecode/fcgi4j/FCGIConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -599,4 +610,4 @@ private void finishRequest(FCGIHeader header) throws IOException {
endBuffer.rewind();
endRequest = FCGIEndRequest.parse(header, endBuffer);
}
}
}