c - 从网络服务器读取标准输入

标签 c cgi

我想在 LINUX (SLES64) 上的 C CGI 程序中处理 POST 数据

第一步,我获取带有内容长度的 HTTP header 。

现在我想从标准输入读取 POST 数据。

我想读取最多 5000 字节的 cgi 数据。每个包含更多内容的帖子都会被忽略!

number_of_bytes_read = read(0, buffer, content_length); 

现在有一个特殊情况:

用户在浏览器中填写表单,然后单击“提交”。

Apache Web 服务器接收请求 header 和表单数据。

Apache 获取请求 header “content-length”,在本例中为 4875 字节,它还获取 4875 字节长的“字节流”。

现在 Apache 创建一个新进程,最终将处理 cgi 数据。

Apache 必须将 4875 字节的 cgi 数据交给新进程。

他是怎么做到的?

他是否正在执行进程间通信,Apache 是否正在为新进程创建套接字?

在这种情况下,另一方的 cgi 程序可能会遇到以下问题:

CGI程序获取内容长度,想要一次性读取4875字节,但是由于 网络延迟获取第一次只有4300字节。

但是如果 Apache 没有为新的 cgi 进程创建套接字(进程间通信),则可能 难道cgi程序也没有在第一时间抓取所有字节?

最佳答案

由于网络延迟等因素,读取可能不会返回5000字节。您想要读取直到读取到 EOF,或者读取的字节数达到 5000,或者 read 返回错误代码,例如通过在循环中调用 read 。来自 read(2)联机帮助页:

On success, the number of bytes read is returned (zero indicates end of file), and the file position is advanced by this number. It is not an error if this number is smaller than the number of bytes requested; this may happen for example because fewer bytes are actually available right now (maybe because we were close to end-of-file, or because we are reading from a pipe, or from a terminal), or because read() was interrupted by a signal. On error, -1 is returned, and errno is set appropriately. In this case it is left unspecified whether the file position (if any) changes.

关于c - 从网络服务器读取标准输入,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26216577/

相关文章:

perl - 无法运行 cgi,仅显示纯文本 (Ubuntu 13.10 Apache 2.4)

python - Google App Engine 中的 cgi.test()

forms - 在 Perl 脚本中混合 POST 值

c - 最小化控制台屏幕

c - 多个以太网接口(interface) - 如何创建一个单独的网络并从 C 代码访问

C: int 或 unsigned int 我用于指针增加的类型

c++ - 一次从套接字读取 1 个字节与读取大块

Perl 抛出 "keys on reference is experimental"

C 中的连续空格去除

c - 静态链接 OpenSSL 正在尝试加载 DLL