Welcome to mirror list, hosted at ThFree Co, Russian Federation.

HttpClient.java « util « mapswithme « com « src « android - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: 6ae7d455fbdafb4b6e5ff5c62d464409b4ea5bde (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*******************************************************************************
 * The MIT License (MIT)
 * <p/>
 * Copyright (c) 2014 Alexander Zolotarev <me@alex.bio> from Minsk, Belarus
 * <p/>
 * Permission is hereby granted, free of charge, to any person obtaining a copy
 * of this software and associated documentation files (the "Software"), to deal
 * in the Software without restriction, including without limitation the rights
 * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
 * copies of the Software, and to permit persons to whom the Software is
 * furnished to do so, subject to the following conditions:
 * <p/>
 * The above copyright notice and this permission notice shall be included in all
 * copies or substantial portions of the Software.
 * <p/>
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
 * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
 * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
 * SOFTWARE.
 *******************************************************************************/

package com.mapswithme.util;

import android.os.Build;
import android.support.annotation.NonNull;
import android.text.TextUtils;

import com.mapswithme.util.log.Logger;
import com.mapswithme.util.log.LoggerFactory;

import javax.net.ssl.HttpsURLConnection;
import javax.net.ssl.SSLSocketFactory;
import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.ByteArrayOutputStream;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.zip.GZIPInputStream;
import java.util.zip.InflaterInputStream;

public final class HttpClient
{
  public static final String HEADER_USER_AGENT = "User-Agent";
  public static final String HEADER_AUTHORIZATION = "Authorization";
  public static final String HEADER_BEARER_PREFFIX = "Bearer ";
  public static final String HEADER_BUNDLE_TIERS = "X-Mapsme-Bundle-Tiers";
  public static final String HEADER_DEVICE_ID = "X-Mapsme-Device-Id";
  private final static String TAG = HttpClient.class.getSimpleName();
  // TODO(AlexZ): tune for larger files
  private final static int STREAM_BUFFER_SIZE = 1024 * 64;
  private static final Logger LOGGER = LoggerFactory.INSTANCE.getLogger(LoggerFactory.Type.NETWORK);

  public static Params run(@NonNull final Params p) throws IOException, NullPointerException
  {
    if (TextUtils.isEmpty(p.httpMethod))
      throw new IllegalArgumentException("Please set valid HTTP method for request at Params.httpMethod field.");

    HttpURLConnection connection = null;

    LOGGER.d(TAG, "Connecting to " + Utils.makeUrlSafe(p.url));

    try
    {
      connection = (HttpURLConnection) new URL(p.url).openConnection();
      setupTLSForPreLollipop(connection);

      // NullPointerException, MalformedUrlException, IOException
      // Redirects from http to https or vice versa are not supported by Android implementation.
      // There is also a nasty bug on Androids before 4.4:
      // if you send any request with Content-Length set, and it is redirected, and your instance is set to automatically follow redirects,
      // then next (internal) GET request to redirected location will incorrectly have have all headers set from the previous request,
      // including Content-Length, Content-Type etc. This leads to unexpected hangs and timeout errors, because some servers are
      // correctly trying to wait for the body if Content-Length is set.
      // It shows in logs like this:
      //
      // java.net.SocketTimeoutException: Read timed out
      //   at org.apache.harmony.xnet.provider.jsse.NativeCrypto.SSL_read(Native Method)
      //   at org.apache.harmony.xnet.provider.jsse.OpenSSLSocketImpl$SSLInputStream.read(OpenSSLSocketImpl.java:687)
      //   ...
      //
      // Looks like this bug was fixed by switching to OkHttp implementation in this commit:
      // https://android.googlesource.com/platform/libcore/+/2503556d17b28c7b4e6e514540a77df1627857d0
      connection.setInstanceFollowRedirects(p.followRedirects);
      connection.setConnectTimeout(p.timeoutMillisec);
      connection.setReadTimeout(p.timeoutMillisec);
      connection.setUseCaches(false);
      connection.setRequestMethod(p.httpMethod);

      if (!TextUtils.isEmpty(p.cookies))
        connection.setRequestProperty("Cookie", p.cookies);

      for (KeyValue header : p.headers)
      {
        connection.setRequestProperty(header.mKey, header.mValue);
      }

      if (!TextUtils.isEmpty(p.inputFilePath) || p.data != null)
      {
        // Send (POST, PUT...) data to the server.
        if (TextUtils.isEmpty(connection.getRequestProperty("Content-Type")))
          throw new NullPointerException("Please set Content-Type for request.");

        // Work-around for situation when more than one consequent POST requests can lead to stable
        // "java.net.ProtocolException: Unexpected status line:" on a client and Nginx HTTP 499 errors.
        // The only found reference to this bug is http://stackoverflow.com/a/24303115/1209392
        connection.setRequestProperty("Connection", "close");
        connection.setDoOutput(true);
        if (p.data != null)
        {
          connection.setFixedLengthStreamingMode(p.data.length);
          final OutputStream os = connection.getOutputStream();
          try
          {
            os.write(p.data);
          }
          finally
          {
            os.close();
          }
          LOGGER.d(TAG, "Sent " + p.httpMethod + " with content of size " + p.data.length);
        }
        else
        {
          final File file = new File(p.inputFilePath);
          connection.setFixedLengthStreamingMode((int) file.length());
          final BufferedInputStream istream = new BufferedInputStream(new FileInputStream(file), STREAM_BUFFER_SIZE);
          final BufferedOutputStream ostream = new BufferedOutputStream(connection.getOutputStream(), STREAM_BUFFER_SIZE);
          final byte[] buffer = new byte[STREAM_BUFFER_SIZE];
          int bytesRead;
          while ((bytesRead = istream.read(buffer, 0, STREAM_BUFFER_SIZE)) > 0)
          {
            ostream.write(buffer, 0, bytesRead);
          }
          istream.close(); // IOException
          ostream.close(); // IOException
          LOGGER.d(TAG, "Sent " + p.httpMethod + " with file of size " + file.length());
        }
      }
      // GET data from the server or receive response body
      p.httpResponseCode = connection.getResponseCode();
      LOGGER.d(TAG, "Received HTTP " + p.httpResponseCode + " from server, content encoding = "
                    + connection.getContentEncoding() + ", for request = " + Utils.makeUrlSafe(p.url));

      if (p.httpResponseCode >= 300 && p.httpResponseCode < 400)
        p.receivedUrl = connection.getHeaderField("Location");
      else
        p.receivedUrl = connection.getURL().toString();

      p.headers.clear();
      if (p.loadHeaders)
      {
        for (Map.Entry<String, List<String>> header : connection.getHeaderFields().entrySet())
        {
          // Some implementations include a mapping for the null key.
          if (header.getKey() == null || header.getValue() == null)
            continue;

          p.headers.add(new KeyValue(header.getKey().toLowerCase(), TextUtils.join(", ", header.getValue())));
        }
      }
      else
      {
        List<String> cookies = connection.getHeaderFields().get("Set-Cookie");
        if (cookies != null)
          p.headers.add(new KeyValue("Set-Cookie", TextUtils.join(", ", cookies)));
      }

      OutputStream ostream;
      if (!TextUtils.isEmpty(p.outputFilePath))
        ostream = new BufferedOutputStream(new FileOutputStream(p.outputFilePath), STREAM_BUFFER_SIZE);
      else
        ostream = new ByteArrayOutputStream(STREAM_BUFFER_SIZE);
      // TODO(AlexZ): Add HTTP resume support in the future for partially downloaded files
      final BufferedInputStream istream = new BufferedInputStream(getInputStream(connection), STREAM_BUFFER_SIZE);
      final byte[] buffer = new byte[STREAM_BUFFER_SIZE];
      // gzip encoding is transparently enabled and we can't use Content-Length for
      // body reading if server has gzipped it.
      int bytesRead;
      while ((bytesRead = istream.read(buffer, 0, STREAM_BUFFER_SIZE)) > 0)
      {
        // Read everything if Content-Length is not known in advance.
        ostream.write(buffer, 0, bytesRead);
      }
      istream.close(); // IOException
      ostream.close(); // IOException
      if (ostream instanceof ByteArrayOutputStream)
        p.data = ((ByteArrayOutputStream) ostream).toByteArray();
    }
    finally
    {
      if (connection != null)
        connection.disconnect();
    }
    return p;
  }

  private static void setupTLSForPreLollipop(@NonNull HttpURLConnection connection)
  {
    // On PreLollipop devices we use the custom ssl factory which enables TLSv1.2 forcibly, because
    // TLS of the mentioned version is not enabled by default on PreLollipop devices, but some of
    // used by us APIs (as Viator) requires TLSv1.2. For more info see
    // https://developer.android.com/reference/javax/net/ssl/SSLEngine.html.
    if ((connection instanceof HttpsURLConnection)
        && Build.VERSION.SDK_INT < Build.VERSION_CODES.LOLLIPOP)
    {
      HttpsURLConnection sslConnection = (HttpsURLConnection) connection;
      SSLSocketFactory factory = sslConnection.getSSLSocketFactory();
      sslConnection.setSSLSocketFactory(new PreLollipopSSLSocketFactory(factory));
    }
  }

  @NonNull
  private static InputStream getInputStream(@NonNull HttpURLConnection connection) throws IOException
  {
    InputStream in;
    try
    {
      if ("gzip".equals(connection.getContentEncoding()))
        in = new GZIPInputStream(connection.getInputStream());
      else if ("deflate".equals(connection.getContentEncoding()))
        in = new InflaterInputStream(connection.getInputStream());
      else
        in = connection.getInputStream();
    }
    catch (IOException e)
    {
      in = connection.getErrorStream();
      if (in == null)
        throw e;
    }
    return in;
  }

  private static class Params
  {
    public void setHeaders(@NonNull KeyValue[] array)
    {
      headers = new ArrayList<>(Arrays.asList(array));
    }

    public Object[] getHeaders()
    {
      return headers.toArray();
    }

    public String url;
    // Can be different from url in case of redirects.
    String receivedUrl;
    String httpMethod;
    // Should be specified for any request whose method allows non-empty body.
    // On return, contains received Content-Type or null.
    // Can be specified for any request whose method allows non-empty body.
    // On return, contains received Content-Encoding or null.
    public byte[] data;
    // Send from input file if specified instead of data.
    String inputFilePath;
    // Received data is stored here if not null or in data otherwise.
    String outputFilePath;
    String cookies;
    ArrayList<KeyValue> headers = new ArrayList<>();
    int httpResponseCode = -1;
    boolean followRedirects = true;
    boolean loadHeaders;
    int timeoutMillisec = Constants.READ_TIMEOUT_MS;

    // Simple GET request constructor.
    public Params(String url)
    {
      this.url = url;
      httpMethod = "GET";
    }
  }
}