1 /* 2 * Copyright (C) 2022 The Android Open Source Project 3 * 4 * Licensed under the Apache License, Version 2.0 (the "License"); 5 * you may not use this file except in compliance with the License. 6 * You may obtain a copy of the License at 7 * 8 * http://www.apache.org/licenses/LICENSE-2.0 9 * 10 * Unless required by applicable law or agreed to in writing, software 11 * distributed under the License is distributed on an "AS IS" BASIS, 12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13 * See the License for the specific language governing permissions and 14 * limitations under the License. 15 */ 16 17 package com.android.adservices.service.measurement; 18 19 import androidx.annotation.NonNull; 20 21 import com.android.adservices.service.Flags; 22 import com.android.adservices.service.FlagsFactory; 23 24 import java.io.IOException; 25 import java.net.URL; 26 import java.net.URLConnection; 27 import java.util.Objects; 28 29 /** 30 * Utility class related to network related activities 31 * 32 * @hide 33 */ 34 public class MeasurementHttpClient { 35 36 enum HttpMethod { 37 GET, 38 POST 39 } 40 41 /** 42 * Opens a {@link URLConnection} and sets the network connection & read timeout. The timeout 43 * values are configurable using the name "measurement_network_connect_timeout_ms" and 44 * "measurement_network_read_timeout_ms" 45 */ 46 @NonNull setup(@onNull URL url)47 public URLConnection setup(@NonNull URL url) throws IOException { 48 Objects.requireNonNull(url); 49 50 final URLConnection urlConnection = url.openConnection(); 51 final Flags flags = FlagsFactory.getFlags(); 52 urlConnection.setConnectTimeout(flags.getMeasurementNetworkConnectTimeoutMs()); 53 urlConnection.setReadTimeout(flags.getMeasurementNetworkReadTimeoutMs()); 54 55 // Overriding default headers to avoid leaking information 56 urlConnection.setRequestProperty("User-Agent", ""); 57 58 return urlConnection; 59 } 60 } 61