http.go 823 B

1234567891011121314151617181920212223242526272829303132333435363738394041
  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. server *http.Server
  11. }
  12. func (s *Server) ServeHTTP(resp http.ResponseWriter, req *http.Request) {
  13. if req.URL.Path == "/" {
  14. resp.Header().Set("Content-Type", "text/plain; charset=utf-8")
  15. resp.WriteHeader(http.StatusOK)
  16. resp.Write([]byte("Home"))
  17. return
  18. }
  19. handler, found := s.PathHandler[req.URL.Path]
  20. if found {
  21. handler(resp, req)
  22. }
  23. }
  24. func (s *Server) Start() (net.Destination, error) {
  25. s.server = &http.Server{
  26. Addr: "127.0.0.1:" + s.Port.String(),
  27. Handler: s,
  28. }
  29. go s.server.ListenAndServe()
  30. return net.TCPDestination(net.LocalHostIP, net.Port(s.Port)), nil
  31. }
  32. func (s *Server) Close() error {
  33. return s.server.Close()
  34. }