| OLD | NEW | 
| (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  | 
 |  12         "golang.org/x/net/context" | 
 |  13         "google.golang.org/grpc/codes" | 
 |  14  | 
 |  15         "github.com/luci/luci-go/common/logging" | 
 |  16         "github.com/luci/luci-go/common/prpc" | 
 |  17 ) | 
 |  18  | 
 |  19 var newLineBytes = []byte{'\n'} | 
 |  20  | 
 |  21 // response is a pRPC server response. | 
 |  22 // All pRPC responses must be written using write. | 
 |  23 type response struct { | 
 |  24         code    codes.Code // defaults to OK | 
 |  25         status  int        // defaults to status derived from code. | 
 |  26         header  http.Header | 
 |  27         body    []byte | 
 |  28         newLine bool // whether to write \n after body | 
 |  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, a...)), | 
 |  40                 newLine: true, | 
 |  41         } | 
 |  42 } | 
 |  43  | 
 |  44 // write writes r to w. | 
 |  45 func (r *response) write(c context.Context, w http.ResponseWriter) { | 
 |  46         body := r.body | 
 |  47         switch r.code { | 
 |  48         case codes.Internal, codes.Unknown: | 
 |  49                 // res.body is error message. | 
 |  50                 logging.Fields{ | 
 |  51                         "code": r.code, | 
 |  52                 }.Errorf(c, "%s", body) | 
 |  53                 body = []byte("Internal Server Error") | 
 |  54         } | 
 |  55  | 
 |  56         for h, vs := range r.header { | 
 |  57                 w.Header()[h] = vs | 
 |  58         } | 
 |  59         w.Header().Set(prpc.HeaderGRPCCode, strconv.Itoa(int(r.code))) | 
 |  60  | 
 |  61         status := r.status | 
 |  62         if status == 0 { | 
 |  63                 status = codeStatus(r.code) | 
 |  64         } | 
 |  65         w.WriteHeader(status) | 
 |  66  | 
 |  67         if _, err := w.Write(body); err != nil { | 
 |  68                 logging.WithError(err).Errorf(c, "Could not respond") | 
 |  69                 // The header is already written. There is nothing more we can d
    o. | 
 |  70                 return | 
 |  71         } | 
 |  72         if r.newLine { | 
 |  73                 w.Write(newLineBytes) | 
 |  74         } | 
 |  75 } | 
| OLD | NEW |