Documentation Source Text

Check-in [1ec2d586fc]
Login

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

Overview
Comment:Performance improvement on CGI handling in althttpd.c
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA3-256: 1ec2d586fcbca862528f690b383b7a18010cb2a299d315ee5bf6be8f1a1804dd
User & Date: drh 2020-03-16 15:07:28.236
Context
2020-03-20
12:44
Provide more accurate elapse time measurements in the output log of althttpd.c (check-in: f474634ac8 user: drh tags: trunk)
2020-03-16
15:07
Performance improvement on CGI handling in althttpd.c (check-in: 1ec2d586fc user: drh tags: trunk)
2020-03-15
18:17
Enhance althttpd to support Range: attributes in the HTTP request header. (check-in: 1d3579379c user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to misc/althttpd.c.
1248
1249
1250
1251
1252
1253
1254



























1255
1256
1257
1258
1259
1260
1261
** Count the number of "/" characters in a string.
*/
static int countSlashes(const char *z){
  int n = 0;
  while( *z ) if( *(z++)=='/' ) n++;
  return n;
}




























/*
** Send the text of the file named by zFile as the reply.  Use the
** suffix on the end of the zFile name to determine the mimetype.
**
** Return 1 to omit making a log entry for the reply.
*/







>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>
>







1248
1249
1250
1251
1252
1253
1254
1255
1256
1257
1258
1259
1260
1261
1262
1263
1264
1265
1266
1267
1268
1269
1270
1271
1272
1273
1274
1275
1276
1277
1278
1279
1280
1281
1282
1283
1284
1285
1286
1287
1288
** Count the number of "/" characters in a string.
*/
static int countSlashes(const char *z){
  int n = 0;
  while( *z ) if( *(z++)=='/' ) n++;
  return n;
}

/*
** Transfer nXfer bytes from in to out, after first discarding
** nSkip bytes from in.  Increment the nOut global variable
** according to the number of bytes transferred.
*/
static void xferBytes(FILE *in, FILE *out, int nXfer, int nSkip){
  size_t n;
  size_t got;
  char zBuf[16384];
  while( nSkip>0 ){
    n = nSkip;
    if( n>sizeof(zBuf) ) n = sizeof(zBuf);
    got = fread(zBuf, 1, n, in);
    if( got==0 ) break;
    nSkip -= got;
  }
  while( nXfer>0 ){
    n = nXfer;
    if( n>sizeof(zBuf) ) n = sizeof(zBuf);
    got = fread(zBuf, 1, n, in);
    if( got==0 ) break;
    fwrite(zBuf, got, 1, out);
    nOut += got;
    nXfer -= got;
  }
}

/*
** Send the text of the file named by zFile as the reply.  Use the
** suffix on the end of the zFile name to determine the mimetype.
**
** Return 1 to omit making a log entry for the reply.
*/
1315
1316
1317
1318
1319
1320
1321
1322
1323
1324
1325
1326
1327
1328
1329
1330
1331
1332
1333
1334
1335
1336
1337
1338
  if( useTimeout ) alarm(30 + pStat->st_size/1000);
#ifdef linux
  {
    off_t offset = rangeStart;
    nOut += sendfile(fileno(stdout), fileno(in), &offset, pStat->st_size);
  }
#else
  {
    int c;
    int n = (int)pStat->st_size;
    while( rangeStart>0 && getc(in)!=EOF ){ rangeStart--; }
    while( (c = getc(in))!=EOF && n>0 ){
      putc(c,stdout);
      nOut++;
      n--;
    }
  }
#endif
  fclose(in);
  return 0;
}

/*
** A CGI or SCGI script has run and is sending its reply back across







<
<
|
<
<
<
<
<
<
<







1342
1343
1344
1345
1346
1347
1348


1349







1350
1351
1352
1353
1354
1355
1356
  if( useTimeout ) alarm(30 + pStat->st_size/1000);
#ifdef linux
  {
    off_t offset = rangeStart;
    nOut += sendfile(fileno(stdout), fileno(in), &offset, pStat->st_size);
  }
#else


  xferBytes(in, stdout, (int)pStat->st_size, rangeStart);







#endif
  fclose(in);
  return 0;
}

/*
** A CGI or SCGI script has run and is sending its reply back across
1401
1402
1403
1404
1405
1406
1407
1408
1409
1410
1411
1412
1413
1414
1415
1416
1417
1418
1419
    aRes[nRes] = 0;
    printf("%s", aRes);
    nOut += nRes;
    nRes = 0;
  }
  if( seenContentLength ){
    nOut += printf("Content-length: %d\r\n\r\n", contentLength);
    while( rangeStart>0 && getc(in)!=EOF ){ rangeStart--; }
    while( (contentLength--)>0 && (c = getc(in))!=EOF ){
      putc(c,stdout);
      nOut++;
    }
  }else{
    while( (c = getc(in))!=EOF ){
      if( nRes>=nMalloc ){
        nMalloc = nMalloc*2;
        aRes = realloc(aRes, nMalloc+1);
        if( aRes==0 ){
           Malfunction(610, "Out of memory: %d bytes", nMalloc);







<
|
<
<
<







1419
1420
1421
1422
1423
1424
1425

1426



1427
1428
1429
1430
1431
1432
1433
    aRes[nRes] = 0;
    printf("%s", aRes);
    nOut += nRes;
    nRes = 0;
  }
  if( seenContentLength ){
    nOut += printf("Content-length: %d\r\n\r\n", contentLength);

    xferBytes(in, stdout, contentLength, rangeStart);



  }else{
    while( (c = getc(in))!=EOF ){
      if( nRes>=nMalloc ){
        nMalloc = nMalloc*2;
        aRes = realloc(aRes, nMalloc+1);
        if( aRes==0 ){
           Malfunction(610, "Out of memory: %d bytes", nMalloc);