tiny_http/response.rs
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574
use crate::common::{HTTPVersion, Header, StatusCode};
use httpdate::HttpDate;
use std::cmp::Ordering;
use std::sync::mpsc::Receiver;
use std::io::Result as IoResult;
use std::io::{self, Cursor, Read, Write};
use std::fs::File;
use std::str::FromStr;
use std::time::SystemTime;
/// Object representing an HTTP response whose purpose is to be given to a `Request`.
///
/// Some headers cannot be changed. Trying to define the value
/// of one of these will have no effect:
///
/// - `Connection`
/// - `Trailer`
/// - `Transfer-Encoding`
/// - `Upgrade`
///
/// Some headers have special behaviors:
///
/// - `Content-Encoding`: If you define this header, the library
/// will assume that the data from the `Read` object has the specified encoding
/// and will just pass-through.
///
/// - `Content-Length`: The length of the data should be set manually
/// using the `Reponse` object's API. Attempting to set the value of this
/// header will be equivalent to modifying the size of the data but the header
/// itself may not be present in the final result.
///
/// - `Content-Type`: You may only set this header to one value at a time. If you
/// try to set it more than once, the existing value will be overwritten. This
/// behavior differs from the default for most headers, which is to allow them to
/// be set multiple times in the same response.
///
pub struct Response<R> {
reader: R,
status_code: StatusCode,
headers: Vec<Header>,
data_length: Option<usize>,
chunked_threshold: Option<usize>,
}
/// A `Response` without a template parameter.
pub type ResponseBox = Response<Box<dyn Read + Send>>;
/// Transfer encoding to use when sending the message.
/// Note that only *supported* encoding are listed here.
#[derive(Copy, Clone)]
enum TransferEncoding {
Identity,
Chunked,
}
impl FromStr for TransferEncoding {
type Err = ();
fn from_str(input: &str) -> Result<TransferEncoding, ()> {
if input.eq_ignore_ascii_case("identity") {
Ok(TransferEncoding::Identity)
} else if input.eq_ignore_ascii_case("chunked") {
Ok(TransferEncoding::Chunked)
} else {
Err(())
}
}
}
/// Builds a Date: header with the current date.
fn build_date_header() -> Header {
let d = HttpDate::from(SystemTime::now());
Header::from_bytes(&b"Date"[..], &d.to_string().into_bytes()[..]).unwrap()
}
fn write_message_header<W>(
mut writer: W,
http_version: &HTTPVersion,
status_code: &StatusCode,
headers: &[Header],
) -> IoResult<()>
where
W: Write,
{
// writing status line
write!(
&mut writer,
"HTTP/{}.{} {} {}\r\n",
http_version.0,
http_version.1,
status_code.0,
status_code.default_reason_phrase()
)?;
// writing headers
for header in headers.iter() {
writer.write_all(header.field.as_str().as_ref())?;
write!(&mut writer, ": ")?;
writer.write_all(header.value.as_str().as_ref())?;
write!(&mut writer, "\r\n")?;
}
// separator between header and data
write!(&mut writer, "\r\n")?;
Ok(())
}
fn choose_transfer_encoding(
status_code: StatusCode,
request_headers: &[Header],
http_version: &HTTPVersion,
entity_length: &Option<usize>,
has_additional_headers: bool,
chunked_threshold: usize,
) -> TransferEncoding {
use crate::util;
// HTTP 1.0 doesn't support other encoding
if *http_version <= (1, 0) {
return TransferEncoding::Identity;
}
// Per section 3.3.1 of RFC7230:
// A server MUST NOT send a Transfer-Encoding header field in any response with a status code
// of 1xx (Informational) or 204 (No Content).
if status_code.0 < 200 || status_code.0 == 204 {
return TransferEncoding::Identity;
}
// parsing the request's TE header
let user_request = request_headers
.iter()
// finding TE
.find(|h| h.field.equiv("TE"))
// getting its value
.map(|h| h.value.clone())
// getting the corresponding TransferEncoding
.and_then(|value| {
// getting list of requested elements
let mut parse = util::parse_header_value(value.as_str()); // TODO: remove conversion
// sorting elements by most priority
parse.sort_by(|a, b| b.1.partial_cmp(&a.1).unwrap_or(Ordering::Equal));
// trying to parse each requested encoding
for value in parse.iter() {
// q=0 are ignored
if value.1 <= 0.0 {
continue;
}
if let Ok(te) = TransferEncoding::from_str(value.0) {
return Some(te);
}
}
// encoding not found
None
});
if let Some(user_request) = user_request {
return user_request;
}
// if we have additional headers, using chunked
if has_additional_headers {
return TransferEncoding::Chunked;
}
// if we don't have a Content-Length, or if the Content-Length is too big, using chunks writer
if entity_length
.as_ref()
.map_or(true, |val| *val >= chunked_threshold)
{
return TransferEncoding::Chunked;
}
// Identity by default
TransferEncoding::Identity
}
impl<R> Response<R>
where
R: Read,
{
/// Creates a new Response object.
///
/// The `additional_headers` argument is a receiver that
/// may provide headers even after the response has been sent.
///
/// All the other arguments are straight-forward.
pub fn new(
status_code: StatusCode,
headers: Vec<Header>,
data: R,
data_length: Option<usize>,
additional_headers: Option<Receiver<Header>>,
) -> Response<R> {
let mut response = Response {
reader: data,
status_code,
headers: Vec::with_capacity(16),
data_length,
chunked_threshold: None,
};
for h in headers {
response.add_header(h)
}
// dummy implementation
if let Some(additional_headers) = additional_headers {
for h in additional_headers.iter() {
response.add_header(h)
}
}
response
}
/// Set a threshold for `Content-Length` where we chose chunked
/// transfer. Notice that chunked transfer might happen regardless of
/// this threshold, for instance when the request headers indicate
/// it is wanted or when there is no `Content-Length`.
pub fn with_chunked_threshold(mut self, length: usize) -> Response<R> {
self.chunked_threshold = Some(length);
self
}
/// Convert the response into the underlying `Read` type.
///
/// This is mainly useful for testing as it must consume the `Response`.
pub fn into_reader(self) -> R {
self.reader
}
/// The current `Content-Length` threshold for switching over to
/// chunked transfer. The default is 32768 bytes. Notice that
/// chunked transfer is mutually exclusive with sending a
/// `Content-Length` header as per the HTTP spec.
pub fn chunked_threshold(&self) -> usize {
self.chunked_threshold.unwrap_or(32768)
}
/// Adds a header to the list.
/// Does all the checks.
pub fn add_header<H>(&mut self, header: H)
where
H: Into<Header>,
{
let header = header.into();
// ignoring forbidden headers
if header.field.equiv("Connection")
|| header.field.equiv("Trailer")
|| header.field.equiv("Transfer-Encoding")
|| header.field.equiv("Upgrade")
{
return;
}
// if the header is Content-Length, setting the data length
if header.field.equiv("Content-Length") {
if let Ok(val) = usize::from_str(header.value.as_str()) {
self.data_length = Some(val)
}
return;
// if the header is Content-Type and it's already set, overwrite it
} else if header.field.equiv("Content-Type") {
if let Some(content_type_header) = self
.headers
.iter_mut()
.find(|h| h.field.equiv("Content-Type"))
{
content_type_header.value = header.value;
return;
}
}
self.headers.push(header);
}
/// Returns the same request, but with an additional header.
///
/// Some headers cannot be modified and some other have a
/// special behavior. See the documentation above.
#[inline]
pub fn with_header<H>(mut self, header: H) -> Response<R>
where
H: Into<Header>,
{
self.add_header(header.into());
self
}
/// Returns the same request, but with a different status code.
#[inline]
pub fn with_status_code<S>(mut self, code: S) -> Response<R>
where
S: Into<StatusCode>,
{
self.status_code = code.into();
self
}
/// Returns the same request, but with different data.
pub fn with_data<S>(self, reader: S, data_length: Option<usize>) -> Response<S>
where
S: Read,
{
Response {
reader,
headers: self.headers,
status_code: self.status_code,
data_length,
chunked_threshold: self.chunked_threshold,
}
}
/// Prints the HTTP response to a writer.
///
/// This function is the one used to send the response to the client's socket.
/// Therefore you shouldn't expect anything pretty-printed or even readable.
///
/// The HTTP version and headers passed as arguments are used to
/// decide which features (most notably, encoding) to use.
///
/// Note: does not flush the writer.
pub fn raw_print<W: Write>(
mut self,
mut writer: W,
http_version: HTTPVersion,
request_headers: &[Header],
do_not_send_body: bool,
upgrade: Option<&str>,
) -> IoResult<()> {
let mut transfer_encoding = Some(choose_transfer_encoding(
self.status_code,
request_headers,
&http_version,
&self.data_length,
false, /* TODO */
self.chunked_threshold(),
));
// add `Date` if not in the headers
if !self.headers.iter().any(|h| h.field.equiv("Date")) {
self.headers.insert(0, build_date_header());
}
// add `Server` if not in the headers
if !self.headers.iter().any(|h| h.field.equiv("Server")) {
self.headers.insert(
0,
Header::from_bytes(&b"Server"[..], &b"tiny-http (Rust)"[..]).unwrap(),
);
}
// handling upgrade
if let Some(upgrade) = upgrade {
self.headers.insert(
0,
Header::from_bytes(&b"Upgrade"[..], upgrade.as_bytes()).unwrap(),
);
self.headers.insert(
0,
Header::from_bytes(&b"Connection"[..], &b"upgrade"[..]).unwrap(),
);
transfer_encoding = None;
}
// if the transfer encoding is identity, the content length must be known ; therefore if
// we don't know it, we buffer the entire response first here
// while this is an expensive operation, it is only ever needed for clients using HTTP 1.0
let (mut reader, data_length): (Box<dyn Read>, _) =
match (self.data_length, transfer_encoding) {
(Some(l), _) => (Box::new(self.reader), Some(l)),
(None, Some(TransferEncoding::Identity)) => {
let mut buf = Vec::new();
self.reader.read_to_end(&mut buf)?;
let l = buf.len();
(Box::new(Cursor::new(buf)), Some(l))
}
_ => (Box::new(self.reader), None),
};
// checking whether to ignore the body of the response
let do_not_send_body = do_not_send_body
|| match self.status_code.0 {
// status code 1xx, 204 and 304 MUST not include a body
100..=199 | 204 | 304 => true,
_ => false,
};
// preparing headers for transfer
match transfer_encoding {
Some(TransferEncoding::Chunked) => self
.headers
.push(Header::from_bytes(&b"Transfer-Encoding"[..], &b"chunked"[..]).unwrap()),
Some(TransferEncoding::Identity) => {
assert!(data_length.is_some());
let data_length = data_length.unwrap();
self.headers.push(
Header::from_bytes(
&b"Content-Length"[..],
format!("{}", data_length).as_bytes(),
)
.unwrap(),
)
}
_ => (),
};
// sending headers
write_message_header(
writer.by_ref(),
&http_version,
&self.status_code,
&self.headers,
)?;
// sending the body
if !do_not_send_body {
match transfer_encoding {
Some(TransferEncoding::Chunked) => {
use chunked_transfer::Encoder;
let mut writer = Encoder::new(writer);
io::copy(&mut reader, &mut writer)?;
}
Some(TransferEncoding::Identity) => {
assert!(data_length.is_some());
let data_length = data_length.unwrap();
if data_length >= 1 {
io::copy(&mut reader, &mut writer)?;
}
}
_ => (),
}
}
Ok(())
}
/// Retrieves the current value of the `Response` status code
pub fn status_code(&self) -> StatusCode {
self.status_code
}
/// Retrieves the current value of the `Response` data length
pub fn data_length(&self) -> Option<usize> {
self.data_length
}
/// Retrieves the current list of `Response` headers
pub fn headers(&self) -> &[Header] {
&self.headers
}
}
impl<R> Response<R>
where
R: Read + Send + 'static,
{
/// Turns this response into a `Response<Box<Read + Send>>`.
pub fn boxed(self) -> ResponseBox {
Response {
reader: Box::new(self.reader) as Box<dyn Read + Send>,
status_code: self.status_code,
headers: self.headers,
data_length: self.data_length,
chunked_threshold: self.chunked_threshold,
}
}
}
impl Response<File> {
/// Builds a new `Response` from a `File`.
///
/// The `Content-Type` will **not** be automatically detected,
/// you must set it yourself.
pub fn from_file(file: File) -> Response<File> {
let file_size = file.metadata().ok().map(|v| v.len() as usize);
Response::new(
StatusCode(200),
Vec::with_capacity(0),
file,
file_size,
None,
)
}
}
impl Response<Cursor<Vec<u8>>> {
pub fn from_data<D>(data: D) -> Response<Cursor<Vec<u8>>>
where
D: Into<Vec<u8>>,
{
let data = data.into();
let data_len = data.len();
Response::new(
StatusCode(200),
Vec::with_capacity(0),
Cursor::new(data),
Some(data_len),
None,
)
}
pub fn from_string<S>(data: S) -> Response<Cursor<Vec<u8>>>
where
S: Into<String>,
{
let data = data.into();
let data_len = data.len();
Response::new(
StatusCode(200),
vec![
Header::from_bytes(&b"Content-Type"[..], &b"text/plain; charset=UTF-8"[..])
.unwrap(),
],
Cursor::new(data.into_bytes()),
Some(data_len),
None,
)
}
}
impl Response<io::Empty> {
/// Builds an empty `Response` with the given status code.
pub fn empty<S>(status_code: S) -> Response<io::Empty>
where
S: Into<StatusCode>,
{
Response::new(
status_code.into(),
Vec::with_capacity(0),
io::empty(),
Some(0),
None,
)
}
/// DEPRECATED. Use `empty` instead.
pub fn new_empty(status_code: StatusCode) -> Response<io::Empty> {
Response::empty(status_code)
}
}
impl Clone for Response<io::Empty> {
fn clone(&self) -> Response<io::Empty> {
Response {
reader: io::empty(),
status_code: self.status_code,
headers: self.headers.clone(),
data_length: self.data_length,
chunked_threshold: self.chunked_threshold,
}
}
}