2009-12-22 45 views
3

我已經配置了一個Web服務器,通過命名管道使用'遠程'fastCGI應用程序(它實際上在同一個Windows主機上)。我現在試圖找出如何啓動fastCGI應用程序來使用這個管道,但我不確定這應該如何完成。其他操作系統似乎有spawn-fcgi實用程序用於執行此操作,但對於Windows似乎沒有任何類似的操作。如何在Windows上手動啓動fastCGI應用程序?

這是我的APP:

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

int main(int argc, char ** argv) 
{ 
    while (FCGI_Accept() >= 0) { 
    printf("Content-type: text/html\r\n" 
     "\r\n" 
     "<title>Web Services Interface Module</title>" 
     "<h1>Web Services Interface Module</h1>\n"); 
    } 
    return(0); 
} 

出於興趣我使用的深淵Web服務器,雖然我希望不會對答案的軸承。

問候

回答

2

的FCGI界面不會讓你這樣做,而是使用FCGX接口。 調用FCGX_Open_Socket以在特定端口上偵聽例如9345或命名管道。

FCGX_OpenSocket(":9345", 500); 

然後你不需要使用像spawn_fcgi這樣的工具來啓動你的應用程序。在fcgiapp.c

+0

+1:謝謝。但是在代碼中的什麼地方應該被調用? – 2013-04-19 08:35:20

1
/* 
*---------------------------------------------------------------------- 
* 
* FCGX_OpenSocket -- 
* 
* Create a FastCGI listen socket. 
* 
* path is the Unix domain socket (named pipe for WinNT), or a colon 
* followed by a port number. e.g. "/tmp/fastcgi/mysocket", ":5000" 
* 
* backlog is the listen queue depth used in the listen() call. 
* 
* Returns the socket's file descriptor or -1 on error. 
* 
*---------------------------------------------------------------------- 
*/ 
DLLAPI int FCGX_OpenSocket(const char *path, int backlog); 

默認情況下libfcgi從標準輸入讀取。所以重新打開stdin句柄作爲管道。

dup2(FCGX_OpenSocket("pipe name", 5),0); 
1

變化FCGX_Init

int FCGX_Init(void) 
{ 
    char *p; 

    int listen_socket; 

    if (libInitialized) { 
     return 0; 
    } 

    if (OS_LibInit(NULL) == -1) { 
     return OS_Errno ? OS_Errno : -9997; 
    } 

    /*sureone socket*/ 
     /* 9010 is your listen port*/ 
    listen_socket = FCGX_OpenSocket(":9010", 400); 
    if(listen_socket < 0) exit(1); 
    printf("FCGX_InitRequest...\n"); 
    FCGX_InitRequest(&the_request, listen_socket, 0); 
    /*end sureone*/ 

    //FCGX_InitRequest(&the_request, FCGI_LISTENSOCK_FILENO, 0); 



    p = getenv("FCGI_WEB_SERVER_ADDRS"); 
    webServerAddressList = p ? StringCopy(p) : NULL; 

    libInitialized = 1; 
    return 0; 
} 
+0

1+太好了!它的工作,但它應該是改變fcgi代碼的另一種方式? – 2013-04-19 08:34:10

0

我拿出剛在Windows工作對我來說代碼:

int main() 
{ 
    char **initialEnv = environ; //Keep track of initial environment 
    int count = 0;   
    int listenSocket; 


    //It's ugly, but in Windows we need to initialize the 
    //socket library. We can do it by calling 
    //libfcgi's OS_LibInit() function 
    OS_LibInit(NULL); 

    //Open a socket. Here, we use localhost:9000 
    listenSocket = FCGX_OpenSocket("localhost:9000", 5); 
    if (listenSocket < 0) { 
     exit(1); 
    } 

    FCGX_Request request; 
    FCGX_Init(); 
    FCGX_InitRequest(&request, listenSocket, 0); 


    while (FCGX_Accept_r(&request) >= 0) { 
     //Init I/O streams wrapper as well as set the new environment 
     FCGI_stdin->stdio_stream = NULL; 
     FCGI_stdin->fcgx_stream = request.in; 
     FCGI_stdout->stdio_stream = NULL; 
     FCGI_stdout->fcgx_stream = request.out; 
     FCGI_stderr->stdio_stream = NULL; 
     FCGI_stderr->fcgx_stream = request.err; 
     environ = request.envp; 

     //Funny stuff 
     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, Process ID: %d<p>\n", ++count, getpid()); 

     if (contentLength != NULL) { 
      len = strtol(contentLength, NULL, 10); 
     } 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"); 
     } 

    } /* while */  

    return 0; 
} 
相關問題