Skip to content

Commit

Permalink
Add httpVersion and protocol fields to KibanaRequest (#183725)
Browse files Browse the repository at this point in the history
## Summary

Part of #7104

Prepare the work for `http2` support by introducing the `httpVersion`
and `protocol` fields to the `KibanaRequest` type and implementation.

Proper handling of h2 protocol for those fields will be added in the PR
implementing http2 (#183465)
  • Loading branch information
pgayvallet authored May 21, 2024
1 parent ad03dfb commit 3290d39
Show file tree
Hide file tree
Showing 7 changed files with 91 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -201,6 +201,36 @@ describe('CoreKibanaRequest', () => {
});
});

describe('route.httpVersion property', () => {
it('returns the version from the raw request', () => {
const request = hapiMocks.createRequest({
raw: {
req: {
httpVersion: '7.4',
},
},
});
const kibanaRequest = CoreKibanaRequest.from(request);

expect(kibanaRequest.httpVersion).toEqual('7.4');
});
});

describe('route.protocol property', () => {
it('return a static value for now as only http1 is supported', () => {
const request = hapiMocks.createRequest({
raw: {
req: {
httpVersion: '2.0',
},
},
});
const kibanaRequest = CoreKibanaRequest.from(request);

expect(kibanaRequest.protocol).toEqual('http1');
});
});

describe('route.options.authRequired property', () => {
it('handles required auth: undefined', () => {
const auth: RouteOptions['auth'] = undefined;
Expand Down Expand Up @@ -370,6 +400,17 @@ describe('CoreKibanaRequest', () => {
});
});

describe('httpVersion', () => {
it('should be 1.0', () => {
const request: FakeRawRequest = {
headers: {},
path: '/',
};
const kibanaRequest = CoreKibanaRequest.from(request);
expect(kibanaRequest.httpVersion).toEqual('1.0');
});
});

describe('headers', () => {
it('returns the correct headers', () => {
const request: FakeRawRequest = {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,7 @@ import {
KibanaRequestRouteOptions,
RawRequest,
FakeRawRequest,
HttpProtocol,
} from '@kbn/core-http-server';
import {
ELASTIC_INTERNAL_ORIGIN_QUERY_PARAM,
Expand Down Expand Up @@ -131,6 +132,10 @@ export class CoreKibanaRequest<
public readonly isInternalApiRequest: boolean;
/** {@inheritDoc KibanaRequest.rewrittenUrl} */
public readonly rewrittenUrl?: URL;
/** {@inheritDoc KibanaRequest.httpVersion} */
public readonly httpVersion: string;
/** {@inheritDoc KibanaRequest.protocol} */
public readonly protocol: HttpProtocol;

/** @internal */
protected readonly [requestSymbol]!: Request;
Expand Down Expand Up @@ -167,6 +172,10 @@ export class CoreKibanaRequest<
enumerable: false,
});

this.httpVersion = isRealReq ? request.raw.req.httpVersion : '1.0';
// hardcoded for now as only supporting http1
this.protocol = 'http1';

this.route = deepFreeze(this.getRouteInfo(request));
this.socket = isRealReq
? new KibanaSocket(request.raw.req.socket)
Expand Down
1 change: 1 addition & 0 deletions packages/core/http/core-http-server/index.ts
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,7 @@ export type {
HttpServicePreboot,
HttpServiceSetup,
HttpServiceStart,
HttpProtocol,
} from './src/http_contract';

export type {
Expand Down
8 changes: 8 additions & 0 deletions packages/core/http/core-http-server/src/http_contract.ts
Original file line number Diff line number Diff line change
Expand Up @@ -402,3 +402,11 @@ export interface HttpServerInfo {
/** The protocol used by the server */
protocol: 'http' | 'https' | 'socket';
}

/**
* Defines an http protocol.
* (Only supporting http1 for now)
*
* - http1: regroups all http/1.x protocols
*/
export type HttpProtocol = 'http1';
11 changes: 11 additions & 0 deletions packages/core/http/core-http-server/src/router/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ import type { URL } from 'url';
import type { RequestApplicationState, RouteOptionsApp } from '@hapi/hapi';
import type { Observable } from 'rxjs';
import type { RecursiveReadonly } from '@kbn/utility-types';
import type { HttpProtocol } from '../http_contract';
import type { IKibanaSocket } from './socket';
import type { RouteMethod, RouteConfigOptions } from './route';
import type { Headers } from './headers';
Expand Down Expand Up @@ -141,6 +142,16 @@ export interface KibanaRequest<
*/
readonly isInternalApiRequest: boolean;

/**
* The HTTP version sent by the client.
*/
readonly httpVersion: string;

/**
* The protocol used by the client, inferred from the httpVersion.
*/
readonly protocol: HttpProtocol;

/**
* The socket associated with this request.
* See {@link IKibanaSocket}.
Expand Down
1 change: 1 addition & 0 deletions packages/kbn-hapi-mocks/src/request.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ export const createRequestMock = (customization: DeepPartial<Request> = {}): Req
req: {
url: path,
socket: {},
httpVersion: '1.1',
},
res: {
addListener: jest.fn(),
Expand Down
20 changes: 20 additions & 0 deletions src/core/server/integration_tests/http/request.test.ts
Original file line number Diff line number Diff line change
Expand Up @@ -427,6 +427,7 @@ describe('KibanaRequest', () => {
expect(resp3.body).toEqual({ requestId: 'gamma' });
});
});

describe('request uuid', () => {
it('generates a UUID', async () => {
const { server: innerServer, createRouter } = await server.setup(setupDeps);
Expand All @@ -442,4 +443,23 @@ describe('KibanaRequest', () => {
expect(resp1.body.requestUuid).toBe('xxxxxxxx-xxxx-xxxx-xxxx-xxxxxxxxxxxx');
});
});

describe('httpVersion and protocol', () => {
it('returns the correct values', async () => {
const { server: innerServer, createRouter } = await server.setup(setupDeps);
const router = createRouter('/');
router.get({ path: '/', validate: false }, async (context, req, res) => {
return res.ok({ body: { httpVersion: req.httpVersion, protocol: req.protocol } });
});
await server.start();

const st = supertest(innerServer.listener);

const resp1 = await st.get('/').expect(200);
expect(resp1.body).toEqual({
httpVersion: '1.1',
protocol: 'http1',
});
});
});
});

0 comments on commit 3290d39

Please sign in to comment.