You can not select more than 25 topics Topics must start with a chinese character,a letter or number, can include dashes ('-') and can be up to 35 characters long.

response.go 1.2 kB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162
  1. // Copyright 2021 The Gitea Authors. All rights reserved.
  2. // Use of this source code is governed by a MIT-style
  3. // license that can be found in the LICENSE file.
  4. package context
  5. import "net/http"
  6. // ResponseWriter represents a response writer for HTTP
  7. type ResponseWriter interface {
  8. http.ResponseWriter
  9. Flush()
  10. Status() int
  11. }
  12. var (
  13. _ ResponseWriter = &Response{}
  14. )
  15. // Response represents a response
  16. type Response struct {
  17. http.ResponseWriter
  18. status int
  19. }
  20. // Write writes bytes to HTTP endpoint
  21. func (r *Response) Write(bs []byte) (int, error) {
  22. size, err := r.ResponseWriter.Write(bs)
  23. if err != nil {
  24. return 0, err
  25. }
  26. if r.status == 0 {
  27. r.WriteHeader(200)
  28. }
  29. return size, nil
  30. }
  31. // WriteHeader write status code
  32. func (r *Response) WriteHeader(statusCode int) {
  33. r.status = statusCode
  34. r.ResponseWriter.WriteHeader(statusCode)
  35. }
  36. // Flush flush cached data
  37. func (r *Response) Flush() {
  38. if f, ok := r.ResponseWriter.(http.Flusher); ok {
  39. f.Flush()
  40. }
  41. }
  42. // Status returned status code written
  43. func (r *Response) Status() int {
  44. return r.status
  45. }
  46. // NewResponse creates a response
  47. func NewResponse(resp http.ResponseWriter) *Response {
  48. if v, ok := resp.(*Response); ok {
  49. return v
  50. }
  51. return &Response{resp, 0}
  52. }