0 1 2 3
0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1 2 3 4 5 6 7 8 9 0 1
+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+-+
| Origin-Len (16) | Origin? () …
+——————————-+—————-+————–+
| Alt-Svc-Field-Value () …
+—————————————————————+
The ALTSVC frame contains the following fields:
Origin-Len: An unsigned, 16-bit integer indicating the length, in
octets, of the Origin field.
Origin: An OPTIONAL sequence of characters containing ASCII
serialisation of an origin (RFC6454,
Section 6.2) that the alternate service is applicable to.
Alt-Svc-Field-Value: A sequence of octets (length determined by
subtracting the length of all preceding fields from the frame
length) containing a value identical to the Alt-Svc field value
defined in (Section 3)[https://tools.ietf.org/html/rfc7838#section-3]
(ABNF production “Alt-Svc”).
typeSpecificAttributes.ALTSVC = ['maxAge', 'port', 'protocolID', 'host',
'origin'];
function istchar(c) {
return ('!#$&\'*+-.^_`|~1234567890ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz'.indexOf(c) > -1);
}
function hexencode(s) {
var t = '';
for (var i = 0; i < s.length; i++) {
if (!istchar(s[i])) {
t += '%';
t += new Buffer(s[i]).toString('hex');
} else {
t += s[i];
}
}
return t;
}
Serializer.ALTSVC = function writeAltSvc(frame, buffers) {
var buffer = new Buffer(2);
buffer.writeUInt16BE(frame.origin.length, 0);
buffers.push(buffer);
buffers.push(new Buffer(frame.origin, 'ascii'));
var fieldValue = hexencode(frame.protocolID) + '="' + frame.host + ':' + frame.port + '"';
if (frame.maxAge !== 86400) {
fieldValue += "; ma=" + frame.maxAge;
}
buffers.push(new Buffer(fieldValue, 'ascii'));
};
function stripquotes(s) {
var start = 0;
var end = s.length;
while ((start < end) && (s[start] === '"')) {
start++;
}
while ((end > start) && (s[end - 1] === '"')) {
end--;
}
if (start >= end) {
return "";
}
return s.substring(start, end);
}
function splitNameValue(nvpair) {
var eq = -1;
var inQuotes = false;
for (var i = 0; i < nvpair.length; i++) {
if (nvpair[i] === '"') {
inQuotes = !inQuotes;
continue;
}
if (inQuotes) {
continue;
}
if (nvpair[i] === '=') {
eq = i;
break;
}
}
if (eq === -1) {
return {'name': nvpair, 'value': null};
}
var name = stripquotes(nvpair.substring(0, eq).trim());
var value = stripquotes(nvpair.substring(eq + 1).trim());
return {'name': name, 'value': value};
}
function splitHeaderParameters(hv) {
return parseHeaderValue(hv, ';', splitNameValue);
}
function parseHeaderValue(hv, separator, callback) {
var start = 0;
var inQuotes = false;
var values = [];
for (var i = 0; i < hv.length; i++) {
if (hv[i] === '"') {
inQuotes = !inQuotes;
continue;
}
if (inQuotes) {