use std::mem;
use std::ffi::{CString, CStr};
use std::str;
use traits::Wrappable;
use system::Time;
use csfml_network_sys as ffi;
#[repr(i32)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Copy)]
pub enum Method {
Get = ffi::GET as i32,
Post = ffi::POST as i32,
Head = ffi::HEAD as i32
}
#[repr(i32)]
#[derive(Clone, PartialEq, Eq, PartialOrd, Ord, Debug, Copy)]
pub enum Status {
Ok = ffi::sfHttpOk as i32,
Created = ffi::CREATED as i32,
Accepted = ffi::ACCEPTED as i32,
NoContent = ffi::NOCONTENT as i32,
ResetContent = ffi::RESETCONTENT as i32,
PartialContent = ffi::PARTIALCONTENT as i32,
MultipleChoices = ffi::MULTIPLECHOICES as i32,
MovedPermanently = ffi::MOVEDPERMANENTLY as i32,
MovedTemporarily = ffi::MOVEDTEMPORARILY as i32,
NotModified = ffi::NOTMODIFIED as i32,
BadRequest = ffi::BADREQUEST as i32,
Unauthorized = ffi::UNAUTHORIZED as i32,
Forbidden = ffi::FORBIDDEN as i32,
NotFound = ffi::NOTFOUND as i32,
RangeNotSatisfiable = ffi::RANGENOTSATISFIABLE as i32,
InternalServerError = ffi::INTERNALSERVERERROR as i32,
NotImplemented = ffi::NOTIMPLEMENTED as i32,
BadGateway = ffi::BADGATEWAY as i32,
ServiceNotAvailable = ffi::SERVICENOTAVAILABLE as i32,
GatewayTimeout = ffi::GATEWAYTIMEOUT as i32,
VersionNotSupported = ffi::VERSIONNOTSUPPORTED as i32,
InvalidResponse = ffi::sfHttpInvalidResponse as i32,
ConnectionFailed = ffi::sfHttpConnectionFailed as i32
}
pub struct Request {
request: *mut ffi::sfHttpRequest
}
pub struct Response {
response: *mut ffi::sfHttpResponse
}
pub struct Http {
http: *mut ffi::sfHttp
}
impl Request {
pub fn new() -> Option<Request> {
let ptr = unsafe { ffi::sfHttpRequest_create() };
if ptr.is_null() {
None
} else {
Some(Request {
request: ptr
})
}
}
pub fn set_field(&self, field: &str, value: &str) {
let c_field = CString::new(field.as_bytes()).unwrap().as_ptr();
let c_value = CString::new(value.as_bytes()).unwrap().as_ptr();
unsafe {
ffi::sfHttpRequest_setField(self.request,
c_field,
c_value)
}
}
pub fn set_method(&self, method: Method) {
unsafe {
ffi::sfHttpRequest_setMethod(self.request, method as ffi::Method)
}
}
pub fn set_uri(&self, uri: &str) {
let c_uri = CString::new(uri.as_bytes()).unwrap().as_ptr();
unsafe {
ffi::sfHttpRequest_setUri(self.request, c_uri)
}
}
pub fn set_http_version(&self, major: u32, minor: u32) {
unsafe {
ffi::sfHttpRequest_setHttpVersion(self.request, major, minor)
}
}
pub fn set_body(&self, body: &str) {
let c_body = CString::new(body.as_bytes()).unwrap().as_ptr();
unsafe {
ffi::sfHttpRequest_setBody(self.request, c_body)
}
}
#[doc(hidden)]
fn unwrap(&self) -> *mut ffi::sfHttpRequest {
self.request
}
}
impl Drop for Request {
fn drop(&mut self) {
unsafe {
ffi::sfHttpRequest_destroy(self.request)
}
}
}
impl Response {
pub fn get_field(&self, field: &str) -> String {
let c_field = CString::new(field.as_bytes()).unwrap().as_ptr();
unsafe {
let string = ffi::sfHttpResponse_getField(self.response, c_field);
str::from_utf8(CStr::from_ptr(string).to_bytes_with_nul()).unwrap().into()
}
}
pub fn get_status(&self) -> Status {
unsafe {
mem::transmute(ffi::sfHttpResponse_getStatus(self.response) as i32)
}
}
pub fn get_major_version(&self) -> u32 {
unsafe {
ffi::sfHttpResponse_getMajorVersion(self.response)
}
}
pub fn get_minor_version(&self) -> u32 {
unsafe {
ffi::sfHttpResponse_getMinorVersion(self.response)
}
}
pub fn get_body(&self) -> String {
unsafe {
let string = ffi::sfHttpResponse_getBody(self.response);
str::from_utf8(CStr::from_ptr(string).to_bytes_with_nul()).unwrap().into()
}
}
}
impl Drop for Response {
fn drop(&mut self) {
unsafe {
ffi::sfHttpResponse_destroy(self.response)
}
}
}
impl Http {
pub fn new() -> Option<Http> {
let ptr = unsafe{ ffi::sfHttp_create() };
if ptr.is_null() {
None
} else {
Some(Http {
http: ptr
})
}
}
pub fn set_host(&self, host: &str, port: u16) {
let c_host = CString::new(host.as_bytes()).unwrap().as_ptr();
unsafe {
ffi::sfHttp_setHost(self.http, c_host, port)
}
}
pub fn send_request(&self, request: &Request, timeout: &Time) -> Response {
Response {
response: unsafe { ffi::sfHttp_sendRequest(self.http, request.unwrap(), timeout.unwrap()) }
}
}
}
impl Drop for Http {
fn drop(&mut self) {
unsafe {
ffi::sfHttp_destroy(self.http)
}
}
}