Lots of small cleanups
This commit is contained in:
@@ -20,6 +20,12 @@ func init() {
|
||||
})
|
||||
}
|
||||
|
||||
type envdocArgs struct {
|
||||
Headers map[string]string
|
||||
ProcessEnv map[string]string
|
||||
ReqURL string
|
||||
}
|
||||
|
||||
func dumpEnv(w http.ResponseWriter, r *http.Request) {
|
||||
w.Header().Set("Content-Type", "text/html")
|
||||
|
||||
|
||||
@@ -1,24 +1,17 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"flag"
|
||||
"fmt"
|
||||
"git.thequux.com/thequux/ipasso/resources"
|
||||
"git.thequux.com/thequux/ipasso/sso-proxy/backend"
|
||||
"git.thequux.com/thequux/ipasso/util/startup"
|
||||
"github.com/CloudyKit/jet/v6"
|
||||
"github.com/julienschmidt/httprouter"
|
||||
"gitlab.com/jamietanna/content-negotiation-go"
|
||||
"go.uber.org/zap"
|
||||
"log"
|
||||
"net"
|
||||
"net/http"
|
||||
"net/http/fcgi"
|
||||
"os"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
var (
|
||||
@@ -58,89 +51,3 @@ func main() {
|
||||
l.Info("Listening", zap.Stringer("addr", listener.Addr()))
|
||||
log.Fatal(fcgi.Serve(listener, router))
|
||||
}
|
||||
|
||||
type envdocArgs struct {
|
||||
Headers map[string]string
|
||||
ProcessEnv map[string]string
|
||||
ReqURL string
|
||||
}
|
||||
|
||||
// Try to continue negotiation via a www-authenticate header, if this is an error page.
|
||||
func continueNegotate(w http.ResponseWriter, r *http.Request) bool {
|
||||
procEnv := fcgi.ProcessEnv(r)
|
||||
l := zap.L().Named("req")
|
||||
l.Debug("Received request", zap.Any("env", procEnv))
|
||||
|
||||
rStatus, ok := procEnv["REDIRECT_STATUS"]
|
||||
if ok && rStatus == "401" {
|
||||
w.Header().Set("WWW-Authenticate", "Negotiate")
|
||||
w.WriteHeader(401)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type RespErr struct {
|
||||
Err string
|
||||
Status string
|
||||
}
|
||||
|
||||
func ReportError(w http.ResponseWriter, err error) {
|
||||
w.Header().Set("Content-Type", "text/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
js, err0 := json.Marshal(RespErr{Err: err.Error(), Status: "ERROR"})
|
||||
if err0 != nil {
|
||||
panic(err0)
|
||||
}
|
||||
_, _ = w.Write(js)
|
||||
}
|
||||
|
||||
var negotiator = contentnegotiation.NewNegotiator("text/html", "text/plain", "application/json")
|
||||
|
||||
func RenderPage(w http.ResponseWriter, req *http.Request, template string, data interface{}) {
|
||||
ctype, _, err := negotiator.Negotiate(req.Header.Get("Accept"))
|
||||
if err != nil {
|
||||
templateLogger.Warn("Negotiation failed",
|
||||
zap.String("accept", req.Header.Get("Accept")),
|
||||
zap.Error(err),
|
||||
)
|
||||
ReportError(w, err)
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", ctype.String())
|
||||
|
||||
var result []byte
|
||||
var jetTemplate *jet.Template
|
||||
|
||||
if ctype.String() == "text/html" {
|
||||
template = template + ".html"
|
||||
jetTemplate, err = resources.Templates.HtmlTemplate(template)
|
||||
} else if ctype.String() == "text/plain" {
|
||||
template = template + ".txt"
|
||||
jetTemplate, err = resources.Templates.TextTemplate(template)
|
||||
} else if ctype.String() == "application/json" {
|
||||
jetTemplate = nil
|
||||
result, err = json.MarshalIndent(data, "", " ")
|
||||
} else {
|
||||
templateLogger.Warn("Negotiation returned something unexpected", zap.Stringer("negotiated", &ctype))
|
||||
err = errors.New("unexpected negotiation result")
|
||||
}
|
||||
|
||||
if err == nil && jetTemplate != nil && len(result) == 0 {
|
||||
var builder bytes.Buffer
|
||||
err := jetTemplate.Execute(&builder, nil, data)
|
||||
if err != nil {
|
||||
templateLogger.Warn("Failed to render template", zap.String("tmpl", template), zap.Error(err))
|
||||
} else {
|
||||
result = builder.Bytes()
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
w.Header().Set("Content-Type", ctype.String())
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(result)), 10))
|
||||
w.WriteHeader(200)
|
||||
_, _ = w.Write(result)
|
||||
} else {
|
||||
ReportError(w, err)
|
||||
}
|
||||
}
|
||||
|
||||
101
app/ipasso/renderCommon.go
Normal file
101
app/ipasso/renderCommon.go
Normal file
@@ -0,0 +1,101 @@
|
||||
package main
|
||||
|
||||
import (
|
||||
"bytes"
|
||||
"encoding/json"
|
||||
"errors"
|
||||
"git.thequux.com/thequux/ipasso/resources"
|
||||
"github.com/CloudyKit/jet/v6"
|
||||
contentnegotiation "gitlab.com/jamietanna/content-negotiation-go"
|
||||
"go.uber.org/zap"
|
||||
"net/http"
|
||||
"net/http/fcgi"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
// Try to continue negotiation via a www-authenticate header, if this is an error page.
|
||||
func continueNegotate(w http.ResponseWriter, r *http.Request) bool {
|
||||
procEnv := fcgi.ProcessEnv(r)
|
||||
l := zap.L().Named("req")
|
||||
l.Debug("Received request", zap.Any("env", procEnv))
|
||||
|
||||
rStatus, ok := procEnv["REDIRECT_STATUS"]
|
||||
if ok && rStatus == "401" {
|
||||
w.Header().Set("WWW-Authenticate", "Negotiate")
|
||||
w.WriteHeader(401)
|
||||
return true
|
||||
}
|
||||
return false
|
||||
}
|
||||
|
||||
type RespErr struct {
|
||||
Err string
|
||||
Status string
|
||||
}
|
||||
|
||||
func ReportError(w http.ResponseWriter, err error) {
|
||||
w.Header().Set("Content-Type", "text/json")
|
||||
w.WriteHeader(http.StatusInternalServerError)
|
||||
js, err0 := json.Marshal(RespErr{Err: err.Error(), Status: "ERROR"})
|
||||
if err0 != nil {
|
||||
panic(err0)
|
||||
}
|
||||
_, _ = w.Write(js)
|
||||
}
|
||||
|
||||
var ctypes = [resources.ATComboCount]contentnegotiation.Negotiator{
|
||||
contentnegotiation.NewNegotiator("application/json"),
|
||||
contentnegotiation.NewNegotiator("text/html", "application/json", "text/html"),
|
||||
contentnegotiation.NewNegotiator("text/plain", "application/json"),
|
||||
contentnegotiation.NewNegotiator("text/html", "text/plain", "application/json"),
|
||||
}
|
||||
|
||||
//var negotiator = contentnegotiation.NewNegotiator("text/html", "text/plain", "application/json")
|
||||
|
||||
func RenderPage(w http.ResponseWriter, req *http.Request, template string, data interface{}) {
|
||||
act := resources.Templates.GetContentTypes(template)
|
||||
negotiator := ctypes[act]
|
||||
ctype, _, err := negotiator.Negotiate(req.Header.Get("Accept"))
|
||||
if err != nil {
|
||||
w.Header().Set("Content-Length", "0")
|
||||
w.WriteHeader(http.StatusUnsupportedMediaType)
|
||||
return
|
||||
}
|
||||
|
||||
w.Header().Set("Content-Type", ctype.String())
|
||||
|
||||
var result []byte
|
||||
var jetTemplate *jet.Template
|
||||
|
||||
if ctype.String() == "text/html" {
|
||||
//template = template + ".html"
|
||||
jetTemplate, err = resources.Templates.HtmlTemplate(template)
|
||||
} else if ctype.String() == "text/plain" {
|
||||
//template = template + ".txt"
|
||||
jetTemplate, err = resources.Templates.TextTemplate(template)
|
||||
} else if ctype.String() == "application/json" {
|
||||
jetTemplate = nil
|
||||
result, err = json.MarshalIndent(data, "", " ")
|
||||
} else {
|
||||
templateLogger.Warn("Negotiation returned something unexpected", zap.Stringer("negotiated", &ctype))
|
||||
err = errors.New("unexpected negotiation result")
|
||||
}
|
||||
|
||||
if err == nil && jetTemplate != nil && len(result) == 0 {
|
||||
var builder bytes.Buffer
|
||||
err := jetTemplate.Execute(&builder, nil, data)
|
||||
if err != nil {
|
||||
templateLogger.Warn("Failed to render template", zap.String("tmpl", template), zap.Error(err))
|
||||
} else {
|
||||
result = builder.Bytes()
|
||||
}
|
||||
}
|
||||
if err == nil {
|
||||
w.Header().Set("Content-Type", ctype.String())
|
||||
w.Header().Set("Content-Length", strconv.FormatInt(int64(len(result)), 10))
|
||||
w.WriteHeader(200)
|
||||
_, _ = w.Write(result)
|
||||
} else {
|
||||
ReportError(w, err)
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user