Preserve URL sorting arguments on directory links
If a sorting criteria different than the default is used, carry the URL arguments to the links pointing to other directories, so the sorting is "remembered" when navigating across different directories.pull/8/head
parent
9b3cdb5583
commit
ea30f7fb21
|
@ -375,7 +375,7 @@ make_content_buf(
|
||||||
ngx_http_fancyindex_entry_t *entry;
|
ngx_http_fancyindex_entry_t *entry;
|
||||||
|
|
||||||
int (*sort_cmp_func) (const void*, const void*);
|
int (*sort_cmp_func) (const void*, const void*);
|
||||||
ngx_int_t sort_descending = 0;
|
const char *sort_url_args = "";
|
||||||
|
|
||||||
off_t length;
|
off_t length;
|
||||||
size_t len, root, copy, allocated;
|
size_t len, root, copy, allocated;
|
||||||
|
@ -559,6 +559,7 @@ make_content_buf(
|
||||||
len = r->uri.len
|
len = r->uri.len
|
||||||
+ ngx_sizeof_ssz(t05_body2)
|
+ ngx_sizeof_ssz(t05_body2)
|
||||||
+ ngx_sizeof_ssz(t06_list1)
|
+ ngx_sizeof_ssz(t06_list1)
|
||||||
|
+ ngx_sizeof_ssz(t_parentdir_entry)
|
||||||
+ ngx_sizeof_ssz(t07_list2)
|
+ ngx_sizeof_ssz(t07_list2)
|
||||||
;
|
;
|
||||||
|
|
||||||
|
@ -569,12 +570,13 @@ make_content_buf(
|
||||||
* is stripped out:
|
* is stripped out:
|
||||||
*
|
*
|
||||||
* <tr class="X">
|
* <tr class="X">
|
||||||
* <td><a href="U">fname</a></td>
|
* <td><a href="U[?sort]">fname</a></td>
|
||||||
* <td>size</td><td>date</td>
|
* <td>size</td><td>date</td>
|
||||||
* </tr>
|
* </tr>
|
||||||
*/
|
*/
|
||||||
len += ngx_sizeof_ssz("<tr class=\"X\"><td><a href=\"")
|
len += ngx_sizeof_ssz("<tr class=\"X\"><td><a href=\"")
|
||||||
+ entry[i].name.len + entry[i].escape /* Escaped URL */
|
+ entry[i].name.len + entry[i].escape /* Escaped URL */
|
||||||
|
+ ngx_sizeof_ssz("?C=x&O=y") /* URL sorting arguments */
|
||||||
+ ngx_sizeof_ssz("\">")
|
+ ngx_sizeof_ssz("\">")
|
||||||
+ entry[i].name.len + entry[i].utf_len
|
+ entry[i].name.len + entry[i].utf_len
|
||||||
+ NGX_HTTP_FANCYINDEX_NAME_LEN + ngx_sizeof_ssz(">")
|
+ NGX_HTTP_FANCYINDEX_NAME_LEN + ngx_sizeof_ssz(">")
|
||||||
|
@ -590,8 +592,6 @@ make_content_buf(
|
||||||
if ((b = ngx_create_temp_buf(r->pool, len)) == NULL)
|
if ((b = ngx_create_temp_buf(r->pool, len)) == NULL)
|
||||||
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
return NGX_HTTP_INTERNAL_SERVER_ERROR;
|
||||||
|
|
||||||
/* Sort entries, if needed */
|
|
||||||
if (entries.nelts > 1) {
|
|
||||||
/*
|
/*
|
||||||
* Determine the sorting criteria. URL arguments look like:
|
* Determine the sorting criteria. URL arguments look like:
|
||||||
*
|
*
|
||||||
|
@ -603,7 +603,7 @@ make_content_buf(
|
||||||
r->args.data[0] == 'C' && r->args.data[1] == '=')
|
r->args.data[0] == 'C' && r->args.data[1] == '=')
|
||||||
{
|
{
|
||||||
/* Determine whether the direction of the sorting */
|
/* Determine whether the direction of the sorting */
|
||||||
sort_descending = r->args.len == 7
|
ngx_int_t sort_descending = r->args.len == 7
|
||||||
&& r->args.data[4] == 'O'
|
&& r->args.data[4] == 'O'
|
||||||
&& r->args.data[5] == '='
|
&& r->args.data[5] == '='
|
||||||
&& r->args.data[6] == 'D';
|
&& r->args.data[6] == 'D';
|
||||||
|
@ -611,20 +611,34 @@ make_content_buf(
|
||||||
/* Pick the sorting criteria */
|
/* Pick the sorting criteria */
|
||||||
switch (r->args.data[2]) {
|
switch (r->args.data[2]) {
|
||||||
case 'M': /* Sort by mtime */
|
case 'M': /* Sort by mtime */
|
||||||
sort_cmp_func = sort_descending
|
if (sort_descending) {
|
||||||
? ngx_http_fancyindex_cmp_entries_mtime_desc
|
sort_cmp_func = ngx_http_fancyindex_cmp_entries_mtime_desc;
|
||||||
: ngx_http_fancyindex_cmp_entries_mtime_asc;
|
sort_url_args = "?C=M&O=D";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sort_cmp_func = ngx_http_fancyindex_cmp_entries_mtime_asc;
|
||||||
|
sort_url_args = "?C=M&O=A";
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'S': /* Sort by size */
|
case 'S': /* Sort by size */
|
||||||
sort_cmp_func = sort_descending
|
if (sort_descending) {
|
||||||
? ngx_http_fancyindex_cmp_entries_size_desc
|
sort_cmp_func = ngx_http_fancyindex_cmp_entries_size_desc;
|
||||||
: ngx_http_fancyindex_cmp_entries_size_asc;
|
sort_url_args = "?C=S&O=D";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sort_cmp_func = ngx_http_fancyindex_cmp_entries_size_asc;
|
||||||
|
sort_url_args = "?C=S&O=A";
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
case 'N': /* Sort by name */
|
case 'N': /* Sort by name */
|
||||||
default:
|
default:
|
||||||
sort_cmp_func = sort_descending
|
if (sort_descending) {
|
||||||
? ngx_http_fancyindex_cmp_entries_name_desc
|
sort_cmp_func = ngx_http_fancyindex_cmp_entries_name_desc;
|
||||||
: ngx_http_fancyindex_cmp_entries_name_asc;
|
sort_url_args = "?C=N&O=D";
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
sort_cmp_func = ngx_http_fancyindex_cmp_entries_name_asc;
|
||||||
|
}
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -632,6 +646,8 @@ make_content_buf(
|
||||||
sort_cmp_func = ngx_http_fancyindex_cmp_entries_name_asc;
|
sort_cmp_func = ngx_http_fancyindex_cmp_entries_name_asc;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/* Sort entries, if needed */
|
||||||
|
if (entries.nelts > 1) {
|
||||||
ngx_qsort(entry, (size_t) entries.nelts,
|
ngx_qsort(entry, (size_t) entries.nelts,
|
||||||
sizeof(ngx_http_fancyindex_entry_t),
|
sizeof(ngx_http_fancyindex_entry_t),
|
||||||
sort_cmp_func);
|
sort_cmp_func);
|
||||||
|
@ -643,6 +659,22 @@ make_content_buf(
|
||||||
|
|
||||||
tp = ngx_timeofday();
|
tp = ngx_timeofday();
|
||||||
|
|
||||||
|
/* "Parent dir" entry, always first */
|
||||||
|
b->last = ngx_cpymem_ssz(b->last,
|
||||||
|
"<tr class=\"o\">"
|
||||||
|
"<td><a href=\"../");
|
||||||
|
if (*sort_url_args) {
|
||||||
|
b->last = ngx_cpymem(b->last,
|
||||||
|
sort_url_args,
|
||||||
|
ngx_sizeof_ssz("?C=N&O=A"));
|
||||||
|
}
|
||||||
|
b->last = ngx_cpymem_ssz(b->last,
|
||||||
|
"\">Parent directory/</a></td>"
|
||||||
|
"<td>-</td>"
|
||||||
|
"<td>-</td>"
|
||||||
|
"</tr>");
|
||||||
|
|
||||||
|
/* Entries for directories and files */
|
||||||
for (i = 0; i < entries.nelts; i++) {
|
for (i = 0; i < entries.nelts; i++) {
|
||||||
static const char _evenodd[] = { 'e', 'o' };
|
static const char _evenodd[] = { 'e', 'o' };
|
||||||
b->last = ngx_cpymem_ssz(b->last, "<tr class=\"");
|
b->last = ngx_cpymem_ssz(b->last, "<tr class=\"");
|
||||||
|
@ -666,6 +698,11 @@ make_content_buf(
|
||||||
|
|
||||||
if (entry[i].dir) {
|
if (entry[i].dir) {
|
||||||
*b->last++ = '/';
|
*b->last++ = '/';
|
||||||
|
if (*sort_url_args) {
|
||||||
|
b->last = ngx_cpymem(b->last,
|
||||||
|
sort_url_args,
|
||||||
|
ngx_sizeof_ssz("?C=x&O=y"));
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
*b->last++ = '"';
|
*b->last++ = '"';
|
||||||
|
|
|
@ -72,8 +72,10 @@ static const u_char t06_list1[] = ""
|
||||||
"</thead>"
|
"</thead>"
|
||||||
"\n"
|
"\n"
|
||||||
"<tbody>"
|
"<tbody>"
|
||||||
|
;
|
||||||
|
static const u_char t_parentdir_entry[] = ""
|
||||||
"<tr class=\"o\">"
|
"<tr class=\"o\">"
|
||||||
"<td><a href=\"../\">Parent directory/</a></td>"
|
"<td><a href=\"../?C=N&O=A\">Parent directory/</a></td>"
|
||||||
"<td>-</td>"
|
"<td>-</td>"
|
||||||
"<td>-</td>"
|
"<td>-</td>"
|
||||||
"</tr>"
|
"</tr>"
|
||||||
|
@ -93,6 +95,7 @@ static const u_char t08_foot1[] = ""
|
||||||
+ nfi_sizeof_ssz(t04_body1) \
|
+ nfi_sizeof_ssz(t04_body1) \
|
||||||
+ nfi_sizeof_ssz(t05_body2) \
|
+ nfi_sizeof_ssz(t05_body2) \
|
||||||
+ nfi_sizeof_ssz(t06_list1) \
|
+ nfi_sizeof_ssz(t06_list1) \
|
||||||
|
+ nfi_sizeof_ssz(t_parentdir_entry) \
|
||||||
+ nfi_sizeof_ssz(t07_list2) \
|
+ nfi_sizeof_ssz(t07_list2) \
|
||||||
+ nfi_sizeof_ssz(t08_foot1) \
|
+ nfi_sizeof_ssz(t08_foot1) \
|
||||||
)
|
)
|
||||||
|
|
|
@ -70,8 +70,9 @@
|
||||||
</thead>
|
</thead>
|
||||||
|
|
||||||
<tbody>
|
<tbody>
|
||||||
|
<!-- var t_parentdir_entry -->
|
||||||
<tr class="o">
|
<tr class="o">
|
||||||
<td><a href="../">Parent directory/</a></td>
|
<td><a href="../?C=N&O=A">Parent directory/</a></td>
|
||||||
<td>-</td>
|
<td>-</td>
|
||||||
<td>-</td>
|
<td>-</td>
|
||||||
</tr>
|
</tr>
|
||||||
|
|
Loading…
Reference in New Issue