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> {
|
||||
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 state = mem::replace(self, State::Closed);
|
||||
let new_state = match (state, next.interest) {
|
||||
(_, Next_::Remove) => State::Closed,
|
||||
(State::Closed, _) => State::Closed,
|
||||
(State::Init { timeout, .. }, e) => State::Init {
|
||||
match (state, next.interest) {
|
||||
(_, Next_::Remove) |
|
||||
(State::Closed, _) => return, // Keep State::Closed.
|
||||
(State::Init { .. }, e) => {
|
||||
mem::replace(self,
|
||||
State::Init {
|
||||
interest: e,
|
||||
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 {
|
||||
Reading::Body(ref decoder) |
|
||||
Reading::Wait(ref decoder) if decoder.is_eof() => {
|
||||
@@ -688,78 +697,61 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
||||
} else {
|
||||
Reading::Closed
|
||||
}
|
||||
},
|
||||
}
|
||||
Reading::KeepAlive => http1.reading,
|
||||
_ => Reading::Closed,
|
||||
};
|
||||
let writing = match http1.writing {
|
||||
Writing::Wait(encoder) |
|
||||
Writing::Ready(encoder) => {
|
||||
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
|
||||
}
|
||||
}
|
||||
let mut writing = Writing::Closed;
|
||||
let encoder = match http1.writing {
|
||||
Writing::Wait(enc) |
|
||||
Writing::Ready(enc) => Some(enc),
|
||||
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
|
||||
}
|
||||
Some(chunk.next.0)
|
||||
} else {
|
||||
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) {
|
||||
(Reading::KeepAlive, Writing::KeepAlive) => {
|
||||
let next = factory.keep_alive_interest();
|
||||
mem::replace(self,
|
||||
State::Init {
|
||||
interest: next.interest,
|
||||
timeout: next.timeout,
|
||||
});
|
||||
return;
|
||||
}
|
||||
},
|
||||
(reading, Writing::Chunk(chunk)) => {
|
||||
State::Http1(Http1 {
|
||||
reading: reading,
|
||||
writing: Writing::Chunk(chunk),
|
||||
.. http1
|
||||
})
|
||||
http1.reading = reading;
|
||||
http1.writing = Writing::Chunk(chunk);
|
||||
}
|
||||
_ => State::Closed
|
||||
_ => return, // Keep State::Closed.
|
||||
}
|
||||
},
|
||||
(State::Http1(mut http1), Next_::Read) => {
|
||||
}
|
||||
Next_::Read => {
|
||||
http1.reading = match http1.reading {
|
||||
Reading::Init => Reading::Parse,
|
||||
Reading::Wait(decoder) => Reading::Body(decoder),
|
||||
same => same
|
||||
same => same,
|
||||
};
|
||||
|
||||
http1.writing = match http1.writing {
|
||||
@@ -775,7 +767,7 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
||||
Writing::Chunk(Chunk {
|
||||
buf: buf.bytes,
|
||||
pos: buf.pos,
|
||||
next: (h1::Encoder::length(0), Next::wait())
|
||||
next: (h1::Encoder::length(0), Next::wait()),
|
||||
})
|
||||
} else {
|
||||
Writing::Closed
|
||||
@@ -783,31 +775,34 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
||||
} else {
|
||||
Writing::Wait(encoder)
|
||||
}
|
||||
},
|
||||
Writing::Chunk(chunk) => if chunk.is_written() {
|
||||
}
|
||||
Writing::Chunk(chunk) => {
|
||||
if chunk.is_written() {
|
||||
Writing::Wait(chunk.next.0)
|
||||
} else {
|
||||
Writing::Chunk(chunk)
|
||||
},
|
||||
same => same
|
||||
}
|
||||
}
|
||||
same => same,
|
||||
};
|
||||
|
||||
State::Http1(http1)
|
||||
},
|
||||
(State::Http1(mut http1), Next_::Write) => {
|
||||
}
|
||||
Next_::Write => {
|
||||
http1.writing = match http1.writing {
|
||||
Writing::Wait(encoder) => Writing::Ready(encoder),
|
||||
Writing::Init => Writing::Head,
|
||||
Writing::Chunk(chunk) => if chunk.is_written() {
|
||||
Writing::Chunk(chunk) => {
|
||||
if chunk.is_written() {
|
||||
Writing::Ready(chunk.next.0)
|
||||
} else {
|
||||
Writing::Chunk(chunk)
|
||||
},
|
||||
same => same
|
||||
}
|
||||
}
|
||||
same => same,
|
||||
};
|
||||
|
||||
http1.reading = match http1.reading {
|
||||
Reading::Body(decoder) => if decoder.is_eof() {
|
||||
Reading::Body(decoder) => {
|
||||
if decoder.is_eof() {
|
||||
if http1.keep_alive {
|
||||
Reading::KeepAlive
|
||||
} else {
|
||||
@@ -815,59 +810,53 @@ impl<H: MessageHandler<T>, T: Transport> State<H, T> {
|
||||
}
|
||||
} else {
|
||||
Reading::Wait(decoder)
|
||||
},
|
||||
same => same
|
||||
}
|
||||
}
|
||||
same => same,
|
||||
};
|
||||
State::Http1(http1)
|
||||
},
|
||||
(State::Http1(mut http1), Next_::ReadWrite) => {
|
||||
}
|
||||
Next_::ReadWrite => {
|
||||
http1.reading = match http1.reading {
|
||||
Reading::Init => Reading::Parse,
|
||||
Reading::Wait(decoder) => Reading::Body(decoder),
|
||||
same => same
|
||||
same => same,
|
||||
};
|
||||
http1.writing = match http1.writing {
|
||||
Writing::Wait(encoder) => Writing::Ready(encoder),
|
||||
Writing::Init => Writing::Head,
|
||||
Writing::Chunk(chunk) => if chunk.is_written() {
|
||||
Writing::Chunk(chunk) => {
|
||||
if chunk.is_written() {
|
||||
Writing::Ready(chunk.next.0)
|
||||
} else {
|
||||
Writing::Chunk(chunk)
|
||||
},
|
||||
same => same
|
||||
}
|
||||
}
|
||||
same => same,
|
||||
};
|
||||
State::Http1(http1)
|
||||
},
|
||||
(State::Http1(mut http1), Next_::Wait) => {
|
||||
}
|
||||
Next_::Wait => {
|
||||
http1.reading = match http1.reading {
|
||||
Reading::Body(decoder) => Reading::Wait(decoder),
|
||||
same => same
|
||||
same => same,
|
||||
};
|
||||
|
||||
http1.writing = match http1.writing {
|
||||
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)
|
||||
} else {
|
||||
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;
|
||||
State::Http1(http1)
|
||||
mem::replace(self, State::Http1(http1));
|
||||
}
|
||||
other => other
|
||||
};
|
||||
mem::replace(self, new_state);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user