logo
Apache Lounge
Webmasters

 

About Forum Index Downloads Search Register Log in RSS X


Keep Server Online

If you find the Apache Lounge, the downloads and overall help useful, please express your satisfaction with a donation.

or

Bitcoin

A donation makes a contribution towards the costs, the time and effort that's going in this site and building.

Thank You! Steffen

Your donations will help to keep this site alive and well, and continuing building binaries. Apache Lounge is not sponsored.
Post new topic   Forum Index -> Coding & Scripting Corner View previous topic :: View next topic
Reply to topic   Topic: Writing a mod_fcgid responder in Rust.
Author
animats



Joined: 16 Aug 2025
Posts: 3
Location: Silicon Valley, CA

PostPosted: Sat 16 Aug '25 7:52    Post subject: Writing a mod_fcgid responder in Rust. Reply with quote

I'm trying to run a mod_fcgid responder written in Rust on Dreamhost shared hosting. I've done this successfully from Go, but now I'm trying to do it from Rust. Had to write an entire responder library, because all the existing ones are standalone or run under ngnix.

If I can get far enough that a simple test program will return error messages, I can do this. But right now, I can't get that far. I'm stuck at:

500 Internal Server Error error.

The Apache logs say:

Code:

error
[pid 964859] util_script.c(497): End of script output before headers: vehiclelogserver.fcgi

warning
[pid 964859] fcgid_proc_unix.c(627): (104)Connection reset by peer: mod_fcgid: error reading data from FastCGI server


I'm doing something basic wrong, obviously.

Version info:
Code:
/usr/sbin/apache2 -v
Server version: Apache/2.4.52 (Ubuntu)
Server built:   2025-08-11T12:10:10


My own code:

https://github.com/John-Nagle/maptools/tree/main/server

See example "echo", which I upload as "echo.fcgi"

This is just the beginnings of a responder. Doesn't do anything useful yet.

The headers I'm sending (or at least think I am) are the usual 8-byte FCGI header followed by content, per the spec at https://www.mit.edu/~yandros/doc/specs/fcgi-spec.html

If I run the program from a command line on the server, my program generates

Code:
./echo.fcgi
ffsafsfsdf
�bStatus: 500 FCGI responder error: Invalid FCGI record type: 102
Content-Type: text; charset=utf-8���


(That's printing bytes as text, so the binary headers are garbage characters.)

So it's sending something reasonable, although "id" may be wrong if the error message
happens before the input records have been parsed.

Obvious things checked:

- Executable "echo.fcgi" is executable.

- I can't see the Dreamhost config files, but .htaccess is exactly the same as it is for the site that runs a Go program.

Aux questions:

- Is there any way I can put a log entry into the Apache log from this program? Writing to stderr seems to go nowhere.

Thanks.
Back to top
animats



Joined: 16 Aug 2025
Posts: 3
Location: Silicon Valley, CA

PostPosted: Sat 16 Aug '25 21:50    Post subject: More info - stdin raw mode problem? Reply with quote

Added logging and stripped down the program for a test.

What's wrong is that I get an OS error 22 (EINVAL) reading from stdin.
Here's the Rust code:

Code:
    let inio = std::io::stdin();
    let mut instream = inio.lock(); // Lock the stdin for reading.
    let mut header_bytes:[u8;8] = Default::default();
    use std::io::Read;
    let stat = instream.read_exact(&mut header_bytes);
    log::debug!("Stat: {:?} Bytes: {:?}", stat, header_bytes);


That returns

Code:
19:14:55 [DEBUG] (1) echo: Stat: Err(Os { code: 22, kind: InvalidInput, message: "Invalid argument" }) Bytes: [0, 0, 0, 0, 0, 0, 0, 0]
 


This seems to be some kind of incompatibility between mod_fcgid's pipes/sockets and what Rust does with stdin.
Back to top
animats



Joined: 16 Aug 2025
Posts: 3
Location: Silicon Valley, CA

PostPosted: Mon 18 Aug '25 4:52    Post subject: Reply with quote

Got past that.

UNIX sockets were never intended to be passed to subprocesses, like pipe descriptors were.
The ritual for parent/child process communication via UNIX sockets is quite obscure.
Here it is in Rust: https://users.rust-lang.org/t/reading-from-pipe-via-stdin-in-binary/133088/10
Back to top
James Blond
Moderator


Joined: 19 Jan 2006
Posts: 7431
Location: EU, Germany, Next to Hamburg

PostPosted: Mon 18 Aug '25 11:49    Post subject: Reply with quote

I don't know if it is a good idea to look at https://github.com/php/php-src/blob/master/sapi/cgi/cgi_main.c

But that is the implementation that works with mod_fcgid. Yes, it is C code, not Rust. But maybe a point to start?

After some short searching, I also found https://github.com/User65k/async-fcgi and https://crates.io/crates/fastcgi-client
Back to top


Reply to topic   Topic: Writing a mod_fcgid responder in Rust. View previous topic :: View next topic
Post new topic   Forum Index -> Coding & Scripting Corner