FastCGI: Making C Language Shine in Web Development

Hello everyone! Today I want to share a particularly interesting topic – FastCGI. Are you still worried that C language cannot develop web applications? With FastCGI, C language can easily handle web development! Let’s explore this powerful interface protocol together.

What is FastCGI?

FastCGI is an upgraded version of CGI (Common Gateway Interface), allowing C language programs to communicate with web servers (like Nginx, Apache). Compared to traditional CGI, FastCGI processes can run continuously and handle multiple requests, greatly improving performance.

Why Choose FastCGI?

  1. Performance Advantages

  • Avoids the overhead of frequently creating and destroying processes in traditional CGI

  • Can handle multiple requests simultaneously

  • Uses less memory

  1. Development Convenience

  • Supports multiple programming languages, with comprehensive support for C language

  • Simple and intuitive interface

  • Has mature development frameworks

Let’s Write the First FastCGI Program

First, let’s look at the simplest example:

#include <fcgi_stdio.h>

int main(void) {
    while(FCGI_Accept() >= 0) {
        printf("Content-type: text/html\r\n\r\n");
        printf("<html>\n");
        printf("<title>Hello FastCGI</title>\n");
        printf("<body>\n");
        printf("<h1>Hello, this is my first FastCGI program!</h1>\n");
        printf("</body>\n");
        printf("</html>\n");
    }
    return 0;
}

Tip: You need to install the FastCGI development library before compiling. On Ubuntu, you can use the command: sudo apt-get install libfcgi-dev

Handling GET Request Parameters

Let’s see how to get parameters from the URL:

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

int main(void) {
    char *query_string;

    while(FCGI_Accept() >= 0) {
        printf("Content-type: text/html\r\n\r\n");

        // Get GET parameters
        query_string = getenv("QUERY_STRING");

        printf("<html><body>");
        printf("Received parameters: %s", query_string ? query_string : "No parameters");
        printf("</body></html>");
    }
    return 0;
}

Handling POST Data

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

int main(void) {
    char *content_length_str;
    int content_length;
    char *post_data;

    while(FCGI_Accept() >= 0) {
        printf("Content-type: text/html\r\n\r\n");

        // Get POST data length
        content_length_str = getenv("CONTENT_LENGTH");
        if (content_length_str != NULL) {
            content_length = atoi(content_length_str);
            post_data = (char *)malloc(content_length + 1);
            fread(post_data, 1, content_length, stdin);
            post_data[content_length] = '\0';

            printf("<html><body>");
            printf("Received POST data: %s", post_data);
            printf("</body></html>");

            free(post_data);
        }
    }
    return 0;
}

Note: Pay attention to memory management when handling POST data, and remember to free dynamically allocated memory.

Useful Tips

  1. Compilation Command

    gcc -o program program.c -lfcgi

  2. Start FastCGI Process

    spawn-fcgi -p 9000 -n ./program

  3. Nginx Configuration Example

    location ~ .fcgi$ { fastcgi_pass 127.0.0.1:9000; fastcgi_index index.fcgi; include fastcgi_params;}

Hands-On Exercises

  1. Try writing a simple calculator program that receives two numbers and an operator via GET parameters and returns the calculation result

  2. Implement a form submission program that can receive POST data and display it

Friends, today’s journey of learning C language ends here! FastCGI is truly a powerful assistant for C language web development, so remember to practice coding. If you have any questions, feel free to ask me in the comments, and we can discuss and improve together! I wish you all happy learning and continuous progress in learning C language!

Leave a Comment