-
Notifications
You must be signed in to change notification settings - Fork 1.5k
Expand file tree
/
Copy pathframe.go
More file actions
92 lines (84 loc) · 2.25 KB
/
Copy pathframe.go
File metadata and controls
92 lines (84 loc) · 2.25 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
package jsonrpc2
import (
"bufio"
"fmt"
"io"
"math"
"strconv"
"strings"
)
// headerReader reads Content-Length delimited JSON-RPC frames from a stream.
type headerReader struct {
in *bufio.Reader
}
func newHeaderReader(r io.Reader) *headerReader {
return &headerReader{in: bufio.NewReader(r)}
}
// Read reads the next complete frame from the stream. It returns io.EOF on a
// clean end-of-stream (no partial data) and io.ErrUnexpectedEOF if the stream
// was interrupted mid-header.
func (r *headerReader) Read() ([]byte, error) {
firstRead := true
var contentLength int64
// Read headers, stop on the first blank line.
for {
line, err := r.in.ReadString('\n')
if err != nil {
if err == io.EOF {
if firstRead && line == "" {
return nil, io.EOF // clean EOF
}
err = io.ErrUnexpectedEOF
}
return nil, fmt.Errorf("failed reading header line: %w", err)
}
firstRead = false
line = strings.TrimSpace(line)
if line == "" {
break
}
colon := strings.IndexRune(line, ':')
if colon < 0 {
return nil, fmt.Errorf("invalid header line %q", line)
}
name, value := line[:colon], strings.TrimSpace(line[colon+1:])
switch name {
case "Content-Length":
contentLength, err = strconv.ParseInt(value, 10, 64)
if err != nil {
return nil, fmt.Errorf("failed parsing Content-Length: %v", value)
}
if contentLength <= 0 {
return nil, fmt.Errorf("invalid Content-Length: %v", contentLength)
}
default:
// ignoring unknown headers
}
}
if contentLength == 0 {
return nil, fmt.Errorf("missing Content-Length header")
}
if contentLength > math.MaxInt {
return nil, fmt.Errorf("Content-Length too large: %d", contentLength)
}
data := make([]byte, contentLength)
if _, err := io.ReadFull(r.in, data); err != nil {
return nil, err
}
return data, nil
}
// headerWriter writes Content-Length delimited JSON-RPC frames to a stream.
type headerWriter struct {
out io.Writer
}
func newHeaderWriter(w io.Writer) *headerWriter {
return &headerWriter{out: w}
}
// Write sends a single frame with a Content-Length header.
func (w *headerWriter) Write(data []byte) error {
if _, err := fmt.Fprintf(w.out, "Content-Length: %d\r\n\r\n", len(data)); err != nil {
return err
}
_, err := w.out.Write(data)
return err
}