-
Notifications
You must be signed in to change notification settings - Fork 13
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #70 from parkr/log-path-from-js
pingv2: create a mechanism to send an XHR request with full path
- Loading branch information
Showing
11 changed files
with
568 additions
and
79 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,14 @@ | ||
package dnt | ||
|
||
import "net/http" | ||
|
||
const DoNotTrackHeaderName = "DNT" | ||
const DoNotTrackHeaderValue = "1" | ||
|
||
func RequestsDoNotTrack(r *http.Request) bool { | ||
return r.Header.Get(DoNotTrackHeaderName) == DoNotTrackHeaderValue | ||
} | ||
|
||
func SetDoNotTrack(w http.ResponseWriter) { | ||
w.Header().Set(DoNotTrackHeaderName, DoNotTrackHeaderValue) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file was deleted.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,32 @@ | ||
package jsv1 | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
|
||
"github.com/parkr/ping/dnt" | ||
) | ||
|
||
const returnedJavaScript = "(function(){})();" | ||
const lengthOfJavaScript = "17" | ||
|
||
func Write(w http.ResponseWriter, code int) { | ||
w.WriteHeader(code) | ||
w.Header().Set("Content-Type", "application/javascript") | ||
w.Header().Set("Content-Length", lengthOfJavaScript) | ||
fmt.Fprintf(w, returnedJavaScript) | ||
} | ||
|
||
func DoNotTrack(w http.ResponseWriter) { | ||
Write(w, http.StatusOK) | ||
dnt.SetDoNotTrack(w) | ||
} | ||
|
||
func Error(w http.ResponseWriter, code int, err string) { | ||
w.WriteHeader(code) | ||
content := fmt.Sprintf(`(function(){console.error("%s")})();`, err) | ||
w.Header().Set("Content-Type", "application/javascript") | ||
w.Header().Set("Content-Length", strconv.Itoa(len(content))) | ||
fmt.Fprintf(w, content) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,44 @@ | ||
package jsv2 | ||
|
||
import ( | ||
"fmt" | ||
"net/http" | ||
"strconv" | ||
) | ||
|
||
const returnedJavaScript = ` | ||
function logVisit(document) { | ||
var httpRequest = new XMLHttpRequest(); | ||
httpRequest.onreadystatechange = () => { | ||
if (httpRequest.readyState === XMLHttpRequest.DONE) { | ||
if (httpRequest.status > 100 && httpRequest.status < 300) { | ||
console.log("visit log result:", httpRequest.responseText) | ||
} else { | ||
console.error('There was a problem with the request.') | ||
console.error(httpRequest.status, httpRequest.responseText, httpRequest) | ||
} | ||
} | ||
}; | ||
const visitSearchParams = new URLSearchParams() | ||
visitSearchParams.append('host', document.location.hostname) | ||
visitSearchParams.append('path', document.location.pathname) | ||
const visitURL = new URL('https://ping.parkermoo.re/submit.js') | ||
visitURL.search = "?" + visitSearchParams.toString() | ||
httpRequest.open('POST', visitURL.toString(), true); | ||
httpRequest.send(); | ||
} | ||
(function(){ | ||
document.addEventListener('readystatechange', (event) => { | ||
if (document.readyState === 'complete') { | ||
logVisit(document) | ||
} | ||
}); | ||
})() | ||
` | ||
|
||
func Write(w http.ResponseWriter, code int) { | ||
w.WriteHeader(code) | ||
w.Header().Set("Content-Type", "application/javascript") | ||
w.Header().Set("Content-Length", strconv.Itoa(len(returnedJavaScript))) | ||
fmt.Fprintf(w, returnedJavaScript) | ||
} |
Oops, something went wrong.