Skip to content

Commit 46d3281

Browse files
committed
stream: add some constant and support Content-Type header
1 parent 02f0b98 commit 46d3281

File tree

1 file changed

+37
-1
lines changed

1 file changed

+37
-1
lines changed

stream.go

Lines changed: 37 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,42 @@ import (
1616
"golang.org/x/xerrors"
1717
)
1818

19+
const (
20+
// "Header Content-Length" is the HTTP header name of the length of the content part in bytes. This header is required.
21+
// This entity header indicates the size of the entity-body, in bytes, sent to the recipient.
22+
//
23+
// RFC 7230, section 3.3.2: Content-Length:
24+
// https://tools.ietf.org/html/rfc7230#section-3.3.2
25+
HeaderContentLength = "Content-Length"
26+
27+
// "HeaderContentType" is the mime type of the content part. Defaults to "application/vscode-jsonrpc; charset=utf-8".
28+
// This entity header is used to indicate the media type of the resource.
29+
//
30+
// RFC 7231, section 3.1.1.5: Content-Type:
31+
// https://tools.ietf.org/html/rfc7231#section-3.1.1.5
32+
HeaderContentType = "Content-Type"
33+
34+
// HeaderContentSeparator is the header and content part separator.
35+
HeaderContentSeparator = "\r\n"
36+
37+
headerSeparatorComma = ":"
38+
)
39+
40+
const (
41+
// ContentTypeJSONRPC is the custom mime type content for the Language Server Protocol.
42+
ContentTypeJSONRPC = "application/jsonrpc; charset=utf-8"
43+
44+
// ContentTypeVSCodeJSONRPC is the default mime type content for the Language Server Protocol Specification.
45+
ContentTypeVSCodeJSONRPC = "application/vscode-jsonrpc; charset=utf-8"
46+
)
47+
48+
const (
49+
// HeaderContentLengthFmt is the a format of "Content-Length" header for fmt function arg.
50+
HeaderContentLengthFmt = HeaderContentLength + headerSeparatorComma + " %d" + HeaderContentSeparator
51+
// HeaderContentTypeFmt is the a format of "Content-Type" header for fmt function arg.
52+
HeaderContentTypeFmt = HeaderContentType + headerSeparatorComma + " %s" + HeaderContentSeparator
53+
)
54+
1955
// Stream abstracts the transport mechanics from the JSON RPC protocol.
2056
type Stream interface {
2157
// Read gets the next message from the stream.
@@ -97,7 +133,7 @@ func (s *stream) Write(ctx context.Context, p []byte) (n int, err error) {
97133
}
98134

99135
s.Lock()
100-
n, err = fmt.Fprintf(s.out, "Content-Length: %v\r\n\r\n", len(p))
136+
n, err = fmt.Fprintf(s.out, HeaderContentLengthFmt+HeaderContentTypeFmt+HeaderContentSeparator, len(p), ContentTypeJSONRPC)
101137
if err == nil {
102138
n, err = s.out.Write(p)
103139
}

0 commit comments

Comments
 (0)