Skip to content

Commit

Permalink
-
Browse files Browse the repository at this point in the history
  • Loading branch information
pakohan committed Aug 14, 2015
1 parent e866776 commit f674bf0
Show file tree
Hide file tree
Showing 3 changed files with 29 additions and 15 deletions.
4 changes: 3 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
@@ -1,2 +1,4 @@
# go-seo4ajax
Golang connector for Seo4Ajax [GoDoc](http://godoc.org/github.com/justwatchcom/go-seo4ajax)
Golang connector for Seo4Ajax [GoDoc](http://godoc.org/github.com/justwatchcom/go-seo4ajax).

Code and tests is highly adapted from [connect middleware](https://www.npmjs.com/package/connect-s4a).
32 changes: 21 additions & 11 deletions seo4ajax.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"errors"
"fmt"
"io"
"net"
"net/http"
"net/url"
"os"
Expand All @@ -12,17 +13,25 @@ import (
)

var (
// IP the server is listening on.
ServerIp string

// token is either empty or invalid
ErrNoToken = errors.New("no token given")
// please set ServerIp to a valid IP (parseable by net.ParseIP())
ErrNoAddress = errors.New("no ip address")
// seo4ajax responded with a cache miss
ErrCacheMiss = errors.New("cache miss from seo4ajax")
errRedirect = errors.New("SEO4AJAX: do not follow redirect")

// used for testing
apiHost = "http://api.seo4ajax.com"

regexInvalidUserAgent = regexp.MustCompile(`(?i:google.*bot|bing|msnbot|yandexbot|pinterest.*ios|mail\.ru)`)
regexValidUserAgent = regexp.MustCompile(`(?i:bot|crawler|spider|archiver|pinterest|facebookexternalhit|flipboardproxy)`)
regexPath = regexp.MustCompile(`.*(\.[^?]{2,4}$|\.[^?]{2,4}?.*)`)
token = os.Getenv("SEO4AJAX_TOKEN")

ErrNoToken = errors.New("no token given")
ErrCacheMiss = errors.New("cache miss from seo4ajax")
errRedirect = errors.New("SEO4AJAX: do not follow redirect")

client = &http.Client{
CheckRedirect: func(req *http.Request, via []*http.Request) error {
return errRedirect
Expand Down Expand Up @@ -62,6 +71,11 @@ func GetPrerenderedPage(w http.ResponseWriter, req *http.Request) (err error) {
return
}

if ServerIp == "" || net.ParseIP(ServerIp) == nil {
err = ErrNoAddress
return
}

var prerenderRequest *http.Request
prerenderRequest, err = http.NewRequest("GET", fmt.Sprintf("%s/%s%s", apiHost, token, cleanPath(req.URL)), nil)
if err != nil {
Expand All @@ -72,9 +86,9 @@ func GetPrerenderedPage(w http.ResponseWriter, req *http.Request) (err error) {

xForwardedFor := req.Header.Get("X-Forwarded-For")
if xForwardedFor != "" {
xForwardedFor = fmt.Sprintf("%s, %s", req.URL.Host, xForwardedFor)
xForwardedFor = fmt.Sprintf("%s, %s", ServerIp, xForwardedFor)
} else {
xForwardedFor = req.URL.Host
xForwardedFor = ServerIp
}
prerenderRequest.Header.Set("X-Forwarded-For", xForwardedFor)

Expand Down Expand Up @@ -103,11 +117,7 @@ func GetPrerenderedPage(w http.ResponseWriter, req *http.Request) (err error) {
}

if err != nil {
code := resp.StatusCode
if code == 200 {
code = http.StatusInternalServerError
}
http.Error(w, err.Error(), code)
http.Error(w, err.Error(), 503)
}

return
Expand Down
8 changes: 5 additions & 3 deletions seo4ajax_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,10 +11,12 @@ import (

var (
port = ":8080"
appAdress = "localhost:3000"
appAdress = "127.0.0.1:3000"
)

func TestIsPrerender(t *testing.T) {
ServerIp = "127.0.0.1"

Convey("_escaped_fragment_ urls properly proxified", t, func() {
Convey("without _escaped_fragment_", func() {
req, err := http.NewRequest("GET", "http://"+appAdress+"/path?withQuery=parameter", nil)
Expand Down Expand Up @@ -283,7 +285,7 @@ func TestIsPrerender(t *testing.T) {
Convey("x-forwarded-for added", func(c C) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Convey("expected request in (mock) server", func() {
So(r.Header.Get("x-forwarded-for"), ShouldEqual, appAdress)
So(r.Header.Get("x-forwarded-for"), ShouldEqual, ServerIp)
})
}))
defer ts.Close()
Expand All @@ -302,7 +304,7 @@ func TestIsPrerender(t *testing.T) {
Convey("x-forwarded-for already present", func(c C) {
ts := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
c.Convey("expected request in (mock) server", func() {
So(r.Header.Get("X-Forwarded-For"), ShouldResemble, appAdress+", 10.0.0.2, 10.0.0.1")
So(r.Header.Get("X-Forwarded-For"), ShouldResemble, ServerIp+", 10.0.0.2, 10.0.0.1")
})
}))
defer ts.Close()
Expand Down

0 comments on commit f674bf0

Please sign in to comment.