c-get_arg()无法与wake_up()一起使用

  • 本文关键字:up 一起 wake arg c-get g-wan
  • 更新时间 :
  • 英文 :


我在stream3.c中的xbuf_xcat下添加了一个printf语句,如下所示:

xbuf_xcat(reply, "%xrn%srn", len, readbuf);
char *client_arg_time=0;
get_arg("time=", & client_arg_time, argc, argv);
printf("%sn", client_arg_time);

它应该从http参数打印时间
但它打印(null),除了第一个。

我的代码出了什么问题
我使用的是ubuntu 12.04,G-WAN 4.3.14。
编辑=============================
带uri的http?time=123456,所以get_arg("time=",…(函数得到"1223456"并打印到屏幕。可以,但只打印一次。当脚本被wake_up((唤醒时,它应该打印另一个"1223456",但它打印的是"(null("。如果您查看gwan中的示例stream3.c,您可以在底部附近找到"xbuf_xcat(回复,"%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 OKrn"
                        "Connection: closern"
                        "Content-type: text/html; charset=utf-8rn"
                        "Transfer-Encoding: chunkedrnrn"
                        "5rn<pre>rn";
          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[] = "6rn</pre>rn0rnrn";
          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, "%xrn%srn", len, readbuf);
// HERE! added code.
       char *client_arg_time=0;
       get_arg("time=", & client_arg_time, argc, argv);
       printf("%sn", 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使用相同的配置运行请求,我不会感到惊讶。

我会把我需要的所有东西都保存在gc_alloc的data_t结构上,因为我们知道这是一个内存区域,由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("%un", (*data)->time);
// End of my added code.    

相关内容

  • 没有找到相关文章

最新更新