adds HttpWriters
This commit is contained in:
		| @@ -8,7 +8,7 @@ use time::now_utc; | ||||
|  | ||||
| use header; | ||||
| use header::common; | ||||
| use http::{CR, LF, LINE_ENDING}; | ||||
| use http::{CR, LF, LINE_ENDING, HttpWriter, ThroughWriter, ChunkedWriter, SizedWriter}; | ||||
| use status; | ||||
| use net::{NetworkStream, WriteStatus, Fresh, Streaming}; | ||||
| use version; | ||||
| @@ -18,7 +18,7 @@ pub struct Response<W: WriteStatus> { | ||||
|     /// The HTTP version of this response. | ||||
|     pub version: version::HttpVersion, | ||||
|     // Stream the Response is writing to, not accessible through UnwrittenResponse | ||||
|     body: BufferedWriter<Box<NetworkStream + Send>>, // TODO: use a HttpWriter from http | ||||
|     body: HttpWriter<BufferedWriter<Box<NetworkStream + Send>>>, | ||||
|     // The status code for the request. | ||||
|     status: status::StatusCode, | ||||
|     // The outgoing headers on this response. | ||||
| @@ -35,7 +35,7 @@ impl<W: WriteStatus> Response<W> { | ||||
|  | ||||
|     /// Construct a Response from its constituent parts. | ||||
|     pub fn construct(version: version::HttpVersion, | ||||
|                      body: BufferedWriter<Box<NetworkStream + Send>>, | ||||
|                      body: HttpWriter<BufferedWriter<Box<NetworkStream + Send>>>, | ||||
|                      status: status::StatusCode, | ||||
|                      headers: header::Headers) -> Response<Fresh> { | ||||
|         Response { | ||||
| @@ -54,7 +54,7 @@ impl Response<Fresh> { | ||||
|             status: status::Ok, | ||||
|             version: version::Http11, | ||||
|             headers: header::Headers::new(), | ||||
|             body: BufferedWriter::new(stream.abstract()) | ||||
|             body: ThroughWriter(BufferedWriter::new(stream.abstract())) | ||||
|         } | ||||
|     } | ||||
|  | ||||
| @@ -67,18 +67,50 @@ impl Response<Fresh> { | ||||
|             self.headers.set(common::Date(now_utc())); | ||||
|         } | ||||
|  | ||||
|  | ||||
|         let mut chunked = true; | ||||
|         let mut len = 0; | ||||
|  | ||||
|         match self.headers.get_ref::<common::ContentLength>() { | ||||
|             Some(cl) => { | ||||
|                 chunked = false; | ||||
|                 len = cl.len(); | ||||
|             }, | ||||
|             None => () | ||||
|         }; | ||||
|  | ||||
|         // cant do in match above, thanks borrowck | ||||
|         if chunked { | ||||
|             //TODO: use CollectionViews (when implemented) to prevent double hash/lookup | ||||
|             let encodings = match self.headers.get::<common::TransferEncoding>() { | ||||
|                 Some(common::TransferEncoding(mut encodings)) => { | ||||
|                     //TODO: check if chunked is already in encodings. use HashSet? | ||||
|                     encodings.push(common::transfer_encoding::Chunked); | ||||
|                     encodings | ||||
|                 }, | ||||
|                 None => vec![common::transfer_encoding::Chunked] | ||||
|             }; | ||||
|             self.headers.set(common::TransferEncoding(encodings)); | ||||
|         } | ||||
|  | ||||
|         for (name, header) in self.headers.iter() { | ||||
|             debug!("headers {}: {}", name, header); | ||||
|             debug!("header {}: {}", name, header); | ||||
|             try!(write!(self.body, "{}: {}", name, header)); | ||||
|             try!(self.body.write(LINE_ENDING)); | ||||
|         } | ||||
|  | ||||
|         try!(self.body.write(LINE_ENDING)); | ||||
|  | ||||
|         let stream = if chunked { | ||||
|             ChunkedWriter(self.body.unwrap()) | ||||
|         } else { | ||||
|             SizedWriter(self.body.unwrap(), len) | ||||
|         }; | ||||
|  | ||||
|         // "copy" to change the phantom type | ||||
|         Ok(Response { | ||||
|             version: self.version, | ||||
|             body: self.body, | ||||
|             body: stream, | ||||
|             status: self.status, | ||||
|             headers: self.headers | ||||
|         }) | ||||
| @@ -92,7 +124,7 @@ impl Response<Fresh> { | ||||
|     pub fn headers_mut(&mut self) -> &mut header::Headers { &mut self.headers } | ||||
|  | ||||
|     /// Deconstruct this Response into its constituent parts. | ||||
|     pub fn deconstruct(self) -> (version::HttpVersion, BufferedWriter<Box<NetworkStream + Send>>, | ||||
|     pub fn deconstruct(self) -> (version::HttpVersion, HttpWriter<BufferedWriter<Box<NetworkStream + Send>>>, | ||||
|                                  status::StatusCode, header::Headers) { | ||||
|         (self.version, self.body, self.status, self.headers) | ||||
|     } | ||||
| @@ -100,9 +132,10 @@ impl Response<Fresh> { | ||||
|  | ||||
| impl Response<Streaming> { | ||||
|     /// Flushes all writing of a response to the client. | ||||
|     pub fn end(mut self) -> IoResult<()> { | ||||
|     pub fn end(self) -> IoResult<()> { | ||||
|         debug!("ending"); | ||||
|         self.flush() | ||||
|         try!(self.body.end()); | ||||
|         Ok(()) | ||||
|     } | ||||
| } | ||||
|  | ||||
|   | ||||
		Reference in New Issue
	
	Block a user