Include size of response body in media type header

This commit is contained in:
Trevor Slocum 2020-11-04 20:41:11 -08:00
parent dc58324dff
commit 2b7e21666b
3 changed files with 20 additions and 2 deletions

View File

@ -62,6 +62,11 @@ certbot certonly --config-dir /home/www/certs \
Provide the path to the certificate file at `certs/live/$DOMAIN/fullchain.pem`
and the private key file at `certs/live/$DOMAIN/privkey.pem` to twins.
### DisableSize
The size of the response body is included in the media type header by default.
Set this option to `true` to disable this feature.
### Path
#### Resources

View File

@ -57,6 +57,8 @@ type serverConfig struct {
Hosts map[string]*hostConfig
DisableSize bool
hostname string
port int
fcgiPools map[string]gofast.ConnFactory

View File

@ -141,7 +141,12 @@ func serveFile(c net.Conn, request *url.URL, filePath string, listDir bool) {
buf := make([]byte, 261)
n, _ := file.Read(buf)
// Write header
// Write response header
size := int64(-1)
info, err := file.Stat()
if err == nil {
size = info.Size()
}
var mimeType string
if strings.HasSuffix(filePath, ".html") && strings.HasSuffix(filePath, ".htm") {
mimeType = "text/html; charset=utf-8"
@ -156,7 +161,13 @@ func serveFile(c net.Conn, request *url.URL, filePath string, listDir bool) {
if mimeType == "" {
mimeType = "text/gemini; charset=utf-8"
}
writeHeader(c, statusSuccess, mimeType)
var meta string
if !config.DisableSize && size >= 0 {
meta = fmt.Sprintf("%s; size=%d", mimeType, size)
} else {
meta = mimeType
}
writeHeader(c, statusSuccess, meta)
// Write body
c.Write(buf[:n])