trust_dns_proto/https/
response.rs

1// Copyright 2015-2018 Benjamin Fry <benjaminfry@me.com>
2//
3// Licensed under the Apache License, Version 2.0, <LICENSE-APACHE or
4// http://apache.org/licenses/LICENSE-2.0> or the MIT license <LICENSE-MIT or
5// http://opensource.org/licenses/MIT>, at your option. This file may not be
6// copied, modified, or distributed except according to those terms.
7
8//! HTTP request creation and validation
9
10use http::header::{CONTENT_LENGTH, CONTENT_TYPE};
11use http::{Response, StatusCode, Version};
12
13use crate::error::ProtoError;
14use crate::https::HttpsResult;
15
16/// Create a new Response for an http/2 dns-message request
17///
18/// ```text
19///  4.2.1.  Handling DNS and HTTP Errors
20///
21/// DNS response codes indicate either success or failure for the DNS
22/// query.  A successful HTTP response with a 2xx status code ([RFC7231]
23/// Section 6.3) is used for any valid DNS response, regardless of the
24/// DNS response code.  For example, a successful 2xx HTTP status code is
25/// used even with a DNS message whose DNS response code indicates
26/// failure, such as SERVFAIL or NXDOMAIN.
27///
28/// HTTP responses with non-successful HTTP status codes do not contain
29/// replies to the original DNS question in the HTTP request.  DoH
30///
31/// clients need to use the same semantic processing of non-successful
32/// HTTP status codes as other HTTP clients.  This might mean that the
33/// DoH client retries the query with the same DoH server, such as if
34/// there are authorization failures (HTTP status code 401 [RFC7235]
35/// Section 3.1).  It could also mean that the DoH client retries with a
36/// different DoH server, such as for unsupported media types (HTTP
37/// status code 415, [RFC7231] Section 6.5.13), or where the server
38/// cannot generate a representation suitable for the client (HTTP status
39/// code 406, [RFC7231] Section 6.5.6), and so on.
40/// ```
41pub fn new(message_len: usize) -> HttpsResult<Response<()>> {
42    Response::builder()
43        .status(StatusCode::OK)
44        .version(Version::HTTP_2)
45        .header(CONTENT_TYPE, crate::https::MIME_APPLICATION_DNS)
46        .header(CONTENT_LENGTH, message_len)
47        .body(())
48        .map_err(|e| ProtoError::from(format!("invalid response: {e}")).into())
49}