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

Add Lua HTTP Client API #195

Closed
wants to merge 14 commits into from
Closed

Add Lua HTTP Client API #195

wants to merge 14 commits into from

Conversation

Dreae
Copy link

@Dreae Dreae commented Nov 27, 2023

This adds lua functions for sending HTTP POST and GET requests (closes #55). The requests are sent synchronously using the current httplib header on a boost::asio::thread_pool so as to not block the lua thread. The request results are returned to lua via callback functions.
It supports both HTTP and HTTPS, the API looks like this:

-- err - string error if something went wrong, or else nil
-- status - numeric HTTP status code
-- body - string response body
-- headers - response headers as a table 
Http.Get("http://localhost:8080", "/hello", function (err, status, body, headers)
    print(err)
    print(status)
    print(headers)
end)

Http.Post("http://localhost:8080", "/hello", { hello = "world" }, function (err, status, body, headers)
    print(err)
    print(status)
    print(headers)
end)

The following overloads are available for both new methods:

Http.Get(host, path, callback)
Http.Get(host, path, headers, callback)
-- For these methods body is passed as a table and sent as `application/x-www-form-urlencoded`
Http.Post(host, path, body, callback)
Http.Post(host, path, body, headers, callback)
-- For these methods body is passed as a string and you must provide a content_type
Http.Post(host, path, body, content_type, callback)
Http.Post(host, path, body, content_type, headers, callback)

There are some supporting changes to TLuaEngine and TLuaEngine::QueuedFunction to support enqueuing function calls to a reference to a function on the lua stack, as well as a new overload to StateThreadData::EnqueueFunction

[[nodiscard]] std::shared_ptr<TLuaResult> EnqueueFunctionCall(std::shared_ptr<sol::reference> FunctionRef, const std::vector<TLuaArgTypes>& Args);

@Dreae Dreae changed the title Add Lua HTTP Client API (closes #55) Add Lua HTTP Client API Nov 27, 2023
@lionkor
Copy link
Member

lionkor commented Dec 2, 2023

Hi, do you have Discord? I'd like to talk to you really quick :) If you could add me lion_kor that'd be awesome.

@lionkor lionkor self-requested a review December 8, 2023 16:20
@lionkor lionkor self-assigned this Dec 8, 2023
@lionkor
Copy link
Member

lionkor commented Dec 10, 2023

The existing "Http.CreateConnection" can be nuked, it's not used anywhere :)

@lionkor
Copy link
Member

lionkor commented Dec 21, 2023

Rebased to update to current master, if you can't pull now, try git fetch && git reset --hard origin/master (this will reset your own changes if you made any that aren't pushed)

@lionkor
Copy link
Member

lionkor commented Dec 21, 2023

Currently doesn't compile for me:

/home/lion/src/BeamMP-Server/src/TLuaEngine.cpp: At global scope:
/home/lion/src/BeamMP-Server/src/TLuaEngine.cpp:608:18: error: no previous declaration for ‘httplib::Headers table_to_headers(const sol::table&)’ [-Werror=missing-declarations]
  608 | httplib::Headers table_to_headers(const sol::table& headers) {
      |                  ^~~~~~~~~~~~~~~~

src/TLuaEngine.cpp Outdated Show resolved Hide resolved
@Dreae Dreae requested a review from lionkor December 24, 2023 20:32
@lionkor lionkor changed the base branch from master to develop January 25, 2024 09:16
@lionkor
Copy link
Member

lionkor commented Feb 8, 2024

Hi @Dreae, sorry it's been so slow. It seems like it doesn't work when the host has a trailing /

src/TLuaEngine.cpp Outdated Show resolved Hide resolved
@lionkor
Copy link
Member

lionkor commented Feb 8, 2024

I basically joined back into the team around when you made the latest changes, so there was more than enough to do that had very high prio - but I think the way you solved this is very very neat, and it'd be cool if we could give you the contributor role in our discord so we can all chat for any possible future issues as well :)

@lionkor lionkor added this to the v4.0.0 (develop) milestone Feb 8, 2024
@lionkor lionkor changed the base branch from develop to minor May 24, 2024 07:43
@SaltySnail SaltySnail self-requested a review July 14, 2024 01:24
@carsakiller
Copy link

Is there any chance this could be merged in as part of a minor update rather than 4.0.0? This feature is super valuable, and I would love to get my hands on it as soon as possible 🙂

@lionkor
Copy link
Member

lionkor commented Jul 25, 2024

This is still in review as last time I tested it there were many issues.

@lionkor lionkor removed their assignment Aug 3, 2024
@SaltySnail SaltySnail self-assigned this Aug 3, 2024
@lionkor
Copy link
Member

lionkor commented Aug 18, 2024

needs fixing, please reopen when ready

@lionkor lionkor closed this Aug 18, 2024
@Tcoder3
Copy link

Tcoder3 commented Sep 17, 2024

@Dreae Are you planning to fix/continue/reopen this? This would be useful for my project.
Otherwise is there some guidance/tutorial/something available for the luasocket or similar library to make HTTP requests and/or open sockets/serve HTTP from beammp server lua?

@carsakiller
Copy link

@Tcoder3 HTTP is something I've been trying to work around as well. I used to use io.popen to call a separate process that would make the request for me, however that no longer works. My last hope for a workaround is probably os.execute and pipe into a temp file that can then be read from 😔

@20dka
Copy link
Collaborator

20dka commented Sep 17, 2024

@carsakiller that is a workaround I've seen others use as well, but we are aware of the bug with popen

@Dreae
Copy link
Author

Dreae commented Sep 17, 2024

@Tcoder3 as far as I know this code is working. There were some minor changes requested, which I made, before the PR was closed as "needs fixing" so 🤷

@Tcoder3
Copy link

Tcoder3 commented Sep 17, 2024

@Dreae Would it be a bad idea to reopen the PR or @-mention the guy who closed it and say that you want to know what needs fixing or does not work? Because of cource you can't fix something you don't know about... 🤷

@Dreae
Copy link
Author

Dreae commented Sep 17, 2024

I probably wont be reopening it personally, but I'm happy to answer questions about the code or provide assistance if someone else wants to take up the torch to get it merged

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.

[Feature Request] Lua API: Http(s) GET, POST, ...
6 participants