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 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285
use std; use std::collections::HashMap; use std::sync::{Arc, Mutex}; use std::ffi::{CStr, CString}; use std::os::raw::c_char; use std::ops::Deref; use std::rc::Rc; use std::cell::RefCell; use hyper; use ice_server; use session_storage; use glue::serialize; use glue::common; pub struct Request { pub uri: CString, pub remote_addr: CString, pub method: CString, pub url_params: HashMap<String, String>, pub headers: Rc<hyper::header::Headers>, pub cookies: HashMap<String, CString>, pub custom_properties: Arc<common::CustomProperties>, pub body: Box<Deref<Target = RefCell<Vec<u8>>>>, pub context: Arc<ice_server::Context>, pub session: Option<Arc<Mutex<session_storage::Session>>>, pub cache: RequestCache } #[derive(Default)] pub struct RequestCache { stats: Option<CString>, session_items: HashMap<String, CString>, headers: HashMap<String, CString>, url_params_raw: Option<Vec<u8>>, headers_raw: Option<Vec<u8>>, cookies_raw: Option<Vec<u8>>, session_items_raw: Option<Vec<u8>> } impl Request { pub fn into_boxed(self) -> Box<Request> { Box::new(self) } } #[no_mangle] pub unsafe fn ice_glue_request_get_stats(req: *mut Request) -> *const c_char { let req = &mut *req; req.cache.stats = Some(CString::new(req.context.stats.serialize().to_string()).unwrap()); let ret = req.cache.stats.as_ref().unwrap().as_ptr(); ret } #[no_mangle] pub unsafe fn ice_glue_request_get_uri(req: *mut Request) -> *const c_char { let req = &*req; let ret = req.uri.as_ptr(); ret } #[no_mangle] pub unsafe fn ice_glue_request_get_method(req: *mut Request) -> *const c_char { let req = &*req; let ret = req.method.as_ptr(); ret } #[no_mangle] pub unsafe fn ice_glue_request_get_remote_addr(req: *mut Request) -> *const c_char { let req = &*req; let ret = req.remote_addr.as_ptr(); ret } #[no_mangle] pub unsafe fn ice_glue_request_get_url_params(req: *mut Request) -> *const u8 { let req = &mut *req; if req.cache.url_params_raw.is_none() { req.cache.url_params_raw = Some( serialize::std_map(req.url_params.iter(), req.url_params.len()) ); } req.cache.url_params_raw.as_ref().unwrap().as_ptr() } #[no_mangle] pub unsafe fn ice_glue_request_set_custom_stat(req: *mut Request, k: *const c_char, v: *const c_char) { let req = &*req; let k = CStr::from_ptr(k).to_str().unwrap(); let v = CStr::from_ptr(v).to_str().unwrap(); req.context.stats.set_custom(k.to_string(), v.to_string()); } #[no_mangle] pub unsafe fn ice_glue_request_get_body(req: *mut Request, len_out: *mut u32) -> *const u8 { let req = &*req; let body = req.body.borrow(); let ret = body.as_slice().as_ptr(); *len_out = body.len() as u32; ret } #[no_mangle] pub unsafe fn ice_glue_request_get_header(req: *mut Request, k: *const c_char) -> *const c_char { let req = &mut *req; let k = CStr::from_ptr(k).to_str().unwrap(); let ret = match req.headers.get_raw(k) { Some(v) => match v.one() { Some(v) => match std::str::from_utf8(v) { Ok(v) => Some(CString::new(v).unwrap()), Err(_) => None }, None => None }, None => None }; let ret = match ret { Some(v) => { req.cache.headers.insert(k.to_string(), v); req.cache.headers.get(k).as_ref().unwrap().as_ptr() }, None => std::ptr::null() }; ret } #[no_mangle] pub unsafe fn ice_glue_request_get_headers(req: *mut Request) -> *const u8 { let req = &mut *req; if req.cache.headers_raw.is_none() { req.cache.headers_raw = Some( serialize::std_map(req.headers.iter().map(|v| { (v.name(), v.value_string()) }), req.headers.len()) ); } req.cache.headers_raw.as_ref().unwrap().as_ptr() } #[no_mangle] pub unsafe fn ice_glue_request_get_cookie(req: *mut Request, k: *const c_char) -> *const c_char { let req = &*req; let k = CStr::from_ptr(k).to_str().unwrap(); let ret = match req.cookies.get(k) { Some(ref v) => v.as_ptr(), None => std::ptr::null() }; ret } #[no_mangle] pub unsafe fn ice_glue_request_get_cookies(req: *mut Request) -> *const u8 { let req = &mut *req; if req.cache.cookies_raw.is_none() { req.cache.cookies_raw = Some( serialize::std_map(req.cookies.iter().map(|(k, v)| { (k, match std::str::from_utf8(v.as_bytes()) { Ok(v) => v, Err(_) => "" }) }), req.cookies.len()) ); } req.cache.cookies_raw.as_ref().unwrap().as_ptr() } #[no_mangle] pub unsafe fn ice_glue_request_get_session_item(req: *mut Request, k: *const c_char) -> *const c_char { let req = &mut *req; let k = CStr::from_ptr(k).to_str().unwrap(); let ret; { let v = match req.session { Some(ref session) => { match session.lock().unwrap().data.get(k) { Some(v) => { Some(CString::new(v.as_str()).unwrap()) }, None => None } }, None => None }; let mut session_items = &mut req.cache.session_items; ret = match v { Some(v) => { session_items.insert(k.to_string(), v); session_items.get(k).as_ref().unwrap().as_ptr() }, None => std::ptr::null() }; } ret } #[no_mangle] pub unsafe fn ice_glue_request_get_session_items(req: *mut Request) -> *const u8 { let req = &mut *req; match req.session { Some(ref session) => { let session = session.lock().unwrap(); req.cache.session_items_raw = Some( serialize::std_map(session.data.iter(), session.data.len()) ); req.cache.session_items_raw.as_ref().unwrap().as_ptr() }, None => std::ptr::null() } } #[no_mangle] pub unsafe fn ice_glue_request_set_session_item(req: *mut Request, k: *const c_char, value: *const c_char) { let req = &*req; let k = CStr::from_ptr(k).to_str().unwrap(); match req.session { Some(ref session) => { match value.is_null() { true => { session.lock().unwrap().data.remove(k); }, false => { let value = CStr::from_ptr(value).to_str().unwrap(); session.lock().unwrap().data.insert(k.to_string(), value.to_string()); } } }, None => {} } } #[no_mangle] pub unsafe fn ice_glue_request_render_template_to_owned(req: *mut Request, name: *const c_char, data: *const c_char) -> *mut c_char { let req = &*req; let ret = match req.context.templates.render_json( CStr::from_ptr(name).to_str().unwrap(), CStr::from_ptr(data).to_str().unwrap() ) { Some(v) => CString::new(v).unwrap().into_raw(), None => std::ptr::null_mut() }; ret } #[no_mangle] pub unsafe fn ice_glue_request_borrow_context(req: *mut Request) -> *const ice_server::Context { let req = &*req; &*req.context } #[no_mangle] pub unsafe fn ice_glue_request_borrow_custom_properties(req: *mut Request) -> *const common::CustomProperties { let req = &*req; &*req.custom_properties }