Index: components/cronet/android/java/src/org/chromium/net/UrlRequest.java |
diff --git a/components/cronet/android/java/src/org/chromium/net/UrlRequest.java b/components/cronet/android/java/src/org/chromium/net/UrlRequest.java |
index 3b736cbfdbe0e62c0f750886bd2e4dc009d4ee69..da00fdd9a61fb912d17a3ca4e849019bc9aab927 100644 |
--- a/components/cronet/android/java/src/org/chromium/net/UrlRequest.java |
+++ b/components/cronet/android/java/src/org/chromium/net/UrlRequest.java |
@@ -44,6 +44,7 @@ public class UrlRequest { |
private volatile boolean mCanceled; |
private volatile boolean mRecycled; |
private volatile boolean mFinished; |
+ private boolean mHeadersAvailable; |
private String mContentType; |
private long mContentLength; |
private Semaphore mAppendChunkSemaphore; |
@@ -137,36 +138,46 @@ public class UrlRequest { |
} |
public void start() { |
- synchronized (mLock) { |
- if (mCanceled) { |
- return; |
- } |
+ try { |
+ synchronized (mLock) { |
+ if (mCanceled) { |
+ return; |
+ } |
- validateNotStarted(); |
- validateNotRecycled(); |
+ validateNotStarted(); |
+ validateNotRecycled(); |
- mStarted = true; |
+ mStarted = true; |
- if (mHeaders != null && !mHeaders.isEmpty()) { |
- for (Entry<String, String> entry : mHeaders.entrySet()) { |
- nativeAddHeader(mUrlRequestPeer, entry.getKey(), |
- entry.getValue()); |
+ if (mHeaders != null && !mHeaders.isEmpty()) { |
+ for (Entry<String, String> entry : mHeaders.entrySet()) { |
+ nativeAddHeader(mUrlRequestPeer, entry.getKey(), |
+ entry.getValue()); |
+ } |
} |
- } |
- if (mAdditionalHeaders != null && !mAdditionalHeaders.isEmpty()) { |
- for (Entry<String, String> entry : |
- mAdditionalHeaders.entrySet()) { |
- nativeAddHeader(mUrlRequestPeer, entry.getKey(), |
- entry.getValue()); |
+ if (mAdditionalHeaders != null && !mAdditionalHeaders.isEmpty()) { |
+ for (Entry<String, String> entry : |
+ mAdditionalHeaders.entrySet()) { |
+ nativeAddHeader(mUrlRequestPeer, entry.getKey(), |
+ entry.getValue()); |
+ } |
} |
- } |
- nativeStart(mUrlRequestPeer); |
- } |
+ nativeStart(mUrlRequestPeer); |
+ } |
- if (mPostBodyChannel != null) { |
- uploadFromChannel(mPostBodyChannel); |
+ if (mPostBodyChannel != null) { |
+ uploadFromChannel(mPostBodyChannel); |
+ } |
+ } finally { |
+ if (mPostBodyChannel != null) { |
+ try { |
+ mPostBodyChannel.close(); |
+ } catch (IOException e) { |
+ // Ignore |
+ } |
+ } |
} |
} |
@@ -217,12 +228,6 @@ public class UrlRequest { |
} catch (InterruptedException e) { |
mSinkException = new IOException(e); |
cancel(); |
- } finally { |
- try { |
- mPostBodyChannel.close(); |
- } catch (IOException ignore) { |
- ; |
- } |
} |
} |
@@ -303,6 +308,11 @@ public class UrlRequest { |
return mContentType; |
} |
+ public String getHeader(String name) { |
+ validateHeadersAvailable(); |
+ return nativeGetHeader(mUrlRequestPeer, name); |
+ } |
+ |
/** |
* A callback invoked when appending a chunk to the request has completed. |
*/ |
@@ -318,6 +328,7 @@ public class UrlRequest { |
protected void onResponseStarted() { |
mContentType = nativeGetContentType(mUrlRequestPeer); |
mContentLength = nativeGetContentLength(mUrlRequestPeer); |
+ mHeadersAvailable = true; |
} |
/** |
@@ -390,6 +401,13 @@ public class UrlRequest { |
} |
} |
+ |
+ private void validateHeadersAvailable() { |
+ if (!mHeadersAvailable) { |
+ throw new IllegalStateException("Response headers not available"); |
+ } |
+ } |
+ |
public String getUrl() { |
return mUrl; |
} |
@@ -424,4 +442,6 @@ public class UrlRequest { |
private native String nativeGetContentType(long urlRequestPeer); |
private native long nativeGetContentLength(long urlRequestPeer); |
+ |
+ private native String nativeGetHeader(long urlRequestPeer, String name); |
} |