diff options
author | Igor Sysoev <igor@sysoev.ru> | 2019-08-06 15:29:39 +0300 |
---|---|---|
committer | Igor Sysoev <igor@sysoev.ru> | 2019-08-06 15:29:39 +0300 |
commit | 17bb22a4e46b390f42af665d1e92d2e1a09c9b56 (patch) | |
tree | 5d720b03363c529a26ad0367d03fb85dc7d8c68c | |
parent | fdf570f1d9561bfc349f24c87e1ac35afd2254e2 (diff) | |
download | unit-17bb22a4e46b390f42af665d1e92d2e1a09c9b56.tar.gz unit-17bb22a4e46b390f42af665d1e92d2e1a09c9b56.tar.bz2 |
Refactored HTTP protocol callback table.
Diffstat (limited to '')
-rw-r--r-- | src/nxt_h1proto.c | 60 | ||||
-rw-r--r-- | src/nxt_http.h | 41 | ||||
-rw-r--r-- | src/nxt_http_request.c | 28 | ||||
-rw-r--r-- | src/nxt_router.c | 2 |
4 files changed, 48 insertions, 83 deletions
diff --git a/src/nxt_h1proto.c b/src/nxt_h1proto.c index 6bc6c7ee..40cd370e 100644 --- a/src/nxt_h1proto.c +++ b/src/nxt_h1proto.c @@ -89,52 +89,19 @@ static const nxt_conn_state_t nxt_h1p_keepalive_state; static const nxt_conn_state_t nxt_h1p_close_state; -const nxt_http_proto_body_read_t nxt_http_proto_body_read[3] = { - nxt_h1p_request_body_read, - NULL, - NULL, -}; - - -const nxt_http_proto_local_addr_t nxt_http_proto_local_addr[3] = { - nxt_h1p_request_local_addr, - NULL, - NULL, -}; - - -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, +const nxt_http_proto_table_t nxt_http_proto[3] = { + /* NXT_HTTP_PROTO_H1 */ + { + .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, + .body_bytes_sent = nxt_h1p_request_body_bytes_sent, + .discard = nxt_h1p_request_discard, + .close = nxt_h1p_request_close, + }, + /* NXT_HTTP_PROTO_H2 */ + /* NXT_HTTP_PROTO_DEVNULL */ }; @@ -438,6 +405,7 @@ nxt_h1p_conn_request_init(nxt_task_t *task, void *obj, void *data) h1p->request = r; r->proto.h1 = h1p; + /* r->protocol = NXT_HTTP_PROTO_H1 is done by zeroing. */ r->remote = c->remote; #if (NXT_TLS) diff --git a/src/nxt_http.h b/src/nxt_http.h index 7398c9c1..d9916a31 100644 --- a/src/nxt_http.h +++ b/src/nxt_http.h @@ -46,6 +46,13 @@ typedef enum { } 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 { nxt_work_handler_t ready_handler; nxt_work_handler_t error_handler; @@ -145,7 +152,7 @@ struct nxt_http_request_s { nxt_http_status_t status:16; 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 header_sent; /* 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, - nxt_http_request_t *r); -typedef void (*nxt_http_proto_local_addr_t)(nxt_task_t *task, - nxt_http_request_t *r); -typedef void (*nxt_http_proto_header_send_t)(nxt_task_t *task, - nxt_http_request_t *r); -typedef void (*nxt_http_proto_send_t)(nxt_task_t *task, nxt_http_request_t *r, - nxt_buf_t *out); -typedef nxt_off_t (*nxt_http_proto_body_bytes_sent_t)(nxt_task_t *task, - nxt_http_proto_t proto); -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); +typedef struct { + void (*body_read)(nxt_task_t *task, nxt_http_request_t *r); + void (*local_addr)(nxt_task_t *task, nxt_http_request_t *r); + void (*header_send)(nxt_task_t *task, nxt_http_request_t *r); + void (*send)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out); + nxt_off_t (*body_bytes_sent)(nxt_task_t *task, nxt_http_proto_t proto); + void (*discard)(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *last); + void (*close)(nxt_task_t *task, nxt_http_proto_t proto, + nxt_socket_conf_joint_t *joint); +} nxt_http_proto_table_t; 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 const nxt_http_proto_body_read_t nxt_http_proto_body_read[]; -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[]; +extern const nxt_http_proto_table_t nxt_http_proto[]; #endif /* _NXT_HTTP_H_INCLUDED_ */ diff --git a/src/nxt_http_request.c b/src/nxt_http_request.c index ce088acb..1ab22223 100644 --- a/src/nxt_http_request.c +++ b/src/nxt_http_request.c @@ -355,8 +355,8 @@ nxt_http_request_application(nxt_task_t *task, nxt_http_request_t *r, static void nxt_http_request_proto_info(nxt_task_t *task, nxt_http_request_t *r) { - if (r->proto.any != NULL) { - nxt_http_proto_local_addr[r->protocol](task, r); + if (nxt_fast_path(r->proto.any != NULL)) { + 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 nxt_http_request_read_body(nxt_task_t *task, nxt_http_request_t *r) { - if (r->proto.any != NULL) { - nxt_http_proto_body_read[r->protocol](task, r); + if (nxt_fast_path(r->proto.any != NULL)) { + 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; } - if (r->proto.any != NULL) { - nxt_http_proto_header_send[r->protocol](task, r); + if (nxt_fast_path(r->proto.any != NULL)) { + nxt_http_proto[r->protocol].header_send(task, r); } return; @@ -446,8 +446,8 @@ fail: void nxt_http_request_send(nxt_task_t *task, nxt_http_request_t *r, nxt_buf_t *out) { - if (r->proto.any != NULL) { - nxt_http_proto_send[r->protocol](task, r, out); + if (nxt_fast_path(r->proto.any != NULL)) { + 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; - if (proto.any != NULL) { - nxt_http_proto_discard[r->protocol](task, r, nxt_http_buf_last(r)); + if (nxt_fast_path(proto.any != NULL)) { + 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_request_t *r; - nxt_http_proto_close_t handler; + nxt_http_protocol_t protocol; nxt_socket_conf_joint_t *conf; 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; nxt_mp_release(r->mem_pool); - if (proto.any != NULL) { - handler(task, proto, conf); + if (nxt_fast_path(proto.any != NULL)) { + nxt_http_proto[protocol].close(task, proto, conf); } } diff --git a/src/nxt_router.c b/src/nxt_router.c index 018cd4f4..f43b9a9e 100644 --- a/src/nxt_router.c +++ b/src/nxt_router.c @@ -3058,7 +3058,7 @@ nxt_router_access_log_writer(nxt_task_t *task, nxt_http_request_t *r, *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); |