Skip to content
This repository has been archived by the owner on Oct 3, 2024. It is now read-only.

Commit

Permalink
Merge pull request #10 from Akimon658/fix-status-codes
Browse files Browse the repository at this point in the history
fix: Return same status code as the requested URL
  • Loading branch information
akimon658 authored Apr 21, 2022
2 parents 532d0e6 + 052cb6c commit 4c4b753
Show file tree
Hide file tree
Showing 2 changed files with 7 additions and 5 deletions.
5 changes: 0 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down
7 changes: 7 additions & 0 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package main
import (
"encoding/json"
"flag"
"fmt"
"log"
"net/http"
"strings"
Expand Down Expand Up @@ -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()}
Expand Down

0 comments on commit 4c4b753

Please sign in to comment.