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