hammerspoon/extensions/http/http.lua

490 lines
28 KiB
Lua

-- Simple loader for the Objective C code
local http = require("hs.libhttp")
local utf8 = require("hs.utf8")
local fnutils = require("hs.fnutils")
--- === hs.http ===
---
--- Perform HTTP requests
--- hs.http.get(url, headers) -> int, string, table
--- Function
--- Sends an HTTP GET request to a URL
---
--- Parameters:
--- * url - A string containing the URL to retrieve
--- * headers - A table containing string keys and values representing the request headers, or nil to add no headers
---
--- Returns:
--- * A number containing the HTTP response status
--- * A string containing the response body
--- * A table containing the response headers
---
--- Notes:
--- * If authentication is required in order to download the request, the required credentials must be specified as part of the URL (e.g. "http://user:password@host.com/"). If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.
--- * This function is synchronous and will therefore block all other Lua execution while the request is in progress, you are encouraged to use the asynchronous functions
--- * If you attempt to connect to a local Hammerspoon server created with `hs.httpserver`, then Hammerspoon will block until the connection times out (60 seconds), return a failed result due to the timeout, and then the `hs.httpserver` callback function will be invoked (so any side effects of the function will occur, but it's results will be lost). Use [hs.http.asyncGet](#asyncGet) to avoid this.
http.get = function(url, headers)
return http.doRequest(url, "GET", nil, headers)
end
--- hs.http.post(url, data, headers) -> int, string, table
--- Function
--- Sends an HTTP POST request to a URL
---
--- Parameters:
--- * url - A string containing the URL to submit to
--- * data - A string containing the request body, or nil to send no body
--- * headers - A table containing string keys and values representing the request headers, or nil to add no headers
---
--- Returns:
--- * A number containing the HTTP response status
--- * A string containing the response body
--- * A table containing the response headers
---
--- Notes:
--- * If authentication is required in order to download the request, the required credentials must be specified as part of the URL (e.g. "http://user:password@host.com/"). If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.
--- * This function is synchronous and will therefore block all other Lua execution while the request is in progress, you are encouraged to use the asynchronous functions
--- * If you attempt to connect to a local Hammerspoon server created with `hs.httpserver`, then Hammerspoon will block until the connection times out (60 seconds), return a failed result due to the timeout, and then the `hs.httpserver` callback function will be invoked (so any side effects of the function will occur, but it's results will be lost). Use [hs.http.asyncPost](#asyncPost) to avoid this.
http.post = function(url, data, headers)
return http.doRequest(url, "POST", data,headers)
end
--- hs.http.put(url, data, headers) -> int, string, table
--- Function
--- Sends an HTTP PUT request to a URL
---
--- Parameters:
--- * url - A string containing the URL to submit to
--- * data - A string containing the request body, or nil to send no body
--- * headers - A table containing string keys and values representing the request headers, or nil to add no headers
---
--- Returns:
--- * A number containing the HTTP response status
--- * A string containing the response body
--- * A table containing the response headers
---
--- Notes:
--- * If authentication is required in order to download the request, the required credentials must be specified as part of the URL (e.g. "http://user:password@host.com/"). If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.
--- * This function is synchronous and will therefore block all other Lua execution while the request is in progress, you are encouraged to use the asynchronous functions
--- * If you attempt to connect to a local Hammerspoon server created with `hs.httpserver`, then Hammerspoon will block until the connection times out (60 seconds), return a failed result due to the timeout, and then the `hs.httpserver` callback function will be invoked (so any side effects of the function will occur, but it's results will be lost). Use [hs.http.asyncPost](#asyncPost) to avoid this.
http.put = function(url, data, headers)
return http.doRequest(url, "PUT", data,headers)
end
--- hs.http.asyncGet(url, headers, callback)
--- Function
--- Sends an HTTP GET request asynchronously
---
--- Parameters:
--- * url - A string containing the URL to retrieve
--- * headers - A table containing string keys and values representing the request headers, or nil to add no headers
--- * callback - A function to be called when the request succeeds or fails. The function will be passed three parameters:
--- * A number containing the HTTP response status
--- * A string containing the response body
--- * A table containing the response headers
---
--- Returns:
--- * None
---
--- Notes:
--- * If authentication is required in order to download the request, the required credentials must be specified as part of the URL (e.g. "http://user:password@host.com/"). If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.
--- * If the request fails, the callback function's first parameter will be negative and the second parameter will contain an error message. The third parameter will be nil
http.asyncGet = function(url, headers, callback)
http.doAsyncRequest(url, "GET", nil, headers, callback)
end
--- hs.http.asyncPost(url, data, headers, callback)
--- Function
--- Sends an HTTP POST request asynchronously
---
--- Parameters:
--- * url - A string containing the URL to submit to
--- * data - A string containing the request body, or nil to send no body
--- * headers - A table containing string keys and values representing the request headers, or nil to add no headers
--- * callback - A function to be called when the request succeeds or fails. The function will be passed three parameters:
--- * A number containing the HTTP response status
--- * A string containing the response body
--- * A table containing the response headers
---
--- Returns:
--- * None
---
--- Notes:
--- * If authentication is required in order to download the request, the required credentials must be specified as part of the URL (e.g. "http://user:password@host.com/"). If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.
--- * If the request fails, the callback function's first parameter will be negative and the second parameter will contain an error message. The third parameter will be nil
http.asyncPost = function(url, data, headers, callback)
http.doAsyncRequest(url, "POST", data, headers, callback)
end
--- hs.http.asyncPut(url, data, headers, callback)
--- Function
--- Sends an HTTP PUT request asynchronously
---
--- Parameters:
--- * url - A string containing the URL to submit to
--- * data - A string containing the request body, or nil to send no body
--- * headers - A table containing string keys and values representing the request headers, or nil to add no headers
--- * callback - A function to be called when the request succeeds or fails. The function will be passed three parameters:
--- * A number containing the HTTP response status
--- * A string containing the response body
--- * A table containing the response headers
---
--- Returns:
--- * None
---
--- Notes:
--- * If authentication is required in order to download the request, the required credentials must be specified as part of the URL (e.g. "http://user:password@host.com/"). If authentication fails, or credentials are missing, the connection will attempt to continue without credentials.
--- * If the request fails, the callback function's first parameter will be negative and the second parameter will contain an error message. The third parameter will be nil
http.asyncPut = function(url, data, headers, callback)
http.doAsyncRequest(url, "PUT", data, headers, callback)
end
--- hs.http.htmlEntities[]
--- Variable
--- A collection of common HTML Entities (&whatever;) and their UTF8 equivalents. To retrieve the UTF8 sequence for a given entity, reference the table as `hs.http.htmlEntities["&key;"]` where `key` is the text of the entity's name or a numeric reference like `#number`.
---
--- Notes:
--- * This list is likely not complete. It is based on the list of common entities described at http://www.freeformatter.com/html-entities.html.
--- * Additional entities can be temporarily added via the `hs.http.registerEntity(...)` function. If you feel you have a more official list of entities which contains items which are currently not included by default, please open up an issue at https://github.com/Hammerspoon/hammerspoon and your link will be considered.
--- * To see a list of the currently defined entities, a __tostring meta-method is included so that referencing the table directly as a string will return the current definitions.
--- * For reference, this meta-method is essentially the following:
---
--- for i,v in hs.fnutils.sortByKeys(hs.http.htmlEntities) do print(string.format("%-10s %-10s %s\n", i, "&#"..tostring(hs.utf8.codepoint(v))..";", v)) end
---
--- * Note that this list will not include the numeric conversion of entities (e.g. A), as this is handled by an __index metamethod to allow for all possible numeric values.
http.htmlEntities = setmetatable({}, { __index = function(_, key)
if type(key) == "string" then
local num = key:match("^&#(%d+);$")
if num and tonumber(num) then
return utf8.codepointToUTF8(tonumber(num))
else
return nil
end
else
return nil
end
end,
__tostring = function(object)
local output = ""
for i,v in fnutils.sortByKeys(object) do
output = output..string.format("%-10s %-10s %s\n", i, "&#"..tostring(utf8.codepoint(v))..";", v)
end
return output
end
})
http.htmlEntities["Á"] = utf8.codepointToUTF8(193)
http.htmlEntities["á"] = utf8.codepointToUTF8(225)
http.htmlEntities["Â"] = utf8.codepointToUTF8(194)
http.htmlEntities["â"] = utf8.codepointToUTF8(226)
http.htmlEntities["´"] = utf8.codepointToUTF8(180)
http.htmlEntities["Æ"] = utf8.codepointToUTF8(198)
http.htmlEntities["æ"] = utf8.codepointToUTF8(230)
http.htmlEntities["À"] = utf8.codepointToUTF8(192)
http.htmlEntities["à"] = utf8.codepointToUTF8(224)
http.htmlEntities["Α"] = utf8.codepointToUTF8(913)
http.htmlEntities["α"] = utf8.codepointToUTF8(945)
http.htmlEntities["&"] = utf8.codepointToUTF8(38)
http.htmlEntities["∧"] = utf8.codepointToUTF8(8743)
http.htmlEntities["∠"] = utf8.codepointToUTF8(8736)
http.htmlEntities["Å"] = utf8.codepointToUTF8(197)
http.htmlEntities["å"] = utf8.codepointToUTF8(229)
http.htmlEntities["≈"] = utf8.codepointToUTF8(8776)
http.htmlEntities["Ã"] = utf8.codepointToUTF8(195)
http.htmlEntities["ã"] = utf8.codepointToUTF8(227)
http.htmlEntities["Ä"] = utf8.codepointToUTF8(196)
http.htmlEntities["ä"] = utf8.codepointToUTF8(228)
http.htmlEntities["„"] = utf8.codepointToUTF8(8222)
http.htmlEntities["Β"] = utf8.codepointToUTF8(914)
http.htmlEntities["β"] = utf8.codepointToUTF8(946)
http.htmlEntities["¦"] = utf8.codepointToUTF8(166)
http.htmlEntities["•"] = utf8.codepointToUTF8(8226)
http.htmlEntities["∩"] = utf8.codepointToUTF8(8745)
http.htmlEntities["Ç"] = utf8.codepointToUTF8(199)
http.htmlEntities["ç"] = utf8.codepointToUTF8(231)
http.htmlEntities["¸"] = utf8.codepointToUTF8(184)
http.htmlEntities["¢"] = utf8.codepointToUTF8(162)
http.htmlEntities["Χ"] = utf8.codepointToUTF8(935)
http.htmlEntities["χ"] = utf8.codepointToUTF8(967)
http.htmlEntities["ˆ"] = utf8.codepointToUTF8(710)
http.htmlEntities["♣"] = utf8.codepointToUTF8(9827)
http.htmlEntities["≅"] = utf8.codepointToUTF8(8773)
http.htmlEntities["©"] = utf8.codepointToUTF8(169)
http.htmlEntities["↵"] = utf8.codepointToUTF8(8629)
http.htmlEntities["∪"] = utf8.codepointToUTF8(8746)
http.htmlEntities["¤"] = utf8.codepointToUTF8(164)
http.htmlEntities["†"] = utf8.codepointToUTF8(8224)
http.htmlEntities["‡"] = utf8.codepointToUTF8(8225)
http.htmlEntities["↓"] = utf8.codepointToUTF8(8595)
http.htmlEntities["°"] = utf8.codepointToUTF8(176)
http.htmlEntities["Δ"] = utf8.codepointToUTF8(916)
http.htmlEntities["δ"] = utf8.codepointToUTF8(948)
http.htmlEntities["♦"] = utf8.codepointToUTF8(9830)
http.htmlEntities["÷"] = utf8.codepointToUTF8(247)
http.htmlEntities["É"] = utf8.codepointToUTF8(201)
http.htmlEntities["é"] = utf8.codepointToUTF8(233)
http.htmlEntities["Ê"] = utf8.codepointToUTF8(202)
http.htmlEntities["ê"] = utf8.codepointToUTF8(234)
http.htmlEntities["È"] = utf8.codepointToUTF8(200)
http.htmlEntities["è"] = utf8.codepointToUTF8(232)
http.htmlEntities["∅"] = utf8.codepointToUTF8(8709)
http.htmlEntities[" "] = utf8.codepointToUTF8(8195)
http.htmlEntities[" "] = utf8.codepointToUTF8(8194)
http.htmlEntities["Ε"] = utf8.codepointToUTF8(917)
http.htmlEntities["ε"] = utf8.codepointToUTF8(949)
http.htmlEntities["≡"] = utf8.codepointToUTF8(8801)
http.htmlEntities["Η"] = utf8.codepointToUTF8(919)
http.htmlEntities["η"] = utf8.codepointToUTF8(951)
http.htmlEntities["Ð"] = utf8.codepointToUTF8(208)
http.htmlEntities["ð"] = utf8.codepointToUTF8(240)
http.htmlEntities["Ë"] = utf8.codepointToUTF8(203)
http.htmlEntities["ë"] = utf8.codepointToUTF8(235)
http.htmlEntities["€"] = utf8.codepointToUTF8(8364)
http.htmlEntities["∃"] = utf8.codepointToUTF8(8707)
http.htmlEntities["ƒ"] = utf8.codepointToUTF8(402)
http.htmlEntities["∀"] = utf8.codepointToUTF8(8704)
http.htmlEntities["½"] = utf8.codepointToUTF8(189)
http.htmlEntities["¼"] = utf8.codepointToUTF8(188)
http.htmlEntities["¾"] = utf8.codepointToUTF8(190)
http.htmlEntities["Γ"] = utf8.codepointToUTF8(915)
http.htmlEntities["γ"] = utf8.codepointToUTF8(947)
http.htmlEntities["≥"] = utf8.codepointToUTF8(8805)
http.htmlEntities[">"] = utf8.codepointToUTF8(62)
http.htmlEntities["↔"] = utf8.codepointToUTF8(8596)
http.htmlEntities["♥"] = utf8.codepointToUTF8(9829)
http.htmlEntities["…"] = utf8.codepointToUTF8(8230)
http.htmlEntities["Í"] = utf8.codepointToUTF8(205)
http.htmlEntities["í"] = utf8.codepointToUTF8(237)
http.htmlEntities["Î"] = utf8.codepointToUTF8(206)
http.htmlEntities["î"] = utf8.codepointToUTF8(238)
http.htmlEntities["¡"] = utf8.codepointToUTF8(161)
http.htmlEntities["Ì"] = utf8.codepointToUTF8(204)
http.htmlEntities["ì"] = utf8.codepointToUTF8(236)
http.htmlEntities["∞"] = utf8.codepointToUTF8(8734)
http.htmlEntities["∫"] = utf8.codepointToUTF8(8747)
http.htmlEntities["Ι"] = utf8.codepointToUTF8(921)
http.htmlEntities["ι"] = utf8.codepointToUTF8(953)
http.htmlEntities["¿"] = utf8.codepointToUTF8(191)
http.htmlEntities["∈"] = utf8.codepointToUTF8(8712)
http.htmlEntities["Ï"] = utf8.codepointToUTF8(207)
http.htmlEntities["ï"] = utf8.codepointToUTF8(239)
http.htmlEntities["Κ"] = utf8.codepointToUTF8(922)
http.htmlEntities["κ"] = utf8.codepointToUTF8(954)
http.htmlEntities["Λ"] = utf8.codepointToUTF8(923)
http.htmlEntities["λ"] = utf8.codepointToUTF8(955)
http.htmlEntities["«"] = utf8.codepointToUTF8(171)
http.htmlEntities["←"] = utf8.codepointToUTF8(8592)
http.htmlEntities["⌈"] = utf8.codepointToUTF8(8968)
http.htmlEntities["“"] = utf8.codepointToUTF8(8220)
http.htmlEntities["≤"] = utf8.codepointToUTF8(8804)
http.htmlEntities["⌊"] = utf8.codepointToUTF8(8970)
http.htmlEntities["∗"] = utf8.codepointToUTF8(8727)
http.htmlEntities["◊"] = utf8.codepointToUTF8(9674)
http.htmlEntities["‎"] = utf8.codepointToUTF8(8206)
http.htmlEntities["‹"] = utf8.codepointToUTF8(8249)
http.htmlEntities["‘"] = utf8.codepointToUTF8(8216)
http.htmlEntities["<"] = utf8.codepointToUTF8(60)
http.htmlEntities["¯"] = utf8.codepointToUTF8(175)
http.htmlEntities["—"] = utf8.codepointToUTF8(8212)
http.htmlEntities["µ"] = utf8.codepointToUTF8(181)
http.htmlEntities["·"] = utf8.codepointToUTF8(183)
http.htmlEntities["−"] = utf8.codepointToUTF8(8722)
http.htmlEntities["Μ"] = utf8.codepointToUTF8(924)
http.htmlEntities["μ"] = utf8.codepointToUTF8(956)
http.htmlEntities["∇"] = utf8.codepointToUTF8(8711)
http.htmlEntities[" "] = utf8.codepointToUTF8(160)
http.htmlEntities["–"] = utf8.codepointToUTF8(8211)
http.htmlEntities["≠"] = utf8.codepointToUTF8(8800)
http.htmlEntities["∋"] = utf8.codepointToUTF8(8715)
http.htmlEntities["¬"] = utf8.codepointToUTF8(172)
http.htmlEntities["∉"] = utf8.codepointToUTF8(8713)
http.htmlEntities["⊄"] = utf8.codepointToUTF8(8836)
http.htmlEntities["Ñ"] = utf8.codepointToUTF8(209)
http.htmlEntities["ñ"] = utf8.codepointToUTF8(241)
http.htmlEntities["Ν"] = utf8.codepointToUTF8(925)
http.htmlEntities["ν"] = utf8.codepointToUTF8(957)
http.htmlEntities["Ó"] = utf8.codepointToUTF8(211)
http.htmlEntities["ó"] = utf8.codepointToUTF8(243)
http.htmlEntities["Ô"] = utf8.codepointToUTF8(212)
http.htmlEntities["ô"] = utf8.codepointToUTF8(244)
http.htmlEntities["Œ"] = utf8.codepointToUTF8(338)
http.htmlEntities["œ"] = utf8.codepointToUTF8(339)
http.htmlEntities["Ò"] = utf8.codepointToUTF8(210)
http.htmlEntities["ò"] = utf8.codepointToUTF8(242)
http.htmlEntities["‾"] = utf8.codepointToUTF8(8254)
http.htmlEntities["Ω"] = utf8.codepointToUTF8(937)
http.htmlEntities["ω"] = utf8.codepointToUTF8(969)
http.htmlEntities["Ο"] = utf8.codepointToUTF8(927)
http.htmlEntities["ο"] = utf8.codepointToUTF8(959)
http.htmlEntities["⊕"] = utf8.codepointToUTF8(8853)
http.htmlEntities["∨"] = utf8.codepointToUTF8(8744)
http.htmlEntities["ª"] = utf8.codepointToUTF8(170)
http.htmlEntities["º"] = utf8.codepointToUTF8(186)
http.htmlEntities["Ø"] = utf8.codepointToUTF8(216)
http.htmlEntities["ø"] = utf8.codepointToUTF8(248)
http.htmlEntities["Õ"] = utf8.codepointToUTF8(213)
http.htmlEntities["õ"] = utf8.codepointToUTF8(245)
http.htmlEntities["⊗"] = utf8.codepointToUTF8(8855)
http.htmlEntities["Ö"] = utf8.codepointToUTF8(214)
http.htmlEntities["ö"] = utf8.codepointToUTF8(246)
http.htmlEntities["¶"] = utf8.codepointToUTF8(182)
http.htmlEntities["∂"] = utf8.codepointToUTF8(8706)
http.htmlEntities["‰"] = utf8.codepointToUTF8(8240)
http.htmlEntities["⊥"] = utf8.codepointToUTF8(8869)
http.htmlEntities["Φ"] = utf8.codepointToUTF8(934)
http.htmlEntities["φ"] = utf8.codepointToUTF8(966)
http.htmlEntities["Π"] = utf8.codepointToUTF8(928)
http.htmlEntities["π"] = utf8.codepointToUTF8(960)
http.htmlEntities["ϖ"] = utf8.codepointToUTF8(982)
http.htmlEntities["±"] = utf8.codepointToUTF8(177)
http.htmlEntities["£"] = utf8.codepointToUTF8(163)
http.htmlEntities["′"] = utf8.codepointToUTF8(8242)
http.htmlEntities["″"] = utf8.codepointToUTF8(8243)
http.htmlEntities["∏"] = utf8.codepointToUTF8(8719)
http.htmlEntities["∝"] = utf8.codepointToUTF8(8733)
http.htmlEntities["Ψ"] = utf8.codepointToUTF8(936)
http.htmlEntities["ψ"] = utf8.codepointToUTF8(968)
http.htmlEntities["√"] = utf8.codepointToUTF8(8730)
http.htmlEntities["»"] = utf8.codepointToUTF8(187)
http.htmlEntities["→"] = utf8.codepointToUTF8(8594)
http.htmlEntities["⌉"] = utf8.codepointToUTF8(8969)
http.htmlEntities["”"] = utf8.codepointToUTF8(8221)
http.htmlEntities["®"] = utf8.codepointToUTF8(174)
http.htmlEntities["⌋"] = utf8.codepointToUTF8(8971)
http.htmlEntities["Ρ"] = utf8.codepointToUTF8(929)
http.htmlEntities["ρ"] = utf8.codepointToUTF8(961)
http.htmlEntities["‏"] = utf8.codepointToUTF8(8207)
http.htmlEntities["›"] = utf8.codepointToUTF8(8249)
http.htmlEntities["’"] = utf8.codepointToUTF8(8217)
http.htmlEntities["‚"] = utf8.codepointToUTF8(8218)
http.htmlEntities["Š"] = utf8.codepointToUTF8(352)
http.htmlEntities["š"] = utf8.codepointToUTF8(353)
http.htmlEntities["⋅"] = utf8.codepointToUTF8(8901)
http.htmlEntities["§"] = utf8.codepointToUTF8(167)
http.htmlEntities["­"] = utf8.codepointToUTF8(173)
http.htmlEntities["Σ"] = utf8.codepointToUTF8(931)
http.htmlEntities["σ"] = utf8.codepointToUTF8(963)
http.htmlEntities["σ"] = utf8.codepointToUTF8(963)
http.htmlEntities["ς"] = utf8.codepointToUTF8(962)
http.htmlEntities["∼"] = utf8.codepointToUTF8(8764)
http.htmlEntities["♠"] = utf8.codepointToUTF8(9824)
http.htmlEntities["⊂"] = utf8.codepointToUTF8(8834)
http.htmlEntities["⊆"] = utf8.codepointToUTF8(8838)
http.htmlEntities["∑"] = utf8.codepointToUTF8(8721)
http.htmlEntities["⊃"] = utf8.codepointToUTF8(8835)
http.htmlEntities["¹"] = utf8.codepointToUTF8(185)
http.htmlEntities["²"] = utf8.codepointToUTF8(178)
http.htmlEntities["³"] = utf8.codepointToUTF8(179)
http.htmlEntities["⊇"] = utf8.codepointToUTF8(8839)
http.htmlEntities["ß"] = utf8.codepointToUTF8(223)
http.htmlEntities["Τ"] = utf8.codepointToUTF8(932)
http.htmlEntities["τ"] = utf8.codepointToUTF8(964)
http.htmlEntities["∴"] = utf8.codepointToUTF8(8756)
http.htmlEntities["Θ"] = utf8.codepointToUTF8(920)
http.htmlEntities["θ"] = utf8.codepointToUTF8(952)
http.htmlEntities["ϑ"] = utf8.codepointToUTF8(977)
http.htmlEntities[" "] = utf8.codepointToUTF8(8201)
http.htmlEntities["Þ"] = utf8.codepointToUTF8(222)
http.htmlEntities["þ"] = utf8.codepointToUTF8(254)
http.htmlEntities["˜"] = utf8.codepointToUTF8(732)
http.htmlEntities["×"] = utf8.codepointToUTF8(215)
http.htmlEntities["™"] = utf8.codepointToUTF8(8482)
http.htmlEntities["Ú"] = utf8.codepointToUTF8(218)
http.htmlEntities["ú"] = utf8.codepointToUTF8(250)
http.htmlEntities["↑"] = utf8.codepointToUTF8(8593)
http.htmlEntities["Û"] = utf8.codepointToUTF8(219)
http.htmlEntities["û"] = utf8.codepointToUTF8(251)
http.htmlEntities["Ù"] = utf8.codepointToUTF8(217)
http.htmlEntities["ù"] = utf8.codepointToUTF8(249)
http.htmlEntities["¨"] = utf8.codepointToUTF8(168)
http.htmlEntities["ϒ"] = utf8.codepointToUTF8(978)
http.htmlEntities["Υ"] = utf8.codepointToUTF8(933)
http.htmlEntities["υ"] = utf8.codepointToUTF8(965)
http.htmlEntities["Ü"] = utf8.codepointToUTF8(220)
http.htmlEntities["ü"] = utf8.codepointToUTF8(252)
http.htmlEntities["Ξ"] = utf8.codepointToUTF8(926)
http.htmlEntities["ξ"] = utf8.codepointToUTF8(958)
http.htmlEntities["Ý"] = utf8.codepointToUTF8(221)
http.htmlEntities["ý"] = utf8.codepointToUTF8(253)
http.htmlEntities["¥"] = utf8.codepointToUTF8(165)
http.htmlEntities["ÿ"] = utf8.codepointToUTF8(255)
http.htmlEntities["Ÿ"] = utf8.codepointToUTF8(376)
http.htmlEntities["Ζ"] = utf8.codepointToUTF8(918)
http.htmlEntities["ζ"] = utf8.codepointToUTF8(950)
http.htmlEntities["‍"] = utf8.codepointToUTF8(8205)
http.htmlEntities["‌"] = utf8.codepointToUTF8(8204)
--- hs.http.registerEntity(entity, codepoint) -> string
--- Function
--- Registers an HTML Entity with the specified Unicode codepoint which can later referenced in your code as `hs.http.htmlEntity[entity]` for convenience and readability.
---
--- Parameters:
--- * entity -- The full text of the HTML Entity as it appears in HTML encoded documents. A proper entity starts with & and ends with ; and entity labels which do not meet this will have them added -- future dereferences to get the corresponding UTF8 *must* include this initiator and terminator or they will not be recognized.
--- * codepoint -- a Unicode codepoint in numeric or `U+xxxx` format to register with as the given entity.
---
--- Returns:
--- * Returns the UTF8 byte sequence for the entity registered.
---
--- Notes:
--- * If an entity label was previously registered, this will overwrite the previous value with a new one.
--- * The return value is merely syntactic sugar and you do not need to save it locally; it can be safely ignored -- future access to the pre-converted entity should be retrieved as `hs.http.htmlEntities[entity]` in your code. It looks good when invoked from the console, though ☺.
http.registerEntity = function(label, codepoint)
local entity = label:match("^&?([^&;]+);?$")
if not entity then
return error("Invalid label '"..label.."' provided to hs.http.registerEntity", 2)
else
label = "&"..entity..";"
http.htmlEntities[label] = utf8.codepointToUTF8(codepoint)
return http.htmlEntities[label]
end
end
--- hs.http.convertHtmlEntities(inString) -> outString
--- Function
--- Convert all recognized HTML Entities in the `inString` to appropriate UTF8 byte sequences and returns the converted text.
---
--- Parameters:
--- * inString -- A string containing any number of HTML Entities (&whatever;) in the text.
---
--- Returns:
--- * outString -- the input string with all recognized HTML Entity sequences converted to UTF8 byte sequences.
---
--- Notes:
--- * Recognized HTML Entities are those registered in `hs.http.htmlEntities` or numeric entity sequences: &#n; where `n` can be any integer number.
--- * This function is especially useful as a post-filter to data retrieved by the `hs.http.get` and `hs.http.asyncGet` functions.
http.convertHtmlEntities = function(input)
return input:gsub("&[^;]+;", function(c) return http.htmlEntities[c] or c end)
end
--- hs.http.encodeForQuery(string) -> string
--- Function
--- Returns a copy of the provided string in which characters that are not valid within an HTTP query key or value are escaped with their %## equivalent.
---
--- Parameters:
--- * originalString - the string to make safe as a key or value for a query
---
--- Returns:
--- * the converted string
---
--- Notes:
--- * The intent of this function is to provide a valid key or a valid value for a query string, not to validate the entire query string. For this reason, ?, =, +, and & are included in the converted characters.
local encodeForQuery = http.encodeForQuery
http.encodeForQuery = function(...)
return (encodeForQuery(...):gsub("[%?=&+]", { ["?"] = "%3F", ["="] = "%3D", ["&"] = "%26", ["+"] = "%2B" } ))
end
-- Wrapper for legacy `hs.http.websocket(url, callback)`
-- This is undocumented, as `hs.http.websocket` was never originally exposed/documented.
local websocket = require("hs.websocket")
http.websocket = function(url, callback)
return websocket.new(url, function(status, message)
if type(callback) == "function" and status == "received" then
return callback(message)
end
end)
end
return http