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/>.
32 Continuations []*Record
35 func (rec *Record) URI() string {
36 return strings.Trim(rec.Hdr.Get("WARC-Target-URI"), "<>")
39 func (rec *Record) TotalSize() int64 {
41 for _, r := range rec.Continuations {
47 type SelfRecordReader struct {
52 func (srr *SelfRecordReader) Read(p []byte) (n int, err error) {
53 n, err = srr.r.Read(p)
60 func (srr *SelfRecordReader) Close() error {
61 return srr.rsc.Close()
64 func (rec *Record) selfReader(noHdr bool) (*SelfRecordReader, error) {
65 rsc, err := Open(rec.WARCPath)
71 offset += int64(rec.HdrLen)
73 if _, err = rsc.Seek(offset, io.SeekStart); err != nil {
77 return &SelfRecordReader{r: &io.LimitedReader{R: rsc, N: rec.Size}, rsc: rsc}, nil
80 type RecordReader struct {
82 srrs []*SelfRecordReader
85 func (rec *Record) Reader(noHdr bool) (*RecordReader, error) {
86 srrs := make([]*SelfRecordReader, 0, 1+len(rec.Continuations))
87 rs := make([]io.Reader, 0, 1+len(rec.Continuations))
88 for i, r := range append([]*Record{rec}, rec.Continuations...) {
92 srr, err := r.selfReader(noHdr)
94 for _, srr := range srrs {
99 srrs = append(srrs, srr)
102 return &RecordReader{r: io.MultiReader(rs...), srrs: srrs}, nil
105 func (rr *RecordReader) Read(p []byte) (n int, err error) {
106 n, err = rr.r.Read(p)
113 func (rr *RecordReader) Close() error {
114 for _, srr := range rr.srrs {