ghttp_response.go 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145
  1. // Copyright 2017 gf Author(https://github.com/gogf/gf). 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. serveFile := (*StaticFile)(nil)
  38. if file := gres.Get(path); file != nil {
  39. serveFile = &StaticFile{
  40. File: file,
  41. IsDir: file.FileInfo().IsDir(),
  42. }
  43. } else {
  44. path = gfile.RealPath(path)
  45. if path == "" {
  46. r.WriteStatus(http.StatusNotFound)
  47. return
  48. }
  49. serveFile = &StaticFile{Path: path}
  50. }
  51. r.Server.serveFile(r.Request, serveFile, allowIndex...)
  52. }
  53. // ServeFileDownload serves file downloading to the response.
  54. func (r *Response) ServeFileDownload(path string, name ...string) {
  55. serveFile := (*StaticFile)(nil)
  56. downloadName := ""
  57. if len(name) > 0 {
  58. downloadName = name[0]
  59. }
  60. if file := gres.Get(path); file != nil {
  61. serveFile = &StaticFile{
  62. File: file,
  63. IsDir: file.FileInfo().IsDir(),
  64. }
  65. if downloadName == "" {
  66. downloadName = gfile.Basename(file.Name())
  67. }
  68. } else {
  69. path = gfile.RealPath(path)
  70. if path == "" {
  71. r.WriteStatus(http.StatusNotFound)
  72. return
  73. }
  74. serveFile = &StaticFile{Path: path}
  75. if downloadName == "" {
  76. downloadName = gfile.Basename(path)
  77. }
  78. }
  79. r.Header().Set("Content-Type", "application/force-download")
  80. r.Header().Set("Accept-Ranges", "bytes")
  81. r.Header().Set("Content-Disposition", fmt.Sprintf(`attachment;filename="%s"`, downloadName))
  82. r.Server.serveFile(r.Request, serveFile)
  83. }
  84. // RedirectTo redirects client to another location.
  85. // The optional parameter <code> specifies the http status code for redirecting,
  86. // which commonly can be 301 or 302. It's 302 in default.
  87. func (r *Response) RedirectTo(location string, code ...int) {
  88. r.Header().Set("Location", location)
  89. if len(code) > 0 {
  90. r.WriteHeader(code[0])
  91. } else {
  92. r.WriteHeader(http.StatusFound)
  93. }
  94. r.Request.Exit()
  95. }
  96. // RedirectBack redirects client back to referer.
  97. // The optional parameter <code> specifies the http status code for redirecting,
  98. // which commonly can be 301 or 302. It's 302 in default.
  99. func (r *Response) RedirectBack(code ...int) {
  100. r.RedirectTo(r.Request.GetReferer(), code...)
  101. }
  102. // BufferString returns the buffered content as []byte.
  103. func (r *Response) Buffer() []byte {
  104. return r.buffer.Bytes()
  105. }
  106. // BufferString returns the buffered content as string.
  107. func (r *Response) BufferString() string {
  108. return r.buffer.String()
  109. }
  110. // BufferLength returns the length of the buffered content.
  111. func (r *Response) BufferLength() int {
  112. return r.buffer.Len()
  113. }
  114. // SetBuffer overwrites the buffer with <data>.
  115. func (r *Response) SetBuffer(data []byte) {
  116. r.buffer.Reset()
  117. r.buffer.Write(data)
  118. }
  119. // ClearBuffer clears the response buffer.
  120. func (r *Response) ClearBuffer() {
  121. r.buffer.Reset()
  122. }
  123. // Output outputs the buffer content to the client and clears the buffer.
  124. func (r *Response) Flush() {
  125. if r.Server.config.ServerAgent != "" {
  126. r.Header().Set("Server", r.Server.config.ServerAgent)
  127. }
  128. r.Writer.Flush()
  129. }