Skip to main content

Command Palette

Search for a command to run...

Parsing deeplinks by url.Parse may fail

Published
1 min read

Why

This question starts with a deeplink 1mg://www.1mg.com/search.

package main  

import (  
    "fmt"  
    "net/url"  
    "runtime"  
)  

func main() {  
    fmt.Println(url.Parse("1mg://www.1mg.com/search"))  
    // <nil> parse "1mg://www.1mg.com/search": first path segment in URL cannot contain colon  
}

[Go Playground]

Is it a bug?

After I test some cases, I found that url.Parse doesn't limit the scheme of uri to http or https. It failed if the scheme starts with a digit.

Here is the comment for parsing scheme:

// Maybe rawURL is of the form scheme:path.
// (Scheme must be [a-zA-Z][a-zA-Z0-9+.-]*)
// If so, return scheme, path; else return "", rawURL.
func getScheme(rawURL string) (scheme, path string, err error) {
...

In the document of URI RFC 3986

scheme = ALPHA *( ALPHA / DIGIT / "+" / "-" / "." )

It means that uri scheme implementation of golang fit the standard.

More from this blog

簡介 C++ 的 Type Erase (用多型和模板做 Duck Type)

起點 讓我們先從 template 出發:foo 需要一個 callback function。 template<typename Func> void foo(Func callback) { // ... callback(); } 但是這會讓編譯錯誤訊息有點模糊:假如 callback 並不是一個可以呼叫的函數指標,或者並不是一個 callable object ,那編譯器會說錯出在第四行。但是我們都希望,編譯器在呼叫函數時就幫我們指出:這不是 foo 想要的 call...

May 14, 20243 min read

帕秋莉的魔法筆記

45 posts

後端工程師。

不定時張貼一些寫扣時的筆記。