support serving Access-Control-Allow-Origin header (#19)

support serving Access-Control-Allow-Origin header

Closes #17.
This commit is contained in:
Scott Lamb
2018-03-03 06:43:36 -08:00
committed by GitHub
parent 760b8d95fb
commit 672a327ee2
3 changed files with 30 additions and 6 deletions

View File

@@ -39,7 +39,7 @@ use futures::{future, stream};
use futures_cpupool;
use json;
use http_serve;
use hyper::header;
use hyper::header::{self, Header};
use hyper::server::{self, Request, Response};
use mime;
use mp4;
@@ -170,6 +170,7 @@ struct ServiceInner {
db: Arc<db::Database>,
dir: Arc<SampleFileDir>,
ui_files: HashMap<String, UiFile>,
allow_origin: Option<header::AccessControlAllowOrigin>,
pool: futures_cpupool::CpuPool,
time_zone_name: String,
}
@@ -383,17 +384,22 @@ impl ServiceInner {
pub struct Service(Arc<ServiceInner>);
impl Service {
pub fn new(db: Arc<db::Database>, dir: Arc<SampleFileDir>, ui_dir: Option<&str>, zone: String)
-> Result<Self, Error> {
pub fn new(db: Arc<db::Database>, dir: Arc<SampleFileDir>, ui_dir: Option<&str>,
allow_origin: Option<String>, zone: String) -> Result<Self, Error> {
let mut ui_files = HashMap::new();
if let Some(d) = ui_dir {
Service::fill_ui_files(d, &mut ui_files);
}
debug!("UI files: {:#?}", ui_files);
let allow_origin = match allow_origin {
None => None,
Some(o) => Some(header::AccessControlAllowOrigin::parse_header(&header::Raw::from(o))?),
};
Ok(Service(Arc::new(ServiceInner {
db,
dir,
ui_files,
allow_origin,
pool: futures_cpupool::Builder::new().pool_size(1).name_prefix("static").create(),
time_zone_name: zone,
})))
@@ -461,6 +467,11 @@ impl server::Service for Service {
Path::NotFound => self.0.not_found(),
Path::Static => self.0.static_file(&req),
};
let res = if let Some(ref o) = self.0.allow_origin {
res.map(|resp| resp.with_header(o.clone()))
} else {
res
};
future::result(res.map_err(|e| {
error!("error: {}", e);
hyper::Error::Incomplete
@@ -519,7 +530,8 @@ mod bench {
::std::thread::spawn(move || {
let addr = "127.0.0.1:0".parse().unwrap();
let (db, dir) = (db.db.clone(), db.dir.clone());
let service = super::Service::new(db.clone(), dir.clone(), None, "".to_owned()).unwrap();
let service = super::Service::new(db.clone(), dir.clone(), None, None,
"".to_owned()).unwrap();
let server = hyper::server::Http::new()
.bind(&addr, move || Ok(service.clone()))
.unwrap();