Skip to content

Commit e50343a

Browse files
authored
Merge pull request #49 from mvslovers/issue-48-remove-dd-docroot
Remove DD-based docroot, UFS-only static file serving
2 parents 258e21c + 2538328 commit e50343a

3 files changed

Lines changed: 35 additions & 244 deletions

File tree

src/httpfile.c

Lines changed: 10 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -462,47 +462,7 @@ ssi_system_env(HTTPC *httpc)
462462

463463
}
464464

465-
/* ddname() convert "/index.html" -> "/DD:html(index)" */
466-
static UCHAR *
467-
ddname(const UCHAR *in, UCHAR *out, int size)
468-
{
469-
UCHAR *ext = strchr(in,'.');
470-
UCHAR *mem = strchr(in,'/');
471-
int extlen = ext ? strlen(ext+1) : 0;
472-
int memlen = (mem && ext) ? (int)(ext-mem) - 1 : 0;
473-
int pos = 0;
474-
int len;
475-
476-
/* we need space for "/DD:_ddname_(_member_)" */
477-
if (size < sizeof("/DD:_ddname_(_member_)")) goto quit;
478-
479-
if (extlen < 0 OR memlen < 0 OR strchr(in, ':')) {
480-
/* doesn't look like a normal "file.ext" name */
481-
out = NULL;
482-
goto quit;
483-
}
484-
485-
if (extlen > 8) extlen = 8; /* limit ddname to 8 characters */
486-
if (memlen > 8) memlen = 8; /* limit member name to 8 characters */
487-
488-
strcpy(out, "/DD:");
489-
pos += 4;
490-
491-
for(ext++;(*ext) && (extlen > 0); extlen--) {
492-
out[pos++]=*ext++;
493-
}
494-
out[pos++] = '(';
495-
for(mem++;(*mem) && (*mem!='.') && (memlen > 0); memlen--) {
496-
out[pos++]=*mem++;
497-
}
498-
out[pos++] = ')';
499-
out[pos] = 0;
500-
501-
quit:
502-
return out;
503-
}
504-
505-
static int
465+
static int
506466
ssi_include(HTTPC *httpc, char *next)
507467
{
508468
int isfile = 0;
@@ -516,7 +476,6 @@ ssi_include(HTTPC *httpc, char *next)
516476
UCHAR subtype = httpc->subtype;
517477
UCHAR substate = httpc->substate;
518478
UCHAR ssi = httpc->ssi;
519-
char buf[40];
520479
char uri[UFS_PATH_MAX];
521480

522481
// wtof("%s: enter next=\"%s\"", __func__, next);
@@ -555,40 +514,17 @@ ssi_include(HTTPC *httpc, char *next)
555514
strncpy(uri, path, sizeof(uri));
556515
uri[sizeof(uri)-1] = 0;
557516

558-
if (httpc->ufs) {
559-
/* try to open path asis */
560-
mime = http_mime(path);
561-
if (!mime->binary) {
562-
httpc->fp = http_open(httpc, path, mime);
563-
if (httpc->fp || httpc->ufp) goto okay; /* file was opened */
564-
}
517+
mime = http_mime(path);
518+
if (mime->binary) {
519+
ssi_printf(httpc, "<!-- Oops: we can't include a binary file \"%s\" -->\n", uri);
520+
goto quit;
565521
}
566522

567-
/* now we'll try to convert the path to a "/DD:EXT(MEMBER)" */
568-
if (http_cmpn(path,"/DD:",4)!=0 &&
569-
strchr(path,'.') && strlen(path) < 19) {
570-
571-
/* convert the path to a "/DD:ddname(member)" */
572-
if (ddname(path, buf, sizeof(buf))) {
573-
path = buf;
574-
575-
mime = http_mime(path);
576-
if (!mime->binary) {
577-
/* open the file */
578-
httpc->fp = http_open(httpc, path, mime);
579-
}
580-
}
581-
}
582-
583-
if (!httpc->fp && !httpc->ufp) {
584-
if (mime->binary) {
585-
ssi_printf(httpc, "<!-- Oops: we can't include a binary file \"%s\" -->\n", uri);
586-
goto quit;
587-
}
588-
/* open failed for path name */
589-
ssi_printf(httpc, "<!-- Oops: we didn't find \"%s\" -->\n", uri);
590-
goto quit;
591-
}
523+
httpc->fp = http_open(httpc, path, mime);
524+
if (!httpc->fp && !httpc->ufp) {
525+
ssi_printf(httpc, "<!-- Oops: we didn't find \"%s\" -->\n", uri);
526+
goto quit;
527+
}
592528

593529
okay:
594530
// ssi_printf(httpc, "\n");

src/httpget.c

Lines changed: 22 additions & 104 deletions
Original file line numberDiff line numberDiff line change
@@ -11,46 +11,6 @@ enum ctype {
1111
CTYPE_TEXT
1212
};
1313

14-
/* ddname() convert "/index.html" -> "/DD:html(index)" */
15-
static UCHAR *
16-
ddname(const UCHAR *in, UCHAR *out, int size)
17-
{
18-
UCHAR *ext = strchr(in,'.');
19-
UCHAR *mem = strchr(in,'/');
20-
int extlen = ext ? strlen(ext+1) : 0;
21-
int memlen = (mem && ext) ? (int)(ext-mem) - 1 : 0;
22-
int pos = 0;
23-
int len;
24-
25-
/* we need space for "/DD:_ddname_(_member_)" */
26-
if (size < sizeof("/DD:_ddname_(_member_)")) goto quit;
27-
28-
if (extlen < 0 OR memlen < 0 OR strchr(in, ':')) {
29-
/* doesn't look like a normal "file.ext" name */
30-
out = NULL;
31-
goto quit;
32-
}
33-
34-
if (extlen > 8) extlen = 8; /* limit ddname to 8 characters */
35-
if (memlen > 8) memlen = 8; /* limit member name to 8 characters */
36-
37-
strcpy(out, "/DD:");
38-
pos += 4;
39-
40-
for(ext++;(*ext) && (extlen > 0); extlen--) {
41-
out[pos++]=*ext++;
42-
}
43-
out[pos++] = '(';
44-
for(mem++;(*mem) && (*mem!='.') && (memlen > 0); memlen--) {
45-
out[pos++]=*mem++;
46-
}
47-
out[pos++] = ')';
48-
out[pos] = 0;
49-
50-
quit:
51-
return out;
52-
}
53-
5414
extern int
5515
httpget(HTTPC *httpc)
5616
{
@@ -76,71 +36,30 @@ httpget(HTTPC *httpc)
7636
__asm__("DC\tH'0'");
7737
}
7838

79-
if (http_get_ufs(httpc)) {
80-
/* try to open path asis */
81-
mime = http_mime(path);
82-
fp = http_open(httpc, path, mime);
83-
// wtof("%s: 1 http_open(%p, \"%s\", %p) fp=%p, httpc->ufp=%p",
84-
// __func__, httpc, path, mime, fp, httpc->ufp);
85-
if (fp || httpc->ufp) goto okay; /* file was opened */
86-
87-
/* If the path is for a directory then we need to see if a
88-
* "index.html" exist in this directory and open it.
89-
*/
90-
len = strlen(path);
91-
if (path[len-1]=='/') {
92-
memcpy(buf, path, len);
93-
strcpy(&buf[len], "index.html");
94-
95-
mime = http_mime(buf);
96-
fp = http_open(httpc, buf, mime);
97-
98-
if (fp || httpc->ufp) goto okay; /* file was opened */
99-
}
100-
}
101-
102-
if (http_cmpn(path,"/DD:",4)!=0 &&
103-
strchr(path,'.') && strlen(path) < 19) {
104-
105-
/* convert the path to a "/DD:ddname(member)" */
106-
if (ddname(path, buf, sizeof(buf))) {
107-
path = buf;
108-
http_dbgf("ddname:\"%s\"\n", path);
109-
}
110-
}
39+
/* try to open path from UFS */
11140
mime = http_mime(path);
112-
113-
/* try to open the requested document */
11441
fp = http_open(httpc, path, mime);
115-
// wtof("%s: 2 http_open(%p, \"%s\", %p) fp=%p, httpc->ufp=%p",
116-
// __func__, httpc, path, mime, fp, httpc->ufp);
117-
if (!fp && !httpc->ufp && http_cmp(path,"/")==0) {
118-
/* try to open default documents */
119-
path = "/index.html";
120-
mime = http_mime(path);
121-
fp = http_open(httpc, path, mime);
122-
if (!fp && !httpc->ufp) {
123-
path = "/default.html";
124-
mime = http_mime(path);
125-
fp = http_open(httpc, path, mime);
126-
}
127-
if (!fp && !httpc->ufp) {
128-
path = "/DD:HTML(INDEX)";
129-
mime = http_mime(path);
130-
fp = http_open(httpc, path, mime);
131-
}
132-
if (!fp && !httpc->ufp) {
133-
/* try another one */
134-
path = "/DD:HTML(DEFAULT)";
135-
fp = http_open(httpc, path, mime);
136-
}
42+
if (fp || httpc->ufp) goto okay;
43+
44+
/* If the path is for a directory, try index.html / default.html */
45+
len = strlen(path);
46+
if (path[len-1]=='/') {
47+
memcpy(buf, path, len);
48+
strcpy(&buf[len], "index.html");
49+
mime = http_mime(buf);
50+
fp = http_open(httpc, buf, mime);
51+
if (fp || httpc->ufp) goto okay;
52+
53+
strcpy(&buf[len], "default.html");
54+
mime = http_mime(buf);
55+
fp = http_open(httpc, buf, mime);
56+
if (fp || httpc->ufp) goto okay;
13757
}
13858

139-
if (!fp && !httpc->ufp) {
140-
rc = http_resp_not_found(httpc, path);
141-
httpc->state = CSTATE_DONE;
142-
goto quit;
143-
}
59+
/* file not found */
60+
rc = http_resp_not_found(httpc, path);
61+
httpc->state = CSTATE_DONE;
62+
goto quit;
14463

14564
okay:
14665
httpc->fp = fp;
@@ -153,9 +72,8 @@ httpget(HTTPC *httpc)
15372
if (rc) goto die;
15473
#if 1
15574
/* don't allow the browser to cache our html documents */
156-
if (strstr(mime->type, "html") ||
157-
__patmat(path, "*.html") ||
158-
__patmat(path, "/DD:HTML(*)")) {
75+
if (strstr(mime->type, "html") ||
76+
__patmat(path, "*.html")) {
15977
rc = http_printf(httpc, "Cache-Control: no-store\r\n");
16078
if (rc) goto die;
16179
}

src/httpopen.c

Lines changed: 3 additions & 66 deletions
Original file line numberDiff line numberDiff line change
@@ -7,17 +7,10 @@
77
FILE *
88
http_open(HTTPC *httpc, const UCHAR *path, const HTTPM *mime)
99
{
10-
FILE *fp = NULL;
1110
UCHAR *mode;
12-
UCHAR *p;
1311
int len;
14-
int lock;
15-
UCHAR ddname[12] = "";
16-
UCHAR member[12] = "";
1712
UCHAR buf[256];
1813

19-
// wtof("%s: enter path=\"%s\"", __func__, path);
20-
2114
if (!path) goto quit;
2215

2316
/* reject path traversal attempts */
@@ -29,50 +22,6 @@ http_open(HTTPC *httpc, const UCHAR *path, const HTTPM *mime)
2922
memcpy(buf, path, len);
3023
buf[len]=0;
3124

32-
/* wtof("%s buf=\"%s\"", __func__, buf); */
33-
34-
if (http_cmpn(buf, "/DD:", 4)==0) {
35-
/* open by DD name */
36-
strcpy(buf, buf+1); /* strip leading '/' */
37-
38-
/* Ideally we would expect the path to be "/DD:EXT(MEMBER)"
39-
** but we'd also like to handle "/DD:MEMBER.EXT" and convert
40-
** that internally to "/DD:EXT(MEMBER)".
41-
**
42-
** The EXT part of the name will be used to lookup the MIME
43-
** info and would also be used as the DDNAME to open.
44-
*/
45-
46-
p = strchr(buf, '(');
47-
if (!p) {
48-
/* We likely have "DD:MEMBER.EXT" */
49-
p = strrchr(buf, '.'); /* find the extension */
50-
if (p) {
51-
/* We have "DD:MEMBER.EXT"
52-
** Use the EXT as the DDNAME
53-
*/
54-
p++;
55-
len = strlen(p);
56-
if (len>=sizeof(ddname)) len = sizeof(ddname)-1;
57-
memcpy(ddname, p, len);
58-
ddname[len] = 0;
59-
}
60-
else {
61-
/* We don't want to allow "DD:MEMBER"
62-
** They should use "DD:MEMBER.EXT"
63-
*/
64-
goto quit;
65-
}
66-
67-
len = strlen(&buf[3]);
68-
if (len>=sizeof(member)) len = sizeof(member)-1;
69-
memcpy(member, &buf[3], len);
70-
member[len] = 0;
71-
/* convert to "DD:DDNAME(MEMBER)" */
72-
sprintf(buf, "DD:%s(%s)", ddname, member);
73-
}
74-
}
75-
7625
if (!mime) mime = (const HTTPM *) http_mime(buf);
7726

7827
if (mime) {
@@ -82,42 +31,30 @@ http_open(HTTPC *httpc, const UCHAR *path, const HTTPM *mime)
8231
else {
8332
mode = "r";
8433
}
85-
34+
8635
if (http_cmp(mime->type, "text/html")==0 ||
8736
http_cmp(mime->type, "text/x-server-parsed-html")==0) {
88-
/* enable Server Side Include processing of this file */
8937
httpc->ssi = 1;
90-
// wtof("%s: enabling SSI processing for \"%s\" as \"%s\"", __func__, buf, mime->type);
9138
}
9239
else {
93-
/* disable Server Side Processing of this file */
9440
httpc->ssi = 0;
95-
// wtof("%s: disabling SSI processing for \"%s\" as \"%s\"", __func__, buf, mime->type);
9641
}
9742
}
9843

99-
10044
{
10145
UFS *ufs = http_get_ufs(httpc);
10246
if (ufs) {
10347
UCHAR ufspath[256];
10448
const char *dr = httpc->httpd->docroot;
105-
if (dr[0] && http_cmpn(buf, "/DD:", 4) != 0) {
106-
/* prepend document root to UFS path */
49+
if (dr[0]) {
10750
snprintf((char *)ufspath, sizeof(ufspath), "%s%s", dr, buf);
10851
httpc->ufp = ufs_fopen(ufs, ufspath, mode);
10952
} else {
11053
httpc->ufp = ufs_fopen(ufs, buf, mode);
11154
}
112-
if (httpc->ufp) {
113-
goto quit; /* success, return NULL to caller */
114-
}
11555
}
11656
}
11757

118-
fp = fopen(buf, mode);
119-
12058
quit:
121-
// wtof("%s: exit fp=%p", __func__, fp);
122-
return fp;
59+
return NULL;
12360
}

0 commit comments

Comments
 (0)