Skip to content
Snippets Groups Projects
Commit d426aae7 authored by Dean's avatar Dean
Browse files

return a Content-Length with file requests

For some reason I forgot to do this or assumed it was done
automatically.
parent b47c9d3c
No related branches found
No related tags found
No related merge requests found
...@@ -21,6 +21,9 @@ import ( ...@@ -21,6 +21,9 @@ import (
// version is the current version of cdn-origin. // version is the current version of cdn-origin.
const version = "0.2.0" const version = "0.2.0"
// Default SeaweedFS fetch parameters.
const defaultSeaweedFSQueryParameters = ""
// HTTP header strings. // HTTP header strings.
const ( const (
accept = "accept" accept = "accept"
...@@ -382,7 +385,7 @@ func requestHandler(ctx *fasthttp.RequestCtx) { ...@@ -382,7 +385,7 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
} }
// TODO: ?thumbnail query parameter for images // TODO: ?thumbnail query parameter for images
statusCode, err := seaweed.Get(ctx, backend_file_id.String, "") statusCode, contentSize, err := seaweed.Get(ctx, backend_file_id.String, defaultSeaweedFSQueryParameters)
if err != nil { if err != nil {
log.WithField("err", err).Warn("failed to retrieve file from SeaweedFS volume server") log.WithField("err", err).Warn("failed to retrieve file from SeaweedFS volume server")
ctx.SetStatusCode(fasthttp.StatusInternalServerError) ctx.SetStatusCode(fasthttp.StatusInternalServerError)
...@@ -398,7 +401,9 @@ func requestHandler(ctx *fasthttp.RequestCtx) { ...@@ -398,7 +401,9 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
ctx.SetStatusCode(fasthttp.StatusInternalServerError) ctx.SetStatusCode(fasthttp.StatusInternalServerError)
ctx.SetContentType("text/plain; charset=utf8") ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprint(ctx, "500 Internal Server Error") fmt.Fprint(ctx, "500 Internal Server Error")
return
} }
ctx.Response.Header.SetContentLength(contentSize)
case 1: // redirect case 1: // redirect
if !dest_url.Valid { if !dest_url.Valid {
......
...@@ -55,10 +55,10 @@ func New(masterURI string, lookupTimeout time.Duration) *Seaweed { ...@@ -55,10 +55,10 @@ func New(masterURI string, lookupTimeout time.Duration) *Seaweed {
} }
} }
func (s *Seaweed) Get(writer io.Writer, fid string, query string) (int, error) { func (s *Seaweed) Get(writer io.Writer, fid string, query string) (int, int, error) {
volumeURL := s.lookupVolume(strings.Split(fid, ",")[0]) volumeURL := s.lookupVolume(strings.Split(fid, ",")[0])
if volumeURL == "" { if volumeURL == "" {
return 500, errors.New("failed to retrieve volume URL") return fasthttp.StatusInternalServerError, 0, errors.New("failed to retrieve volume URL")
} }
requestURL := volumeURL requestURL := volumeURL
if !strings.HasPrefix(requestURL, "http://") && !strings.HasPrefix(requestURL, "https://") { if !strings.HasPrefix(requestURL, "http://") && !strings.HasPrefix(requestURL, "https://") {
...@@ -85,15 +85,15 @@ func (s *Seaweed) Get(writer io.Writer, fid string, query string) (int, error) { ...@@ -85,15 +85,15 @@ func (s *Seaweed) Get(writer io.Writer, fid string, query string) (int, error) {
// Perform request // Perform request
err := fasthttp.Do(req, res) err := fasthttp.Do(req, res)
if err != nil { if err != nil {
return 0, err return 0, 0, err
} }
if res.StatusCode() == fasthttp.StatusOK { if res.StatusCode() == fasthttp.StatusOK {
if err := res.BodyWriteTo(writer); err != nil { if err := res.BodyWriteTo(writer); err != nil {
log.WithField("err", err).Warn("failed to set body writer for response") log.WithField("err", err).Warn("failed to set body writer for response")
return fasthttp.StatusInternalServerError, err return fasthttp.StatusInternalServerError, 0, err
} }
} }
return fasthttp.StatusOK, err return fasthttp.StatusOK, res.Header.ContentLength(), err
} }
func (s *Seaweed) Ping() error { func (s *Seaweed) Ping() error {
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment