Skip to content

Commit 16adf52

Browse files
committed
CDRIVER-3739 Add 5 second timeout to OCSP (#658)
- Reuse libmongoc's HTTP function instead of OCSP_sendreq_new - Support TLS in OCSP endpoint
1 parent 1f1ea55 commit 16adf52

10 files changed

+428
-124
lines changed

src/libmongoc/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -512,6 +512,7 @@ set (SOURCES ${SOURCES}
512512
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-gridfs-file-list.c
513513
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-handshake.c
514514
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-host-list.c
515+
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-http.c
515516
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-index.c
516517
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-init.c
517518
${PROJECT_SOURCE_DIR}/src/mongoc/mongoc-interrupt.c
@@ -898,6 +899,7 @@ set (test-libmongoc-sources
898899
${PROJECT_SOURCE_DIR}/tests/test-mongoc-gridfs.c
899900
${PROJECT_SOURCE_DIR}/tests/test-mongoc-handshake.c
900901
${PROJECT_SOURCE_DIR}/tests/test-mongoc-hedged-reads.c
902+
${PROJECT_SOURCE_DIR}/tests/test-mongoc-http.c
901903
${PROJECT_SOURCE_DIR}/tests/test-mongoc-interrupt.c
902904
${PROJECT_SOURCE_DIR}/tests/test-mongoc-linux-distro-scanner.c
903905
${PROJECT_SOURCE_DIR}/tests/test-mongoc-list.c

src/libmongoc/src/mongoc/CMakeLists.txt

+2
Original file line numberDiff line numberDiff line change
@@ -116,6 +116,7 @@ set (src_libmongoc_src_mongoc_DIST_noinst_hs
116116
mongoc-handshake-os-private.h
117117
mongoc-handshake-private.h
118118
mongoc-host-list-private.h
119+
mongoc-http-private.h
119120
mongoc-interrupt-private.h
120121
mongoc-libressl-private.h
121122
mongoc-linux-distro-scanner-private.h
@@ -210,6 +211,7 @@ set (src_libmongoc_src_mongoc_DIST_cs
210211
mongoc-gridfs-file-page.c
211212
mongoc-gridfs-file-list.c
212213
mongoc-handshake.c
214+
mongoc-http.c
213215
mongoc-index.c
214216
mongoc-linux-distro-scanner.c
215217
mongoc-list.c

src/libmongoc/src/mongoc/mongoc-cluster-aws.c

+26-78
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@
2525
#include "mongoc-trace-private.h"
2626
#include "mongoc-uri-private.h"
2727
#include "mongoc-util-private.h"
28+
#include "mongoc-http-private.h"
2829

2930
#undef MONGOC_LOG_DOMAIN
3031
#define MONGOC_LOG_DOMAIN "aws_auth"
@@ -145,88 +146,35 @@ _send_http_request (const char *ip,
145146
char **http_response_headers,
146147
bson_error_t *error)
147148
{
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;
154151
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;
159153

160154
*http_response_body = NULL;
161155
*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);
230178
return ret;
231179
}
232180

Original file line numberDiff line numberDiff line change
@@ -0,0 +1,67 @@
1+
/*
2+
* Copyright 2020-present MongoDB, Inc.
3+
*
4+
* Licensed under the Apache License, Version 2.0 (the "License");
5+
* you may not use this file except in compliance with the License.
6+
* You may obtain a copy of the License at
7+
*
8+
* http://www.apache.org/licenses/LICENSE-2.0
9+
*
10+
* Unless required by applicable law or agreed to in writing, software
11+
* distributed under the License is distributed on an "AS IS" BASIS,
12+
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13+
* See the License for the specific language governing permissions and
14+
* limitations under the License.
15+
*/
16+
17+
#include "mongoc.h"
18+
#include "mongoc-ssl.h"
19+
20+
#include "mongoc-prelude.h"
21+
22+
#ifndef MONGOC_HTTP_PRIVATE_H
23+
#define MONGOC_HTTP_PRIVATE_H
24+
25+
typedef struct {
26+
const char *host;
27+
int port;
28+
const char *method;
29+
const char *path;
30+
const char *extra_headers;
31+
const char *body;
32+
int body_len;
33+
} mongoc_http_request_t;
34+
35+
typedef struct {
36+
int status;
37+
char *headers;
38+
int headers_len;
39+
char *body;
40+
int body_len;
41+
} mongoc_http_response_t;
42+
43+
void
44+
_mongoc_http_request_init (mongoc_http_request_t *request);
45+
46+
void
47+
_mongoc_http_response_init (mongoc_http_response_t *response);
48+
49+
void
50+
_mongoc_http_response_cleanup (mongoc_http_response_t *response);
51+
52+
/*
53+
* Send an HTTP request and get a response.
54+
* On success, returns true.
55+
* On failure, returns false and sets error.
56+
* If use_tls is true, then ssl_opts must be set.
57+
* Caller must call _mongoc_http_response_cleanup on res.
58+
*/
59+
bool
60+
_mongoc_http_send (mongoc_http_request_t *req,
61+
int timeout_ms,
62+
bool use_tls,
63+
mongoc_ssl_opt_t *ssl_opts,
64+
mongoc_http_response_t *res,
65+
bson_error_t *error);
66+
67+
#endif /* MONGOC_HTTP_PRIVATE */

0 commit comments

Comments
 (0)