c - 在 C 中使用 FastCGI 访问 PUT 或 POST 请求的主体

标签 c fastcgi monkey

我在尝试将我的正文内容放入我的猴子服务器时遇到了一些问题。 事实上,我真的不知道如何使用 fastcgi 库函数访问我的数据。

我正在使用 Postman 在 http://my.server.address:myport/cgi/something 上发送 PUT 请求 具有以下 JSON 内容:

{ "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 做错了什么,这不是访问正文的方法。

如何访问我的请求正文?

最佳答案

看来我的代码泄露了。
这是获取 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);   
    }
}

关于c - 在 C 中使用 FastCGI 访问 PUT 或 POST 请求的主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44302851/

相关文章:

c - 如何在 linux C 的用户空间程序中找到 sigset_t 的补码

python - 为什么我的 python 脚本是下载而不是运行?

android - 猴子在安卓模拟器上给出了一个奇怪的错误

javascript - Eclipse Monkey 文档

c - 如何打开用户指定的COM端口?

c - 在c中声明没有任何数据类型的变量

C : non blocking sockets with timeout : how to avoid busy waiting?

apache - Hostmonster apache fcgi 上的 Redmine 2 : Rails application failed to start properly

django - 使用 django + nginx + flup 时打印语句写到哪里?

android - 在 android 中使用带有几乎所有选项的 monkey 命令的示例