Skip to content

Commit

Permalink
iron/hyper header hack, revert once Hyper updates to 0.9.x
Browse files Browse the repository at this point in the history
Signed-off-by: Dave Parfitt <[email protected]>

Pull request: #526
Approved by: fnichol
  • Loading branch information
Dave Parfitt authored and jtimberman committed Jun 12, 2016
1 parent ae76ec3 commit b7d000f
Show file tree
Hide file tree
Showing 3 changed files with 33 additions and 25 deletions.
1 change: 0 additions & 1 deletion components/depot-core/src/data_object.rs
Original file line number Diff line number Diff line change
Expand Up @@ -248,4 +248,3 @@ impl AsRef<OriginKeyIdent> for OriginKeyIdent {
self
}
}

11 changes: 2 additions & 9 deletions components/depot-core/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
// the Software until such time that the Software is made available under an
// open source license such as the Apache 2.0 License.


extern crate habitat_core as hcore;
#[macro_use]
extern crate hyper;
Expand All @@ -13,15 +14,7 @@ extern crate rustc_serialize;

pub mod data_object;

use hyper::header::{Headers, ContentDisposition, DispositionType, DispositionParam, Charset};

header! { (XFileName, "X-Filename") => [String] }
header! { (ETag, "ETag") => [String] }

/// convenience function for setting Content-Disposition
pub fn set_disposition(headers: &mut Headers, filename: String, charset: Charset) -> () {
headers.set(ContentDisposition {
disposition: DispositionType::Attachment,
parameters: vec![DispositionParam::Filename( charset, None, filename.into_bytes())],
});
}

46 changes: 31 additions & 15 deletions components/depot/src/server.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ use std::io::{Read, Write, BufWriter};
use std::path::PathBuf;

use dbcache;
use depot_core::{ETag, XFileName, set_disposition};
use depot_core::data_object::{self, DataObject};
use iron::prelude::*;
use iron::{status, headers, AfterMiddleware};
Expand All @@ -20,6 +19,7 @@ use router::{Params, Router};
use rustc_serialize::json;
use urlencoded::UrlEncodedQuery;


use super::Depot;
use config::Config;
use error::{Error, Result};
Expand Down Expand Up @@ -188,13 +188,17 @@ fn download_origin_key(depot: &Depot, req: &mut Request) -> IronResult<Response>

let xfilename = origin_keyfile.file_name().unwrap().to_string_lossy().into_owned();
let mut response = Response::with((status::Ok, origin_keyfile));
response.headers.set(XFileName(xfilename.clone()));
set_disposition(&mut response.headers,
xfilename,
headers::Charset::Ext("utf-8".to_string()));
// use set_raw because we're having problems with Iron's Hyper 0.8.x
// and the newer Hyper 0.9.4. TODO: change back to set() once
// Iron updates to Hyper 0.9.x.
response.headers.set_raw("X-Filename", vec![xfilename.clone().into_bytes()]);
response.headers.set_raw("content-disposition",
vec![format!("attachment; filename=\"{}\"", xfilename.clone())
.into_bytes()]);
Ok(response)
}


fn download_latest_origin_key(depot: &Depot, req: &mut Request) -> IronResult<Response> {
debug!("Download latest origin key {:?}", req);
let params = req.extensions.get::<Router>().unwrap();
Expand Down Expand Up @@ -223,10 +227,13 @@ fn download_latest_origin_key(depot: &Depot, req: &mut Request) -> IronResult<Re

let xfilename = origin_keyfile.file_name().unwrap().to_string_lossy().into_owned();
let mut response = Response::with((status::Ok, origin_keyfile));
response.headers.set(XFileName(xfilename.clone()));
set_disposition(&mut response.headers,
xfilename,
headers::Charset::Ext("utf-8".to_string()));
// use set_raw because we're having problems with Iron's Hyper 0.8.x
// and the newer Hyper 0.9.4. TODO: change back to set() once
// Iron updates to Hyper 0.9.x.
response.headers.set_raw("X-Filename", vec![xfilename.clone().into_bytes()]);
response.headers.set_raw("content-disposition",
vec![format!("attachment; filename=\"{}\"", xfilename.clone())
.into_bytes()]);
Ok(response)
}

Expand All @@ -241,11 +248,16 @@ fn download_package(depot: &Depot, req: &mut Request) -> IronResult<Response> {
match fs::metadata(&archive.path) {
Ok(_) => {
let mut response = Response::with((status::Ok, archive.path.clone()));
response.headers.set(XFileName(archive.file_name()));

set_disposition(&mut response.headers,
archive.file_name(),
headers::Charset::Ext("utf-8".to_string()));
// use set_raw because we're having problems with Iron's Hyper 0.8.x
// and the newer Hyper 0.9.4. TODO: change back to set() once
// Iron updates to Hyper 0.9.x.

response.headers.set_raw("X-Filename",
vec![archive.file_name().clone().into_bytes()]);
response.headers.set_raw("content-disposition",
vec![format!("attachment; filename=\"{}\"",
archive.file_name().clone())
.into_bytes()]);
Ok(response)
}
Err(_) => Ok(Response::with(status::NotFound)),
Expand Down Expand Up @@ -409,10 +421,14 @@ fn show_package(depot: &Depot, req: &mut Request) -> IronResult<Response> {
}
}


fn render_package(pkg: &data_object::Package) -> IronResult<Response> {
let body = json::encode(pkg).unwrap();
let mut response = Response::with((status::Ok, body));
response.headers.set(ETag(pkg.checksum.clone()));
// use set_raw because we're having problems with Iron's Hyper 0.8.x
// and the newer Hyper 0.9.4. TODO: change back to set() once
// Iron updates to Hyper 0.9.x.
response.headers.set_raw("ETag", vec![pkg.checksum.clone().into_bytes()]);
Ok(response)
}

Expand Down

0 comments on commit b7d000f

Please sign in to comment.