Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
6 changed files
with
152 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,48 @@ | ||
package gltf | ||
|
||
import ( | ||
"errors" | ||
"io" | ||
"net/http" | ||
"net/url" | ||
"os" | ||
) | ||
|
||
// RelativeFileHandler implements a secure ReadHandler supporting relative paths. | ||
// If Dir is empty the os.Getws will be used. It comes with directory traversal protection. | ||
type RelativeFileHandler struct { | ||
Dir string | ||
} | ||
|
||
// ReadFull should as io.ReadFull in terms of reading the external resource. | ||
func (h *RelativeFileHandler) ReadFull(uri string, data []byte) (err error) { | ||
dir := h.Dir | ||
if dir == "" { | ||
if dir, err = os.Getwd(); err != nil { | ||
return | ||
} | ||
} | ||
var f http.File | ||
f, err = http.Dir(dir).Open(uri) | ||
if err != nil { | ||
return | ||
} | ||
_, err = io.ReadFull(f, data) | ||
return | ||
} | ||
|
||
// ProtocolRegistry implements a secure ProtocolReadHandler as a map of supported schemes. | ||
type ProtocolRegistry map[string]ReadHandler | ||
|
||
// ReadFull should as io.ReadFull in terms of reading the external resource. | ||
// An error is returned when the scheme is not supported. | ||
func (reg ProtocolRegistry) ReadFull(uri string, data []byte) error { | ||
u, err := url.Parse(uri) | ||
if err != nil { | ||
return err | ||
} | ||
if f, ok := reg[u.Scheme]; ok { | ||
return f.ReadFull(uri, data) | ||
} | ||
return errors.New("gltf: not supported scheme") | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,47 @@ | ||
package gltf | ||
|
||
import "testing" | ||
|
||
func TestRelativeFileHandler_ReadFull(t *testing.T) { | ||
type args struct { | ||
uri string | ||
data []byte | ||
} | ||
tests := []struct { | ||
name string | ||
h *RelativeFileHandler | ||
args args | ||
wantErr bool | ||
}{ | ||
{"no dir", new(RelativeFileHandler), args{"a.bin", []byte{}}, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.h.ReadFull(tt.args.uri, tt.args.data); (err != nil) != tt.wantErr { | ||
t.Errorf("RelativeFileHandler.ReadFull() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} | ||
|
||
func TestProtocolRegistry_ReadFull(t *testing.T) { | ||
type args struct { | ||
uri string | ||
data []byte | ||
} | ||
tests := []struct { | ||
name string | ||
reg ProtocolRegistry | ||
args args | ||
wantErr bool | ||
}{ | ||
{"invalid url", make(ProtocolRegistry), args{"%$·$·23", []byte{}}, true}, | ||
} | ||
for _, tt := range tests { | ||
t.Run(tt.name, func(t *testing.T) { | ||
if err := tt.reg.ReadFull(tt.args.uri, tt.args.data); (err != nil) != tt.wantErr { | ||
t.Errorf("ProtocolRegistry.ReadFull() error = %v, wantErr %v", err, tt.wantErr) | ||
} | ||
}) | ||
} | ||
} |