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

Unified Diff: server/prpc/response.go

Issue 1605363002: common/prpc, tools/cmd/cproto: prpc client (Closed) Base URL: https://chromium.googlesource.com/external/github.com/luci/luci-go@master
Patch Set: rebased and addressed comments 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 side-by-side diff with in-line comments
Download patch
« no previous file with comments | « server/prpc/method.go ('k') | server/prpc/response_test.go » ('j') | no next file with comments »
Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
Index: server/prpc/response.go
diff --git a/server/prpc/response.go b/server/prpc/response.go
new file mode 100644
index 0000000000000000000000000000000000000000..0d76aa9a68394914271da3e39476b5abd9e80de1
--- /dev/null
+++ b/server/prpc/response.go
@@ -0,0 +1,75 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+package prpc
+
+import (
+ "fmt"
+ "net/http"
+ "strconv"
+
+ "golang.org/x/net/context"
+ "google.golang.org/grpc/codes"
+
+ "github.com/luci/luci-go/common/logging"
+ "github.com/luci/luci-go/common/prpc"
+)
+
+var newLineBytes = []byte{'\n'}
+
+// response is a pRPC server response.
+// All pRPC responses must be written using write.
+type response struct {
+ code codes.Code // defaults to OK
+ status int // defaults to status derived from code.
+ header http.Header
+ body []byte
+ newLine bool // whether to write \n after body
+}
+
+// errResponse creates a response with an error.
+func errResponse(code codes.Code, status int, format string, a ...interface{}) *response {
+ return &response{
+ code: code,
+ status: status,
+ header: http.Header{
+ headerContentType: []string{"text/plain"},
+ },
+ body: []byte(fmt.Sprintf(format, a...)),
+ newLine: true,
+ }
+}
+
+// write writes r to w.
+func (r *response) write(c context.Context, w http.ResponseWriter) {
+ body := r.body
+ switch r.code {
+ case codes.Internal, codes.Unknown:
+ // res.body is error message.
+ logging.Fields{
+ "code": r.code,
+ }.Errorf(c, "%s", body)
+ body = []byte("Internal Server Error")
+ }
+
+ for h, vs := range r.header {
+ w.Header()[h] = vs
+ }
+ w.Header().Set(prpc.HeaderGRPCCode, strconv.Itoa(int(r.code)))
+
+ status := r.status
+ if status == 0 {
+ status = codeStatus(r.code)
+ }
+ w.WriteHeader(status)
+
+ if _, err := w.Write(body); err != nil {
+ logging.WithError(err).Errorf(c, "Could not respond")
+ // The header is already written. There is nothing more we can do.
+ return
+ }
+ if r.newLine {
+ w.Write(newLineBytes)
+ }
+}
« 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