2 tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
3 manager, WARC/geminispace browser
4 Copyright (C) 2021-2023 Sergey Matveev <stargrave@stargrave.org>
6 This program is free software: you can redistribute it and/or modify
7 it under the terms of the GNU General Public License as published by
8 the Free Software Foundation, version 3 of the License.
10 This program is distributed in the hope that it will be useful,
11 but WITHOUT ANY WARRANTY; without even the implied warranty of
12 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 GNU General Public License for more details.
15 You should have received a copy of the GNU General Public License
16 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 type CountableReader struct {
33 func (r *CountableReader) ReadByte() (b byte, err error) {
34 b, err = r.Reader.ReadByte()
41 func (r *CountableReader) Read(p []byte) (n int, err error) {
42 n, err = r.Reader.Read(p)
47 type GZIPReader struct {
53 func (r *GZIPReader) Read(p []byte) (n int, err error) {
57 func (r *GZIPReader) Close() error {
61 func (r *GZIPReader) Offsets() []Offset {
69 ) (*GZIPReader, error) {
71 for _, off := range offsets {
72 if uOffset < offU+off.U {
78 fd, err := os.Open(warcPath)
82 if _, err = fd.Seek(offZ, io.SeekStart); err != nil {
86 cr := &CountableReader{Reader: bufio.NewReader(fd)}
87 z, err := gzip.NewReader(cr)
93 gr := GZIPReader{r: r}
96 var offset, offsetPrev int64
98 written, err := io.Copy(w, z)
100 w.CloseWithError(err)
104 gr.offsets = append(gr.offsets, Offset{offset - offsetPrev, written})
111 w.CloseWithError(err)
116 w.CloseWithError(io.EOF)
118 _, err = io.CopyN(io.Discard, r, uOffset-offU)