summaryrefslogtreecommitdiff
path: root/src/response.rs
diff options
context:
space:
mode:
Diffstat (limited to 'src/response.rs')
-rw-r--r--src/response.rs52
1 files changed, 52 insertions, 0 deletions
diff --git a/src/response.rs b/src/response.rs
new file mode 100644
index 0000000..801e682
--- /dev/null
+++ b/src/response.rs
@@ -0,0 +1,52 @@
+use tokio::{
+ fs::File,
+ io::{self, AsyncWrite, AsyncWriteExt},
+};
+
+#[derive(Debug)]
+pub enum Body {
+ File(File),
+ Bytes(Vec<u8>),
+ Empty,
+}
+
+#[derive(Debug)]
+pub struct Response(http::Response<Body>);
+
+impl Response {
+ pub fn new(inner: http::Response<Body>) -> Self {
+ Self(inner)
+ }
+
+ pub fn inner(&self) -> &http::Response<Body> {
+ &self.0
+ }
+}
+
+impl Response {
+ pub async fn to_wire<W: AsyncWrite + Unpin>(self, writer: &mut W) -> io::Result<()> {
+ writer
+ .write_all(format!("HTTP/1.1 {}\r\n", self.0.status()).as_bytes())
+ .await?;
+
+ for (key, value) in self.0.headers() {
+ writer.write_all(format!("{key}: ").as_bytes()).await?;
+ writer.write_all(value.as_bytes()).await?;
+ writer.write_all(b"\r\n").await?;
+ }
+
+ writer.write_all(b"\r\n").await?;
+
+ match self.0.into_body() {
+ Body::File(mut file) => {
+ io::copy(&mut file, writer).await?;
+ }
+ Body::Bytes(buf) => {
+ writer.write_all(&buf).await?;
+ }
+ Body::Empty => (),
+ }
+
+ Ok(())
+ }
+}