YugabyteDB (2.13.0.0-b42, bfc6a6643e7399ac8a0e81d06a3ee6d6571b33ab)

Coverage Report

Created: 2022-03-09 17:30

/Users/deen/code/yugabyte-db/src/yb/util/curl_util.h
Line
Count
Source
1
// Licensed to the Apache Software Foundation (ASF) under one
2
// or more contributor license agreements.  See the NOTICE file
3
// distributed with this work for additional information
4
// regarding copyright ownership.  The ASF licenses this file
5
// to you under the Apache License, Version 2.0 (the
6
// "License"); you may not use this file except in compliance
7
// with the License.  You may obtain a copy of the License at
8
//
9
//   http://www.apache.org/licenses/LICENSE-2.0
10
//
11
// Unless required by applicable law or agreed to in writing,
12
// software distributed under the License is distributed on an
13
// "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
14
// KIND, either express or implied.  See the License for the
15
// specific language governing permissions and limitations
16
// under the License.
17
//
18
// The following only applies to changes made to this file as part of YugaByte development.
19
//
20
// Portions Copyright (c) YugaByte, Inc.
21
//
22
// Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
23
// in compliance with the License.  You may obtain a copy of the License at
24
//
25
// http://www.apache.org/licenses/LICENSE-2.0
26
//
27
// Unless required by applicable law or agreed to in writing, software distributed under the License
28
// is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
29
// or implied.  See the License for the specific language governing permissions and limitations
30
// under the License.
31
//
32
#ifndef YB_UTIL_CURL_UTIL_H
33
#define YB_UTIL_CURL_UTIL_H
34
35
#include <string>
36
#include <vector>
37
38
#include <boost/optional.hpp>
39
40
#include "yb/gutil/macros.h"
41
42
#include "yb/util/status_fwd.h"
43
44
typedef void CURL;
45
46
namespace yb {
47
48
class faststring;
49
50
// Simple wrapper around curl's "easy" interface, allowing the user to
51
// fetch web pages into memory using a blocking API.
52
//
53
// This is not thread-safe.
54
class EasyCurl {
55
 public:
56
  EasyCurl();
57
  ~EasyCurl();
58
59
  // Fetch the given URL into the provided buffer.
60
  // Any existing data in the buffer is replaced.
61
  // The optional param 'headers' holds additional headers.
62
  // e.g. {"Accept-Encoding: gzip"}
63
  CHECKED_STATUS FetchURL(
64
      const std::string& url,
65
      faststring* dst,
66
      int64_t timeout_sec = kDefaultTimeoutSec,
67
      const std::vector<std::string>& headers = {});
68
69
  // Issue an HTTP POST to the given URL with the given data.
70
  // Returns results in 'dst' as above.
71
  CHECKED_STATUS PostToURL(
72
      const std::string& url,
73
      const std::string& post_data,
74
      faststring* dst,
75
      int64_t timeout_sec = kDefaultTimeoutSec);
76
77
  CHECKED_STATUS PostToURL(
78
      const std::string& url,
79
      const std::string& post_data,
80
      const std::string& content_type,
81
      faststring* dst,
82
      int64_t timeout_sec = kDefaultTimeoutSec);
83
84
  std::string EscapeString(const std::string& data);
85
86
  static const int64_t kDefaultTimeoutSec = 600;
87
88
3
  void set_return_headers(bool v) {
89
3
    return_headers_ = v;
90
3
  }
91
92
 private:
93
  // Do a request. If 'post_data' is non-NULL, does a POST.
94
  // Otherwise, does a GET.
95
  CHECKED_STATUS DoRequest(
96
      const std::string& url,
97
      const boost::optional<const std::string>& post_data,
98
      const boost::optional<const std::string>& content_type,
99
      int64_t timeout_sec,
100
      faststring* dst,
101
      const std::vector<std::string>& headers = {});
102
103
  CURL* curl_;
104
  // Whether to return the HTTP headers with the response.
105
  bool return_headers_ = false;
106
  DISALLOW_COPY_AND_ASSIGN(EasyCurl);
107
};
108
109
} // namespace yb
110
111
#endif /* YB_UTIL_CURL_UTIL_H */