@@ -2,8 +2,9 @@ package main
22
33import (
44 "encoding/base64"
5- "io/ioutil "
5+ "io"
66 "net/http"
7+ "os"
78 "strings"
89)
910
@@ -30,7 +31,21 @@ func fetch(ctx Context) (*http.Response, bool) {
3031 return & http.Response {
3132 StatusCode : 200 ,
3233 Status : "OK" ,
33- Body : ioutil .NopCloser (strings .NewReader (data )),
34+ Body : io .NopCloser (strings .NewReader (data )),
35+ }, true
36+ }
37+
38+ if ctx .Url .Scheme == "file" && ctx .Url .Host == "sourcerer" {
39+ f , err := os .Open (strings .TrimPrefix (ctx .Url .Path , "/" ))
40+ if err != nil {
41+ Error (ctx .Depth , "Failed to read file:" , err )
42+ return nil , false
43+ }
44+
45+ return & http.Response {
46+ StatusCode : 200 ,
47+ Status : "OK" ,
48+ Body : io .NopCloser (f ),
3449 }, true
3550 }
3651
@@ -43,15 +58,25 @@ func fetch(ctx Context) (*http.Response, bool) {
4358 Info (ctx .Depth , "Fetching URL:" , ctx .Url .String ())
4459 res , err := http .Get (ctx .Url .String ())
4560 if err != nil {
61+ res .Body .Close ()
4662 Error (ctx .Depth , "Failed to fetch URL:" , err )
4763 return nil , false
4864 }
4965
5066 if res .StatusCode < 200 || res .StatusCode >= 300 {
67+ res .Body .Close ()
5168 Warn (ctx .Depth , "URL responded with status:" , res .Status )
5269 return nil , false
5370 }
5471
72+ // if res.Header.Get("Content-Type") == "" {
73+ // res.Body.Close()
74+ // Warn(ctx.Depth, "URL responded with no content type")
75+ // ext := filepath.Ext(ctx.Url.Path)
76+ // mimeType := mime.TypeByExtension(ext)
77+ // res.Header.Set("Content-Type", mimeType)
78+ // }
79+
5580 Success (ctx .Depth , "URL responded with status:" , res .Status )
5681 return res , true
5782}
0 commit comments