mirror of
https://github.com/jhillyerd/inbucket.git
synced 2025-12-17 17:47:03 +00:00
53 lines
1.0 KiB
Go
53 lines
1.0 KiB
Go
package web
|
|
|
|
import (
|
|
"github.com/gorilla/mux"
|
|
"github.com/gorilla/sessions"
|
|
"github.com/jhillyerd/inbucket/smtpd"
|
|
"net/http"
|
|
"strings"
|
|
)
|
|
|
|
type Context struct {
|
|
Vars map[string]string
|
|
Session *sessions.Session
|
|
DataStore smtpd.DataStore
|
|
IsJson bool
|
|
}
|
|
|
|
func (c *Context) Close() {
|
|
// Do nothing
|
|
}
|
|
|
|
// headerMatch returns true if the request header specified by name contains
|
|
// the specified value. Case is ignored.
|
|
func headerMatch(req *http.Request, name string, value string) bool {
|
|
name = http.CanonicalHeaderKey(name)
|
|
value = strings.ToLower(value)
|
|
|
|
if header := req.Header[name]; header != nil {
|
|
for _, hv := range header {
|
|
if value == strings.ToLower(hv) {
|
|
return true
|
|
}
|
|
}
|
|
}
|
|
|
|
return false
|
|
}
|
|
|
|
func NewContext(req *http.Request) (*Context, error) {
|
|
vars := mux.Vars(req)
|
|
sess, err := sessionStore.Get(req, "inbucket")
|
|
ctx := &Context{
|
|
Vars: vars,
|
|
Session: sess,
|
|
DataStore: DataStore,
|
|
IsJson: headerMatch(req, "Accept", "application/json"),
|
|
}
|
|
if err != nil {
|
|
return ctx, err
|
|
}
|
|
return ctx, err
|
|
}
|