use bytes::Bytes;
use crate::ffi::errors::{ErrorHandle, Status};
use crate::ffi::Slice;
use crate::cookies::SetCookie;
use crate::models::{Body, Message, Version};
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_with_body(status_code: u16, version: i32, body: *const u8, body_len: usize) -> *mut Message {
let mut response = Message::response(status_code, Version::of(version));
if let Some(body) = unsafe { Slice::borrow(body, body_len) } {
response.body = Some(Body::Data(Bytes::copy_from_slice(body)));
}
Box::into_raw(Box::new(response))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_content(content_type: *const u8, content_type_len: usize, body: *const u8, body_len: usize, version: i32) -> *mut Message {
let Some(content_type) = (unsafe { Slice::borrow_text(content_type, content_type_len) }) else {
return std::ptr::null_mut();
};
let body = Body::Data(Bytes::copy_from_slice(unsafe { Slice::borrow(body, body_len) }.unwrap_or_default()));
Box::into_raw(Box::new(Message::content(content_type, body, Version::of(version))))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_text(content: *const u8, content_len: usize, version: i32) -> *mut Message {
match unsafe { Slice::borrow_text(content, content_len) } {
Some(content) => Box::into_raw(Box::new(Message::text(content, Version::of(version)))),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_html(content: *const u8, content_len: usize, version: i32) -> *mut Message {
match unsafe { Slice::borrow_text(content, content_len) } {
Some(content) => Box::into_raw(Box::new(Message::html(content, Version::of(version)))),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_markdown(content: *const u8, content_len: usize, version: i32) -> *mut Message {
match unsafe { Slice::borrow_text(content, content_len) } {
Some(content) => Box::into_raw(Box::new(Message::markdown(content, Version::of(version)))),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_json(content: *const u8, content_len: usize, version: i32) -> *mut Message {
match unsafe { Slice::borrow_text(content, content_len) } {
Some(content) => Box::into_raw(Box::new(Message::json(content, Version::of(version)))),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_file(path: *const u8, path_len: usize, version: i32) -> *mut Message {
match unsafe { Slice::borrow_text(path, path_len) } {
Some(path) => Box::into_raw(Box::new(Message::file(path, Version::of(version)))),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_redirect(target: *const u8, target_len: usize, version: i32) -> *mut Message {
match unsafe { Slice::borrow_text(target, target_len) } {
Some(target) => Box::into_raw(Box::new(Message::redirect(target, Version::of(version)))),
None => std::ptr::null_mut(),
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_message_set_cookie(message: *mut Message, cookie: *const SetCookie, error: *mut *mut ErrorHandle) -> Status {
let (Some(message), Some(cookie)) = (unsafe { message.as_mut() }, unsafe { cookie.as_ref() }) else {
return unsafe { ErrorHandle::raise(error, Status::Invalid) };
};
match message.set_cookie(cookie) {
Ok(()) => Status::Ok,
Err(failure) => unsafe { ErrorHandle::report(error, &failure) },
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_message_delete_cookie(message: *mut Message, cookie: *const SetCookie, error: *mut *mut ErrorHandle) -> Status {
let (Some(message), Some(cookie)) = (unsafe { message.as_mut() }, unsafe { cookie.as_ref() }) else {
return unsafe { ErrorHandle::raise(error, Status::Invalid) };
};
match message.delete_cookie(cookie.clone()) {
Ok(()) => Status::Ok,
Err(failure) => unsafe { ErrorHandle::report(error, &failure) },
}
}
#[unsafe(no_mangle)]
pub extern "C" fn soyokaze_status_reason(status_code: u16) -> Slice {
Slice::text(crate::responses::Status::reason(status_code))
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_content_type(path: *const u8, path_len: usize) -> Slice {
match unsafe { Slice::borrow_text(path, path_len) } {
Some(path) => Slice::text(crate::models::Message::content_type(path)),
None => Slice::ABSENT,
}
}
#[unsafe(no_mangle)]
pub unsafe extern "C" fn soyokaze_response_upgrade_required(request: *const Message, version: i32, protocol: *const u8, protocol_len: usize) -> *mut Message {
let (Some(request), Some(protocol)) = (unsafe { request.as_ref() }, unsafe { Slice::borrow_text(protocol, protocol_len) }) else {
return std::ptr::null_mut();
};
Box::into_raw(Box::new(Message::upgrade_required(request, Version::of(version), protocol)))
}