linux - CGI 环境,读取最大大小是固定的?

标签 linux apache cgi

在Apache2提供的CGI环境中,我使用了一个用C写的cgi可执行文件

cgi.c :

#define         MY_SIZE                 102400
#define         MY_SIZE_OUT             (2 + (MY_SIZE * 2))

    int main()
{
char          buf[SIZE + 2];
int           n;

if (write(1, "Content-type: text/html\n\n", 25) == -1)
        {
          puts("An internal error occured, please report the bug #005");
          goto EXIT;
        }
      if ((n = read(0, buf, MY_SIZE)) == -1)
        {
          puts("An internal error occured, please report the bug #004");
          goto EXIT;
        }

      buf[n] = '\n';
      buf[n + 1] = 0;

      printf("Size of input = %i |--| |%s| max size = %i\n", n, buf, MY_SIZE);

     EXIT:
      return 1;
}

我的 POST 请求是通过 Ajax 代码发送的:

ajaxRequest.onreadystatechange = function(){
            if(ajaxRequest.readyState == 4){
                var text;
                var exp;
            text = ajaxRequest.responseText;

            exp = new RegExp("([^ ]+)", "g");
            text = text.replace(exp, "<a class='wd' onClick='man_wd(this);'>$1</a>");
            document.getElementById("out").innerHTML = text;
            }
 }

  document.getElementById("out").innerHTML = "Processing...";
  ajaxRequest.open("POST", "cgi-bin/a.cgi", true);
  ajaxRequest.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
 ajaxRequest.setRequestHeader("Content-length", 5 + document.forms['f_main'].ta_in.value.length);
 ajaxRequest.setRequestHeader("Connection", "close");

  ajaxRequest.send(getPrefix() + " " + document.forms['f_main'].ta_in.value);

我的问题是,当我发送一个超过 N 个字符的请求时,我的 CGI 可执行文件最多读取 1060 个字符,即使 MY_SIZE 值为 102400。

我遇到了 php.ini 但 POST 大小限制为 8Mb

有人有想法吗?

最佳答案

来自阅读联机帮助页:

read() attempts to read up to count bytes from file descriptor fd into the buffer starting at buf.

因此可能需要多次调用 read。

Unix Network programming第 1 卷,Stevens 建议阅读此包装:

ssize_t readn (int fd, void *vptr, size_t n)
{
    size_t ret;
    size_t nleft;
    ssize_t nread;
    char *ptr;

    /* Initialize local variables */
    ptr = (char*)vptr;
    nleft = n;
    while (nleft > 0)
    {
        if ((nread = read (fd, ptr, nleft)) < 0)
        {
            if (errno == EINTR)
            {
                /* Read interrupted by system call, call read() again */
                nread = 0;
            }
            else
            {
                /* Other type of errors, return */
                return -1;
            }
        }
        else if (nread == 0)
        {
            /* No more data to read, exit */
            break;
        }

        /* Update counters and call read again */
        nleft -= nread;
        ptr += nread;
    }
    ret = n - nleft;

    return ret;
}

它会多次调用 read,只有在读取到所需数量的数据或输入真正结束时才会停止。

关于linux - CGI 环境,读取最大大小是固定的?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/13823755/

相关文章:

cgi - WebSocket 和 CGI​​/FastCGI/SCGI 协议(protocol)

linux - Ubuntu vivid box 没有与 vagrant 一起运行

c - 如何验证 Linux radio 设备驱动程序中的 radio 频率?

virtualhost - 我想为图像创建一个单独的域

regex - 一系列没有 Last [L] Flag 的重写规则

apache - Nginx 如何处理 HTTP 请求?

php - 使用 PHP 的 Http 身份验证不起作用

linux - 如何在 Linux 中用韩文字符对文件进行排序?

linux - 如何等待变量的值在特定时间间隔之前设置为 true

php - 如何使 C 中的 system() 命令不在屏幕上显示任何内容?