在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个字符的请求时,即使MY_SIZE值为102400,我的CGI可以读取最多1060个字符。
我来自php.ini,但限制POST大小是8Mb
有人有想法吗?
答案 0 :(得分:0)
来自read
联机帮助页:
read()尝试读取从文件描述符fd到的字节数 缓冲区从buf开始。
因此可能需要多次调用read。
在Unix Network programming第1卷中,史蒂文斯建议阅读这个包装:
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;
}
多次调用读取,只读取所需数量的数据停止或输入真正结束。