2021-03-20 20:40:40 +00:00
|
|
|
// SPDX-FileCopyrightText: 2021 Luke Granger-Brown <depot@lukegb.com>
|
|
|
|
//
|
|
|
|
// SPDX-License-Identifier: Apache-2.0
|
|
|
|
|
2021-03-20 19:53:43 +00:00
|
|
|
package fuphttp
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"fmt"
|
|
|
|
"io/fs"
|
2021-03-21 03:04:38 +00:00
|
|
|
"log"
|
2021-03-20 19:53:43 +00:00
|
|
|
"net/http"
|
2021-03-21 18:04:37 +00:00
|
|
|
"strings"
|
2021-03-21 03:04:38 +00:00
|
|
|
"time"
|
2021-03-20 19:53:43 +00:00
|
|
|
|
2021-03-21 03:03:15 +00:00
|
|
|
"github.com/google/safehtml"
|
2021-03-20 19:53:43 +00:00
|
|
|
"github.com/google/safehtml/template"
|
|
|
|
"github.com/google/safehtml/template/uncheckedconversions"
|
2021-03-21 03:03:15 +00:00
|
|
|
shuncheckedconversions "github.com/google/safehtml/uncheckedconversions"
|
2021-03-20 19:53:43 +00:00
|
|
|
"github.com/gorilla/mux"
|
2021-03-21 03:04:38 +00:00
|
|
|
"gocloud.dev/blob"
|
2021-03-21 17:07:47 +00:00
|
|
|
"hg.lukegb.com/lukegb/depot/web/fup/fuphttp/fngen"
|
2021-03-21 03:03:15 +00:00
|
|
|
"hg.lukegb.com/lukegb/depot/web/fup/hashfs"
|
2021-03-20 19:53:43 +00:00
|
|
|
)
|
|
|
|
|
2021-03-21 03:04:38 +00:00
|
|
|
const (
|
|
|
|
defaultRedirectExpiry = 5 * time.Minute
|
|
|
|
)
|
|
|
|
|
2021-03-21 18:52:22 +00:00
|
|
|
type Highlighter interface {
|
|
|
|
Markdown(ctx context.Context, text string) (safehtml.HTML, error)
|
|
|
|
Code(ctx context.Context, filename, theme, text string) (safehtml.HTML, error)
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:53:43 +00:00
|
|
|
type Config struct {
|
|
|
|
Templates fs.FS
|
|
|
|
Static fs.FS
|
2021-03-21 03:03:15 +00:00
|
|
|
StaticRoot safehtml.TrustedResourceURL
|
2021-03-21 03:04:38 +00:00
|
|
|
|
2021-03-21 18:04:37 +00:00
|
|
|
AppRoot string
|
|
|
|
|
2021-03-21 03:04:38 +00:00
|
|
|
// If set, redirects to a signed URL if possible instead of serving directly.
|
|
|
|
RedirectToBlobstore bool
|
2021-03-21 14:56:42 +00:00
|
|
|
|
|
|
|
// RedirectExpiry sets the maximum lifetime of a signed URL, if RedirectToBlobstore is in use and the backend supports signed URLs.
|
|
|
|
// Note that if a file has an expiry then the signed URL's validity will be capped at the expiry of the underlying file.
|
|
|
|
RedirectExpiry time.Duration // Defaults to 5 minutes.
|
2021-03-21 03:04:38 +00:00
|
|
|
|
|
|
|
// Set one of these, but not both.
|
|
|
|
StorageURL string
|
|
|
|
StorageBackend *blob.Bucket
|
2021-03-21 17:07:47 +00:00
|
|
|
|
|
|
|
// FilenameGenerator returns a new filename based on the provided prefix and extension.
|
|
|
|
FilenameGenerator fngen.FilenameGenerator
|
2021-03-21 18:04:37 +00:00
|
|
|
|
2021-03-21 18:52:22 +00:00
|
|
|
// Highlighter is used for syntax highlighting and Markdown rendering.
|
|
|
|
// If nil, then no syntax highlighting or Markdown rendering will be performed.
|
|
|
|
Highlighter Highlighter
|
|
|
|
|
2021-03-21 18:04:37 +00:00
|
|
|
// UseDirectDownload decides whether the "pretty" wrapped page or the direct download page is the most appropriate for a given set of parameters.
|
|
|
|
UseDirectDownload func(fileExtension string, mimeType string) bool
|
2021-03-23 00:45:28 +00:00
|
|
|
|
|
|
|
// AuthMiddleware is a Gorilla middleware to provide authentication information.
|
|
|
|
//
|
|
|
|
// It runs on every handler, including public ones, and will be provided with information:
|
|
|
|
// - if the page is an upload page (either the homepage, the paste textbox page, or the upload handler), the IsMutate will return true.
|
|
|
|
// - if the page is an API handler, then IsAPIRequest will return true.
|
|
|
|
AuthMiddleware mux.MiddlewareFunc
|
2021-03-20 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type Application struct {
|
2021-03-22 00:25:32 +00:00
|
|
|
indexTmpl *template.Template
|
|
|
|
pasteTmpl *template.Template
|
|
|
|
notFoundTmpl *template.Template
|
|
|
|
viewTextTmpl *template.Template
|
|
|
|
viewRenderedTmpl *template.Template
|
|
|
|
viewBinaryTmpl *template.Template
|
|
|
|
|
2021-03-21 03:04:38 +00:00
|
|
|
storageBackend *blob.Bucket
|
|
|
|
|
2021-03-21 18:04:37 +00:00
|
|
|
appRoot string
|
|
|
|
|
2021-03-21 18:52:22 +00:00
|
|
|
highlighter Highlighter
|
|
|
|
|
2021-03-21 03:04:38 +00:00
|
|
|
redirectToBlobstore bool
|
|
|
|
redirectExpiry time.Duration
|
2021-03-21 17:07:47 +00:00
|
|
|
|
|
|
|
filenameGenerator fngen.FilenameGenerator
|
2021-03-21 18:04:37 +00:00
|
|
|
useDirectDownload func(fileExtension string, mimeType string) bool
|
2021-03-23 00:45:28 +00:00
|
|
|
|
|
|
|
authMiddleware mux.MiddlewareFunc
|
2021-03-21 18:04:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func DefaultUseDirectDownload(fileExtension, mimeType string) bool {
|
2021-03-22 01:09:00 +00:00
|
|
|
switch mimeType {
|
|
|
|
case "application/json", "application/xml", "application/xhtml+xml", "application/x-csh", "application/x-sh":
|
|
|
|
return false
|
|
|
|
}
|
2021-03-21 18:04:37 +00:00
|
|
|
return !strings.HasPrefix(mimeType, "text/")
|
2021-03-20 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-23 00:45:28 +00:00
|
|
|
func isAPIRequest(r *http.Request) bool {
|
|
|
|
return r.Header.Get("Accept") == "application/json"
|
|
|
|
}
|
|
|
|
|
|
|
|
type contextKey string
|
|
|
|
|
|
|
|
const (
|
|
|
|
ctxKeyIsMutate = contextKey("isMutate")
|
|
|
|
ctxKeyIsAPIRequest = contextKey("isAPIRequest")
|
|
|
|
)
|
|
|
|
|
|
|
|
// IsAPIRequest determines whether a request was made from an API client rather than from a browser.
|
|
|
|
func IsAPIRequest(ctx context.Context) bool {
|
|
|
|
return ctx.Value(ctxKeyIsAPIRequest).(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
// IsMutate determines whether a request for the given context is for a "mutate".
|
|
|
|
// This includes things like the HTML for the home page, which in itself is not a mutate but is useless if you're not allowed to auth.
|
|
|
|
func IsMutate(ctx context.Context) bool {
|
|
|
|
return ctx.Value(ctxKeyIsMutate).(bool)
|
|
|
|
}
|
|
|
|
|
|
|
|
func contextPopulateMiddleware(isMutate bool) mux.MiddlewareFunc {
|
|
|
|
return func(next http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
|
|
|
ctx = context.WithValue(ctx, ctxKeyIsMutate, isMutate)
|
|
|
|
ctx = context.WithValue(ctx, ctxKeyIsAPIRequest, isAPIRequest(r))
|
|
|
|
next.ServeHTTP(rw, r.WithContext(ctx))
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:53:43 +00:00
|
|
|
func (a *Application) Handler() http.Handler {
|
|
|
|
r := mux.NewRouter()
|
|
|
|
|
2021-03-21 03:04:38 +00:00
|
|
|
renderTemplate := func(t *template.Template) http.HandlerFunc {
|
|
|
|
return func(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
if err := t.Execute(rw, nil); err != nil {
|
|
|
|
log.Printf("rendering template: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
r.NotFoundHandler = http.HandlerFunc(a.notFound)
|
2021-03-23 00:45:28 +00:00
|
|
|
|
|
|
|
authR := r.PathPrefix("/").Subrouter()
|
|
|
|
authR.HandleFunc("/", renderTemplate(a.indexTmpl))
|
|
|
|
authR.HandleFunc("/paste", renderTemplate(a.pasteTmpl))
|
|
|
|
authR.HandleFunc("/upload", a.upload).Methods("POST", "PUT")
|
|
|
|
authR.HandleFunc("/upload/{filename}", a.upload).Methods("PUT")
|
|
|
|
|
|
|
|
publicR := r.PathPrefix("/").Subrouter()
|
|
|
|
publicR.HandleFunc("/raw/{filename}", a.rawDownload)
|
|
|
|
publicR.HandleFunc("/{filename}", a.view)
|
|
|
|
|
|
|
|
if a.authMiddleware != nil {
|
|
|
|
authR.Use(contextPopulateMiddleware(true))
|
|
|
|
authR.Use(a.authMiddleware)
|
|
|
|
publicR.Use(contextPopulateMiddleware(false))
|
|
|
|
publicR.Use(a.authMiddleware)
|
|
|
|
}
|
2021-03-20 19:53:43 +00:00
|
|
|
|
|
|
|
return r
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:04:38 +00:00
|
|
|
func (a *Application) notFound(rw http.ResponseWriter, r *http.Request) {
|
|
|
|
rw.WriteHeader(http.StatusNotFound)
|
|
|
|
if err := a.notFoundTmpl.Execute(rw, nil); err != nil {
|
|
|
|
log.Printf("rendering 404 template: %v", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a *Application) internalError(rw http.ResponseWriter, r *http.Request) {
|
2021-03-21 17:08:29 +00:00
|
|
|
rw.Header().Set("Content-type", "text/plain; charset=utf-8")
|
|
|
|
rw.Header().Set("X-Content-Type-Options", "nosniff")
|
2021-03-21 03:04:38 +00:00
|
|
|
rw.WriteHeader(http.StatusInternalServerError)
|
|
|
|
fmt.Fprintf(rw, "hammed server :(\n")
|
|
|
|
}
|
|
|
|
|
2021-03-21 17:08:29 +00:00
|
|
|
func (a *Application) badRequest(rw http.ResponseWriter, r *http.Request, err error) {
|
|
|
|
rw.Header().Set("Content-type", "text/plain; charset=utf-8")
|
|
|
|
rw.Header().Set("X-Content-Type-Options", "nosniff")
|
|
|
|
rw.WriteHeader(http.StatusBadRequest)
|
|
|
|
fmt.Fprintf(rw, "bad request: %v\n", err.Error())
|
|
|
|
}
|
|
|
|
|
2021-03-21 18:04:37 +00:00
|
|
|
func (a *Application) appURL(s string) string {
|
|
|
|
return a.appRoot + s
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:53:43 +00:00
|
|
|
func parseTemplate(t *template.Template, fsys fs.FS, name string) (*template.Template, error) {
|
|
|
|
bs, err := fs.ReadFile(fsys, name)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("reading template %q: %w", name, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return t.ParseFromTrustedTemplate(
|
|
|
|
uncheckedconversions.TrustedTemplateFromStringKnownToSatisfyTypeContract(string(bs)),
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-03-21 03:03:15 +00:00
|
|
|
func loadTemplate(fsys fs.FS, name string, funcs template.FuncMap) (*template.Template, error) {
|
|
|
|
t := template.New(name).Funcs(funcs)
|
2021-03-20 19:53:43 +00:00
|
|
|
var err error
|
|
|
|
if t, err = parseTemplate(t, fsys, "base.html"); err != nil {
|
|
|
|
return nil, fmt.Errorf("loading base template: %w", err)
|
|
|
|
}
|
|
|
|
if t, err = parseTemplate(t, fsys, fmt.Sprintf("%s.html", name)); err != nil {
|
|
|
|
return nil, fmt.Errorf("loading leaf template: %w", err)
|
|
|
|
}
|
|
|
|
return t, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func New(ctx context.Context, cfg *Config) (*Application, error) {
|
2021-03-21 03:04:38 +00:00
|
|
|
a := &Application{
|
|
|
|
redirectToBlobstore: cfg.RedirectToBlobstore,
|
|
|
|
redirectExpiry: cfg.RedirectExpiry,
|
2021-03-21 17:07:47 +00:00
|
|
|
filenameGenerator: cfg.FilenameGenerator,
|
2021-03-21 18:04:37 +00:00
|
|
|
useDirectDownload: cfg.UseDirectDownload,
|
|
|
|
appRoot: cfg.AppRoot,
|
2021-03-22 00:25:32 +00:00
|
|
|
highlighter: cfg.Highlighter,
|
2021-03-23 00:45:28 +00:00
|
|
|
authMiddleware: cfg.AuthMiddleware,
|
2021-03-21 03:04:38 +00:00
|
|
|
}
|
|
|
|
if a.redirectExpiry == 0 {
|
|
|
|
a.redirectExpiry = defaultRedirectExpiry
|
|
|
|
}
|
2021-03-21 17:07:47 +00:00
|
|
|
if a.filenameGenerator == nil {
|
|
|
|
a.filenameGenerator = fngen.PetnameGenerator
|
|
|
|
}
|
2021-03-21 18:04:37 +00:00
|
|
|
if a.useDirectDownload == nil {
|
|
|
|
a.useDirectDownload = DefaultUseDirectDownload
|
|
|
|
}
|
2021-03-21 03:04:38 +00:00
|
|
|
|
|
|
|
bkt := cfg.StorageBackend
|
|
|
|
if bkt == nil {
|
|
|
|
var err error
|
|
|
|
if bkt, err = blob.OpenBucket(ctx, cfg.StorageURL); err != nil {
|
|
|
|
return nil, fmt.Errorf("opening bucket %q: %v", cfg.StorageURL, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
a.storageBackend = bkt
|
2021-03-20 19:53:43 +00:00
|
|
|
|
|
|
|
tmpls := []struct {
|
|
|
|
t **template.Template
|
|
|
|
name string
|
|
|
|
}{
|
|
|
|
{&a.indexTmpl, "index"},
|
2021-03-22 00:25:32 +00:00
|
|
|
{&a.pasteTmpl, "paste"},
|
2021-03-20 19:53:43 +00:00
|
|
|
{&a.notFoundTmpl, "404"},
|
2021-03-22 00:25:32 +00:00
|
|
|
{&a.viewTextTmpl, "text"},
|
|
|
|
{&a.viewRenderedTmpl, "rendered"},
|
|
|
|
{&a.viewBinaryTmpl, "binary"},
|
2021-03-20 19:53:43 +00:00
|
|
|
}
|
|
|
|
|
2021-03-21 03:03:15 +00:00
|
|
|
funcMap := template.FuncMap{
|
2021-03-21 18:04:37 +00:00
|
|
|
"app": func(s string) safehtml.URL {
|
|
|
|
return safehtml.URLSanitized(a.appRoot + s)
|
|
|
|
},
|
2021-03-21 03:03:15 +00:00
|
|
|
"static": func(s string) safehtml.TrustedResourceURL {
|
|
|
|
staticPath := s
|
|
|
|
if fs, ok := cfg.Static.(*hashfs.FS); ok {
|
|
|
|
sp, ok := fs.LookupHashedName(staticPath)
|
|
|
|
if ok {
|
|
|
|
staticPath = sp
|
|
|
|
} else {
|
|
|
|
log.Printf("warning: couldn't find static file %v", staticPath)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return shuncheckedconversions.TrustedResourceURLFromStringKnownToSatisfyTypeContract(cfg.StaticRoot.String() + staticPath)
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
2021-03-20 19:53:43 +00:00
|
|
|
for _, tmpl := range tmpls {
|
2021-03-21 03:03:15 +00:00
|
|
|
t, err := loadTemplate(cfg.Templates, tmpl.name, funcMap)
|
2021-03-20 19:53:43 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("loading template %q: %w", tmpl.name, err)
|
|
|
|
}
|
|
|
|
*tmpl.t = t
|
|
|
|
}
|
|
|
|
|
|
|
|
return a, nil
|
|
|
|
}
|