mirror of
https://github.com/superseriousbusiness/gotosocial.git
synced 2024-11-30 07:32:45 +00:00
fc3741365c
* Add Swagger spec test script * Fix Swagger spec errors not related to statuses with polls * Add API tests that post a status with a poll * Fix creating a status with a poll from form params * Fix Swagger spec errors related to statuses with polls (this is the last error) * Fix Swagger spec warnings not related to unused definitions * Suppress a duplicate list update params definition that was somehow causing wrong param names * Add Swagger test to CI - updates Drone config - vendorizes go-swagger - fixes a file extension issue that caused the test script to generate JSON instead of YAML with the vendorized version * Put `Sample: ` on its own line everywhere * Remove unused id param from emojiCategoriesGet * Add 5 more pairs of profile fields to account update API Swagger * Remove Swagger prefix from dummy fields It makes the generated code look weird * Manually annotate params for statusCreate operation * Fix all remaining Swagger spec warnings - Change some models into operation parameters - Ignore models that already correspond to manually documented operation parameters but can't be trivially changed (those with file fields) * Documented that creating a status with scheduled_at isn't implemented yet * sign drone.yml * Fix filter API Swagger errors * fixup! Fix filter API Swagger errors --------- Co-authored-by: tobi <tobi.smethurst@protonmail.com>
75 lines
1.9 KiB
Go
75 lines
1.9 KiB
Go
package handlers
|
|
|
|
import (
|
|
"net/http"
|
|
"net/url"
|
|
"strings"
|
|
)
|
|
|
|
type canonical struct {
|
|
h http.Handler
|
|
domain string
|
|
code int
|
|
}
|
|
|
|
// CanonicalHost is HTTP middleware that re-directs requests to the canonical
|
|
// domain. It accepts a domain and a status code (e.g. 301 or 302) and
|
|
// re-directs clients to this domain. The existing request path is maintained.
|
|
//
|
|
// Note: If the provided domain is considered invalid by url.Parse or otherwise
|
|
// returns an empty scheme or host, clients are not re-directed.
|
|
//
|
|
// Example:
|
|
//
|
|
// r := mux.NewRouter()
|
|
// canonical := handlers.CanonicalHost("http://www.gorillatoolkit.org", 302)
|
|
// r.HandleFunc("/route", YourHandler)
|
|
//
|
|
// log.Fatal(http.ListenAndServe(":7000", canonical(r)))
|
|
//
|
|
func CanonicalHost(domain string, code int) func(h http.Handler) http.Handler {
|
|
fn := func(h http.Handler) http.Handler {
|
|
return canonical{h, domain, code}
|
|
}
|
|
|
|
return fn
|
|
}
|
|
|
|
func (c canonical) ServeHTTP(w http.ResponseWriter, r *http.Request) {
|
|
dest, err := url.Parse(c.domain)
|
|
if err != nil {
|
|
// Call the next handler if the provided domain fails to parse.
|
|
c.h.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
if dest.Scheme == "" || dest.Host == "" {
|
|
// Call the next handler if the scheme or host are empty.
|
|
// Note that url.Parse won't fail on in this case.
|
|
c.h.ServeHTTP(w, r)
|
|
return
|
|
}
|
|
|
|
if !strings.EqualFold(cleanHost(r.Host), dest.Host) {
|
|
// Re-build the destination URL
|
|
dest := dest.Scheme + "://" + dest.Host + r.URL.Path
|
|
if r.URL.RawQuery != "" {
|
|
dest += "?" + r.URL.RawQuery
|
|
}
|
|
http.Redirect(w, r, dest, c.code)
|
|
return
|
|
}
|
|
|
|
c.h.ServeHTTP(w, r)
|
|
}
|
|
|
|
// cleanHost cleans invalid Host headers by stripping anything after '/' or ' '.
|
|
// This is backported from Go 1.5 (in response to issue #11206) and attempts to
|
|
// mitigate malformed Host headers that do not match the format in RFC7230.
|
|
func cleanHost(in string) string {
|
|
if i := strings.IndexAny(in, " /"); i != -1 {
|
|
return in[:i]
|
|
}
|
|
return in
|
|
}
|