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

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

Issue 1636873006: server/prpc: updated server according to protocol (Closed) Base URL: https://github.com/luci/luci-go@master
Patch Set: Respond to nits, escape externally-supplied strings when passed to format parameters. 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
« no previous file with comments | « server/prpc/method.go ('k') | server/prpc/response_test.go » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "fmt"
9 "net/http"
10 "strconv"
11 "strings"
12
13 "golang.org/x/net/context"
14 "google.golang.org/grpc/codes"
15
16 "github.com/luci/luci-go/common/logging"
17 "github.com/luci/luci-go/common/prpc"
18 )
19
20 var newLineBytes = []byte{'\n'}
21
22 // response is a pRPC server response.
23 // All pRPC responses must be written using write.
24 type response struct {
25 code codes.Code // defaults to OK
26 status int // defaults to status derived from code.
27 header http.Header
28 body []byte
29 }
30
31 // errResponse creates a response with an error.
32 func errResponse(code codes.Code, status int, format string, a ...interface{}) * response {
33 return &response{
34 code: code,
35 status: status,
36 header: http.Header{
37 headerContentType: []string{"text/plain"},
38 },
39 body: []byte(fmt.Sprintf(format+"\n", a...)),
40 }
41 }
42
43 // escapeFmt escapes format characters in a string destined for a format
44 // parameter. This is used to sanitize externally-supplied strings that are
45 // passed verbatim into errResponse.
46 func escapeFmt(s string) string {
47 return strings.Replace(s, "%", "%%", -1)
48 }
49
50 // write writes r to w.
51 func (r *response) write(c context.Context, w http.ResponseWriter) {
52 body := r.body
53 switch r.code {
54 case codes.Internal, codes.Unknown:
55 // res.body is error message.
56 logging.Fields{
57 "code": r.code,
58 }.Errorf(c, "%s", body)
59 body = []byte("Internal Server Error\n")
60 }
61
62 for h, vs := range r.header {
63 w.Header()[h] = vs
64 }
65 w.Header().Set(prpc.HeaderGRPCCode, strconv.Itoa(int(r.code)))
66
67 status := r.status
68 if status == 0 {
69 status = codeStatus(r.code)
70 }
71 w.WriteHeader(status)
72
73 if _, err := w.Write(body); err != nil {
74 logging.WithError(err).Errorf(c, "Could not respond")
75 // The header is already written. There is nothing more we can d o.
76 return
77 }
78 }
OLDNEW
« no previous file with comments | « server/prpc/method.go ('k') | server/prpc/response_test.go » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698