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

标签 c++ c web-applications nginx fastcgi

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

成功从 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>

我的 nginx 配置文件:

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
}

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

有没有更好的快速模块来实现这个?

谢谢。

最佳答案

您可以通过 FCGI_stdin 流访问 POST 主体。例如,您可以使用 FCGI_getchar 一次读取一个字节,它是 FCGI_fgetc(FCGI_stdin) 的缩写形式。您可以使用 FCGI_fread 在一次调用中读取更大的数据 block 。所有这些都是我在看 the source 时发现的.这些消息来源经常引用称为“H&S”的东西——这代表“Harbison and Steele”,这本书的作者 C: A Reference Manual ,数字代表该书的章节。

顺便说一下,它被称为“标准输入/输出”的“stdio”。不是“工作室”。这些函数的大部分行为应该与没有 FCGI_ 前缀的对应函数一样。因此,有关详细信息,请查看 getchar 的手册页。 , fread等等。

一旦您的应用程序中有了字节,您就可以将它们写入文件,使用普通的 stdio 操作或通过 FCGI_fopen 打开的文件。但是请注意,输入流不会直接对应于上传文件的内容。相反,MIME 编码用于传输所有表单数据,包括文件。你必须 parse that stream如果您想访问文件数据。

关于c++ - 如何使用 fastcgi C/C++ 应用程序访问 POST 请求的主体,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12954196/

相关文章:

c++ - OpenCascade中的奇怪段错误

c++ - 变量零初始化 - 是否有未定义的行为

c - malloc 使用什么系统调用?

sql - 临* C : Unable to fetch data from data base

java - 具有不同属性以显示和绑定(bind)的 Vaadin 组合框

java - 将 Web 应用程序和 native 应用程序指向同一数据库/后端?

C++ 纯静态类

c++ - 使用类构造函数作为可调用对象

C加法使用模数

java - 在哪里保存应该可编辑和可下载的 XML?