如何使用fastcgi C/C++应用程序访问POST请求的主体

use*_*653 11 c c++ fastcgi web-applications nginx

我在http://fastcgi.com/中使用C++应用程序中的库作为后端,使用nginx web服务器作为前端.

成功发布HTML表单中的文件,可以在nginx服务器端查看临时文件.但我无法弄清楚如何使用fastcgi_stdio访问多部分POST请求的主体.这是我的HTML表单.

<html>
    <head>
        <title>Test Server</title>
        <script src="http://code.jquery.com/jquery.min.js"></script>
    </head>
    <body>
        <form id="upload-form" method="post" target="upload_target"   enctype="multipart/form-data" action="/upload">
            <input name="file" id="file" size="27" type="file" /><br />
            <input type="submit" name="action" value="Upload" /><br />
            <iframe id="upload_target" name="upload_target" src="" style="width:100%;height:100px;border:0px solid #fff;"></iframe>
        </form>
    </body>
</html>
Run Code Online (Sandbox Code Playgroud)

我的nginx conf文件:

location /upload {

# Pass altered request body to this location
upload_pass @test;

# Store files to this directory
# The directory is hashed, subdirectories 0 1 2 3 4 5 6 7 8 9 should exist
upload_store /www/test;

# Allow uploaded files to be read only by user
upload_store_access user:rw group:r all:r;

# Set specified fields in request body
upload_set_form_field $upload_field_name.name $upload_file_name;
upload_set_form_field $upload_field_name.content_type "$upload_content_type";
upload_set_form_field $upload_field_name.path "$upload_tmp_path";

# Inform backend about hash and size of a file
upload_aggregate_form_field "$upload_field_name.md5" "$upload_file_md5";
upload_aggregate_form_field "$upload_field_name.size" "$upload_file_size";

upload_pass_form_field "^submit$|^description$";
upload_cleanup 400 404 499 500-505;
}

include fastcgi.conf;

# Pass altered request body to a backend
location @test {
        fastcgi_pass  localhost:8080
}
Run Code Online (Sandbox Code Playgroud)

现在,如何处理/获取我的fastcgi c ++应用程序中的POST请求主体以及如何在fastcgi应用程序端的适当文件中编写它?

有没有更好的快速模块来实现这一目标?

谢谢.

MvG*_*MvG 12

您可以POST通过FCGI_stdin流访问身体.例如,您可以使用一个字节一次读取它FCGI_getchar,这是一个简短形式FCGI_fgetc(FCGI_stdin).您可以使用单个调用读取更大的数据块FCGI_fread.所有这些我找到了源头.这些来源经常引用一些名为"H&S"的东西 - 这代表"Harbison and Steele",这是C:A参考手册一书的作者,这些数字指的是该书的章节.

顺便说一句,它被称为"标准输入/输出"的"stdio".不是"工作室".这些函数应该主要表现得像没有FCGI_前缀的对应函数.因此,有关详细信息,请查看手册页getchar,fread等等.

在应用程序中有字节后,可以使用普通的stdio操作或通过打开的文件将它们写入文件FCGI_fopen.但请注意,输入流不会直接对应于上载文件的内容.相反,MIME编码用于传输所有表单数据,包括文件.如果要访问文件数据,则必须解析该流.


Bru*_*uno 5

用这个:

char * method = getenv("REQUEST_METHOD");
if (!strcmp(method, "POST")) {
    int ilen = atoi(getenv("CONTENT_LENGTH"));
    char *bufp = malloc(ilen);
    fread(bufp, ilen, 1, stdin);
    printf("The POST data is<P>%s\n", bufp);
    free(bufp);
}
Run Code Online (Sandbox Code Playgroud)