Browse Source

Include size of response body in media type header

master
Trevor Slocum 2 years ago
parent
commit
2b7e21666b
  1. 5
      CONFIGURATION.md
  2. 2
      config.go
  3. 15
      server_file.go

5
CONFIGURATION.md

@ -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

2
config.go

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

15
server_file.go

@ -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])

Loading…
Cancel
Save