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

Delta Between Two Patch Sets: src/sunlabs/brazil/util/http/HttpRequest.java

Issue 10102005: ABP/Android Process chunked requests (Closed)
Left Patch Set: Created April 5, 2013, 1:45 p.m.
Right Patch Set: ABP/Android Process chunked requests Created April 9, 2013, 8:45 a.m.
Left:
Right:
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
Left: Side by side diff | Download
Right: Side by side diff | Download
« no previous file with change/comment | « src/sunlabs/brazil/server/Request.java ('k') | no next file » | no next file with change/comment »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
LEFTRIGHT
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
11 * 1.0 (the "License"). You may not use this file except in compliance with 11 * 1.0 (the "License"). You may not use this file except in compliance with
12 * the License. A copy of the License is included as the file "license.terms", 12 * the License. A copy of the License is included as the file "license.terms",
13 * and also available at http://www.sun.com/ 13 * and also available at http://www.sun.com/
14 * 14 *
15 * The Original Code is from: 15 * The Original Code is from:
16 * Brazil project web application toolkit release 2.3. 16 * Brazil project web application toolkit release 2.3.
17 * The Initial Developer of the Original Code is: cstevens. 17 * The Initial Developer of the Original Code is: cstevens.
18 * Portions created by cstevens are Copyright (C) Sun Microsystems, Inc. 18 * Portions created by cstevens are Copyright (C) Sun Microsystems, Inc.
19 * All Rights Reserved. 19 * All Rights Reserved.
20 * 20 *
21 * Contributor(s): cstevens, drach, suhler. 21 * Contributor(s): cstevens, drach, suhler.
22 * 22 *
23 * Version: 2.7 23 * Version: 2.7
24 * Created by cstevens on 99/09/15 24 * Created by cstevens on 99/09/15
25 * Last modified by suhler on 07/03/26 13:53:18 25 * Last modified by suhler on 07/03/26 13:53:18
26 * 26 *
27 * Version Histories: 27 * Version Histories:
28 *
29 * 13/04/09-12:44:12 (andrey@adblockplus.org)
30 * implemented proxying chunked request body
28 * 31 *
29 * 2.7 07/03/26-13:53:18 (suhler) 32 * 2.7 07/03/26-13:53:18 (suhler)
30 * doc updates 33 * doc updates
31 * 34 *
32 * 2.6 07/03/26-13:44:17 (suhler) 35 * 2.6 07/03/26-13:44:17 (suhler)
33 * add sample main() to act as a simple "wget" 36 * add sample main() to act as a simple "wget"
34 * 37 *
35 * 2.5 04/11/30-15:19:40 (suhler) 38 * 2.5 04/11/30-15:19:40 (suhler)
36 * fixed sccs version string 39 * fixed sccs version string
37 * 40 *
(...skipping 629 matching lines...) Expand 10 before | Expand all | Expand 10 after
667 { 670 {
668 if (postData == null) { 671 if (postData == null) {
669 postData = new ByteArrayOutputStream(); 672 postData = new ByteArrayOutputStream();
670 } 673 }
671 return postData; 674 return postData;
672 } 675 }
673 676
674 public void 677 public void
675 setHttpInputStream(HttpInputStream cs) 678 setHttpInputStream(HttpInputStream cs)
676 { 679 {
677 this.cs = cs; 680 » 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 } 681 }
679 682
680 /** 683 /**
681 * Connect to the target host (or proxy), send the request, and read the 684 * Connect to the target host (or proxy), send the request, and read the
682 * response headers. Any setup routines must be called before the call 685 * response headers. Any setup routines must be called before the call
683 * to this method, and routines to examine the result must be called after 686 * to this method, and routines to examine the result must be called after
684 * this method. 687 * this method.
685 * <p> 688 * <p>
686 * 689 *
687 * @throws UnknownHostException 690 * @throws UnknownHostException
(...skipping 184 matching lines...) Expand 10 before | Expand all | Expand 10 after
872 requestHeaders.print(p); 875 requestHeaders.print(p);
873 p.print("\r\n"); 876 p.print("\r\n");
874 877
875 if (postData != null) { 878 if (postData != null) {
876 postData.writeTo(p); 879 postData.writeTo(p);
877 postData = null; // Release memory. 880 postData = null; // Release memory.
878 } 881 }
879 882
880 // Pass any data left in client stream (in case of chunked request conte nt) 883 // Pass any data left in client stream (in case of chunked request conte nt)
881 String encoding = requestHeaders.get("Transfer-Encoding", ""); 884 String encoding = requestHeaders.get("Transfer-Encoding", "");
882 if (encoding != null && encoding.equals("chunked") && cs != null) 885 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
Felix Dahlke 2013/04/09 09:16:48 You could just do '"chunked".equals(encoding)' to
Andrey Novikov 2013/04/10 08:30:30 Done.
883 { 886 {
884 » byte[] buf = new byte[4096]; 887 » » byte[] buf = new byte[4096];
Felix Dahlke 2013/04/09 09:16:48 Indentation is off here
885 » int bytesLeft = -1; 888 » » int bytesLeft = -1;
886 » while (true) 889 » » while (true)
887 » { 890 » » {
888 » // Read chunk size 891 » » » // Read chunk size
889 » if (bytesLeft <= 0) 892 » » » if (bytesLeft <= 0)
890 » { 893 » » » {
891 » bytesLeft = getChunkSize(cs); 894 » » » » 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 895 » » » » // Output chunk size
893 » p.print(Integer.toHexString(bytesLeft) + "\r\n"); 896 » » » » p.print(Integer.toHexString(bytesLeft) + "\r\n") ;
894 » } 897 » » » }
895 » if (bytesLeft == 0) 898 » » » if (bytesLeft == 0)
896 » break; 899 » » » » break;
897 » // Pass chunk data 900 » » » // Pass chunk data
898 » int count = cs.read(buf, 0, Math.min(bytesLeft, buf.length)); 901 » » » int count = cs.read(buf, 0, Math.min(bytesLeft, buf.leng th));
899 » if (count < 0) 902 » » » if (count < 0)
900 » { 903 » » » {
901 » // This shouldn't occur - no final zero chunk 904 » » » » // This shouldn't occur - no final zero chunk
902 » bytesLeft = -1; 905 » » » » bytesLeft = -1;
903 » break; 906 » » » » break;
904 » } 907 » » » }
905 p.write(buf, 0, count); 908 » » p.write(buf, 0, count);
Wladimir Palant 2013/04/05 14:27:09 Mixing tabs and spaces again.
906 » bytesLeft -= count; 909 » » » bytesLeft -= count;
907 » if (bytesLeft == 0) 910 » » » if (bytesLeft == 0)
908 » p.print("\r\n"); 911 » » » » p.print("\r\n");
909 » } 912 » » }
910 » // Pass the trailer 913 » » // Pass the trailer
911 » if (bytesLeft == 0) 914 » » if (bytesLeft == 0)
912 » { 915 » » {
913 » while (true) 916 » » » while (true)
914 » { 917 » » » {
915 » String line = cs.readLine(LINE_LIMIT); 918 » » » » String line = cs.readLine(LINE_LIMIT);
916 » if (line == null) 919 » » » » if (line == null)
917 » break; 920 » » » » » break;
918 » p.print(line + "\r\n"); 921 » » » » p.print(line + "\r\n");
919 » if (line.length() == 0) 922 » » » » if (line.length() == 0)
920 » break; 923 » » » » » break;
921 » } 924 » » » }
922 » } 925 » » }
923 } 926 }
924 927
925 p.flush(); 928 p.flush();
926 } 929 }
927 930
931 /*
932 * Copied (with some amendmends) from UnchunkingInputStream / andrey@adblock plus.org
933 */
928 private int 934 private int
929 getChunkSize(HttpInputStream is) 935 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 936 » throws IOException
931 { 937 {
932 /* 938 » /*
933 * Although HTTP/1.1 chunking spec says that there is one "\r\n" 939 » * Although HTTP/1.1 chunking spec says that there is one "\r\n"
934 * between chunks, some servers (for example, maps.yahoo.com) 940 » * between chunks, some servers (for example, maps.yahoo.com)
935 * send more than one blank line between chunks. So, read and skip 941 » * send more than one blank line between chunks. So, read and skip
936 * all the blank lines seen between chunks. 942 » * all the blank lines seen between chunks.
937 */ 943 » */
938 944
939 int bytesLeft = 0; 945 » int bytesLeft = 0;
940 String line; 946 » String line;
941 do { 947 » do {
942 // Sanity check: limit chars when expecting a chunk size. 948 » » // Sanity check: limit chars when expecting a chunk size.
943 949 » » line = is.readLine(HttpRequest.LINE_LIMIT);
944 line = is.readLine(HttpRequest.LINE_LIMIT); 950 » } while ((line != null) && (line.length() == 0));
Felix Dahlke 2013/04/09 09:16:48 Here's a shorter alternative: while("".equals(line
Andrey Novikov 2013/04/10 08:30:30 This was copied from another part of Brazil, I'll
945 } while ((line != null) && (line.length() == 0)); 951
946 952 » try {
947 try { 953 » » bytesLeft = Integer.parseInt(line.trim(), 16);
948 bytesLeft = Integer.parseInt(line.trim(), 16); 954 » } catch (Exception e) {
949 } catch (Exception e) { 955 » » throw new IOException("malformed chunk");
950 throw new IOException("malformed chunk"); 956 » }
951 } 957 » return bytesLeft;
952 return bytesLeft;
953 } 958 }
954 959
955 void 960 void
956 readStatusLine() 961 readStatusLine()
957 throws IOException 962 throws IOException
958 { 963 {
959 while (true) { 964 while (true) {
960 status = in.readLine(LINE_LIMIT); 965 status = in.readLine(LINE_LIMIT);
961 if (status == null) { 966 if (status == null) {
962 throw new EOFException(); 967 throw new EOFException();
(...skipping 879 matching lines...) Expand 10 before | Expand all | Expand 10 after
1842 return "(null)"; 1847 return "(null)";
1843 } 1848 }
1844 StringBuffer sb = new StringBuffer(); 1849 StringBuffer sb = new StringBuffer();
1845 for (int i = 0; i < idle.size(); i++) { 1850 for (int i = 0; i < idle.size(); i++) {
1846 HttpSocket hs = (HttpSocket) idle.elementAt(i); 1851 HttpSocket hs = (HttpSocket) idle.elementAt(i);
1847 sb.append(hs.toString() + ", "); 1852 sb.append(hs.toString() + ", ");
1848 } 1853 }
1849 return sb.toString(); 1854 return sb.toString();
1850 } 1855 }
1851 } 1856 }
LEFTRIGHT

Powered by Google App Engine
This is Rietveld