Comments (9)
I wish we had async iterators tbh:
use async_h1::{server, Body};
use async_std::prelude::*;
use async_std::{net, task, io};
async fn main() -> throws {
let listener = TcpListener::bind(("127.0.0.1", 8080)).await?;
println!("listening on {}", listener.local_addr()?);
par for stream.await? in listener.incoming() {
for conn.await? in server::connect(stream) {
let req = conn.decode().await?;
println!("request: {:?}", req);
let res = Response::new(Body::from("hello chashu"));
conn.encode(res).await?;
}
}
}
from async-h1.
use async_h1::{server, Body};
use async_std::prelude::*;
use async_std::{net, task, io};
async fn main() -> throws {
let listener = TcpListener::bind(("127.0.0.1", 8080)).await?;
println!("listening on {}", listener.local_addr()?);
par for stream.await? in listener.incoming() {
for conn.await? in server::connect(stream) {
println!("request: {:?}", req);
let body = Body::from("hello chashu");
let res = server::encode(Response::new(body));
io::copy(res, conn).await?;
}
}
}
/// Proxy for an individual `TcpStream`, but respects the
/// header's `content-length` header.
struct Connection { headers }
impl AsyncRead for Connection {}
impl AsyncWrite for Connection {}
from async-h1.
A thing the original design didn't properly account for is that we can't "parse HTTP + send" in a single step. Being able to wrap it in SSL or compression streams is necessary.
from async-h1.
Alternative:
use async_h1::{server, Body};
use async_std::prelude::*;
use async_std::{net, task, io};
async fn main() -> throws {
let listener = TcpListener::bind(("127.0.0.1", 8080)).await?;
println!("listening on {}", listener.local_addr()?);
par for stream.await? in listener.incoming() {
for (req, writer).await? in server::connect(stream) {
println!("request: {:?}", req);
let body = Body::from("hello chashu");
let res = server::encode(Response::new(body));
io::copy(res, writer).await?;
}
}
}
from async-h1.
With @rylev:
use async_h1::{server, Body};
use async_std::prelude::*;
use async_std::{net, task, io};
async fn main() -> throws {
let listener = TcpListener::bind(("127.0.0.1", 8080)).await?;
println!("listening on {}", listener.local_addr()?);
let mut incoming = listener.incoming();
while let Some(tcp_connection) = incoming.next().await {
let http_stream = server::connect(tcp_connection);
while let Some((req, res)) = http_stream.next().await {
println!("request: {:?}", req);
let body = Body::from("hello chashu");
res.send_stream(server::encode(Response::new(body))).await?;
}
}
}
use async_h1::{server, Body};
use async_std::prelude::*;
use async_std::{net, task, io};
async fn main() -> throws {
let listener = TcpListener::bind(("127.0.0.1", 8080)).await?;
println!("listening on {}", listener.local_addr()?);
let mut incoming = listener.incoming();
while let Some(tcp_connection) = incoming.next().await {
server::connect(tcp_connection, |req| -> Result<Response> {
println!("request: {:?}", req);
let body = Body::from("hello chashu");
Response::new(200, /* impl AsyncRead */)
});
}
}
from async-h1.
Does this work?
use async_h1::{server, Body};
use async_std::prelude::*;
use async_std::{net, task, io};
async fn main() -> throws {
let listener = TcpListener::bind(("127.0.0.1", 8080)).await?;
println!("listening on {}", listener.local_addr()?);
let mut incoming = listener.incoming();
while let Some((tcp_reader, tcp_writer)) = incoming.next().await { // read and writer from tcp stream
let http_stream = server::connect(tcp_reader);
while let Some(req) = http_stream.next().await { // just each request as it comes.
println!("request: {:?}", req);
let body = Body::from("hello chashu");
tcp_writer.write_all(server::encode(Response::new(body))).await?;
}
}
}
from async-h1.
following this logic, I would say it should be possible to do sth like this for the client
async fn main() -> throws {
let tcp_stream = net::TcpStream::connect("127.0.0.1:8080").await?;
let (tcp_reader, tcp_writer) = &mut (&tcp_stream, &tcp_stream);
let http_stream = client::connect(tcp_reader);
// lets make 10 requests, with the same connection
for i in 0..10 {
// Send a response
let body = Body::from(format!("hello chashu {}", i));
let mut req = client::encode(Request::new(body));
tcp_writer.write_all(req).await?;
// read the response
let res = http_stream.next().await?;
println!("Response {}: {:?}", i, res);
}
}
from async-h1.
@dignifiedquire moved the conversation about clients over to #7; think it'd be nice to have a dedicated issue to track it there!
from async-h1.
This is now in master with #11. Closing this in favor of more specific issues for remaining bugs and/or enhancements.
from async-h1.
Related Issues (20)
- gracefully fail HTTP/1.0 connections HOT 2
- Fix Wasm build HOT 5
- Cache `Date` timestamps up to the minute HOT 2
- body is interpreted as next request if handler doesn't read it HOT 1
- Investigate the `http-desync-guardian` crate
- Suggestion to remove Clone bound from reader HOT 7
- "Continue" response can interleave with response data HOT 7
- Parsing Url is one of the most expensive things that async-h1 does HOT 3
- CloseableCursor implementation is racey HOT 1
- tcp_nodelay HOT 1
- Panic in both client and server: "Empty response" HOT 2
- Header DoS (surf#298)
- Panic in server when sending non-ASCII header value HOT 5
- Keep-Alive and persistent connection in client code
- Handle responses without Content-Length
- When acting as a client, processing HTTP/1.0 responses fails HOT 4
- Decode inserts malformed date header HOT 2
- Chunked decoding fails on a chunk of length 0xFF7 (4087), 9 bytes less than the buffer size HOT 2
- How to send head and body on single tcp packet? HOT 1
- Server response sending triggers Nagle's algorithm/delayed ACK interaction
Recommend Projects
-
React
A declarative, efficient, and flexible JavaScript library for building user interfaces.
-
Vue.js
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
-
Typescript
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
-
TensorFlow
An Open Source Machine Learning Framework for Everyone
-
Django
The Web framework for perfectionists with deadlines.
-
Laravel
A PHP framework for web artisans
-
D3
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
-
Recommend Topics
-
javascript
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
-
web
Some thing interesting about web. New door for the world.
-
server
A server is a program made to process requests and deliver data to clients.
-
Machine learning
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
-
Visualization
Some thing interesting about visualization, use data art
-
Game
Some thing interesting about game, make everyone happy.
Recommend Org
-
Facebook
We are working to build community through open source technology. NB: members must have two-factor auth.
-
Microsoft
Open source projects and samples from Microsoft.
-
Google
Google ❤️ Open Source for everyone.
-
Alibaba
Alibaba Open Source for everyone
-
D3
Data-Driven Documents codes.
-
Tencent
China tencent open source team.
from async-h1.