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: 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:
View unified diff | Download patch
« no previous file with comments | « src/sunlabs/brazil/server/Request.java ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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
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 * 28 *
29 * 13/04/09-12:44:12 (andrey@adblockplus.org)
30 * implemented proxying chunked request body
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 *
38 * 2.4 03/08/01-16:18:01 (suhler) 41 * 2.4 03/08/01-16:18:01 (suhler)
(...skipping 433 matching lines...) Expand 10 before | Expand all | Expand 10 after
472 */ 475 */
473 public static boolean displayAllHeaders = false; 476 public static boolean displayAllHeaders = false;
474 477
475 ByteArrayOutputStream postData; 478 ByteArrayOutputStream postData;
476 479
477 String uri; 480 String uri;
478 String connectionHeader; 481 String connectionHeader;
479 482
480 HttpInputStream in; 483 HttpInputStream in;
481 InputStream under; 484 InputStream under;
485 HttpInputStream cs;
482 486
483 /** 487 /**
484 * The status line from the HTTP response. This field is not valid until 488 * 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 489 * after <code>connect</code> has been called and the HTTP response has
486 * been read. 490 * been read.
487 */ 491 */
488 public String status; 492 public String status;
489 493
490 /** 494 /**
491 * The headers that were present in the HTTP response. This field is 495 * 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 667 public OutputStream
664 getOutputStream() 668 getOutputStream()
665 throws IOException 669 throws IOException
666 { 670 {
667 if (postData == null) { 671 if (postData == null) {
668 postData = new ByteArrayOutputStream(); 672 postData = new ByteArrayOutputStream();
669 } 673 }
670 return postData; 674 return postData;
671 } 675 }
672 676
677 public void
678 setHttpInputStream(HttpInputStream cs)
679 {
680 this.cs = cs;
681 }
682
673 /** 683 /**
674 * 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
675 * response headers. Any setup routines must be called before the call 685 * 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 686 * to this method, and routines to examine the result must be called after
677 * this method. 687 * this method.
678 * <p> 688 * <p>
679 * 689 *
680 * @throws UnknownHostException 690 * @throws UnknownHostException
681 * if the target host (or proxy) could not be contacted. 691 * if the target host (or proxy) could not be contacted.
682 * 692 *
(...skipping 179 matching lines...) Expand 10 before | Expand all | Expand 10 after
862 872
863 PrintStream p = new PrintStream(hs.out); 873 PrintStream p = new PrintStream(hs.out);
864 p.print(method + " " + uri + " " + version + "\r\n"); 874 p.print(method + " " + uri + " " + version + "\r\n");
865 requestHeaders.print(p); 875 requestHeaders.print(p);
866 p.print("\r\n"); 876 p.print("\r\n");
867 877
868 if (postData != null) { 878 if (postData != null) {
869 postData.writeTo(p); 879 postData.writeTo(p);
870 postData = null; // Release memory. 880 postData = null; // Release memory.
871 } 881 }
882
883 // Pass any data left in client stream (in case of chunked request conte nt)
884 String encoding = requestHeaders.get("Transfer-Encoding", "");
885 if (encoding != null && encoding.equals("chunked") && cs != null)
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.
886 {
887 byte[] buf = new byte[4096];
Felix Dahlke 2013/04/09 09:16:48 Indentation is off here
888 int bytesLeft = -1;
889 while (true)
890 {
891 // Read chunk size
892 if (bytesLeft <= 0)
893 {
894 bytesLeft = getChunkSize(cs);
895 // Output chunk size
896 p.print(Integer.toHexString(bytesLeft) + "\r\n") ;
897 }
898 if (bytesLeft == 0)
899 break;
900 // Pass chunk data
901 int count = cs.read(buf, 0, Math.min(bytesLeft, buf.leng th));
902 if (count < 0)
903 {
904 // This shouldn't occur - no final zero chunk
905 bytesLeft = -1;
906 break;
907 }
908 p.write(buf, 0, count);
909 bytesLeft -= count;
910 if (bytesLeft == 0)
911 p.print("\r\n");
912 }
913 // Pass the trailer
914 if (bytesLeft == 0)
915 {
916 while (true)
917 {
918 String line = cs.readLine(LINE_LIMIT);
919 if (line == null)
920 break;
921 p.print(line + "\r\n");
922 if (line.length() == 0)
923 break;
924 }
925 }
926 }
927
872 p.flush(); 928 p.flush();
873 } 929 }
874 930
931 /*
932 * Copied (with some amendmends) from UnchunkingInputStream / andrey@adblock plus.org
933 */
934 private int
935 getChunkSize(HttpInputStream is)
936 throws IOException
937 {
938 /*
939 * Although HTTP/1.1 chunking spec says that there is one "\r\n"
940 * between chunks, some servers (for example, maps.yahoo.com)
941 * send more than one blank line between chunks. So, read and skip
942 * all the blank lines seen between chunks.
943 */
944
945 int bytesLeft = 0;
946 String line;
947 do {
948 // Sanity check: limit chars when expecting a chunk size.
949 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
951
952 try {
953 bytesLeft = Integer.parseInt(line.trim(), 16);
954 } catch (Exception e) {
955 throw new IOException("malformed chunk");
956 }
957 return bytesLeft;
958 }
959
875 void 960 void
876 readStatusLine() 961 readStatusLine()
877 throws IOException 962 throws IOException
878 { 963 {
879 while (true) { 964 while (true) {
880 status = in.readLine(LINE_LIMIT); 965 status = in.readLine(LINE_LIMIT);
881 if (status == null) { 966 if (status == null) {
882 throw new EOFException(); 967 throw new EOFException();
883 } 968 }
884 if (status.startsWith("HTTP/1.1 100") 969 if (status.startsWith("HTTP/1.1 100")
(...skipping 877 matching lines...) Expand 10 before | Expand all | Expand 10 after
1762 return "(null)"; 1847 return "(null)";
1763 } 1848 }
1764 StringBuffer sb = new StringBuffer(); 1849 StringBuffer sb = new StringBuffer();
1765 for (int i = 0; i < idle.size(); i++) { 1850 for (int i = 0; i < idle.size(); i++) {
1766 HttpSocket hs = (HttpSocket) idle.elementAt(i); 1851 HttpSocket hs = (HttpSocket) idle.elementAt(i);
1767 sb.append(hs.toString() + ", "); 1852 sb.append(hs.toString() + ", ");
1768 } 1853 }
1769 return sb.toString(); 1854 return sb.toString();
1770 } 1855 }
1771 } 1856 }
OLDNEW
« no previous file with comments | « src/sunlabs/brazil/server/Request.java ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld