Skip to content

Commit

Permalink
fix #1 (mime-type header bug)
Browse files Browse the repository at this point in the history
  • Loading branch information
SimonWaldherr committed Apr 3, 2016
1 parent 818c85e commit fdc2563
Show file tree
Hide file tree
Showing 2 changed files with 19 additions and 13 deletions.
29 changes: 16 additions & 13 deletions server_handler.go
Original file line number Diff line number Diff line change
Expand Up @@ -11,45 +11,48 @@ import (

func (GWV *WebServer) handle200(rw http.ResponseWriter, req *http.Request, resp string, route *HandlerWrapper, code int) {
var err error
rw.WriteHeader(code)

switch route.mime {
case HTML:
rw.Header().Set("Content-type", "text/html")
_, err = io.WriteString(rw, resp)
rw.Header().Set("Content-Type", "text/html")
break
case PLAIN:
rw.Header().Set("Content-type", "text/plain")
_, err = io.WriteString(rw, resp)
rw.Header().Set("Content-Type", "text/plain")
break
case JSON:
rw.Header().Set("Content-type", "application/json")
err = json.NewEncoder(rw).Encode(map[string]string{
"message": resp,
})
rw.Header().Set("Content-Type", "application/json")
break
case AUTO:
if len(req.URL.Path) > len(route.rawre) {
reqstr := req.URL.Path[len(route.rawre):]
ctype := mime.TypeByExtension(filepath.Ext(reqstr))
rw.Header().Set("Content-Type", ctype)
} else {
rw.Header().Set("Content-type", "text/plain")
rw.Header().Set("Content-Type", "text/plain")
}
_, err = io.WriteString(rw, resp)
break
case ICON:
rw.Header().Set("Content-Type", "image/x-icon")
_, err = io.WriteString(rw, resp)
break
case DOWNLOAD:
rw.Header().Set("Content-Type", "application/octet-stream")
rw.Header().Set("Content-Disposition", "attachment")
_, err = io.WriteString(rw, resp)
break
default:
GWV.logChannelHandler(fmt.Sprint("Unknown handler type: ", route.mime))
break
}

rw.WriteHeader(code)

switch route.mime {
case JSON:
err = json.NewEncoder(rw).Encode(map[string]string{
"message": resp,
})
default:
_, err = io.WriteString(rw, resp)
}
GWV.extendedErrorHandler("Error on WriteString to client: ", err, false)
}

Expand Down
3 changes: 3 additions & 0 deletions server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -187,6 +187,9 @@ func Test_Realtime(t *testing.T) {

HTTPRequest("http://localhost:8082/sse")
HTTPRequest("http://localhost:8082/err")

i, ip := hub.ClientDetails()
t.Logf("count: %v\nIP-Addresses: %v\n", i, ip)

HTTPD.Stop()

Expand Down

0 comments on commit fdc2563

Please sign in to comment.