My favorites | Sign in
Project Home Downloads Wiki Issues Source
Checkout   Browse   Changes    
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/**
* v8cgi - (fast)cgi binary
*/

#include <v8.h>
#include <v8-debug.h>
#include <stdlib.h>
#include <string.h>
#include "app.h"
#include "macros.h"
#include "path.h"

#ifdef FASTCGI
# include <fcgi_stdio.h>
# include <signal.h>
#endif

#ifdef windows
# include <fcntl.h> /* _O_BINARY */
unsigned int _CRT_fmode = _O_BINARY;
#endif

/**
* Format for command line arguments
*
* as you can see if you wish to pass any arguments to v8, you MUST
* put a -- surrounded by whitespace after all the v8 arguments
*
* any arguments after the v8_args but before the program_file are
* used by v8cgi.
*/
static const char * const v8cgi_usage = "v8cgi [v8_args --] [-v] [-h] [-w] [-c path] [-d port] program_file [argument ...]";

class v8cgi_CGI : public v8cgi_App {
public:
/**
* Initialize from command line
*/
void init(int argc, char ** argv) {
v8cgi_App::init();

this->argv0 = (argc > 0 ? path_normalize(argv[0]) : std::string(""));

if (argc == 1) {
/* no command-line arguments, try looking for CGI env vars */
this->fromEnvVars();
}

try {
this->process_args(argc, argv);
} catch (std::string e) {
fwrite((void *) e.c_str(), sizeof(char), e.length(), stderr);
fwrite((void *) "\n", sizeof(char), 1, stderr);
this->exit_code = 1;
}
}

void fromEnvVars() {
char * env = getenv("PATH_TRANSLATED");
if (!env) { env = getenv("SCRIPT_FILENAME"); }
if (env) { this->mainfile = std::string(env); }
}

private:
std::string argv0;

const char * instanceType() {
return "cli";
}

const char * executableName() {
return this->argv0.c_str();
}

/**
* Process command line arguments.
*/
void process_args(int argc, char ** argv) {
std::string err = "Invalid command line usage.\n";
err += "Correct usage: ";
err += v8cgi_usage; /* see the v8cgi_usage definition for the format */

int index = 0;
bool wait_for_debugger = false;
int debugger_port = 0;

/* see if we have v8 options */
bool have_v8args = false;
for (; index < argc; ++index) {
/* FIXME: if there is a standalone "--" after the name of the script
then this breaks. I can't figure out a solution to this, so
for now we don't support any standalone "--" after the script name.
One solution (and the way it currently works) is to require "--"
before all other args even if no v8 args are used, but that seems
I don't like this, but it is where we are right now. */
if (std::string(argv[index]) == "--") {
/* treat all previous arguments as v8 arguments */
int v8argc = index;
v8::V8::SetFlagsFromCommandLine(&v8argc, argv, true);
index++; /* skip over the "--" */
have_v8args = true;
break;
}
}

/* if there were no v8 args, then reset index to the first argument */
if (!have_v8args) { index = 1; }

/* scan for v8cgi-specific arguments now */
while (index < argc) {
std::string optname(argv[index]);
if (optname[0] != '-') { break; } /* not starting with "-" => mainfile */
if (optname.length() != 2) { throw err; } /* one-character options only */
index++; /* skip the option name */

switch (optname[1]) {
case 'c':
if (index >= argc) { throw err; } /* missing option value */
this->cfgfile = argv[index];
#ifdef VERBOSE
printf("cfgfile: %s\n", argv[index]);
#endif
index++; /* skip the option value */
break;

case 'w':
wait_for_debugger = true;
break;

case 'd':
if (index >= argc) { throw err; } /* missing option value */
debugger_port = atoi(argv[index]);
#ifdef VERBOSE
printf("port: %i\n", debugger_port);
#endif
index++; /* skip the option value */
break;

case 'h':
printf(v8cgi_usage);
printf("\n");
break;

case 'v':
printf("v8cgi version %s", STRING(VERSION));
printf("\n");
break;


default:
throw err;
break;
}

}

if (debugger_port) { /* launch debugging agent */
v8::Debug::EnableAgent("v8cgi", debugger_port, wait_for_debugger);
}

if (index < argc) {
/* argv[index] is the program file */
this->mainfile = argv[index];
/* expand mainfile to absolute path */
if (!path_isabsolute(this->mainfile)) {
std::string tmp = path_getcwd();
tmp += "/";
tmp += this->mainfile;
this->mainfile = path_normalize(this->mainfile);
}
index++; /* skip over the program_file */
}

/* all the rest of the arguments are arguments to the program_file */
for (; index < argc; ++index) {
#ifdef VERBOSE
printf("program_arg: %s\n", argv[index]);
#endif
this->mainfile_args.push_back(std::string(argv[index]));
}
}
};

#ifdef FASTCGI
/**
* This is true only after we receive a signal to terminate
*/
void handle_sigterm(int param) {
FCGI_SetExitStatus(0);
exit(0);
}

void handle_sigusr1(int param) {
FCGI_SetExitStatus(0);
exit(0);
}

void handle_sigpipe(int param) {
FCGI_SetExitStatus(0);
exit(0);
}
#endif

extern char ** environ;

int main(int argc, char ** argv) {
v8cgi_CGI cgi;
cgi.init(argc, argv);
if (cgi.exit_code) { exit(cgi.exit_code); }

#ifdef FASTCGI
signal(SIGTERM, handle_sigterm);
# ifdef SIGPIPE
signal(SIGPIPE, handle_sigpipe);
# endif
# ifdef SIGUSR1
signal(SIGUSR1, handle_sigusr1);
# endif
/**
* FastCGI main loop
*/
while (FCGI_Accept() >= 0) {
cgi.fromEnvVars();
#endif

try {
cgi.execute(environ);
} catch (std::string e) {
if (cgi.show_errors) {
fwrite((void *) e.c_str(), sizeof(char), e.length(), stdout);
} else {
fwrite((void *) e.c_str(), sizeof(char), e.length(), stderr);
}
}

#ifdef FASTCGI
FCGI_SetExitStatus(cgi.exit_code);
}
#endif

return cgi.exit_code;
}

Change log

r1004 by ondrej.zara on Yesterday (19 hours ago)   Diff
 issue 109  - proper exit code
Go to: 

Older revisions

r960 by ondrej.zara on Dec 12, 2011   Diff
better error reporting
r959 by ondrej.zara on Dec 12, 2011   Diff
refactored reader/writer/error/flush +
error reporting
r911 by ondrej.zara on Apr 29, 2011   Diff
 issue 91 
All revisions of this file

File info

Size: 5726 bytes, 242 lines
Powered by Google Project Hosting