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