Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: src/sunlabs/brazil/util/http/HttpRequest.java

Issue 10102005: ABP/Android Process chunked requests (Closed)
Patch Set: Created April 5, 2013, 1:45 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: src/sunlabs/brazil/util/http/HttpRequest.java
===================================================================
--- a/src/sunlabs/brazil/util/http/HttpRequest.java
+++ b/src/sunlabs/brazil/util/http/HttpRequest.java
@@ -479,6 +479,7 @@
HttpInputStream in;
InputStream under;
+ HttpInputStream cs;
/**
* The status line from the HTTP response. This field is not valid until
@@ -670,6 +671,12 @@
return postData;
}
+ public void
+ setHttpInputStream(HttpInputStream cs)
+ {
+ this.cs = cs;
Wladimir Palant 2013/04/05 14:27:09 Bad indentation here - are you mixing tabs and spa
Andrey Novikov 2013/04/05 14:34:28 No, I'm trying to preserve ugly Brazil code style.
Wladimir Palant 2013/04/09 07:44:45 Indentation is fine in the Brazil code above...
+ }
+
/**
* Connect to the target host (or proxy), send the request, and read the
* response headers. Any setup routines must be called before the call
@@ -869,9 +876,82 @@
postData.writeTo(p);
postData = null; // Release memory.
}
+
+ // Pass any data left in client stream (in case of chunked request content)
+ String encoding = requestHeaders.get("Transfer-Encoding", "");
+ if (encoding != null && encoding.equals("chunked") && cs != null)
Wladimir Palant 2013/04/05 14:27:09 Should we really limit it to the chunked encoding?
Andrey Novikov 2013/04/05 14:34:28 Why should we not?
Wladimir Palant 2013/04/09 07:44:45 Yes, if we cannot avoid parsing chunked encoding t
+ {
+ byte[] buf = new byte[4096];
+ int bytesLeft = -1;
+ while (true)
+ {
+ // Read chunk size
+ if (bytesLeft <= 0)
+ {
+ bytesLeft = getChunkSize(cs);
Wladimir Palant 2013/04/05 14:27:09 Why do we care about the encoding and make tons of
Andrey Novikov 2013/04/05 14:34:28 Because input stream never ends. I've talked about
+ // Output chunk size
+ p.print(Integer.toHexString(bytesLeft) + "\r\n");
+ }
+ if (bytesLeft == 0)
+ break;
+ // Pass chunk data
+ int count = cs.read(buf, 0, Math.min(bytesLeft, buf.length));
+ if (count < 0)
+ {
+ // This shouldn't occur - no final zero chunk
+ bytesLeft = -1;
+ break;
+ }
+ p.write(buf, 0, count);
Wladimir Palant 2013/04/05 14:27:09 Mixing tabs and spaces again.
+ bytesLeft -= count;
+ if (bytesLeft == 0)
+ p.print("\r\n");
+ }
+ // Pass the trailer
+ if (bytesLeft == 0)
+ {
+ while (true)
+ {
+ String line = cs.readLine(LINE_LIMIT);
+ if (line == null)
+ break;
+ p.print(line + "\r\n");
+ if (line.length() == 0)
+ break;
+ }
+ }
+ }
+
p.flush();
}
+ private int
+ getChunkSize(HttpInputStream is)
Wladimir Palant 2013/04/09 07:44:45 Feel free to add a comment: This function has been
Andrey Novikov 2013/04/09 08:03:56 What's the reason? I have copied it from Brazil co
Wladimir Palant 2013/04/09 08:31:55 You forked Brazil code, later somebody will still
Andrey Novikov 2013/04/09 08:46:10 But this is the same file. :) Anyway, done.
+ throws IOException
+ {
+ /*
+ * Although HTTP/1.1 chunking spec says that there is one "\r\n"
+ * between chunks, some servers (for example, maps.yahoo.com)
+ * send more than one blank line between chunks. So, read and skip
+ * all the blank lines seen between chunks.
+ */
+
+ int bytesLeft = 0;
+ String line;
+ do {
+ // Sanity check: limit chars when expecting a chunk size.
+
+ line = is.readLine(HttpRequest.LINE_LIMIT);
+ } while ((line != null) && (line.length() == 0));
+
+ try {
+ bytesLeft = Integer.parseInt(line.trim(), 16);
+ } catch (Exception e) {
+ throw new IOException("malformed chunk");
+ }
+ return bytesLeft;
+ }
+
void
readStatusLine()
throws IOException
« src/org/adblockplus/brazil/RequestHandler.java ('K') | « src/sunlabs/brazil/server/Request.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld