Skip to content
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

Fix the gRPC User-Agent header name #525

Merged
merged 1 commit into from
Mar 13, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
24 changes: 23 additions & 1 deletion packages/connect/src/protocol-grpc-web/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,26 @@
// See the License for the specific language governing permissions and
// limitations under the License.

export * from "../protocol-grpc/headers.js";
export {
headerContentType,
headerEncoding,
headerAcceptEncoding,
headerTimeout,
headerGrpcStatus,
headerGrpcMessage,
headerStatusDetailsBin,
} from "../protocol-grpc/headers.js";

/**
* gRPC-web does not use the standard header User-Agent.
*/
export const headerXUserAgent = "X-User-Agent";

/**
* The canonical grpc/grpc-web JavaScript implementation sets
* this request header with value "1".
* Some servers may rely on the header to identify gRPC-web
* requests. For example the proxy by improbable:
* https://github.com/improbable-eng/grpc-web/blob/53aaf4cdc0fede7103c1b06f0cfc560c003a5c41/go/grpcweb/wrapper.go#L231
*/
export const headerXGrpcWeb = "X-Grpc-Web";
10 changes: 4 additions & 6 deletions packages/connect/src/protocol-grpc-web/request-header.ts
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ import {
headerContentType,
headerEncoding,
headerTimeout,
headerUserAgent,
headerXUserAgent,
headerXGrpcWeb,
} from "./headers.js";
import { contentTypeJson, contentTypeProto } from "./content-type.js";

Expand All @@ -37,14 +38,11 @@ export function requestHeader(
headerContentType,
useBinaryFormat ? contentTypeProto : contentTypeJson
);
// Some servers may rely on the request header `X-Grpc-Web` to identify
// gRPC-web requests. For example the proxy by improbable:
// https://github.com/improbable-eng/grpc-web/blob/53aaf4cdc0fede7103c1b06f0cfc560c003a5c41/go/grpcweb/wrapper.go#L231
result.set("X-Grpc-Web", "1");
result.set(headerXGrpcWeb, "1");
// Note that we do not comply with recommended structure for the
// user-agent string.
// https://github.com/grpc/grpc/blob/c462bb8d485fc1434ecfae438823ca8d14cf3154/doc/PROTOCOL-HTTP2.md#user-agents
result.set(headerUserAgent, "@bufbuild/connect-web");
result.set(headerXUserAgent, "@bufbuild/connect-web");
if (timeoutMs !== undefined) {
result.set(headerTimeout, `${timeoutMs}m`);
}
Expand Down
3 changes: 2 additions & 1 deletion packages/connect/src/protocol-grpc/headers.ts
Original file line number Diff line number Diff line change
Expand Up @@ -19,4 +19,5 @@ export const headerTimeout = "Grpc-Timeout";
export const headerGrpcStatus = "Grpc-Status";
export const headerGrpcMessage = "Grpc-Message";
export const headerStatusDetailsBin = "Grpc-Status-Details-Bin";
export const headerUserAgent = "X-User-Agent";
export const headerUserAgent = "User-Agent";
export const headerMessageType = "Grpc-Message-Type";
8 changes: 4 additions & 4 deletions packages/connect/src/protocol-grpc/request-header.spec.ts
Original file line number Diff line number Diff line change
Expand Up @@ -31,10 +31,10 @@ describe("requestHeader", () => {
expect(listHeaderKeys(headers)).toEqual([
"content-type",
"te",
"x-user-agent",
"user-agent",
]);
expect(headers.get("Content-Type")).toBe("application/grpc+proto");
expect(headers.get("X-User-Agent")).toBe("@bufbuild/connect-web");
expect(headers.get("User-Agent")).toBe("@bufbuild/connect-web");
});

it("should create request headers with timeout", () => {
Expand All @@ -43,7 +43,7 @@ describe("requestHeader", () => {
"content-type",
"grpc-timeout",
"te",
"x-user-agent",
"user-agent",
]);
expect(headers.get("Grpc-Timeout")).toBe("10m");
});
Expand All @@ -67,7 +67,7 @@ describe("requestHeader", () => {
"grpc-accept-encoding",
"grpc-encoding",
"te",
"x-user-agent",
"user-agent",
]);
expect(headers.get(headerEncoding)).toBe(compressionMock.name);
expect(headers.get(headerAcceptEncoding)).toBe(compressionMock.name);
Expand Down