Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 package org.chromium.net.urlconnection; | 5 package org.chromium.net.urlconnection; |
| 6 | 6 |
| 7 import android.util.Pair; | 7 import android.util.Pair; |
| 8 | 8 |
| 9 import org.chromium.base.Log; | 9 import org.chromium.base.Log; |
| 10 import org.chromium.net.CronetEngine; | 10 import org.chromium.net.CronetEngine; |
| 11 import org.chromium.net.UrlRequest; | 11 import org.chromium.net.UrlRequest; |
| 12 import org.chromium.net.UrlRequestException; | 12 import org.chromium.net.UrlRequestException; |
| 13 import org.chromium.net.UrlResponseInfo; | 13 import org.chromium.net.UrlResponseInfo; |
| 14 | 14 |
| 15 import java.io.FileNotFoundException; | 15 import java.io.FileNotFoundException; |
| 16 import java.io.IOException; | 16 import java.io.IOException; |
| 17 import java.io.InputStream; | 17 import java.io.InputStream; |
| 18 import java.io.OutputStream; | 18 import java.io.OutputStream; |
| 19 import java.net.HttpURLConnection; | 19 import java.net.HttpURLConnection; |
| 20 import java.net.MalformedURLException; | 20 import java.net.MalformedURLException; |
| 21 import java.net.ProtocolException; | 21 import java.net.ProtocolException; |
| 22 import java.net.URL; | 22 import java.net.URL; |
| 23 import java.nio.ByteBuffer; | 23 import java.nio.ByteBuffer; |
| 24 import java.util.AbstractMap; | |
| 24 import java.util.ArrayList; | 25 import java.util.ArrayList; |
| 25 import java.util.Collections; | 26 import java.util.Collections; |
| 26 import java.util.List; | 27 import java.util.List; |
| 27 import java.util.Map; | 28 import java.util.Map; |
| 28 import java.util.TreeMap; | 29 import java.util.TreeMap; |
| 29 | 30 |
| 30 /** | 31 /** |
| 31 * An implementation of {@link HttpURLConnection} that uses Cronet to send | 32 * An implementation of {@link HttpURLConnection} that uses Cronet to send |
| 32 * requests and receive responses. | 33 * requests and receive responses. |
| 33 * @deprecated use {@link CronetEngine#openConnection}. | 34 * @deprecated use {@link CronetEngine#openConnection}. |
| 34 */ | 35 */ |
| 35 @Deprecated | 36 @Deprecated |
| 36 public class CronetHttpURLConnection extends HttpURLConnection { | 37 public class CronetHttpURLConnection extends HttpURLConnection { |
| 37 private static final String TAG = "cr.CronetHttpURLConn"; | 38 private static final String TAG = "cr.CronetHttpURLConn"; |
| 38 private static final String CONTENT_LENGTH = "Content-Length"; | 39 private static final String CONTENT_LENGTH = "Content-Length"; |
| 39 private final CronetEngine mCronetEngine; | 40 private final CronetEngine mCronetEngine; |
| 40 private final MessageLoop mMessageLoop; | 41 private final MessageLoop mMessageLoop; |
| 41 private UrlRequest mRequest; | 42 private UrlRequest mRequest; |
| 42 private final List<Pair<String, String>> mRequestHeaders; | 43 private final List<Pair<String, String>> mRequestHeaders; |
| 43 | 44 |
| 44 private CronetInputStream mInputStream; | 45 private CronetInputStream mInputStream; |
| 45 private CronetOutputStream mOutputStream; | 46 private CronetOutputStream mOutputStream; |
| 46 private UrlResponseInfo mResponseInfo; | 47 private UrlResponseInfo mResponseInfo; |
| 47 private UrlRequestException mException; | 48 private UrlRequestException mException; |
| 48 private boolean mOnRedirectCalled = false; | 49 private boolean mOnRedirectCalled = false; |
| 49 private boolean mHasResponse = false; | 50 private boolean mHasResponse = false; |
| 51 private List<Map.Entry<String, String>> mResponseHeadersList; | |
| 52 private Map<String, List<String>> mResponseHeadersMap; | |
| 50 | 53 |
| 51 public CronetHttpURLConnection(URL url, CronetEngine cronetEngine) { | 54 public CronetHttpURLConnection(URL url, CronetEngine cronetEngine) { |
| 52 super(url); | 55 super(url); |
| 53 mCronetEngine = cronetEngine; | 56 mCronetEngine = cronetEngine; |
| 54 mMessageLoop = new MessageLoop(); | 57 mMessageLoop = new MessageLoop(); |
| 55 mInputStream = new CronetInputStream(this); | 58 mInputStream = new CronetInputStream(this); |
| 56 mRequestHeaders = new ArrayList<Pair<String, String>>(); | 59 mRequestHeaders = new ArrayList<Pair<String, String>>(); |
| 57 } | 60 } |
| 58 | 61 |
| 59 /** | 62 /** |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 102 /** | 105 /** |
| 103 * Returns an unmodifiable map of the response-header fields and values. | 106 * Returns an unmodifiable map of the response-header fields and values. |
| 104 */ | 107 */ |
| 105 @Override | 108 @Override |
| 106 public Map<String, List<String>> getHeaderFields() { | 109 public Map<String, List<String>> getHeaderFields() { |
| 107 try { | 110 try { |
| 108 getResponse(); | 111 getResponse(); |
| 109 } catch (IOException e) { | 112 } catch (IOException e) { |
| 110 return Collections.emptyMap(); | 113 return Collections.emptyMap(); |
| 111 } | 114 } |
| 112 return mResponseInfo.getAllHeaders(); | 115 return getAllHeaders(); |
| 113 } | 116 } |
| 114 | 117 |
| 115 /** | 118 /** |
| 116 * Returns the value of the named header field. If called on a connection | 119 * Returns the value of the named header field. If called on a connection |
| 117 * that sets the same header multiple times with possibly different values, | 120 * that sets the same header multiple times with possibly different values, |
| 118 * only the last value is returned. | 121 * only the last value is returned. |
| 119 */ | 122 */ |
| 120 @Override | 123 @Override |
| 121 public final String getHeaderField(String fieldName) { | 124 public final String getHeaderField(String fieldName) { |
| 122 try { | 125 try { |
| 123 getResponse(); | 126 getResponse(); |
| 124 } catch (IOException e) { | 127 } catch (IOException e) { |
| 125 return null; | 128 return null; |
| 126 } | 129 } |
| 127 Map<String, List<String>> map = mResponseInfo.getAllHeaders(); | 130 Map<String, List<String>> map = getAllHeaders(); |
| 128 if (!map.containsKey(fieldName)) { | 131 if (!map.containsKey(fieldName)) { |
| 129 return null; | 132 return null; |
| 130 } | 133 } |
| 131 List<String> values = map.get(fieldName); | 134 List<String> values = map.get(fieldName); |
| 132 return values.get(values.size() - 1); | 135 return values.get(values.size() - 1); |
| 133 } | 136 } |
| 134 | 137 |
| 135 /** | 138 /** |
| 136 * Returns the name of the header field at the given position {@code pos}, o r {@code null} | 139 * Returns the name of the header field at the given position {@code pos}, o r {@code null} |
| 137 * if there are fewer than {@code pos} fields. | 140 * if there are fewer than {@code pos} fields. |
| (...skipping 399 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 537 | 540 |
| 538 /** | 541 /** |
| 539 * Helper method to return the response header field at position pos. | 542 * Helper method to return the response header field at position pos. |
| 540 */ | 543 */ |
| 541 private Map.Entry<String, String> getHeaderFieldEntry(int pos) { | 544 private Map.Entry<String, String> getHeaderFieldEntry(int pos) { |
| 542 try { | 545 try { |
| 543 getResponse(); | 546 getResponse(); |
| 544 } catch (IOException e) { | 547 } catch (IOException e) { |
| 545 return null; | 548 return null; |
| 546 } | 549 } |
| 547 List<Map.Entry<String, String>> headers = mResponseInfo.getAllHeadersAsL ist(); | 550 List<Map.Entry<String, String>> headers = getAllHeadersAsList(); |
| 548 if (pos >= headers.size()) { | 551 if (pos >= headers.size()) { |
| 549 return null; | 552 return null; |
| 550 } | 553 } |
| 551 return headers.get(pos); | 554 return headers.get(pos); |
| 552 } | 555 } |
| 553 | 556 |
| 554 /** | 557 /** |
| 555 * Returns whether the client has used {@link #setChunkedStreamingMode} to | 558 * Returns whether the client has used {@link #setChunkedStreamingMode} to |
| 556 * set chunked encoding for upload. | 559 * set chunked encoding for upload. |
| 557 */ | 560 */ |
| 558 private boolean isChunkedUpload() { | 561 private boolean isChunkedUpload() { |
| 559 return chunkLength > 0; | 562 return chunkLength > 0; |
| 560 } | 563 } |
| 564 | |
| 565 // TODO(xunjieli): Refactor to reuse code in UrlRequestInfo. | |
|
kapishnikov
2016/03/08 19:37:17
Did you mean UrlResponseInfo?
xunjieli
2016/03/08 19:51:14
Done.
| |
| 566 private Map<String, List<String>> getAllHeaders() { | |
| 567 if (mResponseHeadersMap != null) { | |
| 568 return mResponseHeadersMap; | |
| 569 } | |
| 570 Map<String, List<String>> map = new TreeMap<>(String.CASE_INSENSITIVE_OR DER); | |
| 571 for (Map.Entry<String, String> entry : getAllHeadersAsList()) { | |
| 572 List<String> values = new ArrayList<String>(); | |
| 573 if (map.containsKey(entry.getKey())) { | |
| 574 values.addAll(map.get(entry.getKey())); | |
| 575 } | |
| 576 values.add(entry.getValue()); | |
| 577 map.put(entry.getKey(), Collections.unmodifiableList(values)); | |
| 578 } | |
| 579 mResponseHeadersMap = Collections.unmodifiableMap(map); | |
| 580 return mResponseHeadersMap; | |
| 581 } | |
| 582 | |
| 583 private List<Map.Entry<String, String>> getAllHeadersAsList() { | |
| 584 if (mResponseHeadersList != null) { | |
| 585 return mResponseHeadersList; | |
| 586 } | |
| 587 mResponseHeadersList = new ArrayList<Map.Entry<String, String>>(); | |
| 588 for (Map.Entry<String, String> entry : mResponseInfo.getAllHeadersAsList ()) { | |
| 589 // Strips Content-Encoding response header. See crbug.com/592700. | |
| 590 if (!entry.getKey().equalsIgnoreCase("Content-Encoding")) { | |
| 591 mResponseHeadersList.add( | |
| 592 new AbstractMap.SimpleImmutableEntry<String, String>(ent ry)); | |
| 593 } | |
| 594 } | |
| 595 mResponseHeadersList = Collections.unmodifiableList(mResponseHeadersList ); | |
| 596 return mResponseHeadersList; | |
| 597 } | |
| 561 } | 598 } |
| OLD | NEW |