Check if invoked as FastCGI

This commit is contained in:
Mohd Tarmizi 2018-04-22 13:09:39 +08:00
parent 7f4b1b18aa
commit d9ab93c1c5
2 changed files with 14 additions and 0 deletions

View File

@ -32,6 +32,7 @@
//! use std::io::Write;
//!
//! fn main() {
//! if !fastcgi::is_fastcgi() { return }
//! fastcgi::run(|mut req| {
//! write!(&mut req.stdout(), "Content-Type: text/plain\n\nHello, world!")
//! .unwrap_or(());
@ -630,3 +631,10 @@ pub fn run_tcp<F>(handler: F, listener: &TcpListener) where
F: Fn(Request) + Send + Sync + 'static {
run_transport(handler, &mut Transport::from_tcp(&listener))
}
#[cfg(unix)]
/// Check if the program is running as FastCGI. This check is not necessary if
/// you use `run_tcp`.
pub fn is_fastcgi() -> bool {
Transport::new().is_fastcgi()
}

View File

@ -28,6 +28,7 @@ use std::io::{self, Read, Write};
use std::mem;
use std::net::{Ipv4Addr, Ipv6Addr};
use std::os::unix::io::RawFd;
use std::ptr::{null_mut};
const LISTENSOCK_FILENO: c::c_int = 0;
@ -44,6 +45,11 @@ impl Transport {
Transport { inner: raw_fd }
}
pub fn is_fastcgi(&self) -> bool {
let res = unsafe { c::getpeername(self.inner, null_mut(), null_mut()) };
res == -1 && io::Error::last_os_error().raw_os_error() == Some(c::ENOTCONN)
}
pub fn accept(&mut self) -> io::Result<Socket> {
let res = unsafe {
c::accept(self.inner, 0 as *mut _, 0 as *mut _)