Server redirects non-upgrade http requests

This commit is contained in:
Aadi Desai 2024-01-15 00:32:41 +00:00
parent d703615f7f
commit f4afdce578
Signed by: supleed2
SSH key fingerprint: SHA256:CkbNRs0yVzXEiUp2zd0PSxsfRUMFF9bLlKXtE1xEbKM
2 changed files with 26 additions and 1 deletions

View file

@ -19,6 +19,12 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Security
## [0.1.2] - 2024-01-14
### Changed
- Server redirects non-upgrade http requests
## [0.1.1] - 2024-01-14
### Changed
@ -39,6 +45,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
## Links
[unreleased]: https://github.com/supleed2/omg-rs/compare/v0.1.1...HEAD
[unreleased]: https://github.com/supleed2/omg-rs/compare/v0.1.2...HEAD
[0.1.2]: https://github.com/supleed2/omg-rs/releases/tag/v0.1.2
[0.1.1]: https://github.com/supleed2/omg-rs/releases/tag/v0.1.1
[0.1.0]: https://github.com/supleed2/omg-rs/releases/tag/v0.1.0

View file

@ -106,6 +106,11 @@ func run(addr string, nickMap map[string]string, admin string, rhlen int, log *l
}
func (s server) ServeHTTP(w http.ResponseWriter, r *http.Request) {
if r.ProtoAtLeast(1, 1) && !hasUpgradeHeader(r.Header) {
http.Redirect(w, r, "https://github.com/supleed2/go-chat", http.StatusSeeOther)
return
}
ctx := r.Context()
conn, err := ws.Accept(w, r, nil)
if err != nil {
@ -357,3 +362,16 @@ func alphanumeric(s string) bool {
}
return true
}
func hasUpgradeHeader(h http.Header) bool {
for _, v := range h["Connection"] {
v = strings.TrimSpace(v)
for _, t := range strings.Split(v, ",") {
t = strings.TrimSpace(t)
if strings.EqualFold(t, "Upgrade") {
return true
}
}
}
return false
}