mirror of
https://github.com/minio/minio.git
synced 2025-01-12 15:33:22 -05:00
Remove redundant WriteHeader calls
This commit is contained in:
parent
5c5761ae16
commit
317637fcb8
@ -18,7 +18,6 @@ package api
|
||||
|
||||
import (
|
||||
"net/http"
|
||||
"strconv"
|
||||
|
||||
"github.com/gorilla/mux"
|
||||
"github.com/minio/minio/pkg/iodine"
|
||||
@ -87,10 +86,7 @@ func (server *minioAPI) listMultipartUploadsHandler(w http.ResponseWriter, req *
|
||||
response := generateListMultipartUploadsResult(bucket, resources)
|
||||
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
|
||||
// write headers
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
// set content-length to the size of the body
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
|
||||
// write body
|
||||
w.Write(encodedSuccessResponse)
|
||||
}
|
||||
@ -141,10 +137,7 @@ func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Requ
|
||||
response := generateListObjectsResponse(bucket, objects, resources)
|
||||
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
|
||||
// write headers
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
// set content-length to the size of the body
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
|
||||
// write body
|
||||
w.Write(encodedSuccessResponse)
|
||||
}
|
||||
@ -184,10 +177,7 @@ func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Requ
|
||||
response := generateListBucketsResponse(buckets)
|
||||
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
|
||||
// write headers
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
// set content-length to the size of the body
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
|
||||
// write response
|
||||
w.Write(encodedSuccessResponse)
|
||||
}
|
||||
|
@ -202,9 +202,10 @@ func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
||||
if ignoreNotImplementedObjectResources(r) || ignoreNotImplementedBucketResources(r) {
|
||||
error := getErrorCode(NotImplemented)
|
||||
errorResponse := getErrorResponse(error, "")
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
encodeErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodeErrorResponse))
|
||||
w.WriteHeader(error.HTTPStatusCode)
|
||||
w.Write(encodeErrorResponse(errorResponse, acceptsContentType))
|
||||
w.Write(encodeErrorResponse)
|
||||
return
|
||||
}
|
||||
h.handler.ServeHTTP(w, r)
|
||||
|
@ -230,10 +230,7 @@ func (server *minioAPI) newMultipartUploadHandler(w http.ResponseWriter, req *ht
|
||||
response := generateInitiateMultipartUploadResult(bucket, object, uploadID)
|
||||
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
|
||||
// write headers
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
// set content-length to the size of the body
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
|
||||
// write body
|
||||
w.Write(encodedSuccessResponse)
|
||||
case drivers.ObjectExists:
|
||||
@ -349,7 +346,7 @@ func (server *minioAPI) abortMultipartUploadHandler(w http.ResponseWriter, req *
|
||||
err := server.driver.AbortMultipartUpload(bucket, object, objectResourcesMetadata.UploadID)
|
||||
switch err := iodine.ToError(err).(type) {
|
||||
case nil:
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), 0)
|
||||
w.WriteHeader(http.StatusNoContent)
|
||||
case drivers.InvalidUploadID:
|
||||
writeErrorResponse(w, req, NoSuchUpload, acceptsContentType, req.URL.Path)
|
||||
@ -381,10 +378,7 @@ func (server *minioAPI) listObjectPartsHandler(w http.ResponseWriter, req *http.
|
||||
response := generateListPartsResult(objectResourcesMetadata)
|
||||
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
|
||||
// write headers
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
// set content-length to the size of the body
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
|
||||
// write body
|
||||
w.Write(encodedSuccessResponse)
|
||||
case drivers.InvalidUploadID:
|
||||
@ -431,10 +425,7 @@ func (server *minioAPI) completeMultipartUploadHandler(w http.ResponseWriter, re
|
||||
response := generateCompleteMultpartUploadResult(bucket, object, "", etag)
|
||||
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
|
||||
// write headers
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
// set content-length to the size of the body
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
|
||||
w.WriteHeader(http.StatusOK)
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
|
||||
// write body
|
||||
w.Write(encodedSuccessResponse)
|
||||
case drivers.InvalidUploadID:
|
||||
|
@ -19,7 +19,6 @@ package api
|
||||
import (
|
||||
"net/http"
|
||||
"sort"
|
||||
"strconv"
|
||||
|
||||
"github.com/minio/minio/pkg/storage/drivers"
|
||||
)
|
||||
@ -188,7 +187,7 @@ func generateListMultipartUploadsResult(bucket string, metadata drivers.BucketMu
|
||||
|
||||
// writeSuccessResponse write success headers
|
||||
func writeSuccessResponse(w http.ResponseWriter, acceptsContentType contentType) {
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), 0)
|
||||
w.WriteHeader(http.StatusOK)
|
||||
}
|
||||
|
||||
@ -199,11 +198,9 @@ func writeErrorResponse(w http.ResponseWriter, req *http.Request, errorType int,
|
||||
errorResponse := getErrorResponse(error, resource)
|
||||
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
|
||||
// set common headers
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType))
|
||||
// set content-length to size of error response
|
||||
w.Header().Set("Content-Length", strconv.Itoa(len(encodedErrorResponse)))
|
||||
// set headers
|
||||
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedErrorResponse))
|
||||
// write Header
|
||||
w.WriteHeader(error.HTTPStatusCode)
|
||||
// write body
|
||||
// write error body
|
||||
w.Write(encodedErrorResponse)
|
||||
}
|
||||
|
@ -35,13 +35,13 @@ type encoder interface {
|
||||
//// helpers
|
||||
|
||||
// Write http common headers
|
||||
func setCommonHeaders(w http.ResponseWriter, acceptsType string) {
|
||||
func setCommonHeaders(w http.ResponseWriter, acceptsType string, contentLength int) {
|
||||
w.Header().Set("Server", "Minio")
|
||||
w.Header().Set("Accept-Ranges", "bytes")
|
||||
w.Header().Set("Content-Type", acceptsType)
|
||||
w.Header().Set("Connection", "close")
|
||||
// should be set to '0' by default
|
||||
w.Header().Set("Content-Length", "0")
|
||||
w.Header().Set("Content-Length", strconv.Itoa(contentLength))
|
||||
}
|
||||
|
||||
// Write error response headers
|
||||
@ -66,16 +66,16 @@ func encodeErrorResponse(response interface{}, acceptsType contentType) []byte {
|
||||
func setObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata) {
|
||||
lastModified := metadata.Created.Format(time.RFC1123)
|
||||
// common headers
|
||||
setCommonHeaders(w, metadata.ContentType)
|
||||
setCommonHeaders(w, metadata.ContentType, int(metadata.Size))
|
||||
// object related headers
|
||||
w.Header().Set("ETag", metadata.Md5)
|
||||
w.Header().Set("Last-Modified", lastModified)
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10))
|
||||
}
|
||||
|
||||
// Write range object header
|
||||
func setRangeObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata, contentRange *httpRange) {
|
||||
// set common headers
|
||||
setCommonHeaders(w, metadata.ContentType)
|
||||
setCommonHeaders(w, metadata.ContentType, int(metadata.Size))
|
||||
// set object headers
|
||||
setObjectHeaders(w, metadata)
|
||||
// set content range
|
||||
|
@ -44,15 +44,24 @@ type LogMessage struct {
|
||||
|
||||
// LogWriter is used to capture status for log messages
|
||||
type LogWriter struct {
|
||||
http.ResponseWriter
|
||||
LogMessage *LogMessage
|
||||
ResponseWriter http.ResponseWriter
|
||||
LogMessage *LogMessage
|
||||
}
|
||||
|
||||
// WriteHeader writes headers and stores status in LogMessage
|
||||
func (w *LogWriter) WriteHeader(status int) {
|
||||
w.LogMessage.Status = status
|
||||
w.ResponseWriter.WriteHeader(status)
|
||||
w.ResponseWriter.Header()
|
||||
}
|
||||
|
||||
// Header Dummy wrapper for LogWriter
|
||||
func (w *LogWriter) Header() http.Header {
|
||||
return w.ResponseWriter.Header()
|
||||
}
|
||||
|
||||
// Write Dummy wrapper for LogWriter
|
||||
func (w *LogWriter) Write(data []byte) (int, error) {
|
||||
return w.ResponseWriter.Write(data)
|
||||
}
|
||||
|
||||
func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
@ -60,12 +69,12 @@ func (h *logHandler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
||||
StartTime: time.Now().UTC(),
|
||||
}
|
||||
logWriter := &LogWriter{ResponseWriter: w, LogMessage: logMessage}
|
||||
h.Handler.ServeHTTP(logWriter, req)
|
||||
logMessage.ResponseHeaders = w.Header()
|
||||
logMessage.Request = req
|
||||
logMessage.Duration = time.Now().UTC().Sub(logMessage.StartTime)
|
||||
js, _ := json.Marshal(logMessage)
|
||||
h.Logger <- string(js)
|
||||
h.Handler.ServeHTTP(logWriter, req)
|
||||
}
|
||||
|
||||
// LogHandler logs requests
|
||||
|
Loading…
Reference in New Issue
Block a user