Skip to content
This repository has been archived by the owner on Nov 6, 2020. It is now read-only.

Commit

Permalink
Extracting URLHint to separate module
Browse files Browse the repository at this point in the history
  • Loading branch information
Tomasz Drwięga committed Aug 18, 2016
1 parent db3c965 commit 5c4f048
Show file tree
Hide file tree
Showing 4 changed files with 112 additions and 86 deletions.
90 changes: 5 additions & 85 deletions dapps/src/apps/fetcher.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,81 +20,18 @@ use std::io::{self, Read, Write};
use std::path::PathBuf;
use std::sync::Arc;
use std::collections::HashMap;
use rustc_serialize::hex::ToHex;

use hyper::Control;
use hyper::status::StatusCode;

use random_filename;
use util::{Address, FromHex, Mutex};
use apps::manifest::{MANIFEST_FILENAME, deserialize_manifest, serialize_manifest, Manifest};

use util::Mutex;
use page::LocalPageEndpoint;
use handlers::{ContentHandler, AppFetcherHandler, DappHandler};
use endpoint::{Endpoint, EndpointPath, Handler};
use apps::manifest::{MANIFEST_FILENAME, deserialize_manifest, serialize_manifest, Manifest};
use apps::urlhint::{URLHintContract, URLHint};

const COMMIT_LEN: usize = 20;

#[derive(Debug)]
pub struct GithubApp {
pub account: String,
pub repo: String,
pub commit: [u8;COMMIT_LEN],
pub owner: Address,
}

impl GithubApp {
pub fn url(&self) -> String {
// format!("https://github.com/{}/{}/archive/{}.zip", self.account, self.repo, self.commit.to_hex())
format!("http://github.todr.me/{}/{}/zip/{}", self.account, self.repo, self.commit.to_hex())
}

fn commit(bytes: &[u8]) -> Option<[u8;COMMIT_LEN]> {
if bytes.len() < COMMIT_LEN {
return None;
}

let mut commit = [0; COMMIT_LEN];
for i in 0..COMMIT_LEN {
commit[i] = bytes[i];
}

Some(commit)
}

}

pub trait URLHint {
fn resolve(&self, app_id: &str) -> Option<GithubApp>;
}

pub struct URLHintContract;

impl URLHint for URLHintContract {
fn resolve(&self, app_id: &str) -> Option<GithubApp> {
// TODO [todr] use GithubHint contract to check the details
// For now we are just accepting patterns: <commithash>.<repo>.<account>.parity
let mut app_parts = app_id.split('.');

let hash = app_parts.next()
.and_then(|h| h.from_hex().ok())
.and_then(|h| GithubApp::commit(&h));
let repo = app_parts.next();
let account = app_parts.next();

match (hash, repo, account) {
(Some(hash), Some(repo), Some(account)) => {
Some(GithubApp {
account: account.into(),
repo: repo.into(),
commit: hash,
owner: Address::default(),
})
},
_ => None,
}
}
}

enum AppStatus {
Fetching,
Expand Down Expand Up @@ -321,10 +258,10 @@ impl DappHandler for DappInstaller {
#[cfg(test)]
mod tests {
use std::path::PathBuf;
use super::{GithubApp, AppFetcher, AppStatus, URLHint};
use super::{AppFetcher, AppStatus};
use apps::urlhint::{GithubApp, URLHint};
use endpoint::EndpointInfo;
use page::LocalPageEndpoint;
use util::Address;

struct FakeResolver;
impl URLHint for FakeResolver {
Expand All @@ -333,23 +270,6 @@ mod tests {
}
}

#[test]
fn should_return_valid_url() {
// given
let app = GithubApp {
account: "test".into(),
repo: "xyz".into(),
commit: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
owner: Address::default(),
};

// when
let url = app.url();

// then
assert_eq!(url, "http://github.todr.me/test/xyz/zip/000102030405060708090a0b0c0d0e0f10111213".to_owned());
}

#[test]
fn should_true_if_contains_the_app() {
// given
Expand Down
1 change: 1 addition & 0 deletions dapps/src/apps/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@ use proxypac::ProxyPac;
use parity_dapps::WebApp;

mod fs;
pub mod urlhint;
pub mod fetcher;
pub mod manifest;

Expand Down
105 changes: 105 additions & 0 deletions dapps/src/apps/urlhint.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,105 @@
// Copyright 2015, 2016 Ethcore (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/>.

use rustc_serialize::hex::ToHex;

use util::{Address, FromHex};

const COMMIT_LEN: usize = 20;

#[derive(Debug)]
pub struct GithubApp {
pub account: String,
pub repo: String,
pub commit: [u8;COMMIT_LEN],
pub owner: Address,
}

impl GithubApp {
pub fn url(&self) -> String {
// format!("https://github.com/{}/{}/archive/{}.zip", self.account, self.repo, self.commit.to_hex())
format!("http://github.todr.me/{}/{}/zip/{}", self.account, self.repo, self.commit.to_hex())
}

fn commit(bytes: &[u8]) -> Option<[u8;COMMIT_LEN]> {
if bytes.len() < COMMIT_LEN {
return None;
}

let mut commit = [0; COMMIT_LEN];
for i in 0..COMMIT_LEN {
commit[i] = bytes[i];
}

Some(commit)
}

}

pub trait URLHint {
fn resolve(&self, app_id: &str) -> Option<GithubApp>;
}

pub struct URLHintContract;

impl URLHint for URLHintContract {
fn resolve(&self, app_id: &str) -> Option<GithubApp> {
// TODO [todr] use GithubHint contract to check the details
// For now we are just accepting patterns: <commithash>.<repo>.<account>.parity
let mut app_parts = app_id.split('.');

let hash = app_parts.next()
.and_then(|h| h.from_hex().ok())
.and_then(|h| GithubApp::commit(&h));
let repo = app_parts.next();
let account = app_parts.next();

match (hash, repo, account) {
(Some(hash), Some(repo), Some(account)) => {
Some(GithubApp {
account: account.into(),
repo: repo.into(),
commit: hash,
owner: Address::default(),
})
},
_ => None,
}
}
}

#[cfg(test)]
mod tests {
use super::GithubApp;
use util::Address;

#[test]
fn should_return_valid_url() {
// given
let app = GithubApp {
account: "test".into(),
repo: "xyz".into(),
commit: [0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19],
owner: Address::default(),
};

// when
let url = app.url();

// then
assert_eq!(url, "http://github.todr.me/test/xyz/zip/000102030405060708090a0b0c0d0e0f10111213".to_owned());
}
}
2 changes: 1 addition & 1 deletion dapps/src/handlers/fetch.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,7 +27,7 @@ use hyper::status::StatusCode;
use handlers::ContentHandler;
use handlers::client::{Fetch, FetchResult};
use apps::DAPPS_DOMAIN;
use apps::fetcher::GithubApp;
use apps::urlhint::GithubApp;
use apps::manifest::Manifest;

const FETCH_TIMEOUT: u64 = 30;
Expand Down

0 comments on commit 5c4f048

Please sign in to comment.