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

http_client.hpp « platform - github.com/mapsme/omim.git - Unnamed repository; edit this file 'description' to name the repository.
summaryrefslogtreecommitdiff
blob: d49a07ddda544fa0fc2a01c95dd1e0e18f2c5509 (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
/*******************************************************************************
The MIT License (MIT)

Copyright (c) 2015 Alexander Zolotarev <me@alex.bio> from Minsk, Belarus

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:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

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.
*******************************************************************************/
#pragma once

#include "base/macros.hpp"

#include "std/string.hpp"
#include "std/unordered_map.hpp"

namespace platform
{
class HttpClient
{
public:
  static auto constexpr kNoError = -1;

  HttpClient() = default;
  HttpClient(string const & url) : m_urlRequested(url) {}

  // Synchronous (blocking) call, should be implemented for each platform
  // @returns true if connection was made and server returned something (200, 404, etc.).
  // @note Implementations should transparently support all needed HTTP redirects.
  // Implemented for each platform.
  bool RunHttpRequest();

  // Shared methods for all platforms, implemented at http_client.cpp
  HttpClient & SetDebugMode(bool debug_mode);
  HttpClient & SetUrlRequested(string const & url);
  HttpClient & SetHttpMethod(string const & method);
  // This method is mutually exclusive with set_body_data().
  HttpClient & SetBodyFile(string const & body_file, string const & content_type,
                           string const & http_method = "POST",
                           string const & content_encoding = "");
  // If set, stores server reply in file specified.
  HttpClient & SetReceivedFile(string const & received_file);
  // This method is mutually exclusive with set_body_file().
  template <typename StringT>
  HttpClient & SetBodyData(StringT && body_data, string const & content_type,
                           string const & http_method  = "POST",
                           string const & content_encoding = "")
  {
    m_bodyData = forward<StringT>(body_data);
    m_inputFile.clear();
    m_headers.emplace("Content-Type", content_type);
    m_httpMethod = http_method;
    m_headers.emplace("Content-Encoding", content_encoding);
    return *this;
  }
  // HTTP Basic Auth.
  HttpClient & SetUserAndPassword(string const & user, string const & password);
  // Set HTTP Cookie header.
  HttpClient & SetCookies(string const & cookies);
  // When set to true (default), clients never get 3XX codes from servers, redirects are handled automatically.
  // TODO: "false" is now supported on Android only.
  HttpClient & SetHandleRedirects(bool handle_redirects);
  HttpClient & SetRawHeader(string const & key, string const & value);

  string const & UrlRequested() const;
  // @returns empty string in the case of error
  string const & UrlReceived() const;
  bool WasRedirected() const;
  // Mix of HTTP errors (in case of successful connection) and system-dependent error codes,
  // in the simplest success case use 'if (200 == client.error_code())' // 200 means OK in HTTP
  int ErrorCode() const;
  string const & ServerResponse() const;
  string const & HttpMethod() const;
  // Pass this getter's value to the set_cookies() method for easier cookies support in the next request.
  string CombinedCookies() const;
  // Returns cookie value or empty string if it's not present.
  string CookieByName(string name) const;
  void LoadHeaders(bool loadHeaders);
  unordered_map<string, string> const & GetHeaders() const;

private:
  // Internal helper to convert cookies like this:
  // "first=value1; expires=Mon, 26-Dec-2016 12:12:32 GMT; path=/, second=value2; path=/, third=value3; "
  // into this:
  // "first=value1; second=value2; third=value3"
  static string NormalizeServerCookies(string && cookies);

  string m_urlRequested;
  // Contains final content's url taking redirects (if any) into an account.
  string m_urlReceived;
  int m_errorCode = kNoError;
  string m_inputFile;
  // Used instead of server_reply_ if set.
  string m_outputFile;
  // Data we received from the server if output_file_ wasn't initialized.
  string m_serverResponse;
  string m_bodyData;
  string m_httpMethod = "GET";
  // Cookies set by the client before request is run.
  string m_cookies;
  unordered_map<string, string> m_headers;
  bool m_handleRedirects = true;
  bool m_loadHeaders = false;

  DISALLOW_COPY_AND_MOVE(HttpClient);
};

string DebugPrint(HttpClient const & request);
}  // namespace platform