rest-nvim / rest.nvim

A fast Neovim http client written in Lua
GNU General Public License v3.0
1.61k stars 142 forks source link

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

Closed PhilRunninger closed 1 year ago

PhilRunninger commented 1 year ago

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.

ShivamJoker commented 1 year ago

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