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/>.
34 Continuations []*Record
37 func (rec *Record) URI() string {
38 return strings.Trim(rec.Hdr.Get("WARC-Target-URI"), "<>")
41 func (rec *Record) TotalSize() int64 {
43 for _, r := range rec.Continuations {
49 type SelfRecordReader struct {
54 func (srr *SelfRecordReader) Read(p []byte) (n int, err error) {
55 n, err = srr.rrr.Read(p)
62 func (srr *SelfRecordReader) Close() error {
63 return srr.rrr.Close()
66 func (rec *Record) selfReader(noHdr bool, offsets []Offset) (*SelfRecordReader, error) {
69 offset += int64(rec.HdrLen)
71 rrr, err := Open(rec.WARCPath, offsets, offset)
75 return &SelfRecordReader{lr: &io.LimitedReader{R: rrr, N: rec.Size}, rrr: rrr}, nil
78 type RecordReader struct {
80 srrs []*SelfRecordReader
83 func (rec *Record) Reader(
85 warcOffsets map[string][]Offset,
86 ) (*RecordReader, error) {
87 srrs := make([]*SelfRecordReader, 0, 1+len(rec.Continuations))
88 rs := make([]io.Reader, 0, 1+len(rec.Continuations))
89 for i, r := range append([]*Record{rec}, rec.Continuations...) {
93 srr, err := r.selfReader(noHdr, warcOffsets[rec.WARCPath])
95 for _, srr := range srrs {
100 srrs = append(srrs, srr)
103 return &RecordReader{r: io.MultiReader(rs...), srrs: srrs}, nil
106 func (rr *RecordReader) Read(p []byte) (n int, err error) {
107 n, err = rr.r.Read(p)
114 func (rr *RecordReader) Close() error {
115 for _, srr := range rr.srrs {