Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 1 | /* |
| 2 | * Client-side variables and functions. |
| 3 | * |
| 4 | * Copyright 2000-2006 Willy Tarreau <w@1wt.eu> |
| 5 | * |
| 6 | * This program is free software; you can redistribute it and/or |
| 7 | * modify it under the terms of the GNU General Public License |
| 8 | * as published by the Free Software Foundation; either version |
| 9 | * 2 of the License, or (at your option) any later version. |
| 10 | * |
| 11 | */ |
| 12 | |
| 13 | #include <errno.h> |
| 14 | #include <fcntl.h> |
| 15 | #include <stdio.h> |
| 16 | #include <stdlib.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 17 | #include <string.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 18 | |
| 19 | #include <sys/socket.h> |
| 20 | #include <sys/stat.h> |
| 21 | #include <sys/types.h> |
| 22 | |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 23 | #include <common/compat.h> |
Willy Tarreau | e3ba5f0 | 2006-06-29 18:54:54 +0200 | [diff] [blame] | 24 | #include <common/config.h> |
Willy Tarreau | 2dd0d47 | 2006-06-29 17:53:05 +0200 | [diff] [blame] | 25 | #include <common/time.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 26 | |
| 27 | #include <types/backend.h> |
| 28 | #include <types/buffers.h> |
| 29 | #include <types/global.h> |
| 30 | #include <types/httperr.h> |
| 31 | #include <types/polling.h> |
| 32 | #include <types/proxy.h> |
| 33 | #include <types/server.h> |
| 34 | #include <types/session.h> |
| 35 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 36 | #include <proto/buffers.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 37 | #include <proto/client.h> |
| 38 | #include <proto/fd.h> |
| 39 | #include <proto/log.h> |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 40 | #include <proto/hdr_idx.h> |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 41 | #include <proto/proto_http.h> |
| 42 | #include <proto/stream_sock.h> |
| 43 | #include <proto/task.h> |
| 44 | |
| 45 | |
| 46 | |
| 47 | /* |
| 48 | * FIXME: This should move to the STREAM_SOCK code then split into TCP and HTTP. |
| 49 | */ |
| 50 | |
| 51 | /* |
| 52 | * this function is called on a read event from a listen socket, corresponding |
| 53 | * to an accept. It tries to accept as many connections as possible. |
| 54 | * It returns 0. |
| 55 | */ |
| 56 | int event_accept(int fd) { |
| 57 | struct proxy *p = (struct proxy *)fdtab[fd].owner; |
| 58 | struct session *s; |
| 59 | struct task *t; |
| 60 | int cfd; |
| 61 | int max_accept; |
| 62 | |
| 63 | if (global.nbproc > 1) |
| 64 | max_accept = 8; /* let other processes catch some connections too */ |
| 65 | else |
| 66 | max_accept = -1; |
| 67 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 68 | while (p->feconn < p->maxconn && max_accept--) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 69 | struct sockaddr_storage addr; |
| 70 | socklen_t laddr = sizeof(addr); |
| 71 | |
| 72 | if ((cfd = accept(fd, (struct sockaddr *)&addr, &laddr)) == -1) { |
| 73 | switch (errno) { |
| 74 | case EAGAIN: |
| 75 | case EINTR: |
| 76 | case ECONNABORTED: |
| 77 | return 0; /* nothing more to accept */ |
| 78 | case ENFILE: |
| 79 | send_log(p, LOG_EMERG, |
| 80 | "Proxy %s reached system FD limit at %d. Please check system tunables.\n", |
| 81 | p->id, maxfd); |
| 82 | return 0; |
| 83 | case EMFILE: |
| 84 | send_log(p, LOG_EMERG, |
| 85 | "Proxy %s reached process FD limit at %d. Please check 'ulimit-n' and restart.\n", |
| 86 | p->id, maxfd); |
| 87 | return 0; |
| 88 | case ENOBUFS: |
| 89 | case ENOMEM: |
| 90 | send_log(p, LOG_EMERG, |
| 91 | "Proxy %s reached system memory limit at %d sockets. Please check system tunables.\n", |
| 92 | p->id, maxfd); |
| 93 | return 0; |
| 94 | default: |
| 95 | return 0; |
| 96 | } |
| 97 | } |
| 98 | |
| 99 | if ((s = pool_alloc(session)) == NULL) { /* disable this proxy for a while */ |
| 100 | Alert("out of memory in event_accept().\n"); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 101 | MY_FD_CLR(fd, StaticReadEvent); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 102 | p->state = PR_STIDLE; |
| 103 | close(cfd); |
| 104 | return 0; |
| 105 | } |
| 106 | |
| 107 | /* if this session comes from a known monitoring system, we want to ignore |
| 108 | * it as soon as possible, which means closing it immediately for TCP. |
| 109 | */ |
| 110 | s->flags = 0; |
| 111 | if (addr.ss_family == AF_INET && |
| 112 | p->mon_mask.s_addr && |
| 113 | (((struct sockaddr_in *)&addr)->sin_addr.s_addr & p->mon_mask.s_addr) == p->mon_net.s_addr) { |
| 114 | if (p->mode == PR_MODE_TCP) { |
| 115 | close(cfd); |
| 116 | pool_free(session, s); |
| 117 | continue; |
| 118 | } |
| 119 | s->flags |= SN_MONITOR; |
| 120 | } |
| 121 | |
| 122 | if ((t = pool_alloc(task)) == NULL) { /* disable this proxy for a while */ |
| 123 | Alert("out of memory in event_accept().\n"); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 124 | MY_FD_CLR(fd, StaticReadEvent); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 125 | p->state = PR_STIDLE; |
| 126 | close(cfd); |
| 127 | pool_free(session, s); |
| 128 | return 0; |
| 129 | } |
| 130 | |
| 131 | s->cli_addr = addr; |
| 132 | if (cfd >= global.maxsock) { |
| 133 | Alert("accept(): not enough free sockets. Raise -n argument. Giving up.\n"); |
| 134 | close(cfd); |
| 135 | pool_free(task, t); |
| 136 | pool_free(session, s); |
| 137 | return 0; |
| 138 | } |
| 139 | |
| 140 | if ((fcntl(cfd, F_SETFL, O_NONBLOCK) == -1) || |
| 141 | (setsockopt(cfd, IPPROTO_TCP, TCP_NODELAY, |
| 142 | (char *) &one, sizeof(one)) == -1)) { |
| 143 | Alert("accept(): cannot set the socket in non blocking mode. Giving up\n"); |
| 144 | close(cfd); |
| 145 | pool_free(task, t); |
| 146 | pool_free(session, s); |
| 147 | return 0; |
| 148 | } |
| 149 | |
| 150 | if (p->options & PR_O_TCP_CLI_KA) |
| 151 | setsockopt(cfd, SOL_SOCKET, SO_KEEPALIVE, (char *) &one, sizeof(one)); |
| 152 | |
| 153 | t->next = t->prev = t->rqnext = NULL; /* task not in run queue yet */ |
| 154 | t->wq = LIST_HEAD(wait_queue[0]); /* but already has a wait queue assigned */ |
| 155 | t->state = TASK_IDLE; |
| 156 | t->process = process_session; |
| 157 | t->context = s; |
| 158 | |
| 159 | s->task = t; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 160 | s->be = s->fe = p; |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 161 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 162 | s->cli_state = (p->mode == PR_MODE_HTTP) ? CL_STHEADERS : CL_STDATA; /* no HTTP headers for non-HTTP proxies */ |
| 163 | s->srv_state = SV_STIDLE; |
| 164 | s->req = s->rep = NULL; /* will be allocated later */ |
| 165 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 166 | s->cli_fd = cfd; |
| 167 | s->srv_fd = -1; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 168 | s->srv = NULL; |
| 169 | s->pend_pos = NULL; |
Willy Tarreau | ddb358d | 2006-12-17 22:55:52 +0100 | [diff] [blame] | 170 | s->conn_retries = p->beprm->conn_retries; |
| 171 | |
| 172 | /* FIXME: the logs are horribly complicated now, because they are |
| 173 | * defined in <p>, <p->beprm>, and later <be> and <be->beprm>. |
| 174 | */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 175 | |
| 176 | if (s->flags & SN_MONITOR) |
| 177 | s->logs.logwait = 0; |
| 178 | else |
| 179 | s->logs.logwait = p->to_log; |
| 180 | |
| 181 | s->logs.tv_accept = now; |
| 182 | s->logs.t_request = -1; |
| 183 | s->logs.t_queue = -1; |
| 184 | s->logs.t_connect = -1; |
| 185 | s->logs.t_data = -1; |
| 186 | s->logs.t_close = 0; |
| 187 | s->logs.uri = NULL; |
| 188 | s->logs.cli_cookie = NULL; |
| 189 | s->logs.srv_cookie = NULL; |
| 190 | s->logs.status = -1; |
Willy Tarreau | 35d66b0 | 2007-01-02 00:28:21 +0100 | [diff] [blame] | 191 | s->logs.bytes_in = s->logs.bytes_out = 0; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 192 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 193 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 194 | |
| 195 | s->data_source = DATA_SRC_NONE; |
| 196 | |
| 197 | s->uniq_id = totalconn; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 198 | p->cum_feconn++; /* cum_beconn will be increased once assigned */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 199 | |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 200 | s->rsp_cap = NULL; |
| 201 | s->hreq.cap = NULL; |
| 202 | s->hreq.hdr_idx.v = NULL; |
| 203 | s->hreq.hdr_idx.size = s->hreq.hdr_idx.used = 0; |
| 204 | |
| 205 | if (p->mode == PR_MODE_HTTP) { |
| 206 | s->hreq.hdr_state = HTTP_PA_EMPTY; /* at the very beginning of the request */ |
| 207 | s->hreq.start.len = -1; |
| 208 | s->hreq.auth_hdr.len = -1; |
| 209 | s->hreq.sor = s->hreq.eoh = 0; /* relative to the buffer */ |
| 210 | |
| 211 | s->hreq.hdr_idx.size = MAX_HTTP_HDR; |
| 212 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 213 | if (p->fiprm->nb_req_cap > 0) { |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 214 | if ((s->hreq.cap = |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 215 | pool_alloc_from(p->fiprm->req_cap_pool, p->fiprm->nb_req_cap*sizeof(char *))) |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 216 | == NULL) { /* no memory */ |
| 217 | close(cfd); /* nothing can be done for this fd without memory */ |
| 218 | pool_free(task, t); |
| 219 | pool_free(session, s); |
| 220 | return 0; |
| 221 | } |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 222 | memset(s->hreq.cap, 0, p->fiprm->nb_req_cap*sizeof(char *)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 223 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 224 | |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 225 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 226 | if (p->fiprm->nb_rsp_cap > 0) { |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 227 | if ((s->rsp_cap = |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 228 | pool_alloc_from(p->fiprm->rsp_cap_pool, p->fiprm->nb_rsp_cap*sizeof(char *))) |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 229 | == NULL) { /* no memory */ |
| 230 | if (s->hreq.cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 231 | pool_free_to(p->fiprm->req_cap_pool, s->hreq.cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 232 | close(cfd); /* nothing can be done for this fd without memory */ |
| 233 | pool_free(task, t); |
| 234 | pool_free(session, s); |
| 235 | return 0; |
| 236 | } |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 237 | memset(s->rsp_cap, 0, p->fiprm->nb_rsp_cap*sizeof(char *)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 238 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 239 | |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 240 | |
| 241 | if ((s->hreq.hdr_idx.v = |
| 242 | pool_alloc_from(p->hdr_idx_pool, s->hreq.hdr_idx.size*sizeof(*s->hreq.hdr_idx.v))) |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 243 | == NULL) { /* no memory */ |
| 244 | if (s->rsp_cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 245 | pool_free_to(p->fiprm->rsp_cap_pool, s->rsp_cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 246 | if (s->hreq.cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 247 | pool_free_to(p->fiprm->req_cap_pool, s->hreq.cap); |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 248 | close(cfd); /* nothing can be done for this fd without memory */ |
| 249 | pool_free(task, t); |
| 250 | pool_free(session, s); |
| 251 | return 0; |
| 252 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 253 | hdr_idx_init(&s->hreq.hdr_idx); |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 254 | } |
| 255 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 256 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 257 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
| 258 | struct sockaddr_storage sockname; |
| 259 | socklen_t namelen = sizeof(sockname); |
| 260 | |
| 261 | if (addr.ss_family != AF_INET || |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 262 | !(s->fe->options & PR_O_TRANSP) || |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 263 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 264 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 265 | |
| 266 | if (p->to_log) { |
| 267 | /* we have the client ip */ |
| 268 | if (s->logs.logwait & LW_CLIP) |
| 269 | if (!(s->logs.logwait &= ~LW_CLIP)) |
| 270 | sess_log(s); |
| 271 | } |
| 272 | else if (s->cli_addr.ss_family == AF_INET) { |
| 273 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 274 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 275 | sn, sizeof(sn)) && |
| 276 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 277 | pn, sizeof(pn))) { |
| 278 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 279 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 280 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 281 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 282 | } |
| 283 | } |
| 284 | else { |
| 285 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 286 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 287 | sn, sizeof(sn)) && |
| 288 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 289 | pn, sizeof(pn))) { |
| 290 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 291 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 292 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 293 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 294 | } |
| 295 | } |
| 296 | } |
| 297 | |
| 298 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 299 | struct sockaddr_in sockname; |
| 300 | socklen_t namelen = sizeof(sockname); |
| 301 | int len; |
| 302 | if (addr.ss_family != AF_INET || |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 303 | !(s->fe->options & PR_O_TRANSP) || |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 304 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 305 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 306 | |
| 307 | if (s->cli_addr.ss_family == AF_INET) { |
| 308 | char pn[INET_ADDRSTRLEN]; |
| 309 | inet_ntop(AF_INET, |
| 310 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 311 | pn, sizeof(pn)); |
| 312 | |
| 313 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 314 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 315 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 316 | } |
| 317 | else { |
| 318 | char pn[INET6_ADDRSTRLEN]; |
| 319 | inet_ntop(AF_INET6, |
| 320 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 321 | pn, sizeof(pn)); |
| 322 | |
| 323 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 324 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 325 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 326 | } |
| 327 | |
| 328 | write(1, trash, len); |
| 329 | } |
| 330 | |
| 331 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 332 | if (s->hreq.hdr_idx.v != NULL) |
| 333 | pool_free_to(p->hdr_idx_pool, s->hreq.hdr_idx.v); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 334 | if (s->rsp_cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 335 | pool_free_to(p->fiprm->rsp_cap_pool, s->rsp_cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 336 | if (s->hreq.cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 337 | pool_free_to(p->fiprm->req_cap_pool, s->hreq.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 338 | close(cfd); /* nothing can be done for this fd without memory */ |
| 339 | pool_free(task, t); |
| 340 | pool_free(session, s); |
| 341 | return 0; |
| 342 | } |
| 343 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 344 | buffer_init(s->req); |
| 345 | s->req->rlim += BUFSIZE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 346 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
| 347 | s->req->rlim -= MAXREWRITE; |
| 348 | |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 349 | s->req->rto = s->fe->clitimeout; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 350 | s->req->wto = s->be->beprm->srvtimeout; |
| 351 | s->req->cto = s->be->beprm->srvtimeout; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 352 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 353 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 354 | pool_free(buffer, s->req); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 355 | if (s->hreq.hdr_idx.v != NULL) |
| 356 | pool_free_to(p->hdr_idx_pool, s->hreq.hdr_idx.v); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 357 | if (s->rsp_cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 358 | pool_free_to(p->fiprm->rsp_cap_pool, s->rsp_cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 359 | if (s->hreq.cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 360 | pool_free_to(p->fiprm->req_cap_pool, s->hreq.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 361 | close(cfd); /* nothing can be done for this fd without memory */ |
| 362 | pool_free(task, t); |
| 363 | pool_free(session, s); |
| 364 | return 0; |
| 365 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 366 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 367 | buffer_init(s->rep); |
| 368 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 369 | s->rep->rto = s->be->beprm->srvtimeout; |
| 370 | s->rep->wto = s->fe->clitimeout; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 371 | s->rep->cto = 0; |
| 372 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 373 | fdtab[cfd].owner = t; |
| 374 | fdtab[cfd].state = FD_STREADY; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 375 | fdtab[cfd].cb[DIR_RD].f = &stream_sock_read; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 376 | fdtab[cfd].cb[DIR_RD].b = s->req; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 377 | fdtab[cfd].cb[DIR_WR].f = &stream_sock_write; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 378 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 379 | |
| 380 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 381 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) { |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 382 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 383 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 384 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 385 | */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 386 | struct chunk msg = { .str = "HTTP/1.0 200 OK\r\n\r\n", .len = 19 }; |
| 387 | client_retnclose(s, &msg); /* forge a 200 response */ |
| 388 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 389 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
Willy Tarreau | 0f77253 | 2006-12-23 20:51:41 +0100 | [diff] [blame] | 390 | struct chunk msg = { .str = "OK\n", .len = 3 }; |
| 391 | client_retnclose(s, &msg); /* forge an "OK" response */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 392 | } |
| 393 | else { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 394 | MY_FD_SET(cfd, StaticReadEvent); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 395 | } |
| 396 | |
| 397 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 398 | if (PrevReadEvent) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 399 | assert(!(MY_FD_ISSET(cfd, PrevReadEvent))); |
| 400 | assert(!(MY_FD_ISSET(cfd, PrevWriteEvent))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 401 | } |
| 402 | #endif |
| 403 | fd_insert(cfd); |
| 404 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 405 | tv_eternity(&s->req->rex); |
| 406 | tv_eternity(&s->req->wex); |
| 407 | tv_eternity(&s->req->cex); |
| 408 | tv_eternity(&s->rep->rex); |
| 409 | tv_eternity(&s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 410 | |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 411 | if (s->fe->clitimeout) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 412 | if (MY_FD_ISSET(cfd, StaticReadEvent)) |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 413 | tv_delayfrom(&s->req->rex, &now, s->fe->clitimeout); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 414 | if (MY_FD_ISSET(cfd, StaticWriteEvent)) |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 415 | tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 416 | } |
| 417 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 418 | tv_min(&t->expire, &s->req->rex, &s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 419 | |
| 420 | task_queue(t); |
| 421 | |
| 422 | if (p->mode != PR_MODE_HEALTH) |
| 423 | task_wakeup(&rq, t); |
| 424 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 425 | p->feconn++; /* beconn will be increased later */ |
| 426 | if (p->feconn > p->feconn_max) |
| 427 | p->feconn_max = p->feconn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 428 | actconn++; |
| 429 | totalconn++; |
| 430 | |
| 431 | // fprintf(stderr, "accepting from %p => %d conn, %d total, task=%p\n", p, actconn, totalconn, t); |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame] | 432 | } /* end of while (p->feconn < p->maxconn) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 433 | return 0; |
| 434 | } |
| 435 | |
| 436 | |
| 437 | |
| 438 | /* |
| 439 | * Local variables: |
| 440 | * c-indent-level: 8 |
| 441 | * c-basic-offset: 8 |
| 442 | * End: |
| 443 | */ |