send.go 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. // Copyright 2019 Yunion
  2. //
  3. // Licensed under the Apache License, Version 2.0 (the "License");
  4. // you may not use this file except in compliance with the License.
  5. // You may obtain a copy of the License at
  6. //
  7. // http://www.apache.org/licenses/LICENSE-2.0
  8. //
  9. // Unless required by applicable law or agreed to in writing, software
  10. // distributed under the License is distributed on an "AS IS" BASIS,
  11. // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  12. // See the License for the specific language governing permissions and
  13. // limitations under the License.
  14. package appsrv
  15. import (
  16. "encoding/xml"
  17. "fmt"
  18. "io"
  19. "net/http"
  20. "strconv"
  21. "time"
  22. "yunion.io/x/jsonutils"
  23. "yunion.io/x/log"
  24. "yunion.io/x/pkg/errors"
  25. "yunion.io/x/pkg/gotypes"
  26. )
  27. func SendNoContent(w http.ResponseWriter) {
  28. w.WriteHeader(204)
  29. sendBytes(w, []byte{})
  30. }
  31. func Send(w http.ResponseWriter, text string) {
  32. w.Header().Set("Content-Type", "text/plain;charset=utf-8")
  33. sendBytes(w, []byte(text))
  34. }
  35. func SendHTML(w http.ResponseWriter, text string) {
  36. w.Header().Set("Content-Type", "text/html;charset=utf-8")
  37. sendBytes(w, []byte(text))
  38. }
  39. func sendBytes(w http.ResponseWriter, output []byte) {
  40. w.Header().Set("Content-Length", strconv.FormatInt(int64(len(output)), 10))
  41. w.Write(output)
  42. }
  43. func SendStruct(w http.ResponseWriter, obj interface{}) {
  44. jsonObj := jsonutils.Marshal(obj)
  45. SendJSON(w, jsonObj)
  46. }
  47. func SendJSON(w http.ResponseWriter, obj jsonutils.JSONObject) {
  48. var output []byte
  49. w.Header().Set("Content-Type", "application/json;charset=utf-8")
  50. if obj != nil {
  51. output = []byte(obj.String())
  52. }
  53. sendBytes(w, output)
  54. }
  55. func SendHeader(w http.ResponseWriter, hdr http.Header) {
  56. for k, v := range hdr {
  57. if len(v) > 0 && len(v[0]) > 0 {
  58. w.Header().Set(k, v[0])
  59. }
  60. }
  61. w.WriteHeader(204)
  62. w.Write([]byte{})
  63. }
  64. func SendXml(w http.ResponseWriter, hdr http.Header, obj interface{}) {
  65. SendXmlWithIndent(w, hdr, obj, false)
  66. }
  67. func SendXmlWithIndent(w http.ResponseWriter, hdr http.Header, obj interface{}, indent bool) {
  68. if !gotypes.IsNil(obj) {
  69. var xmlBytes []byte
  70. var err error
  71. if indent {
  72. xmlBytes, err = xml.MarshalIndent(obj, "", " ")
  73. } else {
  74. xmlBytes, err = xml.Marshal(obj)
  75. }
  76. if err == nil {
  77. for k, v := range hdr {
  78. if k != "Content-Type" && k != "Content-Length" {
  79. w.Header().Set(k, v[0])
  80. }
  81. }
  82. w.Header().Set("Content-Type", "application/xml;charset=utf-8")
  83. w.Header().Set("Content-Length", strconv.FormatInt(int64(len(xmlBytes)+len(xml.Header)), 10))
  84. w.Write([]byte(xml.Header))
  85. w.Write(xmlBytes)
  86. } else {
  87. w.WriteHeader(400)
  88. Send(w, err.Error())
  89. }
  90. } else {
  91. for k, v := range hdr {
  92. if k != "Content-Type" && k != "Content-Length" {
  93. w.Header().Set(k, v[0])
  94. }
  95. }
  96. SendNoContent(w)
  97. }
  98. }
  99. func SendStream(w http.ResponseWriter, isPartial bool, hdr http.Header, stream io.ReadCloser, sizeBytes int64) error {
  100. defer stream.Close()
  101. for k, v := range hdr {
  102. if k != "Content-Length" {
  103. log.Debugf("send %s %s", k, v)
  104. w.Header().Set(k, v[0])
  105. }
  106. }
  107. if sizeBytes > 0 {
  108. log.Debugf("send content-length %d", sizeBytes)
  109. w.Header().Set("Content-Length", strconv.FormatInt(sizeBytes, 10))
  110. }
  111. if isPartial {
  112. log.Debugf("send partial 206")
  113. w.WriteHeader(206)
  114. } else {
  115. log.Debugf("send full 200")
  116. w.WriteHeader(200)
  117. }
  118. offset := 0
  119. buf := make([]byte, 4096)
  120. for sizeBytes <= 0 || int64(offset) < sizeBytes {
  121. n, err := stream.Read(buf)
  122. if n > 0 {
  123. woff := 0
  124. for woff < n {
  125. m, err := w.Write(buf[woff:n])
  126. if err != nil {
  127. return errors.Wrap(err, fmt.Sprintf("w.Write read_offset %d write_offset %d", offset, woff))
  128. }
  129. woff += m
  130. }
  131. offset += n
  132. }
  133. if err != nil {
  134. if err == io.EOF {
  135. break
  136. }
  137. return errors.Wrap(err, "stream.Read")
  138. }
  139. }
  140. return nil
  141. }
  142. func SendRedirect(w http.ResponseWriter, redirectUrl string) {
  143. w.Header().Set("Location", redirectUrl)
  144. w.WriteHeader(302)
  145. w.Write([]byte{})
  146. }
  147. func DisableClientCache(w http.ResponseWriter) {
  148. // disable client cache
  149. // Expires: Tue, 03 Jul 2001 06:00:00 GMT
  150. // Last-Modified: {now} GMT
  151. // Cache-Control: max-age=0, no-cache, must-revalidate, proxy-revalidate
  152. w.Header().Set("Expires", "Tue, 03 Jul 2001 06:00:00 GMT")
  153. cacheSince := time.Now().Format(http.TimeFormat)
  154. w.Header().Set("Last-Modified", cacheSince)
  155. w.Header().Set("Cache-Control", "max-age=0, no-cache, must-revalidate, proxy-revalidate")
  156. }