summaryrefslogtreecommitdiffhomepage
path: root/src/nxt_go.c
blob: b821ccedea2e1d9e8d786f6f853f88e9abf78378 (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
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196

/*
 * Copyright (C) Max Romanov
 * Copyright (C) NGINX, Inc.
 */

#include <nxt_main.h>
#include <nxt_application.h>


static nxt_int_t nxt_go_init(nxt_task_t *task, nxt_common_app_conf_t *conf);

static nxt_int_t nxt_go_prepare_msg(nxt_task_t *task,
                      nxt_app_request_t *r, nxt_app_wmsg_t *msg);

static nxt_int_t nxt_go_run(nxt_task_t *task,
                      nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *msg);

nxt_application_module_t  nxt_go_module = {
    nxt_go_init,
    nxt_go_prepare_msg,
    nxt_go_run
};


nxt_int_t
nxt_go_module_init(nxt_thread_t *thr, nxt_runtime_t *rt);

nxt_int_t
nxt_go_module_init(nxt_thread_t *thr, nxt_runtime_t *rt)
{
    nxt_app_modules[NXT_APP_GO] = &nxt_go_module;

    return NXT_OK;
}

extern char  **environ;

nxt_inline int
nxt_sock_no_cloexec(nxt_socket_t fd)
{
    if (fd == -1) {
        return 0;
    }
    return fcntl(fd, F_SETFD, 0);
}


static nxt_int_t
nxt_go_init(nxt_task_t *task, nxt_common_app_conf_t *conf)
{
    char               *go_path;
    char               *argv[2];
    u_char             buf[256];
    u_char             *p;
    u_char             stream_buf[32];
    nxt_port_t         *port;
    nxt_runtime_t      *rt;
    nxt_go_app_conf_t  *c;

    c = &conf->u.go;
    rt = task->thread->runtime;
    p = buf;

    nxt_runtime_port_each(rt, port) {

        if (port->pid != nxt_pid && port->type != NXT_PROCESS_MASTER) {
            continue;
        }

        if (port->pid == nxt_pid) {
            nxt_sprintf(stream_buf, stream_buf + sizeof(stream_buf),
                        "%uD", port->process->init->stream);

            setenv("NXT_GO_STREAM", (char *)stream_buf, 1);
        }

        nxt_debug(task, "port %PI, %ud, (%d, %d)", port->pid, port->id,
                  port->pair[0], port->pair[1]);

        p = nxt_sprintf(p, buf + sizeof(buf), "%PI,%ud,%d,%d,%d;",
                        port->pid, port->id, (int)port->type,
                        port->pair[0], port->pair[1]);

        if (nxt_slow_path(nxt_sock_no_cloexec(port->pair[0]))) {
            nxt_log(task, NXT_LOG_WARN, "fcntl() failed %E", nxt_errno);
        }

        if (nxt_slow_path(nxt_sock_no_cloexec(port->pair[1]))) {
            nxt_log(task, NXT_LOG_WARN, "fcntl() failed %E", nxt_errno);
        }

    } nxt_runtime_port_loop;

    *p = '\0';
    nxt_debug(task, "update NXT_GO_PORTS=%s", buf);

    setenv("NXT_GO_PORTS", (char *)buf, 1);

    go_path = malloc(c->executable.length + 1);
    nxt_memcpy(go_path, c->executable.start, c->executable.length);
    go_path[c->executable.length] = '\0';

    argv[0] = go_path;
    argv[1] = NULL;

    (void) execve(go_path, argv, environ);

    nxt_log(task, NXT_LOG_WARN, "execve(%s) failed %E", go_path, nxt_errno);

    return NXT_ERROR;
}


static nxt_int_t
nxt_go_prepare_msg(nxt_task_t *task, nxt_app_request_t *r, nxt_app_wmsg_t *wmsg)
{
    nxt_int_t                 rc;
    nxt_buf_t                 *b;
    nxt_http_field_t          *field;
    nxt_app_request_header_t  *h;

    static const nxt_str_t eof = nxt_null_string;

    h = &r->header;

#define RC(S)                                                                 \
    do {                                                                      \
        rc = (S);                                                             \
        if (nxt_slow_path(rc != NXT_OK)) {                                    \
            goto fail;                                                        \
        }                                                                     \
    } while(0)

#define NXT_WRITE(N)                                                          \
    RC(nxt_app_msg_write_str(task, wmsg, N))

    /* TODO error handle, async mmap buffer assignment */

    NXT_WRITE(&h->method);
    NXT_WRITE(&h->target);
    if (h->path.start == h->target.start) {
        NXT_WRITE(&eof);
    } else {
        NXT_WRITE(&h->path);
    }

    if (h->query.start != NULL) {
        RC(nxt_app_msg_write_size(task, wmsg,
                                  h->query.start - h->target.start + 1));
    } else {
        RC(nxt_app_msg_write_size(task, wmsg, 0));
    }

    NXT_WRITE(&h->version);

    NXT_WRITE(&h->host);
    NXT_WRITE(&h->cookie);
    NXT_WRITE(&h->content_type);
    NXT_WRITE(&h->content_length);

    RC(nxt_app_msg_write_size(task, wmsg, h->parsed_content_length));

    nxt_list_each(field, h->fields) {
        NXT_WRITE(&field->name);
        NXT_WRITE(&field->value);

    } nxt_list_loop;

    /* end-of-headers mark */
    NXT_WRITE(&eof);

    RC(nxt_app_msg_write_size(task, wmsg, r->body.preread_size));

    for(b = r->body.buf; b != NULL; b = b->next) {
        RC(nxt_app_msg_write_raw(task, wmsg, b->mem.pos,
                                 nxt_buf_mem_used_size(&b->mem)));
    }

#undef NXT_WRITE
#undef RC

    return NXT_OK;

fail:

    return NXT_ERROR;
}


static nxt_int_t
nxt_go_run(nxt_task_t *task,
           nxt_app_rmsg_t *rmsg, nxt_app_wmsg_t *msg)
{
    return NXT_ERROR;
}