2 tofuproxy -- flexible HTTP/WARC proxy with TLS certificates management
3 Copyright (C) 2021 Sergey Matveev <stargrave@stargrave.org>
5 This program is free software: you can redistribute it and/or modify
6 it under the terms of the GNU General Public License as published by
7 the Free Software Foundation, version 3 of the License.
9 This program is distributed in the hope that it will be useful,
10 but WITHOUT ANY WARRANTY; without even the implied warranty of
11 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 GNU General Public License for more details.
14 You should have received a copy of the GNU General Public License
15 along with this program. If not, see <http://www.gnu.org/licenses/>.
27 "github.com/klauspost/compress/zstd"
30 type Compressed struct {
36 func (c *Compressed) Read(p []byte) (int, error) {
42 func (c *Compressed) Close() error {
47 func (c *Compressed) Seek(offset int64, whence int) (int64, error) {
48 if whence != io.SeekStart {
49 panic("can only seek from the start")
51 if _, err := io.CopyN(io.Discard, c, offset-c.offset); err != nil {
58 func Open(warcPath string) (io.ReadSeekCloser, error) {
59 ext := path.Ext(warcPath)
62 return os.Open(warcPath)
64 fd, err := os.Open(warcPath)
68 gzr, err := gzip.NewReader(fd)
73 return &Compressed{r: gzr, fd: fd}, nil
75 fd, err := os.Open(warcPath)
79 zstdr, err := zstd.NewReader(fd)
83 return &Compressed{r: zstdr.IOReadCloser(), fd: fd}, nil
85 return nil, fmt.Errorf("unknown extensions: %s", ext)