http.go 3.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130
  1. package http
  2. import (
  3. "bytes"
  4. "net"
  5. "v2ray.com/core/common/alloc"
  6. "v2ray.com/core/common/loader"
  7. "v2ray.com/core/transport/internet"
  8. )
  9. const (
  10. CRLF = "\r\n"
  11. ENDING = CRLF + CRLF
  12. )
  13. type HttpConn struct {
  14. net.Conn
  15. buffer *alloc.Buffer
  16. readHeader bool
  17. writeHeaderContent *alloc.Buffer
  18. writeHeader bool
  19. }
  20. func NewHttpConn(conn net.Conn, writeHeaderContent *alloc.Buffer) *HttpConn {
  21. return &HttpConn{
  22. Conn: conn,
  23. readHeader: true,
  24. writeHeader: true,
  25. writeHeaderContent: writeHeaderContent,
  26. }
  27. }
  28. func (this *HttpConn) Read(b []byte) (int, error) {
  29. if this.readHeader {
  30. buffer := alloc.NewLocalBuffer(2048)
  31. for {
  32. _, err := buffer.FillFrom(this.Conn)
  33. if err != nil {
  34. return 0, err
  35. }
  36. if n := bytes.Index(buffer.Value, []byte(ENDING)); n != -1 {
  37. buffer.SliceFrom(n + len(ENDING))
  38. break
  39. }
  40. if buffer.Len() >= len(ENDING) {
  41. copy(buffer.Value, buffer.Value[buffer.Len()-len(ENDING):])
  42. buffer.Slice(0, len(ENDING))
  43. }
  44. }
  45. this.buffer = buffer
  46. this.readHeader = false
  47. }
  48. if this.buffer.Len() > 0 {
  49. nBytes, err := this.buffer.Read(b)
  50. if nBytes == this.buffer.Len() {
  51. this.buffer.Release()
  52. this.buffer = nil
  53. }
  54. return nBytes, err
  55. }
  56. return this.Conn.Read(b)
  57. }
  58. func (this *HttpConn) Write(b []byte) (int, error) {
  59. if this.writeHeader {
  60. _, err := this.Conn.Write(this.writeHeaderContent.Value)
  61. this.writeHeaderContent.Release()
  62. if err != nil {
  63. return 0, err
  64. }
  65. this.writeHeader = false
  66. }
  67. return this.Conn.Write(b)
  68. }
  69. type HttpAuthenticator struct {
  70. config *Config
  71. }
  72. func (this HttpAuthenticator) GetClientWriteHeader() *alloc.Buffer {
  73. header := alloc.NewLocalBuffer(2048)
  74. config := this.config.Request
  75. header.AppendString(config.Method.GetValue()).AppendString(" ").AppendString(config.PickUri()).AppendString(" ").AppendString(config.GetFullVersion()).AppendString(CRLF)
  76. headers := config.PickHeaders()
  77. for _, h := range headers {
  78. header.AppendString(h).AppendString(CRLF)
  79. }
  80. header.AppendString(CRLF)
  81. return header
  82. }
  83. func (this HttpAuthenticator) GetServerWriteHeader() *alloc.Buffer {
  84. header := alloc.NewLocalBuffer(2048)
  85. config := this.config.Response
  86. header.AppendString(config.GetFullVersion()).AppendString(" ").AppendString(config.Status.GetCode()).AppendString(" ").AppendString(config.Status.GetReason()).AppendString(CRLF)
  87. headers := config.PickHeaders()
  88. for _, h := range headers {
  89. header.AppendString(h).AppendString(CRLF)
  90. }
  91. header.AppendString(CRLF)
  92. return header
  93. }
  94. func (this HttpAuthenticator) Client(conn net.Conn) net.Conn {
  95. return NewHttpConn(conn, this.GetClientWriteHeader())
  96. }
  97. func (this HttpAuthenticator) Server(conn net.Conn) net.Conn {
  98. return NewHttpConn(conn, this.GetServerWriteHeader())
  99. }
  100. type HttpAuthenticatorFactory struct{}
  101. func (HttpAuthenticatorFactory) Create(config interface{}) internet.ConnectionAuthenticator {
  102. return HttpAuthenticator{
  103. config: config.(*Config),
  104. }
  105. }
  106. func init() {
  107. internet.RegisterConnectionAuthenticator(loader.GetType(new(Config)), HttpAuthenticatorFactory{})
  108. }