OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2014 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 package org.chromium.net; | |
6 | |
7 import java.io.IOException; | |
8 import java.nio.ByteBuffer; | |
9 import java.nio.channels.WritableByteChannel; | |
10 import java.util.Map; | |
11 | |
12 /** | |
13 * Network request using the native http stack implementation. | |
14 */ | |
15 class ChromiumUrlRequest extends UrlRequest implements HttpUrlRequest { | |
16 | |
17 private final HttpUrlRequestListener mListener; | |
18 private boolean mBufferFullResponse; | |
19 private long mOffset; | |
dplotnikov
2014/03/07 20:01:12
Do you believe we should keep this resumable downl
mef
2014/03/07 20:54:12
Good question. At this point my primary goal was t
| |
20 private long mContentLength; | |
21 private long mContentLengthLimit; | |
22 private boolean mCancelIfContentLengthOverLimit; | |
23 private boolean mContentLengthOverLimit; | |
24 private boolean mSkippingToOffset; | |
25 private long mSize; | |
mmenke
2014/03/07 17:02:39
Entire file should be using 4-space indent (Goes f
mef
2014/03/07 20:54:12
Yeah, is there some tool for that? 'git cl format'
mef
2014/03/14 20:03:24
Done.
| |
26 | |
27 public ChromiumUrlRequest(ChromiumUrlRequestContext requestContext, String url , | |
mmenke
2014/03/07 17:02:39
Line too long (Goes for a bunch of lines in these
mef
2014/03/07 20:54:12
Roger that.
| |
28 int priority, Map<String, String> headers, HttpUrlRequestListener listener ) { | |
mmenke
2014/03/07 17:02:39
All lines with arguments should be lined up.First
| |
29 this(requestContext, url, priority, headers, new ChunkedWritableByteChannel( ), listener); | |
30 mBufferFullResponse = true; | |
31 } | |
32 | |
33 public ChromiumUrlRequest(ChromiumUrlRequestContext requestContext, String url , | |
34 int priority, Map<String, String> headers, WritableByteChannel sink, | |
35 HttpUrlRequestListener listener) { | |
36 super(requestContext, url, convertRequestPriority(priority), headers, sink); | |
37 if (requestContext == null) { | |
38 throw new NullPointerException("Context is required"); | |
39 } | |
40 if (url == null) { | |
41 throw new NullPointerException("URL is required"); | |
42 } | |
43 mListener = listener; | |
44 } | |
45 | |
46 private static int convertRequestPriority(int priority) { | |
47 switch (priority) { | |
48 case HttpUrlRequest.REQUEST_PRIORITY_IDLE: | |
49 return UrlRequest.REQUEST_PRIORITY_IDLE; | |
50 case HttpUrlRequest.REQUEST_PRIORITY_LOWEST: | |
51 return UrlRequest.REQUEST_PRIORITY_LOWEST; | |
52 case HttpUrlRequest.REQUEST_PRIORITY_LOW: | |
53 return UrlRequest.REQUEST_PRIORITY_LOW; | |
54 case HttpUrlRequest.REQUEST_PRIORITY_MEDIUM: | |
55 return UrlRequest.REQUEST_PRIORITY_MEDIUM; | |
56 case HttpUrlRequest.REQUEST_PRIORITY_HIGHEST: | |
57 return UrlRequest.REQUEST_PRIORITY_HIGHEST; | |
58 default: | |
59 return UrlRequest.REQUEST_PRIORITY_MEDIUM; | |
60 } | |
61 } | |
62 | |
63 @Override | |
64 public void setOffset(long offset) { | |
65 mOffset = offset; | |
66 if (offset != 0) { | |
67 addHeader("Range", "bytes=" + offset + "-"); | |
68 } | |
69 } | |
70 | |
71 @Override | |
72 public long getContentLength() { | |
73 return mContentLength; | |
74 } | |
75 | |
76 @Override | |
77 public void setContentLengthLimit(long limit, boolean cancelEarly) { | |
78 mContentLengthLimit = limit; | |
79 mCancelIfContentLengthOverLimit = cancelEarly; | |
80 } | |
81 | |
82 @Override | |
83 protected void onResponseStarted() { | |
84 super.onResponseStarted(); | |
85 | |
86 mContentLength = super.getContentLength(); | |
87 if (mContentLengthLimit > 0 && mContentLength > mContentLengthLimit | |
88 && mCancelIfContentLengthOverLimit) { | |
89 onContentLengthOverLimit(); | |
90 return; | |
91 } | |
92 | |
93 if (mBufferFullResponse && mContentLength != -1 && !mContentLengthOverLimit) { | |
94 ((ChunkedWritableByteChannel) getSink()).setCapacity((int) mContentLength) ; | |
95 } | |
96 | |
97 if (mOffset != 0) { | |
98 // The server may ignore the request for a byte range | |
mmenke
2014/03/07 17:02:39
Suggestion adding a period. Android style guide d
mef
2014/03/07 20:54:12
Done. Agreed.
| |
99 if (super.getHttpStatusCode() == 200) { | |
100 if (mContentLength != -1) { | |
101 mContentLength -= mOffset; | |
102 } | |
103 mSkippingToOffset = true; | |
104 } else { | |
105 mSize = mOffset; | |
106 } | |
107 } | |
108 } | |
109 | |
110 @Override | |
111 protected void onBytesRead(ByteBuffer buffer) { | |
112 if (mContentLengthOverLimit) { | |
113 return; | |
114 } | |
115 | |
116 int size = buffer.remaining(); | |
117 mSize += size; | |
118 if (mSkippingToOffset) { | |
119 if (mSize <= mOffset) { | |
120 return; | |
121 } else { | |
122 mSkippingToOffset = false; | |
123 buffer.position((int) (mOffset - (mSize - size))); | |
124 } | |
125 } | |
126 | |
127 if (mContentLengthLimit != 0 && mSize > mContentLengthLimit) { | |
128 buffer.limit(size - (int) (mSize - mContentLengthLimit)); | |
129 super.onBytesRead(buffer); | |
130 onContentLengthOverLimit(); | |
131 return; | |
132 } | |
133 | |
134 super.onBytesRead(buffer); | |
135 } | |
136 | |
137 private void onContentLengthOverLimit() { | |
138 mContentLengthOverLimit = true; | |
139 cancel(); | |
140 } | |
141 | |
142 @Override | |
143 protected void onRequestComplete() { | |
144 mListener.onRequestComplete(this); | |
145 } | |
146 | |
147 @Override | |
148 public int getHttpStatusCode() { | |
149 int httpStatusCode = super.getHttpStatusCode(); | |
150 | |
151 // If we have been able to successfully resume a previously interrupted down load, | |
152 // the status code will be 206, not 200. Since the rest of the application i s | |
153 // expecting 200 to indicate success, we need to fake it. | |
mmenke
2014/03/07 17:02:39
Add a TODO about investigating this - I'm not sure
mef
2014/03/07 20:54:12
Done.
| |
154 if (httpStatusCode == 206) { | |
155 httpStatusCode = 200; | |
156 } | |
157 return httpStatusCode; | |
158 } | |
159 | |
160 @Override | |
161 public IOException getException() { | |
162 IOException ex = super.getException(); | |
163 if (ex == null && mContentLengthOverLimit) { | |
164 ex = new ResponseTooLargeException(); | |
165 } | |
166 return ex; | |
167 } | |
168 | |
169 @Override | |
170 public ByteBuffer getByteBuffer() { | |
171 return ((ChunkedWritableByteChannel) getSink()).getByteBuffer(); | |
172 } | |
173 | |
174 @Override | |
175 public byte[] getResponseAsBytes() { | |
176 return ((ChunkedWritableByteChannel) getSink()).getBytes(); | |
177 } | |
178 } | |
OLD | NEW |