Skip to content

Commit ba1f119

Browse files
committed
Rework code to compile on latest rust nightly.
1 parent 5c0da59 commit ba1f119

File tree

3 files changed

+41
-22
lines changed

3 files changed

+41
-22
lines changed

Cargo.toml

+3-1
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
[package]
22

33
name = "fcgi"
4-
version = "0.0.2"
4+
version = "0.1.0"
55
authors = ["Daniel Kaes <[email protected]>"]
66

77
description = "Rust bindings for fast-cgi"
@@ -22,3 +22,5 @@ name = "echo"
2222
path = "examples/echo.rs"
2323
doc = false
2424

25+
[dependencies]
26+
libc = "*"

examples/echo.rs

+6-5
Original file line numberDiff line numberDiff line change
@@ -1,10 +1,9 @@
1-
21
extern crate fcgi;
32
extern crate libc;
43

54
use fcgi::{Request, DefaultRequest};
65
use std::sync::{Arc, Mutex};
7-
use std::thread::Thread;
6+
use std::thread;
87

98
static NTASKS: i32 = 8;
109

@@ -20,20 +19,22 @@ fn handle_request(accept_lock: Arc<Mutex<i32>> ) {
2019
let received = request.readall();
2120
request.write("Content-type: text/plain\r\n");
2221
request.write("\r\n");
23-
request.write(received.as_slice());
22+
request.write(received.as_ref());
2423
request.flush(fcgi::StreamType::OutStream);
2524
request.finish();
2625
}
2726
}
2827

2928
fn main() {
29+
3030
fcgi::initialize_fcgi();
3131

3232
let accept_lock = Arc::new(Mutex::new(0));
3333

34-
for _ in range(0, NTASKS) {
34+
for _ in 0..NTASKS {
3535
let child_accept_lock = accept_lock.clone();
36-
let _ = Thread::scoped(move || handle_request(child_accept_lock));
36+
let t = thread::spawn(move || handle_request(child_accept_lock));
37+
t.join().unwrap();
3738
}
3839
}
3940

src/lib.rs

+32-16
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11

22
#![crate_name = "fcgi"]
33
#![crate_type = "lib"]
4+
#![feature(cstr_memory,cstr_to_str)]
45

56
//! This package provides a Rust binding to the C/C++ [fast-cgi library][]
67
//!
@@ -43,11 +44,10 @@
4344
//! ```
4445
4546
extern crate libc;
46-
use std::default::Default;
47+
use std::default::Default;
4748
use std::ffi;
48-
use std::str;
49-
50-
49+
use std::ffi::{CString};
50+
use std::os::unix::io::{RawFd};
5151
pub mod capi;
5252

5353
/// Initialize the FCGX library. Returns true upon success.
@@ -65,14 +65,17 @@ pub fn is_cgi() -> bool {
6565
}
6666
}
6767

68-
#[deriving(Copy)]
68+
#[derive(Clone,Copy)]
6969
pub enum StreamType { OutStream, InStream, ErrStream }
7070

7171
/// Methods for working with an FCGI request object. A default implementation is provided within this package.
7272
pub trait Request {
7373

7474
/// Creates a new already initialized instance of an FCGI request.
75-
fn new() -> Option<Self>;
75+
fn new() -> Option<Self> where Self: Sized;
76+
77+
/// Creates a new already initialized instance of an FCGI request.
78+
fn new_with_fd(fd: RawFd) -> Option<Self> where Self: Sized;
7679

7780
/// Accept a new request (multi-thread safe). Be sure to call initialize_fcgi() first.
7881
fn accept(&mut self) -> bool;
@@ -121,6 +124,17 @@ impl Request for DefaultRequest {
121124
}
122125
}
123126
}
127+
128+
fn new_with_fd(fd: RawFd) -> Option<DefaultRequest> {
129+
let mut request: capi::FCGX_Request = Default::default();
130+
unsafe {
131+
if capi::FCGX_InitRequest(&mut request, fd, 0) == 0 {
132+
return Some(DefaultRequest {raw_request: request });
133+
} else {
134+
return None;
135+
}
136+
}
137+
}
124138

125139
fn accept(&mut self) -> bool {
126140
unsafe {
@@ -135,27 +149,28 @@ impl Request for DefaultRequest {
135149
}
136150

137151
fn get_param(&self, name: &str) -> Option<String> {
138-
let cstr = ffi::CString::from_slice(name.as_bytes());
152+
let cstr = CString::new(name).unwrap();
139153
unsafe {
140154
let param = capi::FCGX_GetParam(cstr.as_ptr(), self.raw_request.envp);
141155
if param.is_null() {
142156
return None;
143157
}
144-
let resultStr = str::from_c_str(param);
145-
return Some(String::from_str(resultStr));
158+
let result_cstr = ffi::CString::from_ptr(param);
159+
let result_str = result_cstr.to_str().unwrap();
160+
return Some(String::from(result_str));
146161
}
147162
}
148163

149164
fn write(&mut self, msg: &str) -> i32 {
150-
let cstr = ffi::CString::from_slice(msg.as_bytes());
165+
let cstr = ffi::CString::new(msg).unwrap();
151166
unsafe {
152167
return capi::FCGX_PutS(cstr.as_ptr(), self.raw_request.out_stream);
153168
}
154169
}
155170

156171
fn error(&mut self, msg: &str) -> i32 {
157-
let cstr = ffi::CString::from_slice(msg.as_bytes());
158-
unsafe {
172+
let cstr = ffi::CString::new(msg.as_bytes()).unwrap();
173+
unsafe {
159174
return capi::FCGX_PutS(cstr.as_ptr(), self.raw_request.err_stream);
160175
}
161176
}
@@ -167,22 +182,23 @@ impl Request for DefaultRequest {
167182
let pdst = buffer.as_mut_ptr();
168183
let byte_count = capi::FCGX_GetStr(pdst, n, self.raw_request.in_stream);
169184
buffer.set_len(byte_count as usize);
170-
let resultStr = str::from_c_str(pdst);
171-
return (String::from_str(resultStr), byte_count);
185+
let result_cstr = ffi::CString::from_ptr(pdst);
186+
let result_str = result_cstr.to_str().unwrap();
187+
return (String::from(result_str), byte_count);
172188
}
173189
}
174190

175191
fn readall(&mut self) -> String {
176192
let (mut msg, mut n) = self.read(512);
177193
while n == 512 {
178194
let (new_msg, new_n) = self.read(512);
179-
msg = msg + new_msg.as_slice();
195+
msg = msg + new_msg.as_ref();
180196
n = new_n;
181197
}
182198
return msg;
183199
}
184200

185-
fn flush(&mut self, stream_type: StreamType) {
201+
fn flush(&mut self, stream_type: StreamType) {
186202
let stream = match stream_type {
187203
StreamType::OutStream => self.raw_request.out_stream,
188204
StreamType::InStream => self.raw_request.in_stream,

0 commit comments

Comments
 (0)