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