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; |
| 170 | s->conn_retries = p->conn_retries; |
| 171 | |
| 172 | if (s->flags & SN_MONITOR) |
| 173 | s->logs.logwait = 0; |
| 174 | else |
| 175 | s->logs.logwait = p->to_log; |
| 176 | |
| 177 | s->logs.tv_accept = now; |
| 178 | s->logs.t_request = -1; |
| 179 | s->logs.t_queue = -1; |
| 180 | s->logs.t_connect = -1; |
| 181 | s->logs.t_data = -1; |
| 182 | s->logs.t_close = 0; |
| 183 | s->logs.uri = NULL; |
| 184 | s->logs.cli_cookie = NULL; |
| 185 | s->logs.srv_cookie = NULL; |
| 186 | s->logs.status = -1; |
| 187 | s->logs.bytes = 0; |
| 188 | s->logs.prx_queue_size = 0; /* we get the number of pending conns before us */ |
| 189 | s->logs.srv_queue_size = 0; /* we will get this number soon */ |
| 190 | |
| 191 | s->data_source = DATA_SRC_NONE; |
| 192 | |
| 193 | s->uniq_id = totalconn; |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame^] | 194 | p->cum_feconn++; /* cum_beconn will be increased once assigned */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 195 | |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 196 | s->rsp_cap = NULL; |
| 197 | s->hreq.cap = NULL; |
| 198 | s->hreq.hdr_idx.v = NULL; |
| 199 | s->hreq.hdr_idx.size = s->hreq.hdr_idx.used = 0; |
| 200 | |
| 201 | if (p->mode == PR_MODE_HTTP) { |
| 202 | s->hreq.hdr_state = HTTP_PA_EMPTY; /* at the very beginning of the request */ |
| 203 | s->hreq.start.len = -1; |
| 204 | s->hreq.auth_hdr.len = -1; |
| 205 | s->hreq.sor = s->hreq.eoh = 0; /* relative to the buffer */ |
| 206 | |
| 207 | s->hreq.hdr_idx.size = MAX_HTTP_HDR; |
| 208 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 209 | if (p->fiprm->nb_req_cap > 0) { |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 210 | if ((s->hreq.cap = |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 211 | 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] | 212 | == NULL) { /* no memory */ |
| 213 | close(cfd); /* nothing can be done for this fd without memory */ |
| 214 | pool_free(task, t); |
| 215 | pool_free(session, s); |
| 216 | return 0; |
| 217 | } |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 218 | memset(s->hreq.cap, 0, p->fiprm->nb_req_cap*sizeof(char *)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 219 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 220 | |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 221 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 222 | if (p->fiprm->nb_rsp_cap > 0) { |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 223 | if ((s->rsp_cap = |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 224 | 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] | 225 | == NULL) { /* no memory */ |
| 226 | if (s->hreq.cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 227 | pool_free_to(p->fiprm->req_cap_pool, s->hreq.cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 228 | close(cfd); /* nothing can be done for this fd without memory */ |
| 229 | pool_free(task, t); |
| 230 | pool_free(session, s); |
| 231 | return 0; |
| 232 | } |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 233 | memset(s->rsp_cap, 0, p->fiprm->nb_rsp_cap*sizeof(char *)); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 234 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 235 | |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 236 | |
| 237 | if ((s->hreq.hdr_idx.v = |
| 238 | 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] | 239 | == NULL) { /* no memory */ |
| 240 | if (s->rsp_cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 241 | pool_free_to(p->fiprm->rsp_cap_pool, s->rsp_cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 242 | if (s->hreq.cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 243 | pool_free_to(p->fiprm->req_cap_pool, s->hreq.cap); |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 244 | close(cfd); /* nothing can be done for this fd without memory */ |
| 245 | pool_free(task, t); |
| 246 | pool_free(session, s); |
| 247 | return 0; |
| 248 | } |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 249 | hdr_idx_init(&s->hreq.hdr_idx); |
Willy Tarreau | e5f20dc | 2006-12-03 15:21:35 +0100 | [diff] [blame] | 250 | } |
| 251 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 252 | if ((p->mode == PR_MODE_TCP || p->mode == PR_MODE_HTTP) |
| 253 | && (p->logfac1 >= 0 || p->logfac2 >= 0)) { |
| 254 | struct sockaddr_storage sockname; |
| 255 | socklen_t namelen = sizeof(sockname); |
| 256 | |
| 257 | if (addr.ss_family != AF_INET || |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 258 | !(s->fe->options & PR_O_TRANSP) || |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 259 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 260 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 261 | |
| 262 | if (p->to_log) { |
| 263 | /* we have the client ip */ |
| 264 | if (s->logs.logwait & LW_CLIP) |
| 265 | if (!(s->logs.logwait &= ~LW_CLIP)) |
| 266 | sess_log(s); |
| 267 | } |
| 268 | else if (s->cli_addr.ss_family == AF_INET) { |
| 269 | char pn[INET_ADDRSTRLEN], sn[INET_ADDRSTRLEN]; |
| 270 | if (inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&sockname)->sin_addr, |
| 271 | sn, sizeof(sn)) && |
| 272 | inet_ntop(AF_INET, (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 273 | pn, sizeof(pn))) { |
| 274 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 275 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port), |
| 276 | sn, ntohs(((struct sockaddr_in *)&sockname)->sin_port), |
| 277 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 278 | } |
| 279 | } |
| 280 | else { |
| 281 | char pn[INET6_ADDRSTRLEN], sn[INET6_ADDRSTRLEN]; |
| 282 | if (inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&sockname)->sin6_addr, |
| 283 | sn, sizeof(sn)) && |
| 284 | inet_ntop(AF_INET6, (const void *)&((struct sockaddr_in6 *)&s->cli_addr)->sin6_addr, |
| 285 | pn, sizeof(pn))) { |
| 286 | send_log(p, LOG_INFO, "Connect from %s:%d to %s:%d (%s/%s)\n", |
| 287 | pn, ntohs(((struct sockaddr_in6 *)&s->cli_addr)->sin6_port), |
| 288 | sn, ntohs(((struct sockaddr_in6 *)&sockname)->sin6_port), |
| 289 | p->id, (p->mode == PR_MODE_HTTP) ? "HTTP" : "TCP"); |
| 290 | } |
| 291 | } |
| 292 | } |
| 293 | |
| 294 | if ((global.mode & MODE_DEBUG) && (!(global.mode & MODE_QUIET) || (global.mode & MODE_VERBOSE))) { |
| 295 | struct sockaddr_in sockname; |
| 296 | socklen_t namelen = sizeof(sockname); |
| 297 | int len; |
| 298 | if (addr.ss_family != AF_INET || |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 299 | !(s->fe->options & PR_O_TRANSP) || |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 300 | get_original_dst(cfd, (struct sockaddr_in *)&sockname, &namelen) == -1) |
| 301 | getsockname(cfd, (struct sockaddr *)&sockname, &namelen); |
| 302 | |
| 303 | if (s->cli_addr.ss_family == AF_INET) { |
| 304 | char pn[INET_ADDRSTRLEN]; |
| 305 | inet_ntop(AF_INET, |
| 306 | (const void *)&((struct sockaddr_in *)&s->cli_addr)->sin_addr, |
| 307 | pn, sizeof(pn)); |
| 308 | |
| 309 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 310 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 311 | pn, ntohs(((struct sockaddr_in *)&s->cli_addr)->sin_port)); |
| 312 | } |
| 313 | else { |
| 314 | char pn[INET6_ADDRSTRLEN]; |
| 315 | inet_ntop(AF_INET6, |
| 316 | (const void *)&((struct sockaddr_in6 *)(&s->cli_addr))->sin6_addr, |
| 317 | pn, sizeof(pn)); |
| 318 | |
| 319 | len = sprintf(trash, "%08x:%s.accept(%04x)=%04x from [%s:%d]\n", |
| 320 | s->uniq_id, p->id, (unsigned short)fd, (unsigned short)cfd, |
| 321 | pn, ntohs(((struct sockaddr_in6 *)(&s->cli_addr))->sin6_port)); |
| 322 | } |
| 323 | |
| 324 | write(1, trash, len); |
| 325 | } |
| 326 | |
| 327 | if ((s->req = pool_alloc(buffer)) == NULL) { /* no memory */ |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 328 | if (s->hreq.hdr_idx.v != NULL) |
| 329 | pool_free_to(p->hdr_idx_pool, s->hreq.hdr_idx.v); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 330 | if (s->rsp_cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 331 | pool_free_to(p->fiprm->rsp_cap_pool, s->rsp_cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 332 | if (s->hreq.cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 333 | pool_free_to(p->fiprm->req_cap_pool, s->hreq.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 334 | close(cfd); /* nothing can be done for this fd without memory */ |
| 335 | pool_free(task, t); |
| 336 | pool_free(session, s); |
| 337 | return 0; |
| 338 | } |
| 339 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 340 | buffer_init(s->req); |
| 341 | s->req->rlim += BUFSIZE; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 342 | if (s->cli_state == CL_STHEADERS) /* reserve some space for header rewriting */ |
| 343 | s->req->rlim -= MAXREWRITE; |
| 344 | |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 345 | s->req->rto = s->fe->clitimeout; |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 346 | s->req->wto = s->be->beprm->srvtimeout; |
| 347 | s->req->cto = s->be->beprm->srvtimeout; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 348 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 349 | if ((s->rep = pool_alloc(buffer)) == NULL) { /* no memory */ |
| 350 | pool_free(buffer, s->req); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 351 | if (s->hreq.hdr_idx.v != NULL) |
| 352 | pool_free_to(p->hdr_idx_pool, s->hreq.hdr_idx.v); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 353 | if (s->rsp_cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 354 | pool_free_to(p->fiprm->rsp_cap_pool, s->rsp_cap); |
Willy Tarreau | 45e73e3 | 2006-12-17 00:05:15 +0100 | [diff] [blame] | 355 | if (s->hreq.cap != NULL) |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 356 | pool_free_to(p->fiprm->req_cap_pool, s->hreq.cap); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 357 | close(cfd); /* nothing can be done for this fd without memory */ |
| 358 | pool_free(task, t); |
| 359 | pool_free(session, s); |
| 360 | return 0; |
| 361 | } |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 362 | |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 363 | buffer_init(s->rep); |
| 364 | |
Willy Tarreau | 830ff45 | 2006-12-17 19:31:23 +0100 | [diff] [blame] | 365 | s->rep->rto = s->be->beprm->srvtimeout; |
| 366 | s->rep->wto = s->fe->clitimeout; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 367 | s->rep->cto = 0; |
| 368 | |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 369 | fdtab[cfd].owner = t; |
| 370 | fdtab[cfd].state = FD_STREADY; |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 371 | fdtab[cfd].cb[DIR_RD].f = &stream_sock_read; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 372 | fdtab[cfd].cb[DIR_RD].b = s->req; |
Willy Tarreau | f8306d5 | 2006-07-29 19:01:31 +0200 | [diff] [blame] | 373 | fdtab[cfd].cb[DIR_WR].f = &stream_sock_write; |
Willy Tarreau | 5446940 | 2006-07-29 16:59:06 +0200 | [diff] [blame] | 374 | fdtab[cfd].cb[DIR_WR].b = s->rep; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 375 | |
| 376 | if ((p->mode == PR_MODE_HTTP && (s->flags & SN_MONITOR)) || |
| 377 | (p->mode == PR_MODE_HEALTH && (p->options & PR_O_HTTP_CHK))) |
| 378 | /* Either we got a request from a monitoring system on an HTTP instance, |
| 379 | * or we're in health check mode with the 'httpchk' option enabled. In |
| 380 | * both cases, we return a fake "HTTP/1.0 200 OK" response and we exit. |
| 381 | */ |
| 382 | client_retnclose(s, 19, "HTTP/1.0 200 OK\r\n\r\n"); /* forge a 200 response */ |
| 383 | else if (p->mode == PR_MODE_HEALTH) { /* health check mode, no client reading */ |
| 384 | client_retnclose(s, 3, "OK\n"); /* forge an "OK" response */ |
| 385 | } |
| 386 | else { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 387 | MY_FD_SET(cfd, StaticReadEvent); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 388 | } |
| 389 | |
| 390 | #if defined(DEBUG_FULL) && defined(ENABLE_EPOLL) |
| 391 | if (PrevReadEvent) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 392 | assert(!(MY_FD_ISSET(cfd, PrevReadEvent))); |
| 393 | assert(!(MY_FD_ISSET(cfd, PrevWriteEvent))); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 394 | } |
| 395 | #endif |
| 396 | fd_insert(cfd); |
| 397 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 398 | tv_eternity(&s->req->rex); |
| 399 | tv_eternity(&s->req->wex); |
| 400 | tv_eternity(&s->req->cex); |
| 401 | tv_eternity(&s->rep->rex); |
| 402 | tv_eternity(&s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 403 | |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 404 | if (s->fe->clitimeout) { |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 405 | if (MY_FD_ISSET(cfd, StaticReadEvent)) |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 406 | tv_delayfrom(&s->req->rex, &now, s->fe->clitimeout); |
Willy Tarreau | 2a42950 | 2006-10-15 14:52:29 +0200 | [diff] [blame] | 407 | if (MY_FD_ISSET(cfd, StaticWriteEvent)) |
Willy Tarreau | 73de989 | 2006-11-30 11:40:23 +0100 | [diff] [blame] | 408 | tv_delayfrom(&s->rep->wex, &now, s->fe->clitimeout); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 409 | } |
| 410 | |
Willy Tarreau | d797128 | 2006-07-29 18:36:34 +0200 | [diff] [blame] | 411 | tv_min(&t->expire, &s->req->rex, &s->rep->wex); |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 412 | |
| 413 | task_queue(t); |
| 414 | |
| 415 | if (p->mode != PR_MODE_HEALTH) |
| 416 | task_wakeup(&rq, t); |
| 417 | |
Willy Tarreau | f1221aa | 2006-12-17 22:14:12 +0100 | [diff] [blame^] | 418 | p->feconn++; /* beconn will be increased later */ |
| 419 | if (p->feconn > p->feconn_max) |
| 420 | p->feconn_max = p->feconn; |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 421 | actconn++; |
| 422 | totalconn++; |
| 423 | |
| 424 | // 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^] | 425 | } /* end of while (p->feconn < p->maxconn) */ |
Willy Tarreau | baaee00 | 2006-06-26 02:48:02 +0200 | [diff] [blame] | 426 | return 0; |
| 427 | } |
| 428 | |
| 429 | |
| 430 | |
| 431 | /* |
| 432 | * Local variables: |
| 433 | * c-indent-level: 8 |
| 434 | * c-basic-offset: 8 |
| 435 | * End: |
| 436 | */ |