Title: Callback-Based Response Retrieval · Issue #23 · love2d/lua-https · GitHub
Open Graph Title: Callback-Based Response Retrieval · Issue #23 · love2d/lua-https
X Title: Callback-Based Response Retrieval · Issue #23 · love2d/lua-https
Description: One of the limitation in lua-https is that it's not possible for application to create download progress bars on it (if the Content-Length header is available). This is because https.request will wait until all the response data is retri...
Open Graph Description: One of the limitation in lua-https is that it's not possible for application to create download progress bars on it (if the Content-Length header is available). This is because https.request will w...
X Description: One of the limitation in lua-https is that it's not possible for application to create download progress bars on it (if the Content-Length header is available). This is because https.request wi...
Opengraph URL: https://github.com/love2d/lua-https/issues/23
X: @github
Domain: github.com
{"@context":"https://schema.org","@type":"DiscussionForumPosting","headline":"Callback-Based Response Retrieval","articleBody":"One of the limitation in `lua-https` is that it's not possible for application to create download progress bars on it (if the `Content-Length` header is available). This is because `https.request` will wait until all the response data is retrieved.\r\n\r\nMy proposal is to add optional 3rd and 4th parameter `callback` and `context`. Callback is defined as these fields:\r\n```lua\r\nlocal callback = {\r\n -- `context` in here means user-supplied data (4th-parameter), which can be anything.\r\n response = function(context, status, headers)\r\n -- Called when HTTP status code and headers has been retrieved. Called once.\r\n -- `status` is HTTP status code\r\n -- `headers` is HTTP headers in **all lowercase** for the keys.\r\n end,\r\n body = function(context, data)\r\n -- Called when new HTTP body data arrives. Called multiple times as new data arrives.\r\n -- `data` is response data as Lua string.\r\n -- Return \"falsy\" value and additional error message (default is \"aborted\") to abort the request\r\n return true -- success\r\n return false, \"aborted\" -- failure\r\n end,\r\n complete = function(context, err)\r\n -- Called when data has been transferred, or an error occurs.\r\n -- `err` will be the error message, or `nil` if everything worked as intended.\r\n end,\r\n}\r\n```\r\n\r\nExample usage:\r\n```lua\r\nlocal data = {current = 0, length = 0}\r\n\r\nhttps.request(\"https://love2d.org\", {}, {\r\n -- `context` in here means user-supplied data (4th-parameter), which can be anything.\r\n response = function(context, status, headers)\r\n print(\"HTTP status code\", status)\r\n if headers[\"content-length\"] then\r\n context.length = tonumber(headers[\"content-length\"]) or 0\r\n end\r\n end,\r\n body = function(context, data)\r\n context.current = context.current + #data\r\n if context.length \u003e 0 then\r\n print(string.format(\"Received data %d bytes (%.2f)\", #data, context.current * 100 / context.length))\r\n else\r\n print(string.format(\"Received data %d bytes\", #data))\r\n end\r\n end,\r\n complete = function(context, err)\r\n if err then\r\n print(\"Download error\", err)\r\n else\r\n print(\"Download completed!\")\r\n end\r\n end,\r\n}, data)\r\n```\r\n\r\nAs an additional feature/requirement, the `callback` table can be a class object (not instance) by retrieving the function list using Lua C function that invokes metamethod and the `context` can be the class instance itself such that when a class is declared like this:\r\n\r\n```lua\r\nlocal MyDownloader = class \"MyDownloader\"\r\n\r\nfunction MyDownloader:response(status, headers)\r\nend\r\n\r\nfunction MyDownloader:body(data)\r\n return true\r\nend\r\n\r\nfunction MyDownloader:complete(err)\r\nend\r\n```\r\n\r\nDoing something like this simply works and calls the appropriate functions.\r\n\r\n```lua\r\nlocal dlinstance = MyDownloader() -- new class instance\r\nhttps.request(\"https://love2d.org\", {}, MyDownloader, dlinstance)\r\n```\r\n\r\nThe return value of `https.request` when using this variant would be questionable.\r\n* Returning the status code, body, and the headers will preserve backward compatibility, but at same time make things redundant (the library has to keep everything until the function returns instead of discarding them).\r\n* Returning only `true` or `false` + error message (`assert`-compatible style) means less redundancy, but breaks backward compatibility.\r\n\r\nI probably can do this, but the question becomes \"should this is be done?\" and/or \"is this valid usecase to justify the feature addition?\".","author":{"url":"https://github.com/MikuAuahDark","@type":"Person","name":"MikuAuahDark"},"datePublished":"2023-07-02T05:02:17.000Z","interactionStatistic":{"@type":"InteractionCounter","interactionType":"https://schema.org/CommentAction","userInteractionCount":9},"url":"https://github.com/23/lua-https/issues/23"}
| route-pattern | /_view_fragments/issues/show/:user_id/:repository/:id/issue_layout(.:format) |
| route-controller | voltron_issues_fragments |
| route-action | issue_layout |
| fetch-nonce | v2:0bbd6ebb-70d1-57d4-2672-fe0d68c3888b |
| current-catalog-service-hash | 81bb79d38c15960b92d99bca9288a9108c7a47b18f2423d0f6438c5b7bcd2114 |
| request-id | 807A:1419FA:5AA262:81DF12:6A4D15B4 |
| html-safe-nonce | 39f347bcbdb1d4c68fc9fc26a188e5c04ad2d956fc1b0ff27bd004789961b527 |
| visitor-payload | eyJyZWZlcnJlciI6IiIsInJlcXVlc3RfaWQiOiI4MDdBOjE0MTlGQTo1QUEyNjI6ODFERjEyOjZBNEQxNUI0IiwidmlzaXRvcl9pZCI6IjQzMzk5NDcyMjc1NTQwNTk3MDAiLCJyZWdpb25fZWRnZSI6ImlhZCIsInJlZ2lvbl9yZW5kZXIiOiJpYWQifQ== |
| visitor-hmac | de43ff89c43819c1a364c40b8c237803b1a8ea205f1aed2c1fe7501d1f61f652 |
| hovercard-subject-tag | issue:1784452858 |
| github-keyboard-shortcuts | repository,issues,copilot |
| google-site-verification | Apib7-x98H0j5cPqHWwSMm6dNU4GmODRoqxLiDzdx9I |
| octolytics-url | https://collector.github.com/github/collect |
| analytics-location | / |
| fb:app_id | 1401488693436528 |
| apple-itunes-app | app-id=1477376905, app-argument=https://github.com/_view_fragments/issues/show/love2d/lua-https/23/issue_layout |
| twitter:image | https://opengraph.githubassets.com/09c01ca659688b8bec130e2a6421bb134792e1eb1415c79d56840a53554932a9/love2d/lua-https/issues/23 |
| twitter:card | summary_large_image |
| og:image | https://opengraph.githubassets.com/09c01ca659688b8bec130e2a6421bb134792e1eb1415c79d56840a53554932a9/love2d/lua-https/issues/23 |
| og:image:alt | One of the limitation in lua-https is that it's not possible for application to create download progress bars on it (if the Content-Length header is available). This is because https.request will w... |
| og:image:width | 1200 |
| og:image:height | 600 |
| og:site_name | GitHub |
| og:type | object |
| og:author:username | MikuAuahDark |
| hostname | github.com |
| expected-hostname | github.com |
| None | bcb4661e6fb4fe8b394c51ea02ccdb2236d40dc59afc75a3bbba50bf6517134c |
| turbo-cache-control | no-preview |
| go-import | github.com/love2d/lua-https git https://github.com/love2d/lua-https.git |
| octolytics-dimension-user_id | 283107 |
| octolytics-dimension-user_login | love2d |
| octolytics-dimension-repository_id | 435158752 |
| octolytics-dimension-repository_nwo | love2d/lua-https |
| octolytics-dimension-repository_public | true |
| octolytics-dimension-repository_is_fork | false |
| octolytics-dimension-repository_network_root_id | 435158752 |
| octolytics-dimension-repository_network_root_nwo | love2d/lua-https |
| turbo-body-classes | logged-out env-production page-responsive |
| disable-turbo | false |
| browser-stats-url | https://api.github.com/_private/browser/stats |
| browser-errors-url | https://api.github.com/_private/browser/errors |
| release | fb70bd3c4b2bec429781b65419e912c66e2d5581 |
| ui-target | full |
| theme-color | #1e2327 |
| color-scheme | light dark |
Links:
Viewport: width=device-width