Fossil

Check-in [bdfbbddc8f]
Login

Check-in [bdfbbddc8f]

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

Overview
Comment:If a /doc filename ends with "/", then try appending "index.html", "index.wiki", and "index.md" in that order. If none are found, then a 404 error will be generated. Try to find a file named 404.md in the root directory for the text of the 404, or generate a default 404 if no 404.md file is found.
Downloads: Tarball | ZIP archive
Timelines: family | ancestors | descendants | both | trunk
Files: files | file ages | folders
SHA1: bdfbbddc8f170f406b30692d9780a368bf0efc10
User & Date: drh 2015-01-29 01:23:47.874
Context
2015-01-29
01:25
Fix harmless compiler warnings. ... (check-in: ef108998c4 user: drh tags: trunk)
01:23
If a /doc filename ends with "/", then try appending "index.html", "index.wiki", and "index.md" in that order. If none are found, then a 404 error will be generated. Try to find a file named 404.md in the root directory for the text of the 404, or generate a default 404 if no 404.md file is found. ... (check-in: bdfbbddc8f user: drh tags: trunk)
00:44
Fix another instance of index.wiki to index.html. ... (check-in: 2c1677aa17 user: drh tags: trunk)
Changes
Unified Diff Ignore Whitespace Patch
Changes to src/doc.c.
373
374
375
376
377
378
379





380
381
382
383
384
385
386
387
388
389
390



391
392
393

394
395
396





397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466


467
468
469
470
471
472
473
** directly from disk and need not be a managed file.
**
** The "ckout" CHECKIN is intended for development - to provide a mechanism
** for looking at what a file will look like using the /doc webpage after
** it gets checked in.
**
** The file extension is used to decide how to render the file.





*/
void doc_page(void){
  const char *zName;                /* Argument to the /doc page */
  const char *zOrigName;            /* Original document name */
  const char *zMime;                /* Document MIME type */
  char *zCheckin;                   /* The checkin holding the document */
  int vid = 0;                      /* Artifact of checkin */
  int rid = 0;                      /* Artifact of file */
  int i;                            /* Loop counter */
  Blob filebody;                    /* Content of the documentation file */
  int nMiss = 0;                    /* Failed attempts to find the document */




  login_check_credentials();
  if( !g.perm.Read ){ login_needed(); return; }

  zName = PD("name", "tip/index.wiki");
  for(i=0; zName[i] && zName[i]!='/'; i++){}
  zCheckin = mprintf("%.*s", i, zName);





  if( zName[i]==0 ){
    zName = "index.html";
  }else{
    zName += i;
  }
  while( zName[0]=='/' ){ zName++; }
  g.zPath = mprintf("%s/%s/%s", g.zPath, zCheckin, zName);
  zOrigName = zName;
  if( !file_is_simple_pathname(zName, 1) ){
    if( sqlite3_strglob("*/", zName)==0 ){
      zOrigName = zName = mprintf("%sindex.html", zName);
      if( !file_is_simple_pathname(zName, 1) ){
        goto doc_not_found;
      }
    }else{
      goto doc_not_found;
    }
  }
  if( fossil_strcmp(zCheckin,"ckout")==0 && db_open_local(0)==0 ){
    sqlite3_snprintf(sizeof(zCheckin), zCheckin, "tip");
  }
  if( fossil_strcmp(zCheckin,"ckout")==0 ){
    /* Read from the local checkout */
    char *zFullpath;
    db_must_be_within_tree();
    while( rid==0 && nMiss<2 ){
      zFullpath = mprintf("%s/%s", g.zLocalRoot, zName);
      if( file_isfile(zFullpath)
       && blob_read_from_file(&filebody, zFullpath)<0 ){
        rid = 1;  /* Fake RID just to get the loop to end */
      }
      fossil_free(zFullpath);
      if( rid ) break;
      nMiss++;
      zName = "404.md";
    }
  }else{
    db_begin_transaction();
    vid = name_to_typed_rid(zCheckin, "ci");
    db_multi_exec(
      "CREATE TABLE IF NOT EXISTS vcache(\n"
      "  vid INTEGER,         -- checkin ID\n"
      "  fname TEXT,          -- filename\n"
      "  rid INTEGER,         -- artifact ID\n"
      "  PRIMARY KEY(vid,fname)\n"
      ") WITHOUT ROWID"
    );
    if( !db_exists("SELECT 1 FROM vcache WHERE vid=%d", vid) ){
      db_multi_exec(
        "DELETE FROM vcache;\n"
        "CREATE VIRTUAL TABLE temp.foci USING files_of_checkin;\n"
        "INSERT INTO vcache(vid,fname,rid)"
        "  SELECT checkinID, filename, blob.rid FROM foci, blob"
        "   WHERE blob.uuid=foci.uuid"
        "     AND foci.checkinID=%d;",
        vid
      );
    }
    while( rid==0 && nMiss<2 ){
      rid = db_int(0, "SELECT rid FROM vcache"
                      " WHERE vid=%d AND fname=%Q", vid, zName);
      if( rid ) break;
      nMiss++;
      zName = "404.md";
    }
    if( rid==0 || content_get(rid, &filebody)==0 ){
      goto doc_not_found;
    }
    db_end_transaction(0);
  }


  blob_to_utf8_no_bom(&filebody, 0);

  /* The file is now contained in the filebody blob.  Deliver the
  ** file to the user
  */
  zMime = nMiss==0 ? P("mimetype") : 0;
  if( zMime==0 ){







>
>
>
>
>











>
>
>



>
|
|
|
>
>
>
>
>
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<
<
<
|
|
|
|
<


|



<
<
<
<
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
<


<

<
<
|
|
|
|
|
>
>







373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428



429
430
431
432

433
434
435
436
437
438




439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460

461
462

463


464
465
466
467
468
469
470
471
472
473
474
475
476
477
** directly from disk and need not be a managed file.
**
** The "ckout" CHECKIN is intended for development - to provide a mechanism
** for looking at what a file will look like using the /doc webpage after
** it gets checked in.
**
** The file extension is used to decide how to render the file.
**
** If FILE ends in "/" then names "FILE/index.html", "FILE/index.wiki",
** and "FILE/index.md" are  in that order.  If none of those are found,
** then FILE is completely replaced by "404.md" and tried.  If that is
** not found, then a default 404 screen is generated.
*/
void doc_page(void){
  const char *zName;                /* Argument to the /doc page */
  const char *zOrigName;            /* Original document name */
  const char *zMime;                /* Document MIME type */
  char *zCheckin;                   /* The checkin holding the document */
  int vid = 0;                      /* Artifact of checkin */
  int rid = 0;                      /* Artifact of file */
  int i;                            /* Loop counter */
  Blob filebody;                    /* Content of the documentation file */
  int nMiss = 0;                    /* Failed attempts to find the document */
  static const char *azSuffix[] = {
     "index.html", "index.wiki", "index.md"
  };

  login_check_credentials();
  if( !g.perm.Read ){ login_needed(); return; }
  for(nMiss=0; rid==0 && nMiss<=ArraySize(azSuffix); nMiss++){
    zName = PD("name", "tip/index.wiki");
    for(i=0; zName[i] && zName[i]!='/'; i++){}
    zCheckin = mprintf("%.*s", i, zName);
    if( fossil_strcmp(zCheckin,"ckout")==0 && db_open_local(0)==0 ){
      zCheckin = "tip";
    }
    if( nMiss==ArraySize(azSuffix) ){
      zName = "404.md";
    }else if( zName[i]==0 ){
      zName = azSuffix[nMiss];
    }else{
      zName += i;
    }
    while( zName[0]=='/' ){ zName++; }
    g.zPath = mprintf("%s/%s/%s", g.zPath, zCheckin, zName);
    if( nMiss==0 ) zOrigName = zName;
    if( !file_is_simple_pathname(zName, 1) ){
      if( sqlite3_strglob("*/", zName)==0 ){
        zName = mprintf("%s%s", zName, azSuffix[nMiss]);
        if( !file_is_simple_pathname(zName, 1) ){
          goto doc_not_found;
        }
      }else{
        goto doc_not_found;
      }
    }



    if( fossil_strcmp(zCheckin,"ckout")==0 ){
      /* Read from the local checkout */
      char *zFullpath;
      db_must_be_within_tree();

      zFullpath = mprintf("%s/%s", g.zLocalRoot, zName);
      if( file_isfile(zFullpath)
       && blob_read_from_file(&filebody, zFullpath)>0 ){
        rid = 1;  /* Fake RID just to get the loop to end */
      }
      fossil_free(zFullpath);




    }else{
      db_begin_transaction();
      vid = name_to_typed_rid(zCheckin, "ci");
      db_multi_exec(
        "CREATE TABLE IF NOT EXISTS vcache(\n"
        "  vid INTEGER,         -- checkin ID\n"
        "  fname TEXT,          -- filename\n"
        "  rid INTEGER,         -- artifact ID\n"
        "  PRIMARY KEY(vid,fname)\n"
        ") WITHOUT ROWID"
      );
      if( !db_exists("SELECT 1 FROM vcache WHERE vid=%d", vid) ){
        db_multi_exec(
          "DELETE FROM vcache;\n"
          "CREATE VIRTUAL TABLE temp.foci USING files_of_checkin;\n"
          "INSERT INTO vcache(vid,fname,rid)"
          "  SELECT checkinID, filename, blob.rid FROM foci, blob"
          "   WHERE blob.uuid=foci.uuid"
          "     AND foci.checkinID=%d;",
          vid
        );
      }

      rid = db_int(0, "SELECT rid FROM vcache"
                      " WHERE vid=%d AND fname=%Q", vid, zName);

      nMiss++;


      if( rid==0 || content_get(rid, &filebody)==0 ){
        goto doc_not_found;
      }
      db_end_transaction(0);
    }
  }
  if( rid==0 ) goto doc_not_found;
  blob_to_utf8_no_bom(&filebody, 0);

  /* The file is now contained in the filebody blob.  Deliver the
  ** file to the user
  */
  zMime = nMiss==0 ? P("mimetype") : 0;
  if( zMime==0 ){