• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 android.webkit.cts;
18 
19 import android.os.Parcel;
20 import android.os.Parcelable;
21 import android.util.Pair;
22 
23 import java.util.ArrayList;
24 import java.util.List;
25 
26 /**
27  * This class attempts to match the Apache HttpRequest as close as possible so that it can be used
28  * in place of it. The apache HttpRequest is not parcelable.
29  */
30 public class HttpHeader implements Parcelable {
31     private final String mHeader;
32     private final String mValue;
33 
34     public static final Parcelable.Creator<HttpHeader> CREATOR =
35             new Parcelable.Creator<HttpHeader>() {
36                 public HttpHeader createFromParcel(Parcel in) {
37                     return new HttpHeader(in);
38                 }
39 
40                 public HttpHeader[] newArray(int size) {
41                     return new HttpHeader[size];
42                 }
43             };
44 
45     /** Create a new HttpHeader from a header name and value string. */
create(String header, String value)46     public static HttpHeader create(String header, String value) {
47         return new HttpHeader(header, value);
48     }
49 
50     /** Convert a list of HttpHeaders to a List of pairs to be used with CtsTestServer. */
asPairList(List<HttpHeader> headers)51     public static List<Pair<String, String>> asPairList(List<HttpHeader> headers) {
52         List<Pair<String, String>> pairList = new ArrayList<>();
53         if (headers != null) {
54             for (HttpHeader header : headers) {
55                 pairList.add(header.getPair());
56             }
57         }
58         return pairList;
59     }
60 
HttpHeader(Parcel in)61     HttpHeader(Parcel in) {
62         // Note: This must be read in the same order we write
63         // to the parcel in {@link #wroteToParcel(Parcel out, int flags)}.
64         this(in.readString(), in.readString());
65     }
66 
HttpHeader(String header, String value)67     HttpHeader(String header, String value) {
68         mHeader = header;
69         mValue = value;
70     }
71 
72     @Override
writeToParcel(Parcel out, int flags)73     public void writeToParcel(Parcel out, int flags) {
74         // Note: This must be written in the same order we read
75         // from the parcel in {@link #HttpRequest(Parcel in)}.
76         out.writeString(mHeader);
77         out.writeString(mValue);
78     }
79 
80     @Override
describeContents()81     public int describeContents() {
82         return 0;
83     }
84 
getPair()85     private Pair<String, String> getPair() {
86         return Pair.create(mHeader, mValue);
87     }
88 }
89