1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
use ::{Call, CallRef, Httpc, SendState, RecvState, ResponseBody};
#[derive(Clone,Copy,PartialEq,Eq)]
enum State {
Sending,
Receiving,
Done,
}
pub struct SimpleCall {
state: State,
id: Call,
resp: Option<::http::Response<Vec<u8>>>,
resp_body: Option<Vec<u8>>,
}
impl SimpleCall {
pub fn take(&mut self) -> Option<::http::Response<Vec<u8>>> {
let out = ::std::mem::replace(self, SimpleCall::empty());
out.close()
}
pub fn is_ref(&self, r: CallRef) -> bool {
self.id.is_ref(r)
}
pub fn call(&self) -> &Call {
&self.id
}
pub fn close(mut self) -> Option<::http::Response<Vec<u8>>> {
let r = self.resp.take();
let b = self.resp_body.take();
if let Some(mut rs) = r {
if let Some(rb) = b {
::std::mem::replace(rs.body_mut(), rb);
return Some(rs);
}
}
None
}
pub fn is_call(&self, cid: &Option<CallRef>) -> bool {
if let &Some(ref b) = cid {
return self.id.0 == b.0;
}
false
}
pub fn is_opt_callid(a: &Option<SimpleCall>, b: &Option<CallRef>) -> bool {
if let &Some(ref a) = a {
if let &Some(ref b) = b {
return a.id.0 == b.0;
}
}
false
}
pub fn is_done(&self) -> bool {
self.state == State::Done
}
pub fn perform(&mut self, htp: &mut Httpc, poll: &::mio::Poll) -> ::Result<bool> {
if self.is_done() {
return Ok(true);
}
if self.state == State::Sending {
match htp.call_send(poll, &mut self.id, None) {
SendState::Wait => {}
SendState::Receiving => {
self.state = State::Receiving;
}
SendState::SentBody(_) => {}
SendState::Error(e) => {
self.state = State::Done;
return Err(From::from(e));
}
SendState::WaitReqBody => {
self.state = State::Done;
return Err(::Error::MissingBody);
}
SendState::Done => {
self.state = State::Done;
return Ok(true);
}
}
}
if self.state == State::Receiving {
loop {
match htp.call_recv(poll, &mut self.id, None) {
RecvState::DoneWithBody(b) => {
self.resp_body = Some(b);
self.state = State::Done;
return Ok(true);
}
RecvState::Done => {
self.state = State::Done;
return Ok(true);
}
RecvState::Error(e) => {
self.state = State::Done;
return Err(From::from(e));
}
RecvState::Response(r,body) => {
self.resp = Some(r);
match body {
ResponseBody::Sized(0) => {
self.state = State::Done;
return Ok(true);
}
_ => {}
}
}
RecvState::Wait => {}
RecvState::Sending => {}
RecvState::ReceivedBody(_s) => {}
}
}
}
Ok(false)
}
pub fn empty() -> SimpleCall {
SimpleCall {
state: State::Done,
id: ::Call::empty(),
resp: None,
resp_body: None,
}
}
pub fn is_empty(&self) -> bool {
self.id.is_empty()
}
}
impl From<Call> for SimpleCall {
fn from(v: Call) -> SimpleCall {
SimpleCall {
state: State::Sending,
id: v,
resp: None,
resp_body: None,
}
}
}