mirror of
https://github.com/fafhrd91/actix-web
synced 2025-06-25 06:39:22 +02:00
multipart implementation
This commit is contained in:
13
examples/multipart/Cargo.toml
Normal file
13
examples/multipart/Cargo.toml
Normal file
@ -0,0 +1,13 @@
|
||||
[package]
|
||||
name = "multipart-example"
|
||||
version = "0.1.0"
|
||||
authors = ["Nikolay Kim <fafhrd91@gmail.com>"]
|
||||
|
||||
[[bin]]
|
||||
name = "multipart"
|
||||
path = "src/main.rs"
|
||||
|
||||
[dependencies]
|
||||
env_logger = "*"
|
||||
actix = "0.2"
|
||||
actix-web = { path = "../../" }
|
18
examples/multipart/client.py
Normal file
18
examples/multipart/client.py
Normal file
@ -0,0 +1,18 @@
|
||||
import asyncio
|
||||
import aiohttp
|
||||
|
||||
|
||||
def client():
|
||||
with aiohttp.MultipartWriter() as writer:
|
||||
writer.append('test')
|
||||
writer.append_json({'passed': True})
|
||||
|
||||
resp = yield from aiohttp.request(
|
||||
"post", 'http://localhost:8080/multipart',
|
||||
data=writer, headers=writer.headers)
|
||||
print(resp)
|
||||
assert 200 == resp.status
|
||||
|
||||
|
||||
loop = asyncio.get_event_loop()
|
||||
loop.run_until_complete(client())
|
70
examples/multipart/src/main.rs
Normal file
70
examples/multipart/src/main.rs
Normal file
@ -0,0 +1,70 @@
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate env_logger;
|
||||
|
||||
use actix::*;
|
||||
use actix_web::*;
|
||||
|
||||
struct MyRoute;
|
||||
|
||||
impl Actor for MyRoute {
|
||||
type Context = HttpContext<Self>;
|
||||
}
|
||||
|
||||
impl Route for MyRoute {
|
||||
type State = ();
|
||||
|
||||
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self> {
|
||||
println!("{:?}", req);
|
||||
match req.multipart(payload) {
|
||||
Ok(multipart) => {
|
||||
ctx.add_stream(multipart);
|
||||
Reply::async(MyRoute)
|
||||
},
|
||||
// can not read multipart
|
||||
Err(_) => {
|
||||
Reply::reply(httpcodes::HTTPBadRequest)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<multipart::MultipartItem> for MyRoute {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl StreamHandler<multipart::MultipartItem, PayloadError> for MyRoute {
|
||||
fn finished(&mut self, ctx: &mut Self::Context) {
|
||||
println!("FINISHED");
|
||||
ctx.start(httpcodes::HTTPOk);
|
||||
ctx.write_eof();
|
||||
}
|
||||
}
|
||||
|
||||
impl Handler<multipart::MultipartItem, PayloadError> for MyRoute {
|
||||
fn handle(&mut self, msg: multipart::MultipartItem, ctx: &mut HttpContext<Self>)
|
||||
-> Response<Self, multipart::MultipartItem>
|
||||
{
|
||||
println!("==== FIELD ==== {:?}", msg);
|
||||
//if let Some(req) = self.req.take() {
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
fn main() {
|
||||
let _ = env_logger::init();
|
||||
let sys = actix::System::new("multipart-example");
|
||||
|
||||
HttpServer::new(
|
||||
RoutingMap::default()
|
||||
.app("/", Application::default()
|
||||
.resource("/multipart", |r| {
|
||||
r.post::<MyRoute>();
|
||||
})
|
||||
.finish())
|
||||
.finish())
|
||||
.serve::<_, ()>("127.0.0.1:8080").unwrap();
|
||||
|
||||
let _ = sys.run();
|
||||
}
|
126
examples/websocket/src/main.rs
Normal file
126
examples/websocket/src/main.rs
Normal file
@ -0,0 +1,126 @@
|
||||
// #![feature(try_trait)]
|
||||
#![allow(dead_code, unused_variables)]
|
||||
extern crate actix;
|
||||
extern crate actix_web;
|
||||
extern crate tokio_core;
|
||||
extern crate env_logger;
|
||||
|
||||
use actix::*;
|
||||
use actix_web::*;
|
||||
|
||||
struct MyRoute {req: Option<HttpRequest>}
|
||||
|
||||
impl Actor for MyRoute {
|
||||
type Context = HttpContext<Self>;
|
||||
}
|
||||
|
||||
impl Route for MyRoute {
|
||||
type State = ();
|
||||
|
||||
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self> {
|
||||
println!("PARAMS: {:?} {:?}", req.match_info().get("name"), req.match_info());
|
||||
if !payload.eof() {
|
||||
ctx.add_stream(payload);
|
||||
Reply::stream(MyRoute{req: Some(req)})
|
||||
} else {
|
||||
Reply::reply(httpcodes::HTTPOk)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<PayloadItem> for MyRoute {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl StreamHandler<PayloadItem> for MyRoute {}
|
||||
|
||||
impl Handler<PayloadItem> for MyRoute {
|
||||
fn handle(&mut self, msg: PayloadItem, ctx: &mut HttpContext<Self>)
|
||||
-> Response<Self, PayloadItem>
|
||||
{
|
||||
println!("CHUNK: {:?}", msg);
|
||||
if let Some(req) = self.req.take() {
|
||||
ctx.start(httpcodes::HTTPOk);
|
||||
ctx.write_eof();
|
||||
}
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
struct MyWS {}
|
||||
|
||||
impl Actor for MyWS {
|
||||
type Context = HttpContext<Self>;
|
||||
}
|
||||
|
||||
impl Route for MyWS {
|
||||
type State = ();
|
||||
|
||||
fn request(req: HttpRequest, payload: Payload, ctx: &mut HttpContext<Self>) -> Reply<Self>
|
||||
{
|
||||
match ws::handshake(&req) {
|
||||
Ok(resp) => {
|
||||
ctx.start(resp);
|
||||
ctx.add_stream(ws::WsStream::new(payload));
|
||||
Reply::stream(MyWS{})
|
||||
}
|
||||
Err(err) => {
|
||||
Reply::reply(err)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
impl ResponseType<ws::Message> for MyWS {
|
||||
type Item = ();
|
||||
type Error = ();
|
||||
}
|
||||
|
||||
impl StreamHandler<ws::Message> for MyWS {}
|
||||
|
||||
impl Handler<ws::Message> for MyWS {
|
||||
fn handle(&mut self, msg: ws::Message, ctx: &mut HttpContext<Self>)
|
||||
-> Response<Self, ws::Message>
|
||||
{
|
||||
println!("WS: {:?}", msg);
|
||||
match msg {
|
||||
ws::Message::Ping(msg) => ws::WsWriter::pong(ctx, msg),
|
||||
ws::Message::Text(text) => ws::WsWriter::text(ctx, text),
|
||||
ws::Message::Binary(bin) => ws::WsWriter::binary(ctx, bin),
|
||||
ws::Message::Closed | ws::Message::Error => {
|
||||
ctx.stop();
|
||||
}
|
||||
_ => (),
|
||||
}
|
||||
Self::empty()
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
fn main() {
|
||||
let _ = env_logger::init();
|
||||
|
||||
let sys = actix::System::new("http-example");
|
||||
|
||||
HttpServer::new(
|
||||
RoutingMap::default()
|
||||
.app("/blah", Application::default()
|
||||
.resource("/test/{name}", |r| {
|
||||
r.get::<MyRoute>();
|
||||
r.post::<MyRoute>();
|
||||
})
|
||||
.route_handler("/static", StaticFiles::new(".", true))
|
||||
.finish())
|
||||
.resource("/test", |r| r.post::<MyRoute>())
|
||||
.resource("/ws/", |r| r.get::<MyWS>())
|
||||
.resource("/simple/", |r|
|
||||
r.handler(Method::GET, |req, payload, state| {
|
||||
httpcodes::HTTPOk
|
||||
}))
|
||||
.finish())
|
||||
.serve::<_, ()>("127.0.0.1:9080").unwrap();
|
||||
|
||||
println!("starting");
|
||||
let _ = sys.run();
|
||||
}
|
Reference in New Issue
Block a user