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
// Copyright 2015-2018 Benjamin Fry <benjaminfry@me.com> // // Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or // http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or // http://opensource.org/licenses/MIT>, at your option. This file may not be // copied, modified, or distributed except according to those terms. //! HTTP request creation and validation use std::str::FromStr; use http::{Response, StatusCode, Version}; use typed_headers::{mime::Mime, ContentLength, ContentType, HeaderMapExt}; use trust_dns_proto::error::ProtoError; use HttpsResult; /// Create a new Response for an http/2 dns-message request /// /// ```text /// 4.2.1. Handling DNS and HTTP Errors /// /// DNS response codes indicate either success or failure for the DNS /// query. A successful HTTP response with a 2xx status code ([RFC7231] /// Section 6.3) is used for any valid DNS response, regardless of the /// DNS response code. For example, a successful 2xx HTTP status code is /// used even with a DNS message whose DNS response code indicates /// failure, such as SERVFAIL or NXDOMAIN. /// /// HTTP responses with non-successful HTTP status codes do not contain /// replies to the original DNS question in the HTTP request. DoH /// /// clients need to use the same semantic processing of non-successful /// HTTP status codes as other HTTP clients. This might mean that the /// DoH client retries the query with the same DoH server, such as if /// there are authorization failures (HTTP status code 401 [RFC7235] /// Section 3.1). It could also mean that the DoH client retries with a /// different DoH server, such as for unsupported media types (HTTP /// status code 415, [RFC7231] Section 6.5.13), or where the server /// cannot generate a representation suitable for the client (HTTP status /// code 406, [RFC7231] Section 6.5.6), and so on. /// ``` pub fn new(message_len: usize) -> HttpsResult<Response<()>> { let mut response = Response::builder(); response.status(StatusCode::OK); response.version(Version::HTTP_2); let mut response = response .body(()) .map_err(|e| ProtoError::from(format!("invalid response: {}", e)))?; let accepts_dns = Mime::from_str(::MIME_APPLICATION_DNS).unwrap(); let content_type = ContentType(accepts_dns.clone()); response.headers_mut().typed_insert(&content_type); response .headers_mut() .typed_insert(&ContentLength(message_len as u64)); Ok(response) }