MCPcopy
hub / github.com/nirui/sshwifty / serveStaticData

Function serveStaticData

application/controller/static.go:85–154  ·  view source on GitHub ↗
(
	dataName string,
	w http.ResponseWriter,
	r *http.Request,
	l log.Logger,
)

Source from the content-addressed store, hash-verified

83}
84
85func serveStaticData(
86 dataName string,
87 w http.ResponseWriter,
88 r *http.Request,
89 l log.Logger,
90) error {
91 if strings.ToUpper(r.Method) != "GET" {
92 return ErrControllerNotImplemented
93 }
94
95 fileExt := staticFileExt(dataName)
96
97 if fileExt == ".html" || fileExt == ".htm" {
98 return ErrNotFound
99 }
100
101 d, dFound := staticPages[dataName]
102
103 if !dFound {
104 return ErrNotFound
105 }
106
107 selectedData := d.data
108 selectedDataHash := d.dataHash
109 compressEnabled := false
110
111 if clientSupportGZIP(r) && d.hasCompressed() {
112 selectedData = d.compressd
113 selectedDataHash = d.compressdHash
114
115 compressEnabled = true
116
117 w.Header().Add("Vary", "Accept-Encoding")
118 }
119
120 canUseCache := true
121
122 if !clientContentEtagIsValid(r, selectedDataHash) {
123 canUseCache = false
124 }
125
126 if clientContentModifiedSince(r, d.created) {
127 canUseCache = false
128 }
129
130 if canUseCache {
131 w.WriteHeader(http.StatusNotModified)
132
133 return nil
134 }
135
136 w.Header().Add("Cache-Control", "public, max-age=31536000")
137 w.Header().Add("ETag", "\""+selectedDataHash+"\"")
138
139 mimeType := mime.TypeByExtension(fileExt)
140
141 if len(mimeType) > 0 {
142 w.Header().Add("Content-Type", mimeType)

Callers 1

ServeHTTPMethod · 0.85

Calls 6

staticFileExtFunction · 0.85
clientSupportGZIPFunction · 0.85
clientContentEtagIsValidFunction · 0.85
hasCompressedMethod · 0.80
WriteMethod · 0.65

Tested by

no test coverage detected