问题
I'm trying to read cookies but the script below returns an empty string.
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);
I have set a cookie previously using : http_header (which I can see in chrome's console)
So how can I read cookies?
Thank you for your answer.
I'm using GWAN 4.11.20
回答1:
v4.11 was issued without synchronizing the gwan/includes
headers despite new values being added.
As a result, some of the values used by get_env()
in G-WAN scripts do not match the values used by G-WAN.
A solution would be to correct these values in the gwan.h
header. Another simpler way to access cookies is to reach the read buffer using READ_XBUF (see the "connection handler" tab) and then look for the cookies using code similar to the G-WAN cookies.c example.
Paulo, a G-WAN user having the same problem has sent us the following source code:
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;
}
This will get your started.
回答2:
I wrote simple library for g-wan. You can use it for get cookies. Example code:
char *val = gw_cookie(argv, "cookie_name=", 12);
link: https://github.com/fatihky/gwanup/blob/master/gwanup.h#L102
来源:https://stackoverflow.com/questions/24124249/gwan-how-to-read-cookies