PageRenderTime 72ms CodeModel.GetById 17ms RepoModel.GetById 1ms 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

Large files files are truncated, but you can click here to view the full 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. cs

Large files files are truncated, but you can click here to view the full file