Skip to content

Commit 75e0125

Browse files
authored
fix(protocols): fix header serde, handle unset union payloads (#1417)
1 parent 8051551 commit 75e0125

File tree

12 files changed

+139
-9
lines changed

12 files changed

+139
-9
lines changed

.changeset/proud-bugs-deny.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/middleware-compression": patch
3+
---
4+
5+
comma spacing

.changeset/witty-rings-do.md

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,5 @@
1+
---
2+
"@smithy/smithy-client": minor
3+
---
4+
5+
add quoteHeader function

packages/middleware-compression/src/compressionMiddleware.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -160,7 +160,7 @@ describe(compressionMiddleware.name, () => {
160160
body: mockCompressedBody,
161161
headers: {
162162
...mockArgs.request.headers,
163-
"content-encoding": [mockExistingContentEncoding, "gzip"].join(","),
163+
"content-encoding": [mockExistingContentEncoding, "gzip"].join(", "),
164164
},
165165
},
166166
});

packages/middleware-compression/src/compressionMiddleware.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -82,7 +82,7 @@ export const compressionMiddleware =
8282
if (headers["content-encoding"]) {
8383
updatedHeaders = {
8484
...headers,
85-
"content-encoding": `${headers["content-encoding"]},${algorithm}`,
85+
"content-encoding": `${headers["content-encoding"]}, ${algorithm}`,
8686
};
8787
} else {
8888
updatedHeaders = { ...headers, "content-encoding": algorithm };

packages/smithy-client/src/index.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,9 @@ export * from "./lazy-json";
1818
export * from "./NoOpLogger";
1919
export * from "./object-mapping";
2020
export * from "./parse-utils";
21+
export * from "./quote-header";
2122
export * from "./resolve-path";
2223
export * from "./ser-utils";
2324
export * from "./serde-json";
2425
export * from "./split-every";
26+
export * from "./split-header";
Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import { quoteHeader } from "./quote-header";
2+
3+
describe(quoteHeader.name, () => {
4+
it("should not wrap header elements that don't include the delimiter or double quotes", () => {
5+
expect(quoteHeader("bc")).toBe("bc");
6+
});
7+
8+
it("should wrap header elements that include the delimiter", () => {
9+
expect(quoteHeader("b,c")).toBe('"b,c"');
10+
});
11+
12+
it("should wrap header elements that include double quotes", () => {
13+
expect(quoteHeader(`"bc"`)).toBe('"\\"bc\\""');
14+
});
15+
16+
it("should wrap header elements that include the delimiter and double quotes", () => {
17+
expect(quoteHeader(`"b,c"`)).toBe('"\\"b,c\\""');
18+
});
19+
});
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
/**
2+
* @public
3+
* @param part - header list element
4+
* @returns quoted string if part contains delimiter.
5+
*/
6+
export function quoteHeader(part: string) {
7+
if (part.includes(",") || part.includes('"')) {
8+
part = `"${part.replace(/"/g, '\\"')}"`;
9+
}
10+
return part;
11+
}
Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,22 @@
1+
import { splitHeader } from "./split-header";
2+
3+
describe(splitHeader.name, () => {
4+
it("should split a string by commas and trim only the comma delimited outer values", () => {
5+
expect(splitHeader("abc")).toEqual(["abc"]);
6+
expect(splitHeader("a,b,c")).toEqual(["a", "b", "c"]);
7+
expect(splitHeader("a, b, c")).toEqual(["a", "b", "c"]);
8+
expect(splitHeader("a , b , c")).toEqual(["a", "b", "c"]);
9+
expect(splitHeader(`a , b , " c "`)).toEqual(["a", "b", " c "]);
10+
expect(splitHeader(` a , , b`)).toEqual(["a", "", "b"]);
11+
expect(splitHeader(`,,`)).toEqual(["", "", ""]);
12+
expect(splitHeader(` , , `)).toEqual(["", "", ""]);
13+
});
14+
it("should split a string by commas that are not in quotes, and remove outer quotes", () => {
15+
expect(splitHeader('"b,c", "\\"def\\"", a')).toEqual(["b,c", '"def"', "a"]);
16+
expect(splitHeader('"a,b,c", ""def"", "a,b ,c"')).toEqual(["a,b,c", '"def"', "a,b ,c"]);
17+
expect(splitHeader(`""`)).toEqual([``]);
18+
expect(splitHeader(``)).toEqual([``]);
19+
expect(splitHeader(`\\"`)).toEqual([`"`]);
20+
expect(splitHeader(`"`)).toEqual([`"`]);
21+
});
22+
});
Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
/**
2+
* @param value - header string value.
3+
* @returns value split by commas that aren't in quotes.
4+
*/
5+
export const splitHeader = (value: string): string[] => {
6+
const z = value.length;
7+
const values = [];
8+
9+
let withinQuotes = false;
10+
let prevChar = undefined;
11+
let anchor = 0;
12+
13+
for (let i = 0; i < z; ++i) {
14+
const char = value[i];
15+
switch (char) {
16+
case `"`:
17+
if (prevChar !== "\\") {
18+
withinQuotes = !withinQuotes;
19+
}
20+
break;
21+
case ",":
22+
if (!withinQuotes) {
23+
values.push(value.slice(anchor, i));
24+
anchor = i + 1;
25+
}
26+
break;
27+
default:
28+
}
29+
prevChar = char;
30+
}
31+
32+
values.push(value.slice(anchor));
33+
34+
return values.map((v) => {
35+
v = v.trim();
36+
const z = v.length;
37+
if (z < 2) {
38+
return v;
39+
}
40+
if (v[0] === `"` && v[z - 1] === `"`) {
41+
v = v.slice(1, z - 1);
42+
}
43+
return v.replace(/\\"/g, '"');
44+
});
45+
};

private/smithy-rpcv2-cbor/test/functional/rpcv2cbor.spec.ts

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -697,7 +697,7 @@ it("no_input:Request", async () => {
697697
expect(r.headers["smithy-protocol"]).toBeDefined();
698698
expect(r.headers["smithy-protocol"]).toBe("rpc-v2-cbor");
699699

700-
expect(r.body).toBeFalsy();
700+
expect(!r.body || r.body === `{}`).toBeTruthy();
701701
}
702702
});
703703

0 commit comments

Comments
 (0)