2 tofuproxy -- flexible HTTP proxy, TLS terminator, X.509 certificates
3 manager, WARC/Gemini browser
4 Copyright (C) 2021 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/>.
35 Continuations []*Record
38 func (rec *Record) URI() string {
39 return strings.Trim(rec.Hdr.Get("WARC-Target-URI"), "<>")
42 func (rec *Record) TotalSize() int64 {
44 for _, r := range rec.Continuations {
50 type SelfRecordReader struct {
55 func (srr *SelfRecordReader) Read(p []byte) (n int, err error) {
56 n, err = srr.rrr.Read(p)
63 func (srr *SelfRecordReader) Close() error {
64 return srr.rrr.Close()
67 func (rec *Record) selfReader(noHdr bool, offsets []Offset) (*SelfRecordReader, error) {
70 offset += int64(rec.HdrLen)
72 rrr, err := Open(rec.WARCPath, offsets, offset)
76 return &SelfRecordReader{lr: &io.LimitedReader{R: rrr, N: rec.Size}, rrr: rrr}, nil
79 type RecordReader struct {
81 srrs []*SelfRecordReader
84 func (rec *Record) Reader(
86 warcOffsets map[string][]Offset,
87 ) (*RecordReader, error) {
88 srrs := make([]*SelfRecordReader, 0, 1+len(rec.Continuations))
89 rs := make([]io.Reader, 0, 1+len(rec.Continuations))
90 for i, r := range append([]*Record{rec}, rec.Continuations...) {
94 srr, err := r.selfReader(noHdr, warcOffsets[rec.WARCPath])
96 for _, srr := range srrs {
101 srrs = append(srrs, srr)
104 return &RecordReader{r: io.MultiReader(rs...), srrs: srrs}, nil
107 func (rr *RecordReader) Read(p []byte) (n int, err error) {
108 n, err = rr.r.Read(p)
115 func (rr *RecordReader) Close() error {
116 for _, srr := range rr.srrs {