~ubuntu-branches/ubuntu/trusty/nginx/trusty-security

« back to all changes in this revision

Viewing changes to debian/modules/ngx-fancyindex/ngx_http_fancyindex_module.c

  • Committer: Package Import Robot
  • Author(s): Colin Watson
  • Date: 2013-11-22 12:23:25 UTC
  • mfrom: (4.3.6 sid)
  • Revision ID: package-import@ubuntu.com-20131122122325-6i9ns8d0e4fceepd
Tags: 1.4.4-1ubuntu1
* Resynchronise with Debian (LP: #1253691).  Remaining changes:
  - debian/patches/ubuntu-branding.patch:
    + Add Ubuntu branding to server_tokens.

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
1
/*
2
2
 * ngx_http_fancyindex_module.c
3
 
 * Copyright © 2007-2011 Adrian Perez <aperez@igalia.com>
 
3
 * Copyright © 2007-2013 Adrian Perez <aperez@igalia.com>
4
4
 *
5
5
 * Module used for fancy indexing of directories. Features and differences
6
6
 * with the stock nginx autoindex module:
96
96
 
97
97
 
98
98
static int ngx_libc_cdecl
99
 
    ngx_http_fancyindex_cmp_entries(const void *one, const void *two);
 
99
    ngx_http_fancyindex_cmp_entries_name_desc(const void *one, const void *two);
 
100
static int ngx_libc_cdecl
 
101
    ngx_http_fancyindex_cmp_entries_size_desc(const void *one, const void *two);
 
102
static int ngx_libc_cdecl
 
103
    ngx_http_fancyindex_cmp_entries_mtime_desc(const void *one, const void *two);
 
104
static int ngx_libc_cdecl
 
105
    ngx_http_fancyindex_cmp_entries_name_asc(const void *one, const void *two);
 
106
static int ngx_libc_cdecl
 
107
    ngx_http_fancyindex_cmp_entries_size_asc(const void *one, const void *two);
 
108
static int ngx_libc_cdecl
 
109
    ngx_http_fancyindex_cmp_entries_mtime_asc(const void *one, const void *two);
100
110
 
101
111
static ngx_int_t ngx_http_fancyindex_error(ngx_http_request_t *r,
102
112
    ngx_dir_t *dir, ngx_str_t *name);
364
374
{
365
375
    ngx_http_fancyindex_entry_t *entry;
366
376
 
 
377
    int (*sort_cmp_func) (const void*, const void*);
 
378
    const char  *sort_url_args = "";
 
379
 
367
380
    off_t        length;
368
381
    size_t       len, root, copy, allocated;
369
382
    u_char      *filename, *last, scale;
546
559
    len = r->uri.len
547
560
        + ngx_sizeof_ssz(t05_body2)
548
561
        + ngx_sizeof_ssz(t06_list1)
 
562
        + ngx_sizeof_ssz(t_parentdir_entry)
549
563
        + ngx_sizeof_ssz(t07_list2)
550
564
        ;
551
565
 
556
570
         * is stripped out:
557
571
         *
558
572
         *   <tr class="X">
559
 
         *     <td><a href="U">fname</a></td>
 
573
         *     <td><a href="U[?sort]">fname</a></td>
560
574
         *     <td>size</td><td>date</td>
561
575
         *   </tr>
562
576
         */
563
577
        len += ngx_sizeof_ssz("<tr class=\"X\"><td><a href=\"")
564
578
            + entry[i].name.len + entry[i].escape /* Escaped URL */
 
579
            + ngx_sizeof_ssz("?C=x&amp;O=y") /* URL sorting arguments */
565
580
            + ngx_sizeof_ssz("\">")
566
581
            + entry[i].name.len + entry[i].utf_len
567
582
            + NGX_HTTP_FANCYINDEX_NAME_LEN + ngx_sizeof_ssz("&gt;")
577
592
    if ((b = ngx_create_temp_buf(r->pool, len)) == NULL)
578
593
        return NGX_HTTP_INTERNAL_SERVER_ERROR;
579
594
 
 
595
    /*
 
596
     * Determine the sorting criteria. URL arguments look like:
 
597
     *
 
598
     *    C=x[&O=y]
 
599
     *
 
600
     * Where x={M,S,N} and y={A,D}
 
601
     */
 
602
    if ((r->args.len == 3 || (r->args.len == 7 && r->args.data[3] == '&')) &&
 
603
        r->args.data[0] == 'C' && r->args.data[1] == '=')
 
604
    {
 
605
        /* Determine whether the direction of the sorting */
 
606
        ngx_int_t sort_descending = r->args.len == 7
 
607
                                 && r->args.data[4] == 'O'
 
608
                                 && r->args.data[5] == '='
 
609
                                 && r->args.data[6] == 'D';
 
610
 
 
611
        /* Pick the sorting criteria */
 
612
        switch (r->args.data[2]) {
 
613
            case 'M': /* Sort by mtime */
 
614
                if (sort_descending) {
 
615
                    sort_cmp_func = ngx_http_fancyindex_cmp_entries_mtime_desc;
 
616
                    sort_url_args = "?C=M&amp;O=D";
 
617
                }
 
618
                else {
 
619
                    sort_cmp_func = ngx_http_fancyindex_cmp_entries_mtime_asc;
 
620
                    sort_url_args = "?C=M&amp;O=A";
 
621
                }
 
622
                break;
 
623
            case 'S': /* Sort by size */
 
624
                if (sort_descending) {
 
625
                    sort_cmp_func = ngx_http_fancyindex_cmp_entries_size_desc;
 
626
                    sort_url_args = "?C=S&amp;O=D";
 
627
                }
 
628
                else {
 
629
                    sort_cmp_func = ngx_http_fancyindex_cmp_entries_size_asc;
 
630
                    sort_url_args = "?C=S&amp;O=A";
 
631
                }
 
632
                break;
 
633
            case 'N': /* Sort by name */
 
634
            default:
 
635
                if (sort_descending) {
 
636
                    sort_cmp_func = ngx_http_fancyindex_cmp_entries_name_desc;
 
637
                    sort_url_args = "?C=N&amp;O=D";
 
638
                }
 
639
                else {
 
640
                    sort_cmp_func = ngx_http_fancyindex_cmp_entries_name_asc;
 
641
                }
 
642
                break;
 
643
        }
 
644
    }
 
645
    else {
 
646
        sort_cmp_func = ngx_http_fancyindex_cmp_entries_name_asc;
 
647
    }
 
648
 
580
649
    /* Sort entries, if needed */
581
650
    if (entries.nelts > 1) {
582
651
        ngx_qsort(entry, (size_t) entries.nelts,
583
 
                sizeof(ngx_http_fancyindex_entry_t),
584
 
                ngx_http_fancyindex_cmp_entries);
 
652
                  sizeof(ngx_http_fancyindex_entry_t),
 
653
                  sort_cmp_func);
585
654
    }
586
655
 
587
656
    b->last = ngx_cpymem_str(b->last, r->uri);
590
659
 
591
660
    tp = ngx_timeofday();
592
661
 
 
662
    /* "Parent dir" entry, always first */
 
663
    b->last = ngx_cpymem_ssz(b->last,
 
664
                             "<tr class=\"o\">"
 
665
                             "<td><a href=\"../");
 
666
    if (*sort_url_args) {
 
667
        b->last = ngx_cpymem(b->last,
 
668
                             sort_url_args,
 
669
                             ngx_sizeof_ssz("?C=N&amp;O=A"));
 
670
    }
 
671
    b->last = ngx_cpymem_ssz(b->last,
 
672
                             "\">Parent directory/</a></td>"
 
673
                             "<td>-</td>"
 
674
                             "<td>-</td>"
 
675
                             "</tr>");
 
676
 
 
677
    /* Entries for directories and files */
593
678
    for (i = 0; i < entries.nelts; i++) {
594
679
        static const char _evenodd[] = { 'e', 'o' };
595
680
        b->last = ngx_cpymem_ssz(b->last, "<tr class=\"");
613
698
 
614
699
        if (entry[i].dir) {
615
700
            *b->last++ = '/';
 
701
            if (*sort_url_args) {
 
702
                b->last = ngx_cpymem(b->last,
 
703
                                     sort_url_args,
 
704
                                     ngx_sizeof_ssz("?C=x&amp;O=y"));
 
705
            }
616
706
        }
617
707
 
618
708
        *b->last++ = '"';
898
988
 
899
989
 
900
990
static int ngx_libc_cdecl
901
 
ngx_http_fancyindex_cmp_entries(const void *one, const void *two)
902
 
{
903
 
    ngx_http_fancyindex_entry_t *first = (ngx_http_fancyindex_entry_t *) one;
904
 
    ngx_http_fancyindex_entry_t *second = (ngx_http_fancyindex_entry_t *) two;
905
 
 
906
 
    if (first->dir && !second->dir) {
907
 
        /* move the directories to the start */
908
 
        return -1;
909
 
    }
910
 
 
911
 
    if (!first->dir && second->dir) {
912
 
        /* move the directories to the start */
 
991
ngx_http_fancyindex_cmp_entries_name_desc(const void *one, const void *two)
 
992
{
 
993
    ngx_http_fancyindex_entry_t *first = (ngx_http_fancyindex_entry_t *) one;
 
994
    ngx_http_fancyindex_entry_t *second = (ngx_http_fancyindex_entry_t *) two;
 
995
 
 
996
    /* move the directories to the start */
 
997
    if (first->dir && !second->dir) {
 
998
        return -1;
 
999
    }
 
1000
    if (!first->dir && second->dir) {
 
1001
        return 1;
 
1002
    }
 
1003
 
 
1004
    return (int) ngx_strcmp(second->name.data, first->name.data);
 
1005
}
 
1006
 
 
1007
 
 
1008
static int ngx_libc_cdecl
 
1009
ngx_http_fancyindex_cmp_entries_size_desc(const void *one, const void *two)
 
1010
{
 
1011
    ngx_http_fancyindex_entry_t *first = (ngx_http_fancyindex_entry_t *) one;
 
1012
    ngx_http_fancyindex_entry_t *second = (ngx_http_fancyindex_entry_t *) two;
 
1013
 
 
1014
    /* move the directories to the start */
 
1015
    if (first->dir && !second->dir) {
 
1016
        return -1;
 
1017
    }
 
1018
    if (!first->dir && second->dir) {
 
1019
        return 1;
 
1020
    }
 
1021
 
 
1022
    return second->size - first->size;
 
1023
}
 
1024
 
 
1025
 
 
1026
static int ngx_libc_cdecl
 
1027
ngx_http_fancyindex_cmp_entries_mtime_desc(const void *one, const void *two)
 
1028
{
 
1029
    ngx_http_fancyindex_entry_t *first = (ngx_http_fancyindex_entry_t *) one;
 
1030
    ngx_http_fancyindex_entry_t *second = (ngx_http_fancyindex_entry_t *) two;
 
1031
 
 
1032
    /* move the directories to the start */
 
1033
    if (first->dir && !second->dir) {
 
1034
        return -1;
 
1035
    }
 
1036
    if (!first->dir && second->dir) {
 
1037
        return 1;
 
1038
    }
 
1039
 
 
1040
    return second->mtime - first->mtime;
 
1041
}
 
1042
 
 
1043
 
 
1044
static int ngx_libc_cdecl
 
1045
ngx_http_fancyindex_cmp_entries_name_asc(const void *one, const void *two)
 
1046
{
 
1047
    ngx_http_fancyindex_entry_t *first = (ngx_http_fancyindex_entry_t *) one;
 
1048
    ngx_http_fancyindex_entry_t *second = (ngx_http_fancyindex_entry_t *) two;
 
1049
 
 
1050
    /* move the directories to the start */
 
1051
    if (first->dir && !second->dir) {
 
1052
        return -1;
 
1053
    }
 
1054
    if (!first->dir && second->dir) {
913
1055
        return 1;
914
1056
    }
915
1057
 
917
1059
}
918
1060
 
919
1061
 
 
1062
static int ngx_libc_cdecl
 
1063
ngx_http_fancyindex_cmp_entries_size_asc(const void *one, const void *two)
 
1064
{
 
1065
    ngx_http_fancyindex_entry_t *first = (ngx_http_fancyindex_entry_t *) one;
 
1066
    ngx_http_fancyindex_entry_t *second = (ngx_http_fancyindex_entry_t *) two;
 
1067
 
 
1068
    /* move the directories to the start */
 
1069
    if (first->dir && !second->dir) {
 
1070
        return -1;
 
1071
    }
 
1072
    if (!first->dir && second->dir) {
 
1073
        return 1;
 
1074
    }
 
1075
 
 
1076
    return first->size - second->size;
 
1077
}
 
1078
 
 
1079
 
 
1080
static int ngx_libc_cdecl
 
1081
ngx_http_fancyindex_cmp_entries_mtime_asc(const void *one, const void *two)
 
1082
{
 
1083
    ngx_http_fancyindex_entry_t *first = (ngx_http_fancyindex_entry_t *) one;
 
1084
    ngx_http_fancyindex_entry_t *second = (ngx_http_fancyindex_entry_t *) two;
 
1085
 
 
1086
    /* move the directories to the start */
 
1087
    if (first->dir && !second->dir) {
 
1088
        return -1;
 
1089
    }
 
1090
    if (!first->dir && second->dir) {
 
1091
        return 1;
 
1092
    }
 
1093
 
 
1094
    return first->mtime - second->mtime;
 
1095
}
920
1096
 
921
1097
 
922
1098
static ngx_int_t