Refactored HTTP protocol callback table.

This commit is contained in:
Igor Sysoev
2019-08-06 15:29:39 +03:00
parent fdf570f1d9
commit 17bb22a4e4
4 changed files with 48 additions and 83 deletions

View File

@@ -89,52 +89,19 @@ static const nxt_conn_state_t nxt_h1p_keepalive_state;
static const nxt_conn_state_t nxt_h1p_close_state; static const nxt_conn_state_t nxt_h1p_close_state;
const nxt_http_proto_body_read_t nxt_http_proto_body_read[3] = { const nxt_http_proto_table_t nxt_http_proto[3] = {
nxt_h1p_request_body_read, /* NXT_HTTP_PROTO_H1 */
NULL, {
NULL, .body_read = nxt_h1p_request_body_read,
}; .local_addr = nxt_h1p_request_local_addr,
.header_send = nxt_h1p_request_header_send,
.send = nxt_h1p_request_send,
const nxt_http_proto_local_addr_t nxt_http_proto_local_addr[3] = { .body_bytes_sent = nxt_h1p_request_body_bytes_sent,
nxt_h1p_request_local_addr, .discard = nxt_h1p_request_discard,
NULL, .close = nxt_h1p_request_close,
NULL, },
}; /* NXT_HTTP_PROTO_H2 */
/* NXT_HTTP_PROTO_DEVNULL */
const nxt_http_proto_header_send_t nxt_http_proto_header_send[3] = {
nxt_h1p_request_header_send,
NULL,
NULL,
};
const nxt_http_proto_send_t nxt_http_proto_send[3] = {
nxt_h1p_request_send,
NULL,
NULL,
};
const nxt_http_proto_body_bytes_sent_t nxt_http_proto_body_bytes_sent[3] = {
nxt_h1p_request_body_bytes_sent,
NULL,
NULL,
};
const nxt_http_proto_discard_t nxt_http_proto_discard[3] = {
nxt_h1p_request_discard,
NULL,
NULL,
};
const nxt_http_proto_close_t nxt_http_proto_close[3] = {
nxt_h1p_request_close,
NULL,
NULL,
}; };
@@ -438,6 +405,7 @@ nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data)
h1p->request = r; h1p->request = r;
r->proto.h1 = h1p; r->proto.h1 = h1p;
/* r->protocol = NXT_HTTP_PROTO_H1 is done by zeroing. */
r->remote = c->remote; r->remote = c->remote;
#if (NXT_TLS) #if (NXT_TLS)

View File

@@ -46,6 +46,13 @@ typedef enum {
} nxt_http_te_t; } nxt_http_te_t;
typedef enum {
NXT_HTTP_PROTO_H1 = 0,
NXT_HTTP_PROTO_H2,
NXT_HTTP_PROTO_DEVNULL,
} nxt_http_protocol_t;
typedef struct { typedef struct {
nxt_work_handler_t ready_handler; nxt_work_handler_t ready_handler;
nxt_work_handler_t error_handler; nxt_work_handler_t error_handler;
@@ -145,7 +152,7 @@ struct nxt_http_request_s {
nxt_http_status_t status:16; nxt_http_status_t status:16;
uint8_t pass_count; /* 8 bits */ uint8_t pass_count; /* 8 bits */
uint8_t protocol; /* 2 bits */ nxt_http_protocol_t protocol:8; /* 2 bits */
uint8_t logged; /* 1 bit */ uint8_t logged; /* 1 bit */
uint8_t header_sent; /* 1 bit */ uint8_t header_sent; /* 1 bit */
uint8_t error; /* 1 bit */ uint8_t error; /* 1 bit */
@@ -168,20 +175,16 @@ struct nxt_http_pass_s {
}; };
typedef void (*nxt_http_proto_body_read_t)(nxt_task_t *task, typedef struct {
nxt_http_request_t *r); void (*body_read)(nxt_task_t *task, nxt_http_request_t *r);
typedef void (*nxt_http_proto_local_addr_t)(nxt_task_t *task, void (*local_addr)(nxt_task_t *task, nxt_http_request_t *r);
nxt_http_request_t *r); void (*header_send)(nxt_task_t *task, nxt_http_request_t *r);
typedef void (*nxt_http_proto_header_send_t)(nxt_task_t *task, void (*send)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out);
nxt_http_request_t *r); nxt_off_t (*body_bytes_sent)(nxt_task_t *task, nxt_http_proto_t proto);
typedef void (*nxt_http_proto_send_t)(nxt_task_t *task, nxt_http_request_t *r, void (*discard)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *last);
nxt_buf_t *out); void (*close)(nxt_task_t *task, nxt_http_proto_t proto,
typedef nxt_off_t (*nxt_http_proto_body_bytes_sent_t)(nxt_task_t *task, nxt_socket_conf_joint_t *joint);
nxt_http_proto_t proto); } nxt_http_proto_table_t;
typedef void (*nxt_http_proto_discard_t)(nxt_task_t *task,
nxt_http_request_t *r, nxt_buf_t *last);
typedef void (*nxt_http_proto_close_t)(nxt_task_t *task,
nxt_http_proto_t proto, nxt_socket_conf_joint_t *joint);
nxt_int_t nxt_http_init(nxt_task_t *task, nxt_runtime_t *rt); nxt_int_t nxt_http_init(nxt_task_t *task, nxt_runtime_t *rt);
@@ -225,13 +228,7 @@ extern nxt_time_string_t nxt_http_date_cache;
extern nxt_lvlhsh_t nxt_response_fields_hash; extern nxt_lvlhsh_t nxt_response_fields_hash;
extern const nxt_http_proto_body_read_t nxt_http_proto_body_read[]; extern const nxt_http_proto_table_t nxt_http_proto[];
extern const nxt_http_proto_local_addr_t nxt_http_proto_local_addr[];
extern const nxt_http_proto_header_send_t nxt_http_proto_header_send[];
extern const nxt_http_proto_send_t nxt_http_proto_send[];
extern const nxt_http_proto_body_bytes_sent_t nxt_http_proto_body_bytes_sent[];
extern const nxt_http_proto_discard_t nxt_http_proto_discard[];
extern const nxt_http_proto_close_t nxt_http_proto_close[];
#endif /* _NXT_HTTP_H_INCLUDED_ */ #endif /* _NXT_HTTP_H_INCLUDED_ */

View File

@@ -355,8 +355,8 @@ nxt_http_request_application(nxt_task_t *task, nxt_http_request_t *r,
static void static void
nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r) nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r)
{ {
if (r->proto.any != NULL) { if (nxt_fast_path(r->proto.any != NULL)) {
nxt_http_proto_local_addr[r->protocol](task, r); nxt_http_proto[r->protocol].local_addr(task, r);
} }
} }
@@ -364,8 +364,8 @@ nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r)
void void
nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r) nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r)
{ {
if (r->proto.any != NULL) { if (nxt_fast_path(r->proto.any != NULL)) {
nxt_http_proto_body_read[r->protocol](task, r); nxt_http_proto[r->protocol].body_read(task, r);
} }
} }
@@ -431,8 +431,8 @@ nxt_http_request_header_send(nxt_task_t *task, nxt_http_request_t *r)
r->resp.content_length = content_length; r->resp.content_length = content_length;
} }
if (r->proto.any != NULL) { if (nxt_fast_path(r->proto.any != NULL)) {
nxt_http_proto_header_send[r->protocol](task, r); nxt_http_proto[r->protocol].header_send(task, r);
} }
return; return;
@@ -446,8 +446,8 @@ fail:
void void
nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out)
{ {
if (r->proto.any != NULL) { if (nxt_fast_path(r->proto.any != NULL)) {
nxt_http_proto_send[r->protocol](task, r, out); nxt_http_proto[r->protocol].send(task, r, out);
} }
} }
@@ -524,8 +524,8 @@ nxt_http_request_error_handler(nxt_task_t *task, void *obj, void *data)
r->error = 1; r->error = 1;
if (proto.any != NULL) { if (nxt_fast_path(proto.any != NULL)) {
nxt_http_proto_discard[r->protocol](task, r, nxt_http_buf_last(r)); nxt_http_proto[r->protocol].discard(task, r, nxt_http_buf_last(r));
} }
} }
@@ -535,7 +535,7 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
{ {
nxt_http_proto_t proto; nxt_http_proto_t proto;
nxt_http_request_t *r; nxt_http_request_t *r;
nxt_http_proto_close_t handler; nxt_http_protocol_t protocol;
nxt_socket_conf_joint_t *conf; nxt_socket_conf_joint_t *conf;
nxt_router_access_log_t *access_log; nxt_router_access_log_t *access_log;
@@ -556,13 +556,13 @@ nxt_http_request_close_handler(nxt_task_t *task, void *obj, void *data)
} }
} }
handler = nxt_http_proto_close[r->protocol]; protocol = r->protocol;
r->proto.any = NULL; r->proto.any = NULL;
nxt_mp_release(r->mem_pool); nxt_mp_release(r->mem_pool);
if (proto.any != NULL) { if (nxt_fast_path(proto.any != NULL)) {
handler(task, proto, conf); nxt_http_proto[protocol].close(task, proto, conf);
} }
} }

View File

@@ -3058,7 +3058,7 @@ nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r,
*p++ = ' '; *p++ = ' ';
bytes = nxt_http_proto_body_bytes_sent[r->protocol](task, r->proto); bytes = nxt_http_proto[r->protocol].body_bytes_sent(task, r->proto);
p = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%O", bytes); p = nxt_sprintf(p, p + NXT_OFF_T_LEN, "%O", bytes);