How can I run fastCGI applications manually on Windows?

I configured the web server to use the fastCGI 'remote' application over the named pipe (it is actually located on the same Windows host). Now I'm trying to figure out how to run the fastCGI application to use this channel, but I'm not sure how to do it. The other OS seems to have spawn-fcgi utilities for this, but it looks like there was nothing similar for Windows.

This is my APP:

#include <stdio.h>
#include "fcgi_stdio.h"

int main(int argc, char ** argv)
{
  while (FCGI_Accept() >= 0) {
    printf("Content-type: text/html\r\n"
        "\r\n"
        "<title>Web Services Interface Module</title>"
        "<h1>Web Services Interface Module</h1>\n");
  }
  return(0);
}

Out of interest, I use Abyss Web Server, although I hope it has nothing to do with the answer.

Regards

+3
source share
4 answers

FCGI , FCGX. FCGX_Open_Socket , . 9345 .

FCGX_OpenSocket(":9345", 500);

​​, spawn_fcgi, .

+2
 /*
 *----------------------------------------------------------------------
 *
 * FCGX_OpenSocket --
 *
 *  Create a FastCGI listen socket.
 *
 *  path is the Unix domain socket (named pipe for WinNT), or a colon
 *  followed by a port number.  e.g. "/tmp/fastcgi/mysocket", ":5000"
 *
 *  backlog is the listen queue depth used in the listen() call.
 *
 *  Returns the socket file descriptor or -1 on error.
 *
 *----------------------------------------------------------------------
 */
 DLLAPI int FCGX_OpenSocket(const char *path, int backlog);

libfcgi stdin. stdin .

 dup2(FCGX_OpenSocket("pipe name", 5),0);
+1

change FCGX_Init to fcgiapp.c

int FCGX_Init(void)
{
    char *p;

    int listen_socket;

    if (libInitialized) {
        return 0;
    }

    if (OS_LibInit(NULL) == -1) {
        return OS_Errno ? OS_Errno : -9997;
    }   

    /*sureone socket*/
        /* 9010 is your listen port*/
    listen_socket = FCGX_OpenSocket(":9010", 400); 
    if(listen_socket < 0) exit(1);
    printf("FCGX_InitRequest...\n");
    FCGX_InitRequest(&the_request, listen_socket, 0);
    /*end sureone*/

    //FCGX_InitRequest(&the_request, FCGI_LISTENSOCK_FILENO, 0);



    p = getenv("FCGI_WEB_SERVER_ADDRS");
    webServerAddressList = p ? StringCopy(p) : NULL;

    libInitialized = 1;
    return 0;
}
+1
source

I came up with code that just works for me on Windows:

int main ()
{
    char **initialEnv = environ; //Keep track of initial environment
    int count = 0;        
    int listenSocket;


    //It ugly, but in Windows we need to initialize the 
    //socket library. We can do it by calling
    //libfcgi OS_LibInit() function
    OS_LibInit(NULL);

    //Open a socket. Here, we use localhost:9000
    listenSocket = FCGX_OpenSocket("localhost:9000", 5);
    if (listenSocket < 0) {
      exit(1);
    }

    FCGX_Request request;
    FCGX_Init();
    FCGX_InitRequest(&request, listenSocket, 0);


    while (FCGX_Accept_r(&request) >= 0) {
        //Init I/O streams wrapper as well as set the new environment
        FCGI_stdin->stdio_stream = NULL;
        FCGI_stdin->fcgx_stream = request.in;
        FCGI_stdout->stdio_stream = NULL;
        FCGI_stdout->fcgx_stream = request.out;
        FCGI_stderr->stdio_stream = NULL;
        FCGI_stderr->fcgx_stream = request.err;
        environ = request.envp;

        //Funny stuff
        char *contentLength = getenv("CONTENT_LENGTH");
        int len;

        printf("Content-type: text/html\r\n"
               "\r\n"
               "<title>FastCGI echo</title>"
               "<h1>FastCGI echo</h1>\n"
               "Request number %d,  Process ID: %d<p>\n", ++count, getpid());

        if (contentLength != NULL) {
            len = strtol(contentLength, NULL, 10);
        } else {
            len = 0;
        }

        if (len <= 0) {
            printf("No data from standard input.<p>\n");
        }
        else {
            int i, ch;

            printf("Standard input:<br>\n<pre>\n");
            for (i = 0; i < len; i++) {
                if ((ch = getchar()) < 0) {
                    printf("Error: Not enough bytes received on standard input<p>\n");
                    break;
                }
                putchar(ch);
            }
            printf("\n</pre><p>\n");
        }

    } /* while */    

    return 0;
}
0
source

Source: https://habr.com/ru/post/1726326/


All Articles