gohttpquery-stringfasthttp

How to pass an URL to a fasthttp-router, in Golang?


The application consists in outputting a bash command to a buffer. The catch: the arguments are urls.

Which is working for simple cases, like: "duckduckgo". I.e., http://localhost:8080/free-riding/duckduckgo works perfectly.

Output:

   #[1]DuckDuckGo (HTML)

   [2]About DuckDuckGo
   ____________________ Submit

References

   1. https://duckduckgo.com/opensearch_html_v2.xml
   2. https://duckduckgo.com/about.html

But, http://localhost:8080/free-riding/www.estadao.com.br/politica/blog-do-fausto-macedo/dia-d-julgamento-bolsonaro-inelegivel-tse-reuniao-embaixadores/ will give:

Not Found.

Thus, how can I encode an url-argument to be passed to a router, using fasthttp?

Here is the code,

package main

import (
    "fmt"
    "log"
    "os/exec"

    "github.com/fasthttp/router"
    "github.com/valyala/fasthttp"
)

func Lynx(ctx *fasthttp.RequestCtx) {
    var url string = ctx.UserValue("url").(string)
    cmd := exec.Command("lynx", "--dump", url)
    stdout, err := cmd.Output()

    if err != nil {
        log.Fatal(err)
        return
    }
    fmt.Fprintf(ctx, "%s\n", stdout)
}

func main() {
    r := router.New()
    r.GET("/free-riding/{url}", Lynx)
    log.Fatal(fasthttp.ListenAndServe(":8080", r.Handler))
}


Solution

  • The final code, which worked just fine, using @mkopriva 's commentary: Escape the slashes, or use a catch-all parameter instead of {url}.

    func Lynx(ctx *fasthttp.RequestCtx) {
        var urlstring string = ctx.UserValue("url").(string)
        urlparsed, err := url.Parse(urlstring)
        urlparsedstring := urlparsed.String()
        fmt.Fprintf(ctx, "URL: %s\n", urlparsedstring)
    
        cmd := exec.Command("lynx", "--dump", urlparsedstring)
        stdout, err := cmd.Output()
    
        if err != nil {
            log.Fatal(err)
            return
        }
        fmt.Fprintf(ctx, "%s\n", stdout)
    }
    
    func main() {
        r := router.New()
        r.GET("/free-riding/{url:*}", Lynx)
        log.Fatal(fasthttp.ListenAndServe(":8080", r.Handler))
    }
    
    

    Showing the output: https://imgur.com/a/8f3rpjv