gpt4 book ai didi

c - 使用 FastCGI 和 Lighttpd 上传大文件

转载 作者:太空宇宙 更新时间:2023-11-04 05:00:12 27 4
gpt4 key购买 nike

我正在尝试使用 fastcgi 库 ( http://www.fastcgi.com/ ) 和我编译的 fcgi 应用程序上传文件。

当我上传一个小文件 (<500KB) 时,上传成功。但是,当我上传大于 500KB 的文件时,我会收到 503 错误(服务不可用)。我可以确认整个文件已以 1MB block 的形式上传到 lighttpd 的临时目录。

对于我的测试,最大请求大小设置为 30MB,我的测试文件大小为 14MB。

文件 block 上传后,日志立即显示如下:

(mod_fastcgi.c.3058) got proc: pid: 2171 socket: unix:/tmp/fcgi.sock-0 load: 1
(network_writev.c.303) write failed: Bad address 7
(mod_fastcgi.c.3098) write failed: Bad address 14
(mod_fastcgi.c.1490) released proc: pid: 2171 socket: unix:/tmp/fcgi.sock-0 load: 0

组装后的上传文件约为 500KB。

任何人都可以为我解释一下吗?

下面列出了我的 fcgi 应用程序:

#include "fcgi_stdio.h"
#include <stdlib.h>
#include <fcntl.h>
#include <sys/types.h>
#include <sys/stat.h>


void main(void)
{

int count = 0;
while(FCGI_Accept() >= 0) {
char *contentLength = getenv("CONTENT_LENGTH");
int len;

if (contentLength != NULL) {
len = strtol(contentLength, NULL, 10);
}
else {
len = 0;
}
printf("Content-type: text/html\r\n"
"\r\n"
"<title>FastCGI Hello!</title>"
"<h1>FastCGI Hello!</h1>"
"Request number %d running on host <i>%s</i>\n",
++count, getenv("SERVER_NAME"));

printf("<br />CONTENT_LENGTH = %d <br />\r\n", len);
printf("<form enctype='multipart/form-data' method='post' action='?'><input type='text' name='text1' /><input type='file' name='file1'/><input type='submit' /></form>");
printf("<hr />");

fflush(stdout);

FCGI_FILE * fileOut = FCGI_fopen("/tmp/fcgi.out", "w");
if (fileOut) {
int done = 0;
while(done < len) {
char buffer[1024];
int i;
int packetRead;

packetRead = FCGI_fread(buffer, 1, sizeof(buffer), stdin);
if (packetRead < 0) {
break;
}
if (packetRead > 0) {
FCGI_fwrite(buffer, 1, packetRead, fileOut);
done += packetRead;
}


}
FCGI_fclose(fileOut);
}

FCGI_Finish();
}
}

提前致谢。

最佳答案

当您提供的地址位置无效时,会出现写入错误错误地址。

我不知道 FCGI_fread 和 FCGI_fwrite,但是对于它们的 POSIX 等价物

FCGI_fread(buffer, 1, sizeof(buffer), stdin);
FCGI_fwrite(buffer, 1, packetRead, fileOut);

应该是

FCGI_fread(&buffer, 1, sizeof(buffer), stdin);
FCGI_fwrite(&buffer, 1, packetRead, fileOut);

可能有帮助。

第二个:

size_t     FCGI_fread(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp);
size_t FCGI_fwrite(void *ptr, size_t size, size_t nmemb, FCGI_FILE *fp);

可能会更好?:

FCGI_fread(&buffer, sizeof(char), sizeof(buffer), stdin);
FCGI_fwrite(&buffer, sizeof(char), packetRead, fileOut);

关于c - 使用 FastCGI 和 Lighttpd 上传大文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/19441822/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com