mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-25 14:49:20 +02:00
Merge actix-http project
This commit is contained in:
33
actix-http/examples/client.rs
Normal file
33
actix-http/examples/client.rs
Normal file
@ -0,0 +1,33 @@
|
||||
use actix_http::{client, Error};
|
||||
use actix_rt::System;
|
||||
use bytes::BytesMut;
|
||||
use futures::{future::lazy, Future, Stream};
|
||||
|
||||
fn main() -> Result<(), Error> {
|
||||
std::env::set_var("RUST_LOG", "actix_http=trace");
|
||||
env_logger::init();
|
||||
|
||||
System::new("test").block_on(lazy(|| {
|
||||
let mut connector = client::Connector::new().service();
|
||||
|
||||
client::ClientRequest::get("https://www.rust-lang.org/") // <- Create request builder
|
||||
.header("User-Agent", "Actix-web")
|
||||
.finish()
|
||||
.unwrap()
|
||||
.send(&mut connector) // <- Send http request
|
||||
.from_err()
|
||||
.and_then(|response| {
|
||||
// <- server http response
|
||||
println!("Response: {:?}", response);
|
||||
|
||||
// read response body
|
||||
response
|
||||
.from_err()
|
||||
.fold(BytesMut::new(), move |mut acc, chunk| {
|
||||
acc.extend_from_slice(&chunk);
|
||||
Ok::<_, Error>(acc)
|
||||
})
|
||||
.map(|body| println!("Downloaded: {:?} bytes", body.len()))
|
||||
})
|
||||
}))
|
||||
}
|
37
actix-http/examples/echo.rs
Normal file
37
actix-http/examples/echo.rs
Normal file
@ -0,0 +1,37 @@
|
||||
use std::{env, io};
|
||||
|
||||
use actix_http::{error::PayloadError, HttpService, Request, Response};
|
||||
use actix_server::Server;
|
||||
use bytes::BytesMut;
|
||||
use futures::{Future, Stream};
|
||||
use http::header::HeaderValue;
|
||||
use log::info;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
env::set_var("RUST_LOG", "echo=info");
|
||||
env_logger::init();
|
||||
|
||||
Server::build()
|
||||
.bind("echo", "127.0.0.1:8080", || {
|
||||
HttpService::build()
|
||||
.client_timeout(1000)
|
||||
.client_disconnect(1000)
|
||||
.finish(|mut req: Request| {
|
||||
req.take_payload()
|
||||
.fold(BytesMut::new(), move |mut body, chunk| {
|
||||
body.extend_from_slice(&chunk);
|
||||
Ok::<_, PayloadError>(body)
|
||||
})
|
||||
.and_then(|bytes| {
|
||||
info!("request body: {:?}", bytes);
|
||||
let mut res = Response::Ok();
|
||||
res.header(
|
||||
"x-head",
|
||||
HeaderValue::from_static("dummy value!"),
|
||||
);
|
||||
Ok(res.body(bytes))
|
||||
})
|
||||
})
|
||||
})?
|
||||
.run()
|
||||
}
|
34
actix-http/examples/echo2.rs
Normal file
34
actix-http/examples/echo2.rs
Normal file
@ -0,0 +1,34 @@
|
||||
use std::{env, io};
|
||||
|
||||
use actix_http::http::HeaderValue;
|
||||
use actix_http::{error::PayloadError, Error, HttpService, Request, Response};
|
||||
use actix_server::Server;
|
||||
use bytes::BytesMut;
|
||||
use futures::{Future, Stream};
|
||||
use log::info;
|
||||
|
||||
fn handle_request(mut req: Request) -> impl Future<Item = Response, Error = Error> {
|
||||
req.take_payload()
|
||||
.fold(BytesMut::new(), move |mut body, chunk| {
|
||||
body.extend_from_slice(&chunk);
|
||||
Ok::<_, PayloadError>(body)
|
||||
})
|
||||
.from_err()
|
||||
.and_then(|bytes| {
|
||||
info!("request body: {:?}", bytes);
|
||||
let mut res = Response::Ok();
|
||||
res.header("x-head", HeaderValue::from_static("dummy value!"));
|
||||
Ok(res.body(bytes))
|
||||
})
|
||||
}
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
env::set_var("RUST_LOG", "echo=info");
|
||||
env_logger::init();
|
||||
|
||||
Server::build()
|
||||
.bind("echo", "127.0.0.1:8080", || {
|
||||
HttpService::build().finish(|_req: Request| handle_request(_req))
|
||||
})?
|
||||
.run()
|
||||
}
|
28
actix-http/examples/framed_hello.rs
Normal file
28
actix-http/examples/framed_hello.rs
Normal file
@ -0,0 +1,28 @@
|
||||
use std::{env, io};
|
||||
|
||||
use actix_codec::Framed;
|
||||
use actix_http::{h1, Response, SendResponse, ServiceConfig};
|
||||
use actix_server::{Io, Server};
|
||||
use actix_service::{fn_service, NewService};
|
||||
use actix_utils::framed::IntoFramed;
|
||||
use actix_utils::stream::TakeItem;
|
||||
use futures::Future;
|
||||
use tokio_tcp::TcpStream;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
env::set_var("RUST_LOG", "framed_hello=info");
|
||||
env_logger::init();
|
||||
|
||||
Server::build()
|
||||
.bind("framed_hello", "127.0.0.1:8080", || {
|
||||
fn_service(|io: Io<TcpStream>| Ok(io.into_parts().0))
|
||||
.and_then(IntoFramed::new(|| h1::Codec::new(ServiceConfig::default())))
|
||||
.and_then(TakeItem::new().map_err(|_| ()))
|
||||
.and_then(|(_req, _framed): (_, Framed<_, _>)| {
|
||||
SendResponse::send(_framed, Response::Ok().body("Hello world!"))
|
||||
.map_err(|_| ())
|
||||
.map(|_| ())
|
||||
})
|
||||
})?
|
||||
.run()
|
||||
}
|
26
actix-http/examples/hello-world.rs
Normal file
26
actix-http/examples/hello-world.rs
Normal file
@ -0,0 +1,26 @@
|
||||
use std::{env, io};
|
||||
|
||||
use actix_http::{HttpService, Response};
|
||||
use actix_server::Server;
|
||||
use futures::future;
|
||||
use http::header::HeaderValue;
|
||||
use log::info;
|
||||
|
||||
fn main() -> io::Result<()> {
|
||||
env::set_var("RUST_LOG", "hello_world=info");
|
||||
env_logger::init();
|
||||
|
||||
Server::build()
|
||||
.bind("hello-world", "127.0.0.1:8080", || {
|
||||
HttpService::build()
|
||||
.client_timeout(1000)
|
||||
.client_disconnect(1000)
|
||||
.finish(|_req| {
|
||||
info!("{:?}", _req);
|
||||
let mut res = Response::Ok();
|
||||
res.header("x-head", HeaderValue::from_static("dummy value!"));
|
||||
future::ok::<_, ()>(res.body("Hello world!"))
|
||||
})
|
||||
})?
|
||||
.run()
|
||||
}
|
Reference in New Issue
Block a user