ghttp_response.go 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  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. "net/http"
  12. "net/url"
  13. "github.com/gogf/gf/v2/net/gtrace"
  14. "github.com/gogf/gf/v2/os/gfile"
  15. "github.com/gogf/gf/v2/os/gres"
  16. )
  17. // Response is the http response manager.
  18. // Note that it implements the http.ResponseWriter interface with buffering feature.
  19. type Response struct {
  20. *ResponseWriter // Underlying ResponseWriter.
  21. Server *Server // Parent server.
  22. Writer *ResponseWriter // Alias of ResponseWriter.
  23. Request *Request // According request.
  24. }
  25. // newResponse creates and returns a new Response object.
  26. func newResponse(s *Server, w http.ResponseWriter) *Response {
  27. r := &Response{
  28. Server: s,
  29. ResponseWriter: &ResponseWriter{
  30. writer: w,
  31. buffer: bytes.NewBuffer(nil),
  32. },
  33. }
  34. r.Writer = r.ResponseWriter
  35. return r
  36. }
  37. // ServeFile serves the file to the response.
  38. func (r *Response) ServeFile(path string, allowIndex ...bool) {
  39. var (
  40. serveFile *staticFile
  41. )
  42. if file := gres.Get(path); file != nil {
  43. serveFile = &staticFile{
  44. File: file,
  45. IsDir: file.FileInfo().IsDir(),
  46. }
  47. } else {
  48. path, _ = gfile.Search(path)
  49. if path == "" {
  50. r.WriteStatus(http.StatusNotFound)
  51. return
  52. }
  53. serveFile = &staticFile{Path: path}
  54. }
  55. r.Server.serveFile(r.Request, serveFile, allowIndex...)
  56. }
  57. // ServeFileDownload serves file downloading to the response.
  58. func (r *Response) ServeFileDownload(path string, name ...string) {
  59. var (
  60. serveFile *staticFile
  61. downloadName = ""
  62. )
  63. if len(name) > 0 {
  64. downloadName = name[0]
  65. }
  66. if file := gres.Get(path); file != nil {
  67. serveFile = &staticFile{
  68. File: file,
  69. IsDir: file.FileInfo().IsDir(),
  70. }
  71. if downloadName == "" {
  72. downloadName = gfile.Basename(file.Name())
  73. }
  74. } else {
  75. path, _ = gfile.Search(path)
  76. if path == "" {
  77. r.WriteStatus(http.StatusNotFound)
  78. return
  79. }
  80. serveFile = &staticFile{Path: path}
  81. if downloadName == "" {
  82. downloadName = gfile.Basename(path)
  83. }
  84. }
  85. r.Header().Set("Content-Type", "application/force-download")
  86. r.Header().Set("Accept-Ranges", "bytes")
  87. r.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename=%s`, url.QueryEscape(downloadName)))
  88. r.Server.serveFile(r.Request, serveFile)
  89. }
  90. // RedirectTo redirects the client to another location.
  91. // The optional parameter `code` specifies the http status code for redirecting,
  92. // which commonly can be 301 or 302. It's 302 in default.
  93. func (r *Response) RedirectTo(location string, code ...int) {
  94. r.Header().Set("Location", location)
  95. if len(code) > 0 {
  96. r.WriteHeader(code[0])
  97. } else {
  98. r.WriteHeader(http.StatusFound)
  99. }
  100. r.Request.Exit()
  101. }
  102. // RedirectBack redirects the client back to referer.
  103. // The optional parameter `code` specifies the http status code for redirecting,
  104. // which commonly can be 301 or 302. It's 302 in default.
  105. func (r *Response) RedirectBack(code ...int) {
  106. r.RedirectTo(r.Request.GetReferer(), code...)
  107. }
  108. // Buffer returns the buffered content as []byte.
  109. func (r *Response) Buffer() []byte {
  110. return r.buffer.Bytes()
  111. }
  112. // BufferString returns the buffered content as string.
  113. func (r *Response) BufferString() string {
  114. return r.buffer.String()
  115. }
  116. // BufferLength returns the length of the buffered content.
  117. func (r *Response) BufferLength() int {
  118. return r.buffer.Len()
  119. }
  120. // SetBuffer overwrites the buffer with `data`.
  121. func (r *Response) SetBuffer(data []byte) {
  122. r.buffer.Reset()
  123. r.buffer.Write(data)
  124. }
  125. // ClearBuffer clears the response buffer.
  126. func (r *Response) ClearBuffer() {
  127. r.buffer.Reset()
  128. }
  129. // Flush outputs the buffer content to the client and clears the buffer.
  130. func (r *Response) Flush() {
  131. r.Header().Set(responseTraceIDHeader, gtrace.GetTraceID(r.Request.Context()))
  132. if r.Server.config.ServerAgent != "" {
  133. r.Header().Set("Server", r.Server.config.ServerAgent)
  134. }
  135. r.Writer.Flush()
  136. }