如何创建404或503响应

Fastcgi++: How to create 404 or 503 responses

本文关键字:响应 创建 何创建      更新时间:2023-10-16

Fastcgi++是一个简化在c++中实现Fastcgi服务器的库。这是我想做的一个非常简单的用例:检查文件是否存在,如果不存在,则生成一些错误消息。下面是代码,查找问号

struct the_fastcgi_server_t: Fastcgipp::Request<char>
{
    bool response() 
    {
        using namespace Fastcgipp;
        Fastcgipp::Http::Environment<char> const &env =
            this->environment();
        // Can I resolve the file?
        std::string target_js;
        try {
            target_js = path_processor( env.scriptName );
        } catch ( file_not_found_exc_t const& e )
        {
            // TODO How do I set a standard 404 here???!!
            return true;
        }
        out << "Content-Type: text/javascript; charset=utf-8rnrn";
        // ... Here I fill the response.
        return true;
    }
};

关于如何设置响应类型有什么想法吗?

答案基本如下:

https://web.archive.org/web/20160115021335/http://www.fastcgi.com/docs/faq.html httpstatus

也就是说,使用像这样的代码片段

 out << "Status: 404 Not foundrnrn";

中的响应方法。它不是标准的http,但FastCGI只是CGI的包装,这就是CGI的工作方式。