nxt_controller.c (56:92b4984ca3c1) nxt_controller.c (60:b80bfbd9bddc)
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Valentin V. Bartenev
5 * Copyright (C) NGINX, Inc.
6 */
7
8#include <nxt_main.h>

--- 37 unchanged lines hidden (view full) ---

46static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
47 void *data);
48static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
49 void *data);
50static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
51static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
52
53static nxt_int_t nxt_controller_request_content_length(void *ctx,
1
2/*
3 * Copyright (C) Igor Sysoev
4 * Copyright (C) Valentin V. Bartenev
5 * Copyright (C) NGINX, Inc.
6 */
7
8#include <nxt_main.h>

--- 37 unchanged lines hidden (view full) ---

46static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
47 void *data);
48static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
49 void *data);
50static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
51static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
52
53static nxt_int_t nxt_controller_request_content_length(void *ctx,
54 nxt_str_t *name, nxt_str_t *value, uintptr_t data);
54 nxt_http_field_t *field, uintptr_t data, nxt_log_t *log);
55
56static void nxt_controller_process_request(nxt_task_t *task,
57 nxt_event_conn_t *c, nxt_controller_request_t *r);
58static nxt_int_t nxt_controller_response(nxt_task_t *task, nxt_event_conn_t *c,
59 nxt_controller_response_t *resp);
60static nxt_buf_t *nxt_controller_response_body(nxt_controller_response_t *resp,
61 nxt_mem_pool_t *pool);
62
63
55
56static void nxt_controller_process_request(nxt_task_t *task,
57 nxt_event_conn_t *c, nxt_controller_request_t *r);
58static nxt_int_t nxt_controller_response(nxt_task_t *task, nxt_event_conn_t *c,
59 nxt_controller_response_t *resp);
60static nxt_buf_t *nxt_controller_response_body(nxt_controller_response_t *resp,
61 nxt_mem_pool_t *pool);
62
63
64static nxt_http_fields_t nxt_controller_request_fields[] = {
64static nxt_http_fields_hash_entry_t nxt_controller_request_fields[] = {
65 { nxt_string("Content-Length"),
66 &nxt_controller_request_content_length, 0 },
67
68 { nxt_null_string, NULL, 0 }
69};
70
65 { nxt_string("Content-Length"),
66 &nxt_controller_request_content_length, 0 },
67
68 { nxt_null_string, NULL, 0 }
69};
70
71static nxt_http_fields_hash_t *nxt_controller_request_fields_hash;
71static nxt_http_fields_hash_t *nxt_controller_fields_hash;
72
73
74static nxt_controller_conf_t nxt_controller_conf;
75
76
77static const nxt_event_conn_state_t nxt_controller_conn_read_state;
78static const nxt_event_conn_state_t nxt_controller_conn_body_read_state;
79static const nxt_event_conn_state_t nxt_controller_conn_write_state;

--- 5 unchanged lines hidden (view full) ---

85{
86 nxt_mem_pool_t *mp;
87 nxt_conf_json_value_t *conf;
88 nxt_http_fields_hash_t *hash;
89
90 static const nxt_str_t json
91 = nxt_string("{ \"sockets\": {}, \"applications\": {} }");
92
72
73
74static nxt_controller_conf_t nxt_controller_conf;
75
76
77static const nxt_event_conn_state_t nxt_controller_conn_read_state;
78static const nxt_event_conn_state_t nxt_controller_conn_body_read_state;
79static const nxt_event_conn_state_t nxt_controller_conn_write_state;

--- 5 unchanged lines hidden (view full) ---

85{
86 nxt_mem_pool_t *mp;
87 nxt_conf_json_value_t *conf;
88 nxt_http_fields_hash_t *hash;
89
90 static const nxt_str_t json
91 = nxt_string("{ \"sockets\": {}, \"applications\": {} }");
92
93 hash = nxt_http_fields_hash(nxt_controller_request_fields, rt->mem_pool);
94
93 hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
94 rt->mem_pool);
95 if (nxt_slow_path(hash == NULL)) {
96 return NXT_ERROR;
97 }
98
95 if (nxt_slow_path(hash == NULL)) {
96 return NXT_ERROR;
97 }
98
99 nxt_controller_request_fields_hash = hash;
99 nxt_controller_fields_hash = hash;
100
101 if (nxt_listen_event(task, rt->controller_socket) == NULL) {
102 return NXT_ERROR;
103 }
104
105 mp = nxt_mem_pool_create(256);
106
107 if (nxt_slow_path(mp == NULL)) {

--- 102 unchanged lines hidden (view full) ---

210 nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
211
212 r = nxt_mem_zalloc(c->mem_pool, sizeof(nxt_controller_request_t));
213 if (nxt_slow_path(r == NULL)) {
214 nxt_controller_conn_free(task, c, NULL);
215 return;
216 }
217
100
101 if (nxt_listen_event(task, rt->controller_socket) == NULL) {
102 return NXT_ERROR;
103 }
104
105 mp = nxt_mem_pool_create(256);
106
107 if (nxt_slow_path(mp == NULL)) {

--- 102 unchanged lines hidden (view full) ---

210 nxt_debug(task, "controller conn init fd:%d", c->socket.fd);
211
212 r = nxt_mem_zalloc(c->mem_pool, sizeof(nxt_controller_request_t));
213 if (nxt_slow_path(r == NULL)) {
214 nxt_controller_conn_free(task, c, NULL);
215 return;
216 }
217
218 r->parser.hash = nxt_controller_request_fields_hash;
219 r->parser.ctx = r;
218 if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
219 != NXT_OK))
220 {
221 nxt_controller_conn_free(task, c, NULL);
222 return;
223 }
220
221 b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
222 if (nxt_slow_path(b == NULL)) {
223 nxt_controller_conn_free(task, c, NULL);
224 return;
225 }
226
227 c->read = b;

--- 59 unchanged lines hidden (view full) ---

287 /* rc == NXT_ERROR */
288
289 nxt_log(task, NXT_LOG_ERR, "parsing error");
290
291 nxt_controller_conn_close(task, c, r);
292 return;
293 }
294
224
225 b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
226 if (nxt_slow_path(b == NULL)) {
227 nxt_controller_conn_free(task, c, NULL);
228 return;
229 }
230
231 c->read = b;

--- 59 unchanged lines hidden (view full) ---

291 /* rc == NXT_ERROR */
292
293 nxt_log(task, NXT_LOG_ERR, "parsing error");
294
295 nxt_controller_conn_close(task, c, r);
296 return;
297 }
298
299 rc = nxt_http_fields_process(r->parser.fields, nxt_controller_fields_hash,
300 r, task->log);
301
302 if (nxt_slow_path(rc != NXT_OK)) {
303 nxt_controller_conn_close(task, c, r);
304 return;
305 }
306
295 preread = nxt_buf_mem_used_size(&b->mem);
296
297 nxt_debug(task, "controller request header parsing complete, "
298 "body length: %O, preread: %uz",
299 r->length, preread);
300
301 if (preread >= r->length) {
302 nxt_controller_process_request(task, c, r);

--- 200 unchanged lines hidden (view full) ---

503
504 nxt_mem_pool_destroy(c->mem_pool);
505
506 //nxt_free(c);
507}
508
509
510static nxt_int_t
307 preread = nxt_buf_mem_used_size(&b->mem);
308
309 nxt_debug(task, "controller request header parsing complete, "
310 "body length: %O, preread: %uz",
311 r->length, preread);
312
313 if (preread >= r->length) {
314 nxt_controller_process_request(task, c, r);

--- 200 unchanged lines hidden (view full) ---

515
516 nxt_mem_pool_destroy(c->mem_pool);
517
518 //nxt_free(c);
519}
520
521
522static nxt_int_t
511nxt_controller_request_content_length(void *ctx, nxt_str_t *name,
512 nxt_str_t *value, uintptr_t data)
523nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
524 uintptr_t data, nxt_log_t *log)
513{
514 off_t length;
515 nxt_controller_request_t *r;
516
517 r = ctx;
518
525{
526 off_t length;
527 nxt_controller_request_t *r;
528
529 r = ctx;
530
519 length = nxt_off_t_parse(value->start, value->length);
531 length = nxt_off_t_parse(field->value.start, field->value.length);
520
521 if (nxt_fast_path(length > 0)) {
532
533 if (nxt_fast_path(length > 0)) {
522 /* TODO length too big */
534 nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
523
524 r->length = length;
525 return NXT_OK;
526 }
527
535
536 r->length = length;
537 return NXT_OK;
538 }
539
528 /* TODO logging (task?) */
540 nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
529
530 return NXT_ERROR;
531}
532
533
534static void
535nxt_controller_process_request(nxt_task_t *task, nxt_event_conn_t *c,
536 nxt_controller_request_t *req)

--- 265 unchanged lines hidden ---
541
542 return NXT_ERROR;
543}
544
545
546static void
547nxt_controller_process_request(nxt_task_t *task, nxt_event_conn_t *c,
548 nxt_controller_request_t *req)

--- 265 unchanged lines hidden ---