hmn/src/website/requesthandling.go

426 lines
9.8 KiB
Go
Raw Normal View History

2021-03-18 02:14:06 +00:00
package website
import (
"bytes"
"context"
2021-04-29 03:07:14 +00:00
"fmt"
2021-03-26 03:33:00 +00:00
"html"
2021-08-17 05:18:04 +00:00
"html/template"
2021-03-18 02:14:06 +00:00
"io"
2021-08-08 20:05:52 +00:00
"net"
2021-03-18 02:14:06 +00:00
"net/http"
"net/url"
2021-03-26 03:33:00 +00:00
"path"
2021-04-29 03:07:14 +00:00
"regexp"
2021-03-26 03:33:00 +00:00
"strings"
2021-03-18 02:14:06 +00:00
2021-08-30 00:15:48 +00:00
"git.handmade.network/hmn/hmn/src/hmnurl"
2021-03-18 02:14:06 +00:00
"git.handmade.network/hmn/hmn/src/logging"
"git.handmade.network/hmn/hmn/src/models"
"git.handmade.network/hmn/hmn/src/oops"
2021-04-26 06:56:49 +00:00
"git.handmade.network/hmn/hmn/src/perf"
2021-03-18 02:14:06 +00:00
"git.handmade.network/hmn/hmn/src/templates"
"github.com/jackc/pgx/v4/pgxpool"
2021-03-18 02:14:06 +00:00
"github.com/rs/zerolog"
)
2021-04-29 03:07:14 +00:00
type Router struct {
Routes []Route
}
type Route struct {
Method string
Regexes []*regexp.Regexp
2021-04-29 03:07:14 +00:00
Handler Handler
}
2021-11-10 17:34:48 +00:00
func (r *Route) String() string {
var routeStrings []string
for _, regex := range r.Regexes {
routeStrings = append(routeStrings, regex.String())
}
return fmt.Sprintf("%s %v", r.Method, routeStrings)
}
2021-04-29 03:07:14 +00:00
type RouteBuilder struct {
Router *Router
Prefixes []*regexp.Regexp
2021-04-29 03:07:14 +00:00
Middleware Middleware
}
type Handler func(c *RequestContext) ResponseData
type Middleware func(h Handler) Handler
func (rb *RouteBuilder) Handle(methods []string, regex *regexp.Regexp, h Handler) {
// Ensure that this regex matches the start of the string
regexStr := regex.String()
if len(regexStr) == 0 || regexStr[0] != '^' {
panic("All routing regexes must begin with '^'")
}
2021-04-29 03:07:14 +00:00
h = rb.Middleware(h)
for _, method := range methods {
rb.Router.Routes = append(rb.Router.Routes, Route{
Method: method,
Regexes: append(rb.Prefixes, regex),
Handler: h,
})
}
2021-04-29 03:07:14 +00:00
}
2021-05-05 20:34:32 +00:00
func (rb *RouteBuilder) AnyMethod(regex *regexp.Regexp, h Handler) {
rb.Handle([]string{""}, regex, h)
2021-04-29 03:07:14 +00:00
}
2021-05-05 20:34:32 +00:00
func (rb *RouteBuilder) GET(regex *regexp.Regexp, h Handler) {
rb.Handle([]string{http.MethodGet}, regex, h)
2021-04-29 03:07:14 +00:00
}
2021-05-05 20:34:32 +00:00
func (rb *RouteBuilder) POST(regex *regexp.Regexp, h Handler) {
rb.Handle([]string{http.MethodPost}, regex, h)
2021-04-29 03:07:14 +00:00
}
func (rb *RouteBuilder) Group(regex *regexp.Regexp, addRoutes func(rb *RouteBuilder)) {
newRb := *rb
newRb.Prefixes = append(newRb.Prefixes, regex)
addRoutes(&newRb)
}
2021-04-29 03:07:14 +00:00
func (r *Router) ServeHTTP(rw http.ResponseWriter, req *http.Request) {
nextroute:
2021-04-29 03:07:14 +00:00
for _, route := range r.Routes {
if route.Method != "" && req.Method != route.Method {
continue
}
currentPath := strings.TrimSuffix(req.URL.Path, "/")
if currentPath == "" {
currentPath = "/"
}
var params map[string]string
for _, regex := range route.Regexes {
match := regex.FindStringSubmatch(currentPath)
if len(match) == 0 {
continue nextroute
}
2021-04-29 03:07:14 +00:00
if params == nil {
params = map[string]string{}
}
subexpNames := regex.SubexpNames()
2021-04-29 03:07:14 +00:00
for i, paramValue := range match {
paramName := subexpNames[i]
if paramName == "" {
continue
}
2021-11-10 17:34:48 +00:00
if _, alreadyExists := params[paramName]; alreadyExists {
logging.Warn().
Str("route", route.String()).
Str("paramName", paramName).
Msg("duplicate names for path parameters; last one wins")
}
2021-04-29 03:07:14 +00:00
params[paramName] = paramValue
}
// Make sure that we never consume trailing slashes even if the route regex matches them
toConsume := strings.TrimSuffix(match[0], "/")
currentPath = currentPath[len(toConsume):]
if currentPath == "" {
currentPath = "/"
}
}
c := &RequestContext{
2021-11-10 17:34:48 +00:00
Route: route.String(),
Logger: logging.GlobalLogger(),
Req: req,
Res: rw,
PathParams: params,
2021-04-29 03:07:14 +00:00
}
c.PathParams = params
2021-04-29 03:07:14 +00:00
doRequest(rw, c, route.Handler)
return
}
panic(fmt.Sprintf("Path '%s' did not match any routes! Make sure to register a wildcard route to act as a 404.", req.URL))
2021-03-31 04:20:50 +00:00
}
2021-03-18 02:14:06 +00:00
type RequestContext struct {
2021-04-29 03:07:14 +00:00
Route string
Logger *zerolog.Logger
Req *http.Request
2021-04-29 03:07:14 +00:00
PathParams map[string]string
2021-07-23 03:09:46 +00:00
// NOTE(asaf): This is the http package's internal response object. Not just a ResponseWriter.
// We sometimes need the original response object so that some functions of the http package can set connection-management flags on it.
Res http.ResponseWriter
Conn *pgxpool.Pool
CurrentProject *models.Project
CurrentUser *models.User
CurrentSession *models.Session
Theme string
UrlContext *hmnurl.UrlContext
2021-03-18 02:14:06 +00:00
2021-04-29 03:07:14 +00:00
Perf *perf.RequestPerf
ctx context.Context
2021-03-18 02:14:06 +00:00
}
func (c *RequestContext) Context() context.Context {
if c.ctx == nil {
c.ctx = c.Req.Context()
}
return c.ctx
2021-03-18 02:14:06 +00:00
}
func (c *RequestContext) URL() *url.URL {
return c.Req.URL
2021-03-18 02:14:06 +00:00
}
2021-05-04 13:23:02 +00:00
func (c *RequestContext) FullUrl() string {
2021-08-28 13:31:19 +00:00
var scheme string
if scheme == "" {
proto, hasProto := c.Req.Header["X-Forwarded-Proto"]
if hasProto {
scheme = fmt.Sprintf("%s://", proto[0])
2021-08-28 13:31:19 +00:00
}
}
if scheme == "" {
if c.Req.TLS != nil {
scheme = "https://"
} else {
scheme = "http://"
}
2021-05-04 13:23:02 +00:00
}
2021-05-04 13:23:02 +00:00
return scheme + c.Req.Host + c.Req.URL.String()
}
2021-08-28 12:52:40 +00:00
// NOTE(asaf): Assumes port is present (it should be for RemoteAddr according to the docs)
var ipRegex = regexp.MustCompile(`^(\[(?P<addrv6>[^\]]+)\]:\d+)|((?P<addrv4>[^:]+):\d+)$`)
2021-08-08 20:05:52 +00:00
func (c *RequestContext) GetIP() *net.IPNet {
ipString := ""
if ipString == "" {
cf, hasCf := c.Req.Header["CF-Connecting-IP"]
if hasCf {
ipString = cf[0]
}
}
if ipString == "" {
forwarded, hasForwarded := c.Req.Header["X-Forwarded-For"]
if hasForwarded {
ipString = forwarded[0]
}
}
if ipString == "" {
ipString = c.Req.RemoteAddr
2021-08-28 12:52:40 +00:00
if ipString != "" {
matches := ipRegex.FindStringSubmatch(ipString)
if matches != nil {
v4 := matches[ipRegex.SubexpIndex("addrv4")]
v6 := matches[ipRegex.SubexpIndex("addrv6")]
if v4 != "" {
ipString = v4
} else {
ipString = v6
}
}
}
2021-08-08 20:05:52 +00:00
}
if ipString != "" {
_, res, err := net.ParseCIDR(fmt.Sprintf("%s/32", ipString))
if err == nil {
return res
}
}
return nil
}
2021-03-26 03:33:00 +00:00
func (c *RequestContext) GetFormValues() (url.Values, error) {
err := c.Req.ParseForm()
if err != nil {
return nil, err
}
return c.Req.PostForm, nil
}
// The logic of this function is copy-pasted from the Go standard library.
// https://golang.org/pkg/net/http/#Redirect
func (c *RequestContext) Redirect(dest string, code int) ResponseData {
var res ResponseData
2021-03-26 03:33:00 +00:00
if u, err := url.Parse(dest); err == nil {
// If url was relative, make its path absolute by
// combining with request path.
// The client would probably do this for us,
// but doing it ourselves is more reliable.
// See RFC 7231, section 7.1.2
if u.Scheme == "" && u.Host == "" {
oldpath := c.Req.URL.Path
if oldpath == "" { // should not happen, but avoid a crash if it does
oldpath = "/"
}
// no leading http://server
if dest == "" || dest[0] != '/' {
// make relative path absolute
olddir, _ := path.Split(oldpath)
dest = olddir + dest
}
var query string
if i := strings.Index(dest, "?"); i != -1 {
dest, query = dest[:i], dest[i:]
}
// clean up but preserve trailing slash
trailing := strings.HasSuffix(dest, "/")
dest = path.Clean(dest)
if trailing && !strings.HasSuffix(dest, "/") {
dest += "/"
}
dest += query
}
}
// Escape stuff
2021-08-30 00:15:48 +00:00
destUrl, err := url.Parse(dest)
if err != nil {
c.Logger.Warn().Err(err).Str("dest", dest).Msg("Failed to parse redirect URI")
return c.Redirect(hmnurl.BuildHomepage(), http.StatusSeeOther)
}
2021-03-26 03:33:00 +00:00
dest = destUrl.String()
2021-04-29 03:07:14 +00:00
res.Header().Set("Location", dest)
if c.Req.Method == "GET" || c.Req.Method == "HEAD" {
2021-04-29 03:07:14 +00:00
res.Header().Set("Content-Type", "text/html; charset=utf-8")
2021-03-26 03:33:00 +00:00
}
res.StatusCode = code
2021-03-26 03:33:00 +00:00
// Shouldn't send the body for POST or HEAD; that leaves GET.
if c.Req.Method == "GET" {
res.Write([]byte("<a href=\"" + html.EscapeString(dest) + "\">" + http.StatusText(code) + "</a>.\n"))
2021-03-26 03:33:00 +00:00
}
return res
2021-03-18 02:14:06 +00:00
}
2021-08-28 12:21:03 +00:00
func (c *RequestContext) ErrorResponse(status int, errs ...error) ResponseData {
defer func() {
if r := recover(); r != nil {
LogContextErrors(c, errs...)
panic(r)
}
}()
2021-08-28 12:21:03 +00:00
res := ResponseData{
StatusCode: status,
Errors: errs,
}
2021-09-01 18:25:09 +00:00
res.MustWriteTemplate("error.html", getBaseData(c, "", nil), c.Perf)
2021-08-28 12:21:03 +00:00
return res
}
type ResponseData struct {
2021-08-17 05:18:04 +00:00
StatusCode int
Body *bytes.Buffer
Errors []error
FutureNotices []templates.Notice
header http.Header
}
2021-04-29 03:07:14 +00:00
var _ http.ResponseWriter = &ResponseData{}
func (rd *ResponseData) Header() http.Header {
if rd.header == nil {
rd.header = make(http.Header)
}
return rd.header
}
func (rd *ResponseData) Write(p []byte) (n int, err error) {
if rd.Body == nil {
rd.Body = new(bytes.Buffer)
}
return rd.Body.Write(p)
}
2021-04-29 03:07:14 +00:00
func (rd *ResponseData) WriteHeader(status int) {
rd.StatusCode = status
}
func (rd *ResponseData) SetCookie(cookie *http.Cookie) {
2021-04-29 03:07:14 +00:00
rd.Header().Add("Set-Cookie", cookie.String())
}
2021-08-17 05:18:04 +00:00
func (rd *ResponseData) AddFutureNotice(class string, content string) {
rd.FutureNotices = append(rd.FutureNotices, templates.Notice{Class: class, Content: template.HTML(content)})
}
2021-04-26 06:56:49 +00:00
func (rd *ResponseData) WriteTemplate(name string, data interface{}, rp *perf.RequestPerf) error {
if rp != nil {
rp.StartBlock("TEMPLATE", name)
defer rp.EndBlock()
}
template, hasTemplate := templates.Templates[name]
if !hasTemplate {
panic(oops.New(nil, "Template not found: %s", name))
}
return template.Execute(rd, data)
}
func (rd *ResponseData) MustWriteTemplate(name string, data interface{}, rp *perf.RequestPerf) {
err := rd.WriteTemplate(name, data, rp)
if err != nil {
panic(err)
}
}
2021-04-29 03:07:14 +00:00
func doRequest(rw http.ResponseWriter, c *RequestContext, h Handler) {
defer func() {
/*
This panic recovery is the last resort. If you want to render
an error page or something, make it a request wrapper.
*/
if recovered := recover(); recovered != nil {
rw.WriteHeader(http.StatusInternalServerError)
logging.LogPanicValue(c.Logger, recovered, "request panicked and was not handled")
2021-08-28 12:21:03 +00:00
rw.Write([]byte("There was a problem handling your request.\nPlease notify an admin at team@handmade.network"))
}
}()
res := h(c)
if res.StatusCode == 0 {
res.StatusCode = http.StatusOK
}
2021-03-18 02:14:06 +00:00
2021-04-29 03:07:14 +00:00
for name, vals := range res.Header() {
for _, val := range vals {
rw.Header().Add(name, val)
}
2021-03-18 02:14:06 +00:00
}
rw.WriteHeader(res.StatusCode)
2021-04-22 23:02:50 +00:00
if res.Body != nil {
io.Copy(rw, res.Body)
}
2021-03-18 02:14:06 +00:00
}