Skip to content

Commit 52821a1

Browse files
committed
handle local files
1 parent 8e30147 commit 52821a1

File tree

6 files changed

+81
-14
lines changed

6 files changed

+81
-14
lines changed

fetch.go

Lines changed: 27 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,8 +2,9 @@ package main
22

33
import (
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
}

html.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -8,6 +8,7 @@ import (
88
)
99

1010
func fromHtml(ctx Context, res *http.Response) {
11+
defer res.Body.Close()
1112
Info(ctx.Depth, "Processing HTML...")
1213

1314
parsedHtml, err := html.Parse(res.Body)

js.go

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,18 +1,19 @@
11
package main
22

33
import (
4-
"io/ioutil"
4+
"io"
55
"net/http"
66
"net/url"
77
"strings"
88
)
99

1010
func fromJs(ctx Context, res *http.Response) {
11+
defer res.Body.Close()
1112
Info(ctx.Depth, "Processing JavaScript...")
1213

1314
var sourceMapUrl url.URL
1415

15-
str, err := ioutil.ReadAll(res.Body)
16+
str, err := io.ReadAll(res.Body)
1617
if err != nil {
1718
Error(ctx.Depth, "Failed to read body:", err)
1819
return

main.go

Lines changed: 28 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,11 +32,19 @@ func main() {
3232
dir := filepath.Join(cwd, name)
3333

3434
if err := os.Mkdir(dir, os.ModePerm); err != nil {
35-
Error(0, "Failed to create output directory", err)
36-
return
35+
if !os.IsExist(err) {
36+
Error(0, "Failed to create output directory", err)
37+
return
38+
}
39+
40+
children, err := os.ReadDir(dir)
41+
if err == nil && len(children) > 0 {
42+
Error(0, "Cannot write into existing non-empty directory")
43+
return
44+
}
3745
}
3846

39-
for _, arg := range args {
47+
for _, arg := range args[1:] {
4048
if strings.HasPrefix(arg, "http") {
4149
url, err := url.Parse(arg)
4250
if err != nil {
@@ -52,6 +60,23 @@ func main() {
5260
}
5361

5462
fromUrl(ctx)
63+
} else if _, err := os.Stat(arg); err == nil {
64+
url := url.URL{
65+
Scheme: "file",
66+
Host: "sourcerer",
67+
Path: arg,
68+
}
69+
70+
ctx := Context{
71+
Dir: dir,
72+
Url: url,
73+
Cache: make(map[string]struct{}),
74+
Depth: 0,
75+
}
76+
77+
fromUrl(ctx)
78+
} else {
79+
Warn(0, "Unrecognized arg, expected URL or file", arg)
5580
}
5681
}
5782
}

sourceMap.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -34,6 +34,7 @@ func pathify(path string) (string, error) {
3434
}
3535

3636
func fromSourceMap(ctx Context, res *http.Response) {
37+
defer res.Body.Close()
3738
Info(ctx.Depth, "Processing source map...")
3839

3940
var sourceMap SourceMap

url.go

Lines changed: 21 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package main
22

33
import (
4+
"path/filepath"
45
"strings"
56
)
67

@@ -10,14 +11,10 @@ func fromUrl(ctx Context) {
1011
return
1112
}
1213

13-
contentType := res.Header.Get("Content-Type")
14-
if contentType == "" {
15-
Warn(ctx.Depth, "URL responded with no content type:", contentType)
16-
return
17-
}
14+
defer res.Body.Close()
1815

1916
// clean up text/html; charset=utf-8
20-
contentType = strings.SplitN(contentType, ";", 2)[0]
17+
contentType := strings.SplitN(res.Header.Get("Content-Type"), ";", 2)[0]
2118

2219
switch contentType {
2320
case "text/html":
@@ -36,6 +33,23 @@ func fromUrl(ctx Context) {
3633
fromSourceMap(ctx, res)
3734

3835
default:
39-
Error(ctx.Depth, "URL responded with unknown content type:", contentType)
36+
ext := filepath.Ext(ctx.Url.Path)
37+
switch ext {
38+
case ".htm", ".html":
39+
fromHtml(ctx, res)
40+
41+
case ".css":
42+
fromCss(ctx, res)
43+
44+
case ".js":
45+
fromJs(ctx, res)
46+
47+
case ".map":
48+
fromSourceMap(ctx, res)
49+
50+
default:
51+
Error(ctx.Depth, "URL responded with unknown content type or extension:", contentType, ext)
52+
53+
}
4054
}
4155
}

0 commit comments

Comments
 (0)