Skip to content

Commit

Permalink
fix: handle invalid image (#167)
Browse files Browse the repository at this point in the history
  • Loading branch information
ykzts authored Sep 11, 2019
1 parent ddedf6b commit 0f4ea33
Show file tree
Hide file tree
Showing 5 changed files with 153 additions and 3 deletions.
50 changes: 50 additions & 0 deletions response.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,50 @@
// Copyright (c) 2019 Yamagishi Kazutoshi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

// Package manael provides HTTP handler for processing images.
package manael // import "manael.org/x/manael"

import (
"bytes"
"fmt"
"io/ioutil"
"net/http"
)

// NewResponse returns response
func NewResponse(r *http.Request, status int) *http.Response {
header := make(http.Header)
statusText := http.StatusText(status)

body := fmt.Sprintf("%d %s", status, statusText)
buf := bytes.NewBufferString(body)

header.Set("Content-Type", "text/plain; charset=utf-8")

return &http.Response{
Status: statusText,
StatusCode: status,
Header: header,
Body: ioutil.NopCloser(buf),
ContentLength: int64(buf.Len()),
Request: r,
TransferEncoding: r.TransferEncoding,
}
}
86 changes: 86 additions & 0 deletions response_test.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,86 @@
// Copyright (c) 2019 Yamagishi Kazutoshi <[email protected]>
//
// Permission is hereby granted, free of charge, to any person obtaining a copy
// of this software and associated documentation files (the "Software"), to deal
// in the Software without restriction, including without limitation the rights
// to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
// copies of the Software, and to permit persons to whom the Software is
// furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
// AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
// LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
// OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
// SOFTWARE.

package manael_test // import "manael.org/x/manael"

import (
"io/ioutil"
"net/http"
"net/http/httptest"
"testing"

"manael.org/x/manael"
)

var responseTests = []struct {
statusCode int
status string
contentLength int64
body string
}{
{
http.StatusOK,
"OK",
6,
"200 OK",
},
{
http.StatusNotFound,
"Not Found",
13,
"404 Not Found",
},
{
http.StatusInternalServerError,
"Internal Server Error",
25,
"500 Internal Server Error",
},
}

func TestNewResponse(t *testing.T) {
for _, tc := range responseTests {
req := httptest.NewRequest(http.MethodGet, "https://manael.test/", nil)

resp := manael.NewResponse(req, tc.statusCode)
defer resp.Body.Close()

if got, want := resp.StatusCode, tc.statusCode; got != want {
t.Errorf("Status code is %d, want %d", got, want)
}

if got, want := resp.Status, tc.status; got != want {
t.Errorf("Status is %s, want %s", got, want)
}

if got, want := resp.ContentLength, tc.contentLength; got != want {
t.Errorf("Content length is %d, want %d", got, want)
}

body, err := ioutil.ReadAll(resp.Body)
if err != nil {
t.Fatal(err)
}

if got, want := string(body), tc.body; got != want {
t.Errorf("Response body is %s, want %s", got, want)
}
}
}
Empty file added testdata/invalid.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
6 changes: 5 additions & 1 deletion transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ package manael // import "manael.org/x/manael"
import (
"fmt"
"io/ioutil"
"log"
"net"
"net/http"
"net/url"
Expand Down Expand Up @@ -119,7 +120,10 @@ func (t *Transport) RoundTrip(req *http.Request) (*http.Response, error) {

buf, err := convert(resp.Body)
if err != nil {
return nil, err
resp = NewResponse(req2, http.StatusInternalServerError)
log.Printf("error: %v\n", err)

return resp, nil
}

resp.Body = ioutil.NopCloser(buf)
Expand Down
14 changes: 12 additions & 2 deletions transport_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,13 @@ var transportTests = []struct {
"text/plain; charset=utf-8",
"not image",
},
{
"image/webp,image/*,*/*",
"/invalid.png",
http.StatusInternalServerError,
"text/plain; charset=utf-8",
"not image",
},
}

func TestTransport_RoundTrip(t *testing.T) {
Expand All @@ -107,6 +114,9 @@ func TestTransport_RoundTrip(t *testing.T) {
mux.HandleFunc("/empty.txt", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "testdata/empty.txt")
})
mux.HandleFunc("/invalid.png", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, "testdata/invalid.png")
})

ts := httptest.NewServer(mux)
defer ts.Close()
Expand Down Expand Up @@ -398,7 +408,7 @@ func TestTransport_RoundTrip_noTransform(t *testing.T) {

var transportTests6 = []struct {
path string
xff string
xff string
}{
{
"/xff.txt",
Expand Down Expand Up @@ -441,4 +451,4 @@ func TestTransport_RoundTrip_xForwardedFor(t *testing.T) {
t.Errorf(`X-Forwarded-For is "%s", want "%s"`, got, want)
}
}
}
}

0 comments on commit 0f4ea33

Please sign in to comment.