Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cache support progress #1

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
47 changes: 44 additions & 3 deletions src/ffi.rs
Original file line number Diff line number Diff line change
Expand Up @@ -232,6 +232,13 @@ pub const NONFATAL_UNKNOWN: c_int = 1024;
pub const NONFATAL_ALL: c_int = NONFATAL_OVERRIDE | NONFATAL_UNKNOWN;
pub const OR_ALL: c_int = OR_LIMIT | OR_OPTIONS | OR_FILEINFO | OR_AUTHCFG | OR_INDEXES;

/** The provider group used to register socache providers. */
pub const SOCACHE_PROVIDER_GROUP: &'static str = "socache";
/** The provider version used to register socache providers. */
pub const SOCACHE_PROVIDER_VERSION: &'static str = "0";
/** Default provider name. */
pub const SOCACHE_DEFAULT_PROVIDER: &'static str = "default";

#[repr(C)]
#[derive(Copy, Clone)]
pub struct request_rec {
Expand Down Expand Up @@ -481,6 +488,29 @@ pub struct ap_list_provider_groups_t {
pub provider_version: *const c_char,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ap_socache_provider_t {
pub name: *const c_char,
pub flags: c_uint,
pub create: Option<socache_provider_create_fn>,
pub init: Option<socache_provider_init_fn>,
pub destroy: Option<socache_provider_destroy_fn>,
pub store: Option<socache_provider_store_fn>,
pub retrieve: Option<socache_provider_retrieve_fn>,
pub remove: Option<socache_provider_remove_fn>,
pub status: Option<socache_provider_status_fn>,
pub iterate: Option<socache_provider_iterate_fn>,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ap_socache_hints {
pub avg_id_len: apr_size_t,
pub avg_obj_size: apr_size_t,
pub expiry_interval: apr_interval_time_t,
}

#[repr(C)]
#[derive(Copy, Clone)]
pub struct ap_method_list_t;
Expand Down Expand Up @@ -515,13 +545,14 @@ pub enum ap_filter_t { }
#[derive(Copy, Clone)]
pub enum ap_conf_vector_t { }

#[derive(Copy, Clone)]
pub enum ap_socache_instance_t { }

pub type cmd_how = c_uint;

pub type ap_conn_keepalive_e = c_uint;

pub type rewrite_args_fn = extern "C" fn(
process: *mut process_rec
);
pub type rewrite_args_fn = extern "C" fn(process: *mut process_rec);

pub type create_dir_config_fn = extern "C" fn(p: *mut apr_pool_t, dir: *mut c_char) -> *mut c_void;
pub type merge_config_fn = extern "C" fn(p: *mut apr_pool_t, base_conf: *mut c_void, new_conf: *mut c_void) -> *mut c_void;
Expand All @@ -543,6 +574,16 @@ pub type hook_check_config_fn = extern "C" fn(conf: *mut apr_pool_t, log: *mut a
pub type hook_test_config_fn = extern "C" fn(conf: *mut apr_pool_t, s: *mut server_rec) -> c_int;
pub type hook_post_config_fn = extern "C" fn(conf: *mut apr_pool_t, log: *mut apr_pool_t, temp: *mut apr_pool_t, s: *mut server_rec) -> c_int;

pub type socache_provider_create_fn = extern "C" fn(instance: *mut *mut ap_socache_instance_t, arg: *const c_char, tmp: *mut apr_pool_t, p: *mut apr_pool_t) -> *const c_char;
pub type socache_provider_init_fn = extern "C" fn(instance: *mut ap_socache_instance_t, cname: *const c_char, hints: *const ap_socache_hints, s: *mut server_rec, pool: *mut apr_pool_t) -> apr_status_t;
pub type socache_provider_destroy_fn = extern "C" fn(instance: *mut ap_socache_instance_t, s: *mut server_rec) -> ();
pub type socache_provider_store_fn = extern "C" fn(instance: *mut ap_socache_instance_t, s: *mut server_rec, id: *const c_uchar, idlen: c_uint, expiry: apr_time_t, data: *mut c_uchar, datalen: c_uint, pool: *mut apr_pool_t) -> apr_status_t;
pub type socache_provider_retrieve_fn = extern "C" fn(instance: *mut ap_socache_instance_t, s: *mut server_rec, id: *const c_uchar, idlen: c_uint, data: *mut c_uchar, datalen: *mut c_uint, pool: *mut apr_pool_t) -> apr_status_t;
pub type socache_provider_remove_fn = extern "C" fn(instance: *mut ap_socache_instance_t, s: *mut server_rec, id: *const c_uchar, idlen: c_uint, pool: *mut apr_pool_t) -> apr_status_t;
pub type socache_provider_status_fn = extern "C" fn(instance: *mut ap_socache_instance_t, r: *mut request_rec, flags: c_int) -> ();
pub type socache_provider_iterate_fn = extern "C" fn(instance: *mut ap_socache_instance_t, s: *mut server_rec, userctx: *mut c_void, iterator: *mut ap_socache_iterator_t, pool: *mut apr_pool_t) -> apr_status_t;

pub type ap_socache_iterator_t = extern "C" fn(instance: *mut ap_socache_instance_t, s: *mut server_rec, userctx: *mut c_void, id: *const c_uchar, idlen: c_uint, data: *const c_uchar, datalen: c_uint, pool: *mut apr_pool_t) -> c_int;

extern "C" {
pub fn ap_get_server_banner() -> *const c_char;
Expand Down
85 changes: 82 additions & 3 deletions src/httpd.rs
Original file line number Diff line number Diff line change
@@ -1,15 +1,15 @@
#![allow(non_camel_case_types)]
#![allow(unused_unsafe)]

use libc::{c_void, c_int, c_char};
use libc::{c_void, c_int, c_uint, c_char, c_uchar, strlen};

use std::{fmt, ptr};
use std::ffi::CString;
use std::marker::PhantomData;

use ffi;

use wrapper::{Wrapper, from_char_ptr, FromRaw};
use wrapper::{Wrapper, from_char_ptr, FromRaw, WrappedType};

use apr::{Table, Pool, ArrayHeaderIter};
use cookie::Cookie;
Expand Down Expand Up @@ -701,6 +701,85 @@ impl ListProviderName {
}


pub type SOCacheInstance = Wrapper<ffi::ap_socache_instance_t>;


pub type SOCacheProvider = Wrapper<ffi::ap_socache_provider_t>;


impl SOCacheProvider {
str_getter!(name);

pub fn create<'a, T: Into<Vec<u8>>>(&mut self, arg: Option<T>, tmp: &mut Pool, p: &mut Pool) -> Result<SOCacheInstance, &'a str> {
let arg: *const c_char = match arg {
None => ptr::null(),
Some(s) => ffi::strdup(tmp.ptr, s)
};

let mut instance: *mut ffi::ap_socache_instance_t = ptr::null_mut();

match from_char_ptr(
field!(self, create).unwrap()(&mut instance, arg, tmp.ptr, p.ptr)
) {
None => SOCacheInstance::from_raw(instance).ok_or(""),
Some(res) => Err(res)
}
}

pub fn init<'a, T: Into<Vec<u8>>>(&mut self, instance: &mut SOCacheInstance, cname: T, s: &Server, pool: &mut Pool) {
let cname = ffi::strdup(pool.ptr, cname);

unsafe {
field!(self, init).unwrap()(instance.ptr, cname, ptr::null(), s.ptr, pool.ptr);
}

//extern "C" fn(instance: *mut ap_socache_instance_t, cname: *const c_char, hints: *const ap_socache_hints, s: *mut server_rec, pool: *mut apr_pool_t) -> apr_status_t;
}

pub fn store<'a, T: Into<Vec<u8>>>(&mut self, instance: &mut SOCacheInstance, s: &Server, id: T, data: T, pool: &mut Pool) {
let id = ffi::strdup(pool.ptr, id);
let data = ffi::strdup(pool.ptr, data);

unsafe {
field!(self, store).unwrap()(instance.ptr, s.ptr, id as *const c_uchar, strlen(id) as c_uint, ffi::apr_time_now() + 10000000000, data as *mut c_uchar, (strlen(data) + 1) as c_uint, pool.ptr);
}

//extern "C" fn(instance: *mut ap_socache_instance_t, s: *mut server_rec, id: *const c_uchar, idlen: c_uint, expiry: apr_time_t, data: *mut c_uchar, datalen: c_uint, pool: *mut apr_pool_t) -> apr_status_t;

}

pub fn retrieve<'a, T: Into<Vec<u8>>>(&mut self, instance: &mut SOCacheInstance, s: &Server, id: T, pool: &mut Pool) -> Option<&'a str> {
let id = ffi::strdup(pool.ptr, id);

let mut datalen: c_uint = 100;

let data: *mut c_char = unsafe {
ffi::apr_palloc(pool.ptr, datalen as ffi::apr_size_t) as *mut c_char
};

unsafe {
field!(self, retrieve).unwrap()(instance.ptr, s.ptr, id as *const c_uchar, strlen(id) as c_uint, data as *mut c_uchar, &mut datalen, pool.ptr);
}

from_char_ptr(data)
//pub type socache_provider_retrieve_fn = extern "C" fn(instance: *mut ap_socache_instance_t, s: *mut server_rec, id: *const c_uchar, idlen: c_uint, data: *mut c_uchar, datalen: *mut c_uint, pool: *mut apr_pool_t) -> apr_status_t;
}

}


pub fn lookup_provider<P: Copy + WrappedType + FromRaw<*mut <P as WrappedType>::wrapped_type>, T: Into<Vec<u8>>, U: Into<Vec<u8>>, V: Into<Vec<u8>>>(pool: &mut Pool, provider_group: T, provider_name: U, provider_version: V) -> Option<P> {
let provider_group = ffi::strdup(pool.ptr, provider_group);
let provider_name = ffi::strdup(pool.ptr, provider_name);
let provider_version = ffi::strdup(pool.ptr, provider_version);

P::from_raw(
unsafe {
ffi::ap_lookup_provider(provider_group, provider_name, provider_version)
} as *mut <P as WrappedType>::wrapped_type
)
}

pub fn list_provider_groups(pool: &mut Pool) -> ArrayHeaderIter<ListProviderGroup> {
let ptr = unsafe { ffi::ap_list_provider_groups(pool.ptr) };

Expand All @@ -711,7 +790,7 @@ pub fn list_provider_groups(pool: &mut Pool) -> ArrayHeaderIter<ListProviderGrou
}
}

pub fn list_provider_names<T: Into<Vec<u8>>>(pool: &mut Pool, provider_group: T, provider_version: T) -> ArrayHeaderIter<ListProviderName> {
pub fn list_provider_names<T: Into<Vec<u8>>, U: Into<Vec<u8>>>(pool: &mut Pool, provider_group: T, provider_version: U) -> ArrayHeaderIter<ListProviderName> {
let provider_group = ffi::strdup(pool.ptr, provider_group);
let provider_version = ffi::strdup(pool.ptr, provider_version);

Expand Down
5 changes: 3 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,8 @@ pub mod cookie;
pub use libc::{c_void, c_char, c_int};

pub use httpd::{Request, Status, ProxyReq, CmdParms, Server, server_banner, server_description,
server_built, show_mpm, ConfVector, list_provider_groups, list_provider_names};
server_built, show_mpm, ConfVector, lookup_provider, list_provider_groups, list_provider_names,
SOCacheProvider, SOCacheInstance};

pub use apr::{apr_version_string, apu_version_string, HookOrder, Pool, time_now};

Expand All @@ -23,7 +24,7 @@ pub use wrapper::{WrappedType, from_char_ptr, FromRaw};
pub use ffi::{OR_NONE, OR_LIMIT, OR_OPTIONS, OR_FILEINFO, OR_AUTHCFG, OR_INDEXES, OR_UNSET,
ACCESS_CONF, RSRC_CONF, EXEC_ON_READ, NONFATAL_OVERRIDE, NONFATAL_UNKNOWN, NONFATAL_ALL, OR_ALL,
RAW_ARGS, TAKE1, TAKE2, ITERATE, ITERATE2, FLAG, NO_ARGS, TAKE12, TAKE3, TAKE23, TAKE123,
TAKE13, TAKE_ARGV};
TAKE13, TAKE_ARGV, SOCACHE_PROVIDER_GROUP, SOCACHE_PROVIDER_VERSION, SOCACHE_DEFAULT_PROVIDER};


pub type StringType<'a> = &'a str;
Expand Down