2016-11-04 11 views
0

Я реализую fastCGI в C++ вместе с nginx. До сих пор я могу разработать базовый метод HTTP-запроса и некоторое перенаправление URL-адресов. Но я не могу отправить тело сообщения при перенаправлении с почтового URL на другой почтовый URL. Ниже мой код:Включить тело сообщения при перенаправлении URL-адреса в fastCGI?

#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. Здесь я хочу отправить строку json (выше), когда/posttopostredirect попадает в/postmethod url. Но не мог понять, как это сделать

ответ

0

Любое содержимое сообщений перенаправления HTTP полностью игнорируется. Это не имеет ничего общего с FastCGI. Так работает HTTP. Сообщения перенаправления HTTP не могут использоваться для отправки какого-либо контента. Ну, они могут, но HTTP-клиент просто проигнорирует его и выдаст запрос GET на перенаправленный URL-адрес.

Единственное, что можно сделать, это включить любые данные в качестве параметров, закодированных в перенаправленном URL-адресе. Это делается так же, как кодирование любых параметров в URL-адресе.

 Смежные вопросы

  • Нет связанных вопросов^_^