Fix cache attribute parsing

This commit is contained in:
Trevor Slocum 2020-11-09 21:12:22 -08:00
parent 8d02c6d779
commit 05da75b22b
3 changed files with 26 additions and 15 deletions

View file

@ -15,16 +15,6 @@ These additions are backwards-compatible as specified in [RFC 2045](https://tool
The terms MAY, SHOULD and SHOULD NOT are defined in [RFC 2119](https://tools.ietf.org/html/rfc2119). The terms MAY, SHOULD and SHOULD NOT are defined in [RFC 2119](https://tools.ietf.org/html/rfc2119).
# Size
Gemini servers SHOULD include the size (in bytes) of the response body when the
request is successful. Clients SHOULD utilize this information when downloading
files to indicate progress.
```
20 text/gemini; charset=utf-8; size=1108
```
# Cache # Cache
Gemini servers MAY include a duration (in seconds) which a client SHOULD cache Gemini servers MAY include a duration (in seconds) which a client SHOULD cache
@ -42,3 +32,13 @@ Do not cache:
``` ```
20 text/gemini; charset=utf-8; cache=0 20 text/gemini; charset=utf-8; cache=0
``` ```
# Size
Gemini servers SHOULD include the size (in bytes) of the response body when the
request is successful. Clients SHOULD utilize this information when downloading
files to indicate progress.
```
20 text/gemini; charset=utf-8; size=1108
```

View file

@ -38,13 +38,14 @@ type pathConfig struct {
Type string Type string
// Cache duration // Cache duration
Cache int64 `default:"-1965"` Cache string
// FastCGI server address // FastCGI server address
FastCGI string FastCGI string
r *regexp.Regexp r *regexp.Regexp
cmd []string cmd []string
cache int64
} }
type hostConfig struct { type hostConfig struct {
@ -69,6 +70,8 @@ type serverConfig struct {
fcgiPools map[string]gofast.ConnFactory fcgiPools map[string]gofast.ConnFactory
} }
const cacheUnset = -1965
var config *serverConfig var config *serverConfig
func readconfig(configPath string) error { func readconfig(configPath string) error {
@ -139,6 +142,14 @@ func readconfig(configPath string) error {
serve.r = regexp.MustCompile(serve.Path) serve.r = regexp.MustCompile(serve.Path)
} }
serve.cache = cacheUnset
if serve.Cache != "" {
serve.cache, err = strconv.ParseInt(serve.Cache, 10, 64)
if err != nil {
log.Fatalf("failed to parse cache duration for path %s: %s", serve.Path, err)
}
}
if serve.FastCGI != "" { if serve.FastCGI != "" {
if serve.Root == "" { if serve.Root == "" {
log.Fatalf("root must be specified to use fastcgi resource %s of path %s%s", serve.FastCGI, hostname, serve.Path) log.Fatalf("root must be specified to use fastcgi resource %s of path %s%s", serve.FastCGI, hostname, serve.Path)

View file

@ -86,8 +86,8 @@ func writeSuccess(c net.Conn, serve *pathConfig, contentType string, size int64)
meta += fmt.Sprintf("; size=%d", size) meta += fmt.Sprintf("; size=%d", size)
} }
if serve.Cache != -1965 { if serve.cache != cacheUnset {
meta += fmt.Sprintf("; cache=%d", serve.Cache) meta += fmt.Sprintf("; cache=%d", serve.cache)
} }
writeHeader(c, statusSuccess, meta) writeHeader(c, statusSuccess, meta)