Skip to content

Commit

Permalink
refactor: small formatting change
Browse files Browse the repository at this point in the history
  • Loading branch information
metcoder95 committed Aug 3, 2021
1 parent 2d89e5f commit 8e7d98c
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 70 deletions.
6 changes: 3 additions & 3 deletions lib/client.js
Original file line number Diff line number Diff line change
Expand Up @@ -1395,9 +1395,9 @@ function write (client, request) {
let header

if (upgrade) {
let connectionString = "upgrade";
if (headers.toLowerCase().includes("http2-settings")) {
connectionString += ", http2-settings";
let connectionString = 'upgrade'
if (headers.toLowerCase().includes('http2-settings')) {
connectionString += ', http2-settings'
}
header = `${method} ${path} HTTP/1.1\r\nconnection: ${connectionString}\r\nupgrade: ${upgrade}\r\n`
} else if (client[kPipelining]) {
Expand Down
135 changes: 68 additions & 67 deletions lib/http2client.js
Original file line number Diff line number Diff line change
@@ -1,79 +1,80 @@
"use strict";
'use strict'
/* eslint-disable no-unused-vars, max-len, id-length */

const assert = require("assert");
const { kUrl, kSocket, kHTTP2Opts } = require("./core/symbols");
const assert = require('assert')
const { kUrl, kSocket, kHTTP2Opts } = require('./core/symbols')

const FRAME_TYPES = [
"DATA", // standard request / response payloads
"HEADERS", // starts stream, carries request headers
"PRIORITY", // advises the priority of a stream
"RST_STREAM", // immediately terminates a stream
"SETTINGS", // inform other end of endpoint config
"PUSH_PROMISE", // pre-warns peer of wanted streams
"PING",
"GOAWAY", // graceful version of RST_STREAM
"WINDOW_UPDATE", // defines flow-control
"CONTINUATION" // continue a sequence of headers
];
'DATA', // standard request / response payloads
'HEADERS', // starts stream, carries request headers
'PRIORITY', // advises the priority of a stream
'RST_STREAM', // immediately terminates a stream
'SETTINGS', // inform other end of endpoint config
'PUSH_PROMISE', // pre-warns peer of wanted streams
'PING',
'GOAWAY', // graceful version of RST_STREAM
'WINDOW_UPDATE', // defines flow-control
'CONTINUATION' // continue a sequence of headers
]

function parseHttp2Settings(settings) {
// under RFC7540 section 3.2.1, the settings must be a base64url encoded
// SETTINGS frame. 16 bit identifiers, 32 bit values. */
let parsed = "";
for (let i = 0; i < Object.keys(settings).length; i += 2) {
parsed += `${settings[i]}${settings[i + 1]}\r\n`;
}
function parseHttp2Settings (settings) {
// under RFC7540 section 3.2.1, the settings must be a base64url encoded
// SETTINGS frame. 16 bit identifiers, 32 bit values. */
let parsed = ''
for (let i = 0; i < Object.keys(settings).length; i += 2) {
parsed += `${settings[i]}${settings[i + 1]}\r\n`
}

// base64url polyfill, courtesy of @panva
let encoded;
if (Buffer.isEncoding("base64url")) {
encoded = Buffer.from(parsed)
.toString("base64url")
.replace(/[=]+$/g, "");
} else {
encoded = Buffer.from(parsed)
.toString("base64")
.replace(/\+/g, "-")
.replace(/\//g, "_")
.replace(/[=]+$/g, "");
}
return encoded;
// base64url polyfill, courtesy of @panva
let encoded
if (Buffer.isEncoding('base64url')) {
encoded = Buffer.from(parsed)
.toString('base64url')
.replace(/[=]+$/g, '')
} else {
encoded = Buffer.from(parsed)
.toString('base64')
.replace(/\+/g, '-')
.replace(/\//g, '_')
.replace(/[=]+$/g, '')
}
return encoded
}

function http2Connect(client) {
assert(!client[kSocket]);
const { protocol, port, hostname, pathname } = client[kUrl];
if (protocol === "https:") {
throw new Error("Invalid protocol - httpConnect upgrades http streams");
}
// TODO: allow ALT-SVC
client.upgrade({
path: pathname,
protocol: "h2c",
headers: {
"HTTP2-Settings": parseHttp2Settings(client[kHTTP2Opts] || {})
}
})
.then((res) => {
console.log("hi");
console.log(res);
})
.catch((err) => {
if (err.code === "UND_ERR_SOCKET" && err.message === "bad upgrade") {
// do not upgrade
} else {
throw err;
}
});
function http2Connect (client) {
assert(!client[kSocket])
const { protocol, port, hostname, pathname } = client[kUrl]
if (protocol === 'https:') {
throw new Error('Invalid protocol - httpConnect upgrades http streams')
}
// TODO: allow ALT-SVC
client
.upgrade({
path: pathname,
protocol: 'h2c',
headers: {
'HTTP2-Settings': parseHttp2Settings(client[kHTTP2Opts] || {})
}
})
.then(res => {
console.log('hi')
console.log(res)
})
.catch(err => {
if (err.code === 'UND_ERR_SOCKET' && err.message === 'bad upgrade') {
// do not upgrade
} else {
throw err
}
})
}

function https2Connect(client) {
const { protocol, port, hostname } = client[kUrl];
if (protocol === "http:") {
throw new Error("Invalid protocol - https2Connect upgrades https streams");
}
// TODO: tls negotiation comes FIRST
function https2Connect (client) {
const { protocol, port, hostname } = client[kUrl]
if (protocol === 'http:') {
throw new Error('Invalid protocol - https2Connect upgrades https streams')
}
// TODO: tls negotiation comes FIRST
}

module.exports = http2Connect;
module.exports = http2Connect

0 comments on commit 8e7d98c

Please sign in to comment.