|
25 | 25 | #include "mongoc-trace-private.h" |
26 | 26 | #include "mongoc-uri-private.h" |
27 | 27 | #include "mongoc-util-private.h" |
| 28 | +#include "mongoc-http-private.h" |
28 | 29 |
|
29 | 30 | #undef MONGOC_LOG_DOMAIN |
30 | 31 | #define MONGOC_LOG_DOMAIN "aws_auth" |
@@ -145,88 +146,35 @@ _send_http_request (const char *ip, |
145 | 146 | char **http_response_headers, |
146 | 147 | bson_error_t *error) |
147 | 148 | { |
148 | | - mongoc_stream_t *stream = NULL; |
149 | | - mongoc_host_list_t host_list; |
150 | | - bool ret = false; |
151 | | - mongoc_iovec_t iovec; |
152 | | - uint8_t buf[512]; |
153 | | - ssize_t bytes_read; |
| 149 | + mongoc_http_request_t req; |
| 150 | + mongoc_http_response_t res; |
154 | 151 | const int socket_timeout_ms = 10000; |
155 | | - char *http_request = NULL; |
156 | | - bson_string_t *http_response = NULL; |
157 | | - char *ptr; |
158 | | - bool need_slash; |
| 152 | + bool ret; |
159 | 153 |
|
160 | 154 | *http_response_body = NULL; |
161 | 155 | *http_response_headers = NULL; |
162 | | - |
163 | | - if (!_mongoc_host_list_from_hostport_with_err ( |
164 | | - &host_list, ip, port, error)) { |
165 | | - goto fail; |
166 | | - } |
167 | | - |
168 | | - stream = mongoc_client_connect_tcp (socket_timeout_ms, &host_list, error); |
169 | | - if (!stream) { |
170 | | - goto fail; |
171 | | - } |
172 | | - |
173 | | - if (strstr (path, "/") == path) { |
174 | | - need_slash = false; |
175 | | - } else { |
176 | | - need_slash = true; |
177 | | - } |
178 | | - |
179 | | - /* Always add 'Host: <domain>' header. */ |
180 | | - http_request = bson_strdup_printf ( |
181 | | - "%s %s%s HTTP/1.1\r\nHost: %s\r\nConnection: close\r\n%s\r\n", |
182 | | - method, |
183 | | - need_slash ? "/" : "", |
184 | | - path, |
185 | | - ip, |
186 | | - headers); |
187 | | - iovec.iov_base = http_request; |
188 | | - iovec.iov_len = strlen (http_request); |
189 | | - |
190 | | - if (!_mongoc_stream_writev_full ( |
191 | | - stream, &iovec, 1, socket_timeout_ms, error)) { |
192 | | - goto fail; |
193 | | - } |
194 | | - |
195 | | - /* If timeout == 0, you'll get EAGAIN errors. */ |
196 | | - http_response = bson_string_new (NULL); |
197 | | - memset (buf, 0, sizeof (buf)); |
198 | | - /* leave at least one byte out of buffer to leave it null terminated. */ |
199 | | - while ((bytes_read = mongoc_stream_read ( |
200 | | - stream, buf, (sizeof buf) - 1, 0, socket_timeout_ms)) > 0) { |
201 | | - bson_string_append (http_response, (const char *) buf); |
202 | | - memset (buf, 0, sizeof (buf)); |
203 | | - } |
204 | | - |
205 | | - if (bytes_read < 0) { |
206 | | - char errmsg_buf[BSON_ERROR_BUFFER_SIZE]; |
207 | | - char *errmsg; |
208 | | - |
209 | | - errmsg = bson_strerror_r (errno, errmsg_buf, sizeof errmsg_buf); |
210 | | - AUTH_ERROR_AND_FAIL ("error occurred reading stream: %s", errmsg); |
211 | | - } |
212 | | - |
213 | | - /* Find the body. */ |
214 | | - ptr = strstr (http_response->str, "\r\n\r\n"); |
215 | | - if (NULL == ptr) { |
216 | | - AUTH_ERROR_AND_FAIL ("error occurred reading response, body not found"); |
217 | | - } |
218 | | - |
219 | | - *http_response_headers = |
220 | | - bson_strndup (http_response->str, ptr - http_response->str); |
221 | | - *http_response_body = bson_strdup (ptr + 4); |
222 | | - |
223 | | - ret = true; |
224 | | -fail: |
225 | | - mongoc_stream_destroy (stream); |
226 | | - bson_free (http_request); |
227 | | - if (http_response) { |
228 | | - bson_string_free (http_response, true); |
229 | | - } |
| 156 | + _mongoc_http_request_init (&req); |
| 157 | + _mongoc_http_response_init (&res); |
| 158 | + |
| 159 | + req.host = ip; |
| 160 | + req.port = port; |
| 161 | + req.method = method; |
| 162 | + req.path = path; |
| 163 | + req.extra_headers = headers; |
| 164 | + ret = _mongoc_http_send (&req, |
| 165 | + socket_timeout_ms, |
| 166 | + false /* use_tls */, |
| 167 | + NULL /* ssl_opts */, |
| 168 | + &res, |
| 169 | + error); |
| 170 | + |
| 171 | + if (ret) { |
| 172 | + *http_response_headers = bson_strndup (res.headers, res.headers_len); |
| 173 | + *http_response_body = (char *) bson_malloc0 (res.body_len + 1); |
| 174 | + memcpy (*http_response_body, res.body, res.body_len); |
| 175 | + } |
| 176 | + |
| 177 | + _mongoc_http_response_cleanup (&res); |
230 | 178 | return ret; |
231 | 179 | } |
232 | 180 |
|
|
0 commit comments