如何使用FastCGI C/C 应用程序访问POST请求的主体
how to access a body of POST request using fastcgi C/C++ application
我正在使用http://fastcgi.com/在C 应用程序中使用库作为后端,而Nginx Web-Server作为前端。
成功地从HTML形式发布文件,可以在Nginx服务器端看到临时文件。但是我不知道如何使用fastcgi_stdio访问多部分邮政请求的主体。这是我的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 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
}
现在,如何在我的fastcgi C 应用程序中处理/获取POST请求主体,以及如何在Fastcgi应用程序端的适当文件中写入它?
是否有更好的快速模块可以实现这一目标?
谢谢。
您可以通过FCGI_stdin
流访问POST
主体。例如,您可以使用FCGI_getchar
一次从中读取一个字节,这是FCGI_fgetc(FCGI_stdin)
的简短表格。您可以使用FCGI_fread
在单个调用中读取较大的数据。我发现所有这些都在看源。这些来源通常会引用所谓的" H&amp; s"的内容 - 这代表" Harbison and Steele",《书C:参考手册》的作者,这些数字是指该书的章节和部分。
,顺便说一句,它称为"标准输入/输出"的" stdio"。不是"工作室"。这些功能的行为应像同行一样,没有FCGI_
前缀。因此,有关详细信息,请查看getchar
,fread
等的人页。
在应用程序中包含字节后,您可以使用普通的STDIO操作或通过FCGI_fopen
打开的文件将其写入文件。但是请注意,输入流将不会直接对应上传文件的内容。相反,MIME编码用于传输所有表单数据,包括文件。如果要访问文件数据,则必须分析该流。
使用以下:
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>%sn", bufp);
free(bufp);
}
相关文章:
- 如何使用boost/beast从HTTP POST请求中解析和提取有效负载?
- 如何使用 curl 将 POST 请求从 python 重写为 C++
- 从简单的 POST 请求中解析异常
- 使用 cpp-httplib 的 POST 请求
- C++:如何通过 curl 调用使用 HTTP post 请求发送二进制数据(protobuf 数据)
- C++中的 cURL POST 请求
- C++ Schannel POST 400 错误代码错误请求
- c++curl返回413请求实体过大,但是post大小远小于max大小
- HTTP POST 请求不允许我定义上下文类型
- 如何在c++中使用libcurl发送POST请求并接收它
- 我想将 POST 请求从 arduino 发送到 mysql 工作台并保存值
- 如何捕获从 MFC Web 浏览器 c++ 发出的 GET/POST 请求
- 如何解决来自 arduino 的 post 请求的问题,在失败之前只收到 2-3 次
- C HTTP POST 400不良请求
- 使用 restbed C++发送 POST 多部分/表单数据请求
- C++ 对 nodejs 服务器的 POST 请求失败(基于套接字)
- Qt Post请求正在执行但失败
- 使用C REST SDK(Casablanca)的HTTP_CLIENT POST请求
- 使用CPP手动构建POST请求,并使用它提交php表单
- 如何在 qt main() 中运行 post 请求