Skip to content

Commit a0f86dd

Browse files
authored
Return string view from HTTPHdr methods (#12091)
* Return string_view from http_hdr_method_get * Return string_view from http_hdr_reason_get * Return string_view from HTTPHdr::method_get * Return string_view from HTTPHdr::path_get * Return string_view from HTTPHdr::query_get * Return string_view from HTTPHdr::fragment_get * Return string_view from HTTPHdr::scheme_get * Return string_view from HTTPHdr::host_get * Return string_view from HTTPHdr::reason_get * Remove uneeded .data() since swoc::bwprint supports string_view directly * Use TextView = operator * Change is_localhost in HttpTransact.cc to take string_view
1 parent 6bc5d77 commit a0f86dd

14 files changed

Lines changed: 216 additions & 209 deletions

File tree

include/proxy/hdrs/HTTP.h

Lines changed: 69 additions & 55 deletions
Original file line numberDiff line numberDiff line change
@@ -434,17 +434,17 @@ void http_hdr_describe(HdrHeapObjImpl *obj, bool recurse = true);
434434

435435
bool http_hdr_version_set(HTTPHdrImpl *hh, const HTTPVersion &ver);
436436

437-
const char *http_hdr_method_get(HTTPHdrImpl *hh, int *length);
438-
void http_hdr_method_set(HdrHeap *heap, HTTPHdrImpl *hh, const char *method, int16_t method_wks_idx, int method_length,
439-
bool must_copy);
437+
std::string_view http_hdr_method_get(HTTPHdrImpl *hh);
438+
void http_hdr_method_set(HdrHeap *heap, HTTPHdrImpl *hh, const char *method, int16_t method_wks_idx, int method_length,
439+
bool must_copy);
440440

441441
void http_hdr_url_set(HdrHeap *heap, HTTPHdrImpl *hh, URLImpl *url);
442442

443443
// HTTPStatus http_hdr_status_get (HTTPHdrImpl *hh);
444-
void http_hdr_status_set(HTTPHdrImpl *hh, HTTPStatus status);
445-
const char *http_hdr_reason_get(HTTPHdrImpl *hh, int *length);
446-
void http_hdr_reason_set(HdrHeap *heap, HTTPHdrImpl *hh, const char *value, int length, bool must_copy);
447-
const char *http_hdr_reason_lookup(unsigned status);
444+
void http_hdr_status_set(HTTPHdrImpl *hh, HTTPStatus status);
445+
std::string_view http_hdr_reason_get(HTTPHdrImpl *hh);
446+
void http_hdr_reason_set(HdrHeap *heap, HTTPHdrImpl *hh, const char *value, int length, bool must_copy);
447+
const char *http_hdr_reason_lookup(unsigned status);
448448

449449
void http_parser_init(HTTPParser *parser);
450450
void http_parser_clear(HTTPParser *parser);
@@ -516,9 +516,9 @@ class HTTPHdr : public MIMEHdr
516516
HTTPVersion version_get() const;
517517
void version_set(HTTPVersion version);
518518

519-
const char *method_get(int *length);
520-
int method_get_wksidx() const;
521-
void method_set(const char *value, int length);
519+
std::string_view method_get();
520+
int method_get_wksidx() const;
521+
void method_set(const char *value, int length);
522522

523523
URL *url_create(URL *url);
524524

@@ -561,31 +561,28 @@ class HTTPHdr : public MIMEHdr
561561

562562
/** Get the URL path.
563563
This is a reference, not allocated.
564-
@return A pointer to the path or @c NULL if there is no valid URL.
564+
@return A string_view to the path or an empty string_view if there is no valid URL.
565565
*/
566-
const char *path_get(int *length ///< Storage for path length.
567-
);
566+
std::string_view path_get();
568567

569568
/** Get the URL query.
570569
This is a reference, not allocated.
571-
@return A pointer to the query or @c NULL if there is no valid URL.
570+
@return A string_view to the query or an empty string_view if there is no valid URL.
572571
*/
573-
const char *query_get(int *length ///< Storage for query length.
574-
);
572+
std::string_view query_get();
575573

576574
/** Get the URL fragment.
577575
This is a reference, not allocated.
578-
@return A pointer to the fragment or @c NULL if there is no valid URL.
576+
@return A string_view to the fragment or an empty string_view if there is no valid URL.
579577
*/
580-
const char *fragment_get(int *length ///< Storage for fragment length.
581-
);
578+
std::string_view fragment_get();
582579

583580
/** Get the target host name.
584581
The length is returned in @a length if non-NULL.
585582
@note The results are cached so this is fast after the first call.
586-
@return A pointer to the host name.
583+
@return A string_view to the host name.
587584
*/
588-
const char *host_get(int *length = nullptr) const;
585+
std::string_view host_get() const;
589586

590587
/** Get the target port.
591588
If the target port is not found then it is adjusted to the
@@ -597,12 +594,11 @@ class HTTPHdr : public MIMEHdr
597594

598595
/** Get the URL scheme.
599596
This is a reference, not allocated.
600-
@return A pointer to the scheme or @c NULL if there is no valid URL.
597+
@return A string_view to the scheme or an empty string_view if there is no valid URL.
601598
*/
602-
const char *scheme_get(int *length ///< Storage for path length.
603-
);
604-
void url_set(URL *url);
605-
void url_set(const char *str, int length);
599+
std::string_view scheme_get();
600+
void url_set(URL *url);
601+
void url_set(const char *str, int length);
606602

607603
/// Check location of target host.
608604
/// @return @c true if the host was in the URL, @c false otherwise.
@@ -627,8 +623,8 @@ class HTTPHdr : public MIMEHdr
627623
HTTPStatus status_get() const;
628624
void status_set(HTTPStatus status);
629625

630-
const char *reason_get(int *length);
631-
void reason_set(const char *value, int length);
626+
std::string_view reason_get();
627+
void reason_set(const char *value, int length);
632628

633629
void mark_early_data(bool flag = true) const;
634630
bool is_early_data() const;
@@ -774,21 +770,19 @@ HTTPHdr::_test_and_fill_target_cache() const
774770
/*-------------------------------------------------------------------------
775771
-------------------------------------------------------------------------*/
776772

777-
inline const char *
778-
HTTPHdr::host_get(int *length) const
773+
inline std::string_view
774+
HTTPHdr::host_get() const
779775
{
780776
this->_test_and_fill_target_cache();
781777
if (m_target_in_url) {
782-
return url_get()->host_get(length);
778+
int length;
779+
auto host{url_get()->host_get(&length)};
780+
return std::string_view{host, static_cast<std::string_view::size_type>(length)};
783781
} else if (m_host_mime) {
784-
if (length)
785-
*length = m_host_length;
786-
return m_host_mime->m_ptr_value;
782+
return std::string_view{m_host_mime->m_ptr_value, static_cast<std::string_view::size_type>(m_host_length)};
787783
}
788784

789-
if (length)
790-
*length = 0;
791-
return nullptr;
785+
return std::string_view{};
792786
}
793787

794788
/*-------------------------------------------------------------------------
@@ -948,13 +942,13 @@ HTTPHdr::version_set(HTTPVersion version)
948942
/*-------------------------------------------------------------------------
949943
-------------------------------------------------------------------------*/
950944

951-
inline const char *
952-
HTTPHdr::method_get(int *length)
945+
inline std::string_view
946+
HTTPHdr::method_get()
953947
{
954948
ink_assert(valid());
955949
ink_assert(m_http->m_polarity == HTTP_TYPE_REQUEST);
956950

957-
return http_hdr_method_get(m_http, length);
951+
return http_hdr_method_get(m_http);
958952
}
959953

960954
inline int
@@ -1099,13 +1093,13 @@ HTTPHdr::status_set(HTTPStatus status)
10991093
/*-------------------------------------------------------------------------
11001094
-------------------------------------------------------------------------*/
11011095

1102-
inline const char *
1103-
HTTPHdr::reason_get(int *length)
1096+
inline std::string_view
1097+
HTTPHdr::reason_get()
11041098
{
11051099
ink_assert(valid());
11061100
ink_assert(m_http->m_polarity == HTTP_TYPE_RESPONSE);
11071101

1108-
return http_hdr_reason_get(m_http, length);
1102+
return http_hdr_reason_get(m_http);
11091103
}
11101104

11111105
/*-------------------------------------------------------------------------
@@ -1201,32 +1195,52 @@ HTTPHdr::url_string_get_ref(int *length)
12011195
return this->url_string_get(USE_HDR_HEAP_MAGIC, length);
12021196
}
12031197

1204-
inline const char *
1205-
HTTPHdr::path_get(int *length)
1198+
inline std::string_view
1199+
HTTPHdr::path_get()
12061200
{
12071201
URL *url = this->url_get();
1208-
return url ? url->path_get(length) : nullptr;
1202+
if (url) {
1203+
int length;
1204+
auto path{url->path_get(&length)};
1205+
return std::string_view{path, static_cast<std::string_view::size_type>(length)};
1206+
}
1207+
return std::string_view{};
12091208
}
12101209

1211-
inline const char *
1212-
HTTPHdr::query_get(int *length)
1210+
inline std::string_view
1211+
HTTPHdr::query_get()
12131212
{
12141213
URL *url = this->url_get();
1215-
return url ? url->query_get(length) : nullptr;
1214+
if (url) {
1215+
int length;
1216+
auto query{url->query_get(&length)};
1217+
return std::string_view{query, static_cast<std::string_view::size_type>(length)};
1218+
}
1219+
return std::string_view{};
12161220
}
12171221

1218-
inline const char *
1219-
HTTPHdr::fragment_get(int *length)
1222+
inline std::string_view
1223+
HTTPHdr::fragment_get()
12201224
{
12211225
URL *url = this->url_get();
1222-
return url ? url->fragment_get(length) : nullptr;
1226+
if (url) {
1227+
int length;
1228+
auto fragment{url->fragment_get(&length)};
1229+
return std::string_view{fragment, static_cast<std::string_view::size_type>(length)};
1230+
}
1231+
return std::string_view{};
12231232
}
12241233

1225-
inline const char *
1226-
HTTPHdr::scheme_get(int *length)
1234+
inline std::string_view
1235+
HTTPHdr::scheme_get()
12271236
{
12281237
URL *url = this->url_get();
1229-
return url ? url->scheme_get(length) : nullptr;
1238+
if (url) {
1239+
int length;
1240+
auto scheme{url->scheme_get(&length)};
1241+
return std::string_view{scheme, static_cast<std::string_view::size_type>(length)};
1242+
}
1243+
return std::string_view{};
12301244
}
12311245

12321246
/*-------------------------------------------------------------------------

src/api/InkAPI.cc

Lines changed: 9 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -2792,7 +2792,9 @@ TSHttpHdrMethodGet(TSMBuffer bufp, TSMLoc obj, int *length)
27922792
HTTPHdr h;
27932793

27942794
SET_HTTP_HDR(h, bufp, obj);
2795-
return h.method_get(length);
2795+
auto method{h.method_get()};
2796+
*length = static_cast<int>(method.length());
2797+
return method.data();
27962798
}
27972799

27982800
TSReturnCode
@@ -2831,7 +2833,9 @@ TSHttpHdrHostGet(TSMBuffer bufp, TSMLoc obj, int *length)
28312833
HTTPHdr h;
28322834

28332835
SET_HTTP_HDR(h, bufp, obj);
2834-
return h.host_get(length);
2836+
auto host{h.host_get()};
2837+
*length = static_cast<int>(host.length());
2838+
return host.data();
28352839
}
28362840

28372841
TSReturnCode
@@ -2921,7 +2925,9 @@ TSHttpHdrReasonGet(TSMBuffer bufp, TSMLoc obj, int *length)
29212925
HTTPHdr h;
29222926

29232927
SET_HTTP_HDR(h, bufp, obj);
2924-
return h.reason_get(length);
2928+
auto reason{h.reason_get()};
2929+
*length = static_cast<int>(reason.length());
2930+
return reason.data();
29252931
}
29262932

29272933
TSReturnCode

src/iocore/cache/CacheVC.cc

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -775,7 +775,9 @@ CacheVC::scanObject(int /* event ATS_UNUSED */, Event * /* e ATS_UNUSED */)
775775
goto Lskip;
776776
}
777777
if (!hostinfo_copied) {
778-
memccpy(hname, vector.get(i)->request_get()->host_get(&hlen), 0, 500);
778+
auto host{vector.get(i)->request_get()->host_get()};
779+
hlen = static_cast<int>(host.length());
780+
memccpy(hname, host.data(), 0, 500);
779781
hname[hlen] = 0;
780782
Dbg(dbg_ctl_cache_scan, "hostname = '%s', hostlen = %d", hname, hlen);
781783
hostinfo_copied = true;

src/proxy/ControlBase.cc

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -486,9 +486,8 @@ MethodMod::name() const
486486
bool
487487
MethodMod::check(HttpRequestData *req) const
488488
{
489-
int method_len;
490-
const char *method = req->hdr->method_get(&method_len);
491-
return method_len >= static_cast<int>(text.size()) && 0 == strncasecmp(method, text.data(), text.size());
489+
auto method{req->hdr->method_get()};
490+
return method.length() >= text.length() && 0 == strncasecmp(method.data(), text.data(), text.length());
492491
}
493492
MethodMod *
494493
MethodMod::make(char *value, const char **)

src/proxy/hdrs/HTTP.cc

Lines changed: 13 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -691,22 +691,23 @@ http_hdr_version_set(HTTPHdrImpl *hh, const HTTPVersion &ver)
691691
/*-------------------------------------------------------------------------
692692
-------------------------------------------------------------------------*/
693693

694-
const char *
695-
http_hdr_method_get(HTTPHdrImpl *hh, int *length)
694+
std::string_view
695+
http_hdr_method_get(HTTPHdrImpl *hh)
696696
{
697697
const char *str;
698+
int length;
698699

699700
ink_assert(hh->m_polarity == HTTP_TYPE_REQUEST);
700701

701702
if (hh->u.req.m_method_wks_idx >= 0) {
702-
str = hdrtoken_index_to_wks(hh->u.req.m_method_wks_idx);
703-
*length = hdrtoken_index_to_length(hh->u.req.m_method_wks_idx);
703+
str = hdrtoken_index_to_wks(hh->u.req.m_method_wks_idx);
704+
length = hdrtoken_index_to_length(hh->u.req.m_method_wks_idx);
704705
} else {
705-
str = hh->u.req.m_ptr_method;
706-
*length = hh->u.req.m_len_method;
706+
str = hh->u.req.m_ptr_method;
707+
length = hh->u.req.m_len_method;
707708
}
708709

709-
return (str);
710+
return std::string_view{str, static_cast<std::string_view::size_type>(length)};
710711
}
711712

712713
/*-------------------------------------------------------------------------
@@ -761,12 +762,11 @@ http_hdr_status_set(HTTPHdrImpl *hh, HTTPStatus status)
761762
/*-------------------------------------------------------------------------
762763
-------------------------------------------------------------------------*/
763764

764-
const char *
765-
http_hdr_reason_get(HTTPHdrImpl *hh, int *length)
765+
std::string_view
766+
http_hdr_reason_get(HTTPHdrImpl *hh)
766767
{
767768
ink_assert(hh->m_polarity == HTTP_TYPE_RESPONSE);
768-
*length = hh->u.resp.m_len_reason;
769-
return (hh->u.resp.m_ptr_reason);
769+
return std::string_view{hh->u.resp.m_ptr_reason, static_cast<std::string_view::size_type>(hh->u.resp.m_len_reason)};
770770
}
771771

772772
/*-------------------------------------------------------------------------
@@ -1740,10 +1740,9 @@ HTTPHdr::set_url_target_from_host_field(URL *url)
17401740
m_target_in_url = true; // it's there now.
17411741
}
17421742
} else {
1743-
int host_len = 0;
1744-
const char *host = host_get(&host_len);
1743+
auto host{host_get()};
17451744

1746-
url->host_set(host, host_len);
1745+
url->host_set(host.data(), static_cast<int>(host.length()));
17471746
if (m_port_in_header) {
17481747
url->port_set(m_port);
17491748
}

0 commit comments

Comments
 (0)