Permalink
Browse files

Added Ruby support.

  • Loading branch information...
lexborisov committed Mar 21, 2018
1 parent 778a90c commit 37051b6c15cce7d6ab01c50e1086f8ef0b34e93d
@@ -48,4 +48,7 @@ cat << END
perl OPTIONS configure Perl module
run "./configure perl --help" to see available options
ruby OPTIONS configure Ruby module
run "./configure ruby --help" to see available options
END
@@ -21,6 +21,10 @@ case "$nxt_module" in
. auto/modules/perl
;;
ruby)
. auto/modules/ruby
;;
*)
echo
echo $0: error: invalid module \"$nxt_module\".
@@ -0,0 +1,171 @@
# Copyright (C) Alexander Borisov
# Copyright (C) NGINX, Inc.
shift
for nxt_option; do
case "$nxt_option" in
-*=*) value=`echo "$nxt_option" | sed -e 's/[-_a-zA-Z0-9]*=//'` ;;
*) value="" ;;
esac
case "$nxt_option" in
--ruby=*) NXT_RUBY="$value" ;;
--module=*) NXT_RUBY_MODULE="$value" ;;
--help)
cat << END
--ruby=FILE set ruby executable, default: ruby
--module=NAME set unit ruby module name
END
exit 0
;;
*)
echo
echo $0: error: invalid Ruby option \"$nxt_option\"
echo
exit 1
;;
esac
done
if [ ! -f $NXT_AUTOCONF_DATA ]; then
echo
echo Please run common $0 before configuring module \"$nxt_module\".
echo
exit 1
fi
. $NXT_AUTOCONF_DATA
$echo "configuring Ruby module"
$echo "configuring Ruby module ..." >> $NXT_AUTOCONF_ERR
NXT_RUBY=${NXT_RUBY=ruby}
NXT_RUBY_MODULE=${NXT_RUBY_MODULE=${NXT_RUBY}}
nxt_found=no
if /bin/sh -c "$NXT_RUBY -v" >> $NXT_AUTOCONF_ERR 2>&1; then
NXT_RUBY_RUBYHDRDIR=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["rubyhdrdir"])'`
NXT_RUBY_ARCHHDRDIR=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["rubyarchhdrdir"])'`
NXT_RUBY_INCPATH="-I$NXT_RUBY_ARCHHDRDIR -I$NXT_RUBY_RUBYHDRDIR"
NXT_RUBY_LIBNAME=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["RUBY_SO_NAME"])'`
NXT_RUBY_LIBSCONF=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["LIBS"])'`
NXT_RUBY_LIBPATH=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["libdir"])'`
NXT_RUBY_LIBS="-L$NXT_RUBY_LIBPATH -Wl,-rpath,${NXT_RUBY_LIBPATH} -l$NXT_RUBY_LIBNAME $NXT_RUBY_LIBSCONF"
nxt_feature="Ruby"
nxt_feature_name=""
nxt_feature_run=no
nxt_feature_incs="${NXT_RUBY_INCPATH}"
nxt_feature_libs="${NXT_RUBY_LIBS}"
nxt_feature_test="
#include <ruby.h>
int main() {
ruby_init();
return ruby_cleanup(0);
}"
. auto/feature
else
$echo "checking for Ruby ... not found"
fi
if [ $nxt_found = no ]; then
$echo
$echo $0: error: no Ruby found.
$echo
exit 1;
fi
NXT_RUBY_VERSION=`$NXT_RUBY -r rbconfig -e 'printf("%s",RbConfig::CONFIG["ruby_version"])'`
$echo " + Ruby version: ${NXT_RUBY_VERSION}"
if grep ^$NXT_RUBY_MODULE: $NXT_MAKEFILE 2>&1 > /dev/null; then
$echo
$echo $0: error: duplicate \"$NXT_RUBY_MODULE\" module configured.
$echo
exit 1;
fi
$echo " + Ruby module: ${NXT_RUBY_MODULE}.unit.so"
. auto/cc/deps
$echo >> $NXT_MAKEFILE
NXT_RUBY_MODULE_SRCS=" \
src/ruby/nxt_ruby.c \
src/ruby/nxt_ruby_stream_io.c
"
# The Ruby module object files.
nxt_objs=
for nxt_src in $NXT_RUBY_MODULE_SRCS; do
nxt_obj=${nxt_src%.c}-$NXT_RUBY_MODULE.o
nxt_dep=${nxt_src%.c}-$NXT_RUBY_MODULE.dep
nxt_dep_flags=`nxt_gen_dep_flags`
nxt_dep_post=`nxt_gen_dep_post`
nxt_objs="$nxt_objs $NXT_BUILD_DIR/$nxt_obj"
cat << END >> $NXT_MAKEFILE
$NXT_BUILD_DIR/$nxt_obj: $nxt_src
mkdir -p $NXT_BUILD_DIR/src/ruby
\$(CC) -c \$(CFLAGS) \$(NXT_INCS) $NXT_RUBY_INCPATH \\
$nxt_dep_flags \\
-o $NXT_BUILD_DIR/$nxt_obj $nxt_src
$nxt_dep_post
-include $NXT_BUILD_DIR/$nxt_dep
END
done
cat << END >> $NXT_MAKEFILE
.PHONY: ${NXT_RUBY_MODULE}
.PHONY: ${NXT_RUBY_MODULE}-install
.PHONY: ${NXT_RUBY_MODULE}-uninstall
all: ${NXT_RUBY_MODULE}
${NXT_RUBY_MODULE}: $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so
$NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so: $nxt_objs
\$(NXT_MODULE_LINK) -o $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so \\
$nxt_objs $NXT_RUBY_LIBS $NXT_LD_OPT
install: ${NXT_RUBY_MODULE}-install
${NXT_RUBY_MODULE}-install: ${NXT_RUBY_MODULE}
install -d \$(DESTDIR)$NXT_MODULES
install -p $NXT_BUILD_DIR/${NXT_RUBY_MODULE}.unit.so \\
\$(DESTDIR)$NXT_MODULES/
uninstall: ${NXT_RUBY_MODULE}-uninstall
${NXT_RUBY_MODULE}-uninstall:
rm -f \$(DESTDIR)$NXT_MODULES/${NXT_RUBY_MODULE}.unit.so
@rmdir -p \$(DESTDIR)$NXT_MODULES 2>/dev/null || true
END
@@ -875,6 +875,9 @@ nxt_app_parse_type(u_char *p, size_t length)
} else if (nxt_str_eq(&str, "perl", 4)) {
return NXT_APP_PERL;
} else if (nxt_str_eq(&str, "ruby", 4)) {
return NXT_APP_RUBY;
}
return NXT_APP_UNKNOWN;
@@ -14,6 +14,7 @@ typedef enum {
NXT_APP_PHP,
NXT_APP_GO,
NXT_APP_PERL,
NXT_APP_RUBY,
NXT_APP_UNKNOWN,
} nxt_app_type_t;
@@ -58,6 +59,11 @@ typedef struct {
} nxt_perl_app_conf_t;
typedef struct {
nxt_str_t script;
} nxt_ruby_app_conf_t;
struct nxt_common_app_conf_s {
nxt_str_t name;
nxt_str_t type;
@@ -71,6 +77,7 @@ struct nxt_common_app_conf_s {
nxt_php_app_conf_t php;
nxt_go_app_conf_t go;
nxt_perl_app_conf_t perl;
nxt_ruby_app_conf_t ruby;
} u;
};
@@ -145,8 +152,9 @@ struct nxt_app_wmsg_s {
uint32_t stream;
};
struct nxt_app_rmsg_s {
nxt_buf_t *buf; /* current buffer to read */
nxt_buf_t *buf; /* current buffer to read */
};
@@ -224,6 +224,16 @@ static nxt_conf_vldt_object_t nxt_conf_vldt_perl_members[] = {
};
static nxt_conf_vldt_object_t nxt_conf_vldt_ruby_members[] = {
{ nxt_string("script"),
NXT_CONF_VLDT_STRING,
NULL,
NULL },
NXT_CONF_VLDT_NEXT(&nxt_conf_vldt_common_members)
};
nxt_int_t
nxt_conf_validate(nxt_conf_validation_t *vldt)
{
@@ -413,6 +423,7 @@ nxt_conf_vldt_app(nxt_conf_validation_t *vldt, nxt_str_t *name,
nxt_conf_vldt_php_members,
nxt_conf_vldt_go_members,
nxt_conf_vldt_perl_members,
nxt_conf_vldt_ruby_members,
};
ret = nxt_conf_vldt_type(vldt, name, value, NXT_CONF_VLDT_OBJECT);
@@ -182,11 +182,21 @@ static nxt_conf_map_t nxt_perl_app_conf[] = {
};
static nxt_conf_map_t nxt_ruby_app_conf[] = {
{
nxt_string("script"),
NXT_CONF_MAP_STR,
offsetof(nxt_common_app_conf_t, u.ruby.script),
},
};
static nxt_conf_app_map_t nxt_app_maps[] = {
{ nxt_nitems(nxt_python_app_conf), nxt_python_app_conf },
{ nxt_nitems(nxt_php_app_conf), nxt_php_app_conf },
{ nxt_nitems(nxt_go_app_conf), nxt_go_app_conf },
{ nxt_nitems(nxt_perl_app_conf), nxt_perl_app_conf },
{ nxt_nitems(nxt_ruby_app_conf), nxt_ruby_app_conf },
};
@@ -229,6 +229,8 @@ static nxt_int_t nxt_go_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
nxt_app_wmsg_t *wmsg);
static nxt_int_t nxt_perl_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
nxt_app_wmsg_t *wmsg);
static nxt_int_t nxt_ruby_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
nxt_app_wmsg_t *wmsg);
static void nxt_router_conn_free(nxt_task_t *task, void *obj, void *data);
static void nxt_router_app_timeout(nxt_task_t *task, void *obj, void *data);
@@ -250,6 +252,7 @@ static nxt_app_prepare_msg_t nxt_app_prepare_msg[] = {
nxt_php_prepare_msg,
nxt_go_prepare_msg,
nxt_perl_prepare_msg,
nxt_ruby_prepare_msg,
};
@@ -4127,6 +4130,91 @@ nxt_perl_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
}
static nxt_int_t
nxt_ruby_prepare_msg(nxt_task_t *task, nxt_app_request_t *r,
nxt_app_wmsg_t *wmsg)
{
nxt_int_t rc;
nxt_str_t str;
nxt_buf_t *b;
nxt_http_field_t *field;
nxt_app_request_header_t *h;
static const nxt_str_t prefix = nxt_string("HTTP_");
static const nxt_str_t eof = nxt_null_string;
h = &r->header;
#define RC(S) \
do { \
rc = (S); \
if (nxt_slow_path(rc != NXT_OK)) { \
goto fail; \
} \
} while(0)
#define NXT_WRITE(N) \
RC(nxt_app_msg_write_str(task, wmsg, N))
/* TODO error handle, async mmap buffer assignment */
NXT_WRITE(&h->method);
NXT_WRITE(&h->target);
if (h->query.length) {
str.start = h->target.start;
str.length = (h->target.length - h->query.length) - 1;
RC(nxt_app_msg_write_str(task, wmsg, &str));
} else {
NXT_WRITE(&eof);
}
if (h->query.start != NULL) {
RC(nxt_app_msg_write_size(task, wmsg,
h->query.start - h->target.start + 1));
} else {
RC(nxt_app_msg_write_size(task, wmsg, 0));
}
NXT_WRITE(&h->version);
NXT_WRITE(&r->remote);
NXT_WRITE(&r->local);
NXT_WRITE(&h->host);
NXT_WRITE(&h->content_type);
NXT_WRITE(&h->content_length);
nxt_list_each(field, h->fields) {
RC(nxt_app_msg_write_prefixed_upcase(task, wmsg, &prefix,
field->name, field->name_length));
RC(nxt_app_msg_write(task, wmsg, field->value, field->value_length));
} nxt_list_loop;
/* end-of-headers mark */
NXT_WRITE(&eof);
RC(nxt_app_msg_write_size(task, wmsg, r->body.preread_size));
for (b = r->body.buf; b != NULL; b = b->next) {
RC(nxt_app_msg_write_raw(task, wmsg, b->mem.pos,
nxt_buf_mem_used_size(&b->mem)));
}
#undef NXT_WRITE
#undef RC
return NXT_OK;
fail:
return NXT_ERROR;
}
const nxt_conn_state_t nxt_router_conn_close_state
nxt_aligned(64) =
{
Oops, something went wrong.

0 comments on commit 37051b6

Please sign in to comment.