denoland / std

The Deno Standard Library
https://jsr.io/@std
MIT License
3.16k stars 619 forks source link

compat(node/https): request function does not accept a complete url as the path #2478

Closed gamer0mega closed 2 years ago

gamer0mega commented 2 years ago

Describe the bug

Most HTTP Servers can handle a complete url as the request path, and in node, you can set it to a complete url. It just passes it to the HTTP header, while std just yields an error about invalid url.

Steps to Reproduce

  1. Create a script with contents
    const https = require('https');
    const request = https.request({method: 'GET', host: 'discord.com', path: 'https://discord.com/api/v10/gateway', headers: {"User-Agent": "DiscordBot"}});
    request.end();
    request.on('response', response => {
    response.on('data', data=>console.log(data.toString()));
    response.on('end', ()=>console.log('ok'));
    });
  2. Run it using the deno compat mode(it uses modules from std for node defaults)
    >deno run -A --unstable --compat httpsTest.js
    error: Uncaught TypeError: Invalid URL
    const mayResponse = fetch(this._createUrlStrFromOptions(this.opts), opts)
                        ^
    at Object.opSync (deno:core/01_core.js:170:12)
    at opUrlParse (deno:ext/url/00_url.js:49:27)
    at new URL (deno:ext/url/00_url.js:323:20)
    at new Request (deno:ext/fetch/23_request.js:241:27)
    at deno:ext/fetch/26_fetch.js:429:29
    at new Promise (<anonymous>)
    at fetch (deno:ext/fetch/26_fetch.js:425:20)
    at HttpsClientRequest._final (https://deno.land/std@0.149.0/node/http.ts:126:25)

Expected behavior

>node httpsTest.js
{"url": "wss://gateway.discord.gg"}
ok

As in node.

Environment

gamer0mega commented 2 years ago

The issue seems to be that std does not specify the protocol field by default, unlike node. The code actually returns

error: Uncaught TypeError: error sending request for url (https://discord.comhttps//discord.com/api/v10/gateway): error trying to connect: dns error: Unknown host. (os error 11001)
      await mayResponse,
      ^
    at async mainFetch (deno:ext/fetch/26_fetch.js:287:14)
    at async fetch (deno:ext/fetch/26_fetch.js:501:9)
    at async HttpsClientRequest._final (https://deno.land/std@0.149.0/node/http.ts:136:7)

if I set protocol to https:, this needs to be fixed aswell.