2017-06-01 169 views
0

我正面临着一些问题,试图让我的身体内容在我的猴子服务器。实际上,我并不知道如何使用fastcgi库函数来访问我的数据。使用C中的FastCGI访问PUT或POST请求的主体C

我跟邮递员发送具有以下JSON内容上http://my.server.address:myport/cgi/something PUT请求:

{ "hello" : "bonjour" } 

在服务器端
主线程上运行:

int32_t SWEB_traiter_requette_f(int32_t i32_socket_listen) { 
    FCGX_Request st_request; 

    (void)FCGX_InitRequest(&st_request, i32_socket_listen, 0); 

    if (FCGX_Accept_r(&st_request) == 0) { 

     ULOG_log_f(ULOG_PRIO_INFO, "accepting request"); 
     (void)pthread_mutex_lock(gpu_mutex_s_web); 
     traiter_requette_s_web_f(&st_request,FCGX_GetParam("REQUEST_URI", st_request.envp)); 
     (void)pthread_mutex_unlock(gpu_mutex_s_web); 


     (void)FCGX_Finish_r(&st_request); 
    } 
    FCGX_Free(&st_request,i32_socket_listen); 

    return 0; 
} 

这就是我处理请求的方式:

static void traiter_requette_s_web_f(FCGX_Request *pst_request,const char * pc_url) { 
    size_t z_len; 
    char_t *pc_data; 
    int i = 0; 
    int ch; 
    char_t *sz_content_len = FCGX_GetParam("CONTENT_LENGTH" , pst_request->envp); 
    char_t *sz_method  = FCGX_GetParam("REQUEST_METHOD" , pst_request->envp); 
    char_t *sz_contenttype = FCGX_GetParam("CONTENT_TYPE" , pst_request->envp); 

    if (sz_contenttype != NULL){ 

     /* first method ..... not working */ 
     ch = FCGX_GetChar(pst_request->in); 
     while(ch != -1){ 
      i++; 
      z_len = strtol(sz_content_len, NULL, 10); 
      pc_data = calloc(1,z_len+1); 
      if (pc_data == NULL){ 
       //LCOV_EXCL_START 
       ULOG_log_f(ULOG_PRIO_ERREUR, "Erreur d'allocation de psz_data"); 
       return; 
       //LCOV_EXCL_STOP 
      } 
      pc_data[i-1] = (char_t) ch; 
      ch = FCGX_GetChar(pst_request->in); 
      if (ch == -1) 
      { 
       pc_data=(char*)realloc(pc_data,(i + 1)*sizeof(char)); 
       pc_data[i] = '\0'; 
      } 

     } 
     printf("data !! : %s\n",pc_data); 
     /* second method .... not working */ 

     z_len = strtol(sz_content_len, NULL, 10); 
     pc_data = calloc(1,z_len+1); 
     if (pc_data == NULL){ 
      //LCOV_EXCL_START 
      ULOG_log_f(ULOG_PRIO_ERREUR, "Erreur d'allocation de psz_data"); 
      return; 
      //LCOV_EXCL_STOP 
     } 
     (void)FCGX_GetStr(pc_data,z_len,pst_request->in); 
     printf("data !! : %s\n",pc_data); 

    } 
} 

也许我做错了pc_data,这不是如何访问正文。

我怎样才能访问我的请求?

+0

'...,FCGX_GetParam( “REQUEST_URI”,st_request.envp)...'::什么是'st_request.envp' ,一个指针? – joop

+0

通常情况下,PUT和POST数据通过标准输入发送,因此您必须访问该数据。但确定这个猴子图书馆能够访问它。 –

+0

'while(ch!= -1){++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ z_len = strtol(sz_content_len,NULL,10); pc_data = calloc(1,z_len + 1);'看起来你在'(ch!= EOF)'循环中。调用每个字符的calloc看起来像是一个内存泄漏给我。然后再为每个角色计算'content_length'似乎也不正确。 – joop

回答

0

看来我的代码泄漏了。
这是你上一个POST请求的主体内容:

static void traiter_requette_s_web_f(FCGX_Request *pst_request,const char * pc_url) { 
    size_t z_len; 

    int i = 0; 
    int ch; 
    char_t *sz_content_len = FCGX_GetParam("CONTENT_LENGTH" , pst_request->envp); 
    char_t *sz_method  = FCGX_GetParam("REQUEST_METHOD" , pst_request->envp); 
    char_t *sz_contenttype = FCGX_GetParam("CONTENT_TYPE" , pst_request->envp); 
    char_t *pc_data = FCGX_GetParam("REQUEST_URI", pst_request->envp); 

    if (sz_contenttype != NULL) 
    { 

     z_len = strtol(sz_content_len, NULL, 10); 
     pc_data = calloc(1,z_len+1); 
     if (pc_data == NULL){ 
      return; 
     } 
     ch = FCGX_GetChar(pst_request->in); 
     while(ch != -1){ 
      i++; 
      pc_data[i-1] = (char_t) ch; 
      ch = FCGX_GetChar(pst_request->in); 
      if (ch == -1) 
      { 
       pc_data=(char*)realloc(pc_data,(i + 1)*sizeof(char)); 
       pc_data[i] = '\0'; 
      } 

     }   
     printf("data !! : %s\n",pc_data); 
    } 
} 
+0

消毒您的循环。上面的代码可以被发送比指定的content_length多的字节的客户端拷贝致死。所以你的循环应该在leas包含一个测试'i joop