Skip to content

Commit

Permalink
[RSDK-1273] Error return from intelrealserver causes panic in gostream (
Browse files Browse the repository at this point in the history
  • Loading branch information
bazile-clyde authored Feb 13, 2023
1 parent b0f2963 commit 2dd02ec
Show file tree
Hide file tree
Showing 2 changed files with 29 additions and 0 deletions.
5 changes: 5 additions & 0 deletions components/camera/videosource/helpers.go
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import (
"image"
"io"
"net/http"
"strings"

"github.com/edaniels/golog"
"github.com/edaniels/gostream"
Expand All @@ -22,6 +23,10 @@ import (
// detected by http.DetectContentType.
func getMIMETypeFromData(ctx context.Context, data []byte) (string, error) {
detectedMimeType := http.DetectContentType(data)
if !strings.Contains(detectedMimeType, "image") {
return "", errors.Errorf("cannot decode image from MIME type '%s'", detectedMimeType)
}

requestedMime := gostream.MIMETypeHint(ctx, "")
actualMime, isLazy := utils.CheckLazyMIMEType(requestedMime)
if actualMime == utils.MimeTypeRawRGBA {
Expand Down
24 changes: 24 additions & 0 deletions components/camera/videosource/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -213,3 +213,27 @@ func TestDualServerSource(t *testing.T) {
test.That(t, pc2, test.ShouldResemble, pc1)
test.That(t, cam2.Close(context.Background()), test.ShouldBeNil)
}

func TestServerError(t *testing.T) {
logger := golog.NewTestLogger(t)
//nolint:dogsled
router, _, _, _ := createTestRouter(t)
svr := httptest.NewServer(router)
defer svr.Close()

attrs := ServerAttrs{
// we expect a 404 error of MIME type "text/plain"
URL: svr.URL + "/bad_path",
Stream: "color",
}

cam, err := NewServerSource(context.Background(), &attrs, logger)
test.That(t, err, test.ShouldBeNil)

lazyCtx := gostream.WithMIMETypeHint(context.Background(), utils.WithLazyMIMEType(utils.MimeTypeJPEG))
img, release, err := camera.ReadImage(lazyCtx, cam)

test.That(t, err, test.ShouldNotBeNil)
test.That(t, img, test.ShouldBeNil)
test.That(t, release, test.ShouldBeNil)
}

0 comments on commit 2dd02ec

Please sign in to comment.