使用FastCGI和Lighttpd上传大文件

时间:2013-10-18 04:53:12

标签: c fastcgi lighttpd

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

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

对于我的测试,max-request-size设置为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();
    }    
}

提前致谢。

1 个答案:

答案 0 :(得分:-1)

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

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