Skip to content

Add asynchronous HTTP handling with HttpAsync module#490

Draft
cocorico8 wants to merge 11 commits intoBeamMP:minorfrom
cocorico8:httpasync
Draft

Add asynchronous HTTP handling with HttpAsync module#490
cocorico8 wants to merge 11 commits intoBeamMP:minorfrom
cocorico8:httpasync

Conversation

@cocorico8
Copy link
Copy Markdown

@cocorico8 cocorico8 commented Apr 26, 2026

Description

This PR introduces a comprehensive, non-blocking asynchronous networking suite to the server-side Lua API, consisting of the AsyncHttp and AsyncWebSocket modules. It provides modders with high-performance tools for external integrations while strictly protecting the server's main thread and system resources.

Key Features:

  • Full REST & WebSocket Support: Implements GET, POST, PUT, PATCH, DELETE, HEAD, and full-duplex WebSockets (ws:// and wss://).
  • Request Handles & Cancellation: All requests return a handle allowing for manual cancellation via :Cancel(), which immediately severs the underlying TCP socket to reclaim resources.
  • Smart Payload Handling: Automatically detects Lua tables and encodes them to JSON, defaulting to application/json where appropriate.
  • Advanced Networking:
    • Separate Timeouts: Granular control over Connection and Read timeouts to prevent thread-hangs on dead endpoints.
    • Standards Compliant Headers: Support for multi-value headers (e.g., multiple Set-Cookie fields) and automatic lowercase header normalization for easy Lua access.
  • File I/O: Streaming Download to disk and PostFile for multipart form-data uploads.
  • Universal Progress Tracking: Real-time progress callbacks supported for all HTTP methods and downloads.

Stability & Safety Implementation:

  • Dynamic Resource Scaling: The module automatically scales the internal httplib::ThreadPool and WebSocket quotas based on the host's hardware (CPU thread count).
  • Plugin Sandboxing: Implements per-plugin rate limiting (quotas) for both HTTP requests and active WebSocket threads. This prevents a single buggy or malicious script from exhausting the server's network capacity.
  • Memory Management:
    • Utilizes a thread-safe result queue processed during the server's Update cycle to ensure Lua callbacks only execute on the main thread.
    • Robust CleanupState and Abandon logic ensures all Lua Registry references are freed and background threads are terminated immediately when a script reloads or stops.
  • Thread Safety: Comprehensive use of mutexes and atomic booleans across the bridge to prevent race conditions during async operations.

By creating this pull request, I understand that code that is AI generated or otherwise automatically generated may be rejected without further discussion.
I declare that I fully understand all code I pushed into this PR, and wrote all this code myself and own the rights to this code.

cocorico8 and others added 3 commits April 26, 2026 23:21
@cocorico8
Copy link
Copy Markdown
Author

currently there's a bug in the version of cpp-httplib, I would need to update the baseline to get rid of it to avoid using hacky solution, should i make another pr to update the vcpkg baseline (tried in a debian 12 vm that it still compiles and it does)

cocorico8 and others added 8 commits April 27, 2026 21:28
…that had a bug in it that was resolved in more recent version
… handling

Co-authored-by: Copilot <copilot@github.com>
…s that wasn't returning the handles.

Co-authored-by: Copilot <copilot@github.com>
…fety

Co-authored-by: Copilot <copilot@github.com>
… warnings for HTTP and WebSocket connections.

Also fixes a race condition in the websockets part
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.

1 participant