Oleksandr Kucher
Oleksandr Kucher

Reputation: 489

FCGX_Accept_r runs twice in simple FastCGI application on C

I am trying to create simple FastCGI app written in C:

#include <fcgiapp.h>
#include <stdio.h>
int main()
{
    int sockfd = FCGX_OpenSocket("127.0.0.1:9000", 1024);
    FCGX_Request request;

    FCGX_Init();
    FCGX_InitRequest(&request, sockfd, 0);

    while (FCGX_Accept_r(&request) == 0)
    {
        printf("Accepted\n");
        FCGX_FPrintF(request.out, "Content-type: text/html\r\n\r\n<h1>Hello World!</h1>");
        FCGX_Finish_r(&request);
    }
}

It works fine - when i call it from browser, it displays page with "Hello World!" message.

The problem is that code inside a "while" works twice, i.e. I see following output in terminal:

[root@localhost example]$ ./hello 
Accepted 
Accepted

Why it prints "Accepted" twice per each request? If I put real code here, e.g. query a DB, it will be executed twice as well.

Upvotes: 3

Views: 953

Answers (1)

thrig
thrig

Reputation: 676

What program are you using to make the web request? Some web clients may also request an e.g. favicon.ico file, which may account for the second request to the fcgi process:

127.0.0.1 - - [29/Aug/2015:16:02:11 +0000] "GET / HTTP/1.1" 200 32 "-" "..."
127.0.0.1 - - [29/Aug/2015:16:02:11 +0000] "GET /favicon.ico HTTP/1.1" 200 32 "http://127.0.0.1/" "..."

This could be determined by inspecting the webserver logs, or adding debugging to the C program to show the request parameters. Using just telnet to request GET / HTTP/1.0, I do not see a double hit for a forward-everything-to-fcgi webserver configuration using the nginx webserver.

Upvotes: 5

Related Questions