@@ -82,6 +82,7 @@ func (sw *statusResponseWriter) WriteHeader(code int) {
82
82
redirect := sw .ResponseWriter .Header ().Get ("Location" )
83
83
if redirect != "" && code == http .StatusOK {
84
84
code = http .StatusMovedPermanently
85
+ log .Debugw ("subdomain redirect" , "location" , redirect , "status" , code )
85
86
}
86
87
sw .ResponseWriter .WriteHeader (code )
87
88
}
@@ -198,6 +199,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
198
199
urlPath := r .URL .Path
199
200
escapedURLPath := r .URL .EscapedPath ()
200
201
202
+ logger := log .With ("from" , r .RequestURI )
203
+ logger .Debug ("http request received" )
204
+
201
205
// If the gateway is behind a reverse proxy and mounted at a sub-path,
202
206
// the prefix header can be set to signal this sub-path.
203
207
// It will be prepended to links in directory listings and the index.html redirect.
@@ -210,6 +214,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
210
214
break
211
215
}
212
216
}
217
+ logger .Debugw ("sub-path (deprecrated)" , "prefix" , prefix )
213
218
}
214
219
215
220
// HostnameOption might have constructed an IPNS/IPFS path using the Host header.
@@ -242,7 +247,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
242
247
if u .RawQuery != "" { // preserve query if present
243
248
path = path + "?" + u .RawQuery
244
249
}
245
- http .Redirect (w , r , gopath .Join ("/" , prefix , u .Scheme , u .Host , path ), http .StatusMovedPermanently )
250
+
251
+ redirectURL := gopath .Join ("/" , prefix , u .Scheme , u .Host , path )
252
+ logger .Debugw ("uri param, redirect" , "to" , redirectURL , "status" , http .StatusMovedPermanently )
253
+ http .Redirect (w , r , redirectURL , http .StatusMovedPermanently )
246
254
return
247
255
}
248
256
@@ -263,6 +271,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
263
271
if prefix == "" && fixupSuperfluousNamespace (w , urlPath , r .URL .RawQuery ) {
264
272
// the error was due to redundant namespace, which we were able to fix
265
273
// by returning error/redirect page, nothing left to do here
274
+ logger .Debugw ("redundant namespace; noop" )
266
275
return
267
276
}
268
277
// unable to fix path, returning error
@@ -279,6 +288,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
279
288
return
280
289
default :
281
290
if i .servePretty404IfPresent (w , r , parsedPath ) {
291
+ logger .Debugw ("serve pretty 404 if present" )
282
292
return
283
293
}
284
294
@@ -345,6 +355,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
345
355
} else {
346
356
name = getFilename (urlPath )
347
357
}
358
+
359
+ logger .Debugw ("serving file" , "name" , name )
348
360
i .serveFile (w , r , name , modtime , f )
349
361
return
350
362
}
@@ -354,7 +366,8 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
354
366
return
355
367
}
356
368
357
- idx , err := i .api .Unixfs ().Get (r .Context (), ipath .Join (resolvedPath , "index.html" ))
369
+ idxPath := ipath .Join (resolvedPath , "index.html" )
370
+ idx , err := i .api .Unixfs ().Get (r .Context (), idxPath )
358
371
switch err .(type ) {
359
372
case nil :
360
373
dirwithoutslash := urlPath [len (urlPath )- 1 ] != '/'
@@ -366,7 +379,10 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
366
379
// preserve query parameters
367
380
suffix = suffix + "?" + r .URL .RawQuery
368
381
}
369
- http .Redirect (w , r , originalUrlPath + suffix , 302 )
382
+
383
+ redirectURL := originalUrlPath + suffix
384
+ logger .Debugw ("serving index.html file" , "to" , redirectURL , "status" , http .StatusFound , "path" , idxPath )
385
+ http .Redirect (w , r , redirectURL , http .StatusFound )
370
386
return
371
387
}
372
388
@@ -376,11 +392,12 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
376
392
return
377
393
}
378
394
395
+ logger .Debugw ("serving index.html file" , "path" , idxPath )
379
396
// write to request
380
397
i .serveFile (w , r , "index.html" , modtime , f )
381
398
return
382
399
case resolver.ErrNoLink :
383
- // no index.html; noop
400
+ logger . Debugw ( " no index.html; noop" , "path" , idxPath )
384
401
default :
385
402
internalWebError (w , err )
386
403
return
@@ -391,6 +408,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
391
408
// Note: this needs to occur before listingTemplate.Execute otherwise we get
392
409
// superfluous response.WriteHeader call from prometheus/client_golang
393
410
if w .Header ().Get ("Location" ) != "" {
411
+ logger .Debugw ("location moved permanently" , "status" , http .StatusMovedPermanently )
394
412
w .WriteHeader (http .StatusMovedPermanently )
395
413
return
396
414
}
@@ -399,6 +417,7 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
399
417
// type instead of relying on autodetection (which may fail).
400
418
w .Header ().Set ("Content-Type" , "text/html" )
401
419
if r .Method == http .MethodHead {
420
+ logger .Debug ("return as request's HTTP method is HEAD" )
402
421
return
403
422
}
404
423
@@ -490,8 +509,9 @@ func (i *gatewayHandler) getOrHeadHandler(w http.ResponseWriter, r *http.Request
490
509
Hash : hash ,
491
510
}
492
511
493
- err = listingTemplate .Execute (w , tplData )
494
- if err != nil {
512
+ logger .Debugw ("request processed" , "tplDataDNSLink" , dnslink , "tplDataSize" , size , "tplDataBackLink" , backLink , "tplDataHash" , hash , "duration" , time .Since (begin ))
513
+
514
+ if err := listingTemplate .Execute (w , tplData ); err != nil {
495
515
internalWebError (w , err )
496
516
return
497
517
}
@@ -568,7 +588,7 @@ func (i *gatewayHandler) servePretty404IfPresent(w http.ResponseWriter, r *http.
568
588
return false
569
589
}
570
590
571
- log .Debugf ("using pretty 404 file for %s" , parsedPath . String () )
591
+ log .Debugw ("using pretty 404 file" , "path" , parsedPath )
572
592
w .Header ().Set ("Content-Type" , ctype )
573
593
w .Header ().Set ("Content-Length" , strconv .FormatInt (size , 10 ))
574
594
w .WriteHeader (http .StatusNotFound )
@@ -585,6 +605,7 @@ func (i *gatewayHandler) postHandler(w http.ResponseWriter, r *http.Request) {
585
605
586
606
i .addUserHeaders (w ) // ok, _now_ write user's headers.
587
607
w .Header ().Set ("IPFS-Hash" , p .Cid ().String ())
608
+ log .Debugw ("CID created, http redirect" , "from" , r .URL , "to" , p , "status" , http .StatusCreated )
588
609
http .Redirect (w , r , p .String (), http .StatusCreated )
589
610
}
590
611
@@ -677,7 +698,10 @@ func (i *gatewayHandler) putHandler(w http.ResponseWriter, r *http.Request) {
677
698
678
699
i .addUserHeaders (w ) // ok, _now_ write user's headers.
679
700
w .Header ().Set ("IPFS-Hash" , newcid .String ())
680
- http .Redirect (w , r , gopath .Join (ipfsPathPrefix , newcid .String (), newPath ), http .StatusCreated )
701
+
702
+ redirectURL := gopath .Join (ipfsPathPrefix , newcid .String (), newPath )
703
+ log .Debugw ("CID replaced, redirect" , "from" , r .URL , "to" , redirectURL , "status" , http .StatusCreated )
704
+ http .Redirect (w , r , redirectURL , http .StatusCreated )
681
705
}
682
706
683
707
func (i * gatewayHandler ) deleteHandler (w http.ResponseWriter , r * http.Request ) {
@@ -748,8 +772,11 @@ func (i *gatewayHandler) deleteHandler(w http.ResponseWriter, r *http.Request) {
748
772
749
773
i .addUserHeaders (w ) // ok, _now_ write user's headers.
750
774
w .Header ().Set ("IPFS-Hash" , ncid .String ())
775
+
776
+ redirectURL := gopath .Join (ipfsPathPrefix + ncid .String (), directory )
751
777
// note: StatusCreated is technically correct here as we created a new resource.
752
- http .Redirect (w , r , gopath .Join (ipfsPathPrefix + ncid .String (), directory ), http .StatusCreated )
778
+ log .Debugw ("CID deleted, redirect" , "from" , r .RequestURI , "to" , redirectURL , "status" , http .StatusCreated )
779
+ http .Redirect (w , r , redirectURL , http .StatusCreated )
753
780
}
754
781
755
782
func (i * gatewayHandler ) addUserHeaders (w http.ResponseWriter ) {
0 commit comments