Skip to content

Commit

Permalink
uri/RedirectHttps: return std::string_view
Browse files Browse the repository at this point in the history
  • Loading branch information
MaxKellermann committed Jan 17, 2025
1 parent 504c585 commit 3614855
Show file tree
Hide file tree
Showing 3 changed files with 24 additions and 23 deletions.
14 changes: 7 additions & 7 deletions src/uri/RedirectHttps.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
#include "net/HostParser.hxx"
#include "AllocatorPtr.hxx"

const char *
std::string_view
MakeHttpsRedirect(AllocatorPtr alloc, const char *_host, uint16_t port,
const char *uri) noexcept
{
Expand All @@ -27,10 +27,10 @@ MakeHttpsRedirect(AllocatorPtr alloc, const char *_host, uint16_t port,
std::find(eh.host.begin(), eh.host.end(), ':') != eh.host.end();
const size_t need_brackets = is_ipv6 && !port_sv.empty();

return alloc.Concat("https://",
std::string_view{&a, need_brackets},
host,
std::string_view{&b, need_brackets},
port_sv,
uri);
return alloc.ConcatView("https://",
std::string_view{&a, need_brackets},
host,
std::string_view{&b, need_brackets},
port_sv,
uri);
}
5 changes: 3 additions & 2 deletions src/uri/RedirectHttps.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@

#pragma once

#include <stdint.h>
#include <cstdint>
#include <string_view>

class AllocatorPtr;

Expand All @@ -15,6 +16,6 @@ class AllocatorPtr;
* @param port the new port; 0 means default
* @param uri the request URI
*/
const char *
std::string_view
MakeHttpsRedirect(AllocatorPtr alloc, const char *host, uint16_t port,
const char *uri) noexcept;
28 changes: 14 additions & 14 deletions test/uri/TestRedirectHttps.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -15,30 +15,30 @@ TEST(TestRedirectHttps, Basic)
TestPool pool;
const AllocatorPtr alloc(pool);

ASSERT_STREQ(MakeHttpsRedirect(alloc, "localhost", 0, "/foo"),
"https://localhost/foo");
ASSERT_EQ(MakeHttpsRedirect(alloc, "localhost", 0, "/foo"),
"https://localhost/foo");

ASSERT_STREQ(MakeHttpsRedirect(alloc, "localhost:80", 0, "/foo"),
"https://localhost/foo");
ASSERT_EQ(MakeHttpsRedirect(alloc, "localhost:80", 0, "/foo"),
"https://localhost/foo");

ASSERT_STREQ(MakeHttpsRedirect(alloc, "localhost:80", 443, "/foo"),
"https://localhost/foo");
ASSERT_EQ(MakeHttpsRedirect(alloc, "localhost:80", 443, "/foo"),
"https://localhost/foo");

ASSERT_STREQ(MakeHttpsRedirect(alloc, "localhost:80", 444, "/foo"),
"https://localhost:444/foo");
ASSERT_EQ(MakeHttpsRedirect(alloc, "localhost:80", 444, "/foo"),
"https://localhost:444/foo");
}

TEST(TestRedirectHttps, IPv6)
{
TestPool pool;
const AllocatorPtr alloc(pool);

ASSERT_STREQ(MakeHttpsRedirect(alloc, "::", 0, "/foo"),
"https://::/foo");
ASSERT_EQ(MakeHttpsRedirect(alloc, "::", 0, "/foo"),
"https://::/foo");

ASSERT_STREQ(MakeHttpsRedirect(alloc, "[::]:80", 0, "/foo"),
"https://::/foo");
ASSERT_EQ(MakeHttpsRedirect(alloc, "[::]:80", 0, "/foo"),
"https://::/foo");

ASSERT_STREQ(MakeHttpsRedirect(alloc, "::", 444, "/foo"),
"https://[::]:444/foo");
ASSERT_EQ(MakeHttpsRedirect(alloc, "::", 444, "/foo"),
"https://[::]:444/foo");
}

0 comments on commit 3614855

Please sign in to comment.