| 824 int service_index(pblock *pb, Session *sn, Request *rq) { |
824 int service_index(pblock *pb, Session *sn, Request *rq) { |
| 825 //printf("service_index\n"); |
825 //printf("service_index\n"); |
| 826 const CxAllocator *a = pool_allocator(sn->pool); |
826 const CxAllocator *a = pool_allocator(sn->pool); |
| 827 |
827 |
| 828 char *path = pblock_findkeyval(pb_key_path, rq->vars); |
828 char *path = pblock_findkeyval(pb_key_path, rq->vars); |
| 829 char *uri = pblock_findkeyval(pb_key_uri, rq->reqpb); |
829 cxstring uri = cx_str(pblock_findkeyval(pb_key_uri, rq->reqpb)); |
| |
830 if(uri.length == 0) { |
| |
831 return REQ_ABORTED; |
| |
832 } |
| |
833 if(uri.ptr[uri.length-1] != '/') { |
| |
834 cxmutstr newuri = cx_strcat_a(a, 2, uri, CX_STR("/")); |
| |
835 uri = cx_strcast(newuri); |
| |
836 } |
| |
837 |
| 830 |
838 |
| 831 // params |
839 // params |
| 832 char *useemojis = pblock_findval("use-emojis", pb); |
840 char *useemojis = pblock_findval("use-emojis", pb); |
| 833 char *diricon = pblock_findval("dir-icon", pb); |
841 char *diricon = pblock_findval("dir-icon", pb); |
| 834 char *fileicon = pblock_findval("file-icon", pb); |
842 char *fileicon = pblock_findval("file-icon", pb); |
| 869 |
877 |
| 870 sbuf_t *out = sbuf_new(1024); // output buffer |
878 sbuf_t *out = sbuf_new(1024); // output buffer |
| 871 |
879 |
| 872 // write html header |
880 // write html header |
| 873 sbuf_puts(out, "<!DOCTYPE html>\n<html>\n<head>\n<title>Index of "); |
881 sbuf_puts(out, "<!DOCTYPE html>\n<html>\n<head>\n<title>Index of "); |
| 874 sbuf_puts(out, uri); |
882 sbuf_append(out, uri); |
| 875 sbuf_puts(out, "</title>\n"); |
883 sbuf_puts(out, "</title>\n"); |
| 876 sbuf_puts(out, "<style>\n"); |
884 sbuf_puts(out, "<style>\n"); |
| |
885 sbuf_puts(out, "body { font-family: sans; }\n"); |
| |
886 sbuf_puts(out, "h1 { font-size: 1.1em; }\n"); |
| 877 sbuf_puts(out, "th { text-align: left; }\n"); |
887 sbuf_puts(out, "th { text-align: left; }\n"); |
| 878 sbuf_puts(out, "td { padding-right: 2em; }\n"); |
888 sbuf_puts(out, "td { padding-right: 2em; }\n"); |
| |
889 sbuf_puts(out, "a { text-decoration: none; }\n"); |
| 879 sbuf_puts(out, ".type { padding-right: 0em; }\n"); |
890 sbuf_puts(out, ".type { padding-right: 0em; }\n"); |
| |
891 sbuf_puts(out, ".path { background-color: #e0e0e0; border-radius: 0.5em; padding: 0.25em 0.75em 0.25em 0.75em; }"); |
| 880 sbuf_puts(out, "</style>\n"); |
892 sbuf_puts(out, "</style>\n"); |
| 881 sbuf_puts(out, "</head><body>\n<h1>Index of "); |
893 sbuf_puts(out, "</head><body><h1>"); |
| 882 sbuf_puts(out, uri); |
894 |
| 883 sbuf_puts(out, "</h1><hr>\n\n"); |
895 if(uri.length > 0) { |
| |
896 sbuf_puts(out, "<span class=\"path\"><a href=\"/\">/</a></span>\n"); |
| |
897 size_t start = 1; |
| |
898 for(size_t i=1;i<uri.length;i++) { |
| |
899 if(uri.ptr[i] == '/') { |
| |
900 sbuf_puts(out, "<span class=\"path\"><a href=\""); |
| |
901 sbuf_write(out, uri.ptr, i); |
| |
902 sbuf_puts(out, "\">"); |
| |
903 sbuf_write(out, uri.ptr+start, i-start); |
| |
904 sbuf_puts(out, "</a></span>\n"); |
| |
905 start = i+1; |
| |
906 } |
| |
907 } |
| |
908 } |
| |
909 |
| |
910 sbuf_puts(out, "</h1><hr>"); |
| 884 |
911 |
| 885 // read directory at store entries in the files list |
912 // read directory at store entries in the files list |
| 886 int ret = REQ_PROCEED; |
913 int ret = REQ_PROCEED; |
| 887 VFS_ENTRY f; |
914 VFS_ENTRY f; |
| 888 while(vfs_readdir_stat(dir, &f)) { |
915 while(vfs_readdir_stat(dir, &f)) { |
| 925 sbuf_puts(out, entry->isdir ? dir_str : file_str); |
952 sbuf_puts(out, entry->isdir ? dir_str : file_str); |
| 926 sbuf_puts(out, "</td>"); |
953 sbuf_puts(out, "</td>"); |
| 927 |
954 |
| 928 sbuf_puts(out, "<td>"); |
955 sbuf_puts(out, "<td>"); |
| 929 sbuf_puts(out, "<a href=\""); |
956 sbuf_puts(out, "<a href=\""); |
| |
957 sbuf_append(out, uri); |
| 930 sbuf_puts(out, entry->name); |
958 sbuf_puts(out, entry->name); |
| 931 sbuf_puts(out, "\">"); |
959 sbuf_puts(out, "\">"); |
| 932 sbuf_puts(out, entry->name); |
960 sbuf_puts(out, entry->name); |
| 933 sbuf_puts(out, "</a>"); |
961 sbuf_puts(out, "</a>"); |
| 934 sbuf_puts(out, "</td>"); |
962 sbuf_puts(out, "</td>"); |