summaryrefslogtreecommitdiffhomepage
path: root/src/nginext/nxt_go_port.c
blob: 47e46f02b1d50a9b6d76d528016ae20a1b7f7765 (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
197
198
199

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

#ifndef NXT_CONFIGURE


#include "nxt_go_port.h"
#include "nxt_go_log.h"
#include "nxt_go_process.h"
#include "nxt_go_run_ctx.h"

#include <nxt_main.h>
#include <nxt_go_gen.h>


#define nxt_go_str(p) ((nxt_go_str_t *)(p))

static nxt_go_request_t
nxt_go_data_handler(nxt_port_msg_t *port_msg, size_t size)
{
    size_t                    s;
    nxt_str_t                 n, v;
    nxt_int_t                 rc;
    nxt_uint_t                i;
    nxt_go_run_ctx_t          *ctx;
    nxt_go_request_t          r;
    nxt_app_request_header_t  *h;

    r = nxt_go_find_request(port_msg->stream);
    if (r != 0) {
        return r;
    }

    ctx = malloc(sizeof(nxt_go_run_ctx_t));
    nxt_go_ctx_init(ctx, port_msg, size - sizeof(nxt_port_msg_t));

    r = (nxt_go_request_t)(ctx);
    h = &ctx->r.header;

    nxt_go_ctx_read_str(ctx, &h->method);
    nxt_go_ctx_read_str(ctx, &h->target);
    nxt_go_ctx_read_str(ctx, &h->path);

    nxt_go_ctx_read_size(ctx, &s);
    if (s > 0) {
        s--;
        h->query.start = h->target.start + s;
        h->query.length = h->target.length - s;

        if (h->path.start == NULL) {
            h->path.start = h->target.start;
            h->path.length = s - 1;
        }
    }

    if (h->path.start == NULL) {
        h->path = h->target;
    }

    nxt_go_new_request(r, port_msg->stream, nxt_go_str(&h->method),
                       nxt_go_str(&h->target));

    nxt_go_ctx_read_str(ctx, &h->version);

    nxt_go_request_set_proto(r, nxt_go_str(&h->version),
                             h->version.start[5] - '0',
                             h->version.start[7] - '0');

    nxt_go_ctx_read_str(ctx, &h->host);
    nxt_go_ctx_read_str(ctx, &h->cookie);
    nxt_go_ctx_read_str(ctx, &h->content_type);
    nxt_go_ctx_read_str(ctx, &h->content_length);

    if (h->host.start != NULL) {
        nxt_go_request_set_host(r, nxt_go_str(&h->host));
    }

    nxt_go_ctx_read_size(ctx, &s);
    h->parsed_content_length = s;

    do {
        rc = nxt_go_ctx_read_str(ctx, &n);
        rc = nxt_go_ctx_read_str(ctx, &v);

        if (n.length == 0) {
            break;
        }

        nxt_go_request_add_header(r, nxt_go_str(&n), nxt_go_str(&v));
    } while(1);

    ctx->r.body.preread = v;

    if (h->parsed_content_length > 0) {
        nxt_go_request_set_content_length(r, h->parsed_content_length);
    }

    if (v.length < h->parsed_content_length) {
        nxt_go_request_create_channel(r);
    }

    nxt_go_request_serve(r);

    return r;
}

nxt_go_request_t
nxt_go_port_on_read(void *buf, size_t buf_size, void *oob, size_t oob_size)
{
    void                     *buf_end;
    void                     *payload;
    size_t                   payload_size;
    nxt_fd_t                 fd;
    struct cmsghdr           *cm;
    nxt_port_msg_t           *port_msg;
    nxt_port_msg_new_port_t  *new_port_msg;

    fd = -1;
    nxt_go_debug("on read: %d (%d)", (int)buf_size, (int)oob_size);

    cm = oob;
    if (oob_size >= CMSG_SPACE(sizeof(int))
        && cm->cmsg_len == CMSG_LEN(sizeof(int))
        && cm->cmsg_level == SOL_SOCKET
        && cm->cmsg_type == SCM_RIGHTS) {

        nxt_memcpy(&fd, CMSG_DATA(cm), sizeof(int));
        nxt_go_debug("fd = %d", fd);
    }

    port_msg = buf;
    if (buf_size < sizeof(nxt_port_msg_t)) {
        nxt_go_warn("message too small (%d bytes)", (int)buf_size);
        goto fail;
    }

    buf_end = ((char *)buf) + buf_size;

    payload = port_msg + 1;
    payload_size = buf_size - sizeof(nxt_port_msg_t);

    if (port_msg->mmap) {
        nxt_go_debug("using data in shared memory");
    }

    if (port_msg->type > NXT_PORT_MSG_MAX) {
        nxt_go_warn("unknown message type (%d)", (int)port_msg->type);
        goto fail;
    }

    switch (port_msg->type) {
    case NXT_PORT_MSG_QUIT:
        nxt_go_debug("quit");

        nxt_go_set_quit();
        break;

    case NXT_PORT_MSG_NEW_PORT:
        nxt_go_debug("new port");
        new_port_msg = payload;

        nxt_go_new_port(new_port_msg->pid, new_port_msg->id, new_port_msg->type,
            -1, fd);
        break;

    case NXT_PORT_MSG_CHANGE_FILE:
        nxt_go_debug("change file");
        break;

    case NXT_PORT_MSG_MMAP:
        nxt_go_debug("mmap");

        nxt_go_new_incoming_mmap(port_msg->pid, fd);
        break;

    case NXT_PORT_MSG_DATA:
        nxt_go_debug("data");

        return nxt_go_data_handler(port_msg, buf_size);

    default:
        goto fail;
    }


fail:

    if (fd != -1) {
        close(fd);
    }

    return 0;
}


#endif /* NXT_CONFIGURE */