From 48ae7904d923d415d8ad5c1250100ce9ff1cb807 Mon Sep 17 00:00:00 2001
From: Travis Ralston <travpc@gmail.com>
Date: Sun, 7 Jan 2018 21:42:55 -0700
Subject: [PATCH] Read the entire request body to prevent
 ERR_CONTENT_LENGTH_MISMATCH

Fixes #25 and https://github.com/vector-im/riot-web/issues/2585 (when using matrix-media-repo)
---
 .../turt2live/matrix-media-repo/client/r0/upload.go        | 7 +++++++
 1 file changed, 7 insertions(+)

diff --git a/src/github.com/turt2live/matrix-media-repo/client/r0/upload.go b/src/github.com/turt2live/matrix-media-repo/client/r0/upload.go
index ea0a8bb6..4cfcf3b7 100644
--- a/src/github.com/turt2live/matrix-media-repo/client/r0/upload.go
+++ b/src/github.com/turt2live/matrix-media-repo/client/r0/upload.go
@@ -1,6 +1,8 @@
 package r0
 
 import (
+	"io"
+	"io/ioutil"
 	"net/http"
 
 	"github.com/sirupsen/logrus"
@@ -39,11 +41,16 @@ func UploadMedia(w http.ResponseWriter, r *http.Request, i rcontext.RequestInfo)
 	svc := services.CreateMediaService(i)
 
 	if svc.IsTooLarge(r.ContentLength, r.Header.Get("Content-Length")) {
+		io.Copy(ioutil.Discard, r.Body) // Ditch the entire request
+		defer r.Body.Close()
 		return client.RequestTooLarge()
 	}
 
 	media, err := svc.UploadMedia(r.Body, contentType, filename, userId, r.Host)
 	if err != nil {
+		io.Copy(ioutil.Discard, r.Body) // Ditch the entire request
+		defer r.Body.Close()
+
 		i.Log.Error("Unexpected error storing media: " + err.Error())
 		return client.InternalServerError("Unexpected Error")
 	}
-- 
GitLab