nxt_controller.c (386:d9e23ae1617d) nxt_controller.c (417:47366bb40f2c)
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>

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

60static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
61 void *data);
62static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
63 void *data);
64static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
65static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
66
67static 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>

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

60static void nxt_controller_conn_write_error(nxt_task_t *task, void *obj,
61 void *data);
62static void nxt_controller_conn_write_timeout(nxt_task_t *task, void *obj,
63 void *data);
64static void nxt_controller_conn_close(nxt_task_t *task, void *obj, void *data);
65static void nxt_controller_conn_free(nxt_task_t *task, void *obj, void *data);
66
67static nxt_int_t nxt_controller_request_content_length(void *ctx,
68 nxt_http_field_t *field, nxt_log_t *log);
68 nxt_http_field_t *field, uintptr_t data);
69
70static void nxt_controller_process_request(nxt_task_t *task,
71 nxt_controller_request_t *req);
72static void nxt_controller_conf_handler(nxt_task_t *task,
73 nxt_port_recv_msg_t *msg, void *data);
74static void nxt_controller_conf_store(nxt_task_t *task,
75 nxt_conf_value_t *conf);
76static void nxt_controller_response(nxt_task_t *task,
77 nxt_controller_request_t *req, nxt_controller_response_t *resp);
78static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
79 struct tm *tm, size_t size, const char *format);
80
81
69
70static void nxt_controller_process_request(nxt_task_t *task,
71 nxt_controller_request_t *req);
72static void nxt_controller_conf_handler(nxt_task_t *task,
73 nxt_port_recv_msg_t *msg, void *data);
74static void nxt_controller_conf_store(nxt_task_t *task,
75 nxt_conf_value_t *conf);
76static void nxt_controller_response(nxt_task_t *task,
77 nxt_controller_request_t *req, nxt_controller_response_t *resp);
78static u_char *nxt_controller_date(u_char *buf, nxt_realtime_t *now,
79 struct tm *tm, size_t size, const char *format);
80
81
82static nxt_http_fields_hash_entry_t nxt_controller_request_fields[] = {
82static nxt_http_field_proc_t nxt_controller_request_fields[] = {
83 { nxt_string("Content-Length"),
84 &nxt_controller_request_content_length, 0 },
83 { nxt_string("Content-Length"),
84 &nxt_controller_request_content_length, 0 },
85
86 { nxt_null_string, NULL, 0 }
87};
88
85};
86
89static nxt_http_fields_hash_t *nxt_controller_fields_hash;
87static nxt_lvlhsh_t nxt_controller_fields_hash;
90
91static nxt_uint_t nxt_controller_listening;
92static nxt_controller_conf_t nxt_controller_conf;
93static nxt_queue_t nxt_controller_waiting_requests;
94
95
96static const nxt_event_conn_state_t nxt_controller_conn_read_state;
97static const nxt_event_conn_state_t nxt_controller_conn_body_read_state;

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

109 .rpc_ready = nxt_port_rpc_handler,
110 .rpc_error = nxt_port_rpc_handler,
111};
112
113
114nxt_int_t
115nxt_controller_start(nxt_task_t *task, void *data)
116{
88
89static nxt_uint_t nxt_controller_listening;
90static nxt_controller_conf_t nxt_controller_conf;
91static nxt_queue_t nxt_controller_waiting_requests;
92
93
94static const nxt_event_conn_state_t nxt_controller_conn_read_state;
95static const nxt_event_conn_state_t nxt_controller_conn_body_read_state;

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

107 .rpc_ready = nxt_port_rpc_handler,
108 .rpc_error = nxt_port_rpc_handler,
109};
110
111
112nxt_int_t
113nxt_controller_start(nxt_task_t *task, void *data)
114{
117 nxt_mp_t *mp;
118 nxt_int_t ret;
119 nxt_str_t *json;
120 nxt_runtime_t *rt;
121 nxt_conf_value_t *conf;
122 nxt_event_engine_t *engine;
123 nxt_conf_validation_t vldt;
124 nxt_http_fields_hash_t *hash;
115 nxt_mp_t *mp;
116 nxt_int_t ret;
117 nxt_str_t *json;
118 nxt_runtime_t *rt;
119 nxt_conf_value_t *conf;
120 nxt_event_engine_t *engine;
121 nxt_conf_validation_t vldt;
125
126 rt = task->thread->runtime;
127
128 engine = task->thread->engine;
129
130 engine->mem_pool = nxt_mp_create(4096, 128, 1024, 64);
131 if (nxt_slow_path(engine->mem_pool == NULL)) {
132 return NXT_ERROR;
133 }
134
122
123 rt = task->thread->runtime;
124
125 engine = task->thread->engine;
126
127 engine->mem_pool = nxt_mp_create(4096, 128, 1024, 64);
128 if (nxt_slow_path(engine->mem_pool == NULL)) {
129 return NXT_ERROR;
130 }
131
135 hash = nxt_http_fields_hash_create(nxt_controller_request_fields,
136 rt->mem_pool);
137 if (nxt_slow_path(hash == NULL)) {
132 ret = nxt_http_fields_hash(&nxt_controller_fields_hash, rt->mem_pool,
133 nxt_controller_request_fields,
134 nxt_nitems(nxt_controller_request_fields));
135
136 if (nxt_slow_path(ret != NXT_OK)) {
138 return NXT_ERROR;
139 }
140
137 return NXT_ERROR;
138 }
139
141 nxt_controller_fields_hash = hash;
142 nxt_queue_init(&nxt_controller_waiting_requests);
143
144 json = data;
145
146 if (json->length == 0) {
147 return NXT_OK;
148 }
149

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

424
425 if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
426 != NXT_OK))
427 {
428 nxt_controller_conn_free(task, c, NULL);
429 return;
430 }
431
140 nxt_queue_init(&nxt_controller_waiting_requests);
141
142 json = data;
143
144 if (json->length == 0) {
145 return NXT_OK;
146 }
147

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

422
423 if (nxt_slow_path(nxt_http_parse_request_init(&r->parser, c->mem_pool)
424 != NXT_OK))
425 {
426 nxt_controller_conn_free(task, c, NULL);
427 return;
428 }
429
432 r->parser.fields_hash = nxt_controller_fields_hash;
433
434 b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
435 if (nxt_slow_path(b == NULL)) {
436 nxt_controller_conn_free(task, c, NULL);
437 return;
438 }
439
440 c->read = b;
441 c->socket.data = r;

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

500 /* rc == NXT_ERROR */
501
502 nxt_log(task, NXT_LOG_ERR, "parsing error");
503
504 nxt_controller_conn_close(task, c, r);
505 return;
506 }
507
430 b = nxt_buf_mem_alloc(c->mem_pool, 1024, 0);
431 if (nxt_slow_path(b == NULL)) {
432 nxt_controller_conn_free(task, c, NULL);
433 return;
434 }
435
436 c->read = b;
437 c->socket.data = r;

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

496 /* rc == NXT_ERROR */
497
498 nxt_log(task, NXT_LOG_ERR, "parsing error");
499
500 nxt_controller_conn_close(task, c, r);
501 return;
502 }
503
508 rc = nxt_http_fields_process(r->parser.fields, r, task->log);
504 rc = nxt_http_fields_process(r->parser.fields, &nxt_controller_fields_hash,
505 r);
509
510 if (nxt_slow_path(rc != NXT_OK)) {
511 nxt_controller_conn_close(task, c, r);
512 return;
513 }
514
515 preread = nxt_buf_mem_used_size(&b->mem);
516

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

722 nxt_sockaddr_cache_free(task->thread->engine, c);
723
724 nxt_conn_free(task, c);
725}
726
727
728static nxt_int_t
729nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
506
507 if (nxt_slow_path(rc != NXT_OK)) {
508 nxt_controller_conn_close(task, c, r);
509 return;
510 }
511
512 preread = nxt_buf_mem_used_size(&b->mem);
513

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

719 nxt_sockaddr_cache_free(task->thread->engine, c);
720
721 nxt_conn_free(task, c);
722}
723
724
725static nxt_int_t
726nxt_controller_request_content_length(void *ctx, nxt_http_field_t *field,
730 nxt_log_t *log)
727 uintptr_t data)
731{
732 off_t length;
733 nxt_controller_request_t *r;
734
735 r = ctx;
736
728{
729 off_t length;
730 nxt_controller_request_t *r;
731
732 r = ctx;
733
737 length = nxt_off_t_parse(field->value.start, field->value.length);
734 length = nxt_off_t_parse(field->value, field->value_length);
738
739 if (nxt_fast_path(length > 0)) {
740
741 if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
735
736 if (nxt_fast_path(length > 0)) {
737
738 if (nxt_slow_path(length > NXT_SIZE_T_MAX)) {
742 nxt_log_error(NXT_LOG_ERR, log, "Content-Length is too big");
739 nxt_log_error(NXT_LOG_ERR, &r->conn->log,
740 "Content-Length is too big");
743 return NXT_ERROR;
744 }
745
746 r->length = length;
747 return NXT_OK;
748 }
749
741 return NXT_ERROR;
742 }
743
744 r->length = length;
745 return NXT_OK;
746 }
747
750 nxt_log_error(NXT_LOG_ERR, log, "Content-Length is invalid");
748 nxt_log_error(NXT_LOG_ERR, &r->conn->log, "Content-Length is invalid");
751
752 return NXT_ERROR;
753}
754
755
756static void
757nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
758{

--- 529 unchanged lines hidden ---
749
750 return NXT_ERROR;
751}
752
753
754static void
755nxt_controller_process_request(nxt_task_t *task, nxt_controller_request_t *req)
756{

--- 529 unchanged lines hidden ---