output.go 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395
  1. // Copyright 2014 beego Author. All Rights Reserved.
  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 context
  15. import (
  16. "bytes"
  17. "encoding/json"
  18. "encoding/xml"
  19. "errors"
  20. "fmt"
  21. "html/template"
  22. "io"
  23. "mime"
  24. "net/http"
  25. "net/url"
  26. "os"
  27. "path/filepath"
  28. "strconv"
  29. "strings"
  30. "time"
  31. "gopkg.in/yaml.v2"
  32. )
  33. // BeegoOutput does work for sending response header.
  34. type BeegoOutput struct {
  35. Context *Context
  36. Status int
  37. EnableGzip bool
  38. }
  39. // NewOutput returns new BeegoOutput.
  40. // it contains nothing now.
  41. func NewOutput() *BeegoOutput {
  42. return &BeegoOutput{}
  43. }
  44. // Reset init BeegoOutput
  45. func (output *BeegoOutput) Reset(ctx *Context) {
  46. output.Context = ctx
  47. output.Status = 0
  48. }
  49. // Header sets response header item string via given key.
  50. func (output *BeegoOutput) Header(key, val string) {
  51. output.Context.ResponseWriter.Header().Set(key, val)
  52. }
  53. // Body sets response body content.
  54. // if EnableGzip, compress content string.
  55. // it sends out response body directly.
  56. func (output *BeegoOutput) Body(content []byte) error {
  57. var encoding string
  58. var buf = &bytes.Buffer{}
  59. if output.EnableGzip {
  60. encoding = ParseEncoding(output.Context.Request)
  61. }
  62. if b, n, _ := WriteBody(encoding, buf, content); b {
  63. output.Header("Content-Encoding", n)
  64. output.Header("Content-Length", strconv.Itoa(buf.Len()))
  65. } else {
  66. output.Header("Content-Length", strconv.Itoa(len(content)))
  67. }
  68. // Write status code if it has been set manually
  69. // Set it to 0 afterwards to prevent "multiple response.WriteHeader calls"
  70. if output.Status != 0 {
  71. output.Context.ResponseWriter.WriteHeader(output.Status)
  72. output.Status = 0
  73. } else {
  74. output.Context.ResponseWriter.Started = true
  75. }
  76. io.Copy(output.Context.ResponseWriter, buf)
  77. return nil
  78. }
  79. // Cookie sets cookie value via given key.
  80. // others are ordered as cookie's max age time, path,domain, secure and httponly.
  81. func (output *BeegoOutput) Cookie(name string, value string, others ...interface{}) {
  82. var b bytes.Buffer
  83. fmt.Fprintf(&b, "%s=%s", sanitizeName(name), sanitizeValue(value))
  84. //fix cookie not work in IE
  85. if len(others) > 0 {
  86. var maxAge int64
  87. switch v := others[0].(type) {
  88. case int:
  89. maxAge = int64(v)
  90. case int32:
  91. maxAge = int64(v)
  92. case int64:
  93. maxAge = v
  94. }
  95. switch {
  96. case maxAge > 0:
  97. fmt.Fprintf(&b, "; Expires=%s; Max-Age=%d", time.Now().Add(time.Duration(maxAge)*time.Second).UTC().Format(time.RFC1123), maxAge)
  98. case maxAge < 0:
  99. fmt.Fprintf(&b, "; Max-Age=0")
  100. }
  101. }
  102. // the settings below
  103. // Path, Domain, Secure, HttpOnly
  104. // can use nil skip set
  105. // default "/"
  106. if len(others) > 1 {
  107. if v, ok := others[1].(string); ok && len(v) > 0 {
  108. fmt.Fprintf(&b, "; Path=%s", sanitizeValue(v))
  109. }
  110. } else {
  111. fmt.Fprintf(&b, "; Path=%s", "/")
  112. }
  113. // default empty
  114. if len(others) > 2 {
  115. if v, ok := others[2].(string); ok && len(v) > 0 {
  116. fmt.Fprintf(&b, "; Domain=%s", sanitizeValue(v))
  117. }
  118. }
  119. // default empty
  120. if len(others) > 3 {
  121. var secure bool
  122. switch v := others[3].(type) {
  123. case bool:
  124. secure = v
  125. default:
  126. if others[3] != nil {
  127. secure = true
  128. }
  129. }
  130. if secure {
  131. fmt.Fprintf(&b, "; Secure")
  132. }
  133. }
  134. // default false. for session cookie default true
  135. if len(others) > 4 {
  136. if v, ok := others[4].(bool); ok && v {
  137. fmt.Fprintf(&b, "; HttpOnly")
  138. }
  139. }
  140. output.Context.ResponseWriter.Header().Add("Set-Cookie", b.String())
  141. }
  142. var cookieNameSanitizer = strings.NewReplacer("\n", "-", "\r", "-")
  143. func sanitizeName(n string) string {
  144. return cookieNameSanitizer.Replace(n)
  145. }
  146. var cookieValueSanitizer = strings.NewReplacer("\n", " ", "\r", " ", ";", " ")
  147. func sanitizeValue(v string) string {
  148. return cookieValueSanitizer.Replace(v)
  149. }
  150. func jsonRenderer(value interface{}) Renderer {
  151. return rendererFunc(func(ctx *Context) {
  152. ctx.Output.JSON(value, false, false)
  153. })
  154. }
  155. func errorRenderer(err error) Renderer {
  156. return rendererFunc(func(ctx *Context) {
  157. ctx.Output.SetStatus(500)
  158. ctx.Output.Body([]byte(err.Error()))
  159. })
  160. }
  161. // JSON writes json to response body.
  162. // if encoding is true, it converts utf-8 to \u0000 type.
  163. func (output *BeegoOutput) JSON(data interface{}, hasIndent bool, encoding bool) error {
  164. output.Header("Content-Type", "application/json; charset=utf-8")
  165. var content []byte
  166. var err error
  167. if hasIndent {
  168. content, err = json.MarshalIndent(data, "", " ")
  169. } else {
  170. content, err = json.Marshal(data)
  171. }
  172. if err != nil {
  173. http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  174. return err
  175. }
  176. if encoding {
  177. content = []byte(stringsToJSON(string(content)))
  178. }
  179. return output.Body(content)
  180. }
  181. // YAML writes yaml to response body.
  182. func (output *BeegoOutput) YAML(data interface{}) error {
  183. output.Header("Content-Type", "application/x-yaml; charset=utf-8")
  184. var content []byte
  185. var err error
  186. content, err = yaml.Marshal(data)
  187. if err != nil {
  188. http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  189. return err
  190. }
  191. return output.Body(content)
  192. }
  193. // JSONP writes jsonp to response body.
  194. func (output *BeegoOutput) JSONP(data interface{}, hasIndent bool) error {
  195. output.Header("Content-Type", "application/javascript; charset=utf-8")
  196. var content []byte
  197. var err error
  198. if hasIndent {
  199. content, err = json.MarshalIndent(data, "", " ")
  200. } else {
  201. content, err = json.Marshal(data)
  202. }
  203. if err != nil {
  204. http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  205. return err
  206. }
  207. callback := output.Context.Input.Query("callback")
  208. if callback == "" {
  209. return errors.New(`"callback" parameter required`)
  210. }
  211. callback = template.JSEscapeString(callback)
  212. callbackContent := bytes.NewBufferString(" if(window." + callback + ")" + callback)
  213. callbackContent.WriteString("(")
  214. callbackContent.Write(content)
  215. callbackContent.WriteString(");\r\n")
  216. return output.Body(callbackContent.Bytes())
  217. }
  218. // XML writes xml string to response body.
  219. func (output *BeegoOutput) XML(data interface{}, hasIndent bool) error {
  220. output.Header("Content-Type", "application/xml; charset=utf-8")
  221. var content []byte
  222. var err error
  223. if hasIndent {
  224. content, err = xml.MarshalIndent(data, "", " ")
  225. } else {
  226. content, err = xml.Marshal(data)
  227. }
  228. if err != nil {
  229. http.Error(output.Context.ResponseWriter, err.Error(), http.StatusInternalServerError)
  230. return err
  231. }
  232. return output.Body(content)
  233. }
  234. // ServeFormatted serve YAML, XML OR JSON, depending on the value of the Accept header
  235. func (output *BeegoOutput) ServeFormatted(data interface{}, hasIndent bool, hasEncode ...bool) {
  236. accept := output.Context.Input.Header("Accept")
  237. switch accept {
  238. case ApplicationYAML:
  239. output.YAML(data)
  240. case ApplicationXML, TextXML:
  241. output.XML(data, hasIndent)
  242. default:
  243. output.JSON(data, hasIndent, len(hasEncode) > 0 && hasEncode[0])
  244. }
  245. }
  246. // Download forces response for download file.
  247. // it prepares the download response header automatically.
  248. func (output *BeegoOutput) Download(file string, filename ...string) {
  249. // check get file error, file not found or other error.
  250. if _, err := os.Stat(file); err != nil {
  251. http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file)
  252. return
  253. }
  254. var fName string
  255. if len(filename) > 0 && filename[0] != "" {
  256. fName = filename[0]
  257. } else {
  258. fName = filepath.Base(file)
  259. }
  260. output.Header("Content-Disposition", "attachment; filename="+url.PathEscape(fName))
  261. output.Header("Content-Description", "File Transfer")
  262. output.Header("Content-Type", "application/octet-stream")
  263. output.Header("Content-Transfer-Encoding", "binary")
  264. output.Header("Expires", "0")
  265. output.Header("Cache-Control", "must-revalidate")
  266. output.Header("Pragma", "public")
  267. http.ServeFile(output.Context.ResponseWriter, output.Context.Request, file)
  268. }
  269. // ContentType sets the content type from ext string.
  270. // MIME type is given in mime package.
  271. func (output *BeegoOutput) ContentType(ext string) {
  272. if !strings.HasPrefix(ext, ".") {
  273. ext = "." + ext
  274. }
  275. ctype := mime.TypeByExtension(ext)
  276. if ctype != "" {
  277. output.Header("Content-Type", ctype)
  278. }
  279. }
  280. // SetStatus sets response status code.
  281. // It writes response header directly.
  282. func (output *BeegoOutput) SetStatus(status int) {
  283. output.Status = status
  284. }
  285. // IsCachable returns boolean of this request is cached.
  286. // HTTP 304 means cached.
  287. func (output *BeegoOutput) IsCachable() bool {
  288. return output.Status >= 200 && output.Status < 300 || output.Status == 304
  289. }
  290. // IsEmpty returns boolean of this request is empty.
  291. // HTTP 201,204 and 304 means empty.
  292. func (output *BeegoOutput) IsEmpty() bool {
  293. return output.Status == 201 || output.Status == 204 || output.Status == 304
  294. }
  295. // IsOk returns boolean of this request runs well.
  296. // HTTP 200 means ok.
  297. func (output *BeegoOutput) IsOk() bool {
  298. return output.Status == 200
  299. }
  300. // IsSuccessful returns boolean of this request runs successfully.
  301. // HTTP 2xx means ok.
  302. func (output *BeegoOutput) IsSuccessful() bool {
  303. return output.Status >= 200 && output.Status < 300
  304. }
  305. // IsRedirect returns boolean of this request is redirection header.
  306. // HTTP 301,302,307 means redirection.
  307. func (output *BeegoOutput) IsRedirect() bool {
  308. return output.Status == 301 || output.Status == 302 || output.Status == 303 || output.Status == 307
  309. }
  310. // IsForbidden returns boolean of this request is forbidden.
  311. // HTTP 403 means forbidden.
  312. func (output *BeegoOutput) IsForbidden() bool {
  313. return output.Status == 403
  314. }
  315. // IsNotFound returns boolean of this request is not found.
  316. // HTTP 404 means not found.
  317. func (output *BeegoOutput) IsNotFound() bool {
  318. return output.Status == 404
  319. }
  320. // IsClientError returns boolean of this request client sends error data.
  321. // HTTP 4xx means client error.
  322. func (output *BeegoOutput) IsClientError() bool {
  323. return output.Status >= 400 && output.Status < 500
  324. }
  325. // IsServerError returns boolean of this server handler errors.
  326. // HTTP 5xx means server internal error.
  327. func (output *BeegoOutput) IsServerError() bool {
  328. return output.Status >= 500 && output.Status < 600
  329. }
  330. func stringsToJSON(str string) string {
  331. var jsons bytes.Buffer
  332. for _, r := range str {
  333. rint := int(r)
  334. if rint < 128 {
  335. jsons.WriteRune(r)
  336. } else {
  337. jsons.WriteString("\\u")
  338. if rint < 0x100 {
  339. jsons.WriteString("00")
  340. } else if rint < 0x1000 {
  341. jsons.WriteString("0")
  342. }
  343. jsons.WriteString(strconv.FormatInt(int64(rint), 16))
  344. }
  345. }
  346. return jsons.String()
  347. }
  348. // Session sets session item value with given key.
  349. func (output *BeegoOutput) Session(name interface{}, value interface{}) {
  350. output.Context.Input.CruSession.Set(name, value)
  351. }