From b6de57f62b42a94411079e363073db6201564a18 Mon Sep 17 00:00:00 2001 From: akimon658 Date: Thu, 21 Apr 2022 15:38:25 +0900 Subject: [PATCH 1/2] Return same code as requested URL --- main.go | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/main.go b/main.go index 2c9f8b5..33c514b 100644 --- a/main.go +++ b/main.go @@ -3,6 +3,7 @@ package main import ( "encoding/json" "flag" + "fmt" "log" "net/http" "strings" @@ -40,10 +41,16 @@ func main() { return &serverError{http.StatusInternalServerError, err.Error()} } + if resp.StatusCode >= http.StatusBadRequest { + return &serverError{resp.StatusCode, fmt.Sprintf("error response from %s: %s", url, resp.Status)} + } + if !strings.HasPrefix(resp.Header.Get("Content-Type"), "text/html") { return &serverError{http.StatusNotFound, `Content type of requested URL is not "text/html"`} } + w.WriteHeader(resp.StatusCode) + og := opengraph.New(url) if err = og.Parse(resp.Body); err != nil { return &serverError{http.StatusInternalServerError, err.Error()} From 052cb6cf75f76848bd890cfbdd2b3249829f92f8 Mon Sep 17 00:00:00 2001 From: akimon658 Date: Thu, 21 Apr 2022 15:40:24 +0900 Subject: [PATCH 2/2] Remove result of docs.github.com --- README.md | 5 ----- 1 file changed, 5 deletions(-) diff --git a/README.md b/README.md index 9962b0b..50b856f 100644 --- a/README.md +++ b/README.md @@ -43,11 +43,6 @@ To avoid it, you can use `user-agent` flag. ogjson -user-agent "Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/100.0.4896.127 Safari/537.36" ``` -``` -$ curl "http://localhost:8080/?url=https://docs.github.com" -{"Policy":{"TrustedTags":["meta","link","title"]},"Title":"GitHub.com Help Documentation","Type":"article","URL":{"Source":"https://docs.github.com","Scheme":"https","Opaque":"","User":null,"Host":"docs.github.com","Path":"","RawPath":"","ForceQuery":false,"RawQuery":"","Fragment":"","RawFragment":"","Value":"http://ghdocs-prod.azurewebsites.net:80/en"},"SiteName":"GitHub Docs","Image":[{"URL":"https://github.githubassets.com/images/modules/open_graph/github-logo.png","SURL":"","Type":"","Width":0,"Height":0,"Alt":""}],"Video":[],"Audio":[],"Description":"Get started, troubleshoot, and make the most of GitHub. Documentation for new users, developers, administrators, and all of GitHub's products.","Determiner":"","Locale":"","LocaleAlt":[],"Favicon":"/assets/cb-803/images/site/favicon.svg"} -``` - ## License [MIT](./LICENSE)