check response writer err values
This commit is contained in:
@@ -4,6 +4,7 @@ import (
|
|||||||
"encoding/json"
|
"encoding/json"
|
||||||
"encoding/xml"
|
"encoding/xml"
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"io"
|
||||||
"log"
|
"log"
|
||||||
"net/http"
|
"net/http"
|
||||||
|
|
||||||
@@ -15,12 +16,25 @@ type metaResponse struct {
|
|||||||
*subsonic.Response `json:"subsonic-response"`
|
*subsonic.Response `json:"subsonic-response"`
|
||||||
}
|
}
|
||||||
|
|
||||||
|
type errWriter struct {
|
||||||
|
w io.Writer
|
||||||
|
err error
|
||||||
|
}
|
||||||
|
|
||||||
|
func (ew *errWriter) write(buf []byte) {
|
||||||
|
if ew.err != nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
|
_, ew.err = ew.w.Write(buf)
|
||||||
|
}
|
||||||
|
|
||||||
func respondRaw(w http.ResponseWriter, r *http.Request,
|
func respondRaw(w http.ResponseWriter, r *http.Request,
|
||||||
code int, sub *subsonic.Response) {
|
code int, sub *subsonic.Response) {
|
||||||
w.WriteHeader(code)
|
w.WriteHeader(code)
|
||||||
res := metaResponse{
|
res := metaResponse{
|
||||||
Response: sub,
|
Response: sub,
|
||||||
}
|
}
|
||||||
|
ew := &errWriter{w: w}
|
||||||
switch getStrParam(r, "f") {
|
switch getStrParam(r, "f") {
|
||||||
case "json":
|
case "json":
|
||||||
w.Header().Set("Content-Type", "application/json")
|
w.Header().Set("Content-Type", "application/json")
|
||||||
@@ -29,7 +43,7 @@ func respondRaw(w http.ResponseWriter, r *http.Request,
|
|||||||
log.Printf("could not marshall to json: %v\n", err)
|
log.Printf("could not marshall to json: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Write(data)
|
ew.write(data)
|
||||||
case "jsonp":
|
case "jsonp":
|
||||||
w.Header().Set("Content-Type", "application/javascript")
|
w.Header().Set("Content-Type", "application/javascript")
|
||||||
data, err := json.Marshal(res)
|
data, err := json.Marshal(res)
|
||||||
@@ -37,10 +51,10 @@ func respondRaw(w http.ResponseWriter, r *http.Request,
|
|||||||
log.Printf("could not marshall to json: %v\n", err)
|
log.Printf("could not marshall to json: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Write([]byte(getStrParamOr(r, "callback", "cb")))
|
ew.write([]byte(getStrParamOr(r, "callback", "cb")))
|
||||||
w.Write([]byte("("))
|
ew.write([]byte("("))
|
||||||
w.Write(data)
|
ew.write(data)
|
||||||
w.Write([]byte(");"))
|
ew.write([]byte(");"))
|
||||||
default:
|
default:
|
||||||
w.Header().Set("Content-Type", "application/xml")
|
w.Header().Set("Content-Type", "application/xml")
|
||||||
data, err := xml.MarshalIndent(res, "", " ")
|
data, err := xml.MarshalIndent(res, "", " ")
|
||||||
@@ -48,7 +62,10 @@ func respondRaw(w http.ResponseWriter, r *http.Request,
|
|||||||
log.Printf("could not marshall to xml: %v\n", err)
|
log.Printf("could not marshall to xml: %v\n", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
w.Write(data)
|
ew.write(data)
|
||||||
|
}
|
||||||
|
if ew.err != nil {
|
||||||
|
log.Printf("error writing to response: %v\n", ew.err)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user