Annotation of src/libexec/httpd/dir-index-bozo.c, Revision 1.2
1.2 ! tls 1: /* $NetBSD: $ */
! 2:
1.1 tls 3: /* $eterna: dir-index-bozo.c,v 1.7 2006/05/17 08:18:44 mrg Exp $ */
4:
5: /*
6: * Copyright (c) 1997-2006 Matthew R. Green
7: * All rights reserved.
8: *
9: * Redistribution and use in source and binary forms, with or without
10: * modification, are permitted provided that the following conditions
11: * are met:
12: * 1. Redistributions of source code must retain the above copyright
13: * notice, this list of conditions and the following disclaimer.
14: * 2. Redistributions in binary form must reproduce the above copyright
15: * notice, this list of conditions and the following disclaimer and
16: * dedication in the documentation and/or other materials provided
17: * with the distribution.
18: * 3. The name of the author may not be used to endorse or promote products
19: * derived from this software without specific prior written permission.
20: *
21: * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
22: * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
23: * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
24: * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
25: * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26: * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
27: * LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28: * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29: * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY
30: * OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31: * SUCH DAMAGE.
32: *
33: */
34:
35: /* this code implements directory index generation for bozohttpd */
36:
37: #ifndef NO_DIRINDEX_SUPPORT
38:
39: #include <sys/param.h>
40:
41: #include <dirent.h>
42: #include <errno.h>
43: #include <string.h>
44: #include <time.h>
45: #include <assert.h>
46:
47: #include "bozohttpd.h"
48:
49: int Xflag; /* do directory indexing */
50: int Hflag; /* hide .* */
51:
52: /*
53: * output a directory index. return 1 if it actually did something..
54: */
55: int
56: directory_index(http_req *request, const char *dirname, int isindex)
57: {
58: struct stat sb;
59: struct dirent *de;
60: struct tm *tm;
61: DIR *dp;
62: char buf[MAXPATHLEN];
63: char spacebuf[48];
64: int l, i;
65:
66: dp = NULL; /* XXX */
67:
68: if (!isindex || !Xflag)
69: return 0;
70:
71: if (strlen(dirname) <= strlen(index_html))
72: dirname = ".";
73: else {
74: char *file = bozostrdup(dirname);
75:
76: file[strlen(file) - strlen(index_html)] = '\0';
77: dirname = file;
78: }
79: debug((DEBUG_FAT, "directory_index: dirname ``%s''", dirname));
80: if (stat(dirname, &sb) < 0 ||
81: (dp = opendir(dirname)) == NULL) {
82: if (errno == EPERM)
83: http_error(403, request,
84: "no permission to open directory");
85: else if (errno == ENOENT)
86: http_error(404, request, "no file");
87: else
88: http_error(500, request, "open directory");
89: /* NOTREACHED */
90: }
91:
92: bozoprintf("%s 200 OK\r\n", request->hr_proto);
93:
94: if (request->hr_proto != http_09) {
95: print_header(request, NULL, "text/html", "");
96: bozoprintf("\r\n");
97: }
98: bozoflush(stdout);
99:
100: if (request->hr_method == HTTP_HEAD) {
101: closedir(dp);
102: return 1;
103: }
104:
105: bozoprintf("<html><head><title>Index of %s</title></head>\r\n",
106: request->hr_url);
107: bozoprintf("<body><h1>Index of %s</h1>\r\n", request->hr_url);
108: bozoprintf("<pre>\r\n");
109: #define NAMELEN 40
110: #define LMODLEN 19
111: bozoprintf("Name "
112: "Last modified "
113: "Size\n");
114: bozoprintf("<hr noshade align=\"left\" width=\"80%%\">\r\n\r\n");
115:
116: while ((de = readdir(dp)) != NULL) {
117: int nostat = 0;
118: char *name = de->d_name;
119:
120: if (strcmp(name, ".") == 0 ||
121: (strcmp(name, "..") != 0 && Hflag && name[0] == '.'))
122: continue;
123:
124: snprintf(buf, sizeof buf, "%s/%s", dirname, name);
125: if (stat(buf, &sb))
126: nostat = 1;
127:
128: l = 0;
129:
130: if (strcmp(name, "..") == 0) {
131: bozoprintf("<a href=\"../\">");
132: l += bozoprintf("Parent Directory");
133: } else if (S_ISDIR(sb.st_mode)) {
134: bozoprintf("<a href=\"%s/\">", name);
135: l += bozoprintf("%s/", name);
136: } else {
137: bozoprintf("<a href=\"%s\">", name);
138: l += bozoprintf("%s", name);
139: }
140: bozoprintf("</a>");
141:
142: /* NAMELEN spaces */
143: assert(sizeof(spacebuf) > NAMELEN);
144: i = (l < NAMELEN) ? (NAMELEN - l) : 0;
145: i++;
146: memset(spacebuf, ' ', i);
147: spacebuf[i] = '\0';
148: bozoprintf(spacebuf);
149: l += i;
150:
151: if (nostat)
152: bozoprintf("? ?");
153: else {
154: tm = gmtime(&sb.st_mtime);
155: strftime(buf, sizeof buf, "%d-%b-%Y %R", tm);
156: l += bozoprintf("%s", buf);
157:
158: /* LMODLEN spaces */
159: assert(sizeof(spacebuf) > LMODLEN);
160: i = (l < (LMODLEN+NAMELEN+1)) ? ((LMODLEN+NAMELEN+1) - l) : 0;
161: i++;
162: memset(spacebuf, ' ', i);
163: spacebuf[i] = '\0';
164: bozoprintf(spacebuf);
165:
166: bozoprintf("%7ukB",
167: ((unsigned int)(sb.st_size >> 10)));
168: }
169: bozoprintf("\r\n");
170: }
171:
172: closedir(dp);
173: bozoprintf("</pre><hr></body></html>\r\n");
174: bozoflush(stdout);
175:
176: return 1;
177: }
178: #endif /* NO_DIRINDEX_SUPPORT */
CVSweb <webmaster@jp.NetBSD.org>