mirror of
https://github.com/golang/go
synced 2024-11-19 15:54:46 -07:00
net/http: document use of DetectContentType
Fixes #2365. R=golang-dev, dsymonds CC=golang-dev https://golang.org/cl/5653070
This commit is contained in:
parent
b5d81e5ed5
commit
d9da346078
@ -59,7 +59,9 @@ type ResponseWriter interface {
|
|||||||
|
|
||||||
// Write writes the data to the connection as part of an HTTP reply.
|
// Write writes the data to the connection as part of an HTTP reply.
|
||||||
// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)
|
// If WriteHeader has not yet been called, Write calls WriteHeader(http.StatusOK)
|
||||||
// before writing the data.
|
// before writing the data. If the Header does not contain a
|
||||||
|
// Content-Type line, Write adds a Content-Type set to the result of passing
|
||||||
|
// the initial 512 bytes of written data to DetectContentType.
|
||||||
Write([]byte) (int, error)
|
Write([]byte) (int, error)
|
||||||
|
|
||||||
// WriteHeader sends an HTTP response header with status code.
|
// WriteHeader sends an HTTP response header with status code.
|
||||||
|
@ -9,15 +9,15 @@ import (
|
|||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Content-type sniffing algorithm.
|
// The algorithm uses at most sniffLen bytes to make its decision.
|
||||||
// References in this file refer to this draft specification:
|
|
||||||
// http://mimesniff.spec.whatwg.org/
|
|
||||||
|
|
||||||
// The algorithm prefers to use sniffLen bytes to make its decision.
|
|
||||||
const sniffLen = 512
|
const sniffLen = 512
|
||||||
|
|
||||||
// DetectContentType returns the sniffed Content-Type string
|
// DetectContentType implements the algorithm described
|
||||||
// for the given data. This function always returns a valid MIME type.
|
// at http://mimesniff.spec.whatwg.org/ to determine the
|
||||||
|
// Content-Type of the given data. It considers at most the
|
||||||
|
// first 512 bytes of data. DetectContentType always returns
|
||||||
|
// a valid MIME type: if it cannot determine a more specific one, it
|
||||||
|
// returns "application/octet-stream".
|
||||||
func DetectContentType(data []byte) string {
|
func DetectContentType(data []byte) string {
|
||||||
if len(data) > sniffLen {
|
if len(data) > sniffLen {
|
||||||
data = data[:sniffLen]
|
data = data[:sniffLen]
|
||||||
|
Loading…
Reference in New Issue
Block a user