Uses the curl command to send an HTTP request to the given URL and returns the HTTP response it received. The location redirection and HTTPS connections will be handled automatically by the curl command. The returned HTTP response will have the type 'BODY' (no streaming).
| 102 | // command. The returned HTTP response will have the type 'BODY' (no |
| 103 | // streaming). |
| 104 | static Future<http::Response> curl( |
| 105 | const string& uri, |
| 106 | const http::Headers& headers, |
| 107 | const Option<Duration>& stallTimeout) |
| 108 | { |
| 109 | static process::Once* initialized = new process::Once(); |
| 110 | static bool http11 = false; |
| 111 | |
| 112 | if (!initialized->once()) { |
| 113 | // Test if curl supports locking into HTTP 1.1. We do this as |
| 114 | // HTTP 1.1 is more likely than HTTP 1.0 to function accross all |
| 115 | // infrastructures. The '--http1.1' flag got added to curl with |
| 116 | // with version 7.33.0. Some supported distributions do still come |
| 117 | // with curl version 7.19.0. See MESOS-8907. |
| 118 | http11 = os::system("curl --http1.1 -V > /dev/null 2>&1") == 0; |
| 119 | VLOG(1) << "Curl accepts --http1.1 flag: " << stringify(http11); |
| 120 | initialized->done(); |
| 121 | } |
| 122 | |
| 123 | vector<string> argv = { |
| 124 | "curl", |
| 125 | "-s", // Don't show progress meter or error messages. |
| 126 | "-S", // Make curl show an error message if it fails. |
| 127 | "-L", // Follow HTTP 3xx redirects. |
| 128 | "-i", // Include the HTTP-header in the output. |
| 129 | "--raw" // Disable HTTP decoding of content or transfer encodings. |
| 130 | }; |
| 131 | |
| 132 | // Make sure curl does not enforce HTTP 2 as our HTTP parser does |
| 133 | // currently not support that. See MESOS-8368. |
| 134 | // Older curl versions do not support the HTTP 1.1 flag, but these |
| 135 | // versions are also old enough to not default to HTTP/2. |
| 136 | if (http11) { |
| 137 | argv.push_back("--http1.1"); |
| 138 | } |
| 139 | |
| 140 | // Add additional headers. |
| 141 | foreachpair (const string& key, const string& value, headers) { |
| 142 | argv.push_back("-H"); |
| 143 | argv.push_back(key + ": " + value); |
| 144 | } |
| 145 | |
| 146 | // Add a timeout for curl to abort when the download speed keeps low |
| 147 | // (1 byte per second by default) for the specified duration. See: |
| 148 | // https://curl.haxx.se/docs/manpage.html#-y |
| 149 | if (stallTimeout.isSome()) { |
| 150 | argv.push_back("-y"); |
| 151 | argv.push_back(std::to_string(static_cast<long>(stallTimeout->secs()))); |
| 152 | } |
| 153 | |
| 154 | argv.push_back(strings::trim(uri)); |
| 155 | |
| 156 | string cmd = strings::join(" ", argv); |
| 157 | |
| 158 | Try<Subprocess> s = subprocess( |
| 159 | "curl", |
| 160 | argv, |
| 161 | Subprocess::PATH(os::DEV_NULL), |
no test coverage detected