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, 7:39 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
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;
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)
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);
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);
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)
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/android/ProxyService.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