Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 57 case formatBinary, formatJSONPB, formatText: | 57 case formatBinary, formatJSONPB, formatText: |
| 58 return f, nil | 58 return f, nil |
| 59 | 59 |
| 60 default: | 60 default: |
| 61 panic("cannot happen") | 61 panic("cannot happen") |
| 62 } | 62 } |
| 63 } | 63 } |
| 64 | 64 |
| 65 // readMessage decodes a protobuf message from an HTTP request. | 65 // readMessage decodes a protobuf message from an HTTP request. |
| 66 // Does not close the request body. | 66 // Does not close the request body. |
| 67 func readMessage(r *http.Request, msg proto.Message) *httpError { | 67 func readMessage(r *http.Request, msg proto.Message) *protocolError { |
| 68 if msg == nil { | 68 if msg == nil { |
| 69 » » panicf("cannot decode to nil") | 69 » » panic("cannot decode to nil") |
| 70 } | 70 } |
| 71 format, err := requestFormat(r.Header.Get(headerContentType)) | 71 format, err := requestFormat(r.Header.Get(headerContentType)) |
| 72 if err != nil { | 72 if err != nil { |
| 73 // Spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html# sec10.4.16 | 73 // Spec: http://www.w3.org/Protocols/rfc2616/rfc2616-sec10.html# sec10.4.16 |
| 74 return errorf(http.StatusUnsupportedMediaType, "Content-Type hea der: %s", err) | 74 return errorf(http.StatusUnsupportedMediaType, "Content-Type hea der: %s", err) |
| 75 } | 75 } |
| 76 | 76 |
| 77 buf, err := ioutil.ReadAll(r.Body) | 77 buf, err := ioutil.ReadAll(r.Body) |
| 78 if err != nil { | 78 if err != nil { |
| 79 return errorf(http.StatusBadRequest, "could not read body: %s", err) | 79 return errorf(http.StatusBadRequest, "could not read body: %s", err) |
| 80 } | 80 } |
| 81 if len(buf) == 0 { | 81 if len(buf) == 0 { |
| 82 // no body, no message | 82 // no body, no message |
|
dnj
2016/01/26 19:26:38
Is this true? A completely zero-value protobuf wou
| |
| 83 return nil | 83 return nil |
| 84 } | 84 } |
| 85 | 85 |
| 86 switch format { | 86 switch format { |
| 87 // Do not redefine "err" below. | 87 // Do not redefine "err" below. |
| 88 | 88 |
| 89 case formatBinary: | 89 case formatBinary: |
| 90 err = proto.Unmarshal(buf, msg) | 90 err = proto.Unmarshal(buf, msg) |
| 91 | 91 |
| 92 case formatJSONPB: | 92 case formatJSONPB: |
| 93 err = jsonpb.Unmarshal(bytes.NewBuffer(buf), msg) | 93 err = jsonpb.Unmarshal(bytes.NewBuffer(buf), msg) |
| 94 | 94 |
| 95 case formatText: | 95 case formatText: |
| 96 err = proto.UnmarshalText(string(buf), msg) | 96 err = proto.UnmarshalText(string(buf), msg) |
| 97 | 97 |
| 98 default: | 98 default: |
| 99 » » panicf("cannot happen") | 99 » » panic("cannot happen") |
|
dnj
2016/01/26 19:26:38
nit: I'd prefer that you either don't check this c
| |
| 100 } | 100 } |
| 101 if err != nil { | 101 if err != nil { |
| 102 return errorf(http.StatusBadRequest, "could not decode body: %s" , err) | 102 return errorf(http.StatusBadRequest, "could not decode body: %s" , err) |
| 103 } | 103 } |
| 104 return nil | 104 return nil |
| 105 } | 105 } |
| 106 | 106 |
| 107 // parseHeader parses HTTP headers and derives a new context. | 107 // parseHeader parses HTTP headers and derives a new context. |
| 108 // Supports headerTimeout. | 108 // Supports headerTimeout. |
| 109 // Ignores "Accept" and "Content-Type" headers. | 109 // Ignores "Accept" and "Content-Type" headers. |
| (...skipping 47 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 157 } | 157 } |
| 158 md[trimmedName] = append(md[trimmedName], string (decoded)) | 158 md[trimmedName] = append(md[trimmedName], string (decoded)) |
| 159 } | 159 } |
| 160 } | 160 } |
| 161 } | 161 } |
| 162 if addedMeta { | 162 if addedMeta { |
| 163 c = metadata.NewContext(c, md) | 163 c = metadata.NewContext(c, md) |
| 164 } | 164 } |
| 165 return c, nil | 165 return c, nil |
| 166 } | 166 } |
| OLD | NEW |