Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[feat] added ability to opt-out from mime formatted header names #145

Merged
merged 12 commits into from
May 6, 2021
Merged
3 changes: 1 addition & 2 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -444,13 +444,12 @@ const sub = nc.subscribe(subj);
for (const [key, value] of m.headers) {
console.log(`${key}=${value}`);
}
// reading/setting a header is not case sensitive
// reading a header is not case sensitive
console.log("id", m.headers.get("id"));
}
}
})().then();

// headers always have their names turned into a canonical mime header key
// header names can be any printable ASCII character with the exception of `:`.
// header values can be any ASCII character except `\r` or `\n`.
// see https://www.ietf.org/rfc/rfc822.txt
Expand Down
1 change: 0 additions & 1 deletion doc/snippets/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ const sub = nc.subscribe(subj);
}
})().then();

// headers always have their names turned into a cannoncal mime header key
// header names can be any printable ASCII character with the exception of `:`.
// header values can be any ASCII character except `\r` or `\n`.
// see https://www.ietf.org/rfc/rfc822.txt
Expand Down
91 changes: 61 additions & 30 deletions nats-base-client/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -22,12 +22,14 @@ export interface MsgHdrs extends Iterable<[string, string[]]> {
hasError: boolean;
status: string;
code: number;
get(k: string): string;
set(k: string, v: string): void;
description: string;

get(k: string, exact?: boolean): string;
set(k: string, v: string, exact?: boolean): void;
append(k: string, v: string): void;
has(k: string): boolean;
values(k: string): string[];
delete(k: string): void;
has(k: string, exact?: boolean): boolean;
values(k: string, exact?: boolean): string[];
delete(k: string, exact?: boolean): void;
}

export function headers(): MsgHdrs {
Expand Down Expand Up @@ -59,7 +61,7 @@ export class MsgHdrsImpl implements MsgHdrs {
return count;
}

equals(mh: MsgHdrsImpl) {
equals(mh: MsgHdrsImpl): boolean {
if (
mh && this.headers.size === mh.headers.size &&
this.code === mh.code
Expand All @@ -76,8 +78,8 @@ export class MsgHdrsImpl implements MsgHdrs {
return false;
}
}
return true;
}
return true;
}
return false;
}
Expand All @@ -91,12 +93,8 @@ export class MsgHdrsImpl implements MsgHdrs {
let str = h.replace(HEADER, "");
mh.code = parseInt(str, 10);
const scode = mh.code.toString();
mh.set("Status", scode);
str = str.replace(scode, "");
mh.description = str.trim();
if (mh.description) {
mh.set("Description", mh.description);
}
} else {
lines.slice(1).map((s) => {
if (s) {
Expand Down Expand Up @@ -182,41 +180,74 @@ export class MsgHdrsImpl implements MsgHdrs {
return k.trim();
}

get(k: string): string {
const key = MsgHdrsImpl.canonicalMIMEHeaderKey(k);
const a = this.headers.get(key);
return a ? a[0] : "";
keys(): string[] {
const keys = [];
for (const sk of this.headers.keys()) {
keys.push(sk);
}
return keys;
}

has(k: string): boolean {
return this.get(k) !== "";
findKeys(k: string, exact = false): string[] {
if (exact) {
return [k];
}
const lci = k.toLowerCase();
const keys = this.keys();
return keys.filter((v) => {
return lci === v.toLowerCase();
});
}

set(k: string, v: string): void {
const key = MsgHdrsImpl.canonicalMIMEHeaderKey(k);
const value = MsgHdrsImpl.validHeaderValue(v);
this.headers.set(key, [value]);
get(k: string, exact = false): string {
const keys = this.findKeys(k, exact);
if (keys.length) {
const v = this.headers.get(keys[0]);
if (v) {
return Array.isArray(v) ? v[0] : v;
}
}
return "";
}

has(k: string, exact = false): boolean {
return this.findKeys(k, exact).length > 0;
}

set(k: string, v: string, exact = false): void {
this.delete(k, exact);
this.append(k, v);
}

append(k: string, v: string): void {
aricart marked this conversation as resolved.
Show resolved Hide resolved
const key = MsgHdrsImpl.canonicalMIMEHeaderKey(k);
// validate the key
MsgHdrsImpl.canonicalMIMEHeaderKey(k);
aricart marked this conversation as resolved.
Show resolved Hide resolved
const value = MsgHdrsImpl.validHeaderValue(v);
let a = this.headers.get(key);
let a = this.headers.get(k);
aricart marked this conversation as resolved.
Show resolved Hide resolved
if (!a) {
a = [];
this.headers.set(key, a);
this.headers.set(k, a);
}
a.push(value);
}

values(k: string): string[] {
const key = MsgHdrsImpl.canonicalMIMEHeaderKey(k);
return this.headers.get(key) || [];
values(k: string, exact = false): string[] {
const buf: string[] = [];
const keys = this.findKeys(k, exact);
keys.forEach((v) => {
const values = this.headers.get(v);
if (values) {
buf.push(...values);
}
});
return buf;
}

delete(k: string): void {
const key = MsgHdrsImpl.canonicalMIMEHeaderKey(k);
this.headers.delete(key);
delete(k: string, exact = false): void {
const keys = this.findKeys(k, exact);
keys.forEach((v) => {
this.headers.delete(v);
});
}

get hasError() {
Expand Down
4 changes: 2 additions & 2 deletions nats-base-client/msg.ts
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,10 @@ export function isRequestError(msg: Msg): (NatsError | null) {
if (msg && msg.headers) {
const headers = msg.headers as MsgHdrsImpl;
if (headers.hasError) {
if (headers.status === "503") {
if (headers.code === 503) {
return NatsError.errorForCode(ErrorCode.NoResponders);
} else {
let desc = headers.get("description");
let desc = headers.description;
if (desc === "") {
desc = ErrorCode.RequestError;
}
Expand Down
78 changes: 76 additions & 2 deletions tests/headers_test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -106,9 +106,8 @@ function checkStatus(code = 200, description = "") {
assertEquals(h.code, code);
assertEquals(h.description, description);
assertEquals(h.status, `${code} ${description}`.trim());
assertEquals(h.get("status"), code.toString());
}
assertEquals(h.get("description"), description);
assertEquals(h.description, description);
}

Deno.test("headers - status", () => {
Expand All @@ -118,3 +117,78 @@ Deno.test("headers - status", () => {
checkStatus(503, "No Responders");
checkStatus(404, "No Messages");
});

Deno.test("headers - lookups are case insensitive", () => {
const h = headers();
h.set("a", "aa");
h.set("A", "AA");
const v = h.get("a");
assertEquals(v, "AA");

const a = h.values("a");
assertEquals(a, ["AA"]);

const A = h.values("A");
assertEquals(A, ["AA"]);
});

Deno.test("headers - keys serialize as provided", () => {
const h = headers() as MsgHdrsImpl;
h.set("a", "b");
assert(h.headers.has("a"));

h.set("hello-world", "X");
assert(h.has("hello-world"));
assert(h.headers.has("hello-world"));
});

Deno.test("headers - values should be arrays", () => {
const h = headers() as MsgHdrsImpl;
h.set("a", "aa");
assertEquals(h.headers.get("a"), ["aa"]);
h.append("b", "bb");
assertEquals(h.headers.get("b"), ["bb"]);
});

Deno.test("headers - equality", () => {
const a = headers() as MsgHdrsImpl;
const b = headers() as MsgHdrsImpl;
assert(a.equals(b));

a.set("a", "b");
b.set("a", "b");
assert(a.equals(b));

b.append("a", "bb");
assert(!a.equals(b));

a.append("a", "cc");
assert(!a.equals(b));
});

Deno.test("headers - set", () => {
const a = headers() as MsgHdrsImpl;
a.set("a", "b");
a.set("a", "c");
assertEquals(a.values("a"), ["c"]);
});

Deno.test("headers - exact", () => {
const a = headers() as MsgHdrsImpl;
a.set("a", "b");
a.set("A", "c", true);
assertEquals(a.get("a", true), "b");
assertEquals(a.values("a", true), ["b"]);
assertEquals(a.values("a"), ["b", "c"]);

a.append("a", "d");
assertEquals(a.values("a", true), ["b", "d"]);

a.delete("a", true);
assertEquals(a.values("a", true), []);
assertEquals(a.values("a"), ["c"]);

a.set("A", "x");
assertEquals(a.values("a", true), []);
assertEquals(a.values("a"), ["x"]);
});