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 type CountableReader struct {
32 func (r *CountableReader) ReadByte() (b byte, err error) {
33 b, err = r.Reader.ReadByte()
40 func (r *CountableReader) Read(p []byte) (n int, err error) {
41 n, err = r.Reader.Read(p)
46 type GZIPReader struct {
52 func (r *GZIPReader) Read(p []byte) (n int, err error) {
56 func (r *GZIPReader) Close() error {
60 func (r *GZIPReader) Offsets() []Offset {
68 ) (*GZIPReader, error) {
70 for _, off := range offsets {
71 if uOffset < offU+off.U {
77 fd, err := os.Open(warcPath)
81 if _, err = fd.Seek(offZ, io.SeekStart); err != nil {
85 cr := &CountableReader{Reader: bufio.NewReader(fd)}
86 z, err := gzip.NewReader(cr)
92 gr := GZIPReader{r: r}
95 var offset, offsetPrev int64
97 written, err := io.Copy(w, z)
103 gr.offsets = append(gr.offsets, Offset{offset - offsetPrev, written})
110 w.CloseWithError(err)
115 w.CloseWithError(io.EOF)
117 _, err = io.CopyN(io.Discard, r, uOffset-offU)