Debian Codegraph @

v2/content_type.go

raw
1 // Copyright 2022, Google Inc.
2 // All rights reserved.
3 //
4 // Redistribution and use in source and binary forms, with or without
5 // modification, are permitted provided that the following conditions are
6 // met:
7 //
8 // * Redistributions of source code must retain the above copyright
9 // notice, this list of conditions and the following disclaimer.
10 // * Redistributions in binary form must reproduce the above
11 // copyright notice, this list of conditions and the following disclaimer
12 // in the documentation and/or other materials provided with the
13 // distribution.
14 // * Neither the name of Google Inc. nor the names of its
15 // contributors may be used to endorse or promote products derived from
16 // this software without specific prior written permission.
17 //
18 // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19 // "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
20 // LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
21 // A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
22 // OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
23 // SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
24 // LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
25 // DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
26 // THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
27 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
28 // OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29
30 package gax
31
32 import (
33 "io"
34 "io/ioutil"
35 "net/http"
36 )
37
38 const sniffBuffSize = 512
39
40 func newContentSniffer(r io.Reader) *contentSniffer {
41 return &contentSniffer{r: r}
42 }
43
44 // contentSniffer wraps a Reader, and reports the content type determined by sniffing up to 512 bytes from the Reader.
45 type contentSniffer struct {
46 r io.Reader
47 start []byte // buffer for the sniffed bytes.
48 err error // set to any error encountered while reading bytes to be sniffed.
49
50 ctype string // set on first sniff.
51 sniffed bool // set to true on first sniff.
52 }
53
54 func (cs *contentSniffer) Read(p []byte) (n int, err error) {
55 // Ensure that the content type is sniffed before any data is consumed from Reader.
56 _, _ = cs.ContentType()
57
58 if len(cs.start) > 0 {
59 n := copy(p, cs.start)
60 cs.start = cs.start[n:]
61 return n, nil
62 }
63
64 // We may have read some bytes into start while sniffing, even if the read ended in an error.
65 // We should first return those bytes, then the error.
66 if cs.err != nil {
67 return 0, cs.err
68 }
69
70 // Now we have handled all bytes that were buffered while sniffing. Now just delegate to the underlying reader.
71 return cs.r.Read(p)
72 }
73
74 // ContentType returns the sniffed content type, and whether the content type was successfully sniffed.
75 func (cs *contentSniffer) ContentType() (string, bool) {
76 if cs.sniffed {
77 return cs.ctype, cs.ctype != ""
78 }
79 cs.sniffed = true
80 // If ReadAll hits EOF, it returns err==nil.
81 cs.start, cs.err = ioutil.ReadAll(io.LimitReader(cs.r, sniffBuffSize))
82
83 // Don't try to detect the content type based on possibly incomplete data.
84 if cs.err != nil {
85 return "", false
86 }
87
88 cs.ctype = http.DetectContentType(cs.start)
89 return cs.ctype, true
90 }
91
92 // DetermineContentType determines the content type of the supplied reader.
93 // The content of media will be sniffed to determine the content type.
94 // After calling DetectContentType the caller must not perform further reads on
95 // media, but rather read from the Reader that is returned.
96 func DetermineContentType(media io.Reader) (io.Reader, string) {
97 // For backwards compatibility, allow clients to set content
98 // type by providing a ContentTyper for media.
99 // Note: This is an anonymous interface definition copied from googleapi.ContentTyper.
100 if typer, ok := media.(interface {
101 ContentType() string
102 }); ok {
103 return media, typer.ContentType()
104 }
105
106 sniffer := newContentSniffer(media)
107 if ctype, ok := sniffer.ContentType(); ok {
108 return sniffer, ctype
109 }
110 // If content type could not be sniffed, reads from sniffer will eventually fail with an error.
111 return sniffer, ""
112 }