Write writes to the response. If the response qualifies, it is encoded using the encoder, which is initialized if not done so already.
(p []byte)
| 319 | // it is encoded using the encoder, which is initialized |
| 320 | // if not done so already. |
| 321 | func (rw *responseWriter) Write(p []byte) (int, error) { |
| 322 | // ignore zero data writes, probably head request |
| 323 | if len(p) == 0 { |
| 324 | return 0, nil |
| 325 | } |
| 326 | |
| 327 | // WriteHeader wasn't called and is a CONNECT request, treat it as a success. |
| 328 | // otherwise, determine if the response should be compressed. |
| 329 | if rw.isConnect && !rw.wroteHeader && rw.statusCode == 0 { |
| 330 | rw.WriteHeader(http.StatusOK) |
| 331 | } |
| 332 | |
| 333 | // sniff content-type and determine content-length |
| 334 | if !rw.wroteHeader && rw.config.MinLength > 0 { |
| 335 | var gtMinLength bool |
| 336 | if len(p) > rw.config.MinLength { |
| 337 | gtMinLength = true |
| 338 | } else if cl, err := strconv.Atoi(rw.Header().Get("Content-Length")); err == nil && cl > rw.config.MinLength { |
| 339 | gtMinLength = true |
| 340 | } |
| 341 | |
| 342 | if gtMinLength { |
| 343 | if rw.Header().Get("Content-Type") == "" { |
| 344 | rw.Header().Set("Content-Type", http.DetectContentType(p)) |
| 345 | } |
| 346 | rw.init() |
| 347 | } |
| 348 | } |
| 349 | |
| 350 | // before we write to the response, we need to make |
| 351 | // sure the header is written exactly once; we do |
| 352 | // that by checking if a status code has been set, |
| 353 | // and if so, that means we haven't written the |
| 354 | // header OR the default status code will be written |
| 355 | // by the standard library |
| 356 | if !rw.wroteHeader { |
| 357 | if rw.statusCode != 0 { |
| 358 | rw.ResponseWriter.WriteHeader(rw.statusCode) |
| 359 | } |
| 360 | rw.wroteHeader = true |
| 361 | } |
| 362 | |
| 363 | if rw.w != nil { |
| 364 | return rw.w.Write(p) |
| 365 | } else { |
| 366 | return rw.ResponseWriter.Write(p) |
| 367 | } |
| 368 | } |
| 369 | |
| 370 | // used to mask ReadFrom method |
| 371 | type writerOnly struct { |
no test coverage detected