Skip to content

Instantly share code, notes, and snippets.

@llxier
Forked from dermesser/libfcgi-example.c
Created January 23, 2017 16:49
Show Gist options
  • Save llxier/5d2bec923bd85324d4966a261382793c to your computer and use it in GitHub Desktop.
Save llxier/5d2bec923bd85324d4966a261382793c to your computer and use it in GitHub Desktop.

Revisions

  1. @dermesser dermesser created this gist Jul 3, 2014.
    80 changes: 80 additions & 0 deletions libfcgi-example.c
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,80 @@
    # include <stdlib.h>
    # include <stdio.h>

    # include <sys/stat.h>

    # include <pthread.h>

    # include <fcgiapp.h>

    const char* const sockpath = "/tmp/fcgicpp.sock";

    void* start_fcgi_worker(void* arg);

    struct FCGI_Info
    {
    int fcgi_fd;
    };

    int main(void)
    {
    int fcgifd = FCGX_OpenSocket(sockpath,128);

    chmod(sockpath,0777);

    if ( 0 > fcgifd )
    {
    printf("Error opening socket\n");
    exit(1);
    }

    /*
    if ( FCGX_IsCGI() )
    {
    printf("Please run this process as FastCGI process.\n");
    exit(1);
    }
    */

    const unsigned int n_threads = 4;

    pthread_t threads[n_threads];

    struct FCGI_Info info;
    info.fcgi_fd = fcgifd;

    for ( unsigned int i = 0; i < n_threads; i++ )
    {
    pthread_create(&threads[i],NULL,start_fcgi_worker,(void*)&info);
    }

    // Wait indefinitely
    for ( unsigned int i = 0; i < n_threads; i++ )
    {
    pthread_join(threads[i],NULL);
    }

    return 0;
    }

    void* start_fcgi_worker(void* arg)
    {
    struct FCGI_Info* info = (struct FCGI_Info*)arg;

    FCGX_Init();

    FCGX_Request request;

    FCGX_InitRequest(&request,info->fcgi_fd,0);

    while ( 1 )
    {
    FCGX_Accept_r(&request);

    FCGX_PutStr("Content-type: text/plain\r\n\r\n",28,request.out);
    FCGX_PutStr("Hey!\n",5,request.out);

    FCGX_Finish_r(&request);
    }

    }