Skip to content

Add case-insensitive table lookups, and use for host and content-type.#236

Merged
NTBBloodbath merged 1 commit intorest-nvim:mainfrom
PhilRunninger:case-insensitive-headers
Oct 10, 2023
Merged

Add case-insensitive table lookups, and use for host and content-type.#236
NTBBloodbath merged 1 commit intorest-nvim:mainfrom
PhilRunninger:case-insensitive-headers

Conversation

@PhilRunninger
Copy link
Copy Markdown
Contributor

@PhilRunninger PhilRunninger commented Sep 26, 2023

Closes #163.

  1. A new utility function called key takes a table and a key. It loops through the table items, comparing the each key with the given search key in a case-insensitive manner. Return the first matching key; otherwise, return the given search key. This allows operations like these to be performed:

    local t = {s=37, S=73}
    t[utils.key(t,'a')] = 42   -- Insert a=42             t == {s=37,S=73,a=42}
    y = t[utils.key(t,'a')]    -- Find 'a'                y == 42
    z = t[utils.key(t,'A')]    -- Find 'A' (same as 'a')  z == 42
    m = t[utils.key(t,'b')]    -- Show 'b' is missing     m == nil
    k = utils.key(t,'s')       -- Which 's/S' is first?   k is indeterminate
    l = t['s']                 -- Get 's' and 'S' in      l = 37
    u = t['S']                 --   the usual manner.     u = 73
    t[utils.key(t,'a')] = nil  -- Delete 'a'              t == {s=37, S=73}

    As implied by the k = example above, lua associative tables are unordered, so there's no guarantee that 's' (or 'S') is the first one to be found. In this context, that shouldn't be too much of an issue.

  2. Use this new function to find the 'Host' and 'Content-Type' headers' values no matter in what case they were defined.

A new utility function called `key` takes a table and a key. It loops
through the table items, comparing the each key with the given search
key in a case-insensitive manner. Return the first matching key;
otherwise, return the given search key. This allows operations like
these to be performed:

```lua
local t = {s=37, S=73}
t[utils.key(t,'a')] = 1  -- Insert a:1              t == {s=37,S=73,a=1}
y = t[utils.key(t,'a')]  -- Find 'a'                y == 1
z = t[utils.key(t,'A')]  -- Find 'A' (same as 'a')  z == 1
m = t[utils.key(t,'b')]  -- Show 'b' is missing.    m == nil
k = utils.key(t,'s')     -- Which 's/S' is first?   k is indeterminate
l = t['s']               -- Get 's' and 'S'         l = 37
u = t['S']               -- in the usual manner.    u = 73
t[utils.key(t,'a')] = nil  -- Delete 'a'            t == {s=37, S=73}
```

As implied by the `k = ` example above, lua associative tables are
unordered, so there's no guarantee that 's' (or 'S') is the first one to
be found. In this context, that shouldn't be too much of an issue.

Use this new function to find the 'Host' and 'Content-Type' headers'
values no matter in what case they were defined.
@ShivamJoker
Copy link
Copy Markdown

ShivamJoker commented Oct 10, 2023

@teto can we please get this merged? I struggled a lot with this case sensitivity.

Copy link
Copy Markdown
Member

@NTBBloodbath NTBBloodbath left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

LGTM! Sorry for the delay, life is a little busy and I can't fully focus on every project I have yet!

@NTBBloodbath NTBBloodbath merged commit 5bcaa10 into rest-nvim:main Oct 10, 2023
@PhilRunninger PhilRunninger deleted the case-insensitive-headers branch February 27, 2024 22:17
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Host or host?

3 participants