1 // Copyright 2019 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 package org.chromium.chrome.browser.feed.library.feedrequestmanager;
6 
7 import android.net.Uri;
8 import android.util.Base64;
9 
10 import com.google.protobuf.CodedInputStream;
11 
12 import org.chromium.chrome.browser.feed.library.api.host.network.HttpRequest;
13 import org.chromium.chrome.browser.feed.library.api.host.network.HttpRequest.HttpMethod;
14 
15 import java.io.IOException;
16 import java.util.Collections;
17 
18 /** A class that helps build and sent requests to the server */
19 public final class RequestHelper {
20     public static final String MOTHERSHIP_PARAM_PAYLOAD = "reqpld";
21     public static final String LOCALE_PARAM = "hl";
22     private static final String MOTHERSHIP_PARAM_FORMAT = "fmt";
23     private static final String MOTHERSHIP_VALUE_BINARY = "bin";
24     public static final String PRIORITY_PARAM = "bq";
25     public static final String PRIORITY_VALUE_BACKGROUND = "1";
26     public static final String PRIORITY_VALUE_INTERACTIVE = "0";
27 
RequestHelper()28     private RequestHelper() {}
29 
30     /**
31      * Returns the first length-prefixed value from {@code input}. The first bytes of input are
32      * assumed to be a varint32 encoding the length of the rest of the message. If input contains
33      * more than one message, only the first message is returned.i w
34      *
35      * @throws IOException if input cannot be parsed
36      */
getLengthPrefixedValue(byte[] input)37     static byte[] getLengthPrefixedValue(byte[] input) throws IOException {
38         CodedInputStream codedInputStream = CodedInputStream.newInstance(input);
39         if (codedInputStream.isAtEnd()) {
40             throw new IOException("Empty length-prefixed response");
41         } else {
42             int length = codedInputStream.readRawVarint32();
43             return codedInputStream.readRawBytes(length);
44         }
45     }
46 
buildHttpRequest(String httpMethod, byte[] bytes, String endpoint, String locale, String priorityParamValue)47     static HttpRequest buildHttpRequest(String httpMethod, byte[] bytes, String endpoint,
48             String locale, String priorityParamValue) {
49         boolean isPostMethod = HttpMethod.POST.equals(httpMethod);
50         Uri.Builder uriBuilder = Uri.parse(endpoint).buildUpon();
51         if (!isPostMethod) {
52             uriBuilder.appendQueryParameter(MOTHERSHIP_PARAM_PAYLOAD,
53                     Base64.encodeToString(bytes, Base64.URL_SAFE | Base64.NO_WRAP));
54         }
55 
56         uriBuilder.appendQueryParameter(MOTHERSHIP_PARAM_FORMAT, MOTHERSHIP_VALUE_BINARY);
57         if (!locale.isEmpty()) {
58             uriBuilder.appendQueryParameter(LOCALE_PARAM, locale);
59         }
60         if (!priorityParamValue.isEmpty()) {
61             uriBuilder.appendQueryParameter(PRIORITY_PARAM, priorityParamValue);
62         }
63 
64         return new HttpRequest(uriBuilder.build(), httpMethod, Collections.emptyList(),
65                 isPostMethod ? bytes : new byte[] {});
66     }
67 }
68