summaryrefslogtreecommitdiff
path: root/examples/async_http_client.rs
diff options
context:
space:
mode:
author19年梦醒 <3949379+getong@users.noreply.github.com>2024-03-22 07:06:00 +0800
committerGitHub <noreply@github.com>2024-03-21 23:06:00 +0000
commit849206ef9d56b250424f34077a62a4e6ad96c21a (patch)
tree4185c2418e8173bb8dbc11e2d315851c22f0af63 /examples/async_http_client.rs
parent80fff4f2e7f8250c28bc6f5de79dace1ec914b31 (diff)
downloadmlua-849206ef9d56b250424f34077a62a4e6ad96c21a.zip
update hyper to v1, and add shell command example (#384)
Co-authored-by: Alex Orlenko <zxteam@protonmail.com>
Diffstat (limited to 'examples/async_http_client.rs')
-rw-r--r--examples/async_http_client.rs53
1 files changed, 36 insertions, 17 deletions
diff --git a/examples/async_http_client.rs b/examples/async_http_client.rs
index 3eccdc3..c0fa1ff 100644
--- a/examples/async_http_client.rs
+++ b/examples/async_http_client.rs
@@ -1,20 +1,38 @@
+use bytes::Bytes;
+use http_body_util::BodyExt;
+use http_body_util::Empty;
+use hyper::body::Incoming;
+use hyper_util::client::legacy::Client as HyperClient;
use std::collections::HashMap;
-use hyper::body::{Body as HyperBody, HttpBody as _};
-use hyper::Client as HyperClient;
-
use mlua::{chunk, ExternalResult, Lua, Result, UserData, UserDataMethods};
-struct BodyReader(HyperBody);
+struct BodyReader(Incoming);
impl UserData for BodyReader {
fn add_methods<'lua, M: UserDataMethods<'lua, Self>>(methods: &mut M) {
methods.add_async_method_mut("read", |lua, reader, ()| async move {
- if let Some(bytes) = reader.0.data().await {
- let bytes = bytes.into_lua_err()?;
- return Some(lua.create_string(&bytes)).transpose();
+ let mut summarize = Vec::new(); // Create a vector to accumulate the bytes
+
+ loop {
+ match reader.0.frame().await {
+ Some(Ok(bytes)) => {
+ if let Ok(data) = bytes.into_data() {
+ summarize.extend(data); // Append the bytes to the summarize variable
+ }
+ }
+ Some(Err(_)) => break, // Break on error
+ None => break, // Break if no more frames
+ }
+ }
+
+ if !summarize.is_empty() {
+ // If summarize has collected data, return it as a Lua string
+ Ok(Some(lua.create_string(&summarize)?))
+ } else {
+ // Return None if no data was collected
+ Ok(None)
}
- Ok(None)
});
}
}
@@ -24,7 +42,8 @@ async fn main() -> Result<()> {
let lua = Lua::new();
let fetch_url = lua.create_async_function(|lua, uri: String| async move {
- let client = HyperClient::new();
+ let client =
+ HyperClient::builder(hyper_util::rt::TokioExecutor::new()).build_http::<Empty<Bytes>>();
let uri = uri.parse().into_lua_err()?;
let resp = client.get(uri).await.into_lua_err()?;
@@ -47,18 +66,18 @@ async fn main() -> Result<()> {
let f = lua
.load(chunk! {
- local res = $fetch_url(...)
+ local res = $fetch_url(...)
print("status: "..res.status)
for key, vals in pairs(res.headers) do
- for _, val in ipairs(vals) do
- print(key..": "..val)
- end
+ for _, val in ipairs(vals) do
+ print(key..": "..val)
+ end
end
repeat
- local body = res.body:read()
- if body then
- print(body)
- end
+ local body = res.body:read()
+ if body then
+ print(body)
+ end
until not body
})
.into_function()?;