WriteResponse writes response body, transparently compressing if necessary.
(w http.ResponseWriter, r *http.Request, b []byte)
| 591 | |
| 592 | // WriteResponse writes response body, transparently compressing if necessary. |
| 593 | func WriteResponse(w http.ResponseWriter, r *http.Request, b []byte) (int, error) { |
| 594 | var out io.Writer = w |
| 595 | |
| 596 | if strings.Contains(r.Header.Get("Accept-Encoding"), "gzip") { |
| 597 | w.Header().Set("Content-Encoding", "gzip") |
| 598 | gzw := builtinGzip.NewWriter(w) |
| 599 | defer gzw.Close() |
| 600 | out = gzw |
| 601 | } |
| 602 | |
| 603 | bytesWritten, err := out.Write(b) |
| 604 | if err != nil { |
| 605 | return 0, err |
| 606 | } |
| 607 | w.Header().Set("Content-Length", strconv.FormatInt(int64(bytesWritten), 10)) |
| 608 | return bytesWritten, nil |
| 609 | } |
| 610 | |
| 611 | // Min returns the minimum of the two given numbers. |
| 612 | func Min(a, b uint64) uint64 { |
no test coverage detected