refactor(http): Dedup code in connection state update.

This commit is contained in:
leonardo.yvens
2016-07-27 09:24:46 -03:00
parent c6eea3761b
commit 9f249bbee8

View File

@@ -692,68 +692,49 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
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) | Writing::Ready(enc) => Some(enc),
if encoder.is_eof() { Writing::Chunk(mut chunk) => {
if http1.keep_alive { if chunk.is_written() {
Writing::KeepAlive Some(chunk.next.0)
} else { } else {
Writing::Closed chunk.next.1 = next;
writing = Writing::Chunk(chunk);
None
} }
},
_ => 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() { } else if let Some(buf) = encoder.finish() {
Writing::Chunk(Chunk { writing = Writing::Chunk(Chunk {
buf: buf.bytes, buf: buf.bytes,
pos: buf.pos, pos: buf.pos,
next: (h1::Encoder::length(0), Next::end()) next: (h1::Encoder::length(0), Next::end())
}) })
} else {
Writing::Closed
} }
}
Writing::Chunk(mut chunk) => {
if chunk.is_written() {
let encoder = 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 {
chunk.next.1 = next;
Writing::Chunk(chunk)
}
},
_ => return, // Keep State::Closed.
}; };
match (reading, writing) {
(Reading::KeepAlive, Writing::KeepAlive) => { match (reading, writing) {
let next = factory.keep_alive_interest(); (Reading::KeepAlive, Writing::KeepAlive) => {
mem::replace(self, State::Init { let next = factory.keep_alive_interest();
interest: next.interest, mem::replace(self, State::Init {
timeout: next.timeout, interest: next.interest,
}); timeout: next.timeout,
return; });
}, return;
(reading, Writing::Chunk(chunk)) => { },
http1.reading = reading; (reading, Writing::Chunk(chunk)) => {
http1.writing = Writing::Chunk(chunk); http1.reading = reading;
http1.writing = Writing::Chunk(chunk);
}
_ => return // Keep State::Closed.
} }
_ => return // Keep State::Closed.
}
}, },
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),