2022-11-29 01:30:22PM
This commit is contained in:
parent
b994318beb
commit
fe75e7067b
2 changed files with 34 additions and 1 deletions
|
|
@ -20,6 +20,9 @@ import (
|
||||||
"oss.terrastruct.com/d2/lib/xmain"
|
"oss.terrastruct.com/d2/lib/xmain"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// 32 MB
|
||||||
|
var max_img_size int64 = 33_554_432
|
||||||
|
|
||||||
var imageRe = regexp.MustCompile(`<image href="([^"]+)"`)
|
var imageRe = regexp.MustCompile(`<image href="([^"]+)"`)
|
||||||
|
|
||||||
type resp struct {
|
type resp struct {
|
||||||
|
|
@ -132,7 +135,8 @@ func fetch(ctx context.Context, href string) (string, error) {
|
||||||
if imgResp.StatusCode != 200 {
|
if imgResp.StatusCode != 200 {
|
||||||
return "", fmt.Errorf("img %s returned status code %d", href, imgResp.StatusCode)
|
return "", fmt.Errorf("img %s returned status code %d", href, imgResp.StatusCode)
|
||||||
}
|
}
|
||||||
data, err := ioutil.ReadAll(imgResp.Body)
|
r := http.MaxBytesReader(nil, imgResp.Body, max_img_size)
|
||||||
|
data, err := ioutil.ReadAll(r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return "", err
|
return "", err
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
package imgbundler
|
package imgbundler
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/rand"
|
||||||
_ "embed"
|
_ "embed"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
@ -115,6 +116,34 @@ width="328" height="587" viewBox="-100 -131 328 587"><style type="text/css">
|
||||||
t.Fatal("no png image inserted")
|
t.Fatal("no png image inserted")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Test almost too large response
|
||||||
|
transport = roundTripFunc(func(req *http.Request) *http.Response {
|
||||||
|
respRecorder := httptest.NewRecorder()
|
||||||
|
bytes := make([]byte, max_img_size-1)
|
||||||
|
rand.Read(bytes)
|
||||||
|
respRecorder.Write(bytes)
|
||||||
|
respRecorder.WriteHeader(200)
|
||||||
|
return respRecorder.Result()
|
||||||
|
})
|
||||||
|
_, err = InlineRemote(ms, []byte(sampleSVG))
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
// Test too large response
|
||||||
|
transport = roundTripFunc(func(req *http.Request) *http.Response {
|
||||||
|
respRecorder := httptest.NewRecorder()
|
||||||
|
bytes := make([]byte, max_img_size+1)
|
||||||
|
rand.Read(bytes)
|
||||||
|
respRecorder.Write(bytes)
|
||||||
|
respRecorder.WriteHeader(200)
|
||||||
|
return respRecorder.Result()
|
||||||
|
})
|
||||||
|
_, err = InlineRemote(ms, []byte(sampleSVG))
|
||||||
|
if err == nil {
|
||||||
|
t.Fatal("expected error")
|
||||||
|
}
|
||||||
|
|
||||||
// Test error response
|
// Test error response
|
||||||
transport = roundTripFunc(func(req *http.Request) *http.Response {
|
transport = roundTripFunc(func(req *http.Request) *http.Response {
|
||||||
respRecorder := httptest.NewRecorder()
|
respRecorder := httptest.NewRecorder()
|
||||||
|
|
|
||||||
Loading…
Reference in a new issue