Libfcgi

From Attie's Wiki
Jump to: navigation, search

Simple Sample

#include <stdio.h>
#include <stdlib.h>
 
#include <fcgiapp.h>
 
int main(int argc, char *argv[]) {
  int sock;
  FCGX_Request req;
 
  /* setup libfcgi */
  if (FCGX_Init() != 0) {
    fprintf(stderr, "FCGX_Init() != 0\n");
    return 1;
  }
 
  /* get a socket, and allow 128 clients to queue up */
  if ((sock = FCGX_OpenSocket("/tmp/fastcgi.socket", 128)) == -1) {
    fprintf(stderr, "FCGX_OpenSocket() == -1\n");
    return 1;
  }
 
  /* prepare our request */
  if (FCGX_InitRequest(&req, sock, 0) != 0) {
    fprintf(stderr, "FCGX_InitRequest() != 0\n");
    return 1;
  }
 
  while (FCGX_Accept_r(&req) == 0) {
    printf("Accepted a request!\n");
 
    /* write HTTP headers */
    FCGX_PutS("X-JSON: {\"test\":6}\n", req.out);
 
    /* separate the headers from the content */
    FCGX_PutS("\n", req.out);
 
    /* write HTTP content */
    FCGX_PutS("Hello", req.out);
 
    /* access FCGI paramaters */
    printf("SCRIPT_FILENAME: [%s]\n", FCGX_GetParam("SCRIPT_FILENAME", req.envp));
 
    /* set an exit status, and finish */
    FCGX_SetExitStatus(0, req.out);
    FCGX_Finish_r(&req);
 
    /* prevent libfcgi from accepting new requests... more useful for multithreadded environments
       - it causes FCGX_Accept_r() to fail next time it's called */
    FCGX_ShutdownPending();
  }
 
 
  return 0;
}
Personal tools
Namespaces

Variants
Actions
Navigation
Toolbox