Remove redundant WriteHeader calls

master
Harshavardhana 10 years ago
parent 5c5761ae16
commit 317637fcb8
  1. 16
      pkg/api/api_bucket_handlers.go
  2. 5
      pkg/api/api_generic_handlers.go
  3. 17
      pkg/api/api_object_handlers.go
  4. 11
      pkg/api/api_response.go
  5. 10
      pkg/api/headers.go
  6. 17
      pkg/api/logging/logging.go

@ -18,7 +18,6 @@ package api
import ( import (
"net/http" "net/http"
"strconv"
"github.com/gorilla/mux" "github.com/gorilla/mux"
"github.com/minio/minio/pkg/iodine" "github.com/minio/minio/pkg/iodine"
@ -87,10 +86,7 @@ func (server *minioAPI) listMultipartUploadsHandler(w http.ResponseWriter, req *
response := generateListMultipartUploadsResult(bucket, resources) response := generateListMultipartUploadsResult(bucket, resources)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
// set content-length to the size of the body
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
w.WriteHeader(http.StatusOK)
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
} }
@ -141,10 +137,7 @@ func (server *minioAPI) listObjectsHandler(w http.ResponseWriter, req *http.Requ
response := generateListObjectsResponse(bucket, objects, resources) response := generateListObjectsResponse(bucket, objects, resources)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
// set content-length to the size of the body
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
w.WriteHeader(http.StatusOK)
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
} }
@ -184,10 +177,7 @@ func (server *minioAPI) listBucketsHandler(w http.ResponseWriter, req *http.Requ
response := generateListBucketsResponse(buckets) response := generateListBucketsResponse(buckets)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
// set content-length to the size of the body
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
w.WriteHeader(http.StatusOK)
// write response // write response
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
} }

@ -202,9 +202,10 @@ func (h resourceHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if ignoreNotImplementedObjectResources(r) || ignoreNotImplementedBucketResources(r) { if ignoreNotImplementedObjectResources(r) || ignoreNotImplementedBucketResources(r) {
error := getErrorCode(NotImplemented) error := getErrorCode(NotImplemented)
errorResponse := getErrorResponse(error, "") errorResponse := getErrorResponse(error, "")
setCommonHeaders(w, getContentTypeString(acceptsContentType)) encodeErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodeErrorResponse))
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
w.Write(encodeErrorResponse(errorResponse, acceptsContentType)) w.Write(encodeErrorResponse)
return return
} }
h.handler.ServeHTTP(w, r) h.handler.ServeHTTP(w, r)

@ -230,10 +230,7 @@ func (server *minioAPI) newMultipartUploadHandler(w http.ResponseWriter, req *ht
response := generateInitiateMultipartUploadResult(bucket, object, uploadID) response := generateInitiateMultipartUploadResult(bucket, object, uploadID)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
// set content-length to the size of the body
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
w.WriteHeader(http.StatusOK)
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
case drivers.ObjectExists: case drivers.ObjectExists:
@ -349,7 +346,7 @@ func (server *minioAPI) abortMultipartUploadHandler(w http.ResponseWriter, req *
err := server.driver.AbortMultipartUpload(bucket, object, objectResourcesMetadata.UploadID) err := server.driver.AbortMultipartUpload(bucket, object, objectResourcesMetadata.UploadID)
switch err := iodine.ToError(err).(type) { switch err := iodine.ToError(err).(type) {
case nil: case nil:
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), 0)
w.WriteHeader(http.StatusNoContent) w.WriteHeader(http.StatusNoContent)
case drivers.InvalidUploadID: case drivers.InvalidUploadID:
writeErrorResponse(w, req, NoSuchUpload, acceptsContentType, req.URL.Path) writeErrorResponse(w, req, NoSuchUpload, acceptsContentType, req.URL.Path)
@ -381,10 +378,7 @@ func (server *minioAPI) listObjectPartsHandler(w http.ResponseWriter, req *http.
response := generateListPartsResult(objectResourcesMetadata) response := generateListPartsResult(objectResourcesMetadata)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
// set content-length to the size of the body
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
w.WriteHeader(http.StatusOK)
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
case drivers.InvalidUploadID: case drivers.InvalidUploadID:
@ -431,10 +425,7 @@ func (server *minioAPI) completeMultipartUploadHandler(w http.ResponseWriter, re
response := generateCompleteMultpartUploadResult(bucket, object, "", etag) response := generateCompleteMultpartUploadResult(bucket, object, "", etag)
encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType) encodedSuccessResponse := encodeSuccessResponse(response, acceptsContentType)
// write headers // write headers
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedSuccessResponse))
// set content-length to the size of the body
w.Header().Set("Content-Length", strconv.Itoa(len(encodedSuccessResponse)))
w.WriteHeader(http.StatusOK)
// write body // write body
w.Write(encodedSuccessResponse) w.Write(encodedSuccessResponse)
case drivers.InvalidUploadID: case drivers.InvalidUploadID:

@ -19,7 +19,6 @@ package api
import ( import (
"net/http" "net/http"
"sort" "sort"
"strconv"
"github.com/minio/minio/pkg/storage/drivers" "github.com/minio/minio/pkg/storage/drivers"
) )
@ -188,7 +187,7 @@ func generateListMultipartUploadsResult(bucket string, metadata drivers.BucketMu
// writeSuccessResponse write success headers // writeSuccessResponse write success headers
func writeSuccessResponse(w http.ResponseWriter, acceptsContentType contentType) { func writeSuccessResponse(w http.ResponseWriter, acceptsContentType contentType) {
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), 0)
w.WriteHeader(http.StatusOK) w.WriteHeader(http.StatusOK)
} }
@ -199,11 +198,9 @@ func writeErrorResponse(w http.ResponseWriter, req *http.Request, errorType int,
errorResponse := getErrorResponse(error, resource) errorResponse := getErrorResponse(error, resource)
encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType) encodedErrorResponse := encodeErrorResponse(errorResponse, acceptsContentType)
// set common headers // set common headers
setCommonHeaders(w, getContentTypeString(acceptsContentType)) setCommonHeaders(w, getContentTypeString(acceptsContentType), len(encodedErrorResponse))
// set content-length to size of error response // write Header
w.Header().Set("Content-Length", strconv.Itoa(len(encodedErrorResponse)))
// set headers
w.WriteHeader(error.HTTPStatusCode) w.WriteHeader(error.HTTPStatusCode)
// write body // write error body
w.Write(encodedErrorResponse) w.Write(encodedErrorResponse)
} }

@ -35,13 +35,13 @@ type encoder interface {
//// helpers //// helpers
// Write http common headers // 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("Server", "Minio")
w.Header().Set("Accept-Ranges", "bytes") w.Header().Set("Accept-Ranges", "bytes")
w.Header().Set("Content-Type", acceptsType) w.Header().Set("Content-Type", acceptsType)
w.Header().Set("Connection", "close") w.Header().Set("Connection", "close")
// should be set to '0' by default // 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 // Write error response headers
@ -66,16 +66,16 @@ func encodeErrorResponse(response interface{}, acceptsType contentType) []byte {
func setObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata) { func setObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata) {
lastModified := metadata.Created.Format(time.RFC1123) lastModified := metadata.Created.Format(time.RFC1123)
// common headers // 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("ETag", metadata.Md5)
w.Header().Set("Last-Modified", lastModified) w.Header().Set("Last-Modified", lastModified)
w.Header().Set("Content-Length", strconv.FormatInt(metadata.Size, 10))
} }
// Write range object header // Write range object header
func setRangeObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata, contentRange *httpRange) { func setRangeObjectHeaders(w http.ResponseWriter, metadata drivers.ObjectMetadata, contentRange *httpRange) {
// set common headers // set common headers
setCommonHeaders(w, metadata.ContentType) setCommonHeaders(w, metadata.ContentType, int(metadata.Size))
// set object headers // set object headers
setObjectHeaders(w, metadata) setObjectHeaders(w, metadata)
// set content range // set content range

@ -44,15 +44,24 @@ type LogMessage struct {
// LogWriter is used to capture status for log messages // LogWriter is used to capture status for log messages
type LogWriter struct { type LogWriter struct {
http.ResponseWriter ResponseWriter http.ResponseWriter
LogMessage *LogMessage LogMessage *LogMessage
} }
// WriteHeader writes headers and stores status in LogMessage // WriteHeader writes headers and stores status in LogMessage
func (w *LogWriter) WriteHeader(status int) { func (w *LogWriter) WriteHeader(status int) {
w.LogMessage.Status = status w.LogMessage.Status = status
w.ResponseWriter.WriteHeader(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) { 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(), StartTime: time.Now().UTC(),
} }
logWriter := &LogWriter{ResponseWriter: w, LogMessage: logMessage} logWriter := &LogWriter{ResponseWriter: w, LogMessage: logMessage}
h.Handler.ServeHTTP(logWriter, req)
logMessage.ResponseHeaders = w.Header() logMessage.ResponseHeaders = w.Header()
logMessage.Request = req logMessage.Request = req
logMessage.Duration = time.Now().UTC().Sub(logMessage.StartTime) logMessage.Duration = time.Now().UTC().Sub(logMessage.StartTime)
js, _ := json.Marshal(logMessage) js, _ := json.Marshal(logMessage)
h.Logger <- string(js) h.Logger <- string(js)
h.Handler.ServeHTTP(logWriter, req)
} }
// LogHandler logs requests // LogHandler logs requests

Loading…
Cancel
Save