mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-18 01:57:02 +00:00
Remove httpbuf, manually save sessions
- httpbuf prevents connections being upgraded to websockets
This commit is contained in:
@@ -8,7 +8,6 @@ import (
|
|||||||
"net/http"
|
"net/http"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/goods/httpbuf"
|
|
||||||
"github.com/gorilla/mux"
|
"github.com/gorilla/mux"
|
||||||
"github.com/gorilla/securecookie"
|
"github.com/gorilla/securecookie"
|
||||||
"github.com/gorilla/sessions"
|
"github.com/gorilla/sessions"
|
||||||
@@ -122,26 +121,13 @@ func (h Handler) ServeHTTP(w http.ResponseWriter, req *http.Request) {
|
|||||||
defer ctx.Close()
|
defer ctx.Close()
|
||||||
|
|
||||||
// Run the handler, grab the error, and report it
|
// Run the handler, grab the error, and report it
|
||||||
buf := new(httpbuf.Buffer)
|
|
||||||
log.Tracef("HTTP[%v] %v %v %q", req.RemoteAddr, req.Proto, req.Method, req.RequestURI)
|
log.Tracef("HTTP[%v] %v %v %q", req.RemoteAddr, req.Proto, req.Method, req.RequestURI)
|
||||||
err = h(buf, req, ctx)
|
err = h(w, req, ctx)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
log.Errorf("HTTP error handling %q: %v", req.RequestURI, err)
|
log.Errorf("HTTP error handling %q: %v", req.RequestURI, err)
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
http.Error(w, err.Error(), http.StatusInternalServerError)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// Save the session
|
|
||||||
if err = ctx.Session.Save(req, buf); err != nil {
|
|
||||||
log.Errorf("HTTP failed to save session: %v", err)
|
|
||||||
http.Error(w, err.Error(), http.StatusInternalServerError)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
// Apply the buffered response to the writer
|
|
||||||
if _, err = buf.Apply(w); err != nil {
|
|
||||||
log.Errorf("HTTP failed to write response: %v", err)
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func emergencyShutdown() {
|
func emergencyShutdown() {
|
||||||
|
|||||||
@@ -20,6 +20,7 @@ func MailboxIndex(w http.ResponseWriter, req *http.Request, ctx *httpd.Context)
|
|||||||
|
|
||||||
if len(name) == 0 {
|
if len(name) == 0 {
|
||||||
ctx.Session.AddFlash("Account name is required", "errors")
|
ctx.Session.AddFlash("Account name is required", "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -27,12 +28,16 @@ func MailboxIndex(w http.ResponseWriter, req *http.Request, ctx *httpd.Context)
|
|||||||
name, err = smtpd.ParseMailboxName(name)
|
name, err = smtpd.ParseMailboxName(name)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Session.AddFlash(err.Error(), "errors")
|
ctx.Session.AddFlash(err.Error(), "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Remember this mailbox was visited
|
// Remember this mailbox was visited
|
||||||
RememberMailbox(ctx, name)
|
RememberMailbox(ctx, name)
|
||||||
|
if err = ctx.Session.Save(req, w); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
return httpd.RenderTemplate("mailbox/index.html", w, map[string]interface{}{
|
return httpd.RenderTemplate("mailbox/index.html", w, map[string]interface{}{
|
||||||
"ctx": ctx,
|
"ctx": ctx,
|
||||||
@@ -48,6 +53,7 @@ func MailboxLink(w http.ResponseWriter, req *http.Request, ctx *httpd.Context) (
|
|||||||
name, err := smtpd.ParseMailboxName(ctx.Vars["name"])
|
name, err := smtpd.ParseMailboxName(ctx.Vars["name"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Session.AddFlash(err.Error(), "errors")
|
ctx.Session.AddFlash(err.Error(), "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -57,7 +63,7 @@ func MailboxLink(w http.ResponseWriter, req *http.Request, ctx *httpd.Context) (
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// MailboxList renders a list of messages in a mailbox. Renders JSON or a partial
|
// MailboxList renders a list of messages in a mailbox. Renders a partial
|
||||||
func MailboxList(w http.ResponseWriter, req *http.Request, ctx *httpd.Context) (err error) {
|
func MailboxList(w http.ResponseWriter, req *http.Request, ctx *httpd.Context) (err error) {
|
||||||
// Don't have to validate these aren't empty, Gorilla returns 404
|
// Don't have to validate these aren't empty, Gorilla returns 404
|
||||||
name, err := smtpd.ParseMailboxName(ctx.Vars["name"])
|
name, err := smtpd.ParseMailboxName(ctx.Vars["name"])
|
||||||
@@ -203,6 +209,7 @@ func MailboxDownloadAttach(w http.ResponseWriter, req *http.Request, ctx *httpd.
|
|||||||
name, err := smtpd.ParseMailboxName(ctx.Vars["name"])
|
name, err := smtpd.ParseMailboxName(ctx.Vars["name"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Session.AddFlash(err.Error(), "errors")
|
ctx.Session.AddFlash(err.Error(), "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -210,6 +217,7 @@ func MailboxDownloadAttach(w http.ResponseWriter, req *http.Request, ctx *httpd.
|
|||||||
num, err := strconv.ParseUint(numStr, 10, 32)
|
num, err := strconv.ParseUint(numStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Session.AddFlash("Attachment number must be unsigned numeric", "errors")
|
ctx.Session.AddFlash("Attachment number must be unsigned numeric", "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -234,6 +242,7 @@ func MailboxDownloadAttach(w http.ResponseWriter, req *http.Request, ctx *httpd.
|
|||||||
}
|
}
|
||||||
if int(num) >= len(body.Attachments) {
|
if int(num) >= len(body.Attachments) {
|
||||||
ctx.Session.AddFlash("Attachment number too high", "errors")
|
ctx.Session.AddFlash("Attachment number too high", "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -253,6 +262,7 @@ func MailboxViewAttach(w http.ResponseWriter, req *http.Request, ctx *httpd.Cont
|
|||||||
name, err := smtpd.ParseMailboxName(ctx.Vars["name"])
|
name, err := smtpd.ParseMailboxName(ctx.Vars["name"])
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Session.AddFlash(err.Error(), "errors")
|
ctx.Session.AddFlash(err.Error(), "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -261,6 +271,7 @@ func MailboxViewAttach(w http.ResponseWriter, req *http.Request, ctx *httpd.Cont
|
|||||||
num, err := strconv.ParseUint(numStr, 10, 32)
|
num, err := strconv.ParseUint(numStr, 10, 32)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
ctx.Session.AddFlash("Attachment number must be unsigned numeric", "errors")
|
ctx.Session.AddFlash("Attachment number must be unsigned numeric", "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -285,6 +296,7 @@ func MailboxViewAttach(w http.ResponseWriter, req *http.Request, ctx *httpd.Cont
|
|||||||
}
|
}
|
||||||
if int(num) >= len(body.Attachments) {
|
if int(num) >= len(body.Attachments) {
|
||||||
ctx.Session.AddFlash("Attachment number too high", "errors")
|
ctx.Session.AddFlash("Attachment number too high", "errors")
|
||||||
|
_ = ctx.Session.Save(req, w)
|
||||||
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
http.Redirect(w, req, httpd.Reverse("RootIndex"), http.StatusSeeOther)
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user