1

I used this tutorial http://chriswu.me/blog/writing-hello-world-in-fcgi-with-c-plus-plus/ to write a c++ server app with nginx + fcgi. The c++ server app is being spawned by spawn-fcgi since nginx doesn't spawn it by itself. Here is the c++ code:

#include <iostream>
#include "fcgio.h"

using namespace std;

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);

        cout << "Content-type: text/html\r\n"
             << "\r\n"
             << "<html>\n"
             << "  <head>\n"
             << "    <title>Hello, World!</title>\n"
             << "  </head>\n"
             << "  <body>\n"
             << "    <h1>Hello, World!</h1>\n"
             << "  </body>\n"
             << "</html>\n";

        // Note: the fcgi_streambuf destructor will auto flush
    }

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

    return 0;
}

Everything works fine on linux. On windows the exact same app will fail when FCGX_Accept_r(&request) is being called. The error message is "unknown listenType (0)" . The listenType is an internal variable of the libfcgi library https://github.com/jocelyn-old/libfcgi/blob/master/libfcgi/os_win32.c which should be either FD_SOCKET_ASYNC or FD_PIPE_ASYNC (there are more types, but the rest are not supported). Just by looking at the code it looks to me that either:

  1. spawn-fcgi doesn't properly forward the standard output/input/error and the environment variables towards the c++ libfcgi app and that is why the initialization of the listenType variable is not done correctly on windows
  2. the windows implementation of libfcgi is too old and it is broken on recent Windows 10. This case looks improbable to me since I was able to successfully run the same c++ app with apache + fcgi and no spawn-fcgi (apache can spawn fcgi apps).

Based on the evidence, the odds are that spawn-fcgi is broken on windows or the spawn-fcgi build I did with cygwin is not working correctly.

Did anybody successfully run nginx + c++ fastcgi app + spawn-fcgi on windows?

0 Answers0