Pascal
Pascal

Reputation: 2214

FastCGI with C++ other ways to start than spawn-fcgi

I'm currently working on a project that involves FastCGI and C++. Now I found the official FCGI Library. I tried out the echo example.

/* 
 * echo.c --
 *
 *  Produce a page containing all FastCGI inputs
 *
 *
 * Copyright (c) 1996 Open Market, Inc.
 *
 * See the file "LICENSE.TERMS" for information on usage and redistribution
 * of this file, and for a DISCLAIMER OF ALL WARRANTIES.
 *
 */

#ifndef lint
static const char rcsid[] = "$Id: echo.c,v 1.1.1.1 2001/04/25 00:43:49 robs Exp $";
#endif /* not lint */

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

extern char **environ;

void PrintEnv(char *label, char **envp)
{
    printf("%s:<br>\n<pre>\n", label);
    for(; *envp != NULL; envp++) {
        printf("%s\n", *envp);
    }
    printf("</pre><p>\n");
}

void main ()
{
    char **initialEnv = environ;
    int count = 0;
    while(FCGI_Accept() >= 0) {
        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 <p>\n", ++count);
        if(contentLength != NULL) {
            len = strtod(contentLength, NULL);
        } 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");
        }
        PrintEnv("Request environment", environ);
        PrintEnv("Initial environment", initialEnv);
    } /* while */
}

I start this script with the command spawn-fcgi -p 8000 -n hello.

But is it also possible to just start the program xy without the spawn-fcgi. Do you know a good example, or a documentation?

thanks for your answer

Upvotes: 3

Views: 1623

Answers (1)

Harmeet
Harmeet

Reputation: 556

The spawn-fcgi command opens a TCP connection for you and starts listening on the specified port (8000 in your case). It forwards the request coming in on the TCP connection to your application's stdin. It also forwards your writes to the stdout back to the TCP connection.

You can create the connection yourself using FCGX_OpenSocket() call and then pass the returned socket to FCGX_InitRequest(). After that you can go for the loop using FCGX_Accept_r() instead of FCGI_Accept()!

BTW: there is another tool that many people use instead of spawn-fcgi - supervisor. In addition to managing the connection for you, it also monitors your process. So, if your process crashes because of some wierd request, it re-launches your application!

Upvotes: 4

Related Questions