GWAN:如何阅读cookies

时间:2014-06-09 16:09:26

标签: cookies g-wan

我正在尝试读取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的控制台中看到)

那么如何阅读cookies?

感谢您的回答。

我正在使用GWAN 4.11.20

2 个答案:

答案 0 :(得分:1)

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

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

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

答案 1 :(得分:0)

尽管添加了新的值,但在没有同步gwan/includes标题的情况下发布了v4.11。

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

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

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

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;
}

这将让你开始。