ghttp_response.go 4.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright GoFrame Author(https://goframe.org). All Rights Reserved.
  2. //
  3. // This Source Code Form is subject to the terms of the MIT License.
  4. // If a copy of the MIT was not distributed with this file,
  5. // You can obtain one at https://github.com/gogf/gf.
  6. //
  7. package ghttp
  8. import (
  9. "bytes"
  10. "fmt"
  11. "io"
  12. "net/http"
  13. "net/url"
  14. "time"
  15. "github.com/gogf/gf/v2/net/ghttp/internal/response"
  16. "github.com/gogf/gf/v2/net/gtrace"
  17. "github.com/gogf/gf/v2/os/gfile"
  18. "github.com/gogf/gf/v2/os/gres"
  19. )
  20. // Response is the http response manager.
  21. // Note that it implements the http.ResponseWriter interface with buffering feature.
  22. type Response struct {
  23. *ResponseWriter // Underlying ResponseWriter.
  24. Server *Server // Parent server.
  25. Writer *ResponseWriter // Alias of ResponseWriter.
  26. Request *Request // According request.
  27. }
  28. // newResponse creates and returns a new Response object.
  29. func newResponse(s *Server, w http.ResponseWriter) *Response {
  30. r := &Response{
  31. Server: s,
  32. ResponseWriter: &ResponseWriter{
  33. writer: response.NewWriter(w),
  34. buffer: bytes.NewBuffer(nil),
  35. },
  36. }
  37. r.Writer = r.ResponseWriter
  38. return r
  39. }
  40. // ServeFile serves the file to the response.
  41. func (r *Response) ServeFile(path string, allowIndex ...bool) {
  42. var (
  43. serveFile *staticFile
  44. )
  45. if file := gres.Get(path); file != nil {
  46. serveFile = &staticFile{
  47. File: file,
  48. IsDir: file.FileInfo().IsDir(),
  49. }
  50. } else {
  51. path, _ = gfile.Search(path)
  52. if path == "" {
  53. r.WriteStatus(http.StatusNotFound)
  54. return
  55. }
  56. serveFile = &staticFile{Path: path}
  57. }
  58. r.Server.serveFile(r.Request, serveFile, allowIndex...)
  59. }
  60. // ServeFileDownload serves file downloading to the response.
  61. func (r *Response) ServeFileDownload(path string, name ...string) {
  62. var (
  63. serveFile *staticFile
  64. downloadName = ""
  65. )
  66. if len(name) > 0 {
  67. downloadName = name[0]
  68. }
  69. if file := gres.Get(path); file != nil {
  70. serveFile = &staticFile{
  71. File: file,
  72. IsDir: file.FileInfo().IsDir(),
  73. }
  74. if downloadName == "" {
  75. downloadName = gfile.Basename(file.Name())
  76. }
  77. } else {
  78. path, _ = gfile.Search(path)
  79. if path == "" {
  80. r.WriteStatus(http.StatusNotFound)
  81. return
  82. }
  83. serveFile = &staticFile{Path: path}
  84. if downloadName == "" {
  85. downloadName = gfile.Basename(path)
  86. }
  87. }
  88. r.Header().Set("Content-Type", "application/force-download")
  89. r.Header().Set("Accept-Ranges", "bytes")
  90. r.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename=%s`, url.QueryEscape(downloadName)))
  91. r.Server.serveFile(r.Request, serveFile)
  92. }
  93. // RedirectTo redirects the client to another location.
  94. // The optional parameter `code` specifies the http status code for redirecting,
  95. // which commonly can be 301 or 302. It's 302 in default.
  96. func (r *Response) RedirectTo(location string, code ...int) {
  97. r.Header().Set("Location", location)
  98. if len(code) > 0 {
  99. r.WriteHeader(code[0])
  100. } else {
  101. r.WriteHeader(http.StatusFound)
  102. }
  103. r.Request.Exit()
  104. }
  105. // RedirectBack redirects the client back to referer.
  106. // The optional parameter `code` specifies the http status code for redirecting,
  107. // which commonly can be 301 or 302. It's 302 in default.
  108. func (r *Response) RedirectBack(code ...int) {
  109. r.RedirectTo(r.Request.GetReferer(), code...)
  110. }
  111. // Buffer returns the buffered content as []byte.
  112. func (r *Response) Buffer() []byte {
  113. return r.buffer.Bytes()
  114. }
  115. // BufferString returns the buffered content as string.
  116. func (r *Response) BufferString() string {
  117. return r.buffer.String()
  118. }
  119. // BufferLength returns the length of the buffered content.
  120. func (r *Response) BufferLength() int {
  121. return r.buffer.Len()
  122. }
  123. // SetBuffer overwrites the buffer with `data`.
  124. func (r *Response) SetBuffer(data []byte) {
  125. r.buffer.Reset()
  126. r.buffer.Write(data)
  127. }
  128. // ClearBuffer clears the response buffer.
  129. func (r *Response) ClearBuffer() {
  130. r.buffer.Reset()
  131. }
  132. // ServeContent replies to the request using the content in the
  133. // provided ReadSeeker. The main benefit of ServeContent over io.Copy
  134. // is that it handles Range requests properly, sets the MIME type, and
  135. // handles If-Match, If-Unmodified-Since, If-None-Match, If-Modified-Since,
  136. // and If-Range requests.
  137. //
  138. // See http.ServeContent
  139. func (r *Response) ServeContent(name string, modTime time.Time, content io.ReadSeeker) {
  140. http.ServeContent(r.Writer.RawWriter(), r.Request.Request, name, modTime, content)
  141. }
  142. // Flush outputs the buffer content to the client and clears the buffer.
  143. func (r *Response) Flush() {
  144. r.Header().Set(responseHeaderTraceID, gtrace.GetTraceID(r.Request.Context()))
  145. if r.Server.config.ServerAgent != "" {
  146. r.Header().Set("Server", r.Server.config.ServerAgent)
  147. }
  148. r.Writer.Flush()
  149. }