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

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

Issue 1638493004: server/prpc: updated server according to protocol (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: 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
(Empty)
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
3 // found in the LICENSE file.
4
5 package prpc
6
7 import (
8 "net/http"
9 "strconv"
10
11 "golang.org/x/net/context"
12 "google.golang.org/grpc/codes"
13
14 "github.com/luci/luci-go/common/logging"
15 "github.com/luci/luci-go/common/prpc"
16 )
17
18 var newLineBytes = []byte{'\n'}
19
20 // response is a pRPC server response.
21 // All pRPC responses must be written using write.
22 type response struct {
23 code codes.Code // defaults to OK
24 status int // defaults to status derived from code.
25 contentType string // defaults to text/plain
dnj (Google) 2016/01/26 16:13:56 Shouldn't default. Have each method set this expli
nodir 2016/01/26 18:01:07 Done.
26 body []byte // must be non-nil, otherwise write will panic.
dnj (Google) 2016/01/26 16:13:56 Is an empty protobuf not acceptable? I see no need
nodir 2016/01/26 18:01:07 empty and nil are two different things, but ok bec
27 newLine bool // whether to write \n after body
28 }
29
30 // errResponse creates a response with an error.
31 func errResponse(code codes.Code, status int, msg string) *response {
32 return &response{
33 code: code,
34 status: status,
35 body: []byte(msg),
36 newLine: true,
37 }
38 }
39
40 // write writes r to w.
41 func (r *response) write(c context.Context, w http.ResponseWriter) {
42 if r.body == nil {
43 panicf("res.body is nil")
44 }
45
46 if r.code == codes.Internal || r.code == codes.Unknown {
dnj (Google) 2016/01/26 16:13:56 nit: Use switch here.
nodir 2016/01/26 18:01:07 so I counted lines and switch is 1 line more, but
47 // res.body is error message.
48 logging.Errorf(c, "%s error: %s", r.code, r.body)
dnj (Google) 2016/01/26 16:13:57 nit: include fields
nodir 2016/01/26 18:01:07 Done, also included http status
nodir 2016/01/26 18:08:13 actually did not include status
49 }
50
51 w.Header().Set(prpc.HeaderGRPCCode, strconv.Itoa(int(r.code)))
52
53 if r.contentType == "" {
54 w.Header().Set(headerContentType, "text/plain")
55 } else {
56 w.Header().Set(headerContentType, r.contentType)
57 }
58
59 if r.status == 0 {
60 w.WriteHeader(codeStatus(r.code))
61 } else {
62 w.WriteHeader(r.status)
63 }
64
65 var body []byte
dnj (Google) 2016/01/26 16:13:56 nit: merge this with the block above, no need to s
nodir 2016/01/26 18:01:07 Done.
66 if r.code == codes.Internal || r.code == codes.Unknown {
67 body = []byte("Internal Server Error")
68 } else {
69 body = r.body
70 }
71
72 if _, err := w.Write(body); err != nil {
73 logging.WithError(err).Errorf(c, "Could not respond")
74 // The header is already written. There is nothing more we can d o.
75 return
76 }
77 if r.newLine {
78 w.Write(newLineBytes)
79 }
80 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698