我請求您幫助了解FastCGI 協議的部分規范。
目前這是我的代碼:
#![allow(non_snake_case)]
#![allow(unused_must_use)]
use std::os::unix::net::{UnixStream};
use std::io::{Read, Write};
fn main() {
pub const FCGI_VERSION_1: u8 = 1;
pub const FCGI_BEGIN_REQUEST:u8 = 1;
pub const FCGI_RESPONDER: u16 = 1;
pub const FCGI_PARAMS: &str = "FCGI_PARAMS";
let socket_path = "/run/php-fpm/php-fpm.sock";
let mut socket = match UnixStream::connect(socket_path) {
Ok(sock) => sock,
Err(e) => {
println!("Couldn't connect: {e:?}");
return
}
};
let requestId: u16 = 1;
let role: u16 = FCGI_RESPONDER;
let beginRequest = vec![
// FCGI_Header
FCGI_VERSION_1, FCGI_BEGIN_REQUEST,
(requestId >> 8) as u8, (requestId & 0xFF) as u8,
0x00, 0x08, // This is the size of `FCGI_BeginRequestBody`
0, 0,
// FCGI_BeginRequestBody
(role >> 8) as u8, (role & 0xFF) as u8,
0, // Flags
0, 0, 0, 0, 0, // Reserved
];
socket.write_all(&beginRequest).unwrap();
let data = vec![
(100) as u8, // this value is just an example
];
let contentLength = data.len();
assert!(contentLength <= usize::MAX);
let requestHeader = vec![
FCGI_VERSION_1, FCGI_BEGIN_REQUEST,
(requestId >> 8) as u8, (requestId & 0xFF) as u8,
(contentLength >> 8) as u8, (contentLength & 0xFF) as u8,
0, 0,
];
socket.write_all(&requestHeader).unwrap();
}
由于我對與該主題相關的最后一個問題的回答,我有此代碼,因此,使用該示例代碼(對我來說非常適用),我想問你我的問題。
如何撰寫FCGI_PARAMS?
我的意思是,如果我理解正確,檔案說:
FCGI_PARAMS 是一種流記錄型別,用于將名稱-值對從 Web 服務器發送到應用程式
這意味著FCGI_PARAMS是Name-Value Pairs。檔案中描述的部分Name-Value Pairs說:
FastCGI 傳輸一個名稱-值對作為名稱的長度,然后是值的長度,然后是名稱,然后是值
然后這樣我認為它會是(在代碼中表示):
let param = vec![
"SCRIPT_FILENAME".len(),
"index.php".len(),
"SCRIPT_FILENAME",
"index.php",
]; // it is just an example, but i think it represents what i am talking about
但是,如果我添加此代碼,然后使用以下行將其寫入套接字:
socket.write_all(¶m);
然后在讀取套接字時,套接字不回傳任何內容。我究竟做錯了什么?我應該如何發送資料?我希望你能幫我解決這個問題,我想澄清一下我是新手FastCGI,unix sockets所以如果我顯示的代碼中的任何一行沒有很好地舉例說明,我感到非常抱歉。
uj5u.com熱心網友回復:
Rust 不支持異構向量,所以你let param =…不應該編譯。發送引數的方式是使用多次寫入:
let param_name = "SCRIPT_FILENAME".as_bytes(); // could also be written `b"SCRIPT_FILENAME"`
let param_value = "index.php".as_bytes();
let lengths = [ param_name.len() as u8, param_value.len() as u8 ];
socket.write_all (&lengths).unwrap();
socket.write_all (param_name).unwrap();
socket.write_all (param_value).unwrap();
轉載請註明出處,本文鏈接:https://www.uj5u.com/qiye/512339.html
標籤:插座锈
