http.go 3.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134
  1. package http
  2. import (
  3. "bytes"
  4. "io"
  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 RequestAuthenticator struct {
  14. config *RequestConfig
  15. }
  16. func (this *RequestAuthenticator) Seal(writer io.Writer) io.Writer {
  17. header := alloc.NewLocalBuffer(2048)
  18. header.AppendString(this.config.Method).AppendString(" ").AppendString(this.config.PickUri()).AppendString(" ").AppendString(this.config.GetVersion()).AppendString(CRLF)
  19. headers := this.config.PickHeaders()
  20. for _, h := range headers {
  21. header.AppendString(h).AppendString(CRLF)
  22. }
  23. header.AppendString(CRLF)
  24. writer.Write(header.Value)
  25. header.Release()
  26. return writer
  27. }
  28. func (this *RequestAuthenticator) Open(reader io.Reader) (io.Reader, error) {
  29. buffer := alloc.NewLocalBuffer(2048)
  30. for {
  31. _, err := buffer.FillFrom(reader)
  32. if err != nil {
  33. return nil, err
  34. }
  35. if n := bytes.Index(buffer.Value, []byte(ENDING)); n != -1 {
  36. buffer.SliceFrom(n + len(ENDING))
  37. return &BufferAndReader{
  38. buffer: buffer,
  39. reader: reader,
  40. }, nil
  41. }
  42. if buffer.Len() >= len(ENDING) {
  43. copy(buffer.Value, buffer.Value[buffer.Len()-len(ENDING):])
  44. buffer.Slice(0, len(ENDING))
  45. }
  46. }
  47. }
  48. type BufferAndReader struct {
  49. buffer *alloc.Buffer
  50. reader io.Reader
  51. }
  52. func (this *BufferAndReader) Read(b []byte) (int, error) {
  53. if this.buffer.Len() == 0 {
  54. return this.reader.Read(b)
  55. }
  56. n, err := this.buffer.Read(b)
  57. if n == this.buffer.Len() {
  58. this.buffer.Release()
  59. this.buffer = nil
  60. }
  61. return n, err
  62. }
  63. type RequestAuthenticatorFactory struct{}
  64. func (RequestAuthenticatorFactory) Create(config interface{}) internet.ConnectionAuthenticator {
  65. return &RequestAuthenticator{
  66. config: config.(*RequestConfig),
  67. }
  68. }
  69. type ResponseAuthenticator struct {
  70. config *ResponseConfig
  71. }
  72. func (this *ResponseAuthenticator) Seal(writer io.Writer) io.Writer {
  73. header := alloc.NewLocalBuffer(2048)
  74. header.AppendString(this.config.GetVersion()).AppendString(" ").AppendString(this.config.Status).AppendString(" ").AppendString(this.config.Reason).AppendString(CRLF)
  75. headers := this.config.PickHeaders()
  76. for _, h := range headers {
  77. header.AppendString(h).AppendString(CRLF)
  78. }
  79. header.AppendString(CRLF)
  80. writer.Write(header.Value)
  81. header.Release()
  82. return writer
  83. }
  84. func (this *ResponseAuthenticator) Open(reader io.Reader) (io.Reader, error) {
  85. buffer := alloc.NewLocalBuffer(2048)
  86. for {
  87. _, err := buffer.FillFrom(reader)
  88. if err != nil {
  89. return nil, err
  90. }
  91. if n := bytes.Index(buffer.Value, []byte(ENDING)); n != -1 {
  92. buffer.SliceFrom(n + len(ENDING))
  93. return &BufferAndReader{
  94. buffer: buffer,
  95. reader: reader,
  96. }, nil
  97. }
  98. if buffer.Len() >= len(ENDING) {
  99. copy(buffer.Value, buffer.Value[buffer.Len()-len(ENDING):])
  100. buffer.Slice(0, len(ENDING))
  101. }
  102. }
  103. }
  104. type ResponseAuthenticatorFactory struct{}
  105. func (ResponseAuthenticatorFactory) Create(config interface{}) internet.ConnectionAuthenticator {
  106. return &ResponseAuthenticator{
  107. config: config.(*ResponseConfig),
  108. }
  109. }
  110. func init() {
  111. internet.RegisterConnectionAuthenticator(loader.GetType(new(RequestConfig)), RequestAuthenticatorFactory{})
  112. internet.RegisterConnectionAuthenticator(loader.GetType(new(ResponseConfig)), ResponseAuthenticatorFactory{})
  113. }