deno_std icon indicating copy to clipboard operation
deno_std copied to clipboard

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

Open gamer0mega opened this issue 1 year ago • 1 comments

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'));
});
  1. 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/[email protected]/node/http.ts:126:25)

Expected behavior

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

As in node.

Environment

  • OS: Windows 10
  • deno version: 1.24.0
  • std version: 0.149.0

gamer0mega avatar Jul 28 '22 15:07 gamer0mega

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/[email protected]/node/http.ts:136:7)

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

gamer0mega avatar Jul 28 '22 19:07 gamer0mega