]> Sergey Matveev's repositories - tofuproxy.git/blob - warc/reader.go
Unify copyright comment format
[tofuproxy.git] / warc / reader.go
1 // tofuproxy -- flexible HTTP/HTTPS proxy, TLS terminator, X.509 TOFU
2 //              manager, WARC/geminispace browser
3 // Copyright (C) 2021-2024 Sergey Matveev <stargrave@stargrave.org>
4 //
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.
8 //
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.
13 //
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/>.
16
17 package warc
18
19 import (
20         "bufio"
21         "fmt"
22         "io"
23         "strconv"
24         "strings"
25 )
26
27 const CRLF = "\r\n"
28
29 type Reader struct {
30         Path    string
31         rrr     RawRecordReader
32         br      *bufio.Reader
33         offset  int64
34         prevRec *Record
35         offsets []Offset
36 }
37
38 func NewReader(warcPath string) (*Reader, error) {
39         rrr, err := Open(warcPath, nil, 0)
40         if err != nil {
41                 return nil, err
42         }
43         return &Reader{
44                 Path: warcPath,
45                 rrr:  rrr,
46                 br:   bufio.NewReader(rrr),
47         }, nil
48 }
49
50 func (r *Reader) next() error {
51         if r.prevRec == nil {
52                 return nil
53         }
54         if _, err := r.br.Discard(int(r.prevRec.Size)); err != nil {
55                 return err
56         }
57         r.offset += int64(r.prevRec.HdrLen) + r.prevRec.Size
58         for i := 0; i < 2; i++ {
59                 line, err := r.br.ReadString('\n')
60                 if err != nil {
61                         return err
62                 }
63                 r.offset += int64(len(line))
64                 if line != CRLF {
65                         return fmt.Errorf("non-CRLF: %q", line)
66                 }
67         }
68         return nil
69 }
70
71 func (r *Reader) ReadRecord() (*Record, io.Reader, error) {
72         r.next()
73         line, err := r.br.ReadString('\n')
74         if err != nil {
75                 return nil, nil, err
76         }
77         if !strings.HasPrefix(line, "WARC/") {
78                 return nil, nil, fmt.Errorf("non-WARC header: %q", line)
79         }
80         hdrLines := []string{line}
81         hdrLen := len(line)
82         hdr := NewHeader()
83         for {
84                 line, err := r.br.ReadString('\n')
85                 if err != nil {
86                         return nil, nil, err
87                 }
88                 hdrLen += len(line)
89                 if line == CRLF {
90                         break
91                 }
92                 hdrLines = append(hdrLines, line)
93                 hdr.AddLine(line)
94         }
95         size, err := strconv.ParseUint(hdr.Get("Content-Length"), 10, 64)
96         if err != nil {
97                 return nil, nil, err
98         }
99         rec := &Record{
100                 WARCPath: r.Path,
101                 Offset:   r.offset,
102                 Size:     int64(size),
103                 Hdr:      hdr,
104                 HdrLen:   hdrLen,
105                 HdrLines: hdrLines,
106         }
107         r.prevRec = rec
108         return rec, &io.LimitedReader{R: r.br, N: int64(size)}, nil
109 }
110
111 func (r *Reader) RecordWasRead() {
112         r.prevRec.HdrLen = 0
113         r.prevRec.Size = 0
114 }
115
116 func (r *Reader) Close() error {
117         err := r.rrr.Close()
118         r.offsets = r.rrr.Offsets()
119         return err
120 }