Browse Source

Small fixes

Viktor Sokolov 1 tháng trước cách đây
mục cha
commit
026172720f
3 tập tin đã thay đổi với 22 bổ sung21 xóa
  1. 3 3
      handlers/stream/config.go
  2. 17 15
      handlers/stream/handler.go
  3. 2 3
      headerwriter/writer.go

+ 3 - 3
handlers/stream/config.go

@@ -13,8 +13,8 @@ type Config struct {
 	// PassthroughRequestHeaders specifies the request headers to include in the passthrough response
 	PassthroughRequestHeaders []string
 
-	// KeepResponseHeaders specifies the response headers to copy from the response
-	KeepResponseHeaders []string
+	// PassthroughResponseHeaders specifies the response headers to copy from the response
+	PassthroughResponseHeaders []string
 }
 
 // NewConfigFromEnv creates a new Config instance from environment variables
@@ -27,7 +27,7 @@ func NewConfigFromEnv() *Config {
 			httpheaders.AcceptEncoding,
 			httpheaders.Range,
 		},
-		KeepResponseHeaders: []string{
+		PassthroughResponseHeaders: []string{
 			httpheaders.ContentType,
 			httpheaders.ContentEncoding,
 			httpheaders.ContentRange,

+ 17 - 15
handlers/stream/handler.go

@@ -111,7 +111,7 @@ func (s *request) execute(ctx context.Context) error {
 
 	// Output streaming response headers
 	hw := headerwriter.New(s.handler.hwConfig, res.Header, s.imageURL)
-	hw.Passthrough(s.handler.config.KeepResponseHeaders) // NOTE: priority? This is lowest as it was
+	hw.Passthrough(s.handler.config.PassthroughResponseHeaders) // NOTE: priority? This is lowest as it was
 	hw.SetContentLength(int(res.ContentLength))
 	hw.SetCanonical()
 	hw.SetMaxAge(s.po.Expires, 0)
@@ -157,21 +157,23 @@ func (s *request) getPassthroughRequestHeaders() http.Header {
 // writeContentDisposition writes the headers to the response writer
 func (s *request) writeContentDisposition(imagePath string, serverResponse *http.Response) {
 	// Try to set correct Content-Disposition file name and extension
-	if serverResponse.StatusCode >= 200 && serverResponse.StatusCode < 300 {
-		ct := serverResponse.Header.Get(httpheaders.ContentType)
-
-		// Try to best guess the file name and extension
-		cd := httpheaders.ContentDispositionValue(
-			imagePath,
-			s.po.Filename,
-			"",
-			ct,
-			s.po.ReturnAttachment,
-		)
-
-		// Write the Content-Disposition header
-		s.rw.Header().Set(httpheaders.ContentDisposition, cd)
+	if serverResponse.StatusCode < 200 || serverResponse.StatusCode >= 300 {
+		return
 	}
+
+	ct := serverResponse.Header.Get(httpheaders.ContentType)
+
+	// Try to best guess the file name and extension
+	cd := httpheaders.ContentDispositionValue(
+		imagePath,
+		s.po.Filename,
+		"",
+		ct,
+		s.po.ReturnAttachment,
+	)
+
+	// Write the Content-Disposition header
+	s.rw.Header().Set(httpheaders.ContentDisposition, cd)
 }
 
 // streamData copies the image data from the response body to the response writer

+ 2 - 3
headerwriter/writer.go

@@ -199,14 +199,13 @@ func (w *Writer) setCSP() {
 // Write writes the headers to the response writer. It does not overwrite
 // target headers, which were set outside the header writer.
 func (w *Writer) Write(rw http.ResponseWriter) {
-	// By default, we set Cache-Control to No-Cache
-	w.setCacheControlNoCache()
-
 	// Then, let's try to set Cache-Control using priority order
 	switch {
 	case w.setCacheControl(w.maxAge): // First, try set explicit
 	case w.setCacheControlPassthrough(): // Try to pick up from request headers
 	case w.setCacheControl(w.config.DefaultTTL): // Fallback to default value
+	default:
+		w.setCacheControlNoCache() // By default we use no-cache
 	}
 
 	w.setCSP()