summaryrefslogtreecommitdiffhomepage
path: root/src/go/unit/response.go
diff options
context:
space:
mode:
authorIgor Sysoev <igor@sysoev.ru>2017-08-31 00:42:16 +0300
committerIgor Sysoev <igor@sysoev.ru>2017-08-31 00:42:16 +0300
commitf0e9e3ace94c82fab78ab1d4ee8c3042f3e94fdf (patch)
tree74fab38d3799598febc98aaabb0f5902d7dc5a0e /src/go/unit/response.go
parent61606835448554a7ee9a4431d732e1f2a9318376 (diff)
downloadunit-f0e9e3ace94c82fab78ab1d4ee8c3042f3e94fdf.tar.gz
unit-f0e9e3ace94c82fab78ab1d4ee8c3042f3e94fdf.tar.bz2
nginext has been renamed to unit.
Diffstat (limited to 'src/go/unit/response.go')
-rw-r--r--src/go/unit/response.go69
1 files changed, 69 insertions, 0 deletions
diff --git a/src/go/unit/response.go b/src/go/unit/response.go
new file mode 100644
index 00000000..18daa2f3
--- /dev/null
+++ b/src/go/unit/response.go
@@ -0,0 +1,69 @@
+/*
+ * Copyright (C) Max Romanov
+ * Copyright (C) NGINX, Inc.
+ */
+
+package unit
+
+/*
+#include "nxt_go_lib.h"
+*/
+import "C"
+
+import (
+ "fmt"
+ "net/http"
+ "os"
+)
+
+type response struct {
+ header http.Header
+ headerSent bool
+ req *http.Request
+ c_req C.nxt_go_request_t
+}
+
+func new_response(c_req C.nxt_go_request_t, req *http.Request) *response {
+ resp := &response{
+ header: http.Header{},
+ req: req,
+ c_req: c_req,
+ }
+
+ return resp
+}
+
+func (r *response) Header() http.Header {
+ return r.header
+}
+
+func (r *response) Write(p []byte) (n int, err error) {
+ if !r.headerSent {
+ r.WriteHeader(http.StatusOK)
+ }
+
+ l := C.size_t(len(p))
+ b := getCBytes(p)
+ res := C.nxt_go_response_write(r.c_req, b, l)
+ C.free(b)
+ return int(res), nil
+}
+
+func (r *response) WriteHeader(code int) {
+ if r.headerSent {
+ // Note: explicitly using Stderr, as Stdout is our HTTP output.
+ fmt.Fprintf(os.Stderr, "CGI attempted to write header twice")
+ return
+ }
+ r.headerSent = true
+ fmt.Fprintf(r, "%s %d %s\r\n", r.req.Proto, code, http.StatusText(code))
+
+ // Set a default Content-Type
+ if _, hasType := r.header["Content-Type"]; !hasType {
+ r.header.Add("Content-Type", "text/html; charset=utf-8")
+ }
+
+ r.header.Write(r)
+
+ r.Write([]byte("\r\n"))
+}