【问题标题】:GWAN : How to read cookiesGWAN : 如何读取 cookie
【发布时间】:2014-06-09 16:09:25
【问题描述】:

我正在尝试读取 cookie,但下面的脚本返回一个空字符串。

http_t *http =  (http_t*)get_env(argv, HTTP_HEADERS);
xbuf_t *read_buf  = (xbuf_t*)get_env(argv, READ_XBUF);
char *p = read_buf->ptr;
char *cookies = http->h_cookies ? p + http->h_cookies : 0;
xbuf_xcat(reply, "<HR>COOKIES [%s]<br>", cookies);

我之前设置了一个 cookie:http_header(我可以在 chrome 的控制台中看到)

那么我如何读取 cookie?

感谢您的回答。

我正在使用 GWAN 4.11.20

【问题讨论】:

    标签: cookies g-wan


    【解决方案1】:

    我为 g-wan 编写了简单的库。你可以用它来获取cookies。 示例代码:

    char *val = gw_cookie(argv, "cookie_name=", 12);
    

    链接:https://github.com/fatihky/gwanup/blob/master/gwanup.h#L102

    【讨论】:

    • 您的库看起来不错,但 GWAN 4.11.20 似乎存在问题......所以现在我正在等待下一个版本:)
    • 我也在使用 G-WAN 4.3.20 并且工作正常,但我还不能更新库。它需要在 c/c++ 脚本中使用hiredis 和'#pragma link "hiredis"' 行。
    【解决方案2】:

    尽管添加了新值,但在发布 v4.11 时未同步 gwan/includes 标头。

    因此,get_env() 在 G-WAN 脚本中使用的某些值与 G-WAN 使用的值不匹配。

    解决方案是更正gwan.h 标头中的这些值。访问 cookie 的另一种更简单的方法是使用 READ_XBUF 访问读取缓冲区(请参阅“连接处理程序”选项卡),然后使用类似于 G-WAN cookies.c 示例的代码查找 cookie。

    遇到同样问题的 G-WAN 用户 Paulo 向我们发送了以下源代码:

    int getSessionID(int argc, char *argv[]) {
        http_t *http = (http_t*)get_env(argv, HTTP_HEADERS);
        xbuf_t *read_buf = (xbuf_t*)get_env(argv, READ_XBUF);
        char *p = read_buf->ptr;
        int sessionID = 0;
        if (http) {
            sessionID = http->session_id;
    fprintf(stderr, "Get SessionID %d\n", sessionID);        
        }
    
    fprintf(stderr, "Get SessionID Cookie %d\n", http->h_cookies); 
    
        if (p && *p && http->h_cookies) {
            char *cookies = p + http->h_cookies;
    fprintf(stderr, "Get SessionID Cookie %s\n", cookies);
            // The sessionID is on the Cookie
            sessionID = atoi(cookies + 5);
        }
        if (!sessionID) {
            // The sessionID is not on the Cookie so send the server Session_ID   
            sessionID = (int)get_env(argv, SESSION_ID);
        }
        if (!sessionID) {
            // Oops! I have no session from the Server. use the IP Address and a timestamp
            sessionID = (int)get_env(argv, REMOTE_BIN_ADDR) + getms();
        }
        return sessionID;
    }
    

    这会让你开始。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-02-06
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多