]> Sergey Matveev's repositories - tofuproxy.git/blob - warc/reader.go
WARC
[tofuproxy.git] / warc / reader.go
1 /*
2 tofuproxy -- flexible HTTP/WARC proxy with TLS certificates management
3 Copyright (C) 2021 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
18 package warc
19
20 import (
21         "bufio"
22         "fmt"
23         "io"
24         "strconv"
25         "strings"
26 )
27
28 const CRLF = "\r\n"
29
30 type Reader struct {
31         Path    string
32         r       *bufio.Reader
33         rsc     io.ReadSeekCloser
34         offset  int64
35         prevRec *Record
36 }
37
38 func NewReader(warcPath string) (*Reader, error) {
39         rsc, err := Open(warcPath)
40         if err != nil {
41                 return nil, err
42         }
43         return &Reader{
44                 Path: warcPath,
45                 rsc:  rsc,
46                 r:    bufio.NewReader(rsc),
47         }, nil
48 }
49
50 func (r *Reader) next() error {
51         if r.prevRec == nil {
52                 return nil
53         }
54         if _, err := r.r.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.r.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, error) {
72         r.next()
73         line, err := r.r.ReadString('\n')
74         if err != nil {
75                 return nil, err
76         }
77         if !strings.HasPrefix(line, "WARC/") {
78                 return nil, fmt.Errorf("non-WARC header: %q", line)
79         }
80         hdrLen := len(line)
81         hdr := NewHeader()
82         for {
83                 line, err := r.r.ReadString('\n')
84                 if err != nil {
85                         return nil, err
86                 }
87                 hdrLen += len(line)
88                 if line == CRLF {
89                         break
90                 }
91                 hdr.AddLine(line)
92         }
93         size, err := strconv.ParseUint(hdr.Get("Content-Length"), 10, 64)
94         if err != nil {
95                 return nil, err
96         }
97         rec := &Record{
98                 WARCPath: r.Path,
99                 Offset:   r.offset,
100                 Hdr:      hdr,
101                 HdrLen:   hdrLen,
102                 Size:     int64(size),
103         }
104         r.prevRec = rec
105         return rec, nil
106 }
107
108 func (r *Reader) Close() error {
109         return r.rsc.Close()
110 }