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.federatedcompute.services.http;
18 
19 import static com.android.federatedcompute.services.http.HttpClientUtil.CONTENT_LENGTH_HDR;
20 
21 import com.android.federatedcompute.services.http.HttpClientUtil.HttpMethod;
22 
23 import java.util.Map;
24 
25 /** Class to hold FederatedCompute http request. */
26 public final class FederatedComputeHttpRequest {
27     private static final String TAG = "FCPHttpRequest";
28     private static final String HTTPS_SCHEMA = "https://";
29     private static final String LOCAL_HOST_URI = "http://localhost:";
30 
31     private String mUri;
32     private HttpMethod mHttpMethod;
33     private Map<String, String> mExtraHeaders;
34     private byte[] mBody;
35 
FederatedComputeHttpRequest( String uri, HttpMethod httpMethod, Map<String, String> extraHeaders, byte[] body)36     private FederatedComputeHttpRequest(
37             String uri, HttpMethod httpMethod, Map<String, String> extraHeaders, byte[] body) {
38         this.mUri = uri;
39         this.mHttpMethod = httpMethod;
40         this.mExtraHeaders = extraHeaders;
41         this.mBody = body;
42     }
43 
44     /** Creates a {@link FederatedComputeHttpRequest} based on given inputs. */
create( String uri, HttpMethod httpMethod, Map<String, String> extraHeaders, byte[] body)45     public static FederatedComputeHttpRequest create(
46             String uri, HttpMethod httpMethod, Map<String, String> extraHeaders, byte[] body) {
47         if (!uri.startsWith(HTTPS_SCHEMA) && !uri.startsWith(LOCAL_HOST_URI)) {
48             throw new IllegalArgumentException("Non-HTTPS URIs are not supported: " + uri);
49         }
50         if (extraHeaders.containsKey(CONTENT_LENGTH_HDR)) {
51             throw new IllegalArgumentException("Content-Length header should not be provided!");
52         }
53         if (body.length > 0) {
54             if (httpMethod != HttpMethod.POST && httpMethod != HttpMethod.PUT) {
55                 throw new IllegalArgumentException(
56                         "Request method does not allow request mBody: " + httpMethod);
57             }
58             extraHeaders.put(CONTENT_LENGTH_HDR, String.valueOf(body.length));
59         }
60         return new FederatedComputeHttpRequest(uri, httpMethod, extraHeaders, body);
61     }
62 
getUri()63     public String getUri() {
64         return mUri;
65     }
66 
getBody()67     public byte[] getBody() {
68         return mBody;
69     }
70 
getHttpMethod()71     public HttpMethod getHttpMethod() {
72         return mHttpMethod;
73     }
74 
getExtraHeaders()75     public Map<String, String> getExtraHeaders() {
76         return mExtraHeaders;
77     }
78 }
79