GithubHelp home page GithubHelp logo

HTTP Server with KeepAlive about async-h1 HOT 9 CLOSED

http-rs avatar http-rs commented on May 20, 2024
HTTP Server with KeepAlive

from async-h1.

Comments (9)

yoshuawuyts avatar yoshuawuyts commented on May 20, 2024

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.

yoshuawuyts avatar yoshuawuyts commented on May 20, 2024
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.

yoshuawuyts avatar yoshuawuyts commented on May 20, 2024

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.

yoshuawuyts avatar yoshuawuyts commented on May 20, 2024

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.

yoshuawuyts avatar yoshuawuyts commented on May 20, 2024

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.

rylev avatar rylev commented on May 20, 2024

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.

dignifiedquire avatar dignifiedquire commented on May 20, 2024

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.

yoshuawuyts avatar yoshuawuyts commented on May 20, 2024

@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.

rylev avatar rylev commented on May 20, 2024

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)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo 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.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.