tor_dirclient/
response.rs1use std::str;
4
5use tor_linkspec::{LoggedChanTarget, OwnedChanTarget};
6use tor_proto::circuit::{ClientCirc, UniqId};
7
8use crate::{RequestError, RequestFailedError};
9
10#[derive(Debug, Clone)]
13#[must_use = "You need to check whether the response was successful."]
14pub struct DirResponse {
15 status: u16,
17 status_message: Option<String>,
19 output: Vec<u8>,
21 error: Option<RequestError>,
23 source: Option<SourceInfo>,
25}
26
27#[derive(Debug, Clone, derive_more::Display)]
32#[display("{} via {}", cache_id, circuit)]
33pub struct SourceInfo {
34 circuit: UniqId,
36 cache_id: LoggedChanTarget,
38}
39
40impl DirResponse {
41 pub(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 }
57
58 pub fn from_body(body: impl AsRef<[u8]>) -> Self {
60 Self::new(200, None, None, body.as_ref().to_vec(), None)
61 }
62
63 pub fn status_code(&self) -> u16 {
65 self.status
66 }
67
68 pub fn is_partial(&self) -> bool {
70 self.error.is_some()
71 }
72
73 pub fn error(&self) -> Option<&RequestError> {
75 self.error.as_ref()
76 }
77
78 pub fn output_unchecked(&self) -> &[u8] {
82 &self.output
83 }
84
85 pub fn output(&self) -> Result<&[u8], RequestFailedError> {
87 self.check_ok()?;
88 Ok(self.output_unchecked())
89 }
90
91 pub fn output_string(&self) -> Result<&str, RequestFailedError> {
94 let output = self.output()?;
95 let s = str::from_utf8(output).map_err(|_| RequestFailedError {
96 error: String::from_utf8(output.to_owned())
99 .expect_err("was bad, now good")
100 .into(),
101 source: self.source.clone(),
102 })?;
103 Ok(s)
104 }
105
106 pub fn into_output_unchecked(self) -> Vec<u8> {
110 self.output
111 }
112
113 pub fn into_output(self) -> Result<Vec<u8>, RequestFailedError> {
115 self.check_ok()?;
116 Ok(self.into_output_unchecked())
117 }
118
119 pub fn into_output_string(self) -> Result<String, RequestFailedError> {
122 self.check_ok()?;
123 let s = String::from_utf8(self.output).map_err(|error| RequestFailedError {
124 error: error.into(),
125 source: self.source.clone(),
126 })?;
127 Ok(s)
128 }
129
130 pub fn source(&self) -> Option<&SourceInfo> {
132 self.source.as_ref()
133 }
134
135 fn check_ok(&self) -> Result<(), RequestFailedError> {
137 let wrap_err = |error| {
138 Err(RequestFailedError {
139 error,
140 source: self.source.clone(),
141 })
142 };
143 if let Some(error) = &self.error {
144 return wrap_err(error.clone());
145 }
146 assert!(!self.is_partial(), "partial but no error?");
147 if self.status_code() != 200 {
148 let msg = match &self.status_message {
149 Some(m) => m.clone(),
150 None => "".to_owned(),
151 };
152 return wrap_err(RequestError::HttpStatus(self.status_code(), msg));
153 }
154 Ok(())
155 }
156}
157
158impl SourceInfo {
159 pub fn from_circuit(circuit: &ClientCirc) -> tor_proto::Result<Option<Self>> {
164 match circuit.last_hop_info()? {
165 None => Ok(None),
166 Some(last_hop) => Ok(Some(SourceInfo {
167 circuit: circuit.unique_id(),
168 cache_id: last_hop.into(),
169 })),
170 }
171 }
172
173 pub fn unique_circ_id(&self) -> &UniqId {
176 &self.circuit
177 }
178
179 pub fn cache_id(&self) -> &OwnedChanTarget {
181 self.cache_id.as_inner()
182 }
183}
184
185#[cfg(test)]
186mod test {
187 #![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 use super::*;
201
202 #[test]
203 fn errors() {
204 let mut response = DirResponse::new(200, None, None, vec![b'Y'], None);
205
206 assert_eq!(response.output().unwrap(), b"Y");
207 assert_eq!(response.clone().into_output().unwrap(), b"Y");
208
209 let expect_error = |response: &DirResponse, error: RequestError| {
210 let error = RequestFailedError {
211 error,
212 source: None,
213 };
214 let error = format!("{:?}", error);
215
216 assert_eq!(error, format!("{:?}", response.output().unwrap_err()));
217 assert_eq!(
218 error,
219 format!("{:?}", response.clone().into_output().unwrap_err())
220 );
221 };
222
223 let with_error = |response: &DirResponse| {
224 let mut response = response.clone();
225 response.error = Some(RequestError::DirTimeout);
226 expect_error(&response, RequestError::DirTimeout);
227 };
228
229 with_error(&response);
230
231 response.status = 404;
232 response.status_message = Some("Not found".into());
233 expect_error(&response, RequestError::HttpStatus(404, "Not found".into()));
234
235 with_error(&response);
236 }
237}