Skip to content

Instantly share code, notes, and snippets.

@exhuma
Created October 15, 2017 12:41
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save exhuma/55874884bcd3174cd3375c3281dc745b to your computer and use it in GitHub Desktop.
Save exhuma/55874884bcd3174cd3375c3281dc745b to your computer and use it in GitHub Desktop.
Example service which returns a custom media-type with a +json indicator

This service is used as an example to demonstrate the Chromium issue described at https://bugs.chromium.org/p/chromium/issues/detail?id=773654

It does not use any external dependencies. To run this, simply follow theses steps:

go build server.go
./server

This will run a HTTP server on port 8080. To test the issue, open the page with Chromium, open the developer tools network tab and reload the page. Upon loading it will make an XHR call to /data. Inspeciting this in the developer tools should demonstrate the issue linked above.

<!DOCTYPE html>
<html lang="en">
<head>
<meta charset="UTF-8">
<title></title>
</head>
<body>
Hello
<script>
httpRequest = new XMLHttpRequest();
httpRequest.onreadystatechange = function(){
console.log('done with ' + httpRequest.readyState + ' status: ' + httpRequest.status);
console.log(httpRequest.responseText);
};
httpRequest.open('GET', '/data', true);
httpRequest.send()
</script>
</body>
</html>
package main
import (
"fmt"
"html/template"
"net/http"
)
var templates = template.Must(template.ParseFiles("index.html"))
func renderTemplate(w http.ResponseWriter, tmpl string) {
err := templates.ExecuteTemplate(w, tmpl + ".html", nil)
if err != nil {
http.Error(w, err.Error(), http.StatusInternalServerError)
return
}
}
func rootHandler(w http.ResponseWriter, r *http.Request) {
renderTemplate(w, "index")
}
func customDataHandler(w http.ResponseWriter, r *http.Request) {
w.Header().Set("Content-Type", "prs.mytype/foo+json")
fmt.Fprintf(w, "{\"hello\": \"world\"}")
}
func main() {
http.HandleFunc("/", rootHandler)
http.HandleFunc("/data", customDataHandler)
http.ListenAndServe(":8080", nil)
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment