@@ -56,10 +56,11 @@ module.exports = {
56
56
// so we convert /ipns/ to /ipfs/ before passing it to the resolver ¯\_(ツ)_/¯
57
57
// This could be removed if a solution proposed in
58
58
// https://github.com./ipfs/js-ipfs-http-response/issues/22 lands upstream
59
- const ipfsPath = decodeURI ( path . startsWith ( '/ipns/' )
59
+ let ipfsPath = decodeURI ( path . startsWith ( '/ipns/' )
60
60
? await ipfs . name . resolve ( path , { recursive : true } )
61
61
: path )
62
62
63
+ let directory = false
63
64
let data
64
65
try {
65
66
data = await resolver . cid ( ipfs , ipfsPath )
@@ -70,22 +71,23 @@ module.exports = {
70
71
// switch case with true feels so wrong.
71
72
switch ( true ) {
72
73
case ( errorToString === 'Error: This dag node is a directory' ) :
74
+ directory = true
73
75
data = await resolver . directory ( ipfs , ipfsPath , err . cid )
74
76
75
77
if ( typeof data === 'string' ) {
76
78
// no index file found
77
79
if ( ! path . endsWith ( '/' ) ) {
78
- // for a directory, if URL doesn't end with a /
79
- // append / and redirect permanent to that URL
80
+ // add trailing slash for directory listings
80
81
return h . redirect ( `${ path } /` ) . permanent ( true )
81
82
}
82
83
// send directory listing
83
84
return h . response ( data )
84
85
}
85
86
86
- // found index file
87
- // redirect to URL/<found-index-file>
88
- return h . redirect ( PathUtils . joinURLParts ( path , data [ 0 ] . Name ) )
87
+ // found index file: return <ipfsPath>/<found-index-file>
88
+ ipfsPath = PathUtils . joinURLParts ( ipfsPath , data [ 0 ] . Name )
89
+ data = await resolver . cid ( ipfs , ipfsPath )
90
+ break
89
91
case ( errorToString . startsWith ( 'Error: no link named' ) ) :
90
92
throw Boom . boomify ( err , { statusCode : 404 } )
91
93
case ( errorToString . startsWith ( 'Error: multihash length inconsistent' ) ) :
@@ -97,10 +99,14 @@ module.exports = {
97
99
}
98
100
}
99
101
100
- if ( path . endsWith ( '/' ) ) {
102
+ if ( ! directory && path . endsWith ( '/' ) ) {
101
103
// remove trailing slash for files
102
104
return h . redirect ( PathUtils . removeTrailingSlash ( path ) ) . permanent ( true )
103
105
}
106
+ if ( directory && ! path . endsWith ( '/' ) ) {
107
+ // add trailing slash for directories with implicit index.html
108
+ return h . redirect ( `${ path } /` ) . permanent ( true )
109
+ }
104
110
105
111
// Support If-None-Match & Etag (Conditional Requests from RFC7232)
106
112
// https://developer.mozilla.org/en-US/docs/Web/HTTP/Headers/ETag
@@ -153,7 +159,7 @@ module.exports = {
153
159
log . error ( err )
154
160
return reject ( err )
155
161
}
156
- resolve ( { peekedStream, contentType : detectContentType ( path , streamHead ) } )
162
+ resolve ( { peekedStream, contentType : detectContentType ( ipfsPath , streamHead ) } )
157
163
} )
158
164
} )
159
165
@@ -170,7 +176,8 @@ module.exports = {
170
176
res . header ( 'Cache-Control' , 'public, max-age=29030400, immutable' )
171
177
}
172
178
173
- log ( 'path ' , path )
179
+ log ( 'HTTP path ' , path )
180
+ log ( 'IPFS path ' , ipfsPath )
174
181
log ( 'content-type ' , contentType )
175
182
176
183
if ( contentType ) {
0 commit comments