我在stream3.c
中的xbuf_xcat
下添加了printf
语句,如下:
xbuf_xcat(reply, "%x\r\n%s\r\n", len, readbuf);
char *client_arg_time=0;
get_arg("time=", & client_arg_time, argc, argv);
printf("%s\n", client_arg_time);
预计会打印 http 参数中的时间。
但它会打印 (null)
,除了第一个。
我的代码有什么问题?
我使用的是 ubuntu 12.04,G-WAN 4.3.14。
编辑======================
http 与 uri ?time=1223456,因此 get_arg("time=",...) 函数获取“1223456”并打印到屏幕。可以,但是只打印一次。当脚本被wake_up()唤醒时,它应该打印另一个“1223456”,但它打印“(null)”。如果你检查gwan的stream3.c示例,你可以找到“xbuf_xcat(reply, "%x\r\n%s\r\n", len, readbuf);”行如下靠近底部。 (引用自 gwan 示例,stream3.c):
#include "gwan.h"
#include <errno.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#define NBR_CHUNKS "10"
// ----------------------------------------------------------------------------
// popen replacement (GLIBC's popen() does not want to work without buffers)
// ----------------------------------------------------------------------------
int run_cmd(char *cmd_to_run)
{
const u32 cmd_len = strlen(cmd_to_run) + 1;
char cmd[cmd_len + 8];
snprintf(cmd, sizeof(cmd), "%s 2>&1", cmd_to_run);
// create a one-way communication channel (a pipe)
// (bytes written on fd[1] can be read from fd[0])
int fd[2]; if(pipe(fd)) return 0;
const int f = fd[0];
const pid_t pid = fork(); // fork is needed to hook the process stdout I/O
switch(pid)
{
case -1: // error
close(fd[0]);
close(fd[1]);
return 0;
case 0: // new child process, make child's stdout use our fd
dup2(fd[1], 1); // close(fd2) && fd2 = fd
close(fd[0]);
close(fd[1]);
// replace the current process image with a new process image
char c1[] = "/bin/sh", c2[] = "sh", c3[] = "-c";
char *args[] = { c2, c3, cmd, 0 };
execvp(c1, args);
exit(127);
}
// this is the parent process
close(fd[1]);
return f;
}
// ----------------------------------------------------------------------------
// our (minimalist) per-request context
// ----------------------------------------------------------------------------
typedef struct { int f; } data_t;
int main(int argc, char *argv[])
{
char readbuf[1024] = {0};
// get the server 'reply' buffer where to write our answer to the client
xbuf_t *reply = get_reply(argv);
// -------------------------------------------------------------------------
// step 1: setup a per-request context
// -------------------------------------------------------------------------
data_t **data = (void*)get_env(argv, US_REQUEST_DATA);
if(!*data) // we did not setup our per-request structure yet
{
// create a per-request memory pool
if(!gc_init(argv, 4070)) // we can call gc_alloc() to consume 4070 bytes
return 503; // could not allocate memory!
*data = gc_malloc(argv, sizeof(data_t)); // allocate our context
if(!*data) return 503; // not possible here, but better safe than sorry
// ----------------------------------------------------------------------
// step 2: run an asynchrone (or incremental) job
// ----------------------------------------------------------------------
// run the "ping -c 10 127.0.0.1" command
(*data)->f = run_cmd("ping -c " NBR_CHUNKS " 127.0.0.1");
if((*data)->f == 0) // error
{
int ret = strerror_r(errno, readbuf, sizeof(readbuf));
xbuf_cat(reply, ret ? "unknown error" : readbuf);
return 200;
}
// ----------------------------------------------------------------------
// tell G-WAN when to run this script again (for the same request)
// ----------------------------------------------------------------------
wake_up(argv, (*data)->f, WK_FD); // when fd buffer has data
// ----------------------------------------------------------------------
// send chunked encoding HTTP header and HTTP status code
// ----------------------------------------------------------------------
char head[] = "HTTP/1.1 200 OK\r\n"
"Connection: close\r\n"
"Content-type: text/html; charset=utf-8\r\n"
"Transfer-Encoding: chunked\r\n\r\n"
"5\r\n<pre>\r\n";
xbuf_ncat(reply, head, sizeof(head) - 1);
}
// -------------------------------------------------------------------------
// step 3: repeatedly read (and send to client) ping's incremental reply
// -------------------------------------------------------------------------
// fetch the command output and store it in the 'reply' buffer
const int len = read((*data)->f, readbuf, sizeof(readbuf));
if(len <= 0) // done
{
close((*data)->f);
// note that malloc() would have to free the 'data' context here
// (gc_malloc() is automatically freed, along with its memory pool
// so there's no need for an explicit free)
// end the on-going chunked encoding
char end[] = "6\r\n</pre>\r\n0\r\n\r\n";
xbuf_ncat(reply, end, sizeof(end) - 1);
wake_up(argv, 0, WK_FD); // 0:no more wake-up please
return RC_NOHEADERS; // RC_NOHEADERS: do not generate HTTP headers
}
// -------------------------------------------------------------------------
// format reply to use chunked encoding
// -------------------------------------------------------------------------
// anatomy of a chunked response:
//
// HTTP/1.1 200 OK [CRLF] <-+
// Content-Type: text/html [CRLF] | HTTP headers
// Transfer-Encoding: chunked [CRLF] <-+
// [CRLF]
// 1a; optional-stuff-here [CRLF] // hexadecimal length
// abcdefghijklmnopqrstuvwxyz [CRLF] // data (ASCII/binary)
// 10 [CRLF] // hexadecimal length
// 1234567890abcdef [CRLF] // data (ASCII/binary)
// 0 [CRLF] // 0: end of chunks
// optional-footer: some-value [CRLF] // can be HTTP headers
// optional-another-footer: another-value [CRLF] // can be HTTP headers
// [CRLF]
xbuf_xcat(reply, "%x\r\n%s\r\n", len, readbuf);
// HERE! added code.
char *client_arg_time=0;
get_arg("time=", & client_arg_time, argc, argv);
printf("%s\n", client_arg_time);
// End of my added code.
// -------------------------------------------------------------------------
// return code
// -------------------------------------------------------------------------
// RC_NOHEADERS: do not generate HTTP headers
// RC_STREAMING: call me again after send() is done
return RC_NOHEADERS + RC_STREAMING;
}
// ============================================================================
// End of Source Code
// ============================================================================
最佳答案
我不确定wake_up是否会使用所有相同的配置运行请求。
我会将所需的所有内容保存在 data_t 结构(gc_alloc)上,因为我们知道它是由wake_up 调用保存和使用的内存区域。 因此,首先扩展 data_t 结构以容纳您的信息。在第 2 步之前进行设置并在您当前使用它的地方阅读它。
typedef struct {int f;u32 time;} data_t;
...
char *client_arg_time=0;
get_arg("time=", &client_arg_time, argc, argv);
(*data)->time = atol(client_arg_time);
...
// HERE! added code.
printf("%u\n", (*data)->time);
// End of my added code.
关于c - get_arg() 不能与wake_up() 一起使用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24838369/