http.go 758 B

123456789101112131415161718192021222324252627282930313233343536
  1. package tcp
  2. import (
  3. "net/http"
  4. "v2ray.com/core/common/net"
  5. )
  6. type Server struct {
  7. Port net.Port
  8. PathHandler map[string]http.HandlerFunc
  9. accepting bool
  10. }
  11. func (server *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
  12. if req.URL.Path == "/" {
  13. resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
  14. resp.WriteHeader(http.StatusOK)
  15. resp.Write([]byte("Home"))
  16. return
  17. }
  18. handler, found := server.PathHandler[req.URL.Path]
  19. if found {
  20. handler(resp, req)
  21. }
  22. }
  23. func (server *Server) Start() (net.Destination, error) {
  24. go http.ListenAndServe("127.0.0.1:"+server.Port.String(), server)
  25. return net.TCPDestination(net.LocalHostIP, net.Port(server.Port)), nil
  26. }
  27. func (v *Server) Close() {
  28. v.accepting = false
  29. }