Documentation Source Text

Check-in [6faf1e47b9]
Login

Many hyperlinks are disabled.
Use anonymous login to enable hyperlinks.

Overview
Comment:Fixes to the CGI handling in althttpd.c.
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256:6faf1e47b91142fb7e9a39505a8fd79b2bf18f95a3078e04c145b679b4325f21
User & Date: drh 2018-08-08 17:36:12
Context
2018-08-15
13:21
Pre-expand key railroad diagrams in the windowfunctions.html page. check-in: 61483bc834 user: drh tags: trunk
2018-08-08
17:36
Fixes to the CGI handling in althttpd.c. check-in: 6faf1e47b9 user: drh tags: trunk
2018-08-06
01:42
Fix typos in the windowfunction documentation. check-in: ab3b52646c user: drh tags: trunk
Changes
Hide Diffs Unified Diffs Ignore Whitespace Patch

Changes to misc/althttpd.c.

785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
....
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
....
1609
1610
1611
1612
1613
1614
1615

1616
1617
1618
1619
1620
1621
1622
....
1692
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
....
1717
1718
1719
1720
1721
1722
1723

1724
1725

1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
....
1783
1784
1785
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
static int CheckBasicAuthorization(const char *zAuthFile){
  FILE *in;
  char *zRealm = "unknown realm";
  char *zLoginPswd;
  char *zName;
  char zLine[2000];

  in = fopen(zAuthFile, "r");
  if( in==0 ){
    NotFound(150);  /* LOG: Cannot open -auth file */
    return 0;
  }
  if( zAuthArg ) Decode64(zAuthArg);
  while( fgets(zLine, sizeof(zLine), in) ){
    char *zFieldName;
................................................................................
    }
    sprintf(zTmpNamBuf, "/tmp/-post-data-XXXXXX");
    zTmpNam = zTmpNamBuf;
    if( mkstemp(zTmpNam)<0 ){
      Malfunction(280,  /* LOG: mkstemp() failed */
               "Cannot create a temp file in which to store POST data");
    }
    out = fopen(zTmpNam,"w");
    if( out==0 ){
      StartResponse("500 Cannot create /tmp file");
      nOut += printf(
        "Content-type: text/plain\r\n"
        "\r\n"
        "Could not open \"%s\" for writing\n", zTmpNam
      );
................................................................................
      { "SCRIPT_NAME",                 &zRealScript },
      { "SERVER_NAME",                 &zServerName },
      { "SERVER_PORT",                 &zServerPort },
      { "SERVER_PROTOCOL",             &zProtocol },
    };
    char *zBaseFilename;         /* Filename without directory prefix */
    int seenContentLength = 0;   /* True if Content-length: header seen */

    int nRes = 0;                /* Bytes of payload */
    int nMalloc = 0;             /* Bytes of space allocated to aRes */
    char *aRes = 0;              /* Payload */

    /* If its executable, it must be a CGI program.  Start by
    ** changing directories to the directory holding the program.
    */
................................................................................
        }
        close(px[1]);
        for(i=3; close(i)==0; i++){}
        execl(zBaseFilename, zBaseFilename, (char*)0);
        exit(0);
      }
      close(px[1]);
      in = fdopen(px[0], "r");
    }
    if( in==0 ){
      CgiError();
    }

    /* Read and process the first line of the header returned by the
    ** CGI script.
................................................................................
        int i;
        for(i=7; isspace(zLine[i]); i++){}
        nOut += printf("%s %s", zProtocol, &zLine[i]);
        strncpy(zReplyStatus, &zLine[i], 3);
        zReplyStatus[3] = 0;
        statusSent = 1;
      }else{

        if( strncasecmp(zLine, "Content-length:", 14)==0 ){
          seenContentLength = 1;

        }
        StartResponse("200 OK");
        nOut += printf("%s",zLine);
      }
    }

    /* Copy everything else thru without change or analysis.
    */
    StartResponse("200 OK");
    if( useTimeout ) alarm(60*5);
    if( seenContentLength ){
      nOut += printf("%s", zLine);
      while( (c = getc(in))!=EOF ){
        putc(c,stdout);
        nOut++;
      }
    }else{
      nRes = 0;
      nMalloc = 1000;
      aRes = malloc(nMalloc+1);
................................................................................
      nOut += printf("Cache-Control: max-age=%d\r\n", mxAge);
      nOut += printf("ETag: \"%s\"\r\n", zETag);
      nOut += printf("\r\n");
      fflush(stdout);
      MakeLogEntry(0, 470);  /* LOG: ETag Cache Hit */
      return;
    }
    in = fopen(zFile,"r");
    if( in==0 ) NotFound(480); /* LOG: fopen() failed for static content */
    StartResponse("200 OK");
    nOut += DateTag("Last-Modified", statbuf.st_mtime);
    nOut += printf("Cache-Control: max-age=%d\r\n", mxAge);
    nOut += printf("ETag: \"%s\"\r\n", zETag);
    nOut += printf("Content-type: %s\r\n",zContentType);
    nOut += printf("Content-length: %d\r\n\r\n",(int)statbuf.st_size);







|







 







|







 







>







 







|







 







>
|

>












|







 







|







785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
....
1393
1394
1395
1396
1397
1398
1399
1400
1401
1402
1403
1404
1405
1406
1407
....
1609
1610
1611
1612
1613
1614
1615
1616
1617
1618
1619
1620
1621
1622
1623
....
1693
1694
1695
1696
1697
1698
1699
1700
1701
1702
1703
1704
1705
1706
1707
....
1718
1719
1720
1721
1722
1723
1724
1725
1726
1727
1728
1729
1730
1731
1732
1733
1734
1735
1736
1737
1738
1739
1740
1741
1742
1743
1744
1745
1746
1747
1748
....
1786
1787
1788
1789
1790
1791
1792
1793
1794
1795
1796
1797
1798
1799
1800
static int CheckBasicAuthorization(const char *zAuthFile){
  FILE *in;
  char *zRealm = "unknown realm";
  char *zLoginPswd;
  char *zName;
  char zLine[2000];

  in = fopen(zAuthFile, "rb");
  if( in==0 ){
    NotFound(150);  /* LOG: Cannot open -auth file */
    return 0;
  }
  if( zAuthArg ) Decode64(zAuthArg);
  while( fgets(zLine, sizeof(zLine), in) ){
    char *zFieldName;
................................................................................
    }
    sprintf(zTmpNamBuf, "/tmp/-post-data-XXXXXX");
    zTmpNam = zTmpNamBuf;
    if( mkstemp(zTmpNam)<0 ){
      Malfunction(280,  /* LOG: mkstemp() failed */
               "Cannot create a temp file in which to store POST data");
    }
    out = fopen(zTmpNam,"wb");
    if( out==0 ){
      StartResponse("500 Cannot create /tmp file");
      nOut += printf(
        "Content-type: text/plain\r\n"
        "\r\n"
        "Could not open \"%s\" for writing\n", zTmpNam
      );
................................................................................
      { "SCRIPT_NAME",                 &zRealScript },
      { "SERVER_NAME",                 &zServerName },
      { "SERVER_PORT",                 &zServerPort },
      { "SERVER_PROTOCOL",             &zProtocol },
    };
    char *zBaseFilename;         /* Filename without directory prefix */
    int seenContentLength = 0;   /* True if Content-length: header seen */
    int contentLength = 0;       /* The content length */
    int nRes = 0;                /* Bytes of payload */
    int nMalloc = 0;             /* Bytes of space allocated to aRes */
    char *aRes = 0;              /* Payload */

    /* If its executable, it must be a CGI program.  Start by
    ** changing directories to the directory holding the program.
    */
................................................................................
        }
        close(px[1]);
        for(i=3; close(i)==0; i++){}
        execl(zBaseFilename, zBaseFilename, (char*)0);
        exit(0);
      }
      close(px[1]);
      in = fdopen(px[0], "rb");
    }
    if( in==0 ){
      CgiError();
    }

    /* Read and process the first line of the header returned by the
    ** CGI script.
................................................................................
        int i;
        for(i=7; isspace(zLine[i]); i++){}
        nOut += printf("%s %s", zProtocol, &zLine[i]);
        strncpy(zReplyStatus, &zLine[i], 3);
        zReplyStatus[3] = 0;
        statusSent = 1;
      }else{
                            /*  123456789 12345 */
        if( strncasecmp(zLine, "Content-length:", 15)==0 ){
          seenContentLength = 1;
          contentLength = atoi(zLine+15);
        }
        StartResponse("200 OK");
        nOut += printf("%s",zLine);
      }
    }

    /* Copy everything else thru without change or analysis.
    */
    StartResponse("200 OK");
    if( useTimeout ) alarm(60*5);
    if( seenContentLength ){
      nOut += printf("%s", zLine);
      while( (contentLength--)>0 && (c = getc(in))!=EOF ){
        putc(c,stdout);
        nOut++;
      }
    }else{
      nRes = 0;
      nMalloc = 1000;
      aRes = malloc(nMalloc+1);
................................................................................
      nOut += printf("Cache-Control: max-age=%d\r\n", mxAge);
      nOut += printf("ETag: \"%s\"\r\n", zETag);
      nOut += printf("\r\n");
      fflush(stdout);
      MakeLogEntry(0, 470);  /* LOG: ETag Cache Hit */
      return;
    }
    in = fopen(zFile,"rb");
    if( in==0 ) NotFound(480); /* LOG: fopen() failed for static content */
    StartResponse("200 OK");
    nOut += DateTag("Last-Modified", statbuf.st_mtime);
    nOut += printf("Cache-Control: max-age=%d\r\n", mxAge);
    nOut += printf("ETag: \"%s\"\r\n", zETag);
    nOut += printf("Content-type: %s\r\n",zContentType);
    nOut += printf("Content-length: %d\r\n\r\n",(int)statbuf.st_size);