提交 3ed2cbca authored 作者: gled's avatar gled

quick answer add

上级 601e8fe7
...@@ -50,7 +50,97 @@ SWITCH_MODULE_DEFINITION(mod_fgi, mod_fgi_load, mod_fgi_shutdown, NULL); ...@@ -50,7 +50,97 @@ SWITCH_MODULE_DEFINITION(mod_fgi, mod_fgi_load, mod_fgi_shutdown, NULL);
#define MAX(x,y) ((x) > (y) ? (x) : (y)) #define MAX(x,y) ((x) > (y) ? (x) : (y))
#define MIN(x,y) ((x) < (y) ? (x) : (y)) #define MIN(x,y) ((x) < (y) ? (x) : (y))
static void parse_fgi_args(char *buf,int *argc,char *argv[_MAX_CMD_ARGS]); static void parse_fgi_args(char *buf,int *argc,char *argv[_MAX_CMD_ARGS]) {
char *cur;
int x = 0;
int quote = 0;
int escaped = 0;
int space=1;
cur = buf;
while (*buf) {
switch (*buf) {
case '"':
if (escaped)
goto ok;
else
quote = !quote;
if (quote && space) {
argv[x++] = cur;
space=0;
}
escaped = 0;
break;
case ' ':
case '\t':
if (!quote && !escaped) {
space = 1;
*(cur++) = '\0';
} else
goto ok;
break;
case '\\':
if (escaped)
goto ok;
else
escaped = 1;
break;
default:
ok:
if (space) {
argv[x++] = cur;
space=0;
}
*(cur++) = *buf;
escaped=0;
}
buf++;
}
*(cur++) = '\0';
argv[x]= NULL;
*argc = x;
return;
}
static switch_status_t find_and_exec_command(char *cmd, int fd_reply,switch_core_session_t *session) {
char *argv[1024];
int argc,x,y=0;
char *buf=NULL;
parse_fgi_args(cmd,&argc,argv);
/*
TODO
Here we can do things :)
- First find cmd, then call cmd with args and return result on fd_reply formatted... easy no ?
*/
for (x=0; x < argc; x++) {
if ( !strcasecmp(argv[x], "ANSWER") ) {
switch_channel_t *channel = switch_core_session_get_channel(session);
y = switch_channel_answer(channel);
buf = malloc(20 * sizeof(char));
sprintf(buf,"200 result=%d\n\n",y);
write(fd_reply,buf,strlen(buf));
free(buf);
}
else if (!strcasecmp(argv[x],"HANGUP")) {
}
else if (!strcasecmp(argv[x],"EXEC")) {
}
else if (!strcasecmp(argv[x],"STREAM") && !strcasecmp(argv[x+1],"FILE")) {
}
else if (!strcasecmp(argv[x],"SET") && !strcasecmp(argv[x+1],"CALLERID")) {
}
else if (!strcasecmp(argv[x],"GET") && !strcasecmp(argv[x+1],"DATA")) {
}
}
return SWITCH_STATUS_SUCCESS;
}
pid_t fgi_open_process( const char *cmd, int *in, int *out,int *thread_running) { pid_t fgi_open_process( const char *cmd, int *in, int *out,int *thread_running) {
int p_in[2], p_out[2]; int p_in[2], p_out[2];
...@@ -100,22 +190,14 @@ pid_t fgi_open_process( const char *cmd, int *in, int *out,int *thread_running) ...@@ -100,22 +190,14 @@ pid_t fgi_open_process( const char *cmd, int *in, int *out,int *thread_running)
return pid; return pid;
} }
static switch_status_t find_and_exec_command(char *cmd, int fd_reply) { static switch_status_t exec_fgi_run(char *argv,switch_core_session_t *session) {
char *argv[1024];
int argc = 0;
parse_fgi_args(cmd,&argc,argv);
/* */
return SWITCH_STATUS_SUCCESS;
}
static switch_status_t exec_fgi_run(char *argv) {
int in,out; int in,out;
pid_t pid; pid_t pid;
int res=0; int res=0;
int nfds=0; int nfds=0;
fd_set rd;
int thread_running; int thread_running;
fd_set rd;
char cmdbuf[_MAX_CMD_ARGS*128]; char cmdbuf[_MAX_CMD_ARGS*128];
pid = fgi_open_process(argv,&in,&out,&thread_running); pid = fgi_open_process(argv,&in,&out,&thread_running);
...@@ -126,7 +208,6 @@ static switch_status_t exec_fgi_run(char *argv) { ...@@ -126,7 +208,6 @@ static switch_status_t exec_fgi_run(char *argv) {
FD_ZERO(&rd); FD_ZERO(&rd);
FD_SET(in, &rd); FD_SET(in, &rd);
nfds = MAX(nfds,out); nfds = MAX(nfds,out);
res = select(nfds + 1, &rd, NULL, NULL, NULL); res = select(nfds + 1, &rd, NULL, NULL, NULL);
if (res == -1 ) if (res == -1 )
break; break;
...@@ -135,11 +216,11 @@ static switch_status_t exec_fgi_run(char *argv) { ...@@ -135,11 +216,11 @@ static switch_status_t exec_fgi_run(char *argv) {
if (res<0) if (res<0)
goto end; goto end;
else { else {
if ( find_and_exec_command((char *) &cmdbuf,in) != SWITCH_STATUS_SUCCESS ) if ( find_and_exec_command((char *) &cmdbuf,in,session) != SWITCH_STATUS_SUCCESS )
goto end; goto end;
} }
} }
} }
end: end:
kill(pid,SIGHUP); kill(pid,SIGHUP);
close(in); close(in);
...@@ -162,7 +243,7 @@ SWITCH_STANDARD_APP(launch_fgi_thread) { ...@@ -162,7 +243,7 @@ SWITCH_STANDARD_APP(launch_fgi_thread) {
switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "INVALID ARGS!\n"); switch_log_printf(SWITCH_CHANNEL_SESSION_LOG(session), SWITCH_LOG_ERROR, "INVALID ARGS!\n");
return; return;
} }
exec_fgi_run(mydata); exec_fgi_run(mydata,session);
/* Free up things ? */ /* Free up things ? */
} }
...@@ -186,56 +267,3 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_fgi_shutdown) { ...@@ -186,56 +267,3 @@ SWITCH_MODULE_SHUTDOWN_FUNCTION(mod_fgi_shutdown) {
return SWITCH_STATUS_SUCCESS; return SWITCH_STATUS_SUCCESS;
} }
static void parse_fgi_args(char *buf,int *argc,char *argv[_MAX_CMD_ARGS]) {
char *cur;
int x = 0;
int quote = 0;
int escaped = 0;
int space=1;
cur = buf;
while (*buf) {
switch (*buf) {
case '"':
if (escaped)
goto ok;
else
quote = !quote;
if (quote && space) {
argv[x++] = cur;
space=0;
}
escaped = 0;
break;
case ' ':
case '\t':
if (!quote && !escaped) {
space = 1;
*(cur++) = '\0';
} else
goto ok;
break;
case '\\':
if (escaped)
goto ok;
else
escaped = 1;
break;
default:
ok:
if (space) {
argv[x++] = cur;
space=0;
}
*(cur++) = *buf;
escaped=0;
}
buf++;
}
*(cur++) = '\0';
argv[x]= NULL;
*argc = x;
return;
}
Markdown 格式
0% 或
您添加了 0 人 到此讨论。请谨慎行事。
请先完成此评论的编辑!
请 注册 或者 后发表评论