1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126
|
package contentprocessor
import (
"bytes"
"io"
"net/http"
"gitlab.com/gitlab-org/gitlab-workhorse/internal/headers"
)
type contentDisposition struct {
rw http.ResponseWriter
buf *bytes.Buffer
wroteHeader bool
flushed bool
active bool
removedResponseHeaders bool
status int
sentStatus bool
}
// SetContentHeaders buffers the response if Gitlab-Workhorse-Detect-Content-Type
// header is found and set the proper content headers based on the current
// value of content type and disposition
func SetContentHeaders(h http.Handler) http.Handler {
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
cd := &contentDisposition{
rw: w,
buf: &bytes.Buffer{},
status: http.StatusOK,
}
defer cd.flush()
h.ServeHTTP(cd, r)
})
}
func (cd *contentDisposition) Header() http.Header {
return cd.rw.Header()
}
func (cd *contentDisposition) Write(data []byte) (int, error) {
// Normal write if we don't need to buffer
if cd.isUnbuffered() {
cd.WriteHeader(cd.status)
return cd.rw.Write(data)
}
// Write the new data into the buffer
n, _ := cd.buf.Write(data)
// If we have enough data to calculate the content headers then flush the Buffer
var err error
if cd.buf.Len() >= headers.MaxDetectSize {
err = cd.flushBuffer()
}
return n, err
}
func (cd *contentDisposition) flushBuffer() error {
if cd.isUnbuffered() {
return nil
}
cd.flushed = true
// If the buffer has any content then we calculate the content headers and
// write in the response
if cd.buf.Len() > 0 {
cd.writeContentHeaders()
cd.WriteHeader(cd.status)
_, err := io.Copy(cd.rw, cd.buf)
return err
}
// If no content is present in the buffer we still need to send the headers
cd.WriteHeader(cd.status)
return nil
}
func (cd *contentDisposition) writeContentHeaders() {
if cd.wroteHeader {
return
}
cd.wroteHeader = true
contentType, contentDisposition := headers.SafeContentHeaders(cd.buf.Bytes(), cd.Header().Get(headers.ContentDispositionHeader))
cd.Header().Set(headers.ContentTypeHeader, contentType)
cd.Header().Set(headers.ContentDispositionHeader, contentDisposition)
}
func (cd *contentDisposition) WriteHeader(status int) {
if cd.sentStatus {
return
}
cd.status = status
if cd.isUnbuffered() {
cd.rw.WriteHeader(cd.status)
cd.sentStatus = true
}
}
// If we find any response header, then we must calculate the content headers
// If we don't find any, the data is not buffered and it works as
// a usual ResponseWriter
func (cd *contentDisposition) isUnbuffered() bool {
if !cd.removedResponseHeaders {
if headers.IsDetectContentTypeHeaderPresent(cd.rw) {
cd.active = true
}
cd.removedResponseHeaders = true
// We ensure to clear any response header from the response
headers.RemoveResponseHeaders(cd.rw)
}
return cd.flushed || !cd.active
}
func (cd *contentDisposition) flush() {
cd.flushBuffer()
}
|