Skip to content

Commit

Permalink
fix(fetch) always make sure that abort tracker is cleaned, revise ref…
Browse files Browse the repository at this point in the history
… count (oven-sh#13459)
  • Loading branch information
cirospaciari authored Aug 24, 2024
1 parent 0f1d5d5 commit 5a108c5
Show file tree
Hide file tree
Showing 17 changed files with 1,685 additions and 119 deletions.
2 changes: 1 addition & 1 deletion packages/bun-usockets/src/context.c
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,7 @@
#include <arpa/inet.h>
#endif

#define CONCURRENT_CONNECTIONS 6
#define CONCURRENT_CONNECTIONS 4

// clang-format off
int default_is_low_prio_handler(struct us_socket_t *s) {
Expand Down
24 changes: 17 additions & 7 deletions src/bun.js/bindings/webcore/FetchHeaders.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,10 +71,16 @@ static ExceptionOr<void> appendToHeaderMap(const String& name, const String& val
String combinedValue = normalizedValue;
HTTPHeaderName headerName;
if (findHTTPHeaderName(name, headerName)) {
auto index = headers.indexOf(headerName);

if (headerName != HTTPHeaderName::SetCookie) {
if (headers.contains(headerName)) {
combinedValue = makeString(headers.get(headerName), ", "_s, normalizedValue);
if (index.isValid()) {
auto existing = headers.getIndex(index);
if (headerName == HTTPHeaderName::Cookie) {
combinedValue = makeString(existing, "; "_s, normalizedValue);
} else {
combinedValue = makeString(existing, ", "_s, normalizedValue);
}
}
}

Expand All @@ -86,22 +92,26 @@ static ExceptionOr<void> appendToHeaderMap(const String& name, const String& val
return {};

if (headerName != HTTPHeaderName::SetCookie) {
headers.set(headerName, combinedValue);
if (!headers.setIndex(index, combinedValue))
headers.set(headerName, combinedValue);
} else {
headers.add(headerName, normalizedValue);
}

return {};
}

if (headers.contains(name))
combinedValue = makeString(headers.get(name), ", "_s, normalizedValue);
auto index = headers.indexOf(name);
if (index.isValid()) {
combinedValue = makeString(headers.getIndex(index), ", "_s, normalizedValue);
}
auto canWriteResult = canWriteHeader(name, normalizedValue, combinedValue, guard);
if (canWriteResult.hasException())
return canWriteResult.releaseException();
if (!canWriteResult.releaseReturnValue())
return {};
headers.set(name, combinedValue);

if (!headers.setIndex(index, combinedValue))
headers.set(name, combinedValue);

// if (guard == FetchHeaders::Guard::RequestNoCors)
// removePrivilegedNoCORSRequestHeaders(headers);
Expand Down
39 changes: 38 additions & 1 deletion src/bun.js/bindings/webcore/HTTPHeaderMap.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -252,6 +252,30 @@ String HTTPHeaderMap::get(HTTPHeaderName name) const
return index != notFound ? m_commonHeaders[index].value : String();
}

HTTPHeaderMap::HeaderIndex HTTPHeaderMap::indexOf(HTTPHeaderName name) const
{
auto index = m_commonHeaders.findIf([&](auto& header) {
return header.key == name;
});
return (HeaderIndex) { .index = index, .isCommon = true };
}

HTTPHeaderMap::HeaderIndex HTTPHeaderMap::indexOf(const String& name) const
{
auto index = m_uncommonHeaders.findIf([&](auto& header) {
return equalIgnoringASCIICase(header.key, name);
});
return (HeaderIndex) { .index = index, .isCommon = false };
}

String HTTPHeaderMap::getIndex(HTTPHeaderMap::HeaderIndex index) const
{
if (index.index == notFound)
return String();
if (index.isCommon)
return m_commonHeaders[index.index].value;
return m_uncommonHeaders[index.index].value;
}
void HTTPHeaderMap::set(HTTPHeaderName name, const String& value)
{
if (name == HTTPHeaderName::SetCookie) {
Expand All @@ -269,6 +293,19 @@ void HTTPHeaderMap::set(HTTPHeaderName name, const String& value)
m_commonHeaders[index].value = value;
}

bool HTTPHeaderMap::setIndex(HTTPHeaderMap::HeaderIndex index, const String& value)
{
if (!index.isValid())
return false;

if (index.isCommon) {
m_commonHeaders[index.index].value = value;
} else {
m_uncommonHeaders[index.index].value = value;
}
return true;
}

bool HTTPHeaderMap::contains(HTTPHeaderName name) const
{
if (name == HTTPHeaderName::SetCookie)
Expand Down Expand Up @@ -303,7 +340,7 @@ void HTTPHeaderMap::add(HTTPHeaderName name, const String& value)
return header.key == name;
});
if (index != notFound)
m_commonHeaders[index].value = makeString(m_commonHeaders[index].value, ", "_s, value);
m_commonHeaders[index].value = makeString(m_commonHeaders[index].value, name == HTTPHeaderName::Cookie ? "; "_s : ", "_s, value);
else
m_commonHeaders.append(CommonHeader { name, value });
}
Expand Down
13 changes: 13 additions & 0 deletions src/bun.js/bindings/webcore/HTTPHeaderMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -48,6 +48,13 @@ class HTTPHeaderMap {
bool operator==(const CommonHeader &other) const { return key == other.key && value == other.value; }
};

struct HeaderIndex {
size_t index;
bool isCommon;

bool isValid() const { return index != notFound; }
};

struct UncommonHeader {
String key;
String value;
Expand Down Expand Up @@ -180,8 +187,14 @@ class HTTPHeaderMap {
WEBCORE_EXPORT void add(const String &name, const String &value);
WEBCORE_EXPORT void append(const String &name, const String &value);
WEBCORE_EXPORT bool contains(const String &) const;
WEBCORE_EXPORT int64_t indexOf(String &name) const;
WEBCORE_EXPORT bool remove(const String &);

WEBCORE_EXPORT String getIndex(HeaderIndex index) const;
WEBCORE_EXPORT bool setIndex(HeaderIndex index, const String &value);
HeaderIndex indexOf(const String &name) const;
HeaderIndex indexOf(HTTPHeaderName name) const;

#if USE(CF)
void set(CFStringRef name, const String &value);
#ifdef __OBJC__
Expand Down
Loading

0 comments on commit 5a108c5

Please sign in to comment.