英文:
How to pass an URL to a fasthttp-router, in Golang?
问题
该应用程序的功能是将一个bash命令输出到缓冲区。注意:参数是url
。
对于简单的情况,比如:"duckduckgo",即http://localhost:8080/free-riding/duckduckgo
可以正常工作。
输出结果:
#[1]DuckDuckGo (HTML)
[2]About DuckDuckGo
____________________ Submit
References
1. https://duckduckgo.com/opensearch_html_v2.xml
2. https://duckduckgo.com/about.html
但是,http://localhost:8080/free-riding/www.estadao.com.br/politica/blog-do-fausto-macedo/dia-d-julgamento-bolsonaro-inelegivel-tse-reuniao-embaixadores/
会返回"Not Found"。
因此,我应该如何对一个url
参数进行编码,以便将其传递给router
,并使用fasthttp
?
以下是代码:
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))
}
英文:
The application consists in outputting a bash command to a buffer. The catch: the arguments are url
s.
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))
}
答案1
得分: 2
最终的代码,使用了 @mkopriva 的注释,运行得很好:转义斜杠,或者使用一个捕获所有参数的通配符代替 {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))
}
显示输出结果:https://imgur.com/a/8f3rpjv
英文:
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
答案2
得分: 0
你应该使用URL编码对其进行编码。以下是你的代码中包含URL编码和解码的小改动:
package main
import (
"fmt"
"log"
"net/url"
"os/exec"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
func Lynx(ctx *fasthttp.RequestCtx) {
urlParam := ctx.UserValue("url").(string)
decodedURL, err := url.QueryUnescape(urlParam)
if err != nil {
log.Fatalf("解码URL时出错:%v", err)
return
}
encodedURL := url.QueryEscape(decodedURL)
cmd := exec.Command("lynx", "--dump", encodedURL)
stdout, err := cmd.Output()
if err != nil {
log.Fatalf("执行命令时出错:%v", 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))
}
请注意,我已经将代码中的双引号(")替换为了中文引号(“”),以便更好地适应中文环境。
英文:
you should encode it using URL encoding. Here is a small change in your code to include URL encoding and decoding:
package main
import (
"fmt"
"log"
"net/url"
"os/exec"
"github.com/fasthttp/router"
"github.com/valyala/fasthttp"
)
func Lynx(ctx *fasthttp.RequestCtx) {
urlParam := ctx.UserValue("url").(string)
decodedURL, err := url.QueryUnescape(urlParam)
if err != nil {
log.Fatalf("Error decoding URL: %v", err)
return
}
cmd := exec.Command("lynx", "--dump", decodedURL)
stdout, err := cmd.Output()
if err != nil {
log.Fatalf("Error executing command: %v", 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))
}
</details>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论