Merge pull request #876 from leodasvacas/conn-refactor
Dedup code in connection state update.
This commit is contained in:
183
src/http/conn.rs
183
src/http/conn.rs
@@ -669,17 +669,26 @@ impl<H: MessageHandler<T>, T: Transport> fmt::Debug for State<H, T> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
||||||
fn update<F, K>(&mut self, next: Next, factory: &F) where F: MessageHandlerFactory<K, T>, K: Key {
|
fn update<F, K>(&mut self, next: Next, factory: &F)
|
||||||
|
where F: MessageHandlerFactory<K, T>,
|
||||||
|
K: Key
|
||||||
|
{
|
||||||
let timeout = next.timeout;
|
let timeout = next.timeout;
|
||||||
let state = mem::replace(self, State::Closed);
|
let state = mem::replace(self, State::Closed);
|
||||||
let new_state = match (state, next.interest) {
|
match (state, next.interest) {
|
||||||
(_, Next_::Remove) => State::Closed,
|
(_, Next_::Remove) |
|
||||||
(State::Closed, _) => State::Closed,
|
(State::Closed, _) => return, // Keep State::Closed.
|
||||||
(State::Init { timeout, .. }, e) => State::Init {
|
(State::Init { .. }, e) => {
|
||||||
|
mem::replace(self,
|
||||||
|
State::Init {
|
||||||
interest: e,
|
interest: e,
|
||||||
timeout: timeout,
|
timeout: timeout,
|
||||||
},
|
});
|
||||||
(State::Http1(http1), Next_::End) => {
|
}
|
||||||
|
(State::Http1(mut http1), next_) => {
|
||||||
|
match next_ {
|
||||||
|
Next_::Remove => unreachable!(), // Covered in (_, Next_::Remove) case above.
|
||||||
|
Next_::End => {
|
||||||
let reading = match http1.reading {
|
let reading = match http1.reading {
|
||||||
Reading::Body(ref decoder) |
|
Reading::Body(ref decoder) |
|
||||||
Reading::Wait(ref decoder) if decoder.is_eof() => {
|
Reading::Wait(ref decoder) if decoder.is_eof() => {
|
||||||
@@ -688,78 +697,61 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
|||||||
} else {
|
} else {
|
||||||
Reading::Closed
|
Reading::Closed
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Reading::KeepAlive => http1.reading,
|
Reading::KeepAlive => http1.reading,
|
||||||
_ => Reading::Closed,
|
_ => Reading::Closed,
|
||||||
};
|
};
|
||||||
let writing = match http1.writing {
|
let mut writing = Writing::Closed;
|
||||||
Writing::Wait(encoder) |
|
let encoder = match http1.writing {
|
||||||
Writing::Ready(encoder) => {
|
Writing::Wait(enc) |
|
||||||
if encoder.is_eof() {
|
Writing::Ready(enc) => Some(enc),
|
||||||
if http1.keep_alive {
|
|
||||||
Writing::KeepAlive
|
|
||||||
} else {
|
|
||||||
Writing::Closed
|
|
||||||
}
|
|
||||||
} else if let Some(buf) = encoder.finish() {
|
|
||||||
Writing::Chunk(Chunk {
|
|
||||||
buf: buf.bytes,
|
|
||||||
pos: buf.pos,
|
|
||||||
next: (h1::Encoder::length(0), Next::end())
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Writing::Closed
|
|
||||||
}
|
|
||||||
}
|
|
||||||
Writing::Chunk(mut chunk) => {
|
Writing::Chunk(mut chunk) => {
|
||||||
if chunk.is_written() {
|
if chunk.is_written() {
|
||||||
let encoder = chunk.next.0;
|
Some(chunk.next.0)
|
||||||
//TODO: de-dupe this code and from Writing::Ready
|
|
||||||
if encoder.is_eof() {
|
|
||||||
if http1.keep_alive {
|
|
||||||
Writing::KeepAlive
|
|
||||||
} else {
|
|
||||||
Writing::Closed
|
|
||||||
}
|
|
||||||
} else if let Some(buf) = encoder.finish() {
|
|
||||||
Writing::Chunk(Chunk {
|
|
||||||
buf: buf.bytes,
|
|
||||||
pos: buf.pos,
|
|
||||||
next: (h1::Encoder::length(0), Next::end())
|
|
||||||
})
|
|
||||||
} else {
|
|
||||||
Writing::Closed
|
|
||||||
}
|
|
||||||
} else {
|
} else {
|
||||||
chunk.next.1 = next;
|
chunk.next.1 = next;
|
||||||
Writing::Chunk(chunk)
|
writing = Writing::Chunk(chunk);
|
||||||
|
None
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
_ => Writing::Closed,
|
_ => return, // Keep State::Closed.
|
||||||
};
|
};
|
||||||
|
if let Some(encoder) = encoder {
|
||||||
|
if encoder.is_eof() {
|
||||||
|
if http1.keep_alive {
|
||||||
|
writing = Writing::KeepAlive
|
||||||
|
}
|
||||||
|
} else if let Some(buf) = encoder.finish() {
|
||||||
|
writing = Writing::Chunk(Chunk {
|
||||||
|
buf: buf.bytes,
|
||||||
|
pos: buf.pos,
|
||||||
|
next: (h1::Encoder::length(0), Next::end()),
|
||||||
|
})
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
match (reading, writing) {
|
match (reading, writing) {
|
||||||
(Reading::KeepAlive, Writing::KeepAlive) => {
|
(Reading::KeepAlive, Writing::KeepAlive) => {
|
||||||
let next = factory.keep_alive_interest();
|
let next = factory.keep_alive_interest();
|
||||||
|
mem::replace(self,
|
||||||
State::Init {
|
State::Init {
|
||||||
interest: next.interest,
|
interest: next.interest,
|
||||||
timeout: next.timeout,
|
timeout: next.timeout,
|
||||||
|
});
|
||||||
|
return;
|
||||||
}
|
}
|
||||||
},
|
|
||||||
(reading, Writing::Chunk(chunk)) => {
|
(reading, Writing::Chunk(chunk)) => {
|
||||||
State::Http1(Http1 {
|
http1.reading = reading;
|
||||||
reading: reading,
|
http1.writing = Writing::Chunk(chunk);
|
||||||
writing: Writing::Chunk(chunk),
|
|
||||||
.. http1
|
|
||||||
})
|
|
||||||
}
|
}
|
||||||
_ => State::Closed
|
_ => return, // Keep State::Closed.
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
(State::Http1(mut http1), Next_::Read) => {
|
Next_::Read => {
|
||||||
http1.reading = match http1.reading {
|
http1.reading = match http1.reading {
|
||||||
Reading::Init => Reading::Parse,
|
Reading::Init => Reading::Parse,
|
||||||
Reading::Wait(decoder) => Reading::Body(decoder),
|
Reading::Wait(decoder) => Reading::Body(decoder),
|
||||||
same => same
|
same => same,
|
||||||
};
|
};
|
||||||
|
|
||||||
http1.writing = match http1.writing {
|
http1.writing = match http1.writing {
|
||||||
@@ -775,7 +767,7 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
|||||||
Writing::Chunk(Chunk {
|
Writing::Chunk(Chunk {
|
||||||
buf: buf.bytes,
|
buf: buf.bytes,
|
||||||
pos: buf.pos,
|
pos: buf.pos,
|
||||||
next: (h1::Encoder::length(0), Next::wait())
|
next: (h1::Encoder::length(0), Next::wait()),
|
||||||
})
|
})
|
||||||
} else {
|
} else {
|
||||||
Writing::Closed
|
Writing::Closed
|
||||||
@@ -783,31 +775,34 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
|||||||
} else {
|
} else {
|
||||||
Writing::Wait(encoder)
|
Writing::Wait(encoder)
|
||||||
}
|
}
|
||||||
},
|
}
|
||||||
Writing::Chunk(chunk) => if chunk.is_written() {
|
Writing::Chunk(chunk) => {
|
||||||
|
if chunk.is_written() {
|
||||||
Writing::Wait(chunk.next.0)
|
Writing::Wait(chunk.next.0)
|
||||||
} else {
|
} else {
|
||||||
Writing::Chunk(chunk)
|
Writing::Chunk(chunk)
|
||||||
},
|
}
|
||||||
same => same
|
}
|
||||||
|
same => same,
|
||||||
};
|
};
|
||||||
|
}
|
||||||
State::Http1(http1)
|
Next_::Write => {
|
||||||
},
|
|
||||||
(State::Http1(mut http1), Next_::Write) => {
|
|
||||||
http1.writing = match http1.writing {
|
http1.writing = match http1.writing {
|
||||||
Writing::Wait(encoder) => Writing::Ready(encoder),
|
Writing::Wait(encoder) => Writing::Ready(encoder),
|
||||||
Writing::Init => Writing::Head,
|
Writing::Init => Writing::Head,
|
||||||
Writing::Chunk(chunk) => if chunk.is_written() {
|
Writing::Chunk(chunk) => {
|
||||||
|
if chunk.is_written() {
|
||||||
Writing::Ready(chunk.next.0)
|
Writing::Ready(chunk.next.0)
|
||||||
} else {
|
} else {
|
||||||
Writing::Chunk(chunk)
|
Writing::Chunk(chunk)
|
||||||
},
|
}
|
||||||
same => same
|
}
|
||||||
|
same => same,
|
||||||
};
|
};
|
||||||
|
|
||||||
http1.reading = match http1.reading {
|
http1.reading = match http1.reading {
|
||||||
Reading::Body(decoder) => if decoder.is_eof() {
|
Reading::Body(decoder) => {
|
||||||
|
if decoder.is_eof() {
|
||||||
if http1.keep_alive {
|
if http1.keep_alive {
|
||||||
Reading::KeepAlive
|
Reading::KeepAlive
|
||||||
} else {
|
} else {
|
||||||
@@ -815,59 +810,53 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
|||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
Reading::Wait(decoder)
|
Reading::Wait(decoder)
|
||||||
},
|
}
|
||||||
same => same
|
}
|
||||||
|
same => same,
|
||||||
};
|
};
|
||||||
State::Http1(http1)
|
}
|
||||||
},
|
Next_::ReadWrite => {
|
||||||
(State::Http1(mut http1), Next_::ReadWrite) => {
|
|
||||||
http1.reading = match http1.reading {
|
http1.reading = match http1.reading {
|
||||||
Reading::Init => Reading::Parse,
|
Reading::Init => Reading::Parse,
|
||||||
Reading::Wait(decoder) => Reading::Body(decoder),
|
Reading::Wait(decoder) => Reading::Body(decoder),
|
||||||
same => same
|
same => same,
|
||||||
};
|
};
|
||||||
http1.writing = match http1.writing {
|
http1.writing = match http1.writing {
|
||||||
Writing::Wait(encoder) => Writing::Ready(encoder),
|
Writing::Wait(encoder) => Writing::Ready(encoder),
|
||||||
Writing::Init => Writing::Head,
|
Writing::Init => Writing::Head,
|
||||||
Writing::Chunk(chunk) => if chunk.is_written() {
|
Writing::Chunk(chunk) => {
|
||||||
|
if chunk.is_written() {
|
||||||
Writing::Ready(chunk.next.0)
|
Writing::Ready(chunk.next.0)
|
||||||
} else {
|
} else {
|
||||||
Writing::Chunk(chunk)
|
Writing::Chunk(chunk)
|
||||||
},
|
}
|
||||||
same => same
|
}
|
||||||
|
same => same,
|
||||||
};
|
};
|
||||||
State::Http1(http1)
|
}
|
||||||
},
|
Next_::Wait => {
|
||||||
(State::Http1(mut http1), Next_::Wait) => {
|
|
||||||
http1.reading = match http1.reading {
|
http1.reading = match http1.reading {
|
||||||
Reading::Body(decoder) => Reading::Wait(decoder),
|
Reading::Body(decoder) => Reading::Wait(decoder),
|
||||||
same => same
|
same => same,
|
||||||
};
|
};
|
||||||
|
|
||||||
http1.writing = match http1.writing {
|
http1.writing = match http1.writing {
|
||||||
Writing::Ready(encoder) => Writing::Wait(encoder),
|
Writing::Ready(encoder) => Writing::Wait(encoder),
|
||||||
Writing::Chunk(chunk) => if chunk.is_written() {
|
Writing::Chunk(chunk) => {
|
||||||
|
if chunk.is_written() {
|
||||||
Writing::Wait(chunk.next.0)
|
Writing::Wait(chunk.next.0)
|
||||||
} else {
|
} else {
|
||||||
Writing::Chunk(chunk)
|
Writing::Chunk(chunk)
|
||||||
},
|
|
||||||
same => same
|
|
||||||
};
|
|
||||||
State::Http1(http1)
|
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
same => same,
|
||||||
};
|
};
|
||||||
let new_state = match new_state {
|
}
|
||||||
State::Init { interest, .. } => State::Init {
|
}
|
||||||
timeout: timeout,
|
|
||||||
interest: interest,
|
|
||||||
},
|
|
||||||
State::Http1(mut http1) => {
|
|
||||||
http1.timeout = timeout;
|
http1.timeout = timeout;
|
||||||
State::Http1(http1)
|
mem::replace(self, State::Http1(http1));
|
||||||
}
|
}
|
||||||
other => other
|
|
||||||
};
|
};
|
||||||
mem::replace(self, new_state);
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user