OpenCores
URL https://opencores.org/ocsvn/openrisc/openrisc/trunk

Subversion Repositories openrisc

[/] [openrisc/] [trunk/] [gnu-dev/] [or1k-gcc/] [libgo/] [go/] [net/] [http/] [sniff.go] - Blame information for rev 747

Details | Compare with Previous | View Log

Line No. Rev Author Line
1 747 jeremybenn
// Copyright 2011 The Go Authors.  All rights reserved.
2
// Use of this source code is governed by a BSD-style
3
// license that can be found in the LICENSE file.
4
 
5
package http
6
 
7
import (
8
        "bytes"
9
        "encoding/binary"
10
)
11
 
12
// Content-type sniffing algorithm.
13
// References in this file refer to this draft specification:
14
//   http://mimesniff.spec.whatwg.org/
15
 
16
// The algorithm prefers to use sniffLen bytes to make its decision.
17
const sniffLen = 512
18
 
19
// DetectContentType returns the sniffed Content-Type string
20
// for the given data. This function always returns a valid MIME type.
21
func DetectContentType(data []byte) string {
22
        if len(data) > sniffLen {
23
                data = data[:sniffLen]
24
        }
25
 
26
        // Index of the first non-whitespace byte in data.
27
        firstNonWS := 0
28
        for ; firstNonWS < len(data) && isWS(data[firstNonWS]); firstNonWS++ {
29
        }
30
 
31
        for _, sig := range sniffSignatures {
32
                if ct := sig.match(data, firstNonWS); ct != "" {
33
                        return ct
34
                }
35
        }
36
 
37
        return "application/octet-stream" // fallback
38
}
39
 
40
func isWS(b byte) bool {
41
        return bytes.IndexByte([]byte("\t\n\x0C\r "), b) != -1
42
}
43
 
44
type sniffSig interface {
45
        // match returns the MIME type of the data, or "" if unknown.
46
        match(data []byte, firstNonWS int) string
47
}
48
 
49
// Data matching the table in section 6.
50
var sniffSignatures = []sniffSig{
51
        htmlSig("
52
        htmlSig("
53
        htmlSig("
54
        htmlSig("
55
        htmlSig("
56
        htmlSig("
57
        htmlSig("
58
        htmlSig("
59
        htmlSig("
60
        htmlSig("
61
        htmlSig("
62
        htmlSig("
63
        htmlSig("
64
        htmlSig("
65
        htmlSig("
66
        htmlSig("
67
        htmlSig("