Copilot commented on code in PR #3606:
URL: https://github.com/apache/brpc/pull/3606#discussion_r4302310603
##########
src/brpc/http_header.cpp:
##########
@@ -128,28 +161,32 @@ std::string& HttpHeader::GetOrAddHeader(const
std::string& key) {
return _content_type;
}
- bool is_set_cookie = IsSetCookie(key);
- // Only returns the first Set-Cookie header field for compatibility.
- if (is_set_cookie && nullptr != _first_set_cookie) {
- return *_first_set_cookie;
- }
-
std::string* val = _headers.seek(key);
if (nullptr == val) {
val = _headers.insert({ key, "" });
- if (is_set_cookie) {
- _first_set_cookie = val;
- }
}
return *val;
}
std::string& HttpHeader::AddHeader(const std::string& key) {
- std::string* val = _headers.insert({ key, "" });
- if (IsSetCookie(key) && nullptr == _first_set_cookie) {
- _first_set_cookie = val;
+ if (!IsSetCookie(key)) {
+ return *_headers.insert({ key, "" });
+ }
+ if (!_set_cookie_headers) {
+ _set_cookie_headers.reset(new SetCookieHeaders);
+ }
+ _set_cookie_headers->push_back(std::make_pair(key, std::string()));
+ SyncSetCookieHeaders();
+ return _set_cookie_headers->back().second;
+}
+
+void HttpHeader::SyncSetCookieHeaders() {
+ _headers.erase(SET_COOKIE);
+ if (_set_cookie_headers) {
+ for (const auto& header : *_set_cookie_headers) {
+ _headers.insert(header);
+ }
Review Comment:
`SyncSetCookieHeaders()` rebuilds every `Set-Cookie` entry, and it is called
after each append/add. Constructing or parsing n cookies therefore performs
O(n²) copying and map work, unlike the previous one-entry insertion path. Keep
the map update incremental and/or defer one full synchronization until header
parsing finishes (for example with a dirty flag) so multi-cookie responses do
not pay this quadratic cost.
--
This is an automated message from the Apache Git Service.
To respond to the message, please log on to GitHub and use the
URL above to go to the specific comment.
To unsubscribe, e-mail: [email protected]
For queries about this service, please contact Infrastructure at:
[email protected]
---------------------------------------------------------------------
To unsubscribe, e-mail: [email protected]
For additional commands, e-mail: [email protected]