【问题标题】:Large file uploads with FastCGI and Lighttpd使用 FastCGI 和 Lighttpd 上传大文件
【发布时间】:2013-10-26 20:01:40
【问题描述】:

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

当我上传一个小文件 (

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

文件块上传后,日志显示如下:

(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();
    }    
}

提前致谢。

【问题讨论】:

    标签: c fastcgi lighttpd


    【解决方案1】:

    当您提供的地址位置无效时,会出现Write error bad address。

    我不知道 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);
    

    【讨论】:

    • 由于缓冲区被键入为一个数组(字符),我相信没有与号的传递是正确的。奇怪的是,写入确实发生在指定的输出文件中,但仅发生在抛出写入错误之前的前约 500KB。
    • 这个答案是错误的。我同意上面的评论。
    猜你喜欢
    • 1970-01-01
    • 2016-07-16
    • 2011-11-28
    • 2016-11-22
    • 1970-01-01
    • 2012-02-21
    • 2013-04-10
    • 1970-01-01
    • 2010-11-09
    相关资源
    最近更新 更多