Skip to content
体验新版
项目
组织
正在加载...
登录
切换导航
打开侧边栏
张重言
deno
提交
f2223fb9
D
deno
项目概览
张重言
/
deno
通知
1
Star
0
Fork
0
代码
文件
提交
分支
Tags
贡献者
分支图
Diff
Issue
0
列表
看板
标记
里程碑
合并请求
0
Wiki
0
Wiki
分析
仓库
DevOps
项目成员
Pages
D
deno
项目概览
项目概览
详情
发布
仓库
仓库
文件
提交
分支
标签
贡献者
分支图
比较
Issue
0
Issue
0
列表
看板
标记
里程碑
合并请求
0
合并请求
0
Pages
分析
分析
仓库分析
DevOps
Wiki
0
Wiki
成员
成员
收起侧边栏
关闭侧边栏
动态
分支图
创建新Issue
提交
Issue看板
体验新版 GitCode,发现更多精彩内容 >>
提交
f2223fb9
编写于
10月 25, 2018
作者:
R
Ryan Dahl
浏览文件
操作
浏览文件
下载
电子邮件补丁
差异文件
Wrapper around hyper::Body so it can be used as AsyncRead
上级
92e40470
变更
2
隐藏空白更改
内联
并排
Showing
2 changed file
with
116 addition
and
0 deletion
+116
-0
src/http_body.rs
src/http_body.rs
+115
-0
src/main.rs
src/main.rs
+1
-0
未找到文件。
src/http_body.rs
0 → 100644
浏览文件 @
f2223fb9
// Copyright 2018 the Deno authors. All rights reserved. MIT license.
use
futures
::
Async
;
use
futures
::
Poll
;
use
hyper
::
body
::
Payload
;
use
hyper
::
Body
;
use
hyper
::
Chunk
;
use
std
::
cmp
::
min
;
use
std
::
io
;
use
std
::
io
::
Read
;
use
tokio
::
io
::
AsyncRead
;
/// Wraps hyper::Body so that it can be exposed as an AsyncRead and integrated
/// into resources more easily.
pub
struct
HttpBody
{
body
:
Body
,
chunk
:
Option
<
Chunk
>
,
pos
:
usize
,
}
impl
HttpBody
{
pub
fn
from
(
body
:
Body
)
->
HttpBody
{
HttpBody
{
body
,
chunk
:
None
,
pos
:
0
,
}
}
}
impl
Read
for
HttpBody
{
fn
read
(
&
mut
self
,
_
buf
:
&
mut
[
u8
])
->
io
::
Result
<
usize
>
{
unimplemented!
();
}
}
impl
AsyncRead
for
HttpBody
{
fn
poll_read
(
&
mut
self
,
buf
:
&
mut
[
u8
])
->
Poll
<
usize
,
io
::
Error
>
{
match
self
.chunk
.take
()
{
Some
(
chunk
)
=>
{
debug!
(
"HttpBody Fake Read buf {} chunk {} pos {}"
,
buf
.len
(),
chunk
.len
(),
self
.pos
);
let
n
=
min
(
buf
.len
(),
chunk
.len
()
-
self
.pos
);
{
let
rest
=
&
chunk
[
self
.pos
..
];
buf
[
..
n
]
.clone_from_slice
(
&
rest
[
..
n
]);
}
self
.pos
+=
n
;
if
self
.pos
==
chunk
.len
()
{
self
.pos
=
0
;
}
else
{
self
.chunk
=
Some
(
chunk
);
}
return
Ok
(
Async
::
Ready
(
n
));
}
None
=>
{
assert_eq!
(
self
.pos
,
0
);
}
}
let
p
=
self
.body
.poll_data
();
match
p
{
Err
(
e
)
=>
Err
(
// TODO Need to map hyper::Error into std::io::Error.
io
::
Error
::
new
(
io
::
ErrorKind
::
Other
,
e
),
),
Ok
(
Async
::
NotReady
)
=>
Ok
(
Async
::
NotReady
),
Ok
(
Async
::
Ready
(
maybe_chunk
))
=>
match
maybe_chunk
{
None
=>
Ok
(
Async
::
Ready
(
0
)),
Some
(
chunk
)
=>
{
debug!
(
"HttpBody Real Read buf {} chunk {} pos {}"
,
buf
.len
(),
chunk
.len
(),
self
.pos
);
let
n
=
min
(
buf
.len
(),
chunk
.len
());
buf
[
..
n
]
.clone_from_slice
(
&
chunk
[
..
n
]);
if
buf
.len
()
<
chunk
.len
()
{
self
.pos
=
n
;
self
.chunk
=
Some
(
chunk
);
}
Ok
(
Async
::
Ready
(
n
))
}
},
}
}
}
#[test]
fn
test_body_async_read
()
{
use
std
::
str
::
from_utf8
;
let
body
=
Body
::
from
(
"hello world"
);
let
mut
body
=
HttpBody
::
from
(
body
);
let
buf
=
&
mut
[
0
,
0
,
0
,
0
,
0
];
let
r
=
body
.poll_read
(
buf
);
assert
!
(
r
.is_ok
());
assert_eq!
(
r
.unwrap
(),
Async
::
Ready
(
5
));
assert_eq!
(
from_utf8
(
buf
)
.unwrap
(),
"hello"
);
let
r
=
body
.poll_read
(
buf
);
assert
!
(
r
.is_ok
());
assert_eq!
(
r
.unwrap
(),
Async
::
Ready
(
5
));
assert_eq!
(
from_utf8
(
buf
)
.unwrap
(),
" worl"
);
let
r
=
body
.poll_read
(
buf
);
assert
!
(
r
.is_ok
());
assert_eq!
(
r
.unwrap
(),
Async
::
Ready
(
1
));
assert_eq!
(
from_utf8
(
&
buf
[
0
..
1
])
.unwrap
(),
"d"
);
}
src/main.rs
浏览文件 @
f2223fb9
...
...
@@ -29,6 +29,7 @@ pub mod deno_dir;
pub
mod
errors
;
pub
mod
flags
;
mod
fs
;
mod
http_body
;
mod
http_util
;
pub
mod
isolate
;
pub
mod
libdeno
;
...
...
编辑
预览
Markdown
is supported
0%
请重试
或
添加新附件
.
添加附件
取消
You are about to add
0
people
to the discussion. Proceed with caution.
先完成此消息的编辑!
取消
想要评论请
注册
或
登录