Merge lp:~verterok/ubuntu-push/support-media-type into lp:ubuntu-push/automatic

Proposed by Guillermo Gonzalez
Status: Merged
Approved by: Guillermo Gonzalez
Approved revision: 354
Merged at revision: 353
Proposed branch: lp:~verterok/ubuntu-push/support-media-type
Merge into: lp:ubuntu-push/automatic
Diff against target: 48 lines (+19/-1)
2 files modified
server/api/handlers.go (+3/-1)
server/api/handlers_test.go (+16/-0)
To merge this branch: bzr merge lp:~verterok/ubuntu-push/support-media-type
Reviewer Review Type Date Requested Status
Samuele Pedroni Approve
Review via email: mp+242020@code.launchpad.net

Commit message

Add support to media-type in the Content-Type check at server/api handlers

Description of the change

Add support to media-type in the Content-Type check at server/api/handlers.go
This is needed as some clients, like the qml XmlHTTPRequest, append "; charset=UTF-8" to the content-type.

see http://www.w3.org/Protocols/rfc2616/rfc2616-sec14.html#sec14.17

To post a comment you must log in.
Revision history for this message
Bret Barker (noise) wrote :

minor nitpick comment below

Revision history for this message
Bret Barker (noise) wrote :

Hmm, shouldn't the request header be "Accept"? Content-type is the response header.

354. By Guillermo Gonzalez

use mime.ParseMediaType

Revision history for this message
Samuele Pedroni (pedronis) :
review: Approve

Preview Diff

[H/L] Next/Prev Comment, [J/K] Next/Prev File, [N/P] Next/Prev Hunk
1=== modified file 'server/api/handlers.go'
2--- server/api/handlers.go 2014-11-04 17:59:43 +0000
3+++ server/api/handlers.go 2014-11-17 19:54:49 +0000
4@@ -23,6 +23,7 @@
5 "encoding/json"
6 "fmt"
7 "io"
8+ "mime"
9 "net/http"
10 "time"
11
12@@ -274,7 +275,8 @@
13 if err := checkContentLength(request, maxBodySize); err != nil {
14 return err
15 }
16- if request.Header.Get("Content-Type") != JSONMediaType {
17+ mediaType, _, err := mime.ParseMediaType(request.Header.Get("Content-Type"))
18+ if err != nil || mediaType != JSONMediaType {
19 return ErrWrongContentType
20 }
21 return nil
22
23=== modified file 'server/api/handlers_test.go'
24--- server/api/handlers_test.go 2014-11-04 16:29:21 +0000
25+++ server/api/handlers_test.go 2014-11-17 19:54:49 +0000
26@@ -933,6 +933,22 @@
27 checkError(c, response, ErrWrongContentType)
28 }
29
30+func (s *handlersSuite) TestContentTypeWithCharset(c *C) {
31+ testServer := httptest.NewServer(&JSONPostHandler{})
32+ defer testServer.Close()
33+
34+ dataString := `{"foo":"bar"}`
35+
36+ request := newPostRequest("/", &Broadcast{
37+ Channel: "some-channel",
38+ ExpireOn: future,
39+ Data: json.RawMessage([]byte(dataString)),
40+ }, testServer)
41+ request.Header.Set("Content-Type", "application/json; charset=UTF-8")
42+ result := checkRequestAsPost(request, 1024)
43+ c.Assert(result, IsNil)
44+}
45+
46 func (s *handlersSuite) TestCannotBroadcastNonPostMessages(c *C) {
47 testServer := httptest.NewServer(&JSONPostHandler{})
48 defer testServer.Close()

Subscribers

People subscribed via source and target branches