comparison src/http/ngx_http_request.c @ 9119:08ef02ad5c54

HTTP/2: "http2" directive. The directive enables HTTP/2 in the current server. The previous way to enable HTTP/2 via "listen ... http2" is now deprecated. The new approach allows to share HTTP/2 and HTTP/0.9-1.1 on the same port. For SSL connections, HTTP/2 is now selected by ALPN callback based on whether the protocol is enabled in the virtual server chosen by SNI. This however only works since OpenSSL 1.0.2h, where ALPN callback is invoked after SNI callback. For older versions of OpenSSL, HTTP/2 is enabled based on the default virtual server configuration. For plain TCP connections, HTTP/2 is now auto-detected by HTTP/2 preface, if HTTP/2 is enabled in the default virtual server. If preface is not matched, HTTP/0.9-1.1 is assumed.
author Roman Arutyunyan <arut@nginx.com>
date Tue, 16 May 2023 16:30:08 +0400
parents 0af598651e33
children 0aaa09927703
comparison
equal deleted inserted replaced
9118:b4a57278bf24 9119:08ef02ad5c54
316 316
317 rev = c->read; 317 rev = c->read;
318 rev->handler = ngx_http_wait_request_handler; 318 rev->handler = ngx_http_wait_request_handler;
319 c->write->handler = ngx_http_empty_handler; 319 c->write->handler = ngx_http_empty_handler;
320 320
321 #if (NGX_HTTP_V2)
322 if (hc->addr_conf->http2) {
323 rev->handler = ngx_http_v2_init;
324 }
325 #endif
326
327 #if (NGX_HTTP_V3) 321 #if (NGX_HTTP_V3)
328 if (hc->addr_conf->quic) { 322 if (hc->addr_conf->quic) {
329 ngx_http_v3_init_stream(c); 323 ngx_http_v3_init_stream(c);
330 return; 324 return;
331 } 325 }
381 size_t size; 375 size_t size;
382 ssize_t n; 376 ssize_t n;
383 ngx_buf_t *b; 377 ngx_buf_t *b;
384 ngx_connection_t *c; 378 ngx_connection_t *c;
385 ngx_http_connection_t *hc; 379 ngx_http_connection_t *hc;
380 #if (NGX_HTTP_V2)
381 ngx_http_v2_srv_conf_t *h2scf;
382 #endif
386 ngx_http_core_srv_conf_t *cscf; 383 ngx_http_core_srv_conf_t *cscf;
387 384
388 c = rev->data; 385 c = rev->data;
389 386
390 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http wait request handler"); 387 ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http wait request handler");
427 b->pos = b->start; 424 b->pos = b->start;
428 b->last = b->start; 425 b->last = b->start;
429 b->end = b->last + size; 426 b->end = b->last + size;
430 } 427 }
431 428
429 size = b->end - b->last;
430
432 n = c->recv(c, b->last, size); 431 n = c->recv(c, b->last, size);
433 432
434 if (n == NGX_AGAIN) { 433 if (n == NGX_AGAIN) {
435 434
436 if (!rev->timer_set) { 435 if (!rev->timer_set) {
441 if (ngx_handle_read_event(rev, 0) != NGX_OK) { 440 if (ngx_handle_read_event(rev, 0) != NGX_OK) {
442 ngx_http_close_connection(c); 441 ngx_http_close_connection(c);
443 return; 442 return;
444 } 443 }
445 444
446 /* 445 if (b->pos == b->last) {
447 * We are trying to not hold c->buffer's memory for an idle connection. 446
448 */ 447 /*
449 448 * We are trying to not hold c->buffer's memory for an
450 if (ngx_pfree(c->pool, b->start) == NGX_OK) { 449 * idle connection.
451 b->start = NULL; 450 */
451
452 if (ngx_pfree(c->pool, b->start) == NGX_OK) {
453 b->start = NULL;
454 }
452 } 455 }
453 456
454 return; 457 return;
455 } 458 }
456 459
486 b->last = b->start; 489 b->last = b->start;
487 ngx_post_event(rev, &ngx_posted_events); 490 ngx_post_event(rev, &ngx_posted_events);
488 return; 491 return;
489 } 492 }
490 } 493 }
494
495 #if (NGX_HTTP_V2)
496
497 h2scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v2_module);
498
499 if (!hc->ssl && (h2scf->enable || hc->addr_conf->http2)) {
500
501 size = ngx_min(sizeof(NGX_HTTP_V2_PREFACE) - 1,
502 (size_t) (b->last - b->pos));
503
504 if (ngx_memcmp(b->pos, NGX_HTTP_V2_PREFACE, size) == 0) {
505
506 if (size == sizeof(NGX_HTTP_V2_PREFACE) - 1) {
507 ngx_http_v2_init(rev);
508 return;
509 }
510
511 ngx_post_event(rev, &ngx_posted_events);
512 return;
513 }
514 }
515
516 #endif
491 517
492 c->log->action = "reading client request line"; 518 c->log->action = "reading client request line";
493 519
494 ngx_reusable_connection(c, 0); 520 ngx_reusable_connection(c, 0);
495 521
806 c->ssl->no_wait_shutdown = 1; 832 c->ssl->no_wait_shutdown = 1;
807 833
808 #if (NGX_HTTP_V2 \ 834 #if (NGX_HTTP_V2 \
809 && defined TLSEXT_TYPE_application_layer_protocol_negotiation) 835 && defined TLSEXT_TYPE_application_layer_protocol_negotiation)
810 { 836 {
811 unsigned int len; 837 unsigned int len;
812 const unsigned char *data; 838 const unsigned char *data;
813 ngx_http_connection_t *hc; 839 ngx_http_connection_t *hc;
840 ngx_http_v2_srv_conf_t *h2scf;
814 841
815 hc = c->data; 842 hc = c->data;
816 843
817 if (hc->addr_conf->http2) { 844 h2scf = ngx_http_get_module_srv_conf(hc->conf_ctx, ngx_http_v2_module);
845
846 if (h2scf->enable || hc->addr_conf->http2) {
818 847
819 SSL_get0_alpn_selected(c->ssl->connection, &data, &len); 848 SSL_get0_alpn_selected(c->ssl->connection, &data, &len);
820 849
821 if (len == 2 && data[0] == 'h' && data[1] == '2') { 850 if (len == 2 && data[0] == 'h' && data[1] == '2') {
822 ngx_http_v2_init(c->read); 851 ngx_http_v2_init(c->read);