13 #ifndef __STOUT_NET_HPP__ 14 #define __STOUT_NET_HPP__ 25 #include <arpa/inet.h> 30 #include <net/if_dl.h> 31 #include <net/if_types.h> 41 #include <sys/param.h> 44 #include <curl/curl.h> 92 curl_global_init(CURL_GLOBAL_ALL);
108 CURL* curl = curl_easy_init();
109 if (curl ==
nullptr) {
110 curl_easy_cleanup(curl);
111 return Error(
"Failed to initialize libcurl");
114 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
115 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,
true);
116 curl_easy_setopt(curl, CURLOPT_HEADER, 1);
117 curl_easy_setopt(curl, CURLOPT_NOBODY, 1);
119 CURLcode curlErrorCode = curl_easy_perform(curl);
120 if (curlErrorCode != 0) {
121 curl_easy_cleanup(curl);
122 return Error(curl_easy_strerror(curlErrorCode));
126 curl_easy_getinfo(curl, CURLINFO_CONTENT_LENGTH_DOWNLOAD, &result);
128 curl_easy_cleanup(curl);
131 return Error(
"No URL content-length available");
134 return Bytes(uint64_t(result));
143 const std::string& url,
144 const std::string&
path,
158 CURL* curl = curl_easy_init();
160 if (curl ==
nullptr) {
161 curl_easy_cleanup(curl);
163 return Error(
"Failed to initialize libcurl");
166 curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
167 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION,
nullptr);
168 curl_easy_setopt(curl, CURLOPT_FOLLOWLOCATION,
true);
177 const int crt = fd->crt();
179 FILE*
file = ::_fdopen(crt,
"wb");
180 if (file ==
nullptr) {
181 curl_easy_cleanup(curl);
185 return ErrnoError(
"Failed to open file handle of '" + path +
"'");
188 FILE* file = ::fdopen(fd.
get(),
"w");
189 if (file ==
nullptr) {
190 curl_easy_cleanup(curl);
192 return ErrnoError(
"Failed to open file handle of '" + path +
"'");
194 #endif // __WINDOWS__ 196 curl_easy_setopt(curl, CURLOPT_WRITEDATA, file);
198 if (stall_timeout.isSome()) {
203 curl_easy_setopt(curl, CURLOPT_LOW_SPEED_LIMIT, 1L);
205 curl, CURLOPT_LOW_SPEED_TIME, static_cast<long>(stall_timeout->secs()));
208 CURLcode curlErrorCode = curl_easy_perform(curl);
209 if (curlErrorCode != 0) {
210 curl_easy_cleanup(curl);
214 return Error(curl_easy_strerror(curlErrorCode));
218 curl_easy_getinfo(curl, CURLINFO_RESPONSE_CODE, &code);
219 curl_easy_cleanup(curl);
221 if (::fclose(file) != 0) {
222 return ErrnoError(
"Failed to close file handle of '" + path +
"'");
230 #endif // __STOUT_NET_HPP__
Definition: errorbase.hpp:36
const mode_t S_IRGRP
Definition: windows.hpp:313
T & get()&
Definition: try.hpp:80
const mode_t S_IWUSR
Definition: windows.hpp:306
Try< int_fd > open(const std::string &path, int oflag, mode_t mode=0)
Definition: open.hpp:35
Definition: errorbase.hpp:50
const mode_t S_IRUSR
Definition: windows.hpp:305
void initialize()
Definition: net.hpp:76
constexpr int O_CLOEXEC
Definition: open.hpp:41
URI file(const std::string &path)
Creates a file URI with the given path on the local host.
Definition: file.hpp:33
Try< Nothing > close(int fd)
Definition: close.hpp:24
static Try error(const E &e)
Definition: try.hpp:43
static Try some(const T &t)
Definition: try.hpp:42
bool isError() const
Definition: try.hpp:78
Try< Bytes > contentLength(const std::string &url)
Definition: net.hpp:104
Try< int > download(const std::string &url, const std::string &path, const Option< Duration > &stall_timeout=None())
Definition: net.hpp:142
const mode_t S_IROTH
Definition: windows.hpp:321