This commit is contained in:
roberto@sirius
2010-01-18 14:55:18 +01:00
parent a51b19669c
commit e3a67a9bcd
4 changed files with 0 additions and 623 deletions
-92
View File
@@ -1,92 +0,0 @@
from Form import *
from Table import *
from Module import *
from validations import *
from consts import *
# For gettext
N_ = lambda x: x
from ModuleCgi import *
from ModuleBalancer import NOTE_BALANCER
HELPS = [
('modules_handlers_uwsgi', "UWSGI")
]
class ModuleUwsgi (ModuleCgiBase):
PROPERTIES = ModuleCgiBase.PROPERTIES + [
'balancer',
'modifier1',
'modifier2',
'pass_wsgi_vars',
'pass_request_body'
]
def __init__ (self, cfg, prefix, submit):
ModuleCgiBase.__init__ (self, cfg, prefix, 'uwsgi', submit)
self.show_script_alias = False
self.show_change_uid = False
self.show_document_root = True
def _op_render (self):
txt = ModuleCgiBase._op_render (self)
txt += '<h2>%s</h2>' % (_('UWSGI specific'))
table = TableProps()
opt = "%s!modifier1" % (self._prefix)
self.AddPropEntry(table, _('Modifier1'), opt, 'a number between 0 and 255', size=3);
opt = "%s!modifier2" % (self._prefix)
self.AddPropEntry(table, _('Modifier2'), opt, 'a number between 0 and 255', size=3);
opt = "%s!pass_wsgi_vars" % (self._prefix)
self.AddPropCheck (table, _("Pass WSGI vars"), opt, True, 'pass all the wsgi vars to the application')
opt = "%s!pass_request_body" % (self._prefix)
self.AddPropCheck (table, _("Pass Request body"), opt, True, 'pass the request body to the application')
opt = "%s!balancer" % (self._prefix)
e = self.AddPropOptions_Reload_Module (table, _("Balancer"), opt,
modules_available(BALANCERS), _(NOTE_BALANCER))
txt += self.Indent(str(table) + e)
return txt
def _op_apply_changes (self, uri, post):
# Apply modifier1
opt = "%s!modifier1" % (self._prefix)
mod1 = post.pop(opt)
if mod1:
self._cfg[opt] = mod1
# Apply modifier2
opt = "%s!modifier2" % (self._prefix)
mod2 = post.pop(opt)
if mod2:
self._cfg[opt] = mod2
# Apply pass_request_body and pass_wsgi_vars
self.ApplyChangesPrefix (self._prefix, ['pass_wsgi_vars','pass_request_body'], post)
# Apply balancer changes
pre = "%s!balancer" % (self._prefix)
new_balancer = post.pop(pre)
if new_balancer:
self._cfg[pre] = new_balancer
cfg = self._cfg[pre]
if cfg and cfg.value:
name = cfg.value
props = module_obj_factory (name, self._cfg, pre, self.submit_url)
props._op_apply_changes (uri, post)
# And CGI changes
return ModuleCgiBase._op_apply_changes (self, uri, post)
-452
View File
@@ -1,452 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2009 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#include "common-internal.h"
#include "handler_uwsgi.h"
#include "connection.h"
#include "source_interpreter.h"
#include "thread.h"
#include "util.h"
#include "connection-protected.h"
#include "bogotime.h"
#define ENTRIES "handler,cgi"
/* Plug-in initialization
*/
CGI_LIB_INIT (uwsgi, http_all_methods);
#if BYTE_ORDER == BIG_ENDIAN
static uint16_t uwsgi_swap16(uint16_t x) {
return (uint16_t) ((x & 0xff) << 8 | (x & 0xff00) >> 8);
}
#endif
/* Methods implementation
*/
static ret_t
props_free (cherokee_handler_uwsgi_props_t *props)
{
if (props->balancer)
cherokee_balancer_free (props->balancer);
return cherokee_handler_cgi_base_props_free (PROP_CGI_BASE(props));
}
ret_t
cherokee_handler_uwsgi_configure (cherokee_config_node_t *conf, cherokee_server_t *srv, cherokee_module_props_t **_props)
{
ret_t ret;
cherokee_list_t *i;
cherokee_handler_uwsgi_props_t *props;
/* Instance a new property object
*/
if (*_props == NULL) {
CHEROKEE_NEW_STRUCT (n, handler_uwsgi_props);
cherokee_handler_cgi_base_props_init_base (PROP_CGI_BASE(n),
MODULE_PROPS_FREE(props_free));
n->balancer = NULL;
n->modifier1 = 0 ;
n->modifier2 = 0 ;
n->pass_wsgi_vars = true;
n->pass_request_body = true;
*_props = MODULE_PROPS(n);
}
props = PROP_UWSGI(*_props);
/* Parse the configuration tree
*/
cherokee_config_node_foreach (i, conf) {
cherokee_config_node_t *subconf = CONFIG_NODE(i);
if (equal_buf_str (&subconf->key, "balancer")) {
ret = cherokee_balancer_instance (&subconf->val, subconf, srv, &props->balancer);
if (ret != ret_ok) return ret;
}
else if (equal_buf_str (&subconf->key, "modifier1")) {
props->modifier1 = (uint8_t) atoi(subconf->val.buf);
}
else if (equal_buf_str (&subconf->key, "modifier2")) {
props->modifier2 = (uint8_t) atoi(subconf->val.buf);
}
else if (equal_buf_str (&subconf->key, "pass_wsgi_vars")) {
props->pass_wsgi_vars = !! atoi (subconf->val.buf);
}
else if (equal_buf_str (&subconf->key, "pass_request_body")) {
props->pass_request_body = !! atoi (subconf->val.buf);
}
}
/* Init base class
*/
ret = cherokee_handler_cgi_base_configure (conf, srv, _props);
if (ret != ret_ok) return ret;
/* Final checks
*/
if (props->balancer == NULL) {
LOG_CRITICAL_S (CHEROKEE_ERROR_HANDLER_NO_BALANCER);
return ret_error;
}
return ret_ok;
}
static void
add_env_pair (cherokee_handler_cgi_base_t *cgi_base,
const char *key, int key_len,
const char *val, int val_len)
{
cherokee_handler_uwsgi_t *uwsgi = HDL_UWSGI(cgi_base);
uint16_t u_key_len = (uint16_t) key_len ;
uint16_t u_val_len = (uint16_t) val_len ;
/* 2 bytes for every string (16 bit le) */
cherokee_buffer_ensure_size (&uwsgi->header, uwsgi->header.len + key_len + val_len + 4);
/* force to le if cherokee is big-endian */
#if BYTE_ORDER == BIG_ENDIAN
u_key_len = uwsgi_swap16(u_key_len);
#endif
cherokee_buffer_add (&uwsgi->header, (const char *) &u_key_len, 2);
#if BYTE_ORDER == BIG_ENDIAN
u_key_len = uwsgi_swap16(u_key_len);
#endif
cherokee_buffer_add (&uwsgi->header, key, key_len);
#if BYTE_ORDER == BIG_ENDIAN
u_val_len = uwsgi_swap16(u_val_len);
#endif
cherokee_buffer_add (&uwsgi->header, (const char *) &u_val_len, 2);
#if BYTE_ORDER == BIG_ENDIAN
u_val_len = uwsgi_swap16(u_val_len);
#endif
cherokee_buffer_add (&uwsgi->header, val, val_len);
}
static ret_t
read_from_uwsgi (cherokee_handler_cgi_base_t *cgi_base, cherokee_buffer_t *buffer)
{
ret_t ret;
size_t read = 0;
cherokee_handler_uwsgi_t *uwsgi = HDL_UWSGI(cgi_base);
ret = cherokee_socket_bufread (&uwsgi->socket, buffer, 4096, &read);
switch (ret) {
case ret_eagain:
cherokee_thread_deactive_to_polling (HANDLER_THREAD(cgi_base), HANDLER_CONN(cgi_base),
uwsgi->socket.socket, 0, false);
return ret_eagain;
case ret_ok:
TRACE (ENTRIES, "%d bytes read\n", read);
return ret_ok;
case ret_eof:
case ret_error:
cgi_base->got_eof = true;
return ret;
default:
RET_UNKNOWN(ret);
}
SHOULDNT_HAPPEN;
return ret_error;
}
ret_t
cherokee_handler_uwsgi_new (cherokee_handler_t **hdl, void *cnt, cherokee_module_props_t *props)
{
CHEROKEE_NEW_STRUCT (n, handler_uwsgi);
/* Init the base class
*/
cherokee_handler_cgi_base_init (
HDL_CGI_BASE(n), cnt,
PLUGIN_INFO_HANDLER_PTR(uwsgi),
HANDLER_PROPS(props),
add_env_pair, read_from_uwsgi);
/* Virtual methods
*/
MODULE(n)->init = (handler_func_init_t) cherokee_handler_uwsgi_init;
MODULE(n)->free = (module_func_free_t) cherokee_handler_uwsgi_free;
/* Virtual methods: implemented by handler_cgi_base
*/
HANDLER(n)->step = (handler_func_step_t) cherokee_handler_cgi_base_step;
HANDLER(n)->add_headers = (handler_func_add_headers_t) cherokee_handler_cgi_base_add_headers;
/* Properties
*/
n->post_len = 0;
n->src_ref = NULL;
cherokee_buffer_init (&n->header);
cherokee_socket_init (&n->socket);
/* Return the object
*/
*hdl = HANDLER(n);
return ret_ok;
}
ret_t
cherokee_handler_uwsgi_free (cherokee_handler_uwsgi_t *hdl)
{
/* Free the rest of the handler CGI memory
*/
cherokee_handler_cgi_base_free (HDL_CGI_BASE(hdl));
/* UWSGI stuff
*/
cherokee_socket_close (&hdl->socket);
cherokee_socket_mrproper (&hdl->socket);
cherokee_buffer_mrproper (&hdl->header);
return ret_ok;
}
static ret_t
uwsgi_fix_packet (cherokee_buffer_t *buf, uint8_t modifier1, uint8_t modifier2)
{
uwsgi_header uh;
uh.modifier1 = modifier1 ;
uh.modifier2 = modifier2 ;
#if BYTE_ORDER == BIG_ENDIAN
uh.env_size = uwsgi_swap16((uint16_t) buf->len) ;
#else
uh.env_size = (uint16_t) buf->len ;
#endif
cherokee_buffer_ensure_size (buf, buf->len + 4);
cherokee_buffer_prepend (buf, (const char * )&uh, 4);
return ret_ok;
}
static ret_t
build_header (cherokee_handler_uwsgi_t *hdl)
{
cherokee_handler_uwsgi_props_t *props = HANDLER_UWSGI_PROPS(hdl);
cuint_t len;
char tmp[64];
if (props->pass_request_body == true && props->pass_wsgi_vars == true) {
len = snprintf (tmp, sizeof(tmp), FMT_OFFSET, (CST_OFFSET)hdl->post_len);
add_env_pair(HDL_CGI_BASE(hdl), "CONTENT_LENGTH", 14, tmp, len);
}
if (props->pass_wsgi_vars == true) {
cherokee_handler_cgi_base_build_envp (HDL_CGI_BASE(hdl), HANDLER_CONN(hdl));
}
return uwsgi_fix_packet (&hdl->header, props->modifier1, props->modifier2);
}
static ret_t
connect_to_server (cherokee_handler_uwsgi_t *hdl)
{
ret_t ret;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
cherokee_handler_uwsgi_props_t *props = HANDLER_UWSGI_PROPS(hdl);
/* Get a reference to the target host
*/
if (hdl->src_ref == NULL) {
ret = cherokee_balancer_dispatch (props->balancer, conn, &hdl->src_ref);
if (ret != ret_ok)
return ret;
}
/* Try to connect
*/
if (hdl->src_ref->type == source_host) {
ret = cherokee_source_connect_polling (hdl->src_ref, &hdl->socket, conn);
if ((ret == ret_deny) || (ret == ret_error))
{
cherokee_balancer_report_fail (props->balancer, conn, hdl->src_ref);
}
} else {
ret = cherokee_source_interpreter_connect_polling (SOURCE_INT(hdl->src_ref),
&hdl->socket, conn);
}
return ret;
}
static ret_t
send_header (cherokee_handler_uwsgi_t *hdl)
{
ret_t ret;
size_t written = 0;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
ret = cherokee_socket_bufwrite (&hdl->socket, &hdl->header, &written);
if (ret != ret_ok) {
conn->error_code = http_bad_gateway;
return ret;
}
#if 0
cherokee_buffer_print_debug (&hdl->header, -1);
#endif
cherokee_buffer_move_to_begin (&hdl->header, written);
TRACE (ENTRIES, "sent remaining=%d\n", hdl->header.len);
if (! cherokee_buffer_is_empty (&hdl->header))
return ret_eagain;
return ret_ok;
}
static ret_t
send_post (cherokee_handler_uwsgi_t *hdl)
{
ret_t ret;
int e_fd = -1;
int mode = 0;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
ret = cherokee_post_walk_to_fd (&conn->post, hdl->socket.socket, &e_fd, &mode);
switch (ret) {
case ret_ok:
break;
case ret_eagain:
if (e_fd != -1)
cherokee_thread_deactive_to_polling (HANDLER_THREAD(hdl), conn, e_fd, mode, false);
return ret_eagain;
default:
conn->error_code = http_bad_gateway;
return ret;
}
return ret_ok;
}
ret_t
cherokee_handler_uwsgi_init (cherokee_handler_uwsgi_t *hdl)
{
ret_t ret;
cherokee_connection_t *conn = HANDLER_CONN(hdl);
cherokee_handler_uwsgi_props_t *props = HANDLER_UWSGI_PROPS(hdl);
switch (HDL_CGI_BASE(hdl)->init_phase) {
case hcgi_phase_build_headers:
TRACE (ENTRIES, "Init: %s\n", "begins");
/* Extracts PATH_INFO and filename from request uri
*/
ret = cherokee_handler_cgi_base_extract_path (HDL_CGI_BASE(hdl), false);
if (unlikely (ret < ret_ok)) {
conn->error_code = http_internal_error;
return ret_error;
}
/* Prepare Post
*/
if (! cherokee_post_is_empty (&conn->post)) {
cherokee_post_walk_reset (&conn->post);
cherokee_post_get_len (&conn->post, &hdl->post_len);
}
/* Build the headers
*/
ret = build_header (hdl);
if (unlikely (ret != ret_ok)) {
conn->error_code = http_internal_error;
return ret_error;
}
HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_connect;
case hcgi_phase_connect:
TRACE (ENTRIES, "Init: %s\n", "connect");
/* Connect
*/
ret = connect_to_server (hdl);
switch (ret) {
case ret_ok:
break;
case ret_eagain:
return ret_eagain;
case ret_deny:
conn->error_code = http_gateway_timeout;
return ret_error;
default:
conn->error_code = http_service_unavailable;
return ret_error;
}
HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_send_headers;
case hcgi_phase_send_headers:
TRACE (ENTRIES, "Init: %s\n", "send_headers");
/* Send the header
*/
ret = send_header (hdl);
if (ret != ret_ok)
return ret;
HDL_CGI_BASE(hdl)->init_phase = hcgi_phase_send_post;
case hcgi_phase_send_post:
/* Send the Post
*/
if (hdl->post_len > 0 && props->pass_request_body == true) {
return send_post (hdl);
}
break;
}
return ret_ok;
}
-78
View File
@@ -1,78 +0,0 @@
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*- */
/* Cherokee
*
* Authors:
* Alvaro Lopez Ortega <alvaro@alobbs.com>
*
* Copyright (C) 2001-2009 Alvaro Lopez Ortega
*
* This program is free software; you can redistribute it and/or
* modify it under the terms of version 2 of the GNU General Public
* License as published by the Free Software Foundation.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
* 02110-1301, USA.
*/
#ifndef CHEROKEE_HANDLER_UWSGI_H
#define CHEROKEE_HANDLER_UWSGI_H
#include "common-internal.h"
#include "handler.h"
#include "buffer.h"
#include "plugin_loader.h"
#include "socket.h"
#include "handler_cgi_base.h"
#include "balancer.h"
typedef struct __attribute__((packed)) {
uint8_t modifier1;
uint16_t env_size;
uint8_t modifier2;
} uwsgi_header ;
typedef struct {
cherokee_handler_cgi_base_t base;
cherokee_balancer_t *balancer;
uint8_t modifier1;
uint8_t modifier2;
cherokee_boolean_t pass_wsgi_vars;
cherokee_boolean_t pass_request_body;
} cherokee_handler_uwsgi_props_t;
typedef struct {
cherokee_handler_cgi_base_t base;
cherokee_buffer_t header;
cherokee_socket_t socket;
cherokee_source_t *src_ref;
time_t spawned;
off_t post_len;
} cherokee_handler_uwsgi_t;
#define HDL_UWSGI(x) ((cherokee_handler_uwsgi_t *)(x))
#define PROP_UWSGI(x) ((cherokee_handler_uwsgi_props_t *)(x))
#define HANDLER_UWSGI_PROPS(x) (PROP_UWSGI(MODULE(x)->props))
/* Library init function
*/
void PLUGIN_INIT_NAME(uwsgi) (cherokee_plugin_loader_t *loader);
/* Methods
*/
ret_t cherokee_handler_uwsgi_new (cherokee_handler_t **hdl, void *cnt, cherokee_module_props_t *props);
ret_t cherokee_handler_uwsgi_free (cherokee_handler_uwsgi_t *hdl);
ret_t cherokee_handler_uwsgi_init (cherokee_handler_uwsgi_t *hdl);
#endif /* CHEROKEE_HANDLER_UWSGI_H */
-1
View File
@@ -1 +0,0 @@
file