--- davfs2-1.4.5-org/src/webdav.c 2009-11-01 18:14:07.000000000 +0100 +++ davfs2-1.4.5-cookie/src/webdav.c 2011-08-03 18:43:16.000000000 +0200 @@ -217,10 +217,8 @@ /* A user defined header that is added to all requests. */ char *custom_header; -#if NE_VERSION_MINOR > 25 /* Session cookie. */ -static char *cookie; -#endif +static char *cookie_hdr; /* Private function prototypes and inline functions */ @@ -296,12 +294,34 @@ static int ssl_verify(void *userdata, int failures, const ne_ssl_certificate *cert); -#if NE_VERSION_MINOR > 25 -static int -update_cookie(ne_request *req, void *userdata, const ne_status *status); -#endif /* NE_VERSION_MINOR > 25 */ +static void +cookie_out(ne_request *req, void *userdata, ne_buffer *header){ + if(NULL != cookie_hdr){ + ne_buffer_zappend(header, cookie_hdr); + } +} + +static int +cookie_in(ne_request *req, void *userdata, const ne_status *status){ + const char* value = ne_get_response_header(req, "Set-Cookie"); + if(value != NULL){ + if(cookie_hdr != NULL){ + ne_free(cookie_hdr); + } + char* sep = strchr(value, ';'); + char* clean; + if(sep == NULL){ + clean = ne_strdup(value); + }else{ + clean = ne_strndup(value, sep-value); + } + cookie_hdr = ne_concat("Cookie: ", clean, "\r\n", NULL); + ne_free(clean); + } + return NE_OK; +} /* Public functions */ /*==================*/ @@ -355,6 +375,11 @@ } session = ne_session_create(args->scheme, args->host, args->port); + + if(args->allow_cookie){ + ne_hook_pre_send(session, cookie_out, NULL); + ne_hook_post_send(session, cookie_in, NULL); + } ne_set_read_timeout(session, args->read_timeout); @@ -460,11 +485,6 @@ ne_hook_pre_send(session, add_header, custom_header); } -#if NE_VERSION_MINOR > 25 - if (args->allow_cookie) - ne_hook_post_send(session, update_cookie, NULL); -#endif /* NE_VERSION_MINOR > 25 */ - use_expect100 = args->expect100; has_if_match_bug = args->if_match_bug; drop_weak_etags = args->drop_weak_etags; @@ -1994,43 +2014,3 @@ return ret; } - -#if NE_VERSION_MINOR > 25 -static int -update_cookie(ne_request *req, void *userdata, const ne_status *status) -{ - if (status->klass != 2) - return NE_OK; - - const char *cookie_hdr = ne_get_response_header(req, "Set-Cookie2"); - if (!cookie_hdr) { - cookie_hdr = ne_get_response_header(req, "Set-Cookie"); - } - if (!cookie_hdr) - return NE_OK; - - if (cookie && strstr(cookie_hdr, cookie) == cookie_hdr) - return NE_OK; - - char *sep = strpbrk(cookie_hdr, "\",; \n\r\0"); - if (!sep) - return NE_OK; - if (*sep == '\"') - sep = strpbrk(sep + 1, "\""); - if (!sep) - return NE_OK; - - if (cookie) { - ne_unhook_pre_send(session, add_header, cookie); - free(cookie); - cookie = NULL; - } - - char *value = ne_strndup(cookie_hdr, sep - cookie_hdr + 1); - cookie = ne_concat("Cookie: $Version=1;", value, "\r\n", NULL); - free(value); - - ne_hook_pre_send(session, add_header, cookie); - return NE_OK; -} -#endif /* NE_VERSION_MINOR > 25 */