diff options
Diffstat (limited to 'java/com/android/libraries/entitlement/http')
3 files changed, 33 insertions, 34 deletions
diff --git a/java/com/android/libraries/entitlement/http/HttpClient.java b/java/com/android/libraries/entitlement/http/HttpClient.java index 76eeaec..9ccb5ee 100644 --- a/java/com/android/libraries/entitlement/http/HttpClient.java +++ b/java/com/android/libraries/entitlement/http/HttpClient.java @@ -37,6 +37,7 @@ import com.android.libraries.entitlement.ServiceEntitlementException; import com.android.libraries.entitlement.http.HttpConstants.ContentType; import com.android.libraries.entitlement.utils.StreamUtils; +import com.google.common.collect.ImmutableList; import com.google.common.net.HttpHeaders; import java.io.DataOutputStream; @@ -46,6 +47,7 @@ import java.io.OutputStream; import java.net.HttpURLConnection; import java.net.URL; import java.net.URLConnection; +import java.util.List; import java.util.Map; /** Implement the HTTP request method according to TS.43 specification. */ @@ -94,7 +96,7 @@ public class HttpClient { } // add HTTP headers - for (Map.Entry<String, String> entry : request.requestProperties().entrySet()) { + for (Map.Entry<String, String> entry : request.requestProperties().entries()) { mConnection.addRequestProperty(entry.getKey(), entry.getValue()); } @@ -136,7 +138,7 @@ public class HttpClient { throw new ServiceEntitlementException( ERROR_HTTP_STATUS_NOT_SUCCESS, "Read response code failed!", e); } - responseBuilder.setCookie(nullToEmpty(getCookie(connection))); + responseBuilder.setCookies(getCookies(connection)); try { String responseBody = readResponse(connection); logPii("HttpClient.response body: " + responseBody); @@ -171,7 +173,8 @@ public class HttpClient { return ContentType.UNKNOWN; } - private static String getCookie(URLConnection connection) { - return connection.getHeaderField(HttpHeaders.SET_COOKIE); + private static List<String> getCookies(URLConnection connection) { + List<String> cookies = connection.getHeaderFields().get(HttpHeaders.SET_COOKIE); + return cookies == null ? ImmutableList.of() : cookies; } } diff --git a/java/com/android/libraries/entitlement/http/HttpRequest.java b/java/com/android/libraries/entitlement/http/HttpRequest.java index af567db..b6cd771 100644 --- a/java/com/android/libraries/entitlement/http/HttpRequest.java +++ b/java/com/android/libraries/entitlement/http/HttpRequest.java @@ -17,18 +17,17 @@ package com.android.libraries.entitlement.http; import android.net.Network; -import android.util.ArrayMap; import androidx.annotation.Nullable; import com.android.libraries.entitlement.CarrierConfig; import com.google.auto.value.AutoValue; -import com.google.common.collect.ImmutableMap; +import com.google.common.collect.ImmutableListMultimap; import org.json.JSONObject; -import java.util.Map; +import java.util.List; /** The parameters of an http request. */ @AutoValue @@ -42,11 +41,8 @@ public abstract class HttpRequest { /** For "POST" request method, the body of the request in JSON format. */ public abstract JSONObject postData(); - /** For "GET" request method, the parameters to be encoded into the URL. */ - public abstract ImmutableMap<String, String> requestValues(); - /** HTTP header fields. */ - public abstract ImmutableMap<String, String> requestProperties(); + public abstract ImmutableListMultimap<String, String> requestProperties(); /** The client side timeout, in seconds. See {@link Builder#setTimeoutInSec}. */ public abstract int timeoutInSec(); @@ -58,9 +54,6 @@ public abstract class HttpRequest { /** Builder of {@link HttpRequest}. */ @AutoValue.Builder public abstract static class Builder { - private final Map<String, String> values = new ArrayMap<>(); - private final Map<String, String> properties = new ArrayMap<>(); - public abstract HttpRequest build(); /** Sets the URL. */ @@ -76,20 +69,22 @@ public abstract class HttpRequest { /** For "POST" request method, sets the body of the request in JSON format. */ public abstract Builder setPostData(JSONObject postData); - abstract Builder setRequestValues(ImmutableMap<String, String> value); - - abstract Builder setRequestProperties(ImmutableMap<String, String> properties); - - /** For "GET" request method, adds a parameter to be encoded into the URL. */ - public Builder addRequestValues(String key, String value) { - values.put(key, value); - return this.setRequestValues(ImmutableMap.copyOf(values)); - } + abstract ImmutableListMultimap.Builder<String, String> requestPropertiesBuilder(); /** Adds an HTTP header field. */ public Builder addRequestProperty(String key, String value) { - properties.put(key, value); - return this.setRequestProperties(ImmutableMap.copyOf(properties)); + requestPropertiesBuilder().put(key, value); + return this; + } + + /** + * Adds an HTTP header field with multiple values. Equivalent to calling + * {@link #addRequestProperty(String, String)} multiple times with the same key and + * one value at a time. + */ + public Builder addRequestProperty(String key, List<String> value) { + requestPropertiesBuilder().putAll(key, value); + return this; } /** @@ -113,8 +108,6 @@ public abstract class HttpRequest { .setUrl("") .setRequestMethod("") .setPostData(new JSONObject()) - .setRequestValues(ImmutableMap.of()) - .setRequestProperties(ImmutableMap.of()) .setTimeoutInSec(CarrierConfig.DEFAULT_TIMEOUT_IN_SEC); } } diff --git a/java/com/android/libraries/entitlement/http/HttpResponse.java b/java/com/android/libraries/entitlement/http/HttpResponse.java index e331b99..f495578 100644 --- a/java/com/android/libraries/entitlement/http/HttpResponse.java +++ b/java/com/android/libraries/entitlement/http/HttpResponse.java @@ -19,6 +19,9 @@ package com.android.libraries.entitlement.http; import com.android.libraries.entitlement.http.HttpConstants.ContentType; import com.google.auto.value.AutoValue; +import com.google.common.collect.ImmutableList; + +import java.util.List; /** * The response of the http request. @@ -39,7 +42,7 @@ public abstract class HttpResponse { /** * Content of the "Set-Cookie" response header. */ - public abstract String cookie(); + public abstract ImmutableList<String> cookies(); /** * Builder of {@link HttpResponse}. @@ -57,9 +60,9 @@ public abstract class HttpResponse { public abstract Builder setResponseMessage(String responseMessage); /** - * Content of the "Set-Cookie" response header. + * Sets the content of the "Set-Cookie" response headers. */ - public abstract Builder setCookie(String cookie); + public abstract Builder setCookies(List<String> cookies); } public static Builder builder() { @@ -68,7 +71,7 @@ public abstract class HttpResponse { .setBody("") .setResponseCode(0) .setResponseMessage("") - .setCookie(""); + .setCookies(ImmutableList.of()); } @Override @@ -83,9 +86,9 @@ public abstract class HttpResponse { .append(responseCode()) .append(" responseMessage=") .append(responseMessage()) - .append(" cookie=(") - .append(cookie().length()) - .append(" characters)}") + .append(" cookies=[") + .append(cookies().size()) + .append(" cookies]}") .toString(); } } |