-
Notifications
You must be signed in to change notification settings - Fork 1.1k
[go] jsonrpc2 cleanup #949
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
Merged
Changes from 1 commit
Commits
Show all changes
4 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,88 @@ | ||
| package jsonrpc2 | ||
|
|
||
| import ( | ||
| "bufio" | ||
| "fmt" | ||
| "io" | ||
| "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") | ||
| } | ||
| 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 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 | ||
| } | ||
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.