ghttp_response_write.go 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223
  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. "fmt"
  10. "github.com/gogf/gf/internal/json"
  11. "net/http"
  12. "github.com/gogf/gf/encoding/gparser"
  13. "github.com/gogf/gf/util/gconv"
  14. )
  15. // Write writes <content> to the response buffer.
  16. func (r *Response) Write(content ...interface{}) {
  17. if r.hijacked || len(content) == 0 {
  18. return
  19. }
  20. if r.Status == 0 {
  21. r.Status = http.StatusOK
  22. }
  23. for _, v := range content {
  24. switch value := v.(type) {
  25. case []byte:
  26. r.buffer.Write(value)
  27. case string:
  28. r.buffer.WriteString(value)
  29. default:
  30. r.buffer.WriteString(gconv.String(v))
  31. }
  32. }
  33. }
  34. // WriteExit writes <content> to the response buffer and exits executing of current handler.
  35. // The "Exit" feature is commonly used to replace usage of return statement in the handler,
  36. // for convenience.
  37. func (r *Response) WriteExit(content ...interface{}) {
  38. r.Write(content...)
  39. r.Request.Exit()
  40. }
  41. // WriteOver overwrites the response buffer with <content>.
  42. func (r *Response) WriteOver(content ...interface{}) {
  43. r.ClearBuffer()
  44. r.Write(content...)
  45. }
  46. // WriteOverExit overwrites the response buffer with <content> and exits executing
  47. // of current handler. The "Exit" feature is commonly used to replace usage of return
  48. // statement in the handler, for convenience.
  49. func (r *Response) WriteOverExit(content ...interface{}) {
  50. r.WriteOver(content...)
  51. r.Request.Exit()
  52. }
  53. // Writef writes the response with fmt.Sprintf.
  54. func (r *Response) Writef(format string, params ...interface{}) {
  55. r.Write(fmt.Sprintf(format, params...))
  56. }
  57. // WritefExit writes the response with fmt.Sprintf and exits executing of current handler.
  58. // The "Exit" feature is commonly used to replace usage of return statement in the handler,
  59. // for convenience.
  60. func (r *Response) WritefExit(format string, params ...interface{}) {
  61. r.Writef(format, params...)
  62. r.Request.Exit()
  63. }
  64. // Writef writes the response with <content> and new line.
  65. func (r *Response) Writeln(content ...interface{}) {
  66. if len(content) == 0 {
  67. r.Write("\n")
  68. return
  69. }
  70. r.Write(append(content, "\n")...)
  71. }
  72. // WritelnExit writes the response with <content> and new line and exits executing
  73. // of current handler. The "Exit" feature is commonly used to replace usage of return
  74. // statement in the handler, for convenience.
  75. func (r *Response) WritelnExit(content ...interface{}) {
  76. r.Writeln(content...)
  77. r.Request.Exit()
  78. }
  79. // Writefln writes the response with fmt.Sprintf and new line.
  80. func (r *Response) Writefln(format string, params ...interface{}) {
  81. r.Writeln(fmt.Sprintf(format, params...))
  82. }
  83. // WriteflnExit writes the response with fmt.Sprintf and new line and exits executing
  84. // of current handler. The "Exit" feature is commonly used to replace usage of return
  85. // statement in the handler, for convenience.
  86. func (r *Response) WriteflnExit(format string, params ...interface{}) {
  87. r.Writefln(format, params...)
  88. r.Request.Exit()
  89. }
  90. // WriteJson writes <content> to the response with JSON format.
  91. func (r *Response) WriteJson(content interface{}) error {
  92. // If given string/[]byte, response it directly to client.
  93. switch content.(type) {
  94. case string, []byte:
  95. r.Header().Set("Content-Type", "application/json")
  96. r.Write(gconv.String(content))
  97. return nil
  98. }
  99. // Else use json.Marshal function to encode the parameter.
  100. if b, err := json.Marshal(content); err != nil {
  101. return err
  102. } else {
  103. r.Header().Set("Content-Type", "application/json")
  104. r.Write(b)
  105. }
  106. return nil
  107. }
  108. // WriteJsonExit writes <content> to the response with JSON format and exits executing
  109. // of current handler if success. The "Exit" feature is commonly used to replace usage of
  110. // return statement in the handler, for convenience.
  111. func (r *Response) WriteJsonExit(content interface{}) error {
  112. if err := r.WriteJson(content); err != nil {
  113. return err
  114. }
  115. r.Request.Exit()
  116. return nil
  117. }
  118. // WriteJson writes <content> to the response with JSONP format.
  119. //
  120. // Note that there should be a "callback" parameter in the request for JSONP format.
  121. func (r *Response) WriteJsonP(content interface{}) error {
  122. // If given string/[]byte, response it directly to client.
  123. switch content.(type) {
  124. case string, []byte:
  125. r.Header().Set("Content-Type", "application/json")
  126. r.Write(gconv.String(content))
  127. return nil
  128. }
  129. // Else use json.Marshal function to encode the parameter.
  130. if b, err := json.Marshal(content); err != nil {
  131. return err
  132. } else {
  133. //r.Header().Set("Content-Type", "application/json")
  134. if callback := r.Request.GetString("callback"); callback != "" {
  135. buffer := []byte(callback)
  136. buffer = append(buffer, byte('('))
  137. buffer = append(buffer, b...)
  138. buffer = append(buffer, byte(')'))
  139. r.Write(buffer)
  140. } else {
  141. r.Write(b)
  142. }
  143. }
  144. return nil
  145. }
  146. // WriteJsonPExit writes <content> to the response with JSONP format and exits executing
  147. // of current handler if success. The "Exit" feature is commonly used to replace usage of
  148. // return statement in the handler, for convenience.
  149. //
  150. // Note that there should be a "callback" parameter in the request for JSONP format.
  151. func (r *Response) WriteJsonPExit(content interface{}) error {
  152. if err := r.WriteJsonP(content); err != nil {
  153. return err
  154. }
  155. r.Request.Exit()
  156. return nil
  157. }
  158. // WriteXml writes <content> to the response with XML format.
  159. func (r *Response) WriteXml(content interface{}, rootTag ...string) error {
  160. // If given string/[]byte, response it directly to client.
  161. switch content.(type) {
  162. case string, []byte:
  163. r.Header().Set("Content-Type", "application/xml")
  164. r.Write(gconv.String(content))
  165. return nil
  166. }
  167. // Else use gparser.VarToXml function to encode the parameter.
  168. if b, err := gparser.VarToXml(content, rootTag...); err != nil {
  169. return err
  170. } else {
  171. r.Header().Set("Content-Type", "application/xml")
  172. r.Write(b)
  173. }
  174. return nil
  175. }
  176. // WriteXmlExit writes <content> to the response with XML format and exits executing
  177. // of current handler if success. The "Exit" feature is commonly used to replace usage
  178. // of return statement in the handler, for convenience.
  179. func (r *Response) WriteXmlExit(content interface{}, rootTag ...string) error {
  180. if err := r.WriteXml(content, rootTag...); err != nil {
  181. return err
  182. }
  183. r.Request.Exit()
  184. return nil
  185. }
  186. // WriteStatus writes HTTP <status> and <content> to the response.
  187. // Note that do not set Content-Type header here.
  188. func (r *Response) WriteStatus(status int, content ...interface{}) {
  189. r.WriteHeader(status)
  190. if len(content) > 0 {
  191. r.Write(content...)
  192. } else {
  193. r.Write(http.StatusText(status))
  194. }
  195. }
  196. // WriteStatusExit writes HTTP <status> and <content> to the response and exits executing
  197. // of current handler if success. The "Exit" feature is commonly used to replace usage of
  198. // return statement in the handler, for convenience.
  199. func (r *Response) WriteStatusExit(status int, content ...interface{}) {
  200. r.WriteStatus(status, content...)
  201. r.Request.Exit()
  202. }