我正在使用 Apachemod_fastcgi
和fcgapp.h
API 开发一个 FastCGI 应用程序。到目前为止一切正常,但我未能检测到中止的连接。我们需要控制它们,因为我们有时会执行昂贵的操作,当用户中止请求时必须中止这些操作。
我正在尝试按照文档中的说明使用 SIGPIPE,但它似乎不起作用。
我写了一个简化的测试用例来显示我的问题:
#include <iostream>
#include <stdio.h>
#include <signal.h>
#include <unistd.h>
#include <fstream>
#include "fcgio.h"
using namespace std;
void signal_handler(int signo) {
cerr << "SIGNAL RECEIVED: " << signo << endl;
}
int main(void) {
streambuf * cin_streambuf = cin.rdbuf();
streambuf * cout_streambuf = cout.rdbuf();
streambuf * cerr_streambuf = cerr.rdbuf();
struct sigaction sa;
sigemptyset(&sa.sa_mask);
sa.sa_handler = signal_handler;
sa.sa_flags = 0;
sigaction(SIGPIPE, &sa, NULL);
sigaction(SIGUSR1, &sa, NULL);
sigaction(SIGTERM, &sa, NULL);
FCGX_Request request;
int socketfd = FCGX_OpenSocket("/home/guillermo/fastcgi/socket", 10);
FCGX_Init();
FCGX_InitRequest(&request, socketfd, 0);
while (FCGX_Accept_r(&request) == 0) {
fcgi_streambuf cin_fcgi_streambuf(request.in);
fcgi_streambuf cout_fcgi_streambuf(request.out);
fcgi_streambuf cerr_fcgi_streambuf(request.err);
cin.rdbuf(&cin_fcgi_streambuf);
cout.rdbuf(&cout_fcgi_streambuf);
cerr.rdbuf(&cerr_fcgi_streambuf);
sleep(5);
cout << "Content-type: text/html\r\n"
<< "\r\n"
<< "<html>\n"
<< " <head>\n"
<< " <title>Hello, World!</title>\n"
<< " </head>\n"
<< " <body>\n"
<< " <h1>Hello!</h1>\n"
<< " </body>\n"
<< "</html>\n";
cerr << "All right" << endl;
}
cin.rdbuf(cin_streambuf);
cout.rdbuf(cout_streambuf);
cerr.rdbuf(cerr_streambuf);
return 0;
}
我已经尝试使用这两种不同的配置mod_fastcgi
:
FastCGIExternalServer $file -socket $socket
FastCGIServer $file -socket $socket
无论我是否中止连接,5 秒后我总是在 Apache 错误日志中得到“好的”。我已经使用 curl (Ctrl-C) 和不同的浏览器(停止按钮)以及 Javascript 中的 XMLHttpRequest.abort(这是真实场景)对其进行了测试。
这是我的系统配置:
- Linux Debian 3.2.46-1 x86_64
- Apache2 (2.2.22-13)
- mod_fastcgi (2.4.1-SNAP-0910052249)