This is a combined commit that enables SSL support, simplifies the configuration and fixes a few bugs.
-- Sonny Karlsson
>From 06c13dc39f6909942e6e0054041eb16bb507801b Mon Sep 17 00:00:00 2001 From: Sonny Karlsson <[email protected]> Date: Wed, 6 Feb 2013 19:34:52 +0100 Subject: [PATCH] fastcgi: Update plugin to v0.2. commit 03aec5b6d3a364625aa31c3bfef8605d0bdd44f9 Author: Sonny Karlsson <[email protected]> Date: Wed Feb 6 19:23:06 2013 +0100 fastcgi: Set version. commit 064822ac4a913cf067db34c374492243e9079981 Author: Sonny Karlsson <[email protected]> Date: Wed Feb 6 19:21:24 2013 +0100 fastcgi: Set HTTPS in cgi environment. commit 58585d06c37a5a1f1ca008b5bbba0bb1b64f877a Author: Sonny Karlsson <[email protected]> Date: Wed Feb 6 19:13:28 2013 +0100 fastcgi: Compatibility with SSL transport. Use mk_api->socket* on client socket and unistd functions on fastcgi socket. commit e8549387c228c45bc128844deaa27d97fc64cc3d Author: Sonny Karlsson <[email protected]> Date: Mon Jan 28 13:20:12 2013 +0100 fastcgi: Fix conditional close of fastcgi connection. commit f5ebf5edc5062446313b99f6886cad8620df5b06 Author: Sonny Karlsson <[email protected]> Date: Sun Jan 27 16:54:43 2013 +0100 fastcgi: Update minimal libfcgi config example. commit ce56895c132c8b9769e9fd365b6662a2d8b47596 Author: Sonny Karlsson <[email protected]> Date: Sun Jan 27 17:10:16 2013 +0100 fastcgi: Indicate which variables are required in config. commit 46875aafb63897d75fc2398db381679d28d27ffb Author: Sonny Karlsson <[email protected]> Date: Sun Jan 27 17:08:57 2013 +0100 fastcgi: Auto-generate LocationName if none given. commit 427a513223fc7fe5b70cc8657b147f6e86c5f48b Author: Sonny Karlsson <[email protected]> Date: Fri Jan 11 11:57:03 2013 +0100 fastcgi: Let default for MaxConnections be 1. commit 6738f4baff43f4f8455b4606dc1f811e729d2802 Author: Sonny Karlsson <[email protected]> Date: Fri Jan 11 11:52:57 2013 +0100 fastcgi: Fixup of trace and debug messages. Fix format warning in printf. Check return from event_add. Check return of fcgi_fd state change. Remove some redundant trace messages. commit 3f5015ad04c60f286fa1bc998bb13bb334b9e841 Author: Sonny Karlsson <[email protected]> Date: Mon Jan 7 12:23:25 2013 +0100 fastcgi: Add inet_ntop include in fcgi_env. commit f3caaa99dee45405a3e4c993fd987287d6505971 Author: Sonny Karlsson <[email protected]> Date: Fri Jan 11 11:41:46 2013 +0100 fastcgi: Don't set CONTENT_LENGTH unless needed. Signed-off-by: Sonny Karlsson <[email protected]> --- plugins/fastcgi/chunk.c | 4 +- plugins/fastcgi/conf/fastcgi.conf | 16 +++--- plugins/fastcgi/fastcgi.c | 111 +++++++++++++++---------------------- plugins/fastcgi/fcgi_config.c | 11 +++- plugins/fastcgi/fcgi_env.c | 21 ++++--- 5 files changed, 80 insertions(+), 83 deletions(-) diff --git a/plugins/fastcgi/chunk.c b/plugins/fastcgi/chunk.c index 7695d49..0ba76b7 100644 --- a/plugins/fastcgi/chunk.c +++ b/plugins/fastcgi/chunk.c @@ -161,7 +161,7 @@ void chunk_list_stats(struct chunk_list *cm) used = c->write; free = c->size - used; - log_info("Chunk: %d, S: %ld B, U: %ld B, F: %ld B, R: %d", + log_info("Chunk: %d, S: %zu B, U: %zu B, F: %zu B, R: %d", chunks, c->size, used, @@ -174,7 +174,7 @@ void chunk_list_stats(struct chunk_list *cm) } log_info("Total"); - log_info("Count: %d, Size: %ld B, Used: %ld B, Free: %ld B", + log_info("Count: %d, Size: %zu B, Used: %zu B, Free: %zu B", chunks, total_used + total_free, total_used, diff --git a/plugins/fastcgi/conf/fastcgi.conf b/plugins/fastcgi/conf/fastcgi.conf index 3481c33..0f437cc 100644 --- a/plugins/fastcgi/conf/fastcgi.conf +++ b/plugins/fastcgi/conf/fastcgi.conf @@ -14,23 +14,27 @@ ## #[FASTCGI_SERVER] ## Each server must have a unique name. + ## (Required) # ServerName php5-fpm1 ## Depending on your version of php5-fpm, one of these should be ## enabled. ServerAddr is used in the default pool.d/www.conf on ## Debian Squeeze. + ## (One of either required) # ServerAddr 127.0.0.1:9000 # ServerPath /var/run/php5-fpm.sock ## Concurrent connections. - ## Php5-fpm defaults to 5 workers. + ## (Optional, default 1) # MaxConnections 5 #[FASTCGI_LOCATION] ## Each location should have a unique name for easier debugging. + ## (Optional, default auto-generated) # LocationName php5_location ## Space separated list of location servers. + ## (Required) # ServerNames php5-fpm1 ## Keep FastCGI server connections alive. Please tune the @@ -38,6 +42,7 @@ ## count under [SERVER] in monkey.conf (Threads may stall if ## available concurrent connections is less then number of ## workers.) + ## (Optional, default Off) # KeepAlive On ## Space separated list of match regex. If regex overlapp with @@ -50,19 +55,16 @@ ## Match ^/var/htdocs/wordpress/(.*).php$ ## ## This one will render all php scripts in any directory. + ## (Required) # Match /*.php ## -## Example libfcgi configuration +## Example minimal libfcgi TCP configuration ## # [FASTCGI_SERVER] # ServerName test_server1 - # ServerPath 127.0.0.1:7999 - # ServerPath /var/run/fcgitest.sock - # MaxConnections 1 + # ServerAddr 127.0.0.1:7999 # [FASTCGI_LOCATION] - # LocationName fcgitest # ServerNames test_server1 - # KeepAlive Off # Match /fcgitest diff --git a/plugins/fastcgi/fastcgi.c b/plugins/fastcgi/fastcgi.c index 05178ab..c984348 100644 --- a/plugins/fastcgi/fastcgi.c +++ b/plugins/fastcgi/fastcgi.c @@ -36,7 +36,7 @@ MONKEY_PLUGIN("fastcgi", /* shortname */ "FastCGI client", /* name */ - VERSION, /* version */ + "0.2", /* version */ MK_PLUGIN_STAGE_30 | MK_PLUGIN_CORE_THCTX | MK_PLUGIN_CORE_PRCTX); const mk_pointer mk_iov_none = { @@ -146,7 +146,8 @@ int fcgi_wake_connection(int location_id) mk_api->event_socket_change_mode(fd->fd, MK_EPOLL_WAKEUP, MK_EPOLL_LEVEL_TRIGGERED); - fcgi_fd_set_state(fd, FCGI_FD_READY); + check(!fcgi_fd_set_state(fd, FCGI_FD_READY), + "[FCGI_FD %d] State change failed.", fd->fd); } return 0; error: @@ -210,10 +211,11 @@ int fcgi_new_connection(int location_id) check_debug(fd->fd != -1, "Failed to connect to server."); mk_api->socket_set_nonblocking(fd->fd); - mk_api->event_add(fd->fd, + check(!mk_api->event_add(fd->fd, MK_EPOLL_RW, plugin, - MK_EPOLL_LEVEL_TRIGGERED); + MK_EPOLL_LEVEL_TRIGGERED), + "[FD %d] Failed to add event.", fd->fd); fcgi_fd_set_state(fd, FCGI_FD_READY); @@ -353,7 +355,7 @@ int fcgi_send_abort_request(struct request *req, struct fcgi_fd *fd) check(h.req_id > 0, "Bad request id: %d.", h.req_id); fcgi_write_header(buf, &h); - ret = mk_api->socket_send(fd->fd, buf, sizeof(h)); + ret = write(fd->fd, buf, sizeof(h)); check(ret != -1, "Socket error."); return 0; @@ -380,9 +382,6 @@ int fcgi_send_response_headers(struct request *req) "Failed to drop from req->iov."); req->sr->headers.content_length = chunk_iov_length(&req->iov); - PLUGIN_TRACE("[FD %d] Headers length is %ld.", - req->fd, headers_offset); - mk_api->header_send(req->fd, req->cs, req->sr); req->sr->headers.location = NULL; @@ -397,11 +396,16 @@ int fcgi_send_response(struct request *req) { int fd = req->fd; ssize_t ret; + struct mk_iov mkiov; check(request_get_flag(req, REQ_HEADERS_SENT), "Headers not yet sent for request."); - ret = chunk_iov_sendv(fd, &req->iov); + memset(&mkiov, 0, sizeof(mkiov)); + mkiov.io = req->iov.io; + mkiov.iov_idx = req->iov.index; + mkiov.total_len = chunk_iov_length(&req->iov); + ret = mk_api->socket_sendv(fd, &mkiov); PLUGIN_TRACE("[FD %d] Wrote %ld bytes.", fd, ret); check(ret != -1, "[FD %d] Failed to send request response.", fd); @@ -536,7 +540,7 @@ int fcgi_recv_response(struct fcgi_fd *fd, int (*handle_pkg)(struct fcgi_fd *fd, struct request *req, struct fcgi_header h, - struct chunk_ptr read)) + struct chunk_ptr rcp)) { size_t pkg_size = 0, inherit = 0; ssize_t ret = 0; @@ -545,21 +549,22 @@ int fcgi_recv_response(struct fcgi_fd *fd, struct fcgi_header h; struct request *req; struct chunk *c; - struct chunk_ptr write = { .len = 0, .data = NULL, .parent = NULL}; - struct chunk_ptr read = { .len = 0, .data = NULL, .parent = NULL}; + struct chunk_ptr wcp = { .len = 0, .data = NULL, .parent = NULL}; + struct chunk_ptr rcp = { .len = 0, .data = NULL, .parent = NULL}; PLUGIN_TRACE("[FCGI_FD %d] Receiving response.", fd->fd); c = fcgi_fd_get_chunk(fd); if (c != NULL) { - write = chunk_write_ptr(c); - read = chunk_read_ptr(c); + wcp = chunk_write_ptr(c); + rcp = chunk_read_ptr(c); } do { - if (inherit > 0 || write.len < sizeof(h)) { - PLUGIN_TRACE("[FCGI_FD] New chunk, inherit %ld.", - inherit); + if (inherit > 0 || wcp.len < sizeof(h)) { + PLUGIN_TRACE("[FCGI_FD %d] New chunk, inherit %ld.", + fd->fd, + inherit); if (pkg_size > CHUNK_SIZE(8192)) { c = chunk_new(SIZE_CHUNK(pkg_size)); } else { @@ -569,11 +574,11 @@ int fcgi_recv_response(struct fcgi_fd *fd, chunk_list_add(cl, c); check(!fcgi_fd_set_chunk(fd, c, inherit), "[FCGI_FD %d] Failed to add chunk.", fd->fd); - write = chunk_write_ptr(c); + wcp = chunk_write_ptr(c); inherit = 0; } - ret = mk_api->socket_read(fd->fd, write.data, write.len); + ret = read(fd->fd, wcp.data, wcp.len); if (ret == 0) { check(!fcgi_fd_set_state(fd, FCGI_FD_CLOSING), @@ -587,38 +592,38 @@ int fcgi_recv_response(struct fcgi_fd *fd, sentinel("Socket read error."); } } else { - write.data += ret; - write.len -= ret; - check(!chunk_set_write_ptr(c, write), + wcp.data += ret; + wcp.len -= ret; + check(!chunk_set_write_ptr(c, wcp), "Failed to set new write ptr."); - read = chunk_read_ptr(c); + rcp = chunk_read_ptr(c); } - while (read.len > 0) { - if (read.len < sizeof(h)) { + while (rcp.len > 0) { + if (rcp.len < sizeof(h)) { pkg_size = sizeof(h); } else { - fcgi_read_header(read.data, &h); + fcgi_read_header(rcp.data, &h); pkg_size = sizeof(h) + h.body_len + h.body_pad; } - if (read.len < pkg_size) { - inherit = read.len; + if (rcp.len < pkg_size) { + inherit = rcp.len; ret = inherit; } else { req = request_list_get(rl, h.req_id); - check_debug(!handle_pkg(fd, req, h, read), + check_debug(!handle_pkg(fd, req, h, rcp), "[REQ_ID %d] Failed to handle pkg.", h.req_id); ret = pkg_size; } - read.data += ret; - read.len -= ret; + rcp.data += ret; + rcp.len -= ret; } - if (read.parent == c) { - check(!chunk_set_read_ptr(c, read), + if (rcp.parent == c) { + check(!chunk_set_read_ptr(c, rcp), "Failed to set new read ptr."); } } while (!done); @@ -682,11 +687,8 @@ int _mkp_stage_30(struct plugin *plugin, struct client_session *cs, return MK_PLUGIN_RET_NOT_ME; } - PLUGIN_TRACE("[FD %d] URI match found.", cs->socket); req = request_list_next_available(rl, location_id); - check(req, "[FD %d] No available request structs.", cs->socket); - req_id = request_list_index_of(rl, req); check(!request_assign(req, cs->socket, location_id, cs, sr), @@ -696,14 +698,11 @@ int _mkp_stage_30(struct plugin *plugin, struct client_session *cs, "[REQ_ID %d] Failed to prepare request.", req_id); PLUGIN_TRACE("[FD %d] Assigned to req_id %d.", cs->socket, req_id); - PLUGIN_TRACE("[REQ_ID %d] Request ready to be sent.", req_id); if (fcgi_wake_connection(location_id)) { PLUGIN_TRACE("[REQ_ID %d] Create new fcgi connection.", req_id); check_debug(!fcgi_new_connection(location_id), "New connection failed seriously."); - } else { - PLUGIN_TRACE("[REQ_ID %d] Found connection available.", req_id); } request_set_flag(req, REQ_SLEEPING); @@ -750,10 +749,7 @@ error: void _mkp_exit() { - PLUGIN_TRACE("Free thread context list."); fcgi_context_list_free(&fcgi_global_context_list); - - PLUGIN_TRACE("Free configuration."); fcgi_config_free(&fcgi_global_config); } @@ -762,7 +758,6 @@ int _mkp_core_prctx(struct server_config *config) struct mk_list *h; struct plugin *p; - PLUGIN_TRACE("Init thread context list."); check(!fcgi_context_list_init(&fcgi_global_context_list, &fcgi_global_config, config->workers, @@ -791,10 +786,9 @@ void _mkp_core_thctx(void) tid = fcgi_context_list_assign_thread_id(&fcgi_global_context_list); check(tid != -1, "Failed to assign thread id."); - PLUGIN_TRACE("Thread assigned id %d.", tid); - cntx = fcgi_context_list_get(&fcgi_global_context_list, tid); pthread_setspecific(fcgi_local_context, cntx); + return; error: log_err("Failed to initiate thread context."); @@ -826,10 +820,10 @@ static int hangup(int socket) return MK_PLUGIN_RET_EVENT_NEXT; } else if (fd) { - PLUGIN_TRACE("[FCGI_FD %d] Hangup event received.", fd->fd); + PLUGIN_TRACE("[FCGI_FD %d] Hangup event.", fd->fd); mk_api->event_del(fd->fd); - mk_api->socket_close(fd->fd); + close(fd->fd); state = fd->state; @@ -852,7 +846,6 @@ static int hangup(int socket) } if (req->fcgi_fd == -1) { - PLUGIN_TRACE("[REQ_ID %d] Recycle failed request.", req_id); request_recycle(req); } else { req->fd = -1; @@ -875,7 +868,6 @@ int _mkp_event_write(int socket) struct request *req = NULL; struct fcgi_fd_list *fdl; struct fcgi_fd *fd; - struct fcgi_location *locp; ssize_t ret; cntx = pthread_getspecific(fcgi_local_context); @@ -906,7 +898,6 @@ int _mkp_event_write(int socket) req_id = request_list_index_of(rl, req); #endif - PLUGIN_TRACE("[REQ_ID %d] Request failed.", req_id); mk_api->http_request_error(MK_SERVER_INTERNAL_ERROR, req->cs, req->sr); @@ -924,9 +915,6 @@ int _mkp_event_write(int socket) req_id = request_list_index_of(rl, req); request_set_fcgi_fd(req, fd->fd); - PLUGIN_TRACE("[REQ_ID %d] Assigned to FCGI_FD %d.", - req_id, fd->fd); - check(!request_set_state(req, REQ_SENT), "[REQ_ID %d] Failed to set sent state.", req_id); @@ -944,15 +932,7 @@ int _mkp_event_write(int socket) return _mkp_event_write(fd->fd); } else { - locp = fcgi_config_get_location(&fcgi_global_config, - fd->location_id); - check(locp, - "[FCGI_FD %d] Failed to get location.", fd->fd); - check_debug(locp->keep_alive, - "[FCGI_FD %d] Closing new connection, no work.", fd->fd); - - PLUGIN_TRACE("[FCGI_FD %d] Putting fcgi_fd to sleep.", - fd->fd); + PLUGIN_TRACE("[FCGI_FD %d] Sleep.", fd->fd); mk_api->event_socket_change_mode(fd->fd, MK_EPOLL_SLEEP, @@ -1034,15 +1014,16 @@ int _mkp_event_read(int socket) check_debug(!fcgi_recv_response(fd, cl, rl, fcgi_handle_pkg), "[FCGI_FD %d] Failed to receive response.", fd->fd); - check_debug(fd->state != FCGI_FD_CLOSING, - "[FCGI_FD %d] Closing connection.", fd->fd); + + if (fd->state == FCGI_FD_CLOSING) { + return MK_PLUGIN_RET_EVENT_CLOSE; + } PLUGIN_TRACE("[FCGI_FD %d] Data received.", fd->fd); return MK_PLUGIN_RET_EVENT_OWNED; } error: - PLUGIN_TRACE("[FCGI_FD %d] Closing connection.", socket); return MK_PLUGIN_RET_EVENT_CLOSE; } diff --git a/plugins/fastcgi/fcgi_config.c b/plugins/fastcgi/fcgi_config.c index 46ac18d..4106fef 100644 --- a/plugins/fastcgi/fcgi_config.c +++ b/plugins/fastcgi/fcgi_config.c @@ -170,6 +170,10 @@ int fcgi_config_read_server(struct fcgi_server *srv, srv->max_connections = (long int)mk_api->config_section_getval(section, "MaxConnections", MK_CONFIG_VAL_NUM); + if (srv->max_connections <= 0) { + srv->max_connections = 1; + } + srv->max_requests = (long int)mk_api->config_section_getval(section, "MaxRequests", MK_CONFIG_VAL_NUM); @@ -186,6 +190,7 @@ int fcgi_config_read_location(struct fcgi_location *loc, int server_count, struct mk_config_section *section) { + static int unamed_loc_count = 0; int ret = 0; char error_str[80]; int loc_server_n = 0; @@ -205,7 +210,11 @@ int fcgi_config_read_location(struct fcgi_location *loc, server_names = mk_api->config_section_getval(section, "ServerNames", MK_CONFIG_VAL_STR); - check(loc->name, "No name for this location!"); + if (!loc->name) { + loc->name = mk_api->mem_alloc_z(24); + snprintf(loc->name, 24, "location_%d", unamed_loc_count); + unamed_loc_count += 1; + } check(regex, "No match regex defined for this location."); for (tok = regex; *tok != '\0'; tok++) { diff --git a/plugins/fastcgi/fcgi_env.c b/plugins/fastcgi/fcgi_env.c index f1e1712..818b15a 100644 --- a/plugins/fastcgi/fcgi_env.c +++ b/plugins/fastcgi/fcgi_env.c @@ -22,6 +22,7 @@ #include <stdio.h> /* sscanf */ #include <ctype.h> /* toupper */ #include <sys/socket.h> /* getsockname, getpeername */ +#include <arpa/inet.h> /* inet_ntop */ #include "MKPlugin.h" @@ -48,10 +49,6 @@ size_t fcgi_env_write(uint8_t *ptr, char *hinit, *hend; size_t hlen; - mk_api->pointer_set(&key, "PATH_INFO"); - mk_api->pointer_set(&value, ""); - __write_param(ptr, len, pos, key, value); - mk_api->pointer_set(&key, "GATEWAY_INTERFACE"); mk_api->pointer_set(&value, "CGI/1.1"); __write_param(ptr, len, pos, key, value); @@ -146,10 +143,18 @@ size_t fcgi_env_write(uint8_t *ptr, value = sr->content_type; __write_param(ptr, len, pos, key, value); - mk_api->pointer_set(&key, "CONTENT_LENGTH"); - snprintf(buffer, 128, "%d", sr->content_length); - mk_api->pointer_set(&value, buffer); - __write_param(ptr, len, pos, key, value); + if (sr->content_length > 0) { + mk_api->pointer_set(&key, "CONTENT_LENGTH"); + snprintf(buffer, 128, "%d", sr->content_length); + mk_api->pointer_set(&value, buffer); + __write_param(ptr, len, pos, key, value); + } + + if (!strcmp(mk_api->config->transport, MK_TRANSPORT_HTTPS)) { + mk_api->pointer_set(&key, "HTTPS"); + mk_api->pointer_set(&value, "on"); + __write_param(ptr, len, pos, key, value); + } strcpy(buffer, "HTTP_"); -- 1.7.10.4
_______________________________________________ Monkey mailing list [email protected] http://lists.monkey-project.com/listinfo/monkey
