Permalink
Browse files

~ nginx_ -> ngx_

~ subrequest alpha
  • Loading branch information...
1 parent dcc7846 commit 522c3568a95477611b7860d7e2a2528d4852aab6 @peter-leonov committed Dec 8, 2008
Showing with 537 additions and 163 deletions.
  1. +9 −3 config
  2. +117 −0 environment.c
  3. +0 −18 js/lib.js
  4. +44 −23 js/nginx.js
  5. +18 −0 js/proto.js
  6. +15 −0 lib/Handler.js
  7. +2 −2 nginx.conf
  8. +332 −117 ngx_http_js_module.c
View
12 config
@@ -5,10 +5,10 @@ NGX_ADDON_SRCS="$NGX_ADDON_SRCS $ngx_addon_dir/ngx_http_js_module.c"
echo "checking for JavaScript"
NGX_JS="js"
NGX_JS_VER=`$NGX_JS -v 2>&1 | grep '^JavaScript-C' 2>&1 | sed -e 's/^JavaScript-C \(.*\)/\1/'`
-
if test -n "$NGX_JS_VER"; then
echo " + JavaScript version: $NGX_JS_VER"
+ # trying to eval some code for version => 1.7
if [ "`echo 'let a; print("OK")' | $NGX_JS -version 170 2>&1`" != OK ]; then
echo
echo "$0: error: JavaScript 1.7.0 or higher is required"
@@ -17,14 +17,20 @@ if test -n "$NGX_JS_VER"; then
exit 1;
fi
+ # this is to help a compiller find our ngx_http_js_module.h
CFLAGS="-I $ngx_addon_dir $CFLAGS"
- # CORE_LINK="$CORE_LINK -ljs"
+ # for proper linking with libjs
CORE_LIBS="$CORE_LIBS -ljs"
else
echo
- echo "$0: error: JavaScript 1.7.0 or higher is required, none was founded"
+ echo "$0: error: JavaScript 1.7.0 or higher is required: none was founded"
echo
exit 1;
fi
+
+# defining path for main js-file: nginx.js
+NGX_HTTP_JS_CONF_PATH="$NGX_PREFIX/js/nginx.js"
+have=NGX_HTTP_JS_CONF_PATH value="\"$NGX_HTTP_JS_CONF_PATH\""
+. auto/define
View
@@ -0,0 +1,117 @@
+
+// token from SpiderMonkey 1.7.0 source (js.c:2740)
+
+static JSBool
+env_setProperty(JSContext *cx, JSObject *obj, jsval id, jsval *vp)
+{
+/* XXX porting may be easy, but these don't seem to supply setenv by default */
+#if !defined XP_BEOS && !defined XP_OS2 && !defined SOLARIS
+ JSString *idstr, *valstr;
+ const char *name, *value;
+ int rv;
+
+ idstr = JS_ValueToString(cx, id);
+ valstr = JS_ValueToString(cx, *vp);
+ if (!idstr || !valstr)
+ return JS_FALSE;
+ name = JS_GetStringBytes(idstr);
+ value = JS_GetStringBytes(valstr);
+#if defined XP_WIN || defined HPUX || defined OSF1 || defined IRIX
+ {
+ char *waste = JS_smprintf("%s=%s", name, value);
+ if (!waste) {
+ JS_ReportOutOfMemory(cx);
+ return JS_FALSE;
+ }
+ rv = putenv(waste);
+#ifdef XP_WIN
+ /*
+ * HPUX9 at least still has the bad old non-copying putenv.
+ *
+ * Per mail from <s.shanmuganathan@digital.com>, OSF1 also has a putenv
+ * that will crash if you pass it an auto char array (so it must place
+ * its argument directly in the char *environ[] array).
+ */
+ free(waste);
+#endif
+ }
+#else
+ rv = setenv(name, value, 1);
+#endif
+ if (rv < 0) {
+ JS_ReportError(cx, "can't set envariable %s to %s", name, value);
+ return JS_FALSE;
+ }
+ *vp = STRING_TO_JSVAL(valstr);
+#endif /* !defined XP_BEOS && !defined XP_OS2 && !defined SOLARIS */
+ return JS_TRUE;
+}
+
+static JSBool
+env_enumerate(JSContext *cx, JSObject *obj)
+{
+ static JSBool reflected;
+ char **evp, *name, *value;
+ JSString *valstr;
+ JSBool ok;
+
+ if (reflected)
+ return JS_TRUE;
+
+ for (evp = (char **)JS_GetPrivate(cx, obj); (name = *evp) != NULL; evp++) {
+ value = strchr(name, '=');
+ if (!value)
+ continue;
+ *value++ = '\0';
+ valstr = JS_NewStringCopyZ(cx, value);
+ if (!valstr) {
+ ok = JS_FALSE;
+ } else {
+ ok = JS_DefineProperty(cx, obj, name, STRING_TO_JSVAL(valstr),
+ NULL, NULL, JSPROP_ENUMERATE);
+ }
+ value[-1] = '=';
+ if (!ok)
+ return JS_FALSE;
+ }
+
+ reflected = JS_TRUE;
+ return JS_TRUE;
+}
+
+static JSBool
+env_resolve(JSContext *cx, JSObject *obj, jsval id, uintN flags,
+ JSObject **objp)
+{
+ JSString *idstr, *valstr;
+ const char *name, *value;
+
+ if (flags & JSRESOLVE_ASSIGNING)
+ return JS_TRUE;
+
+ idstr = JS_ValueToString(cx, id);
+ if (!idstr)
+ return JS_FALSE;
+ name = JS_GetStringBytes(idstr);
+ value = getenv(name);
+ if (value) {
+ valstr = JS_NewStringCopyZ(cx, value);
+ if (!valstr)
+ return JS_FALSE;
+ if (!JS_DefineProperty(cx, obj, name, STRING_TO_JSVAL(valstr),
+ NULL, NULL, JSPROP_ENUMERATE)) {
+ return JS_FALSE;
+ }
+ *objp = obj;
+ }
+ return JS_TRUE;
+}
+
+static JSClass env_class = {
+ "environment", JSCLASS_HAS_PRIVATE | JSCLASS_NEW_RESOLVE,
+ JS_PropertyStub, JS_PropertyStub,
+ JS_PropertyStub, env_setProperty,
+ env_enumerate, (JSResolveOp) env_resolve,
+ JS_ConvertStub, JS_FinalizeStub,
+ JSCLASS_NO_OPTIONAL_MEMBERS
+};
View
@@ -1,18 +0,0 @@
-
-// base objects extensions
-if (!Object.extend)
- Object.extend = function (d, s) { for (var p in s) d[p] = s[p]; return d }
-
-if (!Object.copy)
- Object.copy = function (src) { var dst = {}; for (var k in src) dst[k] = src[k]; return dst }
-
-if (!Math.longRandom)
- Math.longRandom = function () { return (new Date()).getTime().toString() + Math.round(Math.random() * 1E+17) }
-
-if (!Array.copy)
- Array.copy = function (src) { var dst = []; for (var i = 0, len = src.length; i < len; i++) dst[i] = src[i]; return dst }
-
-if (!Function.prototype.bind)
- Function.prototype.bind = function (inv, args) { var me = this; return function () { me.apply(inv, args || arguments) } }
-
-log("lib.js loaded")
View
@@ -1,35 +1,56 @@
-if (self.Nginx)
-(function(){
+// Nginx wrapers
+if (!self.Nginx)
+ throw 'global.Nginx is undefined'
+
+;(function(){
var N = Nginx
function flat (args) { return Array.prototype.slice.apply(args).join(', ') }
- N.log = function () { this.logError(this.NGX_LOG_WARN, flat(arguments)) } // NGX_LOG_DEBUG
- N.info = function () { this.logError(this.NGX_LOG_INFO, flat(arguments)) }
- N.warn = function () { this.logError(this.NGX_LOG_WARN, flat(arguments)) }
- N.error = function () { this.logError(this.NGX_LOG_ERR, flat(arguments)) }
- log = function () { N.log.apply(N, arguments) }
-})()
-
-load('lib.js')
+ N.log = function () { this.logError(this.LOG_WARN, flat(arguments)) } // LOG_DEBUG
+ N.info = function () { this.logError(this.LOG_INFO, flat(arguments)) }
+ N.warn = function () { this.logError(this.LOG_WARN, flat(arguments)) }
+ N.error = function () { this.logError(this.LOG_ERR, flat(arguments)) }
+ self.log = function () { N.log.apply(N, arguments) }
+})();
-// try
-// {
-// Nginx.warn('warn')
-// // Nginx.logError(null, 'error')
-// Nginx.warn('warn')
-// }
-// catch (ex) { Nginx.error(ex) }
+// basic library loading
+;(function(){
-function processRequest (r)
+var JSLIB = environment.JSLIB, lib = self.lib = JSLIB ? String(JSLIB).split(':') : []
+lib.unshift(__FILE__.replace(/\/[^\/]+$/, ''))
+lib.required = {}
+self.require = function (fname)
{
- // log(r.uri, r.method, r.remoteAddr)
- r.sendHttpHeader('text/html; charset=utf-8')
- r.printString("Привет, Петя!" + r.uri + r.method + r.remoteAddr)
+ // log('require ' + fname)
+ if (lib.required[fname])
+ return lib.required[fname]
- return Nginx.NGX_HTTP_OK
+ if (fname[0] === '/')
+ {
+ load(lib.required[fname] = fname)
+ return fname
+ }
+ else
+ {
+ for (var i = 0; i < lib.length; i++)
+ {
+ var path = lib[i] + '/' + fname
+ log(path)
+ if (new File(path).exists)
+ {
+ load(lib.required[fname] = path)
+ // log(fname + ' is loaded by path ' + path)
+ return path
+ }
+ }
+ throw 'Can`t find "' + fname + '" in [' + lib.join(', ') + ']'
+ }
}
+})();
+
+require('proto.js')
-log("Nginx.js loaded")
+// log('Nginx.js loaded')
View
@@ -0,0 +1,18 @@
+
+// base prototypes and objects extensions
+if (!Object.extend)
+ Object.extend = function (d, s) { if (d) for (var k in s) d[k] = s[k]; return d }
+
+if (!Object.copy)
+ Object.copy = function (s) { var d = {}; for (var k in s) d[k] = s[k]; return d }
+
+if (!Math.longRandom)
+ Math.longRandom = function () { return (new Date()).getTime().toString() + Math.round(Math.random() * 1E+17) }
+
+if (!Array.copy)
+ Array.copy = function (s) { var d = []; for (var i = 0, l = s.length; i < l; i++) d[i] = s[i]; return d }
+
+if (!Function.prototype.bind)
+ Function.prototype.bind = function (inv, args) { var me = this; return function () { me.apply(inv, args || arguments) } }
+
+// log("proto.js is loaded by path " + __FILE__)
View
@@ -0,0 +1,15 @@
+var Handler =
+{
+ processRequest: function (r)
+ {
+ // log(r.uri, r.method, r.remoteAddr)
+ r.sendHttpHeader('text/html; charset=utf-8')
+ r.printString('Привет, Девелопер!\nТы вызвал страницу ' + r.uri + ', методом ' + r.method + ', с IP ' + r.remoteAddr + '\n')
+
+ function callback () { log('/ is loaded') }
+
+ r.request('/', callback)
+
+ return Nginx.HTTP_OK
+ }
+}
View
@@ -11,7 +11,7 @@ http
include mime.types;
default_type application/octet-stream;
- js_require 'nginx.js';
+ js_require Handler.js;
# js_set Nginx.someVar 123;
access_log off;
@@ -32,7 +32,7 @@ http
location /test
{
- js 'processRequest';
+ js 'Handler.processRequest';
}
}
Oops, something went wrong.

0 comments on commit 522c356

Please sign in to comment.