title: Large file uploads with FastCGI and Lighttpd
body: <p>I am trying to upload files using the fastcgi library (<a href="http://www.fastcgi.com/" rel="nofollow">http://www.fastcgi.com/</a>) with my compiled fcgi application. </p>
  
  <p>When I upload a small file (&lt;500KB) the upload succeeds. However when I upload files larger than 500KB, I get the 503 Error (Service Not Available). I can confirm that the entire file had been uploaded to lighttpd's temp directory in 1MB chunks.</p>
  
  <p>For my testing, the max-request-size was set to 30MB, and my test file was 14MB in size.</p>
  
  <p>Immediately after the file chunks had been uploaded, the log showed the following:</p>
  
  <pre><code>(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
  </code></pre>
  
  <p>And the assembled upload file was ~500KB.</p>
  
  <p>Could any one please shed some light on this for me? </p>
  
  <p>My fcgi application is listed below:</p>
  
  <pre><code>#include "fcgi_stdio.h"
  #include &lt;stdlib.h&gt;
  #include &lt;fcntl.h&gt;
  #include &lt;sys/types.h&gt;
  #include &lt;sys/stat.h&gt;
  
  
  void main(void)
  {
  
      int count = 0;
      while(FCGI_Accept() &gt;= 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"
                 "&lt;title&gt;FastCGI Hello!&lt;/title&gt;"
                 "&lt;h1&gt;FastCGI Hello!&lt;/h1&gt;"
                 "Request number %d running on host &lt;i&gt;%s&lt;/i&gt;\n",
                  ++count, getenv("SERVER_NAME"));
  
          printf("&lt;br /&gt;CONTENT_LENGTH = %d &lt;br /&gt;\r\n", len);
          printf("&lt;form enctype='multipart/form-data' method='post' action='?'&gt;&lt;input type='text' name='text1' /&gt;&lt;input type='file' name='file1'/&gt;&lt;input type='submit' /&gt;&lt;/form&gt;");
          printf("&lt;hr /&gt;");
  
          fflush(stdout);
  
          FCGI_FILE * fileOut = FCGI_fopen("/tmp/fcgi.out", "w");
          if (fileOut) {
              int done = 0;
              while(done &lt; len) {
                  char buffer[1024];
                  int i;
                  int packetRead;
  
                  packetRead = FCGI_fread(buffer, 1, sizeof(buffer), stdin);
                  if (packetRead &lt; 0) {
                      break;
                  }
                  if (packetRead &gt; 0) {
                      FCGI_fwrite(buffer, 1, packetRead, fileOut);
                      done += packetRead;
                  }
  
  
              }
              FCGI_fclose(fileOut);
          }
  
          FCGI_Finish();
      }    
  }
  </code></pre>
  
  <p>Thanks in advance.</p>