MCPcopy Create free account
hub / github.com/CPChain/chain / ServeHTTP

Method ServeHTTP

api/rpc/http.go:244–271  ·  view source on GitHub ↗

ServeHTTP serves JSON-RPC requests over HTTP, implements http.Handler

(w http.ResponseWriter, r *http.Request)

Source from the content-addressed store, hash-verified

242
243// ServeHTTP serves JSON-RPC requests over HTTP, implements http.Handler
244func (h *virtualHostHandler) ServeHTTP(w http.ResponseWriter, r *http.Request) {
245 // if r.Host is not set, we can continue serving since a browser would set the Host header
246 if r.Host == "" {
247 h.next.ServeHTTP(w, r)
248 return
249 }
250 host, _, err := net.SplitHostPort(r.Host)
251 if err != nil {
252 // Either invalid (too many colons) or no port specified
253 host = r.Host
254 }
255 if ipAddr := net.ParseIP(host); ipAddr != nil {
256 // It's an IP address, we can serve that
257 h.next.ServeHTTP(w, r)
258 return
259
260 }
261 // Not an ip address, but a hostname. Need to validate
262 if _, exist := h.vhosts["*"]; exist {
263 h.next.ServeHTTP(w, r)
264 return
265 }
266 if _, exist := h.vhosts[host]; exist {
267 h.next.ServeHTTP(w, r)
268 return
269 }
270 http.Error(w, "invalid host specified", http.StatusForbidden)
271}
272
273func newVHostHandler(vhosts []string, next http.Handler) http.Handler {
274 vhostMap := make(map[string]struct{})

Callers

nothing calls this directly

Calls 2

ErrorMethod · 0.65
ServeHTTPMethod · 0.45

Tested by

no test coverage detected