Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(725)

Side by Side Diff: server/prpc/decoding.go

Issue 1636873006: server/prpc: updated server according to protocol (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Removed some panics, malformed JSON now errors. Created 4 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2016 The Chromium Authors. All rights reserved. 1 // Copyright 2016 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 package prpc 5 package prpc
6 6
7 import ( 7 import (
8 "bytes" 8 "bytes"
9 "encoding/base64" 9 "encoding/base64"
10 "fmt" 10 "fmt"
(...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after
44 } 44 }
45 f, err := parseFormat(mediaType, mediaTypeParams) 45 f, err := parseFormat(mediaType, mediaTypeParams)
46 if err != nil { 46 if err != nil {
47 return f, err 47 return f, err
48 } 48 }
49 49
50 switch f { 50 switch f {
51 case formatUnrecognized: 51 case formatUnrecognized:
52 return f, fmt.Errorf("%q is not supported", contentType) 52 return f, fmt.Errorf("%q is not supported", contentType)
53 53
54 case formatUnspecified:
55 return formatBinary, nil
56
57 case formatBinary, formatJSONPB, formatText: 54 case formatBinary, formatJSONPB, formatText:
58 return f, nil 55 return f, nil
59 56
60 default: 57 default:
61 » » panic("cannot happen") 58 » » return formatBinary, nil
62 } 59 }
63 } 60 }
64 61
65 // readMessage decodes a protobuf message from an HTTP request. 62 // readMessage decodes a protobuf message from an HTTP request.
66 // Does not close the request body. 63 // Does not close the request body.
67 func readMessage(r *http.Request, msg proto.Message) *httpError { 64 func readMessage(r *http.Request, msg proto.Message) *protocolError {
68 » if msg == nil {
69 » » panicf("cannot decode to nil")
70 » }
71 format, err := requestFormat(r.Header.Get(headerContentType)) 65 format, err := requestFormat(r.Header.Get(headerContentType))
72 if err != nil { 66 if err != nil {
73 // Spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html# sec10.4.16 67 // Spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html# sec10.4.16
74 return errorf(http.StatusUnsupportedMediaType, "Content-Type hea der: %s", err) 68 return errorf(http.StatusUnsupportedMediaType, "Content-Type hea der: %s", err)
75 } 69 }
76 70
77 buf, err := ioutil.ReadAll(r.Body) 71 buf, err := ioutil.ReadAll(r.Body)
78 if err != nil { 72 if err != nil {
79 return errorf(http.StatusBadRequest, "could not read body: %s", err) 73 return errorf(http.StatusBadRequest, "could not read body: %s", err)
80 } 74 }
81 if len(buf) == 0 {
82 // no body, no message
83 return nil
84 }
85
86 switch format { 75 switch format {
87 // Do not redefine "err" below. 76 // Do not redefine "err" below.
88 77
89 case formatBinary: 78 case formatBinary:
90 err = proto.Unmarshal(buf, msg) 79 err = proto.Unmarshal(buf, msg)
91 80
92 case formatJSONPB: 81 case formatJSONPB:
93 err = jsonpb.Unmarshal(bytes.NewBuffer(buf), msg) 82 err = jsonpb.Unmarshal(bytes.NewBuffer(buf), msg)
94 83
95 case formatText: 84 case formatText:
96 err = proto.UnmarshalText(string(buf), msg) 85 err = proto.UnmarshalText(string(buf), msg)
97 86
98 default: 87 default:
99 » » panicf("cannot happen") 88 » » err = fmt.Errorf("unknown message format: %d", format)
100 } 89 }
101 if err != nil { 90 if err != nil {
102 return errorf(http.StatusBadRequest, "could not decode body: %s" , err) 91 return errorf(http.StatusBadRequest, "could not decode body: %s" , err)
103 } 92 }
104 return nil 93 return nil
105 } 94 }
106 95
107 // parseHeader parses HTTP headers and derives a new context. 96 // parseHeader parses HTTP headers and derives a new context.
108 // Supports headerTimeout. 97 // Supports headerTimeout.
109 // Ignores "Accept" and "Content-Type" headers. 98 // Ignores "Accept" and "Content-Type" headers.
(...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after
157 } 146 }
158 md[trimmedName] = append(md[trimmedName], string (decoded)) 147 md[trimmedName] = append(md[trimmedName], string (decoded))
159 } 148 }
160 } 149 }
161 } 150 }
162 if addedMeta { 151 if addedMeta {
163 c = metadata.NewContext(c, md) 152 c = metadata.NewContext(c, md)
164 } 153 }
165 return c, nil 154 return c, nil
166 } 155 }
OLDNEW
« no previous file with comments | « server/prpc/auth.go ('k') | server/prpc/decoding_test.go » ('j') | server/prpc/encoding.go » ('J')

Powered by Google App Engine
This is Rietveld 408576698