-
Notifications
You must be signed in to change notification settings - Fork 8.2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
refactor listener creation logic + add http2 listener support
- Loading branch information
1 parent
2205584
commit d893a06
Showing
15 changed files
with
139 additions
and
74 deletions.
There are no files selected for viewing
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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
This file contains 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,73 @@ | ||
/* | ||
* Copyright Elasticsearch B.V. and/or licensed to Elasticsearch B.V. under one | ||
* or more contributor license agreements. Licensed under the Elastic License | ||
* 2.0 and the Server Side Public License, v 1; you may not use this file except | ||
* in compliance with, at your election, the Elastic License 2.0 or the Server | ||
* Side Public License, v 1. | ||
*/ | ||
|
||
import http from 'http'; | ||
import https from 'https'; | ||
import http2 from 'http2'; | ||
import { getServerTLSOptions } from './get_tls_options'; | ||
import type { IHttpConfig, ServerListener } from './types'; | ||
|
||
interface GetServerListenerOptions { | ||
configureTLS?: boolean; | ||
} | ||
|
||
export function getServerListener( | ||
config: IHttpConfig, | ||
options: GetServerListenerOptions = {} | ||
): ServerListener { | ||
const useHTTP2 = config.protocol === 'http2'; | ||
return useHTTP2 | ||
? configureHttp2Listener(config, options) | ||
: configureHttp1Listener(config, options); | ||
} | ||
|
||
export const configureHttp1Listener = ( | ||
config: IHttpConfig, | ||
{ configureTLS = true }: GetServerListenerOptions = {} | ||
): ServerListener => { | ||
const useTLS = configureTLS && config.ssl.enabled; | ||
const tlsOptions = useTLS ? getServerTLSOptions(config.ssl) : undefined; | ||
|
||
const listener = useTLS | ||
? https.createServer({ | ||
...tlsOptions, | ||
keepAliveTimeout: config.keepaliveTimeout, | ||
}) | ||
: http.createServer({ | ||
keepAliveTimeout: config.keepaliveTimeout, | ||
}); | ||
|
||
listener.setTimeout(config.socketTimeout); | ||
listener.on('clientError', (err, socket) => { | ||
if (socket.writable) { | ||
socket.end(Buffer.from('HTTP/1.1 400 Bad Request\r\n\r\n', 'ascii')); | ||
} else { | ||
socket.destroy(err); | ||
} | ||
}); | ||
|
||
return listener; | ||
}; | ||
|
||
export const configureHttp2Listener = ( | ||
config: IHttpConfig, | ||
{ configureTLS = true }: GetServerListenerOptions = {} | ||
): ServerListener => { | ||
const useTLS = configureTLS && config.ssl.enabled; | ||
const tlsOptions = useTLS ? getServerTLSOptions(config.ssl) : undefined; | ||
|
||
const listener = useTLS | ||
? http2.createSecureServer({ | ||
...tlsOptions, | ||
}) | ||
: http2.createServer({}); | ||
|
||
listener.setTimeout(config.socketTimeout); | ||
|
||
return listener; | ||
}; |
21 changes: 0 additions & 21 deletions
21
packages/kbn-server-http-tools/src/get_listener_options.ts
This file was deleted.
Oops, something went wrong.
This file contains 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
This file contains 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
This file contains 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
Oops, something went wrong.