Skip to content

HTTP2 - how to send multiple PRIORITY frames and only one HEADERS frame ? #3997

@gointuition

Description

@gointuition

Details

I want to simulate Firefox Http2 frames, like below
Selection_094
The Firefox Http2 frames order are Magic, SETTINGS[0], WINDOW_UPDATE[0], PRIORITY[3], PRIORITY[5], PRIORITY[7], PRIORITY[9], PRIORITY[11], PRIORITY[13], then HEADERS[15]: GET /api/all, WINDOW_UPDATE[15].

I wrote a script with node-http2 but got the result below
Selection_095
As you can see, every stream sends a HEADERS frame. This is unexpected. I want to send the HEADERS frame only once and its parent stream id is 13, just like Firefox.
Maybe there is something wrong with my script, could you help check it? Thank you.

Node.js version

v18.3.0

Example code

const http2 = require('http2');

const
    scheme = 'https',
    method = 'GET',
    host = 'tls.peet.ws',
    path = '/api/all',
    userAgent = 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:105.0) Gecko/20100101 Firefox/105.0',
    accept = 'text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,*/*;q=0.8',
    acceptLanguage = 'en-US,en;q=0.5',
    acceptEncoding = 'gzip, deflate, br',
    connection = 'keep-alive',
    cacheControl = 'no-cache',
    trailers = 'trailers';

const 
    reqOptions = {
        [http2.constants.HTTP2_HEADER_METHOD]: method,
        [http2.constants.HTTP2_HEADER_PATH]: path,
        [http2.constants.HTTP2_HEADER_AUTHORITY]: host,
        [http2.constants.HTTP2_HEADER_SCHEME]: scheme,
        [http2.constants.HTTP2_HEADER_USER_AGENT]: userAgent,
        [http2.constants.HTTP2_HEADER_ACCEPT]: accept,
        [http2.constants.HTTP2_HEADER_ACCEPT_LANGUAGE]: acceptLanguage,
        [http2.constants.HTTP2_HEADER_ACCEPT_ENCODING]: acceptEncoding,
        // [ http2.constants.HTTP2_HEADER_CONNECTION ]         : connection,
        'Upgrade-Insecure-Requests': '1',
        'Sec-Fetch-Dest': 'document',
        'Sec-Fetch-Mode': 'navigate',
        'Sec-Fetch-Site': 'none',
        'Sec-Fetch-User': '?1',
        'Pragma': 'no-cache',
        [http2.constants.HTTP2_HEADER_CACHE_CONTROL]: cacheControl,
        [http2.constants.HTTP2_HEADER_TE]: trailers
    };

const client = http2.connect(`${scheme}://${host}`, {
    settings: {
        // enablePush: false, 
        headerTableSize: 65536,
        initialWindowSize: 131072,
        maxFrameSize: 16384
    },
}, (session) => {
    session.setLocalWindowSize(1024 * 12288);
});

function request(offset) {
    return new Promise((resolve, reject) => {
        const stream = client.request(reqOptions, {
                                        weight: 42, 
                                        parent: offset === 0 ? 0 : 1,
                                        exclusive: false,
                                    });
        if (offset !== 0) {
            stream.priority({
                exclusive: false,
                weight: 241,
            });
        }
        stream.on('error', () => {
            console.log(stream.id);
        });
        stream.on('response', () => {
            console.log(stream.id);
        });

        stream.setEncoding('utf8');
        let data = '';
        stream.on('data', (chunk) => {
            data += chunk;
        });
        stream.on('end', () => {
            console.log(data);
            console.log(stream.id);
        });
        stream.end();
    });
}

const requests = [];
for (let i = 0; i < 7; i++) {
    requests.push(request(i));
}

Promise.all(requests).then().finally(() => {
    client.close();
});

Operating system

Ubuntu 20

Scope

code tests

Module and version

Not applicable.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions