📄 cookie.c
字号:
if (strcmp("TRUE", ptr) && strcmp("FALSE", ptr)) { /* only if the path doesn't look like a boolean option! */ co->path = strdup(ptr); break; } /* this doesn't look like a path, make one up! */ co->path = strdup("/"); fields++; /* add a field and fall down to secure */ /* FALLTHROUGH */ case 3: co->secure = strequal(ptr, "TRUE"); break; case 4: co->expires = atoi(ptr); break; case 5: co->name = strdup(ptr); break; case 6: co->value = strdup(ptr); break; } } if(6 == fields) { /* we got a cookie with blank contents, fix it */ co->value = strdup(""); } else if(7 != fields) { /* we did not find the sufficient number of fields to recognize this as a valid line, abort and go home */ free_cookiemess(co); return NULL; } } if(!c->running && /* read from a file */ c->newsession && /* clean session cookies */ !co->expires) { /* this is a session cookie since it doesn't expire! */ free_cookiemess(co); return NULL; } co->livecookie = c->running; /* now, we have parsed the incoming line, we must now check if this superceeds an already existing cookie, which it may if the previous have the same domain and path as this */ clist = c->cookies; replace_old = FALSE; while(clist) { if(strequal(clist->name, co->name)) { /* the names are identical */ if(clist->domain && co->domain) { if(strequal(clist->domain, co->domain)) /* The domains are identical */ replace_old=TRUE; } else if(!clist->domain && !co->domain) replace_old = TRUE; if(replace_old) { /* the domains were identical */ if(clist->path && co->path) { if(strequal(clist->path, co->path)) { replace_old = TRUE; } else replace_old = FALSE; } else if(!clist->path && !co->path) replace_old = TRUE; else replace_old = FALSE; } if(replace_old && !co->livecookie && clist->livecookie) { /* Both cookies matched fine, except that the already present cookie is "live", which means it was set from a header, while the new one isn't "live" and thus only read from a file. We let live cookies stay alive */ /* Free the newcomer and get out of here! */ if(co->domain) free(co->domain); if(co->path) free(co->path); if(co->name) free(co->name); if(co->value) free(co->value); free(co); return NULL; } if(replace_old) { co->next = clist->next; /* get the next-pointer first */ /* then free all the old pointers */ if(clist->name) free(clist->name); if(clist->value) free(clist->value); if(clist->domain) free(clist->domain); if(clist->path) free(clist->path); if(clist->expirestr) free(clist->expirestr); if(clist->version) free(clist->version); if(clist->maxage) free(clist->maxage); *clist = *co; /* then store all the new data */ free(co); /* free the newly alloced memory */ co = clist; /* point to the previous struct instead */ /* We have replaced a cookie, now skip the rest of the list but make sure the 'lastc' pointer is properly set */ do { lastc = clist; clist = clist->next; } while(clist); break; } } lastc = clist; clist = clist->next; } if(c->running) /* Only show this when NOT reading the cookies from a file */ infof(data, "%s cookie %s=\"%s\" for domain %s, path %s, expire %d\n", replace_old?"Replaced":"Added", co->name, co->value, co->domain, co->path, co->expires); if(!replace_old) { /* then make the last item point on this new one */ if(lastc) lastc->next = co; else c->cookies = co; } c->numcookies++; /* one more cookie in the jar */ return co;}/***************************************************************************** * * Curl_cookie_init() * * Inits a cookie struct to read data from a local file. This is always * called before any cookies are set. File may be NULL. * * If 'newsession' is TRUE, discard all "session cookies" on read from file. * ****************************************************************************/struct CookieInfo *Curl_cookie_init(struct SessionHandle *data, char *file, struct CookieInfo *inc, bool newsession){ char line[MAX_COOKIE_LINE]; struct CookieInfo *c; FILE *fp; bool fromfile=TRUE; if(NULL == inc) { /* we didn't get a struct, create one */ c = (struct CookieInfo *)malloc(sizeof(struct CookieInfo)); if(!c) return NULL; /* failed to get memory */ memset(c, 0, sizeof(struct CookieInfo)); c->filename = strdup(file?file:"none"); /* copy the name just in case */ } else { /* we got an already existing one, use that */ c = inc; } c->running = FALSE; /* this is not running, this is init */ if(file && strequal(file, "-")) { fp = stdin; fromfile=FALSE; } else fp = file?fopen(file, "r"):NULL; c->newsession = newsession; /* new session? */ if(fp) { char *lineptr; bool headerline; while(fgets(line, MAX_COOKIE_LINE, fp)) { if(checkprefix("Set-Cookie:", line)) { /* This is a cookie line, get it! */ lineptr=&line[11]; headerline=TRUE; } else { lineptr=line; headerline=FALSE; } while(*lineptr && isspace((int)*lineptr)) lineptr++; Curl_cookie_add(data, c, headerline, lineptr, NULL, NULL); } if(fromfile) fclose(fp); } c->running = TRUE; /* now, we're running */ return c;}/***************************************************************************** * * Curl_cookie_getlist() * * For a given host and path, return a linked list of cookies that the * client should send to the server if used now. The secure boolean informs * the cookie if a secure connection is achieved or not. * * It shall only return cookies that haven't expired. * ****************************************************************************/struct Cookie *Curl_cookie_getlist(struct CookieInfo *c, char *host, char *path, bool secure){ struct Cookie *newco; struct Cookie *co; time_t now = time(NULL); struct Cookie *mainco=NULL; if(!c || !c->cookies) return NULL; /* no cookie struct or no cookies in the struct */ co = c->cookies; while(co) { /* only process this cookie if it is not expired or had no expire date AND that if the cookie requires we're secure we must only continue if we are! */ if( (co->expires<=0 || (co->expires> now)) && (co->secure?secure:TRUE) ) { /* now check if the domain is correct */ if(!co->domain || (co->tailmatch && tailmatch(co->domain, host)) || (!co->tailmatch && strequal(host, co->domain)) ) { /* the right part of the host matches the domain stuff in the cookie data */ /* now check the left part of the path with the cookies path requirement */ if(!co->path || checkprefix(co->path, path) ) { /* and now, we know this is a match and we should create an entry for the return-linked-list */ newco = (struct Cookie *)malloc(sizeof(struct Cookie)); if(newco) { /* first, copy the whole source cookie: */ memcpy(newco, co, sizeof(struct Cookie)); /* then modify our next */ newco->next = mainco; /* point the main to us */ mainco = newco; } } } } co = co->next; } return mainco; /* return the new list */}/***************************************************************************** * * Curl_cookie_freelist() * * Free a list of cookies previously returned by Curl_cookie_getlist(); * ****************************************************************************/void Curl_cookie_freelist(struct Cookie *co){ struct Cookie *next; if(co) { while(co) { next = co->next; free(co); /* we only free the struct since the "members" are all just copied! */ co = next; } }}/***************************************************************************** * * Curl_cookie_cleanup() * * Free a "cookie object" previous created with cookie_init(). * ****************************************************************************/void Curl_cookie_cleanup(struct CookieInfo *c){ struct Cookie *co; struct Cookie *next; if(c) { if(c->filename) free(c->filename); co = c->cookies; while(co) { if(co->name) free(co->name); if(co->value) free(co->value); if(co->domain) free(co->domain); if(co->path) free(co->path); if(co->expirestr) free(co->expirestr); if(co->version) free(co->version); if(co->maxage) free(co->maxage); next = co->next; free(co); co = next; } free(c); /* free the base struct as well */ }}/* * Curl_cookie_output() * * Writes all internally known cookies to the specified file. Specify * "-" as file name to write to stdout. * * The function returns non-zero on write failure. */int Curl_cookie_output(struct CookieInfo *c, char *dumphere){ struct Cookie *co; FILE *out; bool use_stdout=FALSE; if((NULL == c) || (0 == c->numcookies)) /* If there are no known cookies, we don't write or even create any destination file */ return 0; if(strequal("-", dumphere)) { /* use stdout */ out = stdout; use_stdout=TRUE; } else { out = fopen(dumphere, "w"); if(!out) return 1; /* failure */ } if(c) { fputs("# Netscape HTTP Cookie File\n" "# http://www.netscape.com/newsref/std/cookie_spec.html\n" "# This file was generated by libcurl! Edit at your own risk.\n\n", out); co = c->cookies; while(co) { fprintf(out, "%s%s\t" /* domain */ "%s\t" /* tailmatch */ "%s\t" /* path */ "%s\t" /* secure */ "%u\t" /* expires */ "%s\t" /* name */ "%s\n", /* value */ /* Make sure all domains are prefixed with a dot if they allow tailmatching. This is Mozilla-style. */ (co->tailmatch && co->domain && co->domain[0] != '.')? ".":"", co->domain?co->domain:"unknown", co->tailmatch?"TRUE":"FALSE", co->path?co->path:"/", co->secure?"TRUE":"FALSE", (unsigned int)co->expires, co->name, co->value?co->value:""); co=co->next; } } if(!use_stdout) fclose(out); return 0;}#endif /* CURL_DISABLE_HTTP */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -