summaryrefslogtreecommitdiffhomepage
path: root/go/request.go
blob: 1d8c6702ceb332202050d5272f6e8b7e8664a024 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
/*
 * Copyright (C) Max Romanov
 * Copyright (C) NGINX, Inc.
 */

package unit

/*
#include "nxt_cgo_lib.h"
*/
import "C"

import (
	"io"
	"net/http"
	"net/url"
	"crypto/tls"
	"unsafe"
)

type request struct {
	req   http.Request
	resp  *response
	c_req C.uintptr_t
}

func (r *request) Read(p []byte) (n int, err error) {
	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
	}

	return int(res), nil
}

func (r *request) Close() error {
	C.nxt_cgo_request_close(r.c_req)
	return nil
}

func (r *request) response() *response {
	if r.resp == nil {
		r.resp = new_response(r.c_req, &r.req)
	}

	return r.resp
}

func (r *request) done() {
	resp := r.response()
	if !resp.headerSent {
		resp.WriteHeader(http.StatusOK)
	}
	C.nxt_cgo_request_done(r.c_req, 0)
}

func get_request(go_req uintptr) *request {
	return (*request)(unsafe.Pointer(go_req))
}

//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)

	var URL *url.URL
	var err error
	if URL, err = url.ParseRequestURI(uri); err != nil {
		return 0
	}

	r := &request{
		req: http.Request{
			Method:     C.GoStringN(c_method.start, c_method.length),
			URL:        URL,
			Header:     http.Header{},
			Body:       nil,
			RequestURI: uri,
		},
		c_req: c_req,
	}
	r.req.Body = r

	return uintptr(unsafe.Pointer(r))
}

//export nxt_go_request_set_proto
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)
	r.req.Proto = C.GoStringN(proto.start, proto.length)
	r.req.ProtoMajor = int(maj)
	r.req.ProtoMinor = int(min)
}

//export nxt_go_request_add_header
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),
		C.GoStringN(value.start, value.length))
}

//export nxt_go_request_set_content_length
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 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 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 uintptr, addr *C.nxt_cgo_str_t) {

	get_request(go_req).req.RemoteAddr = C.GoStringN(addr.start, addr.length)
}

//export nxt_go_request_set_tls
func nxt_go_request_set_tls(go_req uintptr) {

	get_request(go_req).req.TLS = &tls.ConnectionState{ }
}

//export nxt_go_request_handler
func nxt_go_request_handler(go_req uintptr, h uintptr) {
	r := get_request(go_req)
	handler := get_handler(h)

	go func(r *request) {
		handler.ServeHTTP(r.response(), &r.req)
		r.done()
	}(r)
}