ghttp_response.go 3.8 KB

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