@@ -277,39 +277,6 @@ namespace hi
277
277
JAVA->env ->DeleteLocalRef (content);
278
278
}
279
279
280
- static bool javascript_engine_init_handler (ngx_http_hi_loc_conf_t *conf)
281
- {
282
- bool result = false ;
283
- if (conf->javascript_engine_index == NGX_CONF_UNSET)
284
- {
285
- std::pair<jobject, jobject> engine{NULL , NULL };
286
- jstring engine_name = JAVA->env ->NewStringUTF ((char *)conf->javascript_lang .data );
287
- engine.first = (jobject)JAVA->env ->CallObjectMethod (JAVA->script_manager_instance , JAVA->script_manager_get_engine_by_name , engine_name);
288
- JAVA->env ->ReleaseStringUTFChars (engine_name, 0 );
289
- JAVA->env ->DeleteLocalRef (engine_name);
290
- if (engine.first != NULL )
291
- {
292
- if (JAVA->env ->IsInstanceOf (engine.first , JAVA->compilable ) == JNI_TRUE)
293
- {
294
- engine.second = (jobject)engine.first ;
295
- }
296
- else
297
- {
298
- engine.second = NULL ;
299
- }
300
- JAVA->engines .push_back (engine);
301
- conf->javascript_engine_index = JAVA->engines .size () - 1 ;
302
- result = true ;
303
- }
304
- }
305
- else
306
- {
307
-
308
- result = true ;
309
- }
310
- return result;
311
- }
312
-
313
280
static void ngx_http_hi_java_handler (ngx_http_request_t *r, ngx_http_hi_loc_conf_t *conf, hi::request &req, hi::response &res)
314
281
{
315
282
if (java_init_handler (conf))
@@ -364,169 +331,4 @@ namespace hi
364
331
JAVA->env ->DeleteLocalRef (response_instance);
365
332
}
366
333
}
367
-
368
- static void ngx_http_hi_javascript_handler (ngx_http_request_t *r, ngx_http_hi_loc_conf_t *conf, hi::request &req, hi::response &res)
369
- {
370
- if (java_init_handler (conf) && javascript_engine_init_handler (conf))
371
- {
372
-
373
- std::pair<jobject, jobject> &engine = JAVA->engines [conf->javascript_engine_index ];
374
-
375
- if (engine.first == NULL )
376
- {
377
- return ;
378
- }
379
-
380
- jobject request_instance, response_instance;
381
-
382
- request_instance = JAVA->env ->NewObject (JAVA->request , JAVA->request_ctor );
383
- response_instance = JAVA->env ->NewObject (JAVA->response , JAVA->response_ctor );
384
-
385
- java_input_handler (conf, req, res, request_instance, response_instance);
386
-
387
- jstring jhi_req = JAVA->env ->NewStringUTF (" hi_req" );
388
- jstring jhi_res = JAVA->env ->NewStringUTF (" hi_res" );
389
-
390
- JAVA->env ->CallVoidMethod (engine.first , JAVA->script_engine_put , jhi_req, request_instance);
391
- JAVA->env ->CallVoidMethod (engine.first , JAVA->script_engine_put , jhi_res, response_instance);
392
-
393
- if (conf->javascript_script .len > 0 )
394
- {
395
- std::string script_path = std::move (std::string ((char *)conf->javascript_script .data , conf->javascript_script .len ));
396
- auto c = script_path.find_last_of (' .' );
397
- if (c == std::string::npos || script_path.substr (c + 1 ) != (char *)conf->javascript_extension .data )
398
- {
399
- script_path.append (req.uri );
400
- }
401
-
402
- if (is_file (script_path))
403
- {
404
- std::string md5key = std::move (md5 (script_path));
405
- if (conf->need_kvdb == 1 )
406
- {
407
- if (JAVA->compiledscript_instances .find (md5key) != JAVA->compiledscript_instances .end ())
408
- {
409
- time_t now (0 );
410
- const std::pair<time_t , jobject> &compiledscript_pair = JAVA->compiledscript_instances [md5key];
411
- if (difftime (now, compiledscript_pair.first ) > conf->javascript_compiledscript_expires )
412
- {
413
- JAVA->env ->DeleteLocalRef (compiledscript_pair.second );
414
- JAVA->compiledscript_instances .erase (md5key);
415
- goto update_string_compiledscript_instance;
416
- }
417
- if (compiledscript_pair.second != NULL )
418
- {
419
- JAVA->env ->CallObjectMethod (compiledscript_pair.second , JAVA->compiledscript_eval_void );
420
- }
421
- }
422
- else
423
- {
424
- update_string_compiledscript_instance:
425
- jstring script_content = NULL ;
426
- std::pair<char *, struct stat > script_mmap;
427
- if (JAVA->script_mmap .get (script_path, script_mmap))
428
- {
429
- script_content = JAVA->env ->NewStringUTF (script_mmap.first );
430
- }
431
- if (engine.second != NULL )
432
- {
433
- jobject compiledscript_instance = (jobject)JAVA->env ->CallObjectMethod (engine.second , JAVA->compile_string , script_content);
434
- if (compiledscript_instance != NULL )
435
- {
436
- JAVA->compiledscript_instances [md5key] = std::make_pair (time (0 ), compiledscript_instance);
437
- JAVA->env ->CallObjectMethod (compiledscript_instance, JAVA->compiledscript_eval_void );
438
- }
439
- }
440
- else
441
- {
442
- JAVA->env ->CallObjectMethod (engine.first , JAVA->script_engine_eval_string , script_content);
443
- }
444
-
445
- if (script_content != NULL )
446
- {
447
- JAVA->env ->ReleaseStringUTFChars (script_content, 0 );
448
- JAVA->env ->DeleteLocalRef (script_content);
449
- }
450
- }
451
- }
452
- else
453
- {
454
- if (JAVA->compiledscript_instances .find (md5key) != JAVA->compiledscript_instances .end ())
455
- {
456
- time_t now (NULL );
457
- const std::pair<time_t , jobject> &compiledscript_pair = JAVA->compiledscript_instances [md5key];
458
- if (difftime (now, compiledscript_pair.first ) > conf->javascript_compiledscript_expires )
459
- {
460
- JAVA->env ->DeleteLocalRef (compiledscript_pair.second );
461
- JAVA->compiledscript_instances .erase (md5key);
462
- goto update_file_compiledscript_instance;
463
- }
464
- if (compiledscript_pair.second != NULL )
465
- {
466
- JAVA->env ->CallObjectMethod (compiledscript_pair.second , JAVA->compiledscript_eval_void );
467
- }
468
- }
469
- else
470
- {
471
- update_file_compiledscript_instance:
472
- jstring javascript_path = JAVA->env ->NewStringUTF (script_path.c_str ());
473
- jobject filereader_instance = (jobject)JAVA->env ->NewObject (JAVA->filereader , JAVA->filereader_ctor , javascript_path);
474
-
475
- if (engine.second != NULL )
476
- {
477
- jobject compiledscript_instance = (jobject)JAVA->env ->CallObjectMethod (engine.second , JAVA->compile_filereader , filereader_instance);
478
- if (compiledscript_instance != NULL )
479
- {
480
- JAVA->compiledscript_instances [md5key] = std::make_pair (time (0 ), compiledscript_instance);
481
- JAVA->env ->CallObjectMethod (compiledscript_instance, JAVA->compiledscript_eval_void );
482
- }
483
- }
484
- else
485
- {
486
- JAVA->env ->CallObjectMethod (engine.first , JAVA->script_engine_eval_filereader , filereader_instance);
487
- }
488
- JAVA->env ->DeleteLocalRef (filereader_instance);
489
- JAVA->env ->ReleaseStringUTFChars (javascript_path, 0 );
490
- JAVA->env ->DeleteLocalRef (javascript_path);
491
- }
492
- }
493
- }
494
- }
495
- else if (conf->javascript_content .len > 0 )
496
- {
497
-
498
- jstring script_content = JAVA->env ->NewStringUTF ((char *)conf->javascript_content .data );
499
-
500
- if (engine.second != NULL )
501
- {
502
- jobject compiledscript_instance = (jobject)JAVA->env ->CallObjectMethod (engine.second , JAVA->compile_string , script_content);
503
- if (compiledscript_instance != NULL )
504
- {
505
- JAVA->env ->CallObjectMethod (compiledscript_instance, JAVA->compiledscript_eval_void );
506
- JAVA->env ->DeleteLocalRef (compiledscript_instance);
507
- }
508
- }
509
- else
510
- {
511
-
512
- JAVA->env ->CallObjectMethod (engine.first , JAVA->script_engine_eval_string , script_content);
513
- }
514
-
515
- JAVA->env ->ReleaseStringUTFChars (script_content, 0 );
516
- JAVA->env ->DeleteLocalRef (script_content);
517
- }
518
-
519
- java_output_handler (conf, req, res, request_instance, response_instance);
520
-
521
- JAVA->env ->DeleteLocalRef (request_instance);
522
- JAVA->env ->DeleteLocalRef (response_instance);
523
-
524
- JAVA->env ->ReleaseStringUTFChars (jhi_req, 0 );
525
- JAVA->env ->DeleteLocalRef (jhi_req);
526
- JAVA->env ->ReleaseStringUTFChars (jhi_res, 0 );
527
- JAVA->env ->DeleteLocalRef (jhi_res);
528
- }
529
- }
530
334
} // namespace hi
531
-
532
-
0 commit comments