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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
use std::cell::{Cell, RefCell};
use std::io::{self, Read, Write};
use std::sync::{Once, ONCE_INIT};
use std::{fmt, ptr, thread};
use log::{Metadata, Record};
use rand::{self, Rng, ThreadRng};
#[derive(Default, Debug)]
pub struct ClientRequest {
boundary: Option<String>,
content_len: Option<u64>,
}
#[cfg(feature = "client")]
impl ::client::HttpRequest for ClientRequest {
type Stream = HttpBuffer;
type Error = io::Error;
fn apply_headers(&mut self, boundary: &str, content_len: Option<u64>) -> bool {
self.boundary = Some(boundary.into());
self.content_len = content_len;
true
}
fn open_stream(self) -> Result<HttpBuffer, io::Error> {
debug!("ClientRequest::open_stream called! {:?}", self);
let boundary = self.boundary.expect("ClientRequest::set_headers() was not called!");
Ok(HttpBuffer::new_empty(boundary, self.content_len))
}
}
pub struct HttpBuffer {
pub buf: Vec<u8>,
pub boundary: String,
pub content_len: Option<u64>,
rng: ThreadRng,
}
impl HttpBuffer {
pub fn new_empty(boundary: String, content_len: Option<u64>) -> HttpBuffer {
Self::with_buf(Vec::new(), boundary, content_len)
}
pub fn with_buf(buf: Vec<u8>, boundary: String, content_len: Option<u64>) -> Self {
HttpBuffer {
buf: buf,
boundary: boundary,
content_len: content_len,
rng: rand::thread_rng()
}
}
pub fn for_server(&self) -> ServerRequest {
ServerRequest {
data: &self.buf,
boundary: &self.boundary,
content_len: self.content_len,
rng: rand::thread_rng(),
}
}
}
impl Write for HttpBuffer {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
if buf.is_empty() {
debug!("HttpBuffer::write() was passed a zero-sized buffer.");
return Ok(0);
}
let len = self.rng.gen_range(1, buf.len() + 1);
self.buf.write(&buf[..len])
}
fn flush(&mut self) -> io::Result<()> {
self.buf.flush()
}
}
#[cfg(feature = "client")]
impl ::client::HttpStream for HttpBuffer {
type Request = ClientRequest;
type Response = HttpBuffer;
type Error = io::Error;
fn finish(self) -> Result<Self, io::Error> { Ok(self) }
}
impl fmt::Debug for HttpBuffer {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
f.debug_struct("multipart::mock::HttpBuffer")
.field("buf", &self.buf)
.field("boundary", &self.boundary)
.field("content_len", &self.content_len)
.finish()
}
}
pub struct ServerRequest<'a> {
pub data: &'a [u8],
pub boundary: &'a str,
pub content_len: Option<u64>,
rng: ThreadRng,
}
impl<'a> ServerRequest<'a> {
pub fn new(data: &'a [u8], boundary: &'a str) -> Self {
ServerRequest {
data: data,
boundary: boundary,
content_len: None,
rng: rand::thread_rng(),
}
}
}
impl<'a> Read for ServerRequest<'a> {
fn read(&mut self, out: &mut [u8]) -> io::Result<usize> {
if out.is_empty() {
debug!("ServerRequest::read() was passed a zero-sized buffer.");
return Ok(0);
}
let len = self.rng.gen_range(1, out.len() + 1);
self.data.read(&mut out[..len])
}
}
#[cfg(feature = "server")]
impl<'a> ::server::HttpRequest for ServerRequest<'a> {
type Body = Self;
fn multipart_boundary(&self) -> Option<&str> { Some(self.boundary) }
fn body(self) -> Self::Body {
self
}
}
pub struct StdoutTee<'s, W> {
inner: W,
stdout: io::StdoutLock<'s>,
}
impl<'s, W> StdoutTee<'s, W> {
pub fn new(inner: W, stdout: &'s io::Stdout) -> Self {
Self {
inner, stdout: stdout.lock(),
}
}
}
impl<'s, W: Write> Write for StdoutTee<'s, W> {
fn write(&mut self, buf: &[u8]) -> io::Result<usize> {
self.inner.write(buf)?;
self.stdout.write(buf)
}
fn flush(&mut self) -> io::Result<()> {
self.inner.flush();
self.stdout.flush()
}
}
pub fn log_on_panic() -> PanicLogger {
::log::set_max_level(::log::LevelFilter::Trace);
if ::log::logger() as *const ::log::Log != &LOGGER as *const ::log::Log {
::log::set_logger(&LOGGER).expect("failed to set logger for `log_on_panic()`");
}
LOG_ENABLED.with(|flag| flag.set(true));
PanicLogger(())
}
#[must_use]
pub struct PanicLogger(());
impl PanicLogger {
pub fn clear(self) {
drop(self);
}
}
impl Drop for PanicLogger {
fn drop(&mut self) {
LOG_ENABLED.with(|flag| flag.set(false));
LOG.with(|log| {
let mut log = log.borrow_mut();
if thread::panicking() {
println!("captured logs before panic:\n{}", *log);
}
log.clear();
});
}
}
static INIT_PANIC_LOGGER: Once = ONCE_INIT;
thread_local! {
static LOG: RefCell<String> = RefCell::new(String::new());
static LOG_ENABLED: Cell<bool> = Cell::new(false);
}
static LOGGER: ThreadLocalLogger = ThreadLocalLogger;
struct ThreadLocalLogger;
impl ::log::Log for ThreadLocalLogger {
fn enabled(&self, metadata: &Metadata) -> bool {
LOG_ENABLED.with(Cell::get)
}
fn log(&self, record: &Record) {
if self.enabled(record.metadata()) {
LOG.with(|log| {
let msg = format!("{}:{}: {}\n", record.level(), record.target(), record.args());
log.borrow_mut().push_str(&msg);
});
}
}
fn flush(&self) {}
}