Factored out HTTP error + log

This commit is contained in:
Michael Bridgen committed 2014-10-22 11:58:55 +01:00
1 parent 1d5a55ea75
commit e21069f3ab
1 file changed
+37 -18
+37 -18
View File
@@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"github.com/miekg/dns"
"log"
"net"
"net/http"
"strings"
@@ -18,75 +19,93 @@ func parseUrl(url string) (identifier string, ipaddr string, err error) {
return parts[2], parts[3], nil
}
func httpErrorAndLog(level *log.Logger, w http.ResponseWriter, msg string,
status int, logmsg string, logargs ...interface{}) {
http.Error(w, msg, status)
level.Printf(logmsg, logargs...)
}
func ListenHttp(domain string, db Zone, port int) {
http.HandleFunc("/name/", func(w http.ResponseWriter, r *http.Request) {
reqError := func(msg string, logmsg string, logargs ...interface{}) {
httpErrorAndLog(Warning, w, msg, http.StatusBadRequest,
logmsg, logargs...)
}
switch r.Method {
case "PUT":
identifier, weave_ipstr, err := parseUrl(r.URL.Path)
name := r.FormValue("fqdn")
prefix := r.FormValue("routing_prefix")
local_ip := r.FormValue("local_ip")
if identifier == "" || weave_ipstr == "" || name == "" || prefix == "" || local_ip == "" {
Warning.Printf("Invalid request: %s, %s", r.URL, r.Form)
http.Error(w, "Invalid Request", http.StatusBadRequest)
reqError("Invalid request", "Invalid request: %s, %s", r.URL, r.Form)
return
}
ip := net.ParseIP(local_ip)
if ip == nil {
Warning.Printf("Invalid IP in request: %s", local_ip)
http.Error(w, "Invalid IP in request", http.StatusBadRequest)
reqError("Invalid IP in request", "Invalid IP in request: %s", local_ip)
return
}
weave_cidr := weave_ipstr + "/" + prefix
weave_ip, subnet, err := net.ParseCIDR(weave_cidr)
if err != nil {
Warning.Printf("Invalid CIDR in request: %s", weave_cidr)
http.Error(w, fmt.Sprintf("Invalid CIDR: %s", weave_cidr), http.StatusBadRequest)
reqError("Invalid CIDR", "Invalid CIDR in request: %s", weave_cidr)
return
}
if dns.IsSubDomain(domain, name) {
Info.Printf("Adding %s (%s) -> %s", name, local_ip, weave_cidr)
err = db.AddRecord(identifier, name, ip, weave_ip, subnet)
if err != nil {
dup, ok := err.(DuplicateError)
if !ok {
Error.Print("Unexpected error from DB", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
httpErrorAndLog(
Error, w, "Internal error", http.StatusInternalServerError,
"Unexpected error from DB", err)
return
} else if dup.Ident != identifier {
http.Error(w, err.Error(), http.StatusConflict)
}
return
return
} // else we are golden
}
} else {
Info.Printf("Ignoring name %s, not in %s", name, domain)
}
case "DELETE":
identifier, weave_ipstr, err := parseUrl(r.URL.Path)
if identifier == "" || weave_ipstr == "" {
Warning.Printf("Invalid request: %s, %s", r.URL, r.Form)
http.Error(w, "Invalid Request", http.StatusBadRequest)
reqError("Invalid Request", "Invalid request: %s, %s", r.URL, r.Form)
return
}
weave_ip := net.ParseIP(weave_ipstr)
if weave_ip == nil {
Warning.Printf("Invalid IP in request: %s", weave_ipstr)
http.Error(w, "Invalid IP in request", http.StatusBadRequest)
reqError("Invalid IP in request", "Invalid IP in request: %s", weave_ipstr)
return
}
Info.Printf("Deleting %s (%s)", identifier, weave_ipstr)
err = db.DeleteRecord(identifier, weave_ip)
if err != nil {
if _, ok := err.(LookupError); !ok {
Error.Print("Unexpected error from DB", err)
http.Error(w, "Internal error", http.StatusInternalServerError)
httpErrorAndLog(
Error, w, "Internal error", http.StatusInternalServerError,
"Unexpected error from DB", err)
return
}
}
default:
Warning.Println("Unexpected http method", r.Method)
http.Error(w, "Unexpected http method: "+r.Method, http.StatusBadRequest)
msg := "Unexpected http method: " + r.Method
reqError(msg, msg)
return
}
})
address := fmt.Sprintf(":%d", port)
err := http.ListenAndServe(address, nil)
if err != nil {