Skip to content

Commit 7d1475a

Browse files
elliotttJakeChampion
authored andcommitted
Rework xqd_fastly_http_req_header_values_get as well
1 parent afb3299 commit 7d1475a

File tree

1 file changed

+63
-60
lines changed

1 file changed

+63
-60
lines changed

c-dependencies/js-compute-runtime/xqd-world/xqd_world_adapter.cpp

Lines changed: 63 additions & 60 deletions
Original file line numberDiff line numberDiff line change
@@ -183,20 +183,20 @@ bool xqd_fastly_http_req_new(fastly_request_handle_t *ret, fastly_error_t *err)
183183
return convert_result(xqd_req_new(ret), err);
184184
}
185185

186+
struct Chunk {
187+
JS::UniqueChars buffer;
188+
size_t length;
189+
190+
static Chunk make(std::string_view data) {
191+
Chunk res{JS::UniqueChars{static_cast<char *>(cabi_malloc(data.size(), 1))}, data.size()};
192+
std::copy(data.begin(), data.end(), res.buffer.get());
193+
return res;
194+
}
195+
};
196+
186197
bool xqd_fastly_http_req_header_names_get(fastly_request_handle_t h, fastly_list_string_t *ret,
187198
fastly_error_t *err) {
188-
struct Chunk {
189-
JS::UniqueChars buffer;
190-
size_t length;
191-
192-
static Chunk make(std::string_view data) {
193-
Chunk res{JS::UniqueChars{static_cast<char *>(cabi_malloc(data.size(), 1))}, data.size()};
194-
std::copy(data.begin(), data.end(), res.buffer.get());
195-
return res;
196-
}
197-
};
198-
199-
std::vector<Chunk> chunks;
199+
std::vector<Chunk> header_names;
200200
{
201201
JS::UniqueChars buf{static_cast<char *>(cabi_malloc(HOSTCALL_BUFFER_LEN, 1))};
202202
uint32_t cursor = 0;
@@ -216,7 +216,7 @@ bool xqd_fastly_http_req_header_names_get(fastly_request_handle_t h, fastly_list
216216
std::string_view result{buf.get(), length};
217217
while (!result.empty()) {
218218
auto end = result.find('\0');
219-
chunks.emplace_back(Chunk::make(result.substr(0, end)));
219+
header_names.emplace_back(Chunk::make(result.substr(0, end)));
220220
if (end == result.npos) {
221221
break;
222222
}
@@ -232,11 +232,11 @@ bool xqd_fastly_http_req_header_names_get(fastly_request_handle_t h, fastly_list
232232
}
233233
}
234234

235-
ret->len = chunks.size();
236-
ret->ptr =
237-
static_cast<xqd_world_string_t *>(cabi_malloc(chunks.size() * sizeof(xqd_world_string_t), 1));
235+
ret->len = header_names.size();
236+
ret->ptr = static_cast<xqd_world_string_t *>(
237+
cabi_malloc(header_names.size() * sizeof(xqd_world_string_t), alignof(xqd_world_string_t)));
238238
auto *next = ret->ptr;
239-
for (auto &chunk : chunks) {
239+
for (auto &chunk : header_names) {
240240
next->len = chunk.length;
241241
next->ptr = chunk.buffer.release();
242242
++next;
@@ -247,54 +247,57 @@ bool xqd_fastly_http_req_header_names_get(fastly_request_handle_t h, fastly_list
247247

248248
bool xqd_fastly_http_req_header_values_get(fastly_request_handle_t h, xqd_world_string_t *name,
249249
fastly_option_list_string_t *ret, fastly_error_t *err) {
250-
size_t str_max = LIST_ALLOC_SIZE;
251-
xqd_world_string_t *strs =
252-
static_cast<xqd_world_string_t *>(cabi_malloc(str_max * sizeof(xqd_world_string_t), 1));
253-
size_t str_cnt = 0;
254-
size_t nwritten;
255-
char *buf = static_cast<char *>(cabi_malloc(HOSTCALL_BUFFER_LEN, 1));
256-
uint32_t cursor = 0;
257-
int64_t next_cursor = 0;
258-
while (true) {
259-
if (!convert_result(xqd_req_header_values_get(h, name->ptr, name->len, buf, HEADER_MAX_LEN,
260-
cursor, &next_cursor, &nwritten),
261-
err)) {
262-
cabi_free(buf);
263-
return false;
264-
}
265-
if (nwritten == 0)
266-
break;
267-
uint32_t offset = 0;
268-
for (size_t i = 0; i < nwritten; i++) {
269-
if (buf[i] != '\0')
270-
continue;
271-
if (str_cnt == str_max) {
272-
strs = static_cast<xqd_world_string_t *>(
273-
cabi_realloc(strs, str_max * sizeof(xqd_world_string_t), 1,
274-
(str_max + LIST_ALLOC_SIZE) * sizeof(xqd_world_string_t)));
275-
str_max += LIST_ALLOC_SIZE;
250+
251+
std::vector<Chunk> header_values;
252+
253+
{
254+
JS::UniqueChars buffer(static_cast<char *>(cabi_malloc(HEADER_MAX_LEN, 1)));
255+
uint32_t cursor = 0;
256+
while (true) {
257+
int64_t ending_cursor = 0;
258+
size_t length = 0;
259+
auto res = xqd_req_header_values_get(h, name->ptr, name->len, buffer.get(), HEADER_MAX_LEN,
260+
cursor, &ending_cursor, &length);
261+
if (!convert_result(res, err)) {
262+
return false;
263+
}
264+
265+
if (length == 0) {
266+
break;
267+
}
268+
269+
std::string_view result{buffer.get(), length};
270+
while (!result.empty()) {
271+
auto end = result.find('\0');
272+
header_values.emplace_back(Chunk::make(result.substr(0, end)));
273+
if (end == result.npos) {
274+
break;
275+
}
276+
277+
result = result.substr(end + 1);
278+
}
279+
280+
if (ending_cursor < 0) {
281+
break;
276282
}
277-
strs[str_cnt].ptr = static_cast<char *>(cabi_malloc(i - offset + 1, 1));
278-
strs[str_cnt].len = i - offset;
279-
memcpy(strs[str_cnt].ptr, buf + offset, i - offset + 1);
280-
offset = i + 1;
281-
str_cnt++;
282283
}
283-
if (next_cursor < 0)
284-
break;
285-
cursor = (uint32_t)next_cursor;
286284
}
287-
cabi_free(buf);
288-
if (str_cnt > 0) {
289-
ret->is_some = true;
290-
ret->val.ptr = strs;
291-
ret->val.len = str_cnt;
292-
strs = static_cast<xqd_world_string_t *>(cabi_realloc(
293-
strs, str_max * sizeof(xqd_world_string_t), 1, str_cnt * sizeof(xqd_world_string_t)));
294-
} else {
285+
286+
if (header_values.empty()) {
295287
ret->is_some = false;
296-
cabi_free(strs);
288+
} else {
289+
ret->is_some = true;
290+
ret->val.len = header_values.size();
291+
ret->val.ptr = static_cast<xqd_world_string_t *>(cabi_malloc(
292+
header_values.size() * sizeof(xqd_world_string_t), alignof(xqd_world_string_t)));
293+
auto *next = ret->val.ptr;
294+
for (auto &chunk : header_values) {
295+
next->len = chunk.length;
296+
next->ptr = chunk.buffer.release();
297+
++next;
298+
}
297299
}
300+
298301
return true;
299302
}
300303

0 commit comments

Comments
 (0)