PageRenderTime 46ms CodeModel.GetById 11ms RepoModel.GetById 0ms app.codeStats 1ms

/src/http/ngx_http_request.c

https://github.com/marutha/nginx
C | 3226 lines | 2280 code | 864 blank | 82 comment | 581 complexity | c3ef7e394991347b813e175ec6f5d15c MD5 | raw file
  1. /*
  2. * Copyright (C) Igor Sysoev
  3. * Copyright (C) Nginx, Inc.
  4. */
  5. #include <ngx_config.h>
  6. #include <ngx_core.h>
  7. #include <ngx_http.h>
  8. static void ngx_http_init_request(ngx_event_t *ev);
  9. static void ngx_http_process_request_line(ngx_event_t *rev);
  10. static void ngx_http_process_request_headers(ngx_event_t *rev);
  11. static ssize_t ngx_http_read_request_header(ngx_http_request_t *r);
  12. static ngx_int_t ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
  13. ngx_uint_t request_line);
  14. static ngx_int_t ngx_http_process_header_line(ngx_http_request_t *r,
  15. ngx_table_elt_t *h, ngx_uint_t offset);
  16. static ngx_int_t ngx_http_process_unique_header_line(ngx_http_request_t *r,
  17. ngx_table_elt_t *h, ngx_uint_t offset);
  18. static ngx_int_t ngx_http_process_host(ngx_http_request_t *r,
  19. ngx_table_elt_t *h, ngx_uint_t offset);
  20. static ngx_int_t ngx_http_process_connection(ngx_http_request_t *r,
  21. ngx_table_elt_t *h, ngx_uint_t offset);
  22. static ngx_int_t ngx_http_process_user_agent(ngx_http_request_t *r,
  23. ngx_table_elt_t *h, ngx_uint_t offset);
  24. static ngx_int_t ngx_http_process_cookie(ngx_http_request_t *r,
  25. ngx_table_elt_t *h, ngx_uint_t offset);
  26. static ngx_int_t ngx_http_process_request_header(ngx_http_request_t *r);
  27. static void ngx_http_process_request(ngx_http_request_t *r);
  28. static ssize_t ngx_http_validate_host(ngx_http_request_t *r, u_char **host,
  29. size_t len, ngx_uint_t alloc);
  30. static ngx_int_t ngx_http_find_virtual_server(ngx_http_request_t *r,
  31. u_char *host, size_t len);
  32. static void ngx_http_request_handler(ngx_event_t *ev);
  33. static void ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc);
  34. static void ngx_http_terminate_handler(ngx_http_request_t *r);
  35. static void ngx_http_finalize_connection(ngx_http_request_t *r);
  36. static ngx_int_t ngx_http_set_write_handler(ngx_http_request_t *r);
  37. static void ngx_http_writer(ngx_http_request_t *r);
  38. static void ngx_http_request_finalizer(ngx_http_request_t *r);
  39. static void ngx_http_set_keepalive(ngx_http_request_t *r);
  40. static void ngx_http_keepalive_handler(ngx_event_t *ev);
  41. static void ngx_http_set_lingering_close(ngx_http_request_t *r);
  42. static void ngx_http_lingering_close_handler(ngx_event_t *ev);
  43. static ngx_int_t ngx_http_post_action(ngx_http_request_t *r);
  44. static void ngx_http_close_request(ngx_http_request_t *r, ngx_int_t error);
  45. static void ngx_http_free_request(ngx_http_request_t *r, ngx_int_t error);
  46. static void ngx_http_log_request(ngx_http_request_t *r);
  47. static void ngx_http_close_connection(ngx_connection_t *c);
  48. static u_char *ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len);
  49. static u_char *ngx_http_log_error_handler(ngx_http_request_t *r,
  50. ngx_http_request_t *sr, u_char *buf, size_t len);
  51. #if (NGX_HTTP_SSL)
  52. static void ngx_http_ssl_handshake(ngx_event_t *rev);
  53. static void ngx_http_ssl_handshake_handler(ngx_connection_t *c);
  54. #endif
  55. static char *ngx_http_client_errors[] = {
  56. /* NGX_HTTP_PARSE_INVALID_METHOD */
  57. "client sent invalid method",
  58. /* NGX_HTTP_PARSE_INVALID_REQUEST */
  59. "client sent invalid request",
  60. /* NGX_HTTP_PARSE_INVALID_09_METHOD */
  61. "client sent invalid method in HTTP/0.9 request"
  62. };
  63. ngx_http_header_t ngx_http_headers_in[] = {
  64. { ngx_string("Host"), offsetof(ngx_http_headers_in_t, host),
  65. ngx_http_process_host },
  66. { ngx_string("Connection"), offsetof(ngx_http_headers_in_t, connection),
  67. ngx_http_process_connection },
  68. { ngx_string("If-Modified-Since"),
  69. offsetof(ngx_http_headers_in_t, if_modified_since),
  70. ngx_http_process_unique_header_line },
  71. { ngx_string("If-Unmodified-Since"),
  72. offsetof(ngx_http_headers_in_t, if_unmodified_since),
  73. ngx_http_process_unique_header_line },
  74. { ngx_string("User-Agent"), offsetof(ngx_http_headers_in_t, user_agent),
  75. ngx_http_process_user_agent },
  76. { ngx_string("Referer"), offsetof(ngx_http_headers_in_t, referer),
  77. ngx_http_process_header_line },
  78. { ngx_string("Content-Length"),
  79. offsetof(ngx_http_headers_in_t, content_length),
  80. ngx_http_process_unique_header_line },
  81. { ngx_string("Content-Type"),
  82. offsetof(ngx_http_headers_in_t, content_type),
  83. ngx_http_process_header_line },
  84. { ngx_string("Range"), offsetof(ngx_http_headers_in_t, range),
  85. ngx_http_process_header_line },
  86. { ngx_string("If-Range"),
  87. offsetof(ngx_http_headers_in_t, if_range),
  88. ngx_http_process_unique_header_line },
  89. { ngx_string("Transfer-Encoding"),
  90. offsetof(ngx_http_headers_in_t, transfer_encoding),
  91. ngx_http_process_header_line },
  92. { ngx_string("Expect"),
  93. offsetof(ngx_http_headers_in_t, expect),
  94. ngx_http_process_unique_header_line },
  95. #if (NGX_HTTP_GZIP)
  96. { ngx_string("Accept-Encoding"),
  97. offsetof(ngx_http_headers_in_t, accept_encoding),
  98. ngx_http_process_header_line },
  99. { ngx_string("Via"), offsetof(ngx_http_headers_in_t, via),
  100. ngx_http_process_header_line },
  101. #endif
  102. { ngx_string("Authorization"),
  103. offsetof(ngx_http_headers_in_t, authorization),
  104. ngx_http_process_unique_header_line },
  105. { ngx_string("Keep-Alive"), offsetof(ngx_http_headers_in_t, keep_alive),
  106. ngx_http_process_header_line },
  107. #if (NGX_HTTP_X_FORWARDED_FOR)
  108. { ngx_string("X-Forwarded-For"),
  109. offsetof(ngx_http_headers_in_t, x_forwarded_for),
  110. ngx_http_process_header_line },
  111. #endif
  112. #if (NGX_HTTP_REALIP)
  113. { ngx_string("X-Real-IP"),
  114. offsetof(ngx_http_headers_in_t, x_real_ip),
  115. ngx_http_process_header_line },
  116. #endif
  117. #if (NGX_HTTP_HEADERS)
  118. { ngx_string("Accept"), offsetof(ngx_http_headers_in_t, accept),
  119. ngx_http_process_header_line },
  120. { ngx_string("Accept-Language"),
  121. offsetof(ngx_http_headers_in_t, accept_language),
  122. ngx_http_process_header_line },
  123. #endif
  124. #if (NGX_HTTP_DAV)
  125. { ngx_string("Depth"), offsetof(ngx_http_headers_in_t, depth),
  126. ngx_http_process_header_line },
  127. { ngx_string("Destination"), offsetof(ngx_http_headers_in_t, destination),
  128. ngx_http_process_header_line },
  129. { ngx_string("Overwrite"), offsetof(ngx_http_headers_in_t, overwrite),
  130. ngx_http_process_header_line },
  131. { ngx_string("Date"), offsetof(ngx_http_headers_in_t, date),
  132. ngx_http_process_header_line },
  133. #endif
  134. { ngx_string("Cookie"), 0, ngx_http_process_cookie },
  135. { ngx_null_string, 0, NULL }
  136. };
  137. void
  138. ngx_http_init_connection(ngx_connection_t *c)
  139. {
  140. ngx_event_t *rev;
  141. ngx_http_log_ctx_t *ctx;
  142. ctx = ngx_palloc(c->pool, sizeof(ngx_http_log_ctx_t));
  143. if (ctx == NULL) {
  144. ngx_http_close_connection(c);
  145. return;
  146. }
  147. ctx->connection = c;
  148. ctx->request = NULL;
  149. ctx->current_request = NULL;
  150. c->log->connection = c->number;
  151. c->log->handler = ngx_http_log_error;
  152. c->log->data = ctx;
  153. c->log->action = "reading client request line";
  154. c->log_error = NGX_ERROR_INFO;
  155. rev = c->read;
  156. rev->handler = ngx_http_init_request;
  157. c->write->handler = ngx_http_empty_handler;
  158. #if (NGX_STAT_STUB)
  159. (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
  160. #endif
  161. if (rev->ready) {
  162. /* the deferred accept(), rtsig, aio, iocp */
  163. if (ngx_use_accept_mutex) {
  164. ngx_post_event(rev, &ngx_posted_events);
  165. return;
  166. }
  167. ngx_http_init_request(rev);
  168. return;
  169. }
  170. ngx_add_timer(rev, c->listening->post_accept_timeout);
  171. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  172. #if (NGX_STAT_STUB)
  173. (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
  174. #endif
  175. ngx_http_close_connection(c);
  176. return;
  177. }
  178. }
  179. static void
  180. ngx_http_init_request(ngx_event_t *rev)
  181. {
  182. ngx_time_t *tp;
  183. ngx_uint_t i;
  184. ngx_connection_t *c;
  185. ngx_http_request_t *r;
  186. struct sockaddr_in *sin;
  187. ngx_http_port_t *port;
  188. ngx_http_in_addr_t *addr;
  189. ngx_http_log_ctx_t *ctx;
  190. ngx_http_addr_conf_t *addr_conf;
  191. ngx_http_connection_t *hc;
  192. ngx_http_core_srv_conf_t *cscf;
  193. ngx_http_core_loc_conf_t *clcf;
  194. ngx_http_core_main_conf_t *cmcf;
  195. #if (NGX_HAVE_INET6)
  196. struct sockaddr_in6 *sin6;
  197. ngx_http_in6_addr_t *addr6;
  198. #endif
  199. #if (NGX_STAT_STUB)
  200. (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
  201. #endif
  202. c = rev->data;
  203. if (rev->timedout) {
  204. ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  205. ngx_http_close_connection(c);
  206. return;
  207. }
  208. c->requests++;
  209. hc = c->data;
  210. if (hc == NULL) {
  211. hc = ngx_pcalloc(c->pool, sizeof(ngx_http_connection_t));
  212. if (hc == NULL) {
  213. ngx_http_close_connection(c);
  214. return;
  215. }
  216. }
  217. r = hc->request;
  218. if (r) {
  219. ngx_memzero(r, sizeof(ngx_http_request_t));
  220. r->pipeline = hc->pipeline;
  221. if (hc->nbusy) {
  222. r->header_in = hc->busy[0];
  223. }
  224. } else {
  225. r = ngx_pcalloc(c->pool, sizeof(ngx_http_request_t));
  226. if (r == NULL) {
  227. ngx_http_close_connection(c);
  228. return;
  229. }
  230. hc->request = r;
  231. }
  232. c->data = r;
  233. r->http_connection = hc;
  234. c->sent = 0;
  235. r->signature = NGX_HTTP_MODULE;
  236. /* find the server configuration for the address:port */
  237. port = c->listening->servers;
  238. r->connection = c;
  239. if (port->naddrs > 1) {
  240. /*
  241. * there are several addresses on this port and one of them
  242. * is an "*:port" wildcard so getsockname() in ngx_http_server_addr()
  243. * is required to determine a server address
  244. */
  245. if (ngx_connection_local_sockaddr(c, NULL, 0) != NGX_OK) {
  246. ngx_http_close_connection(c);
  247. return;
  248. }
  249. switch (c->local_sockaddr->sa_family) {
  250. #if (NGX_HAVE_INET6)
  251. case AF_INET6:
  252. sin6 = (struct sockaddr_in6 *) c->local_sockaddr;
  253. addr6 = port->addrs;
  254. /* the last address is "*" */
  255. for (i = 0; i < port->naddrs - 1; i++) {
  256. if (ngx_memcmp(&addr6[i].addr6, &sin6->sin6_addr, 16) == 0) {
  257. break;
  258. }
  259. }
  260. addr_conf = &addr6[i].conf;
  261. break;
  262. #endif
  263. default: /* AF_INET */
  264. sin = (struct sockaddr_in *) c->local_sockaddr;
  265. addr = port->addrs;
  266. /* the last address is "*" */
  267. for (i = 0; i < port->naddrs - 1; i++) {
  268. if (addr[i].addr == sin->sin_addr.s_addr) {
  269. break;
  270. }
  271. }
  272. addr_conf = &addr[i].conf;
  273. break;
  274. }
  275. } else {
  276. switch (c->local_sockaddr->sa_family) {
  277. #if (NGX_HAVE_INET6)
  278. case AF_INET6:
  279. addr6 = port->addrs;
  280. addr_conf = &addr6[0].conf;
  281. break;
  282. #endif
  283. default: /* AF_INET */
  284. addr = port->addrs;
  285. addr_conf = &addr[0].conf;
  286. break;
  287. }
  288. }
  289. r->virtual_names = addr_conf->virtual_names;
  290. /* the default server configuration for the address:port */
  291. cscf = addr_conf->default_server;
  292. r->main_conf = cscf->ctx->main_conf;
  293. r->srv_conf = cscf->ctx->srv_conf;
  294. r->loc_conf = cscf->ctx->loc_conf;
  295. rev->handler = ngx_http_process_request_line;
  296. r->read_event_handler = ngx_http_block_reading;
  297. #if (NGX_HTTP_SSL)
  298. {
  299. ngx_http_ssl_srv_conf_t *sscf;
  300. sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
  301. if (sscf->enable || addr_conf->ssl) {
  302. if (c->ssl == NULL) {
  303. c->log->action = "SSL handshaking";
  304. if (addr_conf->ssl && sscf->ssl.ctx == NULL) {
  305. ngx_log_error(NGX_LOG_ERR, c->log, 0,
  306. "no \"ssl_certificate\" is defined "
  307. "in server listening on SSL port");
  308. ngx_http_close_connection(c);
  309. return;
  310. }
  311. if (ngx_ssl_create_connection(&sscf->ssl, c, NGX_SSL_BUFFER)
  312. != NGX_OK)
  313. {
  314. ngx_http_close_connection(c);
  315. return;
  316. }
  317. rev->handler = ngx_http_ssl_handshake;
  318. }
  319. r->main_filter_need_in_memory = 1;
  320. }
  321. }
  322. #endif
  323. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  324. c->log->file = clcf->error_log->file;
  325. if (!(c->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
  326. c->log->log_level = clcf->error_log->log_level;
  327. }
  328. if (c->buffer == NULL) {
  329. c->buffer = ngx_create_temp_buf(c->pool,
  330. cscf->client_header_buffer_size);
  331. if (c->buffer == NULL) {
  332. ngx_http_close_connection(c);
  333. return;
  334. }
  335. }
  336. if (r->header_in == NULL) {
  337. r->header_in = c->buffer;
  338. }
  339. r->pool = ngx_create_pool(cscf->request_pool_size, c->log);
  340. if (r->pool == NULL) {
  341. ngx_http_close_connection(c);
  342. return;
  343. }
  344. if (ngx_list_init(&r->headers_out.headers, r->pool, 20,
  345. sizeof(ngx_table_elt_t))
  346. != NGX_OK)
  347. {
  348. ngx_destroy_pool(r->pool);
  349. ngx_http_close_connection(c);
  350. return;
  351. }
  352. r->ctx = ngx_pcalloc(r->pool, sizeof(void *) * ngx_http_max_module);
  353. if (r->ctx == NULL) {
  354. ngx_destroy_pool(r->pool);
  355. ngx_http_close_connection(c);
  356. return;
  357. }
  358. cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
  359. r->variables = ngx_pcalloc(r->pool, cmcf->variables.nelts
  360. * sizeof(ngx_http_variable_value_t));
  361. if (r->variables == NULL) {
  362. ngx_destroy_pool(r->pool);
  363. ngx_http_close_connection(c);
  364. return;
  365. }
  366. c->single_connection = 1;
  367. c->destroyed = 0;
  368. r->main = r;
  369. r->count = 1;
  370. tp = ngx_timeofday();
  371. r->start_sec = tp->sec;
  372. r->start_msec = tp->msec;
  373. r->method = NGX_HTTP_UNKNOWN;
  374. r->headers_in.content_length_n = -1;
  375. r->headers_in.keep_alive_n = -1;
  376. r->headers_out.content_length_n = -1;
  377. r->headers_out.last_modified_time = -1;
  378. r->uri_changes = NGX_HTTP_MAX_URI_CHANGES + 1;
  379. r->subrequests = NGX_HTTP_MAX_SUBREQUESTS + 1;
  380. r->http_state = NGX_HTTP_READING_REQUEST_STATE;
  381. ctx = c->log->data;
  382. ctx->request = r;
  383. ctx->current_request = r;
  384. r->log_handler = ngx_http_log_error_handler;
  385. #if (NGX_STAT_STUB)
  386. (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
  387. r->stat_reading = 1;
  388. (void) ngx_atomic_fetch_add(ngx_stat_requests, 1);
  389. #endif
  390. rev->handler(rev);
  391. }
  392. #if (NGX_HTTP_SSL)
  393. static void
  394. ngx_http_ssl_handshake(ngx_event_t *rev)
  395. {
  396. u_char buf[1];
  397. ssize_t n;
  398. ngx_int_t rc;
  399. ngx_connection_t *c;
  400. ngx_http_request_t *r;
  401. c = rev->data;
  402. r = c->data;
  403. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
  404. "http check ssl handshake");
  405. if (rev->timedout) {
  406. ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  407. c->timedout = 1;
  408. ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  409. return;
  410. }
  411. n = recv(c->fd, (char *) buf, 1, MSG_PEEK);
  412. if (n == -1 && ngx_socket_errno == NGX_EAGAIN) {
  413. if (!rev->timer_set) {
  414. ngx_add_timer(rev, c->listening->post_accept_timeout);
  415. }
  416. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  417. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  418. }
  419. return;
  420. }
  421. if (n == 1) {
  422. if (buf[0] & 0x80 /* SSLv2 */ || buf[0] == 0x16 /* SSLv3/TLSv1 */) {
  423. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, rev->log, 0,
  424. "https ssl handshake: 0x%02Xd", buf[0]);
  425. rc = ngx_ssl_handshake(c);
  426. if (rc == NGX_AGAIN) {
  427. if (!rev->timer_set) {
  428. ngx_add_timer(rev, c->listening->post_accept_timeout);
  429. }
  430. c->ssl->handler = ngx_http_ssl_handshake_handler;
  431. return;
  432. }
  433. ngx_http_ssl_handshake_handler(c);
  434. return;
  435. } else {
  436. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
  437. "plain http");
  438. r->plain_http = 1;
  439. }
  440. }
  441. c->log->action = "reading client request line";
  442. rev->handler = ngx_http_process_request_line;
  443. ngx_http_process_request_line(rev);
  444. }
  445. static void
  446. ngx_http_ssl_handshake_handler(ngx_connection_t *c)
  447. {
  448. ngx_http_request_t *r;
  449. if (c->ssl->handshaked) {
  450. /*
  451. * The majority of browsers do not send the "close notify" alert.
  452. * Among them are MSIE, old Mozilla, Netscape 4, Konqueror,
  453. * and Links. And what is more, MSIE ignores the server's alert.
  454. *
  455. * Opera and recent Mozilla send the alert.
  456. */
  457. c->ssl->no_wait_shutdown = 1;
  458. c->log->action = "reading client request line";
  459. c->read->handler = ngx_http_process_request_line;
  460. /* STUB: epoll edge */ c->write->handler = ngx_http_empty_handler;
  461. ngx_http_process_request_line(c->read);
  462. return;
  463. }
  464. r = c->data;
  465. ngx_http_close_request(r, NGX_HTTP_BAD_REQUEST);
  466. return;
  467. }
  468. #ifdef SSL_CTRL_SET_TLSEXT_HOSTNAME
  469. int
  470. ngx_http_ssl_servername(ngx_ssl_conn_t *ssl_conn, int *ad, void *arg)
  471. {
  472. size_t len;
  473. u_char *host;
  474. const char *servername;
  475. ngx_connection_t *c;
  476. ngx_http_request_t *r;
  477. ngx_http_ssl_srv_conf_t *sscf;
  478. servername = SSL_get_servername(ssl_conn, TLSEXT_NAMETYPE_host_name);
  479. if (servername == NULL) {
  480. return SSL_TLSEXT_ERR_NOACK;
  481. }
  482. c = ngx_ssl_get_connection(ssl_conn);
  483. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  484. "SSL server name: \"%s\"", servername);
  485. len = ngx_strlen(servername);
  486. if (len == 0) {
  487. return SSL_TLSEXT_ERR_NOACK;
  488. }
  489. r = c->data;
  490. host = (u_char *) servername;
  491. len = ngx_http_validate_host(r, &host, len, 1);
  492. if (len <= 0) {
  493. return SSL_TLSEXT_ERR_NOACK;
  494. }
  495. if (ngx_http_find_virtual_server(r, host, len) != NGX_OK) {
  496. return SSL_TLSEXT_ERR_NOACK;
  497. }
  498. sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
  499. if (sscf->ssl.ctx) {
  500. SSL_set_SSL_CTX(ssl_conn, sscf->ssl.ctx);
  501. /*
  502. * SSL_set_SSL_CTX() only changes certs as of 1.0.0d
  503. * adjust other things we care about
  504. */
  505. SSL_set_verify(ssl_conn, SSL_CTX_get_verify_mode(sscf->ssl.ctx),
  506. SSL_CTX_get_verify_callback(sscf->ssl.ctx));
  507. SSL_set_verify_depth(ssl_conn, SSL_CTX_get_verify_depth(sscf->ssl.ctx));
  508. #ifdef SSL_CTRL_CLEAR_OPTIONS
  509. /* only in 0.9.8m+ */
  510. SSL_clear_options(ssl_conn, SSL_get_options(ssl_conn) &
  511. ~SSL_CTX_get_options(sscf->ssl.ctx));
  512. #endif
  513. SSL_set_options(ssl_conn, SSL_CTX_get_options(sscf->ssl.ctx));
  514. }
  515. return SSL_TLSEXT_ERR_OK;
  516. }
  517. #endif
  518. #endif
  519. static void
  520. ngx_http_process_request_line(ngx_event_t *rev)
  521. {
  522. u_char *host;
  523. ssize_t n;
  524. ngx_int_t rc, rv;
  525. ngx_connection_t *c;
  526. ngx_http_request_t *r;
  527. ngx_http_core_srv_conf_t *cscf;
  528. c = rev->data;
  529. r = c->data;
  530. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
  531. "http process request line");
  532. if (rev->timedout) {
  533. ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  534. c->timedout = 1;
  535. ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  536. return;
  537. }
  538. rc = NGX_AGAIN;
  539. for ( ;; ) {
  540. if (rc == NGX_AGAIN) {
  541. n = ngx_http_read_request_header(r);
  542. if (n == NGX_AGAIN || n == NGX_ERROR) {
  543. return;
  544. }
  545. }
  546. rc = ngx_http_parse_request_line(r, r->header_in);
  547. if (rc == NGX_OK) {
  548. /* the request line has been parsed successfully */
  549. r->request_line.len = r->request_end - r->request_start;
  550. r->request_line.data = r->request_start;
  551. if (r->args_start) {
  552. r->uri.len = r->args_start - 1 - r->uri_start;
  553. } else {
  554. r->uri.len = r->uri_end - r->uri_start;
  555. }
  556. if (r->complex_uri || r->quoted_uri) {
  557. r->uri.data = ngx_pnalloc(r->pool, r->uri.len + 1);
  558. if (r->uri.data == NULL) {
  559. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  560. return;
  561. }
  562. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  563. rc = ngx_http_parse_complex_uri(r, cscf->merge_slashes);
  564. if (rc == NGX_HTTP_PARSE_INVALID_REQUEST) {
  565. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  566. "client sent invalid request");
  567. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  568. return;
  569. }
  570. } else {
  571. r->uri.data = r->uri_start;
  572. }
  573. r->unparsed_uri.len = r->uri_end - r->uri_start;
  574. r->unparsed_uri.data = r->uri_start;
  575. r->valid_unparsed_uri = r->space_in_uri ? 0 : 1;
  576. r->method_name.len = r->method_end - r->request_start + 1;
  577. r->method_name.data = r->request_line.data;
  578. if (r->http_protocol.data) {
  579. r->http_protocol.len = r->request_end - r->http_protocol.data;
  580. }
  581. if (r->uri_ext) {
  582. if (r->args_start) {
  583. r->exten.len = r->args_start - 1 - r->uri_ext;
  584. } else {
  585. r->exten.len = r->uri_end - r->uri_ext;
  586. }
  587. r->exten.data = r->uri_ext;
  588. }
  589. if (r->args_start && r->uri_end > r->args_start) {
  590. r->args.len = r->uri_end - r->args_start;
  591. r->args.data = r->args_start;
  592. }
  593. #if (NGX_WIN32)
  594. {
  595. u_char *p, *last;
  596. p = r->uri.data;
  597. last = r->uri.data + r->uri.len;
  598. while (p < last) {
  599. if (*p++ == ':') {
  600. /*
  601. * this check covers "::$data", "::$index_allocation" and
  602. * ":$i30:$index_allocation"
  603. */
  604. if (p < last && *p == '$') {
  605. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  606. "client sent unsafe win32 URI");
  607. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  608. return;
  609. }
  610. }
  611. }
  612. p = r->uri.data + r->uri.len - 1;
  613. while (p > r->uri.data) {
  614. if (*p == ' ') {
  615. p--;
  616. continue;
  617. }
  618. if (*p == '.') {
  619. p--;
  620. continue;
  621. }
  622. break;
  623. }
  624. if (p != r->uri.data + r->uri.len - 1) {
  625. r->uri.len = p + 1 - r->uri.data;
  626. ngx_http_set_exten(r);
  627. }
  628. }
  629. #endif
  630. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  631. "http request line: \"%V\"", &r->request_line);
  632. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  633. "http uri: \"%V\"", &r->uri);
  634. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  635. "http args: \"%V\"", &r->args);
  636. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  637. "http exten: \"%V\"", &r->exten);
  638. if (r->host_start && r->host_end) {
  639. host = r->host_start;
  640. n = ngx_http_validate_host(r, &host,
  641. r->host_end - r->host_start, 0);
  642. if (n == 0) {
  643. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  644. "client sent invalid host in request line");
  645. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  646. return;
  647. }
  648. if (n < 0) {
  649. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  650. return;
  651. }
  652. r->headers_in.server.len = n;
  653. r->headers_in.server.data = host;
  654. }
  655. if (r->http_version < NGX_HTTP_VERSION_10) {
  656. if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
  657. r->headers_in.server.len)
  658. == NGX_ERROR)
  659. {
  660. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  661. return;
  662. }
  663. ngx_http_process_request(r);
  664. return;
  665. }
  666. if (ngx_list_init(&r->headers_in.headers, r->pool, 20,
  667. sizeof(ngx_table_elt_t))
  668. != NGX_OK)
  669. {
  670. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  671. return;
  672. }
  673. if (ngx_array_init(&r->headers_in.cookies, r->pool, 2,
  674. sizeof(ngx_table_elt_t *))
  675. != NGX_OK)
  676. {
  677. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  678. return;
  679. }
  680. c->log->action = "reading client request headers";
  681. rev->handler = ngx_http_process_request_headers;
  682. ngx_http_process_request_headers(rev);
  683. return;
  684. }
  685. if (rc != NGX_AGAIN) {
  686. /* there was error while a request line parsing */
  687. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  688. ngx_http_client_errors[rc - NGX_HTTP_CLIENT_ERROR]);
  689. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  690. return;
  691. }
  692. /* NGX_AGAIN: a request line parsing is still incomplete */
  693. if (r->header_in->pos == r->header_in->end) {
  694. rv = ngx_http_alloc_large_header_buffer(r, 1);
  695. if (rv == NGX_ERROR) {
  696. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  697. return;
  698. }
  699. if (rv == NGX_DECLINED) {
  700. r->request_line.len = r->header_in->end - r->request_start;
  701. r->request_line.data = r->request_start;
  702. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  703. "client sent too long URI");
  704. ngx_http_finalize_request(r, NGX_HTTP_REQUEST_URI_TOO_LARGE);
  705. return;
  706. }
  707. }
  708. }
  709. }
  710. static void
  711. ngx_http_process_request_headers(ngx_event_t *rev)
  712. {
  713. u_char *p;
  714. size_t len;
  715. ssize_t n;
  716. ngx_int_t rc, rv;
  717. ngx_table_elt_t *h;
  718. ngx_connection_t *c;
  719. ngx_http_header_t *hh;
  720. ngx_http_request_t *r;
  721. ngx_http_core_srv_conf_t *cscf;
  722. ngx_http_core_main_conf_t *cmcf;
  723. c = rev->data;
  724. r = c->data;
  725. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, rev->log, 0,
  726. "http process request header line");
  727. if (rev->timedout) {
  728. ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT, "client timed out");
  729. c->timedout = 1;
  730. ngx_http_close_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  731. return;
  732. }
  733. cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
  734. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  735. rc = NGX_AGAIN;
  736. for ( ;; ) {
  737. if (rc == NGX_AGAIN) {
  738. if (r->header_in->pos == r->header_in->end) {
  739. rv = ngx_http_alloc_large_header_buffer(r, 0);
  740. if (rv == NGX_ERROR) {
  741. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  742. return;
  743. }
  744. if (rv == NGX_DECLINED) {
  745. p = r->header_name_start;
  746. r->lingering_close = 1;
  747. if (p == NULL) {
  748. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  749. "client sent too large request");
  750. ngx_http_finalize_request(r,
  751. NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
  752. return;
  753. }
  754. len = r->header_in->end - p;
  755. if (len > NGX_MAX_ERROR_STR - 300) {
  756. len = NGX_MAX_ERROR_STR - 300;
  757. p[len++] = '.'; p[len++] = '.'; p[len++] = '.';
  758. }
  759. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  760. "client sent too long header line: \"%*s\"",
  761. len, r->header_name_start);
  762. ngx_http_finalize_request(r,
  763. NGX_HTTP_REQUEST_HEADER_TOO_LARGE);
  764. return;
  765. }
  766. }
  767. n = ngx_http_read_request_header(r);
  768. if (n == NGX_AGAIN || n == NGX_ERROR) {
  769. return;
  770. }
  771. }
  772. rc = ngx_http_parse_header_line(r, r->header_in,
  773. cscf->underscores_in_headers);
  774. if (rc == NGX_OK) {
  775. if (r->invalid_header && cscf->ignore_invalid_headers) {
  776. /* there was error while a header line parsing */
  777. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  778. "client sent invalid header line: \"%*s\"",
  779. r->header_end - r->header_name_start,
  780. r->header_name_start);
  781. continue;
  782. }
  783. /* a header line has been parsed successfully */
  784. h = ngx_list_push(&r->headers_in.headers);
  785. if (h == NULL) {
  786. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  787. return;
  788. }
  789. h->hash = r->header_hash;
  790. h->key.len = r->header_name_end - r->header_name_start;
  791. h->key.data = r->header_name_start;
  792. h->key.data[h->key.len] = '\0';
  793. h->value.len = r->header_end - r->header_start;
  794. h->value.data = r->header_start;
  795. h->value.data[h->value.len] = '\0';
  796. h->lowcase_key = ngx_pnalloc(r->pool, h->key.len);
  797. if (h->lowcase_key == NULL) {
  798. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  799. return;
  800. }
  801. if (h->key.len == r->lowcase_index) {
  802. ngx_memcpy(h->lowcase_key, r->lowcase_header, h->key.len);
  803. } else {
  804. ngx_strlow(h->lowcase_key, h->key.data, h->key.len);
  805. }
  806. hh = ngx_hash_find(&cmcf->headers_in_hash, h->hash,
  807. h->lowcase_key, h->key.len);
  808. if (hh && hh->handler(r, h, hh->offset) != NGX_OK) {
  809. return;
  810. }
  811. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  812. "http header: \"%V: %V\"",
  813. &h->key, &h->value);
  814. continue;
  815. }
  816. if (rc == NGX_HTTP_PARSE_HEADER_DONE) {
  817. /* a whole header has been parsed successfully */
  818. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  819. "http header done");
  820. r->request_length += r->header_in->pos - r->header_in->start;
  821. r->http_state = NGX_HTTP_PROCESS_REQUEST_STATE;
  822. rc = ngx_http_process_request_header(r);
  823. if (rc != NGX_OK) {
  824. return;
  825. }
  826. ngx_http_process_request(r);
  827. return;
  828. }
  829. if (rc == NGX_AGAIN) {
  830. /* a header line parsing is still not complete */
  831. continue;
  832. }
  833. /* rc == NGX_HTTP_PARSE_INVALID_HEADER: "\r" is not followed by "\n" */
  834. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  835. "client sent invalid header line: \"%*s\\r...\"",
  836. r->header_end - r->header_name_start,
  837. r->header_name_start);
  838. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  839. return;
  840. }
  841. }
  842. static ssize_t
  843. ngx_http_read_request_header(ngx_http_request_t *r)
  844. {
  845. ssize_t n;
  846. ngx_event_t *rev;
  847. ngx_connection_t *c;
  848. ngx_http_core_srv_conf_t *cscf;
  849. c = r->connection;
  850. rev = c->read;
  851. n = r->header_in->last - r->header_in->pos;
  852. if (n > 0) {
  853. return n;
  854. }
  855. if (rev->ready) {
  856. n = c->recv(c, r->header_in->last,
  857. r->header_in->end - r->header_in->last);
  858. } else {
  859. n = NGX_AGAIN;
  860. }
  861. if (n == NGX_AGAIN) {
  862. if (!rev->timer_set) {
  863. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  864. ngx_add_timer(rev, cscf->client_header_timeout);
  865. }
  866. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  867. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  868. return NGX_ERROR;
  869. }
  870. return NGX_AGAIN;
  871. }
  872. if (n == 0) {
  873. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  874. "client prematurely closed connection");
  875. }
  876. if (n == 0 || n == NGX_ERROR) {
  877. c->error = 1;
  878. c->log->action = "reading client request headers";
  879. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  880. return NGX_ERROR;
  881. }
  882. r->header_in->last += n;
  883. return n;
  884. }
  885. static ngx_int_t
  886. ngx_http_alloc_large_header_buffer(ngx_http_request_t *r,
  887. ngx_uint_t request_line)
  888. {
  889. u_char *old, *new;
  890. ngx_buf_t *b;
  891. ngx_http_connection_t *hc;
  892. ngx_http_core_srv_conf_t *cscf;
  893. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  894. "http alloc large header buffer");
  895. if (request_line && r->state == 0) {
  896. /* the client fills up the buffer with "\r\n" */
  897. r->request_length += r->header_in->end - r->header_in->start;
  898. r->header_in->pos = r->header_in->start;
  899. r->header_in->last = r->header_in->start;
  900. return NGX_OK;
  901. }
  902. old = request_line ? r->request_start : r->header_name_start;
  903. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  904. if (r->state != 0
  905. && (size_t) (r->header_in->pos - old)
  906. >= cscf->large_client_header_buffers.size)
  907. {
  908. return NGX_DECLINED;
  909. }
  910. hc = r->http_connection;
  911. if (hc->nfree) {
  912. b = hc->free[--hc->nfree];
  913. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  914. "http large header free: %p %uz",
  915. b->pos, b->end - b->last);
  916. } else if (hc->nbusy < cscf->large_client_header_buffers.num) {
  917. if (hc->busy == NULL) {
  918. hc->busy = ngx_palloc(r->connection->pool,
  919. cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
  920. if (hc->busy == NULL) {
  921. return NGX_ERROR;
  922. }
  923. }
  924. b = ngx_create_temp_buf(r->connection->pool,
  925. cscf->large_client_header_buffers.size);
  926. if (b == NULL) {
  927. return NGX_ERROR;
  928. }
  929. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  930. "http large header alloc: %p %uz",
  931. b->pos, b->end - b->last);
  932. } else {
  933. return NGX_DECLINED;
  934. }
  935. hc->busy[hc->nbusy++] = b;
  936. if (r->state == 0) {
  937. /*
  938. * r->state == 0 means that a header line was parsed successfully
  939. * and we do not need to copy incomplete header line and
  940. * to relocate the parser header pointers
  941. */
  942. r->request_length += r->header_in->end - r->header_in->start;
  943. r->header_in = b;
  944. return NGX_OK;
  945. }
  946. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  947. "http large header copy: %d", r->header_in->pos - old);
  948. r->request_length += old - r->header_in->start;
  949. new = b->start;
  950. ngx_memcpy(new, old, r->header_in->pos - old);
  951. b->pos = new + (r->header_in->pos - old);
  952. b->last = new + (r->header_in->pos - old);
  953. if (request_line) {
  954. r->request_start = new;
  955. if (r->request_end) {
  956. r->request_end = new + (r->request_end - old);
  957. }
  958. r->method_end = new + (r->method_end - old);
  959. r->uri_start = new + (r->uri_start - old);
  960. r->uri_end = new + (r->uri_end - old);
  961. if (r->schema_start) {
  962. r->schema_start = new + (r->schema_start - old);
  963. r->schema_end = new + (r->schema_end - old);
  964. }
  965. if (r->host_start) {
  966. r->host_start = new + (r->host_start - old);
  967. if (r->host_end) {
  968. r->host_end = new + (r->host_end - old);
  969. }
  970. }
  971. if (r->port_start) {
  972. r->port_start = new + (r->port_start - old);
  973. r->port_end = new + (r->port_end - old);
  974. }
  975. if (r->uri_ext) {
  976. r->uri_ext = new + (r->uri_ext - old);
  977. }
  978. if (r->args_start) {
  979. r->args_start = new + (r->args_start - old);
  980. }
  981. if (r->http_protocol.data) {
  982. r->http_protocol.data = new + (r->http_protocol.data - old);
  983. }
  984. } else {
  985. r->header_name_start = new;
  986. r->header_name_end = new + (r->header_name_end - old);
  987. r->header_start = new + (r->header_start - old);
  988. r->header_end = new + (r->header_end - old);
  989. }
  990. r->header_in = b;
  991. return NGX_OK;
  992. }
  993. static ngx_int_t
  994. ngx_http_process_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
  995. ngx_uint_t offset)
  996. {
  997. ngx_table_elt_t **ph;
  998. ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
  999. if (*ph == NULL) {
  1000. *ph = h;
  1001. }
  1002. return NGX_OK;
  1003. }
  1004. static ngx_int_t
  1005. ngx_http_process_unique_header_line(ngx_http_request_t *r, ngx_table_elt_t *h,
  1006. ngx_uint_t offset)
  1007. {
  1008. ngx_table_elt_t **ph;
  1009. ph = (ngx_table_elt_t **) ((char *) &r->headers_in + offset);
  1010. if (*ph == NULL) {
  1011. *ph = h;
  1012. return NGX_OK;
  1013. }
  1014. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1015. "client sent duplicate header line: \"%V: %V\", "
  1016. "previous value: \"%V: %V\"",
  1017. &h->key, &h->value, &(*ph)->key, &(*ph)->value);
  1018. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  1019. return NGX_ERROR;
  1020. }
  1021. static ngx_int_t
  1022. ngx_http_process_host(ngx_http_request_t *r, ngx_table_elt_t *h,
  1023. ngx_uint_t offset)
  1024. {
  1025. u_char *host;
  1026. ssize_t len;
  1027. if (r->headers_in.host == NULL) {
  1028. r->headers_in.host = h;
  1029. }
  1030. host = h->value.data;
  1031. len = ngx_http_validate_host(r, &host, h->value.len, 0);
  1032. if (len == 0) {
  1033. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1034. "client sent invalid host header");
  1035. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  1036. return NGX_ERROR;
  1037. }
  1038. if (len < 0) {
  1039. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  1040. return NGX_ERROR;
  1041. }
  1042. if (r->headers_in.server.len) {
  1043. return NGX_OK;
  1044. }
  1045. r->headers_in.server.len = len;
  1046. r->headers_in.server.data = host;
  1047. return NGX_OK;
  1048. }
  1049. static ngx_int_t
  1050. ngx_http_process_connection(ngx_http_request_t *r, ngx_table_elt_t *h,
  1051. ngx_uint_t offset)
  1052. {
  1053. if (ngx_strcasestrn(h->value.data, "close", 5 - 1)) {
  1054. r->headers_in.connection_type = NGX_HTTP_CONNECTION_CLOSE;
  1055. } else if (ngx_strcasestrn(h->value.data, "keep-alive", 10 - 1)) {
  1056. r->headers_in.connection_type = NGX_HTTP_CONNECTION_KEEP_ALIVE;
  1057. }
  1058. return NGX_OK;
  1059. }
  1060. static ngx_int_t
  1061. ngx_http_process_user_agent(ngx_http_request_t *r, ngx_table_elt_t *h,
  1062. ngx_uint_t offset)
  1063. {
  1064. u_char *user_agent, *msie;
  1065. if (r->headers_in.user_agent) {
  1066. return NGX_OK;
  1067. }
  1068. r->headers_in.user_agent = h;
  1069. /* check some widespread browsers while the header is in CPU cache */
  1070. user_agent = h->value.data;
  1071. msie = ngx_strstrn(user_agent, "MSIE ", 5 - 1);
  1072. if (msie && msie + 7 < user_agent + h->value.len) {
  1073. r->headers_in.msie = 1;
  1074. if (msie[6] == '.') {
  1075. switch (msie[5]) {
  1076. case '4':
  1077. case '5':
  1078. r->headers_in.msie6 = 1;
  1079. break;
  1080. case '6':
  1081. if (ngx_strstrn(msie + 8, "SV1", 3 - 1) == NULL) {
  1082. r->headers_in.msie6 = 1;
  1083. }
  1084. break;
  1085. }
  1086. }
  1087. #if 0
  1088. /* MSIE ignores the SSL "close notify" alert */
  1089. if (c->ssl) {
  1090. c->ssl->no_send_shutdown = 1;
  1091. }
  1092. #endif
  1093. }
  1094. if (ngx_strstrn(user_agent, "Opera", 5 - 1)) {
  1095. r->headers_in.opera = 1;
  1096. r->headers_in.msie = 0;
  1097. r->headers_in.msie6 = 0;
  1098. }
  1099. if (!r->headers_in.msie && !r->headers_in.opera) {
  1100. if (ngx_strstrn(user_agent, "Gecko/", 6 - 1)) {
  1101. r->headers_in.gecko = 1;
  1102. } else if (ngx_strstrn(user_agent, "Chrome/", 7 - 1)) {
  1103. r->headers_in.chrome = 1;
  1104. } else if (ngx_strstrn(user_agent, "Safari/", 7 - 1)
  1105. && ngx_strstrn(user_agent, "Mac OS X", 8 - 1))
  1106. {
  1107. r->headers_in.safari = 1;
  1108. } else if (ngx_strstrn(user_agent, "Konqueror", 9 - 1)) {
  1109. r->headers_in.konqueror = 1;
  1110. }
  1111. }
  1112. return NGX_OK;
  1113. }
  1114. static ngx_int_t
  1115. ngx_http_process_cookie(ngx_http_request_t *r, ngx_table_elt_t *h,
  1116. ngx_uint_t offset)
  1117. {
  1118. ngx_table_elt_t **cookie;
  1119. cookie = ngx_array_push(&r->headers_in.cookies);
  1120. if (cookie) {
  1121. *cookie = h;
  1122. return NGX_OK;
  1123. }
  1124. ngx_http_close_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  1125. return NGX_ERROR;
  1126. }
  1127. static ngx_int_t
  1128. ngx_http_process_request_header(ngx_http_request_t *r)
  1129. {
  1130. if (ngx_http_find_virtual_server(r, r->headers_in.server.data,
  1131. r->headers_in.server.len)
  1132. == NGX_ERROR)
  1133. {
  1134. ngx_http_finalize_request(r, NGX_HTTP_INTERNAL_SERVER_ERROR);
  1135. return NGX_ERROR;
  1136. }
  1137. if (r->headers_in.host == NULL && r->http_version > NGX_HTTP_VERSION_10) {
  1138. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1139. "client sent HTTP/1.1 request without \"Host\" header");
  1140. ngx_http_finalize_request(r, NGX_HTTP_BAD_REQUEST);
  1141. return NGX_ERROR;
  1142. }
  1143. if (r->headers_in.content_length) {
  1144. r->headers_in.content_length_n =
  1145. ngx_atoof(r->headers_in.content_length->value.data,
  1146. r->headers_in.content_length->value.len);
  1147. if (r->headers_in.content_length_n == NGX_ERROR) {
  1148. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1149. "client sent invalid \"Content-Length\" header");
  1150. ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
  1151. return NGX_ERROR;
  1152. }
  1153. }
  1154. if (r->method & NGX_HTTP_PUT && r->headers_in.content_length_n == -1) {
  1155. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1156. "client sent %V method without \"Content-Length\" header",
  1157. &r->method_name);
  1158. ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
  1159. return NGX_ERROR;
  1160. }
  1161. if (r->method & NGX_HTTP_TRACE) {
  1162. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1163. "client sent TRACE method");
  1164. ngx_http_finalize_request(r, NGX_HTTP_NOT_ALLOWED);
  1165. return NGX_ERROR;
  1166. }
  1167. if (r->headers_in.transfer_encoding
  1168. && ngx_strcasestrn(r->headers_in.transfer_encoding->value.data,
  1169. "chunked", 7 - 1))
  1170. {
  1171. ngx_log_error(NGX_LOG_INFO, r->connection->log, 0,
  1172. "client sent \"Transfer-Encoding: chunked\" header");
  1173. ngx_http_finalize_request(r, NGX_HTTP_LENGTH_REQUIRED);
  1174. return NGX_ERROR;
  1175. }
  1176. if (r->headers_in.connection_type == NGX_HTTP_CONNECTION_KEEP_ALIVE) {
  1177. if (r->headers_in.keep_alive) {
  1178. r->headers_in.keep_alive_n =
  1179. ngx_atotm(r->headers_in.keep_alive->value.data,
  1180. r->headers_in.keep_alive->value.len);
  1181. }
  1182. }
  1183. return NGX_OK;
  1184. }
  1185. static void
  1186. ngx_http_process_request(ngx_http_request_t *r)
  1187. {
  1188. ngx_connection_t *c;
  1189. c = r->connection;
  1190. if (r->plain_http) {
  1191. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  1192. "client sent plain HTTP request to HTTPS port");
  1193. ngx_http_finalize_request(r, NGX_HTTP_TO_HTTPS);
  1194. return;
  1195. }
  1196. #if (NGX_HTTP_SSL)
  1197. if (c->ssl) {
  1198. long rc;
  1199. X509 *cert;
  1200. ngx_http_ssl_srv_conf_t *sscf;
  1201. sscf = ngx_http_get_module_srv_conf(r, ngx_http_ssl_module);
  1202. if (sscf->verify) {
  1203. rc = SSL_get_verify_result(c->ssl->connection);
  1204. if (rc != X509_V_OK) {
  1205. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  1206. "client SSL certificate verify error: (%l:%s)",
  1207. rc, X509_verify_cert_error_string(rc));
  1208. ngx_ssl_remove_cached_session(sscf->ssl.ctx,
  1209. (SSL_get0_session(c->ssl->connection)));
  1210. ngx_http_finalize_request(r, NGX_HTTPS_CERT_ERROR);
  1211. return;
  1212. }
  1213. if (sscf->verify == 1) {
  1214. cert = SSL_get_peer_certificate(c->ssl->connection);
  1215. if (cert == NULL) {
  1216. ngx_log_error(NGX_LOG_INFO, c->log, 0,
  1217. "client sent no required SSL certificate");
  1218. ngx_ssl_remove_cached_session(sscf->ssl.ctx,
  1219. (SSL_get0_session(c->ssl->connection)));
  1220. ngx_http_finalize_request(r, NGX_HTTPS_NO_CERT);
  1221. return;
  1222. }
  1223. X509_free(cert);
  1224. }
  1225. }
  1226. }
  1227. #endif
  1228. if (c->read->timer_set) {
  1229. ngx_del_timer(c->read);
  1230. }
  1231. #if (NGX_STAT_STUB)
  1232. (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
  1233. r->stat_reading = 0;
  1234. (void) ngx_atomic_fetch_add(ngx_stat_writing, 1);
  1235. r->stat_writing = 1;
  1236. #endif
  1237. c->read->handler = ngx_http_request_handler;
  1238. c->write->handler = ngx_http_request_handler;
  1239. r->read_event_handler = ngx_http_block_reading;
  1240. ngx_http_handler(r);
  1241. ngx_http_run_posted_requests(c);
  1242. }
  1243. static ssize_t
  1244. ngx_http_validate_host(ngx_http_request_t *r, u_char **host, size_t len,
  1245. ngx_uint_t alloc)
  1246. {
  1247. u_char *h, ch;
  1248. size_t i, dot_pos, host_len;
  1249. enum {
  1250. sw_usual = 0,
  1251. sw_literal,
  1252. sw_rest
  1253. } state;
  1254. dot_pos = len;
  1255. host_len = len;
  1256. h = *host;
  1257. state = sw_usual;
  1258. for (i = 0; i < len; i++) {
  1259. ch = h[i];
  1260. switch (ch) {
  1261. case '.':
  1262. if (dot_pos == i - 1) {
  1263. return 0;
  1264. }
  1265. dot_pos = i;
  1266. break;
  1267. case ':':
  1268. if (state == sw_usual) {
  1269. host_len = i;
  1270. state = sw_rest;
  1271. }
  1272. break;
  1273. case '[':
  1274. if (i == 0) {
  1275. state = sw_literal;
  1276. }
  1277. break;
  1278. case ']':
  1279. if (state == sw_literal) {
  1280. host_len = i + 1;
  1281. state = sw_rest;
  1282. }
  1283. break;
  1284. case '\0':
  1285. return 0;
  1286. default:
  1287. if (ngx_path_separator(ch)) {
  1288. return 0;
  1289. }
  1290. if (ch >= 'A' && ch <= 'Z') {
  1291. alloc = 1;
  1292. }
  1293. break;
  1294. }
  1295. }
  1296. if (dot_pos == host_len - 1) {
  1297. host_len--;
  1298. }
  1299. if (alloc) {
  1300. *host = ngx_pnalloc(r->pool, host_len);
  1301. if (*host == NULL) {
  1302. return -1;
  1303. }
  1304. ngx_strlow(*host, h, host_len);
  1305. }
  1306. return host_len;
  1307. }
  1308. static ngx_int_t
  1309. ngx_http_find_virtual_server(ngx_http_request_t *r, u_char *host, size_t len)
  1310. {
  1311. ngx_http_core_loc_conf_t *clcf;
  1312. ngx_http_core_srv_conf_t *cscf;
  1313. if (r->virtual_names == NULL) {
  1314. return NGX_DECLINED;
  1315. }
  1316. cscf = ngx_hash_find_combined(&r->virtual_names->names,
  1317. ngx_hash_key(host, len), host, len);
  1318. if (cscf) {
  1319. goto found;
  1320. }
  1321. #if (NGX_PCRE)
  1322. if (len && r->virtual_names->nregex) {
  1323. ngx_int_t n;
  1324. ngx_uint_t i;
  1325. ngx_str_t name;
  1326. ngx_http_server_name_t *sn;
  1327. name.len = len;
  1328. name.data = host;
  1329. sn = r->virtual_names->regex;
  1330. for (i = 0; i < r->virtual_names->nregex; i++) {
  1331. n = ngx_http_regex_exec(r, sn[i].regex, &name);
  1332. if (n == NGX_OK) {
  1333. cscf = sn[i].server;
  1334. goto found;
  1335. }
  1336. if (n == NGX_DECLINED) {
  1337. continue;
  1338. }
  1339. return NGX_ERROR;
  1340. }
  1341. }
  1342. #endif
  1343. return NGX_OK;
  1344. found:
  1345. r->srv_conf = cscf->ctx->srv_conf;
  1346. r->loc_conf = cscf->ctx->loc_conf;
  1347. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  1348. r->connection->log->file = clcf->error_log->file;
  1349. if (!(r->connection->log->log_level & NGX_LOG_DEBUG_CONNECTION)) {
  1350. r->connection->log->log_level = clcf->error_log->log_level;
  1351. }
  1352. return NGX_OK;
  1353. }
  1354. static void
  1355. ngx_http_request_handler(ngx_event_t *ev)
  1356. {
  1357. ngx_connection_t *c;
  1358. ngx_http_request_t *r;
  1359. ngx_http_log_ctx_t *ctx;
  1360. c = ev->data;
  1361. r = c->data;
  1362. ctx = c->log->data;
  1363. ctx->current_request = r;
  1364. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1365. "http run request: \"%V?%V\"", &r->uri, &r->args);
  1366. if (ev->write) {
  1367. r->write_event_handler(r);
  1368. } else {
  1369. r->read_event_handler(r);
  1370. }
  1371. ngx_http_run_posted_requests(c);
  1372. }
  1373. void
  1374. ngx_http_run_posted_requests(ngx_connection_t *c)
  1375. {
  1376. ngx_http_request_t *r;
  1377. ngx_http_log_ctx_t *ctx;
  1378. ngx_http_posted_request_t *pr;
  1379. for ( ;; ) {
  1380. if (c->destroyed) {
  1381. return;
  1382. }
  1383. r = c->data;
  1384. pr = r->main->posted_requests;
  1385. if (pr == NULL) {
  1386. return;
  1387. }
  1388. r->main->posted_requests = pr->next;
  1389. r = pr->request;
  1390. ctx = c->log->data;
  1391. ctx->current_request = r;
  1392. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1393. "http posted request: \"%V?%V\"", &r->uri, &r->args);
  1394. r->write_event_handler(r);
  1395. }
  1396. }
  1397. ngx_int_t
  1398. ngx_http_post_request(ngx_http_request_t *r, ngx_http_posted_request_t *pr)
  1399. {
  1400. ngx_http_posted_request_t **p;
  1401. if (pr == NULL) {
  1402. pr = ngx_palloc(r->pool, sizeof(ngx_http_posted_request_t));
  1403. if (pr == NULL) {
  1404. return NGX_ERROR;
  1405. }
  1406. }
  1407. pr->request = r;
  1408. pr->next = NULL;
  1409. for (p = &r->main->posted_requests; *p; p = &(*p)->next) { /* void */ }
  1410. *p = pr;
  1411. return NGX_OK;
  1412. }
  1413. void
  1414. ngx_http_finalize_request(ngx_http_request_t *r, ngx_int_t rc)
  1415. {
  1416. ngx_connection_t *c;
  1417. ngx_http_request_t *pr;
  1418. ngx_http_core_loc_conf_t *clcf;
  1419. c = r->connection;
  1420. ngx_log_debug5(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1421. "http finalize request: %d, \"%V?%V\" a:%d, c:%d",
  1422. rc, &r->uri, &r->args, r == c->data, r->main->count);
  1423. if (rc == NGX_DONE) {
  1424. ngx_http_finalize_connection(r);
  1425. return;
  1426. }
  1427. if (rc == NGX_OK && r->filter_finalize) {
  1428. c->error = 1;
  1429. }
  1430. if (rc == NGX_DECLINED) {
  1431. r->content_handler = NULL;
  1432. r->write_event_handler = ngx_http_core_run_phases;
  1433. ngx_http_core_run_phases(r);
  1434. return;
  1435. }
  1436. if (r != r->main && r->post_subrequest) {
  1437. rc = r->post_subrequest->handler(r, r->post_subrequest->data, rc);
  1438. }
  1439. if (rc == NGX_ERROR
  1440. || rc == NGX_HTTP_REQUEST_TIME_OUT
  1441. || rc == NGX_HTTP_CLIENT_CLOSED_REQUEST
  1442. || c->error)
  1443. {
  1444. if (ngx_http_post_action(r) == NGX_OK) {
  1445. return;
  1446. }
  1447. if (r->main->blocked) {
  1448. r->write_event_handler = ngx_http_request_finalizer;
  1449. }
  1450. ngx_http_terminate_request(r, rc);
  1451. return;
  1452. }
  1453. if (rc >= NGX_HTTP_SPECIAL_RESPONSE
  1454. || rc == NGX_HTTP_CREATED
  1455. || rc == NGX_HTTP_NO_CONTENT)
  1456. {
  1457. if (rc == NGX_HTTP_CLOSE) {
  1458. ngx_http_terminate_request(r, rc);
  1459. return;
  1460. }
  1461. if (r == r->main) {
  1462. if (c->read->timer_set) {
  1463. ngx_del_timer(c->read);
  1464. }
  1465. if (c->write->timer_set) {
  1466. ngx_del_timer(c->write);
  1467. }
  1468. }
  1469. c->read->handler = ngx_http_request_handler;
  1470. c->write->handler = ngx_http_request_handler;
  1471. ngx_http_finalize_request(r, ngx_http_special_response_handler(r, rc));
  1472. return;
  1473. }
  1474. if (r != r->main) {
  1475. if (r->buffered || r->postponed) {
  1476. if (ngx_http_set_write_handler(r) != NGX_OK) {
  1477. ngx_http_terminate_request(r, 0);
  1478. }
  1479. return;
  1480. }
  1481. pr = r->parent;
  1482. if (r == c->data) {
  1483. r->main->count--;
  1484. r->main->subrequests++;
  1485. if (!r->logged) {
  1486. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  1487. if (clcf->log_subrequest) {
  1488. ngx_http_log_request(r);
  1489. }
  1490. r->logged = 1;
  1491. } else {
  1492. ngx_log_error(NGX_LOG_ALERT, c->log, 0,
  1493. "subrequest: \"%V?%V\" logged again",
  1494. &r->uri, &r->args);
  1495. }
  1496. r->done = 1;
  1497. if (pr->postponed && pr->postponed->request == r) {
  1498. pr->postponed = pr->postponed->next;
  1499. }
  1500. c->data = pr;
  1501. } else {
  1502. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1503. "http finalize non-active request: \"%V?%V\"",
  1504. &r->uri, &r->args);
  1505. r->write_event_handler = ngx_http_request_finalizer;
  1506. if (r->waited) {
  1507. r->done = 1;
  1508. }
  1509. }
  1510. if (ngx_http_post_request(pr, NULL) != NGX_OK) {
  1511. r->main->count++;
  1512. ngx_http_terminate_request(r, 0);
  1513. return;
  1514. }
  1515. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1516. "http wake parent request: \"%V?%V\"",
  1517. &pr->uri, &pr->args);
  1518. return;
  1519. }
  1520. if (r->buffered || c->buffered || r->postponed || r->blocked) {
  1521. if (ngx_http_set_write_handler(r) != NGX_OK) {
  1522. ngx_http_terminate_request(r, 0);
  1523. }
  1524. return;
  1525. }
  1526. if (r != c->data) {
  1527. ngx_log_error(NGX_LOG_ALERT, c->log, 0,
  1528. "http finalize non-active request: \"%V?%V\"",
  1529. &r->uri, &r->args);
  1530. return;
  1531. }
  1532. r->done = 1;
  1533. r->write_event_handler = ngx_http_request_empty_handler;
  1534. if (!r->post_action) {
  1535. r->request_complete = 1;
  1536. }
  1537. if (ngx_http_post_action(r) == NGX_OK) {
  1538. return;
  1539. }
  1540. if (c->read->timer_set) {
  1541. ngx_del_timer(c->read);
  1542. }
  1543. if (c->write->timer_set) {
  1544. c->write->delayed = 0;
  1545. ngx_del_timer(c->write);
  1546. }
  1547. if (c->read->eof) {
  1548. ngx_http_close_request(r, 0);
  1549. return;
  1550. }
  1551. ngx_http_finalize_connection(r);
  1552. }
  1553. static void
  1554. ngx_http_terminate_request(ngx_http_request_t *r, ngx_int_t rc)
  1555. {
  1556. ngx_http_cleanup_t *cln;
  1557. ngx_http_request_t *mr;
  1558. ngx_http_ephemeral_t *e;
  1559. mr = r->main;
  1560. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1561. "http terminate request count:%d", mr->count);
  1562. if (rc > 0 && (mr->headers_out.status == 0 || mr->connection->sent == 0)) {
  1563. mr->headers_out.status = rc;
  1564. }
  1565. cln = mr->cleanup;
  1566. mr->cleanup = NULL;
  1567. while (cln) {
  1568. if (cln->handler) {
  1569. cln->handler(cln->data);
  1570. }
  1571. cln = cln->next;
  1572. }
  1573. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1574. "http terminate cleanup count:%d blk:%d",
  1575. mr->count, mr->blocked);
  1576. if (mr->write_event_handler) {
  1577. if (mr->blocked) {
  1578. return;
  1579. }
  1580. e = ngx_http_ephemeral(mr);
  1581. mr->posted_requests = NULL;
  1582. mr->write_event_handler = ngx_http_terminate_handler;
  1583. (void) ngx_http_post_request(mr, &e->terminal_posted_request);
  1584. return;
  1585. }
  1586. ngx_http_close_request(mr, rc);
  1587. }
  1588. static void
  1589. ngx_http_terminate_handler(ngx_http_request_t *r)
  1590. {
  1591. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1592. "http terminate handler count:%d", r->count);
  1593. r->count = 1;
  1594. ngx_http_close_request(r, 0);
  1595. }
  1596. static void
  1597. ngx_http_finalize_connection(ngx_http_request_t *r)
  1598. {
  1599. ngx_http_core_loc_conf_t *clcf;
  1600. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  1601. if (r->main->count != 1) {
  1602. if (r->discard_body) {
  1603. r->read_event_handler = ngx_http_discarded_request_body_handler;
  1604. ngx_add_timer(r->connection->read, clcf->lingering_timeout);
  1605. if (r->lingering_time == 0) {
  1606. r->lingering_time = ngx_time()
  1607. + (time_t) (clcf->lingering_time / 1000);
  1608. }
  1609. }
  1610. ngx_http_close_request(r, 0);
  1611. return;
  1612. }
  1613. if (!ngx_terminate
  1614. && !ngx_exiting
  1615. && r->keepalive
  1616. && clcf->keepalive_timeout > 0)
  1617. {
  1618. ngx_http_set_keepalive(r);
  1619. return;
  1620. }
  1621. if (clcf->lingering_close == NGX_HTTP_LINGERING_ALWAYS
  1622. || (clcf->lingering_close == NGX_HTTP_LINGERING_ON
  1623. && (r->lingering_close
  1624. || r->header_in->pos < r->header_in->last
  1625. || r->connection->read->ready)))
  1626. {
  1627. ngx_http_set_lingering_close(r);
  1628. return;
  1629. }
  1630. ngx_http_close_request(r, 0);
  1631. }
  1632. static ngx_int_t
  1633. ngx_http_set_write_handler(ngx_http_request_t *r)
  1634. {
  1635. ngx_event_t *wev;
  1636. ngx_http_core_loc_conf_t *clcf;
  1637. r->http_state = NGX_HTTP_WRITING_REQUEST_STATE;
  1638. r->read_event_handler = r->discard_body ?
  1639. ngx_http_discarded_request_body_handler:
  1640. ngx_http_test_reading;
  1641. r->write_event_handler = ngx_http_writer;
  1642. wev = r->connection->write;
  1643. if (wev->ready && wev->delayed) {
  1644. return NGX_OK;
  1645. }
  1646. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  1647. if (!wev->delayed) {
  1648. ngx_add_timer(wev, clcf->send_timeout);
  1649. }
  1650. if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
  1651. ngx_http_close_request(r, 0);
  1652. return NGX_ERROR;
  1653. }
  1654. return NGX_OK;
  1655. }
  1656. static void
  1657. ngx_http_writer(ngx_http_request_t *r)
  1658. {
  1659. int rc;
  1660. ngx_event_t *wev;
  1661. ngx_connection_t *c;
  1662. ngx_http_core_loc_conf_t *clcf;
  1663. c = r->connection;
  1664. wev = c->write;
  1665. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
  1666. "http writer handler: \"%V?%V\"", &r->uri, &r->args);
  1667. clcf = ngx_http_get_module_loc_conf(r->main, ngx_http_core_module);
  1668. if (wev->timedout) {
  1669. if (!wev->delayed) {
  1670. ngx_log_error(NGX_LOG_INFO, c->log, NGX_ETIMEDOUT,
  1671. "client timed out");
  1672. c->timedout = 1;
  1673. ngx_http_finalize_request(r, NGX_HTTP_REQUEST_TIME_OUT);
  1674. return;
  1675. }
  1676. wev->timedout = 0;
  1677. wev->delayed = 0;
  1678. if (!wev->ready) {
  1679. ngx_add_timer(wev, clcf->send_timeout);
  1680. if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
  1681. ngx_http_close_request(r, 0);
  1682. }
  1683. return;
  1684. }
  1685. }
  1686. if (wev->delayed || r->aio) {
  1687. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0,
  1688. "http writer delayed");
  1689. if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
  1690. ngx_http_close_request(r, 0);
  1691. }
  1692. return;
  1693. }
  1694. rc = ngx_http_output_filter(r, NULL);
  1695. ngx_log_debug3(NGX_LOG_DEBUG_HTTP, c->log, 0,
  1696. "http writer output filter: %d, \"%V?%V\"",
  1697. rc, &r->uri, &r->args);
  1698. if (rc == NGX_ERROR) {
  1699. ngx_http_finalize_request(r, rc);
  1700. return;
  1701. }
  1702. if (r->buffered || r->postponed || (r == r->main && c->buffered)) {
  1703. if (!wev->delayed) {
  1704. ngx_add_timer(wev, clcf->send_timeout);
  1705. }
  1706. if (ngx_handle_write_event(wev, clcf->send_lowat) != NGX_OK) {
  1707. ngx_http_close_request(r, 0);
  1708. }
  1709. return;
  1710. }
  1711. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, wev->log, 0,
  1712. "http writer done: \"%V?%V\"", &r->uri, &r->args);
  1713. r->write_event_handler = ngx_http_request_empty_handler;
  1714. ngx_http_finalize_request(r, rc);
  1715. }
  1716. static void
  1717. ngx_http_request_finalizer(ngx_http_request_t *r)
  1718. {
  1719. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1720. "http finalizer done: \"%V?%V\"", &r->uri, &r->args);
  1721. ngx_http_finalize_request(r, 0);
  1722. }
  1723. void
  1724. ngx_http_block_reading(ngx_http_request_t *r)
  1725. {
  1726. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  1727. "http reading blocked");
  1728. /* aio does not call this handler */
  1729. if ((ngx_event_flags & NGX_USE_LEVEL_EVENT)
  1730. && r->connection->read->active)
  1731. {
  1732. if (ngx_del_event(r->connection->read, NGX_READ_EVENT, 0) != NGX_OK) {
  1733. ngx_http_close_request(r, 0);
  1734. }
  1735. }
  1736. }
  1737. void
  1738. ngx_http_test_reading(ngx_http_request_t *r)
  1739. {
  1740. int n;
  1741. char buf[1];
  1742. ngx_err_t err;
  1743. ngx_event_t *rev;
  1744. ngx_connection_t *c;
  1745. c = r->connection;
  1746. rev = c->read;
  1747. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http test reading");
  1748. #if (NGX_HAVE_KQUEUE)
  1749. if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
  1750. if (!rev->pending_eof) {
  1751. return;
  1752. }
  1753. rev->eof = 1;
  1754. c->error = 1;
  1755. err = rev->kq_errno;
  1756. goto closed;
  1757. }
  1758. #endif
  1759. n = recv(c->fd, buf, 1, MSG_PEEK);
  1760. if (n == 0) {
  1761. rev->eof = 1;
  1762. c->error = 1;
  1763. err = 0;
  1764. goto closed;
  1765. } else if (n == -1) {
  1766. err = ngx_socket_errno;
  1767. if (err != NGX_EAGAIN) {
  1768. rev->eof = 1;
  1769. c->error = 1;
  1770. goto closed;
  1771. }
  1772. }
  1773. /* aio does not call this handler */
  1774. if ((ngx_event_flags & NGX_USE_LEVEL_EVENT) && rev->active) {
  1775. if (ngx_del_event(rev, NGX_READ_EVENT, 0) != NGX_OK) {
  1776. ngx_http_close_request(r, 0);
  1777. }
  1778. }
  1779. return;
  1780. closed:
  1781. if (err) {
  1782. rev->error = 1;
  1783. }
  1784. ngx_log_error(NGX_LOG_INFO, c->log, err,
  1785. "client prematurely closed connection");
  1786. ngx_http_finalize_request(r, 0);
  1787. }
  1788. static void
  1789. ngx_http_set_keepalive(ngx_http_request_t *r)
  1790. {
  1791. int tcp_nodelay;
  1792. ngx_int_t i;
  1793. ngx_buf_t *b, *f;
  1794. ngx_event_t *rev, *wev;
  1795. ngx_connection_t *c;
  1796. ngx_http_connection_t *hc;
  1797. ngx_http_core_srv_conf_t *cscf;
  1798. ngx_http_core_loc_conf_t *clcf;
  1799. c = r->connection;
  1800. rev = c->read;
  1801. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  1802. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "set http keepalive handler");
  1803. if (r->discard_body) {
  1804. r->write_event_handler = ngx_http_request_empty_handler;
  1805. r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
  1806. ngx_add_timer(rev, clcf->lingering_timeout);
  1807. return;
  1808. }
  1809. c->log->action = "closing request";
  1810. hc = r->http_connection;
  1811. b = r->header_in;
  1812. if (b->pos < b->last) {
  1813. /* the pipelined request */
  1814. if (b != c->buffer) {
  1815. /*
  1816. * If the large header buffers were allocated while the previous
  1817. * request processing then we do not use c->buffer for
  1818. * the pipelined request (see ngx_http_init_request()).
  1819. *
  1820. * Now we would move the large header buffers to the free list.
  1821. */
  1822. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  1823. if (hc->free == NULL) {
  1824. hc->free = ngx_palloc(c->pool,
  1825. cscf->large_client_header_buffers.num * sizeof(ngx_buf_t *));
  1826. if (hc->free == NULL) {
  1827. ngx_http_close_request(r, 0);
  1828. return;
  1829. }
  1830. }
  1831. for (i = 0; i < hc->nbusy - 1; i++) {
  1832. f = hc->busy[i];
  1833. hc->free[hc->nfree++] = f;
  1834. f->pos = f->start;
  1835. f->last = f->start;
  1836. }
  1837. hc->busy[0] = b;
  1838. hc->nbusy = 1;
  1839. }
  1840. }
  1841. r->keepalive = 0;
  1842. ngx_http_free_request(r, 0);
  1843. c->data = hc;
  1844. ngx_add_timer(rev, clcf->keepalive_timeout);
  1845. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  1846. ngx_http_close_connection(c);
  1847. return;
  1848. }
  1849. wev = c->write;
  1850. wev->handler = ngx_http_empty_handler;
  1851. if (b->pos < b->last) {
  1852. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "pipelined request");
  1853. #if (NGX_STAT_STUB)
  1854. (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
  1855. #endif
  1856. hc->pipeline = 1;
  1857. c->log->action = "reading client pipelined request line";
  1858. rev->handler = ngx_http_init_request;
  1859. ngx_post_event(rev, &ngx_posted_events);
  1860. return;
  1861. }
  1862. hc->pipeline = 0;
  1863. /*
  1864. * To keep a memory footprint as small as possible for an idle
  1865. * keepalive connection we try to free the ngx_http_request_t and
  1866. * c->buffer's memory if they were allocated outside the c->pool.
  1867. * The large header buffers are always allocated outside the c->pool and
  1868. * are freed too.
  1869. */
  1870. if (ngx_pfree(c->pool, r) == NGX_OK) {
  1871. hc->request = NULL;
  1872. }
  1873. b = c->buffer;
  1874. if (ngx_pfree(c->pool, b->start) == NGX_OK) {
  1875. /*
  1876. * the special note for ngx_http_keepalive_handler() that
  1877. * c->buffer's memory was freed
  1878. */
  1879. b->pos = NULL;
  1880. } else {
  1881. b->pos = b->start;
  1882. b->last = b->start;
  1883. }
  1884. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc free: %p %d",
  1885. hc->free, hc->nfree);
  1886. if (hc->free) {
  1887. for (i = 0; i < hc->nfree; i++) {
  1888. ngx_pfree(c->pool, hc->free[i]->start);
  1889. hc->free[i] = NULL;
  1890. }
  1891. hc->nfree = 0;
  1892. }
  1893. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0, "hc busy: %p %d",
  1894. hc->busy, hc->nbusy);
  1895. if (hc->busy) {
  1896. for (i = 0; i < hc->nbusy; i++) {
  1897. ngx_pfree(c->pool, hc->busy[i]->start);
  1898. hc->busy[i] = NULL;
  1899. }
  1900. hc->nbusy = 0;
  1901. }
  1902. #if (NGX_HTTP_SSL)
  1903. if (c->ssl) {
  1904. ngx_ssl_free_buffer(c);
  1905. }
  1906. #endif
  1907. rev->handler = ngx_http_keepalive_handler;
  1908. if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
  1909. if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
  1910. ngx_http_close_connection(c);
  1911. return;
  1912. }
  1913. }
  1914. c->log->action = "keepalive";
  1915. if (c->tcp_nopush == NGX_TCP_NOPUSH_SET) {
  1916. if (ngx_tcp_push(c->fd) == -1) {
  1917. ngx_connection_error(c, ngx_socket_errno, ngx_tcp_push_n " failed");
  1918. ngx_http_close_connection(c);
  1919. return;
  1920. }
  1921. c->tcp_nopush = NGX_TCP_NOPUSH_UNSET;
  1922. tcp_nodelay = ngx_tcp_nodelay_and_tcp_nopush ? 1 : 0;
  1923. } else {
  1924. tcp_nodelay = 1;
  1925. }
  1926. if (tcp_nodelay
  1927. && clcf->tcp_nodelay
  1928. && c->tcp_nodelay == NGX_TCP_NODELAY_UNSET)
  1929. {
  1930. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "tcp_nodelay");
  1931. if (setsockopt(c->fd, IPPROTO_TCP, TCP_NODELAY,
  1932. (const void *) &tcp_nodelay, sizeof(int))
  1933. == -1)
  1934. {
  1935. #if (NGX_SOLARIS)
  1936. /* Solaris returns EINVAL if a socket has been shut down */
  1937. c->log_error = NGX_ERROR_IGNORE_EINVAL;
  1938. #endif
  1939. ngx_connection_error(c, ngx_socket_errno,
  1940. "setsockopt(TCP_NODELAY) failed");
  1941. c->log_error = NGX_ERROR_INFO;
  1942. ngx_http_close_connection(c);
  1943. return;
  1944. }
  1945. c->tcp_nodelay = NGX_TCP_NODELAY_SET;
  1946. }
  1947. #if 0
  1948. /* if ngx_http_request_t was freed then we need some other place */
  1949. r->http_state = NGX_HTTP_KEEPALIVE_STATE;
  1950. #endif
  1951. c->idle = 1;
  1952. ngx_reusable_connection(c, 1);
  1953. if (rev->ready) {
  1954. ngx_post_event(rev, &ngx_posted_events);
  1955. }
  1956. }
  1957. static void
  1958. ngx_http_keepalive_handler(ngx_event_t *rev)
  1959. {
  1960. size_t size;
  1961. ssize_t n;
  1962. ngx_buf_t *b;
  1963. ngx_connection_t *c;
  1964. c = rev->data;
  1965. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0, "http keepalive handler");
  1966. if (rev->timedout || c->close) {
  1967. ngx_http_close_connection(c);
  1968. return;
  1969. }
  1970. #if (NGX_HAVE_KQUEUE)
  1971. if (ngx_event_flags & NGX_USE_KQUEUE_EVENT) {
  1972. if (rev->pending_eof) {
  1973. c->log->handler = NULL;
  1974. ngx_log_error(NGX_LOG_INFO, c->log, rev->kq_errno,
  1975. "kevent() reported that client %V closed "
  1976. "keepalive connection", &c->addr_text);
  1977. #if (NGX_HTTP_SSL)
  1978. if (c->ssl) {
  1979. c->ssl->no_send_shutdown = 1;
  1980. }
  1981. #endif
  1982. ngx_http_close_connection(c);
  1983. return;
  1984. }
  1985. }
  1986. #endif
  1987. b = c->buffer;
  1988. size = b->end - b->start;
  1989. if (b->pos == NULL) {
  1990. /*
  1991. * The c->buffer's memory was freed by ngx_http_set_keepalive().
  1992. * However, the c->buffer->start and c->buffer->end were not changed
  1993. * to keep the buffer size.
  1994. */
  1995. b->pos = ngx_palloc(c->pool, size);
  1996. if (b->pos == NULL) {
  1997. ngx_http_close_connection(c);
  1998. return;
  1999. }
  2000. b->start = b->pos;
  2001. b->last = b->pos;
  2002. b->end = b->pos + size;
  2003. }
  2004. /*
  2005. * MSIE closes a keepalive connection with RST flag
  2006. * so we ignore ECONNRESET here.
  2007. */
  2008. c->log_error = NGX_ERROR_IGNORE_ECONNRESET;
  2009. ngx_set_socket_errno(0);
  2010. n = c->recv(c, b->last, size);
  2011. c->log_error = NGX_ERROR_INFO;
  2012. if (n == NGX_AGAIN) {
  2013. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  2014. ngx_http_close_connection(c);
  2015. }
  2016. return;
  2017. }
  2018. if (n == NGX_ERROR) {
  2019. ngx_http_close_connection(c);
  2020. return;
  2021. }
  2022. c->log->handler = NULL;
  2023. if (n == 0) {
  2024. ngx_log_error(NGX_LOG_INFO, c->log, ngx_socket_errno,
  2025. "client %V closed keepalive connection", &c->addr_text);
  2026. ngx_http_close_connection(c);
  2027. return;
  2028. }
  2029. b->last += n;
  2030. #if (NGX_STAT_STUB)
  2031. (void) ngx_atomic_fetch_add(ngx_stat_reading, 1);
  2032. #endif
  2033. c->log->handler = ngx_http_log_error;
  2034. c->log->action = "reading client request line";
  2035. c->idle = 0;
  2036. ngx_reusable_connection(c, 0);
  2037. ngx_http_init_request(rev);
  2038. }
  2039. static void
  2040. ngx_http_set_lingering_close(ngx_http_request_t *r)
  2041. {
  2042. ngx_event_t *rev, *wev;
  2043. ngx_connection_t *c;
  2044. ngx_http_core_loc_conf_t *clcf;
  2045. c = r->connection;
  2046. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  2047. rev = c->read;
  2048. rev->handler = ngx_http_lingering_close_handler;
  2049. r->lingering_time = ngx_time() + (time_t) (clcf->lingering_time / 1000);
  2050. ngx_add_timer(rev, clcf->lingering_timeout);
  2051. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  2052. ngx_http_close_request(r, 0);
  2053. return;
  2054. }
  2055. wev = c->write;
  2056. wev->handler = ngx_http_empty_handler;
  2057. if (wev->active && (ngx_event_flags & NGX_USE_LEVEL_EVENT)) {
  2058. if (ngx_del_event(wev, NGX_WRITE_EVENT, 0) != NGX_OK) {
  2059. ngx_http_close_request(r, 0);
  2060. return;
  2061. }
  2062. }
  2063. if (ngx_shutdown_socket(c->fd, NGX_WRITE_SHUTDOWN) == -1) {
  2064. ngx_connection_error(c, ngx_socket_errno,
  2065. ngx_shutdown_socket_n " failed");
  2066. ngx_http_close_request(r, 0);
  2067. return;
  2068. }
  2069. if (rev->ready) {
  2070. ngx_http_lingering_close_handler(rev);
  2071. }
  2072. }
  2073. static void
  2074. ngx_http_lingering_close_handler(ngx_event_t *rev)
  2075. {
  2076. ssize_t n;
  2077. ngx_msec_t timer;
  2078. ngx_connection_t *c;
  2079. ngx_http_request_t *r;
  2080. ngx_http_core_loc_conf_t *clcf;
  2081. u_char buffer[NGX_HTTP_LINGERING_BUFFER_SIZE];
  2082. c = rev->data;
  2083. r = c->data;
  2084. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, c->log, 0,
  2085. "http lingering close handler");
  2086. if (rev->timedout) {
  2087. ngx_http_close_request(r, 0);
  2088. return;
  2089. }
  2090. timer = (ngx_msec_t) (r->lingering_time - ngx_time());
  2091. if (timer <= 0) {
  2092. ngx_http_close_request(r, 0);
  2093. return;
  2094. }
  2095. do {
  2096. n = c->recv(c, buffer, NGX_HTTP_LINGERING_BUFFER_SIZE);
  2097. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0, "lingering read: %d", n);
  2098. if (n == NGX_ERROR || n == 0) {
  2099. ngx_http_close_request(r, 0);
  2100. return;
  2101. }
  2102. } while (rev->ready);
  2103. if (ngx_handle_read_event(rev, 0) != NGX_OK) {
  2104. ngx_http_close_request(r, 0);
  2105. return;
  2106. }
  2107. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  2108. timer *= 1000;
  2109. if (timer > clcf->lingering_timeout) {
  2110. timer = clcf->lingering_timeout;
  2111. }
  2112. ngx_add_timer(rev, timer);
  2113. }
  2114. void
  2115. ngx_http_empty_handler(ngx_event_t *wev)
  2116. {
  2117. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, wev->log, 0, "http empty handler");
  2118. return;
  2119. }
  2120. void
  2121. ngx_http_request_empty_handler(ngx_http_request_t *r)
  2122. {
  2123. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2124. "http request empty handler");
  2125. return;
  2126. }
  2127. ngx_int_t
  2128. ngx_http_send_special(ngx_http_request_t *r, ngx_uint_t flags)
  2129. {
  2130. ngx_buf_t *b;
  2131. ngx_chain_t out;
  2132. b = ngx_calloc_buf(r->pool);
  2133. if (b == NULL) {
  2134. return NGX_ERROR;
  2135. }
  2136. if (flags & NGX_HTTP_LAST) {
  2137. if (r == r->main && !r->post_action) {
  2138. b->last_buf = 1;
  2139. } else {
  2140. b->sync = 1;
  2141. b->last_in_chain = 1;
  2142. }
  2143. }
  2144. if (flags & NGX_HTTP_FLUSH) {
  2145. b->flush = 1;
  2146. }
  2147. out.buf = b;
  2148. out.next = NULL;
  2149. return ngx_http_output_filter(r, &out);
  2150. }
  2151. static ngx_int_t
  2152. ngx_http_post_action(ngx_http_request_t *r)
  2153. {
  2154. ngx_http_core_loc_conf_t *clcf;
  2155. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  2156. if (clcf->post_action.data == NULL) {
  2157. return NGX_DECLINED;
  2158. }
  2159. if (r->post_action && r->uri_changes == 0) {
  2160. return NGX_DECLINED;
  2161. }
  2162. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, r->connection->log, 0,
  2163. "post action: \"%V\"", &clcf->post_action);
  2164. r->main->count--;
  2165. r->http_version = NGX_HTTP_VERSION_9;
  2166. r->header_only = 1;
  2167. r->post_action = 1;
  2168. r->read_event_handler = ngx_http_block_reading;
  2169. if (clcf->post_action.data[0] == '/') {
  2170. ngx_http_internal_redirect(r, &clcf->post_action, NULL);
  2171. } else {
  2172. ngx_http_named_location(r, &clcf->post_action);
  2173. }
  2174. return NGX_OK;
  2175. }
  2176. static void
  2177. ngx_http_close_request(ngx_http_request_t *r, ngx_int_t rc)
  2178. {
  2179. ngx_connection_t *c;
  2180. r = r->main;
  2181. c = r->connection;
  2182. ngx_log_debug2(NGX_LOG_DEBUG_HTTP, c->log, 0,
  2183. "http request count:%d blk:%d", r->count, r->blocked);
  2184. if (r->count == 0) {
  2185. ngx_log_error(NGX_LOG_ALERT, c->log, 0, "http request count is zero");
  2186. }
  2187. r->count--;
  2188. if (r->count || r->blocked) {
  2189. return;
  2190. }
  2191. ngx_http_free_request(r, rc);
  2192. ngx_http_close_connection(c);
  2193. }
  2194. static void
  2195. ngx_http_free_request(ngx_http_request_t *r, ngx_int_t rc)
  2196. {
  2197. ngx_log_t *log;
  2198. struct linger linger;
  2199. ngx_http_cleanup_t *cln;
  2200. ngx_http_log_ctx_t *ctx;
  2201. ngx_http_core_loc_conf_t *clcf;
  2202. log = r->connection->log;
  2203. ngx_log_debug0(NGX_LOG_DEBUG_HTTP, log, 0, "http close request");
  2204. if (r->pool == NULL) {
  2205. ngx_log_error(NGX_LOG_ALERT, log, 0, "http request already closed");
  2206. return;
  2207. }
  2208. for (cln = r->cleanup; cln; cln = cln->next) {
  2209. if (cln->handler) {
  2210. cln->handler(cln->data);
  2211. }
  2212. }
  2213. #if (NGX_STAT_STUB)
  2214. if (r->stat_reading) {
  2215. (void) ngx_atomic_fetch_add(ngx_stat_reading, -1);
  2216. }
  2217. if (r->stat_writing) {
  2218. (void) ngx_atomic_fetch_add(ngx_stat_writing, -1);
  2219. }
  2220. #endif
  2221. if (rc > 0 && (r->headers_out.status == 0 || r->connection->sent == 0)) {
  2222. r->headers_out.status = rc;
  2223. }
  2224. log->action = "logging request";
  2225. ngx_http_log_request(r);
  2226. log->action = "closing request";
  2227. if (r->connection->timedout) {
  2228. clcf = ngx_http_get_module_loc_conf(r, ngx_http_core_module);
  2229. if (clcf->reset_timedout_connection) {
  2230. linger.l_onoff = 1;
  2231. linger.l_linger = 0;
  2232. if (setsockopt(r->connection->fd, SOL_SOCKET, SO_LINGER,
  2233. (const void *) &linger, sizeof(struct linger)) == -1)
  2234. {
  2235. ngx_log_error(NGX_LOG_ALERT, log, ngx_socket_errno,
  2236. "setsockopt(SO_LINGER) failed");
  2237. }
  2238. }
  2239. }
  2240. /* the various request strings were allocated from r->pool */
  2241. ctx = log->data;
  2242. ctx->request = NULL;
  2243. r->request_line.len = 0;
  2244. r->connection->destroyed = 1;
  2245. ngx_destroy_pool(r->pool);
  2246. }
  2247. static void
  2248. ngx_http_log_request(ngx_http_request_t *r)
  2249. {
  2250. ngx_uint_t i, n;
  2251. ngx_http_handler_pt *log_handler;
  2252. ngx_http_core_main_conf_t *cmcf;
  2253. cmcf = ngx_http_get_module_main_conf(r, ngx_http_core_module);
  2254. log_handler = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.elts;
  2255. n = cmcf->phases[NGX_HTTP_LOG_PHASE].handlers.nelts;
  2256. for (i = 0; i < n; i++) {
  2257. log_handler[i](r);
  2258. }
  2259. }
  2260. static void
  2261. ngx_http_close_connection(ngx_connection_t *c)
  2262. {
  2263. ngx_pool_t *pool;
  2264. ngx_log_debug1(NGX_LOG_DEBUG_HTTP, c->log, 0,
  2265. "close http connection: %d", c->fd);
  2266. #if (NGX_HTTP_SSL)
  2267. if (c->ssl) {
  2268. if (ngx_ssl_shutdown(c) == NGX_AGAIN) {
  2269. c->ssl->handler = ngx_http_close_connection;
  2270. return;
  2271. }
  2272. }
  2273. #endif
  2274. #if (NGX_STAT_STUB)
  2275. (void) ngx_atomic_fetch_add(ngx_stat_active, -1);
  2276. #endif
  2277. c->destroyed = 1;
  2278. pool = c->pool;
  2279. ngx_close_connection(c);
  2280. ngx_destroy_pool(pool);
  2281. }
  2282. static u_char *
  2283. ngx_http_log_error(ngx_log_t *log, u_char *buf, size_t len)
  2284. {
  2285. u_char *p;
  2286. ngx_http_request_t *r;
  2287. ngx_http_log_ctx_t *ctx;
  2288. if (log->action) {
  2289. p = ngx_snprintf(buf, len, " while %s", log->action);
  2290. len -= p - buf;
  2291. buf = p;
  2292. }
  2293. ctx = log->data;
  2294. p = ngx_snprintf(buf, len, ", client: %V", &ctx->connection->addr_text);
  2295. len -= p - buf;
  2296. r = ctx->request;
  2297. if (r) {
  2298. return r->log_handler(r, ctx->current_request, p, len);
  2299. } else {
  2300. p = ngx_snprintf(p, len, ", server: %V",
  2301. &ctx->connection->listening->addr_text);
  2302. }
  2303. return p;
  2304. }
  2305. static u_char *
  2306. ngx_http_log_error_handler(ngx_http_request_t *r, ngx_http_request_t *sr,
  2307. u_char *buf, size_t len)
  2308. {
  2309. char *uri_separator;
  2310. u_char *p;
  2311. ngx_http_upstream_t *u;
  2312. ngx_http_core_srv_conf_t *cscf;
  2313. cscf = ngx_http_get_module_srv_conf(r, ngx_http_core_module);
  2314. p = ngx_snprintf(buf, len, ", server: %V", &cscf->server_name);
  2315. len -= p - buf;
  2316. buf = p;
  2317. if (r->request_line.data == NULL && r->request_start) {
  2318. for (p = r->request_start; p < r->header_in->last; p++) {
  2319. if (*p == CR || *p == LF) {
  2320. break;
  2321. }
  2322. }
  2323. r->request_line.len = p - r->request_start;
  2324. r->request_line.data = r->request_start;
  2325. }
  2326. if (r->request_line.len) {
  2327. p = ngx_snprintf(buf, len, ", request: \"%V\"", &r->request_line);
  2328. len -= p - buf;
  2329. buf = p;
  2330. }
  2331. if (r != sr) {
  2332. p = ngx_snprintf(buf, len, ", subrequest: \"%V\"", &sr->uri);
  2333. len -= p - buf;
  2334. buf = p;
  2335. }
  2336. u = sr->upstream;
  2337. if (u && u->peer.name) {
  2338. uri_separator = "";
  2339. #if (NGX_HAVE_UNIX_DOMAIN)
  2340. if (u->peer.sockaddr && u->peer.sockaddr->sa_family == AF_UNIX) {
  2341. uri_separator = ":";
  2342. }
  2343. #endif
  2344. p = ngx_snprintf(buf, len, ", upstream: \"%V%V%s%V\"",
  2345. &u->schema, u->peer.name,
  2346. uri_separator, &u->uri);
  2347. len -= p - buf;
  2348. buf = p;
  2349. }
  2350. if (r->headers_in.host) {
  2351. p = ngx_snprintf(buf, len, ", host: \"%V\"",
  2352. &r->headers_in.host->value);
  2353. len -= p - buf;
  2354. buf = p;
  2355. }
  2356. if (r->headers_in.referer) {
  2357. p = ngx_snprintf(buf, len, ", referrer: \"%V\"",
  2358. &r->headers_in.referer->value);
  2359. buf = p;
  2360. }
  2361. return buf;
  2362. }