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

Side by Side 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.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View unified diff | Download patch
OLDNEW
1 /* 1 /*
2 * HttpRequest.java 2 * HttpRequest.java
3 * 3 *
4 * Brazil project web application toolkit, 4 * Brazil project web application toolkit,
5 * export version: 2.3 5 * export version: 2.3
6 * Copyright (c) 1999-2007 Sun Microsystems, Inc. 6 * Copyright (c) 1999-2007 Sun Microsystems, Inc.
7 * 7 *
8 * Sun Public License Notice 8 * Sun Public License Notice
9 * 9 *
10 * The contents of this file are subject to the Sun Public License Version 10 * The contents of this file are subject to the Sun Public License Version
(...skipping 461 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 */ 472 */
473 public static boolean displayAllHeaders = false; 473 public static boolean displayAllHeaders = false;
474 474
475 ByteArrayOutputStream postData; 475 ByteArrayOutputStream postData;
476 476
477 String uri; 477 String uri;
478 String connectionHeader; 478 String connectionHeader;
479 479
480 HttpInputStream in; 480 HttpInputStream in;
481 InputStream under; 481 InputStream under;
482 HttpInputStream cs;
482 483
483 /** 484 /**
484 * The status line from the HTTP response. This field is not valid until 485 * The status line from the HTTP response. This field is not valid until
485 * after <code>connect</code> has been called and the HTTP response has 486 * after <code>connect</code> has been called and the HTTP response has
486 * been read. 487 * been read.
487 */ 488 */
488 public String status; 489 public String status;
489 490
490 /** 491 /**
491 * The headers that were present in the HTTP response. This field is 492 * The headers that were present in the HTTP response. This field is
(...skipping 171 matching lines...) Expand 10 before | Expand all | Expand 10 after
663 public OutputStream 664 public OutputStream
664 getOutputStream() 665 getOutputStream()
665 throws IOException 666 throws IOException
666 { 667 {
667 if (postData == null) { 668 if (postData == null) {
668 postData = new ByteArrayOutputStream(); 669 postData = new ByteArrayOutputStream();
669 } 670 }
670 return postData; 671 return postData;
671 } 672 }
672 673
674 public void
675 setHttpInputStream(HttpInputStream cs)
676 {
677 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...
678 }
679
673 /** 680 /**
674 * Connect to the target host (or proxy), send the request, and read the 681 * Connect to the target host (or proxy), send the request, and read the
675 * response headers. Any setup routines must be called before the call 682 * response headers. Any setup routines must be called before the call
676 * to this method, and routines to examine the result must be called after 683 * to this method, and routines to examine the result must be called after
677 * this method. 684 * this method.
678 * <p> 685 * <p>
679 * 686 *
680 * @throws UnknownHostException 687 * @throws UnknownHostException
681 * if the target host (or proxy) could not be contacted. 688 * if the target host (or proxy) could not be contacted.
682 * 689 *
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 869
863 PrintStream p = new PrintStream(hs.out); 870 PrintStream p = new PrintStream(hs.out);
864 p.print(method + " " + uri + " " + version + "\r\n"); 871 p.print(method + " " + uri + " " + version + "\r\n");
865 requestHeaders.print(p); 872 requestHeaders.print(p);
866 p.print("\r\n"); 873 p.print("\r\n");
867 874
868 if (postData != null) { 875 if (postData != null) {
869 postData.writeTo(p); 876 postData.writeTo(p);
870 postData = null; // Release memory. 877 postData = null; // Release memory.
871 } 878 }
879
880 // Pass any data left in client stream (in case of chunked request conte nt)
881 String encoding = requestHeaders.get("Transfer-Encoding", "");
882 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
883 {
884 byte[] buf = new byte[4096];
885 int bytesLeft = -1;
886 while (true)
887 {
888 // Read chunk size
889 if (bytesLeft <= 0)
890 {
891 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
892 // Output chunk size
893 p.print(Integer.toHexString(bytesLeft) + "\r\n");
894 }
895 if (bytesLeft == 0)
896 break;
897 // Pass chunk data
898 int count = cs.read(buf, 0, Math.min(bytesLeft, buf.length));
899 if (count < 0)
900 {
901 // This shouldn't occur - no final zero chunk
902 bytesLeft = -1;
903 break;
904 }
905 p.write(buf, 0, count);
Wladimir Palant 2013/04/05 14:27:09 Mixing tabs and spaces again.
906 bytesLeft -= count;
907 if (bytesLeft == 0)
908 p.print("\r\n");
909 }
910 // Pass the trailer
911 if (bytesLeft == 0)
912 {
913 while (true)
914 {
915 String line = cs.readLine(LINE_LIMIT);
916 if (line == null)
917 break;
918 p.print(line + "\r\n");
919 if (line.length() == 0)
920 break;
921 }
922 }
923 }
924
872 p.flush(); 925 p.flush();
873 } 926 }
874 927
928 private int
929 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.
930 throws IOException
931 {
932 /*
933 * Although HTTP/1.1 chunking spec says that there is one "\r\n"
934 * between chunks, some servers (for example, maps.yahoo.com)
935 * send more than one blank line between chunks. So, read and skip
936 * all the blank lines seen between chunks.
937 */
938
939 int bytesLeft = 0;
940 String line;
941 do {
942 // Sanity check: limit chars when expecting a chunk size.
943
944 line = is.readLine(HttpRequest.LINE_LIMIT);
945 } while ((line != null) && (line.length() == 0));
946
947 try {
948 bytesLeft = Integer.parseInt(line.trim(), 16);
949 } catch (Exception e) {
950 throw new IOException("malformed chunk");
951 }
952 return bytesLeft;
953 }
954
875 void 955 void
876 readStatusLine() 956 readStatusLine()
877 throws IOException 957 throws IOException
878 { 958 {
879 while (true) { 959 while (true) {
880 status = in.readLine(LINE_LIMIT); 960 status = in.readLine(LINE_LIMIT);
881 if (status == null) { 961 if (status == null) {
882 throw new EOFException(); 962 throw new EOFException();
883 } 963 }
884 if (status.startsWith("HTTP/1.1 100") 964 if (status.startsWith("HTTP/1.1 100")
(...skipping 877 matching lines...) Expand 10 before | Expand all | Expand 10 after
1762 return "(null)"; 1842 return "(null)";
1763 } 1843 }
1764 StringBuffer sb = new StringBuffer(); 1844 StringBuffer sb = new StringBuffer();
1765 for (int i = 0; i < idle.size(); i++) { 1845 for (int i = 0; i < idle.size(); i++) {
1766 HttpSocket hs = (HttpSocket) idle.elementAt(i); 1846 HttpSocket hs = (HttpSocket) idle.elementAt(i);
1767 sb.append(hs.toString() + ", "); 1847 sb.append(hs.toString() + ", ");
1768 } 1848 }
1769 return sb.toString(); 1849 return sb.toString();
1770 } 1850 }
1771 } 1851 }
OLDNEW
« 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