Module: Agoo::Server
- Defined in:
- ext/agoo/rserver.c
Class Method Summary collapse
-
.add_mime(suffix, type) ⇒ Object
call-seq: add_mime(suffix, type).
-
.domain(host, path) ⇒ Object
call-seq: domain(host, path).
-
.handle(method, pattern, handler) ⇒ Object
call-seq: handle(method, pattern, handler).
-
.handle_not_found(handler) ⇒ Object
call-seq: not_found_handle(handler).
-
.header_rule(path, mime, key, value) ⇒ Object
call-seq: header_rule(path, mime, key, value).
-
.init(*args) ⇒ Object
call-seq: init(port, root, options).
-
.path_group(path, dirs) ⇒ Object
call-seq: path_group(path, dirs).
-
.rack_early_hints(on) ⇒ Object
call-seq: rack_early_hints(on).
-
.shutdown ⇒ Object
call-seq: shutdown().
-
.start ⇒ Object
call-seq: start().
-
.use(*args) ⇒ Object
call-seq: use(middleware, *args).
Class Method Details
.add_mime(suffix, type) ⇒ Object
call-seq: add_mime(suffix, type)
Adds a mime type by associating a type string with a suffix. This is used for static files.
1069 1070 1071 1072 1073 1074 1075 1076 1077 |
# File 'ext/agoo/rserver.c', line 1069
static VALUE
add_mime(VALUE self, VALUE suffix, VALUE type) {
struct _agooErr err = AGOO_ERR_INIT;
if (AGOO_ERR_OK != mime_set(&err, StringValuePtr(suffix), StringValuePtr(type))) {
rb_raise(rb_eArgError, "%s", err.msg);
}
return Qnil;
}
|
.domain(host, path) ⇒ Object
call-seq: domain(host, path)
Sets up a sub-domain. The first argument, host should be either a String or a Regexp that includes variable replacement elements. The path argument should also be a string. If the host argument is a Regex then the $(n) sequence will be replaced by the matching variable in the Regex result. The path is the root of the sub-domain.
1154 1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 |
# File 'ext/agoo/rserver.c', line 1154
static VALUE
domain(VALUE self, VALUE host, VALUE path) {
struct _agooErr err = AGOO_ERR_INIT;
switch(rb_type(host)) {
case RUBY_T_STRING:
rb_check_type(path, T_STRING);
if (AGOO_ERR_OK != agoo_domain_add(&err, rb_string_value_ptr((VALUE*)&host), rb_string_value_ptr((VALUE*)&path))) {
rb_raise(rb_eArgError, "%s", err.msg);
}
break;
case RUBY_T_REGEXP: {
volatile VALUE v = rb_funcall(host, rb_intern("inspect"), 0);
char rx[1024];
if (sizeof(rx) <= (size_t)RSTRING_LEN(v)) {
rb_raise(rb_eArgError, "host Regex limited to %ld characters", sizeof(rx));
}
strcpy(rx, rb_string_value_ptr((VALUE*)&v) + 1);
rx[RSTRING_LEN(v) - 2] = '\0';
if (AGOO_ERR_OK != agoo_domain_add_regex(&err, rx, rb_string_value_ptr((VALUE*)&path))) {
rb_raise(rb_eArgError, "%s", err.msg);
}
break;
}
default:
rb_raise(rb_eArgError, "host must be a String or Regex");
break;
}
return Qnil;
}
|
.handle(method, pattern, handler) ⇒ Object
call-seq: handle(method, pattern, handler)
Registers a handler for the HTTP method and path pattern specified. The path pattern follows glob like rules in that a single * matches a single token bounded by the ‘/` character and a double ** matches all remaining.
950 951 952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972 973 974 975 976 977 978 979 980 981 982 983 984 985 986 987 988 989 990 991 992 993 994 995 996 997 998 999 1000 1001 1002 1003 1004 1005 1006 1007 1008 1009 1010 1011 1012 1013 1014 1015 1016 1017 1018 1019 1020 1021 1022 1023 1024 1025 1026 1027 1028 1029 1030 1031 1032 1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 |
# File 'ext/agoo/rserver.c', line 950
static VALUE
handle(VALUE self, VALUE method, VALUE pattern, VALUE handler) {
agooHook hook;
agooMethod meth = AGOO_ALL;
const char *pat;
ID static_id = rb_intern("static?");
rb_check_type(pattern, T_STRING);
pat = StringValuePtr(pattern);
if (connect_sym == method) {
meth = AGOO_CONNECT;
} else if (delete_sym == method) {
meth = AGOO_DELETE;
} else if (get_sym == method) {
meth = AGOO_GET;
} else if (head_sym == method) {
meth = AGOO_HEAD;
} else if (options_sym == method) {
meth = AGOO_OPTIONS;
} else if (post_sym == method) {
meth = AGOO_POST;
} else if (put_sym == method) {
meth = AGOO_PUT;
} else if (Qnil == method) {
meth = AGOO_ALL;
} else {
rb_raise(rb_eArgError, "invalid method");
}
if (T_STRING == rb_type(handler)) {
handler = resolve_classpath(StringValuePtr(handler), RSTRING_LEN(handler));
}
if (rb_respond_to(handler, static_id)) {
if (Qtrue == rb_funcall(handler, static_id, 0)) {
VALUE res = rb_funcall(handler, call_id, 1, Qnil);
VALUE bv;
rb_check_type(res, T_ARRAY);
if (3 != RARRAY_LEN(res)) {
rb_raise(rb_eArgError, "a rack call() response must be an array of 3 members.");
}
bv = rb_ary_entry(res, 2);
if (T_ARRAY == rb_type(bv)) {
int i;
int bcnt = (int)RARRAY_LEN(bv);
agooText t = agoo_text_allocate(1024);
struct _agooErr err = AGOO_ERR_INIT;
VALUE v;
if (NULL == t) {
rb_raise(rb_eArgError, "failed to allocate response.");
}
for (i = 0; i < bcnt; i++) {
v = rb_ary_entry(bv, i);
t = agoo_text_append(t, StringValuePtr(v), (int)RSTRING_LEN(v));
}
if (NULL == t) {
rb_raise(rb_eNoMemError, "Failed to allocate memory for a response.");
}
if (NULL == agoo_page_immutable(&err, pat, t->text, (int)t->len)) {
rb_raise(rb_eArgError, "%s", err.msg);
}
agoo_text_release(t);
return Qnil;
}
}
}
if (NULL != the_rserver.uses) {
RUse u;
for (u = the_rserver.uses; NULL != u; u = u->next) {
u->argv[0] = handler;
handler = rb_funcall2(u->clas, rb_intern("new"), u->argc, u->argv);
}
}
if (NULL == (hook = rhook_create(meth, pat, handler, &agoo_server.eval_queue))) {
rb_raise(rb_eStandardError, "out of memory.");
} else {
agooHook h;
agooHook prev = NULL;
for (h = agoo_server.hooks; NULL != h; h = h->next) {
prev = h;
}
if (NULL != prev) {
prev->next = hook;
} else {
agoo_server.hooks = hook;
}
rb_gc_register_address((VALUE*)&hook->handler);
}
return Qnil;
}
|
.handle_not_found(handler) ⇒ Object
call-seq: not_found_handle(handler)
Registers a handler to be called when no other hook is found and no static file is found.
1052 1053 1054 1055 1056 1057 1058 1059 1060 |
# File 'ext/agoo/rserver.c', line 1052
static VALUE
handle_not_found(VALUE self, VALUE handler) {
if (NULL == (agoo_server.hook404 = rhook_create(AGOO_GET, "/", handler, &agoo_server.eval_queue))) {
rb_raise(rb_eStandardError, "out of memory.");
}
rb_gc_register_address((VALUE*)&agoo_server.hook404->handler);
return Qnil;
}
|
.header_rule(path, mime, key, value) ⇒ Object
call-seq: header_rule(path, mime, key, value)
Add a header rule. A header rule will add the key and value to the headers of any static asset that matches the path and mime type specified. The path pattern follows glob like rules in that a single * matches a single token bounded by the ‘/` character and a double ** matches all remaining. The mime can also be a * which matches all types. The mime argument will be compared to the mine type as well as the file extension so ’applicaiton/json’, a mime type can be used as can ‘json’ as a file extension. All rules that match add the header key and value to the header of a static asset.
Note that the server must be initialized before calling this method.
1129 1130 1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 |
# File 'ext/agoo/rserver.c', line 1129
static VALUE
header_rule(VALUE self, VALUE path, VALUE mime, VALUE key, VALUE value) {
struct _agooErr err = AGOO_ERR_INIT;
rb_check_type(path, T_STRING);
rb_check_type(mime, T_STRING);
rb_check_type(key, T_STRING);
rb_check_type(value, T_STRING);
if (AGOO_ERR_OK != agoo_header_rule(&err, StringValuePtr(path), StringValuePtr(mime), StringValuePtr(key), StringValuePtr(value))) {
rb_raise(rb_eArgError, "%s", err.msg);
}
return Qnil;
}
|
.init(*args) ⇒ Object
call-seq: init(port, root, options)
Configures the server that will listen on the designated port and using the root as the root of the static resources. This must be called before using other server methods. Logging is feature based and not level based and the options reflect that approach. If bind option is to be used instead of the port then set the port to zero.
-
options [Hash] server options
-
:pedantic [true|false] if true response header and status codes are checked and an exception raised if they violate the rack spec at github.com/rack/rack/blob/master/SPEC, tools.ietf.org/html/rfc3875#section-4.1.18, or tools.ietf.org/html/rfc7230.
-
:thread_count [Integer] number of ruby worker threads. Defaults to one. If zero then the start function will not return but instead will proess using the thread that called start. Usually the default is best unless the workers are making IO calls.
-
:worker_count [Integer] number of workers to fork. Defaults to one which is not to fork.
-
:poll_timeout [Float] timeout seconds when polling. Default is 0.1. Lower gives faster response times but uses more CPU.
-
:bind [String|Array] a binding or array of binds. Examples are: “http ://127.0.0.1:6464”, “unix:///tmp/agoo.socket”, “http ://[::1]:6464, or to not restrict the address ”http ://:6464“.
-
:graphql [String] path to GraphQL endpoint if support for GraphQL is desired.
-
:max_push_pending [Integer] maximum number or outstanding push messages, less than 1000.
-
:ssl_sert [String] filepath to the SSL certificate file.
-
:ssl_key [String] filepath to the SSL private key file.
-
299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 |
# File 'ext/agoo/rserver.c', line 299
static VALUE
rserver_init(int argc, VALUE *argv, VALUE self) {
struct _agooErr err = AGOO_ERR_INIT;
int port;
const char *root;
VALUE options = Qnil;
if (argc < 2 || 3 < argc) {
rb_raise(rb_eArgError, "Wrong number of arguments to Agoo::Server.configure.");
}
port = FIX2INT(argv[0]);
rb_check_type(argv[1], T_STRING);
root = StringValuePtr(argv[1]);
if (3 <= argc) {
options = argv[2];
}
if (AGOO_ERR_OK != agoo_server_setup(&err)) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
agoo_server.ctx_nil_value = (void*)Qnil;
agoo_server.env_nil_value = (void*)Qnil;
if (AGOO_ERR_OK != configure(&err, port, root, options)) {
rb_raise(rb_eArgError, "%s", err.msg);
}
agoo_server.inited = true;
return Qnil;
}
|
.path_group(path, dirs) ⇒ Object
call-seq: path_group(path, dirs)
Sets up a path group where the path defines a group of directories to search for a file. For example a path of ‘/assets’ could be mapped to a set of [ ‘home/user/images’, ‘/home/user/app/assets/images’ ].
1087 1088 1089 1090 1091 1092 1093 1094 1095 1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108 1109 1110 1111 |
# File 'ext/agoo/rserver.c', line 1087
static VALUE
path_group(VALUE self, VALUE path, VALUE dirs) {
struct _agooErr err = AGOO_ERR_INIT;
agooGroup g;
rb_check_type(path, T_STRING);
rb_check_type(dirs, T_ARRAY);
if (NULL != (g = agoo_group_create(StringValuePtr(path)))) {
int i;
int dcnt = (int)RARRAY_LEN(dirs);
VALUE entry;
for (i = dcnt - 1; 0 <= i; i--) {
entry = rb_ary_entry(dirs, i);
if (T_STRING != rb_type(entry)) {
entry = rb_funcall(entry, rb_intern("to_s"), 0);
}
if (NULL == agoo_group_add(&err, g, StringValuePtr(entry))) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
}
}
return Qnil;
}
|
.rack_early_hints(on) ⇒ Object
call-seq: rack_early_hints(on)
Turns on or off the inclusion of a early_hints object in the rack call env Hash. If the argument is nil then the current value is returned.
1193 1194 1195 1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 |
# File 'ext/agoo/rserver.c', line 1193
static VALUE
rack_early_hints(VALUE self, VALUE on) {
if (Qtrue == on) {
agoo_server.rack_early_hints = true;
} else if (Qfalse == on) {
agoo_server.rack_early_hints = false;
} else if (Qnil == on) {
on = agoo_server.rack_early_hints ? Qtrue : Qfalse;
} else {
rb_raise(rb_eArgError, "rack_early_hints can only be set to true or false");
}
return on;
}
|
.shutdown ⇒ Object
call-seq: shutdown()
Shutdown the server. Logs and queues are flushed before shutting down.
898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 |
# File 'ext/agoo/rserver.c', line 898
VALUE
rserver_shutdown(VALUE self) {
if (agoo_server.inited) {
agoo_server_shutdown("Agoo", stop_runners);
if (1 < the_rserver.worker_cnt && getpid() == *the_rserver.worker_pids) {
int i;
int status;
int exit_cnt = 1;
int j;
for (i = 1; i < the_rserver.worker_cnt; i++) {
kill(the_rserver.worker_pids[i], SIGKILL);
}
for (j = 0; j < 20; j++) {
for (i = 1; i < the_rserver.worker_cnt; i++) {
if (0 == the_rserver.worker_pids[i]) {
continue;
}
if (0 < waitpid(the_rserver.worker_pids[i], &status, WNOHANG)) {
if (WIFEXITED(status)) {
//printf("exited, status=%d for %d\n", agoo_server.worker_pids[i], WEXITSTATUS(status));
the_rserver.worker_pids[i] = 0;
exit_cnt++;
} else if (WIFSIGNALED(status)) {
//printf("*** killed by signal %d for %d\n", agoo_server.worker_pids[i], WTERMSIG(status));
the_rserver.worker_pids[i] = 0;
exit_cnt++;
}
}
}
if (the_rserver.worker_cnt <= exit_cnt) {
break;
}
dsleep(0.2);
}
if (exit_cnt < the_rserver.worker_cnt) {
printf("*-*-* Some workers did not exit.\n");
}
}
}
return Qnil;
}
|
.start ⇒ Object
call-seq: start()
Start the server.
788 789 790 791 792 793 794 795 796 797 798 799 800 801 802 803 804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828 829 830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 |
# File 'ext/agoo/rserver.c', line 788
static VALUE
rserver_start(VALUE self) {
VALUE *vp;
int i;
int pid;
double giveup;
struct _agooErr err = AGOO_ERR_INIT;
VALUE agoo = rb_const_get_at(rb_cObject, rb_intern("Agoo"));
VALUE v = rb_const_get_at(agoo, rb_intern("VERSION"));
*the_rserver.worker_pids = getpid();
// If workers then set the loop_max based on the expected number of
// threads per worker.
if (1 < the_rserver.worker_cnt) {
agoo_server.loop_max /= the_rserver.worker_cnt;
if (agoo_server.loop_max < 1) {
agoo_server.loop_max = 1;
}
}
if (AGOO_ERR_OK != setup_listen(&err)) {
rb_raise(rb_eIOError, "%s", err.msg);
}
for (i = 1; i < the_rserver.worker_cnt; i++) {
VALUE rpid = rb_funcall(rb_cObject, rb_intern("fork"), 0);
if (Qnil == rpid) {
pid = 0;
} else {
pid = NUM2INT(rpid);
}
if (0 > pid) { // error, use single process
agoo_log_cat(&agoo_error_cat, "Failed to fork. %s.", strerror(errno));
break;
} else if (0 == pid) {
if (AGOO_ERR_OK != agoo_log_start(&err, true)) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
break;
} else {
the_rserver.worker_pids[i] = pid;
}
}
if (AGOO_ERR_OK != agoo_server_start(&err, "Agoo", StringValuePtr(v))) {
rb_raise(rb_eStandardError, "%s", err.msg);
}
if (0 >= agoo_server.thread_cnt) {
agooReq req;
while (agoo_server.active) {
if (NULL != (req = (agooReq)agoo_queue_pop(&agoo_server.eval_queue, poll_timeout))) {
handle_protected(req, false);
agoo_req_destroy(req);
} else {
rb_thread_schedule();
}
if (agoo_stop) {
agoo_shutdown();
break;
}
}
} else {
if (NULL == (the_rserver.eval_threads = (VALUE*)AGOO_MALLOC(sizeof(VALUE) * (agoo_server.thread_cnt + 1)))) {
rb_raise(rb_eNoMemError, "Failed to allocate memory for the thread pool.");
}
for (i = agoo_server.thread_cnt, vp = the_rserver.eval_threads; 0 < i; i--, vp++) {
*vp = rb_thread_create(wrap_process_loop, NULL);
}
*vp = Qnil;
giveup = dtime() + 1.0;
while (dtime() < giveup) {
// The processing threads will not start until this thread
// releases ownership so do that and then see if the threads has
// been started yet.
rb_thread_schedule();
if (2 + agoo_server.thread_cnt <= (long)atomic_load(&agoo_server.running)) {
break;
}
}
}
return Qnil;
}
|
.use(*args) ⇒ Object
call-seq: use(middleware, *args)
The use function must be called before the handle functions. Any invocations of use apply only to handlers called after the call to use.
1214 1215 1216 1217 1218 1219 1220 1221 1222 1223 1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236 1237 1238 1239 |
# File 'ext/agoo/rserver.c', line 1214
static VALUE
use(int argc, VALUE *argv, VALUE self) {
VALUE mc;
RUse u;
if (argc < 1) { // at least the middleware class must be provided.
rb_raise(rb_eArgError, "no middleware class provided");
}
mc = argv[0];
if (T_CLASS != rb_type(mc)) {
rb_raise(rb_eArgError, "the first argument to use must be a class");
}
if (NULL == (u = (RUse)AGOO_MALLOC(sizeof(struct _rUse)))) {
rb_raise(rb_eNoMemError, "Failed to allocate memory for a middleware use.");
}
u->clas = mc;
u->argc = argc;
if (NULL == (u->argv = (VALUE*)AGOO_MALLOC(sizeof(VALUE) * u->argc))) {
rb_raise(rb_eNoMemError, "Failed to allocate memory for a middleware use.");
}
memcpy(u->argv, argv, argc * sizeof(VALUE));
u->next = the_rserver.uses;
the_rserver.uses = u;
return Qnil;
}
|