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
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
// Copyright 2015-2017 Parity Technologies (UK) Ltd.
// This file is part of Parity.

// Parity is free software: you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation, either version 3 of the License, or
// (at your option) any later version.

// Parity is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
// GNU General Public License for more details.

// You should have received a copy of the GNU General Public License
// along with Parity.  If not, see <http://www.gnu.org/licenses/>.

//! Hyper Server Handler that fetches a file during a request (proxy).

use std::fmt;
use std::sync::{mpsc, Arc};
use std::sync::atomic::{AtomicBool, Ordering};
use std::time::{Instant, Duration};
use fetch::{self, Fetch};
use futures::Future;
use parity_reactor::Remote;
use util::Mutex;

use hyper::{server, Decoder, Encoder, Next, Method, Control};
use hyper::net::HttpStream;
use hyper::uri::RequestUri;
use hyper::status::StatusCode;

use endpoint::EndpointPath;
use handlers::{ContentHandler, StreamingHandler};
use page::{LocalPageEndpoint, PageHandlerWaiting};

const FETCH_TIMEOUT: u64 = 300;

pub enum ValidatorResponse {
	Local(LocalPageEndpoint),
	Streaming(StreamingHandler<fetch::Response>),
}

pub trait ContentValidator: Send + 'static {
	type Error: fmt::Debug + fmt::Display;

	fn validate_and_install(&self, fetch::Response) -> Result<ValidatorResponse, Self::Error>;
}

enum FetchState {
	Waiting,
	NotStarted(String),
	Error(ContentHandler),
	InProgress(mpsc::Receiver<FetchState>),
	Streaming(StreamingHandler<fetch::Response>),
	Done(LocalPageEndpoint, Box<PageHandlerWaiting>),
}

enum WaitResult {
	Error(ContentHandler),
	Done(LocalPageEndpoint),
	NonAwaitable,
}

#[derive(Clone)]
pub struct FetchControl {
	abort: Arc<AtomicBool>,
	listeners: Arc<Mutex<Vec<(Control, mpsc::Sender<WaitResult>)>>>,
	deadline: Instant,
}

impl Default for FetchControl {
	fn default() -> Self {
		FetchControl {
			abort: Arc::new(AtomicBool::new(false)),
			listeners: Arc::new(Mutex::new(Vec::new())),
			deadline: Instant::now() + Duration::from_secs(FETCH_TIMEOUT),
		}
	}
}

impl FetchControl {
	fn notify<F: Fn() -> WaitResult>(&self, status: F) {
		let mut listeners = self.listeners.lock();
		for (control, sender) in listeners.drain(..) {
			trace!(target: "dapps", "Resuming request waiting for content...");
			if let Err(e) = sender.send(status()) {
				trace!(target: "dapps", "Waiting listener notification failed: {:?}", e);
			} else {
				let _ = control.ready(Next::read());
			}
		}
	}

	fn set_status(&self, status: &FetchState) {
		match *status {
			FetchState::Error(ref handler) => self.notify(|| WaitResult::Error(handler.clone())),
			FetchState::Done(ref endpoint, _) => self.notify(|| WaitResult::Done(endpoint.clone())),
			FetchState::Streaming(_) => self.notify(|| WaitResult::NonAwaitable),
			FetchState::NotStarted(_) | FetchState::InProgress(_) | FetchState::Waiting => {},
		}
	}

	pub fn is_deadline_reached(&self) -> bool {
		self.deadline < Instant::now()
	}

	pub fn abort(&self) {
		self.abort.store(true, Ordering::SeqCst);
	}

	pub fn to_async_handler(&self, path: EndpointPath, control: Control) -> Box<server::Handler<HttpStream> + Send> {
		let (tx, rx) = mpsc::channel();
		self.listeners.lock().push((control, tx));

		Box::new(WaitingHandler {
			receiver: rx,
			state: FetchState::Waiting,
			uri: RequestUri::default(),
			path: path,
		})
	}
}

pub struct WaitingHandler {
	receiver: mpsc::Receiver<WaitResult>,
	state: FetchState,
	uri: RequestUri,
	path: EndpointPath,
}

impl server::Handler<HttpStream> for WaitingHandler {
	fn on_request(&mut self, request: server::Request<HttpStream>) -> Next {
		self.uri = request.uri().clone();
		Next::wait()
	}

	fn on_request_readable(&mut self, decoder: &mut Decoder<HttpStream>) -> Next {
		let result = self.receiver.try_recv().ok();
		self.state = match result {
			Some(WaitResult::Error(handler)) => FetchState::Error(handler),
			Some(WaitResult::Done(endpoint)) => {
				let mut page_handler = endpoint.to_page_handler(self.path.clone());
				page_handler.set_uri(&self.uri);
				FetchState::Done(endpoint, page_handler)
			},
			_ => {
				warn!("A result for waiting request was not received.");
				FetchState::Waiting
			},
		};

		match self.state {
			FetchState::Done(_, ref mut handler) => handler.on_request_readable(decoder),
			FetchState::Streaming(ref mut handler) => handler.on_request_readable(decoder),
			FetchState::Error(ref mut handler) => handler.on_request_readable(decoder),
			_ => Next::write(),
		}
	}

	fn on_response(&mut self, res: &mut server::Response) -> Next {
		match self.state {
			FetchState::Done(_, ref mut handler) => handler.on_response(res),
			FetchState::Streaming(ref mut handler) => handler.on_response(res),
			FetchState::Error(ref mut handler) => handler.on_response(res),
			_ => Next::end(),
		}
	}

	fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
		match self.state {
			FetchState::Done(_, ref mut handler) => handler.on_response_writable(encoder),
			FetchState::Streaming(ref mut handler) => handler.on_response_writable(encoder),
			FetchState::Error(ref mut handler) => handler.on_response_writable(encoder),
			_ => Next::end(),
		}
	}
}

#[derive(Clone)]
struct Errors {
	embeddable_on: Option<(String, u16)>,
}

impl Errors {
	fn download_error<E: fmt::Debug>(&self, e: E) -> ContentHandler {
		ContentHandler::error(
			StatusCode::BadGateway,
			"Download Error",
			"There was an error when fetching the content.",
			Some(&format!("{:?}", e)),
			self.embeddable_on.clone(),
		)
	}

	fn invalid_content<E: fmt::Debug>(&self, e: E) -> ContentHandler {
		ContentHandler::error(
			StatusCode::BadGateway,
			"Invalid Dapp",
			"Downloaded bundle does not contain a valid content.",
			Some(&format!("{:?}", e)),
			self.embeddable_on.clone(),
		)
	}

	fn timeout_error(&self) -> ContentHandler {
		ContentHandler::error(
			StatusCode::GatewayTimeout,
			"Download Timeout",
			&format!("Could not fetch content within {} seconds.", FETCH_TIMEOUT),
			None,
			self.embeddable_on.clone(),
		)
	}

	fn method_not_allowed(&self) -> ContentHandler {
		ContentHandler::error(
			StatusCode::MethodNotAllowed,
			"Method Not Allowed",
			"Only <code>GET</code> requests are allowed.",
			None,
			self.embeddable_on.clone(),
		)
	}
}

pub struct ContentFetcherHandler<H: ContentValidator, F: Fetch> {
	fetch_control: FetchControl,
	control: Control,
	remote: Remote,
	status: FetchState,
	fetch: F,
	installer: Option<H>,
	path: EndpointPath,
	errors: Errors,
}

impl<H: ContentValidator, F: Fetch> ContentFetcherHandler<H, F> {
	pub fn new(
		url: String,
		path: EndpointPath,
		control: Control,
		installer: H,
		embeddable_on: Option<(String, u16)>,
		remote: Remote,
		fetch: F,
	) -> Self {
		ContentFetcherHandler {
			fetch_control: FetchControl::default(),
			control: control,
			remote: remote,
			fetch: fetch,
			status: FetchState::NotStarted(url),
			installer: Some(installer),
			path: path,
			errors: Errors {
				embeddable_on: embeddable_on,
			},
		}
	}

	pub fn fetch_control(&self) -> FetchControl {
		self.fetch_control.clone()
	}

	fn fetch_content(&self, uri: RequestUri, url: &str, installer: H) -> mpsc::Receiver<FetchState> {
		let (tx, rx) = mpsc::channel();
		let abort = self.fetch_control.abort.clone();

		let path = self.path.clone();
		let tx2 = tx.clone();
		let control = self.control.clone();
		let errors = self.errors.clone();

		let future = self.fetch.fetch_with_abort(url, abort.into()).then(move |result| {
			trace!(target: "dapps", "Fetching content finished. Starting validation: {:?}", result);
			let new_state = match result {
				Ok(response) => match installer.validate_and_install(response) {
					Ok(ValidatorResponse::Local(endpoint)) => {
						trace!(target: "dapps", "Validation OK. Returning response.");
						let mut handler = endpoint.to_page_handler(path);
						handler.set_uri(&uri);
						FetchState::Done(endpoint, handler)
					},
					Ok(ValidatorResponse::Streaming(handler)) => {
						trace!(target: "dapps", "Validation OK. Streaming response.");
						FetchState::Streaming(handler)
					},
					Err(e) => {
						trace!(target: "dapps", "Error while validating content: {:?}", e);
						FetchState::Error(errors.invalid_content(e))
					},
				},
				Err(e) => {
					warn!(target: "dapps", "Unable to fetch content: {:?}", e);
					FetchState::Error(errors.download_error(e))
				},
			};
			// Content may be resolved when the connection is already dropped.
			let _ = tx2.send(new_state);
			// Ignoring control errors
			let _ = control.ready(Next::read());
			Ok(()) as Result<(), ()>
		});

		// make sure to run within fetch thread pool.
		let future = self.fetch.process(future);
		// spawn to event loop
		let control = self.control.clone();
		let errors = self.errors.clone();
		self.remote.spawn_with_timeout(|| future, Duration::from_secs(FETCH_TIMEOUT), move || {
			// Notify about the timeout
			let _ = tx.send(FetchState::Error(errors.timeout_error()));
			// Ignoring control errors
			let _ = control.ready(Next::read());
		});

		rx
	}
}

impl<H: ContentValidator, F: Fetch> server::Handler<HttpStream> for ContentFetcherHandler<H, F> {
	fn on_request(&mut self, request: server::Request<HttpStream>) -> Next {
		let status = if let FetchState::NotStarted(ref url) = self.status {
			let uri = request.uri().clone();
			let installer = self.installer.take().expect("Installer always set initialy; installer used only in on_request; on_request invoked only once; qed");

			Some(match *request.method() {
				// Start fetching content
				Method::Get => {
					trace!(target: "dapps", "Fetching content from: {:?}", url);
					let receiver = self.fetch_content(uri, url, installer);
					FetchState::InProgress(receiver)
				},
				// or return error
				_ => FetchState::Error(self.errors.method_not_allowed()),
			})
		} else { None };

		if let Some(status) = status {
			self.fetch_control.set_status(&status);
			self.status = status;
		}

		Next::read()
	}

	fn on_request_readable(&mut self, decoder: &mut Decoder<HttpStream>) -> Next {
		let (status, next) = match self.status {
			// Request may time out
			FetchState::InProgress(_) if self.fetch_control.is_deadline_reached() => {
				trace!(target: "dapps", "Fetching dapp failed because of timeout.");
				(Some(FetchState::Error(self.errors.timeout_error())), Next::write())
			},
			FetchState::InProgress(ref receiver) => {
				// Check if there is an answer
				let rec = receiver.try_recv();
				match rec {
					// just return the new state
					Ok(state) => (Some(state), Next::write()),
					// wait some more
					_ => (None, Next::wait())
				}
			},
			FetchState::Error(ref mut handler) => (None, handler.on_request_readable(decoder)),
			_ => (None, Next::write()),
		};

		if let Some(status) = status {
			self.fetch_control.set_status(&status);
			self.status = status;
		}

		next
	}

	fn on_response(&mut self, res: &mut server::Response) -> Next {
		match self.status {
			FetchState::Done(_, ref mut handler) => handler.on_response(res),
			FetchState::Streaming(ref mut handler) => handler.on_response(res),
			FetchState::Error(ref mut handler) => handler.on_response(res),
			_ => Next::end(),
		}
	}

	fn on_response_writable(&mut self, encoder: &mut Encoder<HttpStream>) -> Next {
		match self.status {
			FetchState::Done(_, ref mut handler) => handler.on_response_writable(encoder),
			FetchState::Streaming(ref mut handler) => handler.on_response_writable(encoder),
			FetchState::Error(ref mut handler) => handler.on_response_writable(encoder),
			_ => Next::end(),
		}
	}
}