| 1 |
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */ |
|---|
| 2 |
|
|---|
| 3 |
/* Cherokee |
|---|
| 4 |
* |
|---|
| 5 |
* Authors: |
|---|
| 6 |
* Alvaro Lopez Ortega <alvaro@alobbs.com> |
|---|
| 7 |
* |
|---|
| 8 |
* Copyright (C) 2001-2008 Alvaro Lopez Ortega |
|---|
| 9 |
* |
|---|
| 10 |
* This program is free software; you can redistribute it and/or |
|---|
| 11 |
* modify it under the terms of version 2 of the GNU General Public |
|---|
| 12 |
* License as published by the Free Software Foundation. |
|---|
| 13 |
* |
|---|
| 14 |
* This program is distributed in the hope that it will be useful, |
|---|
| 15 |
* but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 16 |
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 17 |
* GNU General Public License for more details. |
|---|
| 18 |
* |
|---|
| 19 |
* You should have received a copy of the GNU General Public License |
|---|
| 20 |
* along with this program; if not, write to the Free Software |
|---|
| 21 |
* Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 |
|---|
| 22 |
* USA |
|---|
| 23 |
*/ |
|---|
| 24 |
|
|---|
| 25 |
#ifndef CHEROKEE_CONNECTION_PROTECTED_H |
|---|
| 26 |
#define CHEROKEE_CONNECTION_PROTECTED_H |
|---|
| 27 |
|
|---|
| 28 |
#include "common-internal.h" |
|---|
| 29 |
|
|---|
| 30 |
#ifdef HAVE_SYS_TIME_H |
|---|
| 31 |
# include <sys/time.h> |
|---|
| 32 |
#else |
|---|
| 33 |
# include <time.h> |
|---|
| 34 |
#endif |
|---|
| 35 |
|
|---|
| 36 |
#ifdef HAVE_SYS_SOCKET_H |
|---|
| 37 |
# include <sys/socket.h> |
|---|
| 38 |
#endif |
|---|
| 39 |
|
|---|
| 40 |
#ifdef HAVE_SYS_TYPES_H |
|---|
| 41 |
# include <sys/types.h> |
|---|
| 42 |
#endif |
|---|
| 43 |
|
|---|
| 44 |
#ifdef HAVE_NETINET_IN_H |
|---|
| 45 |
# include <netinet/in.h> |
|---|
| 46 |
#endif |
|---|
| 47 |
|
|---|
| 48 |
#ifdef HAVE_ARPA_INET_H |
|---|
| 49 |
# include <arpa/inet.h> |
|---|
| 50 |
#endif |
|---|
| 51 |
|
|---|
| 52 |
#include "http.h" |
|---|
| 53 |
#include "list.h" |
|---|
| 54 |
#include "avl.h" |
|---|
| 55 |
#include "socket.h" |
|---|
| 56 |
#include "header.h" |
|---|
| 57 |
#include "logger.h" |
|---|
| 58 |
#include "handler.h" |
|---|
| 59 |
#include "encoder.h" |
|---|
| 60 |
#include "iocache.h" |
|---|
| 61 |
#include "post.h" |
|---|
| 62 |
#include "header-protected.h" |
|---|
| 63 |
#include "regex.h" |
|---|
| 64 |
|
|---|
| 65 |
typedef enum { |
|---|
| 66 |
phase_nothing, |
|---|
| 67 |
phase_switching_headers, |
|---|
| 68 |
phase_tls_handshake, |
|---|
| 69 |
phase_reading_header, |
|---|
| 70 |
phase_processing_header, |
|---|
| 71 |
phase_read_post, |
|---|
| 72 |
phase_setup_connection, |
|---|
| 73 |
phase_init, |
|---|
| 74 |
phase_add_headers, |
|---|
| 75 |
phase_send_headers, |
|---|
| 76 |
phase_steping, |
|---|
| 77 |
phase_shutdown, |
|---|
| 78 |
phase_lingering |
|---|
| 79 |
} cherokee_connection_phase_t; |
|---|
| 80 |
|
|---|
| 81 |
|
|---|
| 82 |
#define conn_op_nothing 0 |
|---|
| 83 |
#define conn_op_log_at_end (1 << 0) |
|---|
| 84 |
#define conn_op_root_index (1 << 1) |
|---|
| 85 |
#define conn_op_tcp_cork (1 << 2) |
|---|
| 86 |
#define conn_op_document_root (1 << 3) |
|---|
| 87 |
#define conn_op_was_polling (1 << 4) |
|---|
| 88 |
|
|---|
| 89 |
typedef cuint_t cherokee_connection_options_t; |
|---|
| 90 |
|
|---|
| 91 |
|
|---|
| 92 |
struct cherokee_connection { |
|---|
| 93 |
cherokee_list_t list_node; |
|---|
| 94 |
|
|---|
| 95 |
/* References |
|---|
| 96 |
*/ |
|---|
| 97 |
void *server; |
|---|
| 98 |
void *vserver; |
|---|
| 99 |
void *thread; |
|---|
| 100 |
|
|---|
| 101 |
/* ID |
|---|
| 102 |
*/ |
|---|
| 103 |
culong_t id; |
|---|
| 104 |
cherokee_buffer_t self_trace; |
|---|
| 105 |
|
|---|
| 106 |
/* Socket stuff |
|---|
| 107 |
*/ |
|---|
| 108 |
cherokee_socket_t socket; |
|---|
| 109 |
cherokee_http_upgrade_t upgrade; |
|---|
| 110 |
cherokee_connection_options_t options; |
|---|
| 111 |
|
|---|
| 112 |
cherokee_logger_t *logger_ref; |
|---|
| 113 |
cherokee_handler_t *handler; |
|---|
| 114 |
|
|---|
| 115 |
/* Buffers |
|---|
| 116 |
*/ |
|---|
| 117 |
cherokee_buffer_t incoming_header; /* -> header */ |
|---|
| 118 |
cherokee_buffer_t header_buffer; /* <- header, -> post data */ |
|---|
| 119 |
cherokee_buffer_t buffer; /* <- data */ |
|---|
| 120 |
|
|---|
| 121 |
/* State |
|---|
| 122 |
*/ |
|---|
| 123 |
cherokee_connection_phase_t phase; |
|---|
| 124 |
cherokee_http_t error_code; |
|---|
| 125 |
|
|---|
| 126 |
/* Headers |
|---|
| 127 |
*/ |
|---|
| 128 |
cherokee_header_t header; |
|---|
| 129 |
|
|---|
| 130 |
/* Encoders |
|---|
| 131 |
*/ |
|---|
| 132 |
cherokee_encoder_t *encoder; |
|---|
| 133 |
cherokee_buffer_t encoder_buffer; |
|---|
| 134 |
|
|---|
| 135 |
/* Eg: |
|---|
| 136 |
* http://www.alobbs.com/cherokee/dir/file/param1 |
|---|
| 137 |
*/ |
|---|
| 138 |
cherokee_buffer_t local_directory; /* /var/www/ or /home/alo/public_html/ */ |
|---|
| 139 |
cherokee_buffer_t web_directory; /* /cherokee/ */ |
|---|
| 140 |
cherokee_buffer_t request; /* /dir/file */ |
|---|
| 141 |
cherokee_buffer_t pathinfo; /* /param1 */ |
|---|
| 142 |
cherokee_buffer_t userdir; /* 'alo' in http://www.alobbs.com/~alo/thing */ |
|---|
| 143 |
cherokee_buffer_t query_string; |
|---|
| 144 |
cherokee_avl_t *arguments; |
|---|
| 145 |
|
|---|
| 146 |
cherokee_buffer_t host; |
|---|
| 147 |
cherokee_buffer_t effective_directory; |
|---|
| 148 |
cherokee_buffer_t request_original; |
|---|
| 149 |
|
|---|
| 150 |
/* Authentication |
|---|
| 151 |
*/ |
|---|
| 152 |
cherokee_validator_t *validator; /* Validator object */ |
|---|
| 153 |
cherokee_buffer_t *realm_ref; /* "My private data" */ |
|---|
| 154 |
|
|---|
| 155 |
cherokee_http_auth_t auth_type; /* Auth type of the resource */ |
|---|
| 156 |
cherokee_http_auth_t req_auth_type; /* Auth type of the request */ |
|---|
| 157 |
|
|---|
| 158 |
/* Traffic |
|---|
| 159 |
*/ |
|---|
| 160 |
size_t rx; /* Bytes received */ |
|---|
| 161 |
size_t rx_partial; /* RX partial counter */ |
|---|
| 162 |
size_t tx; /* Bytes sent */ |
|---|
| 163 |
size_t tx_partial; /* TX partial counter */ |
|---|
| 164 |
time_t traffic_next; /* Time to update traffic */ |
|---|
| 165 |
|
|---|
| 166 |
/* Post info |
|---|
| 167 |
*/ |
|---|
| 168 |
cherokee_post_t post; |
|---|
| 169 |
|
|---|
| 170 |
/* Net connection |
|---|
| 171 |
*/ |
|---|
| 172 |
uint32_t keepalive; |
|---|
| 173 |
time_t timeout; |
|---|
| 174 |
|
|---|
| 175 |
/* Polling |
|---|
| 176 |
*/ |
|---|
| 177 |
int polling_fd; |
|---|
| 178 |
cherokee_socket_status_t polling_mode; |
|---|
| 179 |
cherokee_boolean_t polling_multiple; |
|---|
| 180 |
|
|---|
| 181 |
off_t range_start; |
|---|
| 182 |
off_t range_end; |
|---|
| 183 |
|
|---|
| 184 |
void *mmaped; |
|---|
| 185 |
off_t mmaped_len; |
|---|
| 186 |
cherokee_iocache_entry_t *io_entry_ref; |
|---|
| 187 |
|
|---|
| 188 |
int regex_ovector[OVECTOR_LEN]; |
|---|
| 189 |
int regex_ovecsize; |
|---|
| 190 |
|
|---|
| 191 |
/* Content Expiration |
|---|
| 192 |
*/ |
|---|
| 193 |
cherokee_expiration_t expiration; |
|---|
| 194 |
time_t expiration_time; |
|---|
| 195 |
|
|---|
| 196 |
/* Chunked encoding |
|---|
| 197 |
*/ |
|---|
| 198 |
cherokee_boolean_t chunked_encoding; |
|---|
| 199 |
cherokee_boolean_t chunked_last_package; |
|---|
| 200 |
cherokee_buffer_t chunked_len; |
|---|
| 201 |
size_t chunked_sent; |
|---|
| 202 |
struct iovec chunks[3]; |
|---|
| 203 |
uint16_t chunksn; |
|---|
| 204 |
|
|---|
| 205 |
/* Redirections |
|---|
| 206 |
*/ |
|---|
| 207 |
cherokee_buffer_t redirect; |
|---|
| 208 |
cuint_t respins; |
|---|
| 209 |
}; |
|---|
| 210 |
|
|---|
| 211 |
#define CONN_SRV(c) (SRV(CONN(c)->server)) |
|---|
| 212 |
#define CONN_HDR(c) (HDR(CONN(c)->header)) |
|---|
| 213 |
#define CONN_SOCK(c) (SOCKET(CONN(c)->socket)) |
|---|
| 214 |
#define CONN_VSRV(c) (VSERVER(CONN(c)->vserver)) |
|---|
| 215 |
#define CONN_THREAD(c) (THREAD(CONN(c)->thread)) |
|---|
| 216 |
|
|---|
| 217 |
#define TRACE_CONN(c) TRACE("conn", "%s", cherokee_connection_print(c)); |
|---|
| 218 |
|
|---|
| 219 |
/* Basic functions |
|---|
| 220 |
*/ |
|---|
| 221 |
ret_t cherokee_connection_new (cherokee_connection_t **conn); |
|---|
| 222 |
ret_t cherokee_connection_free (cherokee_connection_t *conn); |
|---|
| 223 |
ret_t cherokee_connection_clean (cherokee_connection_t *conn); |
|---|
| 224 |
ret_t cherokee_connection_clean_close (cherokee_connection_t *conn); |
|---|
| 225 |
|
|---|
| 226 |
/* Close |
|---|
| 227 |
*/ |
|---|
| 228 |
ret_t cherokee_connection_shutdown_wr (cherokee_connection_t *conn); |
|---|
| 229 |
ret_t cherokee_connection_linger_read (cherokee_connection_t *conn); |
|---|
| 230 |
|
|---|
| 231 |
/* Connection I/O |
|---|
| 232 |
*/ |
|---|
| 233 |
ret_t cherokee_connection_send (cherokee_connection_t *conn); |
|---|
| 234 |
ret_t cherokee_connection_send_header (cherokee_connection_t *conn); |
|---|
| 235 |
ret_t cherokee_connection_send_header_and_mmaped (cherokee_connection_t *conn); |
|---|
| 236 |
ret_t cherokee_connection_send_switching (cherokee_connection_t *conn); |
|---|
| 237 |
ret_t cherokee_connection_recv (cherokee_connection_t *conn, cherokee_buffer_t *buffer, off_t *len); |
|---|
| 238 |
|
|---|
| 239 |
/* Internal |
|---|
| 240 |
*/ |
|---|
| 241 |
ret_t cherokee_connection_create_handler (cherokee_connection_t *conn, cherokee_config_entry_t *config_entry); |
|---|
| 242 |
ret_t cherokee_connection_create_encoder (cherokee_connection_t *conn, cherokee_avl_t *encoders, cherokee_avl_t *accept_enc); |
|---|
| 243 |
ret_t cherokee_connection_setup_error_handler (cherokee_connection_t *conn); |
|---|
| 244 |
ret_t cherokee_connection_check_authentication (cherokee_connection_t *conn, cherokee_config_entry_t *config_entry); |
|---|
| 245 |
ret_t cherokee_connection_check_ip_validation (cherokee_connection_t *conn, cherokee_config_entry_t *config_entry); |
|---|
| 246 |
ret_t cherokee_connection_check_only_secure (cherokee_connection_t *conn, cherokee_config_entry_t *config_entry); |
|---|
| 247 |
ret_t cherokee_connection_check_http_method (cherokee_connection_t *conn, cherokee_config_entry_t *config_entry); |
|---|
| 248 |
void cherokee_connection_set_keepalive (cherokee_connection_t *conn); |
|---|
| 249 |
|
|---|
| 250 |
/* Iteration |
|---|
| 251 |
*/ |
|---|
| 252 |
ret_t cherokee_connection_open_request (cherokee_connection_t *conn); |
|---|
| 253 |
ret_t cherokee_connection_reading_check (cherokee_connection_t *conn); |
|---|
| 254 |
ret_t cherokee_connection_step (cherokee_connection_t *conn); |
|---|
| 255 |
|
|---|
| 256 |
/* Headers |
|---|
| 257 |
*/ |
|---|
| 258 |
ret_t cherokee_connection_build_header (cherokee_connection_t *conn); |
|---|
| 259 |
ret_t cherokee_connection_get_request (cherokee_connection_t *conn); |
|---|
| 260 |
ret_t cherokee_connection_parse_range (cherokee_connection_t *conn); |
|---|
| 261 |
int cherokee_connection_is_userdir (cherokee_connection_t *conn); |
|---|
| 262 |
ret_t cherokee_connection_build_local_directory (cherokee_connection_t *conn, cherokee_virtual_server_t *vsrv, cherokee_config_entry_t *entry); |
|---|
| 263 |
ret_t cherokee_connection_build_local_directory_userdir (cherokee_connection_t *conn, cherokee_virtual_server_t *vsrv, cherokee_config_entry_t *entry); |
|---|
| 264 |
ret_t cherokee_connection_clean_error_headers (cherokee_connection_t *conn); |
|---|
| 265 |
ret_t cherokee_connection_set_redirect (cherokee_connection_t *conn, cherokee_buffer_t *address); |
|---|
| 266 |
|
|---|
| 267 |
ret_t cherokee_connection_clean_for_respin (cherokee_connection_t *conn); |
|---|
| 268 |
int cherokee_connection_use_webdir (cherokee_connection_t *conn); |
|---|
| 269 |
|
|---|
| 270 |
/* Log |
|---|
| 271 |
*/ |
|---|
| 272 |
ret_t cherokee_connection_log_or_delay (cherokee_connection_t *conn); |
|---|
| 273 |
ret_t cherokee_connection_log_delayed (cherokee_connection_t *conn); |
|---|
| 274 |
ret_t cherokee_connection_update_vhost_traffic (cherokee_connection_t *conn); |
|---|
| 275 |
char *cherokee_connection_print (cherokee_connection_t *conn); |
|---|
| 276 |
|
|---|
| 277 |
/* Transfers |
|---|
| 278 |
*/ |
|---|
| 279 |
void cherokee_connection_rx_add (cherokee_connection_t *conn, ssize_t rx); |
|---|
| 280 |
void cherokee_connection_tx_add (cherokee_connection_t *conn, ssize_t tx); |
|---|
| 281 |
|
|---|
| 282 |
#endif /* CHEROKEE_CONNECTION_PROTECTED_H */ |
|---|