1//! Define a response type for directory requests.
23use std::str;
45use tor_linkspec::{LoggedChanTarget, OwnedChanTarget};
6use tor_proto::circuit::{ClientCirc, UniqId};
78use crate::{RequestError, RequestFailedError};
910/// A successful (or at any rate, well-formed) response to a directory
11/// request.
12#[derive(Debug, Clone)]
13#[must_use = "You need to check whether the response was successful."]
14pub struct DirResponse {
15/// An HTTP status code.
16status: u16,
17/// The message associated with the status code.
18status_message: Option<String>,
19/// The decompressed output that we got from the directory cache.
20output: Vec<u8>,
21/// The error, if any, that caused us to stop getting this response early.
22error: Option<RequestError>,
23/// Information about the directory cache we used.
24source: Option<SourceInfo>,
25}
2627/// Information about the source of a directory response.
28///
29/// We use this to remember when a request has failed, so we can
30/// abandon the circuit.
31#[derive(Debug, Clone, derive_more::Display)]
32#[display("{} via {}", cache_id, circuit)]
33pub struct SourceInfo {
34/// Unique identifier for the circuit we're using
35circuit: UniqId,
36/// Identity of the directory cache that provided us this information.
37cache_id: LoggedChanTarget,
38}
3940impl DirResponse {
41/// Construct a new DirResponse from its parts
42pub(crate) fn new(
43 status: u16,
44 status_message: Option<String>,
45 error: Option<RequestError>,
46 output: Vec<u8>,
47 source: Option<SourceInfo>,
48 ) -> Self {
49 DirResponse {
50 status,
51 status_message,
52 output,
53 error,
54 source,
55 }
56 }
5758/// Construct a new successful DirResponse from its body.
59pub fn from_body(body: impl AsRef<[u8]>) -> Self {
60Self::new(200, None, None, body.as_ref().to_vec(), None)
61 }
6263/// Return the HTTP status code for this response.
64pub fn status_code(&self) -> u16 {
65self.status
66 }
6768/// Return true if this is in incomplete response.
69pub fn is_partial(&self) -> bool {
70self.error.is_some()
71 }
7273/// Return the error from this response, if any.
74pub fn error(&self) -> Option<&RequestError> {
75self.error.as_ref()
76 }
7778/// Return the output from this response.
79 ///
80 /// Returns some output, even if the response indicates truncation or an error.
81pub fn output_unchecked(&self) -> &[u8] {
82&self.output
83 }
8485/// Return the output from this response, if it was successful and complete.
86pub fn output(&self) -> Result<&[u8], RequestFailedError> {
87self.check_ok()?;
88Ok(self.output_unchecked())
89 }
9091/// Return this the output from this response, as a string,
92 /// if it was successful and complete and valid UTF-8.
93pub fn output_string(&self) -> Result<&str, RequestFailedError> {
94let output = self.output()?;
95let s = str::from_utf8(output).map_err(|_| RequestFailedError {
96// For RequestError::Utf8Encoding We need a `String::FromUtf8Error`
97 // (which contains an owned copy of the bytes).
98error: String::from_utf8(output.to_owned())
99 .expect_err("was bad, now good")
100 .into(),
101 source: self.source.clone(),
102 })?;
103Ok(s)
104 }
105106/// Consume this DirResponse and return the output in it.
107 ///
108 /// Returns some output, even if the response indicates truncation or an error.
109pub fn into_output_unchecked(self) -> Vec<u8> {
110self.output
111 }
112113/// Consume this DirResponse and return the output, if it was successful and complete.
114pub fn into_output(self) -> Result<Vec<u8>, RequestFailedError> {
115self.check_ok()?;
116Ok(self.into_output_unchecked())
117 }
118119/// Consume this DirResponse and return the output, as a string,
120 /// if it was successful and complete and valid UTF-8.
121pub fn into_output_string(self) -> Result<String, RequestFailedError> {
122self.check_ok()?;
123let s = String::from_utf8(self.output).map_err(|error| RequestFailedError {
124 error: error.into(),
125 source: self.source.clone(),
126 })?;
127Ok(s)
128 }
129130/// Return the source information about this response.
131pub fn source(&self) -> Option<&SourceInfo> {
132self.source.as_ref()
133 }
134135/// Check if this request was successful and complete.
136fn check_ok(&self) -> Result<(), RequestFailedError> {
137let wrap_err = |error| {
138Err(RequestFailedError {
139 error,
140 source: self.source.clone(),
141 })
142 };
143if let Some(error) = &self.error {
144return wrap_err(error.clone());
145 }
146assert!(!self.is_partial(), "partial but no error?");
147if self.status_code() != 200 {
148let msg = match &self.status_message {
149Some(m) => m.clone(),
150None => "".to_owned(),
151 };
152return wrap_err(RequestError::HttpStatus(self.status_code(), msg));
153 }
154Ok(())
155 }
156}
157158impl SourceInfo {
159/// Try to construct a new SourceInfo representing the last hop of a given circuit.
160 ///
161 /// Return an error if the circuit is closed;
162 /// return `Ok(None)` if the circuit's last hop is virtual.
163pub fn from_circuit(circuit: &ClientCirc) -> tor_proto::Result<Option<Self>> {
164match circuit.last_hop_info()? {
165None => Ok(None),
166Some(last_hop) => Ok(Some(SourceInfo {
167 circuit: circuit.unique_id(),
168 cache_id: last_hop.into(),
169 })),
170 }
171 }
172173/// Return the unique circuit identifier for the circuit on which
174 /// we received this info.
175pub fn unique_circ_id(&self) -> &UniqId {
176&self.circuit
177 }
178179/// Return information about the peer from which we received this info.
180pub fn cache_id(&self) -> &OwnedChanTarget {
181self.cache_id.as_inner()
182 }
183}
184185#[cfg(test)]
186mod test {
187// @@ begin test lint list maintained by maint/add_warning @@
188#![allow(clippy::bool_assert_comparison)]
189 #![allow(clippy::clone_on_copy)]
190 #![allow(clippy::dbg_macro)]
191 #![allow(clippy::mixed_attributes_style)]
192 #![allow(clippy::print_stderr)]
193 #![allow(clippy::print_stdout)]
194 #![allow(clippy::single_char_pattern)]
195 #![allow(clippy::unwrap_used)]
196 #![allow(clippy::unchecked_duration_subtraction)]
197 #![allow(clippy::useless_vec)]
198 #![allow(clippy::needless_pass_by_value)]
199//! <!-- @@ end test lint list maintained by maint/add_warning @@ -->
200use super::*;
201202#[test]
203fn errors() {
204let mut response = DirResponse::new(200, None, None, vec![b'Y'], None);
205206assert_eq!(response.output().unwrap(), b"Y");
207assert_eq!(response.clone().into_output().unwrap(), b"Y");
208209let expect_error = |response: &DirResponse, error: RequestError| {
210let error = RequestFailedError {
211 error,
212 source: None,
213 };
214let error = format!("{:?}", error);
215216assert_eq!(error, format!("{:?}", response.output().unwrap_err()));
217assert_eq!(
218 error,
219format!("{:?}", response.clone().into_output().unwrap_err())
220 );
221 };
222223let with_error = |response: &DirResponse| {
224let mut response = response.clone();
225 response.error = Some(RequestError::DirTimeout);
226 expect_error(&response, RequestError::DirTimeout);
227 };
228229 with_error(&response);
230231 response.status = 404;
232 response.status_message = Some("Not found".into());
233 expect_error(&response, RequestError::HttpStatus(404, "Not found".into()));
234235 with_error(&response);
236 }
237}