Supporting /api/ping for dapps server (#1543)

* Refactoring dapps to support API endpoints.

* Using ContentHandler for unauthorized requests

* Extracting url stuff

* Adding ping endpoint

* CORS support for ping request

* Fixing url.is_none()

* minor formatting fix

[ci:skip]
This commit is contained in:
Tomasz Drwięga
2016-07-07 03:42:49 -04:00
committed by Gav Wood
parent 8282c7dd50
commit 7af366c5b1
11 changed files with 233 additions and 52 deletions

View File

@@ -17,22 +17,18 @@
//! Router implementation
//! Processes request handling authorization and dispatching it to proper application.
mod url;
pub mod auth;
use DAPPS_DOMAIN;
use std::sync::Arc;
use std::collections::HashMap;
use url::Host;
use hyper;
use hyper::{server, uri, header};
use hyper::{Next, Encoder, Decoder};
use url::{Url, Host};
use hyper::{self, server, Next, Encoder, Decoder};
use hyper::net::HttpStream;
use apps;
use endpoint::{Endpoint, Endpoints, EndpointPath};
use self::url::Url;
use handlers::{Redirection, extract_url};
use self::auth::{Authorization, Authorized};
use handlers::Redirection;
/// Special endpoints are accessible on every domain (every dapp)
#[derive(Debug, PartialEq, Hash, Eq)]
@@ -123,32 +119,6 @@ impl<A: Authorization> Router<A> {
}
}
fn extract_url(req: &server::Request<HttpStream>) -> Option<Url> {
match *req.uri() {
uri::RequestUri::AbsoluteUri(ref url) => {
match Url::from_generic_url(url.clone()) {
Ok(url) => Some(url),
_ => None,
}
},
uri::RequestUri::AbsolutePath(ref path) => {
// Attempt to prepend the Host header (mandatory in HTTP/1.1)
let url_string = match req.headers().get::<header::Host>() {
Some(ref host) => {
format!("http://{}:{}{}", host.hostname, host.port.unwrap_or(80), path)
},
None => return None,
};
match Url::parse(&url_string) {
Ok(url) => Some(url),
_ => None,
}
},
_ => None,
}
}
fn extract_endpoint(url: &Option<Url>) -> (Option<EndpointPath>, SpecialEndpoint) {
fn special_endpoint(url: &Url) -> SpecialEndpoint {
if url.path.len() <= 1 {