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
use std; use std::any::Any; use std::sync::Arc; use std::collections::HashMap; use std::ffi::{CStr, CString}; use std::os::raw::c_char; use hyper; use futures; use futures::future::Future; use glue; use ice_server; use streaming; use static_file; #[derive(Debug)] pub struct Response { pub body: Vec<u8>, pub file: Option<String>, pub status: u16, pub headers: hyper::header::Headers, pub cookies: HashMap<String, String>, pub stream_rx: Option<streaming::ChunkReceiver>, pub custom_properties: Option<Arc<glue::common::CustomProperties>> } impl Response { pub fn new() -> Response { Response { body: Vec::new(), file: None, status: 200, headers: hyper::header::Headers::new(), cookies: HashMap::new(), stream_rx: None, custom_properties: None } } pub fn into_boxed(self) -> Box<Response> { Box::new(self) } pub fn into_hyper_response(mut self, ctx: &ice_server::Context, local_ctx: &ice_server::LocalContext, req_headers: Option<&hyper::header::Headers>) -> Box<Future<Error = String, Item = hyper::Response>> { self.headers.set_raw("Server", "Ice Core"); self.headers.set_raw("X-Powered-By", "Ice Core"); let mut cookies_vec = Vec::new(); for (k, v) in self.cookies { cookies_vec.push(k + "=" + v.as_str()); } self.headers.set(hyper::header::SetCookie(cookies_vec)); let resp = hyper::server::Response::new() .with_headers(self.headers) .with_status(match hyper::StatusCode::try_from(self.status) { Ok(v) => v, Err(_) => hyper::StatusCode::InternalServerError }); match self.file { Some(p) => { let etag = match req_headers { Some(v) => match v.get::<hyper::header::IfNoneMatch>() { Some(v) => { match v { &hyper::header::IfNoneMatch::Any => None, &hyper::header::IfNoneMatch::Items(ref v) => { if v.len() == 0 { None } else { Some(v[0].tag().to_string()) } } } }, None => None }, None => None }; static_file::fetch_raw_unchecked(&ctx, &local_ctx, resp, p.as_str(), etag) }, None => { Box::new(futures::future::ok( match self.stream_rx { Some(rx) => { resp.with_body(rx) }, None => resp.with_header(hyper::header::ContentLength(self.body.len() as u64)).with_body(self.body) } )) } } } pub fn add_header(&mut self, k: &str, v: &str) { self.headers.set_raw(glue::header::transform_name(k), v.to_string()); } pub fn set_cookie(&mut self, k: &str, v: &str) { self.cookies.insert(k.to_string(), v.to_string()); } pub fn set_body(&mut self, data: &[u8]) { self.body = data.to_vec(); } pub fn set_file(&mut self, path: &str) { self.file = Some(path.to_string()); } pub fn set_status(&mut self, status: u16) { self.status = status; } pub fn stream(&mut self, ctx: &ice_server::Context) -> streaming::StreamProvider { if self.stream_rx.is_some() { panic!("Attempting to enable streaming for a response that has already enabled it"); } let (provider, rx) = streaming::StreamProvider::new(&ctx.ev_loop_remote); self.stream_rx = Some(rx); provider } } impl Into<Box<Any>> for Box<Response> { fn into(self) -> Box<Any> { self as Box<Any> } } #[no_mangle] pub fn ice_glue_create_response() -> *mut Response { Box::into_raw(Response::new().into_boxed()) } #[no_mangle] pub unsafe fn ice_glue_destroy_response(resp: *mut Response) { Box::from_raw(resp); } #[no_mangle] pub unsafe fn ice_glue_response_add_header(resp: *mut Response, k: *const c_char, v: *const c_char) { let resp = &mut *resp; resp.add_header(CStr::from_ptr(k).to_str().unwrap(), CStr::from_ptr(v).to_str().unwrap()); } #[no_mangle] pub unsafe fn ice_glue_response_set_cookie(resp: *mut Response, k: *const c_char, v: *const c_char) { let resp = &mut *resp; resp.set_cookie(CStr::from_ptr(k).to_str().unwrap(), CStr::from_ptr(v).to_str().unwrap()); } #[no_mangle] pub unsafe fn ice_glue_response_set_body(resp: *mut Response, data: *const u8, len: u32) { let resp = &mut *resp; if data.is_null() || len == 0 { resp.set_body(&[]); } else { resp.set_body(std::slice::from_raw_parts(data, len as usize)); } } #[no_mangle] pub unsafe fn ice_glue_response_set_file(resp: *mut Response, path: *const c_char) { let resp = &mut *resp; resp.set_file(CStr::from_ptr(path).to_str().unwrap()); } #[no_mangle] pub unsafe fn ice_glue_response_set_status(resp: *mut Response, status: u16) { let resp = &mut *resp; resp.set_status(status); } #[no_mangle] pub unsafe fn ice_glue_response_consume_rendered_template(resp: *mut Response, content: *mut c_char) { let resp = &mut *resp; let content = CString::from_raw(content); resp.set_body(content.as_bytes()); } #[no_mangle] pub unsafe fn ice_glue_response_stream(resp: *mut Response, ctx: *const ice_server::Context) -> *mut streaming::StreamProvider { let resp = &mut *resp; let ctx = &*ctx; Box::into_raw(resp.stream(ctx).into_boxed()) } #[no_mangle] pub unsafe fn ice_glue_response_borrow_custom_properties(resp: *mut Response) -> *const glue::common::CustomProperties { let resp = &*resp; match resp.custom_properties { Some(ref v) => &**v, None => std::ptr::null() } }