forked from actori/actori-web
-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
15 changed files
with
781 additions
and
87 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,13 @@ | ||
[package] | ||
name = "multipart-example" | ||
version = "0.1.0" | ||
authors = ["Nikolay Kim <[email protected]>"] | ||
|
||
[[bin]] | ||
name = "multipart" | ||
path = "src/main.rs" | ||
|
||
[dependencies] | ||
env_logger = "*" | ||
actix = "0.2" | ||
actix-web = { path = "../../" } |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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()) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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(); | ||
} |
File renamed without changes.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.