Update content length of request when rewriting the content

This commit is contained in:
Steven Zou 2018-07-24 23:10:42 +08:00
parent 7ee2e1053e
commit 07bfad968a

View File

@ -33,6 +33,7 @@ const (
formFieldNameForChart = "chart"
formFiledNameForProv = "prov"
headerContentType = "Content-Type"
contentTypeMultipart = "multipart/form-data"
)
//chartController is a singleton instance
@ -170,7 +171,8 @@ func (cra *ChartRepositoryAPI) UploadChartVersion() {
return
}
//Rewrite file content
//Rewrite file content if the content type is "multipart/form-data"
if isMultipartFormData(cra.Ctx.Request) {
formFiles := make([]formFile, 0)
formFiles = append(formFiles,
formFile{
@ -184,6 +186,7 @@ func (cra *ChartRepositoryAPI) UploadChartVersion() {
chartserver.WriteInternalError(cra.Ctx.ResponseWriter, err)
return
}
}
chartController.GetManipulationHandler().UploadChartVersion(cra.Ctx.ResponseWriter, cra.Ctx.Request)
}
@ -195,7 +198,8 @@ func (cra *ChartRepositoryAPI) UploadChartProvFile() {
return
}
//Rewrite file content
//Rewrite file content if the content type is "multipart/form-data"
if isMultipartFormData(cra.Ctx.Request) {
formFiles := make([]formFile, 0)
formFiles = append(formFiles,
formFile{
@ -206,6 +210,7 @@ func (cra *ChartRepositoryAPI) UploadChartProvFile() {
chartserver.WriteInternalError(cra.Ctx.ResponseWriter, err)
return
}
}
chartController.GetManipulationHandler().UploadProvenanceFile(cra.Ctx.ResponseWriter, cra.Ctx.Request)
}
@ -401,7 +406,13 @@ func (cra *ChartRepositoryAPI) rewriteFileContent(files []formFile, request *htt
}
request.Header.Set(headerContentType, w.FormDataContentType())
request.ContentLength = int64(body.Len())
request.Body = ioutil.NopCloser(&body)
return nil
}
//Check if the request content type is "multipart/form-data"
func isMultipartFormData(req *http.Request) bool {
return strings.Contains(req.Header.Get(headerContentType), contentTypeMultipart)
}