Skip to content

Commit bea5bc4

Browse files
CDRIVER-536 hostnames are normalized to lowercase
1 parent f21649c commit bea5bc4

File tree

4 files changed

+68
-2
lines changed

4 files changed

+68
-2
lines changed

src/mongoc/Makefile.am

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -77,6 +77,7 @@ INST_H_FILES = \
7777
src/mongoc/mongoc-thread-private.h \
7878
src/mongoc/mongoc-trace.h \
7979
src/mongoc/mongoc-uri.h \
80+
src/mongoc/mongoc-uri-private.h \
8081
src/mongoc/mongoc-util-private.h \
8182
src/mongoc/mongoc-version.h \
8283
src/mongoc/mongoc-write-command-private.h \

src/mongoc/mongoc-uri-private.h

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,39 @@
1+
/*
2+
* Copyright 2015 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+
#ifndef MONGOC_URI_PRIVATE_H
18+
#define MONGOC_URI_PRIVATE_H
19+
20+
#if !defined (MONGOC_I_AM_A_DRIVER) && !defined (MONGOC_COMPILATION)
21+
#error "Only <mongoc.h> can be included directly."
22+
#endif
23+
24+
#include "mongoc-uri.h"
25+
26+
27+
BSON_BEGIN_DECLS
28+
29+
30+
void
31+
mongoc_uri_lowercase_hostname (const char *src,
32+
char *buf /* OUT */,
33+
int len);
34+
35+
36+
BSON_BEGIN_DECLS
37+
38+
39+
#endif /* MONGOC_URI_PRIVATE_H */

src/mongoc/mongoc-uri.c

Lines changed: 20 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -23,7 +23,7 @@
2323
#include "mongoc-host-list-private.h"
2424
#include "mongoc-log.h"
2525
#include "mongoc-socket.h"
26-
#include "mongoc-uri.h"
26+
#include "mongoc-uri-private.h"
2727

2828

2929
#if defined(_WIN32) && !defined(strcasecmp)
@@ -55,6 +55,24 @@ mongoc_uri_do_unescape (char **str)
5555
}
5656
}
5757

58+
void
59+
mongoc_uri_lowercase_hostname (const char *src,
60+
char *buf /* OUT */,
61+
int len)
62+
{
63+
bson_unichar_t c;
64+
const char *iter;
65+
char *buf_iter;
66+
67+
/* TODO: this code only accepts ascii, and assumes that lowercased
68+
chars are the same width as originals */
69+
for (iter = src, buf_iter = buf;
70+
iter && *iter && (c = bson_utf8_get_char(iter)) && buf_iter - buf < len;
71+
iter = bson_utf8_next_char(iter), buf_iter++) {
72+
assert(c < 128);
73+
*buf_iter = tolower(c);
74+
}
75+
}
5876

5977
static void
6078
mongoc_uri_append_host (mongoc_uri_t *uri,
@@ -65,7 +83,7 @@ mongoc_uri_append_host (mongoc_uri_t *uri,
6583
mongoc_host_list_t *link_;
6684

6785
link_ = bson_malloc0(sizeof *link_);
68-
bson_strncpy (link_->host, host, sizeof link_->host);
86+
mongoc_uri_lowercase_hostname(host, link_->host, sizeof link_->host);
6987
if (strchr (host, ':')) {
7088
bson_snprintf (link_->host_and_port, sizeof link_->host_and_port,
7189
"[%s]:%hu", host, port);

tests/test-mongoc-uri.c

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -41,6 +41,14 @@ test_mongoc_uri_new (void)
4141
ASSERT(uri);
4242
mongoc_uri_destroy(uri);
4343

44+
/* should normalize to lowercase */
45+
uri = mongoc_uri_new ("mongodb://cRaZyHoStNaMe");
46+
assert (uri);
47+
hosts = mongoc_uri_get_hosts (uri);
48+
assert (hosts);
49+
ASSERT_CMPSTR (hosts->host, "crazyhostname");
50+
mongoc_uri_destroy (uri);
51+
4452
uri = mongoc_uri_new("mongodb://localhost/?");
4553
ASSERT(uri);
4654
mongoc_uri_destroy(uri);

0 commit comments

Comments
 (0)