bes Updated for version 3.20.13
CurlUtils.h
1// -*- mode: c++; c-basic-offset:4 -*-
2
3// This file is part of the BES http package, part of the Hyrax data server.
4
5// Copyright (c) 2020 OPeNDAP, Inc.
6// Author: Nathan Potter <ndp@opendap.org>
7//
8// This library is free software; you can redistribute it and/or
9// modify it under the terms of the GNU Lesser General Public
10// License as published by the Free Software Foundation; either
11// version 2.1 of the License, or (at your option) any later version.
12//
13// This library is distributed in the hope that it will be useful,
14// but WITHOUT ANY WARRANTY; without even the implied warranty of
15// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16// Lesser General Public License for more details.
17//
18// You should have received a copy of the GNU Lesser General Public
19// License along with this library; if not, write to the Free Software
20// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21//
22// You can contact OPeNDAP, Inc. at PO Box 112, Saunderstown, RI. 02874-0112.
23
24// Authors:
25// ndp Nathan Potter <ndp@opendap.org>
26
27#ifndef _bes_http_CURL_UTILS_H_
28#define _bes_http_CURL_UTILS_H_ 1
29
30#include <memory>
31#include <string>
32#include <vector>
33
34#include <curl/curl.h>
35#include <curl/easy.h>
36
37#include "rapidjson/document.h"
38#include "BESRegex.h"
39#include "EffectiveUrl.h"
40
41namespace curl {
42
43//void http_get_and_write_resource(const std::string &url,
44// const int fd,
45// std::vector<std::string> *http_response_headers){}
46
47void http_get_and_write_resource(const std::shared_ptr<http::url>& target_url,
48 const int fd,
49 std::vector<std::string> *http_response_headers);
50
51void http_get(const std::string &url, char *response_buf);
52
53std::string http_get_as_string(const std::string &url);
54
55rapidjson::Document http_get_as_json(const std::string &target_url);
56
57std::shared_ptr<http::EffectiveUrl> retrieve_effective_url(const std::shared_ptr<http::url> &starting_point_url);
58
59std::string get_netrc_filename();
60
61std::string get_cookie_filename();
62
63bool is_retryable(std::string url);
64
65unsigned long max_redirects();
66
67std::string hyrax_user_agent();
68
69CURL *init(const std::string &target_url,
70 const struct curl_slist *http_request_headers,
71 std::vector<std::string> *resp_hdrs);
72
73CURL *init(CURL *ceh,
74 const std::string &target_url,
75 const struct curl_slist *http_request_headers,
76 std::vector<std::string> *http_response_hdrs);
77
78bool configure_curl_handle_for_proxy(CURL *ceh, const std::string &url);
79
80void set_error_buffer(CURL *ceh, char *error_buffer);
81
82void unset_error_buffer(CURL *ceh);
83
84void eval_curl_easy_setopt_result(CURLcode result,
85 std::string msg_base,
86 std::string opt_name,
87 char *ebuf, std::string file,
88 unsigned int line);
89
90bool eval_curl_easy_perform_code(CURL *ceh,
91 std::string url,
92 CURLcode curl_code,
93 char *error_buffer,
94 unsigned int attempt);
95
96bool eval_http_get_response(CURL *ceh, char *error_buffer, const std::string &requested_url);
97
98void super_easy_perform(CURL *ceh, const int fd);
99void super_easy_perform(CURL *ceh);
100
101std::string get_effective_url(CURL *ceh, std::string requested_url);
102
103std::string get_range_arg_string(const unsigned long long &offset, const unsigned long long &size);
104
105std::string http_status_to_string(int status);
106
107std::string error_message(CURLcode response_code, char *error_buf);
108
109size_t c_write_data(void *buffer, size_t size, size_t nmemb, void *data);
110
111void read_data(CURL *c_handle);
112
113curl_slist *append_http_header(curl_slist *slist, const std::string &header_name, const std::string &value);
114
115curl_slist *add_edl_auth_headers(curl_slist *request_headers);
116
117
118} // namespace curl
119
120#endif /* _bes_http_CURL_UTILS_H_ */