Deleted Added
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_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
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
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
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
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
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 }
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
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
523nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
524 uintptr_t data, nxt_log_t *log)
525{
526 off_t length;
527 nxt_controller_request_t *r;
528
529 r = ctx;
530
531 length = nxt_off_t_parse(field->value.start, field->value.length);
532
533 if (nxt_fast_path(length > 0)) {
534 nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
535
536 r->length = length;
537 return NXT_OK;
538 }
539
540 nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
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 ---