diff options
author | Max Romanov <max.romanov@nginx.com> | 2018-08-06 17:27:33 +0300 |
---|---|---|
committer | Max Romanov <max.romanov@nginx.com> | 2018-08-06 17:27:33 +0300 |
commit | 1bb22d1e922c87d3c86c67bdce626767ee48fb5c (patch) | |
tree | 6e067a82b309c3a0d0f592f037f26d886a7f8c13 /src/go/unit/request.go | |
parent | b6ce2da65c9c5229d744b2d964623b2d0f731ee9 (diff) | |
download | unit-1bb22d1e922c87d3c86c67bdce626767ee48fb5c.tar.gz unit-1bb22d1e922c87d3c86c67bdce626767ee48fb5c.tar.bz2 |
Unit application library.
Library now used in all language modules.
Old 'nxt_app_*' code removed.
See src/test/nxt_unit_app_test.c for usage sample.
Diffstat (limited to '')
-rw-r--r-- | src/go/unit/request.go | 51 |
1 files changed, 28 insertions, 23 deletions
diff --git a/src/go/unit/request.go b/src/go/unit/request.go index 7d99edaa..829a2c64 100644 --- a/src/go/unit/request.go +++ b/src/go/unit/request.go @@ -6,7 +6,7 @@ package unit /* -#include "nxt_go_lib.h" +#include "nxt_cgo_lib.h" */ import "C" @@ -20,15 +20,11 @@ import ( type request struct { req http.Request resp *response - c_req C.nxt_go_request_t - id C.uint32_t + c_req C.uintptr_t } func (r *request) Read(p []byte) (n int, err error) { - c := C.size_t(len(p)) - b := C.uintptr_t(uintptr(unsafe.Pointer(&p[0]))) - - res := C.nxt_go_request_read(r.c_req, b, c) + res := C.nxt_cgo_request_read(r.c_req, buf_ref(p), C.uint32_t(len(p))) if res == 0 && len(p) > 0 { return 0, io.EOF @@ -38,7 +34,7 @@ func (r *request) Read(p []byte) (n int, err error) { } func (r *request) Close() error { - C.nxt_go_request_close(r.c_req) + C.nxt_cgo_request_close(r.c_req) return nil } @@ -55,16 +51,16 @@ func (r *request) done() { if !resp.headerSent { resp.WriteHeader(http.StatusOK) } - C.nxt_go_request_done(r.c_req) + C.nxt_cgo_request_done(r.c_req, 0) } -func get_request(go_req C.nxt_go_request_t) *request { - return (*request)(unsafe.Pointer(uintptr(go_req))) +func get_request(go_req uintptr) *request { + return (*request)(unsafe.Pointer(go_req)) } -//export nxt_go_new_request -func nxt_go_new_request(c_req C.nxt_go_request_t, id C.uint32_t, - c_method *C.nxt_go_str_t, c_uri *C.nxt_go_str_t) uintptr { +//export nxt_go_request_create +func nxt_go_request_create(c_req C.uintptr_t, + c_method *C.nxt_cgo_str_t, c_uri *C.nxt_cgo_str_t) uintptr { uri := C.GoStringN(c_uri.start, c_uri.length) @@ -83,7 +79,6 @@ func nxt_go_new_request(c_req C.nxt_go_request_t, id C.uint32_t, RequestURI: uri, }, c_req: c_req, - id: id, } r.req.Body = r @@ -91,7 +86,7 @@ func nxt_go_new_request(c_req C.nxt_go_request_t, id C.uint32_t, } //export nxt_go_request_set_proto -func nxt_go_request_set_proto(go_req C.nxt_go_request_t, proto *C.nxt_go_str_t, +func nxt_go_request_set_proto(go_req uintptr, proto *C.nxt_cgo_str_t, maj C.int, min C.int) { r := get_request(go_req) @@ -101,8 +96,8 @@ func nxt_go_request_set_proto(go_req C.nxt_go_request_t, proto *C.nxt_go_str_t, } //export nxt_go_request_add_header -func nxt_go_request_add_header(go_req C.nxt_go_request_t, name *C.nxt_go_str_t, - value *C.nxt_go_str_t) { +func nxt_go_request_add_header(go_req uintptr, name *C.nxt_cgo_str_t, + value *C.nxt_cgo_str_t) { r := get_request(go_req) r.req.Header.Add(C.GoStringN(name.start, name.length), @@ -110,23 +105,33 @@ func nxt_go_request_add_header(go_req C.nxt_go_request_t, name *C.nxt_go_str_t, } //export nxt_go_request_set_content_length -func nxt_go_request_set_content_length(go_req C.nxt_go_request_t, l C.int64_t) { +func nxt_go_request_set_content_length(go_req uintptr, l C.int64_t) { get_request(go_req).req.ContentLength = int64(l) } //export nxt_go_request_set_host -func nxt_go_request_set_host(go_req C.nxt_go_request_t, host *C.nxt_go_str_t) { +func nxt_go_request_set_host(go_req uintptr, host *C.nxt_cgo_str_t) { get_request(go_req).req.Host = C.GoStringN(host.start, host.length) } //export nxt_go_request_set_url -func nxt_go_request_set_url(go_req C.nxt_go_request_t, scheme *C.char) { +func nxt_go_request_set_url(go_req uintptr, scheme *C.char) { get_request(go_req).req.URL.Scheme = C.GoString(scheme) } //export nxt_go_request_set_remote_addr -func nxt_go_request_set_remote_addr(go_req C.nxt_go_request_t, - addr *C.nxt_go_str_t) { +func nxt_go_request_set_remote_addr(go_req uintptr, addr *C.nxt_cgo_str_t) { get_request(go_req).req.RemoteAddr = C.GoStringN(addr.start, addr.length) } + +//export nxt_go_request_handler +func nxt_go_request_handler(go_req uintptr, h uintptr) { + r := get_request(go_req) + handler := *(*http.Handler)(unsafe.Pointer(h)) + + go func(r *request) { + handler.ServeHTTP(r.response(), &r.req) + r.done() + }(r) +} |