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

change redirect code to 302 Found, tidy code

This might be a breaking change for clients expecting a permanent (301
Moved Permanently) status code on redirect objects.
parent 150f6906
No related branches found
No related tags found
No related merge requests found
package main package main
import ( import (
"bytes"
"database/sql" "database/sql"
"fmt" "fmt"
"html/template" "html/template"
"strings" "strings"
"time" "time"
"unsafe"
"github.com/whats-this/cdn-origin/prometheus" "github.com/whats-this/cdn-origin/prometheus"
"github.com/whats-this/cdn-origin/weed" "github.com/whats-this/cdn-origin/weed"
...@@ -27,6 +25,7 @@ const version = "0.2.0" ...@@ -27,6 +25,7 @@ const version = "0.2.0"
const ( const (
accept = "accept" accept = "accept"
host = "host" host = "host"
location = "Location"
) )
// Host domain match strings. // Host domain match strings.
...@@ -409,41 +408,29 @@ func requestHandler(ctx *fasthttp.RequestCtx) { ...@@ -409,41 +408,29 @@ func requestHandler(ctx *fasthttp.RequestCtx) {
fmt.Fprint(ctx, "500 Internal Server Error") fmt.Fprint(ctx, "500 Internal Server Error")
} }
if ctx.QueryArgs().Has("preview") { previewMode := ctx.QueryArgs().Has("preview")
buf := new(bytes.Buffer) var err error
err := redirectPreviewHTMLTemplate.Execute(buf, dest_url.String) if previewMode {
if err != nil { err = redirectPreviewHTMLTemplate.Execute(ctx, dest_url.String)
log.WithFields(log.Fields{
"err": err,
"dest_url": dest_url.String,
}).Warn("failed to generate redirect preview HTML to send to client")
ctx.SetContentType("text/plain; charset=utf8")
fmt.Fprintf(ctx, "Failed to generate preview page, destination URL: %s", dest_url.String)
return
}
b := buf.Bytes()
s := *(*string)(unsafe.Pointer(&b))
ctx.SetContentType("text/html; charset=utf8")
fmt.Fprint(ctx, s)
} else { } else {
buf := new(bytes.Buffer) err = redirectHTMLTemplate.Execute(ctx, dest_url.String)
err := redirectHTMLTemplate.Execute(buf, dest_url.String) }
if err != nil {
log.WithFields(log.Fields{ if err != nil {
"err": err, log.WithFields(log.Fields{
"dest_url": dest_url.String, "err": err,
}).Warn("failed to generate redirect HTML to send to client") "dest_url": dest_url.String,
ctx.SetContentType("text/plain; charset=utf8") "preview": ctx.QueryArgs().Has("preview"),
fmt.Fprintf(ctx, "Failed to generate HTML fallback page, destination URL: %s", dest_url.String) }).Warn("failed to generate HTML redirect page to send to client")
return ctx.SetContentType("text/plain; charset=utf8")
} fmt.Fprintf(ctx, "Failed to generate HTML redirect page, destination URL: %s", dest_url.String)
b := buf.Bytes() return
s := *(*string)(unsafe.Pointer(&b)) }
ctx.SetStatusCode(fasthttp.StatusMovedPermanently)
ctx.SetContentType("text/html; charset=utf8")
ctx.Response.Header.Set("Location", dest_url.String)
fmt.Fprint(ctx, s)
ctx.SetContentType("text/html; charset=ut8")
if !previewMode {
ctx.SetStatusCode(fasthttp.StatusFound)
ctx.Response.Header.Set(location, dest_url.String)
} }
} }
} }
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