v2ray-core/testing/servers/http/http.go

42 lines
823 B
Go
Raw Normal View History

2016-01-24 20:48:38 +00:00
package tcp
import (
"net/http"
"v2ray.com/core/common/net"
2016-01-24 20:48:38 +00:00
)
type Server struct {
Port net.Port
2016-01-24 20:48:38 +00:00
PathHandler map[string]http.HandlerFunc
accepting bool
2017-11-09 00:06:40 +00:00
server *http.Server
2016-01-24 20:48:38 +00:00
}
2017-11-09 00:06:40 +00:00
func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
2016-01-24 20:48:38 +00:00
if req.URL.Path == "/" {
resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
resp.WriteHeader(http.StatusOK)
resp.Write([]byte("Home"))
return
}
2017-11-09 00:06:40 +00:00
handler, found := s.PathHandler[req.URL.Path]
2016-01-24 20:48:38 +00:00
if found {
handler(resp, req)
}
}
2017-11-09 00:06:40 +00:00
func (s *Server) Start() (net.Destination, error) {
s.server = &http.Server{
Addr: "127.0.0.1:" + s.Port.String(),
Handler: s,
}
go s.server.ListenAndServe()
return net.TCPDestination(net.LocalHostIP, net.Port(s.Port)), nil
2016-01-24 20:48:38 +00:00
}
2018-02-08 14:39:46 +00:00
func (s *Server) Close() error {
return s.server.Close()
2016-01-24 20:48:38 +00:00
}