1 /* 2 * Copyright (C) 2023 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.common.bhttp; 18 19 import static com.android.adservices.service.common.bhttp.Frc9000VariableLengthIntegerUtil.toFrc9000Int; 20 21 import android.annotation.NonNull; 22 23 import com.google.auto.value.AutoValue; 24 25 import java.nio.charset.StandardCharsets; 26 27 /** 28 * The control data for a request message contains the method and request target. That information 29 * is encoded as an ordered sequence of fields: Method, Scheme, Authority, Path. Each of these 30 * fields is prefixed with a length. 31 * 32 * @see <a 33 * href="https://www.ietf.org/archive/id/draft-ietf-httpbis-binary-message-06.html#name-request-control-data">Binary 34 * HTTP Request Control Data</a> 35 */ 36 @AutoValue 37 public abstract class RequestControlData extends ControlData { 38 private static final int SECTIONS_COUNT = 8; 39 40 /** Returns the method of the request. */ 41 @NonNull getMethod()42 public abstract String getMethod(); 43 44 /** Returns the scheme of the request. */ 45 @NonNull getScheme()46 public abstract String getScheme(); 47 48 /** Returns the authority of the request. */ 49 @NonNull getAuthority()50 public abstract String getAuthority(); 51 52 /** Returns the path of the request. */ 53 @NonNull getPath()54 public abstract String getPath(); 55 56 @Override getKnownLengthSerializedSectionsCount()57 int getKnownLengthSerializedSectionsCount() { 58 return SECTIONS_COUNT; 59 } 60 61 /** 62 * {@inheritDoc} 63 * 64 * @return [method][scheme][authority][path] 65 */ 66 @Override 67 @NonNull knownLengthSerialize()68 byte[][] knownLengthSerialize() { 69 byte[] method = getMethod().getBytes(StandardCharsets.UTF_8); 70 byte[] scheme = getScheme().getBytes(StandardCharsets.UTF_8); 71 byte[] authority = getAuthority().getBytes(StandardCharsets.UTF_8); 72 byte[] path = getPath().getBytes(StandardCharsets.UTF_8); 73 74 return new byte[][] { 75 toFrc9000Int(method.length), 76 method, 77 toFrc9000Int(scheme.length), 78 scheme, 79 toFrc9000Int(authority.length), 80 authority, 81 toFrc9000Int(path.length), 82 path 83 }; 84 } 85 86 /** Get a builder for request control data. */ 87 @NonNull builder()88 public static Builder builder() { 89 return new AutoValue_RequestControlData.Builder(); 90 } 91 92 /** Builder for {@link RequestControlData}. */ 93 @AutoValue.Builder 94 public abstract static class Builder { 95 /** Sets the method of the request. */ 96 @NonNull setMethod(@onNull String value)97 public abstract Builder setMethod(@NonNull String value); 98 99 /** Sets the scheme of the request. */ 100 @NonNull setScheme(@onNull String value)101 public abstract Builder setScheme(@NonNull String value); 102 103 /** Sets the authority of the request. */ 104 @NonNull setAuthority(@onNull String value)105 public abstract Builder setAuthority(@NonNull String value); 106 107 /** Sets the path of the request. */ 108 @NonNull setPath(@onNull String value)109 public abstract Builder setPath(@NonNull String value); 110 111 /** Returns the request control data built. */ 112 @NonNull build()113 public abstract RequestControlData build(); 114 } 115 } 116