Remove connections counter
[MonkeyD.git] / src / connection.c
blobab1edb287a6eee38c7a9659b69c7544924b16d27
1 /* -*- Mode: C; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
3 /* Monkey HTTP Daemon
4 * ------------------
5 * Copyright (C) 2001-2010, Eduardo Silva P. <edsiper@gmail.com>
7 * This program is free software; you can redistribute it and/or modify
8 * it under the terms of the GNU General Public License as published by
9 * the Free Software Foundation; either version 2 of the License, or
10 * (at your option) any later version.
12 * This program is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
15 * GNU Library General Public License for more details.
17 * You should have received a copy of the GNU General Public License
18 * along with this program; if not, write to the Free Software
19 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
22 #include "monkey.h"
23 #include "http.h"
24 #include "connection.h"
25 #include "scheduler.h"
26 #include "epoll.h"
27 #include "request.h"
28 #include "socket.h"
29 #include "plugin.h"
30 #include "utils.h"
32 #include <string.h>
33 #include <stdio.h>
35 int mk_conn_read(int socket)
37 int ret;
38 struct client_request *cr;
39 struct sched_list_node *sched;
41 #ifdef TRACE
42 MK_TRACE("Connection Handler, read on FD %i", socket);
43 #endif
45 /* Plugin hook */
46 ret = mk_plugin_event_read(socket);
47 if (ret != MK_PLUGIN_RET_EVENT_NOT_ME) {
48 return ret;
51 sched = mk_sched_get_thread_conf();
53 cr = mk_request_client_get(socket);
54 if (!cr) {
55 /* Note: Linux don't set TCP_NODELAY socket flag by default,
56 * also we set the client socket on non-blocking mode
58 mk_socket_set_tcp_nodelay(socket);
59 mk_socket_set_nonblocking(socket);
61 cr = mk_request_client_create(socket);
63 else {
64 /* If cr struct already exists, that could means that we
65 * are facing a keepalive connection, need to verify, if it
66 * applies we increase the thread status for active connections
68 if (cr->counter_connections > 1 && cr->body_length == 0) {
69 /* FIXME: KA Connection */
73 /* Read incomming data */
74 ret = mk_handler_read(socket, cr);
76 if (ret > 0) {
77 if (mk_http_pending_request(cr) == 0) {
78 mk_epoll_socket_change_mode(sched->epoll_fd,
79 socket, MK_EPOLL_WRITE);
81 else if (cr->body_length + 1 >= MAX_REQUEST_BODY) {
82 /* Request is incomplete and our buffer is full,
83 * close connection
85 mk_request_client_remove(socket);
86 return -1;
89 return ret;
92 int mk_conn_write(int socket)
94 int ret = -1, ka;
95 struct client_request *cr;
96 struct sched_list_node *sched;
98 #ifdef TRACE
99 MK_TRACE("Connection Handler, write on FD %i", socket);
100 #endif
102 /* Plugin hook */
103 ret = mk_plugin_event_write(socket);
104 #ifdef TRACE
105 MK_TRACE("Check plugin hook | ret = %i", ret);
106 #endif
107 if (ret != MK_PLUGIN_RET_EVENT_NOT_ME) {
108 return ret;
111 #ifdef TRACE
112 MK_TRACE("Normal connection write handling...");
113 #endif
115 sched = mk_sched_get_thread_conf();
116 mk_sched_update_conn_status(sched, socket, MK_SCHEDULER_CONN_PROCESS);
118 /* Get node from schedule list node which contains
119 * the information regarding to the current client/socket
121 cr = mk_request_client_get(socket);
123 if (!cr) {
124 return -1;
127 ret = mk_handler_write(socket, cr);
128 ka = mk_http_keepalive_check(socket, cr);
130 /* if ret < 0, means that some error
131 * happened in the writer call, in the
132 * other hand, 0 means a successful request
133 * processed, if ret > 0 means that some data
134 * still need to be send.
137 if (ret <= 0) {
138 mk_request_free_list(cr);
140 /* We need to ask to http_keepalive if this
141 * connection can continue working or we must
142 * close it.
144 if (ka < 0 || ret < 0) {
145 mk_request_client_remove(socket);
146 return -1;
148 else {
149 mk_request_ka_next(cr);
150 mk_epoll_socket_change_mode(sched->epoll_fd,
151 socket, MK_EPOLL_READ);
152 return 0;
155 else if (ret > 0) {
156 return 0;
159 /* avoid to make gcc cry :_( */
160 return -1;
163 int mk_conn_error(int socket)
165 struct client_request *cr;
166 struct sched_list_node *sched;
168 #ifdef TRACE
169 MK_TRACE("Connection Handler, error on FD %i", socket);
170 #endif
172 sched = mk_sched_get_thread_conf();
173 mk_sched_remove_client(NULL, socket);
174 cr = mk_request_client_get(socket);
175 if (cr) {
176 mk_request_client_remove(socket);
179 return 0;
182 int mk_conn_close(int socket)
184 struct sched_list_node *sched;
186 #ifdef TRACE
187 MK_TRACE("Connection Handler, closed on FD %i", socket);
188 #endif
190 sched = mk_sched_get_thread_conf();
191 mk_sched_remove_client(sched, socket);
193 return 0;
196 int mk_conn_timeout(int socket)
198 struct sched_list_node *sched;
200 #ifdef TRACE
201 MK_TRACE("Connection Handler, timeout on FD %i", socket);
202 #endif
204 sched = mk_sched_get_thread_conf();
205 mk_sched_check_timeouts(sched);
207 return 0;