|
| 1 | +// Copyright 2015 Richard Lehane. All rights reserved. |
| 2 | +// |
| 3 | +// Licensed under the Apache License, Version 2.0 (the "License"); |
| 4 | +// you may not use this file except in compliance with the License. |
| 5 | +// You may obtain a copy of the License at |
| 6 | +// |
| 7 | +// http://www.apache.org/licenses/LICENSE-2.0 |
| 8 | +// |
| 9 | +// Unless required by applicable law or agreed to in writing, software |
| 10 | +// distributed under the License is distributed on an "AS IS" BASIS, |
| 11 | +// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 12 | +// See the License for the specific language governing permissions and |
| 13 | +// limitations under the License. |
| 14 | + |
| 15 | +// ransfer-Encoding The form of encoding used to safely transfer the entity to the user. Currently defined methods are: chunked, compress, deflate, gzip, identity. |
| 16 | + |
| 17 | +/* |
| 18 | +The official list of tokens available to servers and client is maintained by IANA,[4] and it includes: |
| 19 | +
|
| 20 | + compress – UNIX "compress" program method (historic; deprecated in most applications and replaced by gzip or deflate) |
| 21 | + *deflate – compression based on the deflate algorithm (described in RFC 1951), wrapped inside the zlib data format (RFC 1950); |
| 22 | + exi – W3C Efficient XML Interchange |
| 23 | + *gzip – GNU zip format (described in RFC 1952). This method is the most broadly supported as of March 2011.[5] |
| 24 | + *identity – No transformation is used. This is the default value for content coding. |
| 25 | + pack200-gzip – Network Transfer Format for Java Archives[6] |
| 26 | +
|
| 27 | +*/ |
| 28 | +package webarchive |
| 29 | + |
| 30 | +import ( |
| 31 | + "compress/gzip" |
| 32 | + "compress/zlib" |
| 33 | + "io" |
| 34 | + "net/http/httputil" |
| 35 | +) |
| 36 | + |
| 37 | +func isgzip(buf []byte) bool { |
| 38 | + if buf[0] != 0x1f || buf[1] != 0x8b || buf[2] != 8 { |
| 39 | + return false |
| 40 | + } |
| 41 | + return true |
| 42 | +} |
| 43 | + |
| 44 | +const zlibDeflate = 8 |
| 45 | + |
| 46 | +func iszlib(buf []byte) bool { |
| 47 | + h := uint(buf[0])<<8 | uint(buf[1]) |
| 48 | + if (buf[0]&0x0f != zlibDeflate) || (h%31 != 0) { |
| 49 | + return false |
| 50 | + } |
| 51 | + return true |
| 52 | +} |
| 53 | + |
| 54 | +func ischunk(buf []byte) bool { |
| 55 | + for i, c := range buf { |
| 56 | + switch { |
| 57 | + case '0' >= c && c <= '9': |
| 58 | + continue |
| 59 | + case 'a' <= c && c <= 'f': |
| 60 | + continue |
| 61 | + case 'A' <= c && c <= 'F': |
| 62 | + continue |
| 63 | + case c == '\r': |
| 64 | + if i > 0 && i < len(buf)-1 && buf[i+1] == '\n' { |
| 65 | + return true |
| 66 | + } |
| 67 | + return false |
| 68 | + default: |
| 69 | + return false |
| 70 | + } |
| 71 | + } |
| 72 | + return false |
| 73 | +} |
| 74 | + |
| 75 | +type payloadDecoder struct { |
| 76 | + Record |
| 77 | + rdr io.Reader |
| 78 | +} |
| 79 | + |
| 80 | +func (pd *payloadDecoder) Read(b []byte) (int, error) { |
| 81 | + return pd.rdr.Read(b) |
| 82 | +} |
| 83 | + |
| 84 | +func (pd *payloadDecoder) IsSlicer() bool { |
| 85 | + return false |
| 86 | +} |
| 87 | + |
| 88 | +func newDecoder(rec Record, encodings []string) Record { |
| 89 | + if len(encodings) == 0 { |
| 90 | + return rec |
| 91 | + } |
| 92 | + pd := &payloadDecoder{Record: rec, rdr: rec} |
| 93 | + for i, v := range encodings { |
| 94 | + switch v { |
| 95 | + case "chunked": |
| 96 | + if i == 0 { |
| 97 | + if peek, err := rec.peek(10); err != nil || !ischunk(peek) { |
| 98 | + return rec |
| 99 | + } |
| 100 | + } |
| 101 | + pd.rdr = httputil.NewChunkedReader(pd.rdr) |
| 102 | + case "deflate": |
| 103 | + if i == 0 { |
| 104 | + if peek, err := rec.peek(2); err != nil || !iszlib(peek) { |
| 105 | + return rec |
| 106 | + } |
| 107 | + } |
| 108 | + rdr, err := zlib.NewReader(pd.rdr) |
| 109 | + if err == nil { |
| 110 | + pd.rdr = rdr |
| 111 | + } |
| 112 | + case "gzip": |
| 113 | + if i == 0 { |
| 114 | + if peek, err := rec.peek(3); err != nil || !isgzip(peek) { |
| 115 | + return rec |
| 116 | + } |
| 117 | + } |
| 118 | + rdr, err := gzip.NewReader(pd.rdr) |
| 119 | + if err == nil { |
| 120 | + pd.rdr = rdr |
| 121 | + } |
| 122 | + } |
| 123 | + } |
| 124 | + return pd |
| 125 | +} |
| 126 | + |
| 127 | +// DecodePayload decodes any transfer or content encodings declared in the HTTP headers |
| 128 | +// of a record. Decodes chunked, deflate and gzip encodings. |
| 129 | +func DecodePayload(r Record) Record { |
| 130 | + return newDecoder(r, r.encodings()) |
| 131 | +} |
| 132 | + |
| 133 | +// DecodePayload decodes any transfer or content encodings declared in the HTTP headers |
| 134 | +// of a record. Decodes chunked, deflate and gzip encodings. |
| 135 | +func TransferDecodePayload(r Record) Record { |
| 136 | + return newDecoder(r, r.transferEncodings()) |
| 137 | +} |
0 commit comments