Guest User

Untitled

a guest
Jun 22nd, 2018
130
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 4.54 KB | None | 0 0
  1. #include <v8.h>
  2. #include <string>
  3. #include <stdlib.h>
  4.  
  5. #ifdef FASTCGI
  6. # include <fcgi_stdio.h>
  7. #endif
  8.  
  9. #include "js_macros.h"
  10. #include "js_common.h"
  11. #include "js_app.h"
  12. #include "js_system.h"
  13. #include "js_path.h"
  14.  
  15. #ifndef HAVE_SLEEP
  16. # include <windows.h>
  17. # define sleep(num) { Sleep(num * 1000); }
  18. #endif
  19.  
  20. namespace {
  21.  
  22. /**
  23. * Read characters from stdin
  24. * @param {int} count How many; 0 == all
  25. * @param {bool} [arr=false] Return as array of bytes?
  26. */
  27. JS_METHOD(_stdin) {
  28. v8cgi_App * app = APP_PTR;
  29.  
  30. size_t count = 0;
  31. if (args.Length() && args[0]->IsNumber()) {
  32. count = args[0]->IntegerValue();
  33. }
  34.  
  35. std::string data;
  36. size_t size = 0;
  37.  
  38. if (count == 0) { /* all */
  39. size_t tmp;
  40. char * buf = new char[1024];
  41. do {
  42. tmp = app->reader(buf, sizeof(buf));
  43. size += tmp;
  44. data.insert(data.length(), buf, tmp);
  45. } while (tmp == sizeof(buf));
  46. delete[] buf;
  47. } else {
  48. char * tmp = new char[count];
  49. size = app->reader(tmp, count);
  50. data.insert(0, tmp, size);
  51. delete[] tmp;
  52. }
  53.  
  54. if (args.Length() > 1 && args[1]->IsTrue()) {
  55. return JS_CHARARRAY((char *) data.data(), size);
  56. } else {
  57. return JS_STR(data.data(), size);
  58. }
  59. }
  60.  
  61. /**
  62. * Dump data to stdout
  63. * @param {string|int[]} String or array of bytes
  64. */
  65. JS_METHOD(_stdout) {
  66. v8cgi_App * app = APP_PTR;
  67. if (args[0]->IsArray()) {
  68. v8::Handle<v8::Array> arr = v8::Handle<v8::Array>::Cast(args[0]);
  69. uint32_t len = arr->Length();
  70. std::string data;
  71. for (unsigned int i=0;i<len;i++) {
  72. data += (char) arr->Get(JS_INT(i))->Int32Value();
  73. }
  74. app->writer((char *) data.data(), len);
  75. } else {
  76. v8::String::Utf8Value str(args[0]);
  77. app->writer(*str, str.length());
  78. }
  79. return v8::Undefined();
  80. }
  81.  
  82. JS_METHOD(_stderr) {
  83. v8cgi_App * app = APP_PTR;
  84. v8::String::Utf8Value str(args[0]);
  85. v8::String::Utf8Value f(args[1]);
  86. int line = args[2]->Int32Value();
  87. app->error(*str, *f, line);
  88. return v8::Undefined();
  89. }
  90.  
  91. JS_METHOD(_system) {
  92. if (args.Length() != 1) {
  93. return JS_EXCEPTION("Wrong argument count. Use system.system(\"command\")");
  94. }
  95.  
  96. v8::String::Utf8Value cmd(args[0]);
  97. int result = system(*cmd);
  98. return JS_INT(result);
  99. }
  100.  
  101. JS_METHOD(_exec) {
  102. if (args.Length() != 1) {
  103. return JS_EXCEPTION("Wrong argument count. Use system.exec(\"command\")");
  104. }
  105.  
  106. std::string data;
  107. FILE *stream;
  108. int MAX_BUFFER = 256;
  109. char buffer[MAX_BUFFER];
  110.  
  111. v8::String::Utf8Value cmd(args[0]);
  112. stream = popen(*cmd, "r");
  113. while ( fgets(buffer, MAX_BUFFER, stream) != NULL ) {
  114. data.append(buffer);
  115. }
  116. pclose(stream);
  117. return JS_STR(data.c_str());
  118. }
  119.  
  120. JS_METHOD(_getcwd) {
  121. return JS_STR(path_getcwd().c_str());
  122. }
  123.  
  124. /**
  125. * Sleep for a given number of seconds
  126. */
  127. JS_METHOD(_sleep) {
  128. int num = args[0]->Int32Value();
  129. sleep(num);
  130. return v8::Undefined();
  131. }
  132.  
  133. /**
  134. * FIXME: How to do this on win32?
  135. JS_METHOD(_usleep) {
  136. v8::HandleScope handle_scope;
  137. int num = args[0]->Int32Value();
  138. usleep(num);
  139. return v8::Undefined();
  140. }
  141. */
  142.  
  143. }
  144.  
  145. extern char ** environ;
  146.  
  147. void setup_system(v8::Handle<v8::Object> global, char ** envp) {
  148. v8::HandleScope handle_scope;
  149. v8::Handle<v8::ObjectTemplate> systemt = v8::ObjectTemplate::New();
  150. v8::Handle<v8::Object> system = systemt->NewInstance();
  151.  
  152. v8::Handle<v8::Object> env = v8::Object::New();
  153.  
  154. global->Set(JS_STR("system"), system);
  155. system->Set(JS_STR("stdin"), v8::FunctionTemplate::New(_stdin)->GetFunction());
  156. system->Set(JS_STR("stdout"), v8::FunctionTemplate::New(_stdout)->GetFunction());
  157. system->Set(JS_STR("stderr"), v8::FunctionTemplate::New(_stderr)->GetFunction());
  158. system->Set(JS_STR("system"), v8::FunctionTemplate::New(_system)->GetFunction());
  159. system->Set(JS_STR("exec"), v8::FunctionTemplate::New(_exec)->GetFunction());
  160. system->Set(JS_STR("getcwd"), v8::FunctionTemplate::New(_getcwd)->GetFunction());
  161. system->Set(JS_STR("sleep"), v8::FunctionTemplate::New(_sleep)->GetFunction());
  162. /* system->Set(JS_STR("usleep"), v8::FunctionTemplate::New(_usleep)->GetFunction()); */
  163. system->Set(JS_STR("env"), env);
  164.  
  165. std::string name, value;
  166. bool done;
  167. int i,j;
  168. char ch;
  169.  
  170. /* extract environment variables and create JS object */
  171. char ** e = envp;
  172. if (e == NULL) { e = environ; }
  173. if (e == NULL) { return; }
  174.  
  175. for (i = 0; e[i] != NULL; i++) {
  176. done = false;
  177. name = "";
  178. value = "";
  179. for (j = 0; e[i][j] != '\0'; j++) {
  180. ch = e[i][j];
  181. if (!done) {
  182. if (ch == '=') {
  183. done = true;
  184. } else {
  185. name += ch;
  186. }
  187. } else {
  188. value += ch;
  189. }
  190. }
  191. env->Set(JS_STR(name.c_str()), JS_STR(value.c_str()));
  192. }
  193. }
Add Comment
Please, Sign In to add comment