Skip to content

Commit

Permalink
Merge pull request #37 from aogburn/MODCLUSTER-348
Browse files Browse the repository at this point in the history
MODCLUSTER-348: backport MODCLUSTER-267, DefaultMCMPHandler can't handle chunked-encoding
  • Loading branch information
jfclere committed Aug 7, 2013
2 parents 841c171 + 39e64e7 commit f299f33
Showing 1 changed file with 38 additions and 9 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -898,6 +898,7 @@ private String sendRequest(MCMPRequest request, Proxy proxy)
String errorType = null;
int contentLength = 0;
boolean close = false;
boolean chuncked = false;
if (line != null)
{
try
Expand Down Expand Up @@ -938,6 +939,9 @@ else if ("connection".equalsIgnoreCase(headerName))
{
if ("close".equalsIgnoreCase(headerValue))
close = true;
} else if ("Transfer-Encoding".equalsIgnoreCase(headerName)) {
if ("chunked".equalsIgnoreCase(headerValue))
chuncked = true;
}
line = reader.readLine();
}
Expand Down Expand Up @@ -975,21 +979,46 @@ else if ("connection".equalsIgnoreCase(headerName))
}
}

if (contentLength == 0 && !close) return null;
if (close) {
contentLength = Integer.MAX_VALUE;
} else if (contentLength == 0 && ! chuncked) {
return null;
}

// Read the request body
StringBuilder result = new StringBuilder();
char[] buffer = new char[512];
if (close)
contentLength = Integer.MAX_VALUE;
while (contentLength > 0)
{
int bytes = reader.read(buffer, 0, (contentLength > buffer.length) ? buffer.length : contentLength);

if (chuncked) {
boolean skipcrlf = false;
for (;;) {
if (skipcrlf)
reader.readLine(); // Skip CRLF
else
skipcrlf = true;
line = reader.readLine();
contentLength = Integer.parseInt(line, 16);
if (contentLength == 0) {
reader.readLine(); // Skip last CRLF.
break;
}
while (contentLength > 0) {
int bytes = reader.read(buffer, 0, (contentLength > buffer.length) ? buffer.length : contentLength);
if (bytes <= 0)
break;
result.append(buffer, 0, bytes);
contentLength -= bytes;
}
}
} else {
while (contentLength > 0) {
int bytes = reader.read(buffer, 0, (contentLength > buffer.length) ? buffer.length : contentLength);

if (bytes <= 0) break;
if (bytes <= 0) break;

result.append(buffer, 0, bytes);
contentLength -= bytes;
result.append(buffer, 0, bytes);
contentLength -= bytes;
}
}

return result.toString();
Expand Down

0 comments on commit f299f33

Please sign in to comment.