1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use std::str;
use tor_linkspec::{LoggedChanTarget, OwnedChanTarget};
use tor_proto::circuit::{ClientCirc, UniqId};
use crate::{RequestError, RequestFailedError};
#[derive(Debug, Clone)]
pub struct DirResponse {
status: u16,
output: Vec<u8>,
error: Option<RequestError>,
source: Option<SourceInfo>,
}
#[derive(Debug, Clone, derive_more::Display)]
#[display(fmt = "{} via {}", cache_id, circuit)]
pub struct SourceInfo {
circuit: UniqId,
cache_id: LoggedChanTarget,
}
impl DirResponse {
pub(crate) fn new(
status: u16,
error: Option<RequestError>,
output: Vec<u8>,
source: Option<SourceInfo>,
) -> Self {
DirResponse {
status,
output,
error,
source,
}
}
pub fn from_body(body: impl AsRef<[u8]>) -> Self {
Self::new(200, None, body.as_ref().to_vec(), None)
}
pub fn status_code(&self) -> u16 {
self.status
}
pub fn is_partial(&self) -> bool {
self.error.is_some()
}
pub fn error(&self) -> Option<&RequestError> {
self.error.as_ref()
}
pub fn output_unchecked(&self) -> &[u8] {
&self.output
}
pub fn output(&self) -> Result<&[u8], RequestFailedError> {
self.check_ok()?;
Ok(self.output_unchecked())
}
pub fn output_string(&self) -> Result<&str, RequestFailedError> {
let output = self.output()?;
let s = str::from_utf8(output).map_err(|_| RequestFailedError {
error: String::from_utf8(output.to_owned())
.expect_err("was bad, now good")
.into(),
source: self.source.clone(),
})?;
Ok(s)
}
pub fn into_output_unchecked(self) -> Vec<u8> {
self.output
}
pub fn into_output(self) -> Result<Vec<u8>, RequestFailedError> {
self.check_ok()?;
Ok(self.into_output_unchecked())
}
pub fn into_output_string(self) -> Result<String, RequestFailedError> {
self.check_ok()?;
let s = String::from_utf8(self.output).map_err(|error| RequestFailedError {
error: error.into(),
source: self.source.clone(),
})?;
Ok(s)
}
pub fn source(&self) -> Option<&SourceInfo> {
self.source.as_ref()
}
fn check_ok(&self) -> Result<(), RequestFailedError> {
let wrap_err = |error| {
Err(RequestFailedError {
error,
source: self.source.clone(),
})
};
if let Some(error) = &self.error {
return wrap_err(error.clone());
}
assert!(!self.is_partial(), "partial but no error?");
if self.status_code() != 200 {
return wrap_err(RequestError::HttpStatus(self.status_code()));
}
Ok(())
}
}
impl SourceInfo {
pub(crate) fn from_circuit(circuit: &ClientCirc) -> Self {
SourceInfo {
circuit: circuit.unique_id(),
cache_id: circuit.first_hop().into(),
}
}
pub fn unique_circ_id(&self) -> &UniqId {
&self.circuit
}
pub fn cache_id(&self) -> &OwnedChanTarget {
self.cache_id.as_inner()
}
}
#[cfg(test)]
mod test {
#![allow(clippy::bool_assert_comparison)]
#![allow(clippy::clone_on_copy)]
#![allow(clippy::dbg_macro)]
#![allow(clippy::print_stderr)]
#![allow(clippy::print_stdout)]
#![allow(clippy::single_char_pattern)]
#![allow(clippy::unwrap_used)]
use super::*;
#[test]
fn errors() {
let mut response = DirResponse::new(200, None, vec![b'Y'], None);
assert_eq!(response.output().unwrap(), b"Y");
assert_eq!(response.clone().into_output().unwrap(), b"Y");
let expect_error = |response: &DirResponse, error: RequestError| {
let error = RequestFailedError {
error,
source: None,
};
let error = format!("{:?}", error);
assert_eq!(error, format!("{:?}", response.output().unwrap_err()));
assert_eq!(
error,
format!("{:?}", response.clone().into_output().unwrap_err())
);
};
let with_error = |response: &DirResponse| {
let mut response = response.clone();
response.error = Some(RequestError::DirTimeout);
expect_error(&response, RequestError::DirTimeout);
};
with_error(&response);
response.status = 404;
expect_error(&response, RequestError::HttpStatus(404));
with_error(&response);
}
}