【问题标题】:Incorporate body of message while redirecting url in fastCGI?在fastCGI中重定向url时合并消息正文?
【发布时间】:2017-03-18 06:26:48
【问题描述】:

我正在与 nginx 一起在 c++ 中实现 fastCGI。到目前为止,我能够开发基本的 http 请求方法和一些 url 重定向。但是,从帖子网址重定向到另一个帖子网址时,我无法发送邮件正文。以下是我的代码:

    #include <stdlib.h>
    #include <stdio.h>
    #include <sstream>
    #include <iostream>
    #include <string>
    #include "string.h"
    #include "fcgio.h"
    #include <fcgi_stdio.h>
    #include <boost/algorithm/string.hpp>

    using namespace std;
    using namespace boost;

    // Maximum bytes
    const unsigned long STDIN_MAX = 1000000;

    /**
     * Note this is not thread safe due to the static allocation of the
     * content_buffer.
     */
    string get_request_content(const FCGX_Request & request) {
        char * content_length_str = FCGX_GetParam("CONTENT_LENGTH", request.envp);
        unsigned long content_length = STDIN_MAX;

        if (content_length_str) {
            content_length = strtol(content_length_str, &content_length_str, 10);
            if (*content_length_str) {
                cerr << "Can't Parse 'CONTENT_LENGTH='"
                     << FCGX_GetParam("CONTENT_LENGTH", request.envp)
                     << "'. Consuming stdin up to " << STDIN_MAX << endl;
            }

            if (content_length > STDIN_MAX) {
                content_length = STDIN_MAX;
            }
        } else {
            // Do not read from stdin if CONTENT_LENGTH is missing
            content_length = 0;
        }

        char * content_buffer = new char[content_length];
        cin.read(content_buffer, content_length);
        content_length = cin.gcount();

        // Chew up any remaining stdin - this shouldn't be necessary
        // but is because mod_fastcgi doesn't handle it correctly.

        // ignore() doesn't set the eof bit in some versions of glibc++
        // so use gcount() instead of eof()...
        do cin.ignore(1024); while (cin.gcount() == 1024);

        string content(content_buffer, content_length);
        delete [] content_buffer;
        return content;
    }

    int main(void) {
        // Backup the stdio streambufs
        streambuf * cin_streambuf  = cin.rdbuf();
        streambuf * cout_streambuf = cout.rdbuf();
        streambuf * cerr_streambuf = cerr.rdbuf();

        FCGX_Request request;

        FCGX_Init();
        FCGX_InitRequest(&request, 0, 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);

            const char * uri = FCGX_GetParam("REQUEST_URI", request.envp);

            string content = get_request_content(request);

            if (content.length() == 0) {
                content = ", something!";
            }
            const char * mediaType = FCGX_GetParam("REQUEST_METHOD",request.envp);
            string value;
            if(iequals(mediaType,"POST")&&iequals(uri,"/postmethod")) {

                get_request_content(request);
                 cout << "HTTP/1.1 200 OK\r\nContent-Length: " << content.length() << "\r\n\r\n" << content;
            }

            if(iequals(mediaType,"GET")&&iequals(uri,"/getmethod")) {

                string aalu = "this is the new lenght";
                 cout << "HTTP/1.1 200 OK\r\nContent-Length: " << aalu.length() << "\r\n\r\n" << aalu;
                 FCGX_Finish_r(&request);
            }

            if(iequals(mediaType,"GET")&&iequals(uri,"/redirect")) {

                cout << "HTTP/1.1 301\r\nLocation: http://localhost/getmethod\r\n\r\n";
//              cout << "Status: 301\r\n"
        //                       << "Location: http://localhost/getmethod\r\n";
        //                       << "\r\n";
        //                       << "<html><body>Not Found</body></html>\n";
            }

            if(iequals(mediaType,"GET")&&iequals(uri,"/postredirect")) {     // problem here

                string json = "{\"topic\":\"asdf\",\"message\":\"message\"}";
                cout << "HTTP/1.1 308\r\nLocation: http://localhost/postmethod\r\n\r\n";
//              cout << "Status: 304\r\n"
//                      << "Location: http://localhost/postmethod\r\n"
//                        << "\r\n"
//                          << "<html><body>json</body></html>\n";
            }

            if(iequals(mediaType,"POST")&&iequals(uri,"/getredirect")) {

                            string json = "{\"topic\":\"asdf\",\"message\":\"message\"}";
                            cout << "HTTP/1.1 303\r\nLocation: http://localhost/getmethod\r\n\r\n";
            //              cout << "Status: 304\r\n"
            //                      << "Location: http://localhost/postmethod\r\n"
            //                        << "\r\n"
            //                          << "<html><body>json</body></html>\n";
                        }

            if(iequals(mediaType,"POST")&&iequals(uri,"/posttopostredirect")) {


                                    string json = "{\"topic\":\"adf\",\"message\":\"message\"}";

                                    cout << "Status: 307\r\n"
                                            <<"Location: http://localhost/postmethod\r\n"
                                                <<"\r\n"
                                                <<"\n";

//                                  cout << "Status: 305\r\n"
//                                          << "Location: http://localhost/postmethod\r\n"
//                                            << "\r\n"
//                                              << "<html><body>"+json+"</body></html>\n";
                                }

            if(iequals(mediaType,"GET")&&iequals(uri,"/getttogettredirect")) {

                                                string json = "{\"topic\":\"ssdf\",\"message\":\"message\"}";
                                                cout << "HTTP/1.X 301\r\nLocation: http://localhost/getmethod\r\n\r\n";
            //                                  cout << "Status: 307\r\n"
            //                                          << "Location: http://localhost/postmethod\r\n"
            //                                            << "\r\n";
            //                                              << "<html><body>json</body></html>\n";
                                            }
        }

        // restore stdio streambufs
        cin.rdbuf(cin_streambuf);
        cout.rdbuf(cout_streambuf);
        cerr.rdbuf(cerr_streambuf);

        return 0;
    }

/posttopostredirect url 正在重定向到 /postmethod url。在这里,我希望在 /posttopostredirect 被点击到 /postmethod url 时发送 json 字符串(上图)。但不知道该怎么做

【问题讨论】:

    标签: c++ nginx fastcgi


    【解决方案1】:

    HTTP 重定向消息的任何内容都会被完全忽略。这与 FastCGI 无关。这就是 HTTP 的工作方式。 HTTP 重定向消息不能用于发送任何内容。好吧,他们可以,但 HTTP 客户端会简单地忽略它,并向重定向到的 URL 发出 GET 请求。

    唯一可以做的是将任何数据作为编码在重定向到的 URL 中的参数包含在内。这与编码 URL 中的任何参数的方式相同。

    【讨论】:

      猜你喜欢
      • 2017-11-30
      • 2020-08-25
      • 1970-01-01
      • 2017-10-29
      • 1970-01-01
      • 2022-07-18
      • 2013-05-04
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多