Store host as lwc_string in curl fetcher and obtain it via nsurl. Clean up cached hosts on exit.

svn path=/trunk/netsurf/; revision=12918
This commit is contained in:
Michael Drake 2011-10-01 16:53:45 +00:00
parent 4b2730e407
commit 3a319b4829
1 changed files with 25 additions and 29 deletions

View File

@ -75,7 +75,7 @@ struct curl_fetch_info {
bool stopped; /**< Download stopped on purpose. */ bool stopped; /**< Download stopped on purpose. */
bool only_2xx; /**< Only HTTP 2xx responses acceptable. */ bool only_2xx; /**< Only HTTP 2xx responses acceptable. */
nsurl *url; /**< URL of this fetch. */ nsurl *url; /**< URL of this fetch. */
char *host; /**< The hostname of this fetch. */ lwc_string *host; /**< The hostname of this fetch. */
struct curl_slist *headers; /**< List of request headers. */ struct curl_slist *headers; /**< List of request headers. */
char *location; /**< Response Location header, or 0. */ char *location; /**< Response Location header, or 0. */
unsigned long content_length; /**< Response Content-Length, or 0. */ unsigned long content_length; /**< Response Content-Length, or 0. */
@ -91,7 +91,7 @@ struct curl_fetch_info {
struct cache_handle { struct cache_handle {
CURL *handle; /**< The cached cURL handle */ CURL *handle; /**< The cached cURL handle */
char *host; /**< The host for which this handle is cached */ lwc_string *host; /**< The host for which this handle is cached */
struct cache_handle *r_prev; /**< Previous cached handle in ring. */ struct cache_handle *r_prev; /**< Previous cached handle in ring. */
struct cache_handle *r_next; /**< Next cached handle in ring. */ struct cache_handle *r_next; /**< Next cached handle in ring. */
@ -116,8 +116,8 @@ static void * fetch_curl_setup(struct fetch *parent_fetch, nsurl *url,
static bool fetch_curl_start(void *vfetch); static bool fetch_curl_start(void *vfetch);
static bool fetch_curl_initiate_fetch(struct curl_fetch_info *fetch, static bool fetch_curl_initiate_fetch(struct curl_fetch_info *fetch,
CURL *handle); CURL *handle);
static CURL *fetch_curl_get_handle(char *host); static CURL *fetch_curl_get_handle(lwc_string *host);
static void fetch_curl_cache_handle(CURL *handle, char *hostname); static void fetch_curl_cache_handle(CURL *handle, lwc_string *host);
static CURLcode fetch_curl_set_options(struct curl_fetch_info *f); static CURLcode fetch_curl_set_options(struct curl_fetch_info *f);
static CURLcode fetch_curl_sslctxfun(CURL *curl_handle, void *_sslctx, static CURLcode fetch_curl_sslctxfun(CURL *curl_handle, void *_sslctx,
void *p); void *p);
@ -284,6 +284,8 @@ bool fetch_curl_initialise(lwc_string *scheme)
void fetch_curl_finalise(lwc_string *scheme) void fetch_curl_finalise(lwc_string *scheme)
{ {
struct cache_handle *h;
curl_fetchers_registered--; curl_fetchers_registered--;
LOG(("Finalise cURL fetcher %s", lwc_string_data(scheme))); LOG(("Finalise cURL fetcher %s", lwc_string_data(scheme)));
if (curl_fetchers_registered == 0) { if (curl_fetchers_registered == 0) {
@ -299,6 +301,13 @@ void fetch_curl_finalise(lwc_string *scheme)
curl_global_cleanup(); curl_global_cleanup();
} }
/* Free anything remaining in the cached curl handle ring */
while (curl_handle_ring != NULL) {
h = curl_handle_ring;
RING_REMOVE(curl_handle_ring, h);
lwc_string_unref(h->host);
}
} }
@ -329,10 +338,8 @@ void * fetch_curl_setup(struct fetch *parent_fetch, nsurl *url,
const struct fetch_multipart_data *post_multipart, const struct fetch_multipart_data *post_multipart,
const char **headers) const char **headers)
{ {
char *host;
struct curl_fetch_info *fetch; struct curl_fetch_info *fetch;
struct curl_slist *slist; struct curl_slist *slist;
url_func_result res;
int i; int i;
fetch = malloc(sizeof (*fetch)); fetch = malloc(sizeof (*fetch));
@ -340,18 +347,6 @@ void * fetch_curl_setup(struct fetch *parent_fetch, nsurl *url,
return 0; return 0;
fetch->fetch_handle = parent_fetch; fetch->fetch_handle = parent_fetch;
fetch->url = nsurl_ref(url);
res = url_host(nsurl_access(url), &host);
if (res != URL_FUNC_OK) {
/* we only fail memory exhaustion */
if (res == URL_FUNC_NOMEM)
goto failed;
host = strdup("");
if (!host)
goto failed;
}
LOG(("fetch %p, url '%s'", fetch, nsurl_access(url))); LOG(("fetch %p, url '%s'", fetch, nsurl_access(url)));
@ -362,7 +357,8 @@ void * fetch_curl_setup(struct fetch *parent_fetch, nsurl *url,
fetch->stopped = false; fetch->stopped = false;
fetch->only_2xx = only_2xx; fetch->only_2xx = only_2xx;
fetch->headers = 0; fetch->headers = 0;
fetch->host = host; fetch->url = nsurl_ref(url);
fetch->host = nsurl_get_component(url, NSURL_HOST);
fetch->location = 0; fetch->location = 0;
fetch->content_length = 0; fetch->content_length = 0;
fetch->http_code = 0; fetch->http_code = 0;
@ -419,7 +415,7 @@ void * fetch_curl_setup(struct fetch *parent_fetch, nsurl *url,
return fetch; return fetch;
failed: failed:
free(host); lwc_string_unref(fetch->host);
nsurl_unref(fetch->url); nsurl_unref(fetch->url);
free(fetch->post_urlenc); free(fetch->post_urlenc);
if (fetch->post_multipart) if (fetch->post_multipart)
@ -478,14 +474,14 @@ bool fetch_curl_initiate_fetch(struct curl_fetch_info *fetch, CURL *handle)
* Find a CURL handle to use to dispatch a job * Find a CURL handle to use to dispatch a job
*/ */
CURL *fetch_curl_get_handle(char *host) CURL *fetch_curl_get_handle(lwc_string *host)
{ {
struct cache_handle *h; struct cache_handle *h;
CURL *ret; CURL *ret;
RING_FINDBYHOST(curl_handle_ring, h, host); RING_FINDBYLWCHOST(curl_handle_ring, h, host);
if (h) { if (h) {
ret = h->handle; ret = h->handle;
free(h->host); lwc_string_unref(h->host);
RING_REMOVE(curl_handle_ring, h); RING_REMOVE(curl_handle_ring, h);
free(h); free(h);
} else { } else {
@ -499,11 +495,11 @@ CURL *fetch_curl_get_handle(char *host)
* Cache a CURL handle for the provided host (if wanted) * Cache a CURL handle for the provided host (if wanted)
*/ */
void fetch_curl_cache_handle(CURL *handle, char *host) void fetch_curl_cache_handle(CURL *handle, lwc_string *host)
{ {
struct cache_handle *h = 0; struct cache_handle *h = 0;
int c; int c;
RING_FINDBYHOST(curl_handle_ring, h, host); RING_FINDBYLWCHOST(curl_handle_ring, h, host);
if (h) { if (h) {
/* Already have a handle cached for this hostname */ /* Already have a handle cached for this hostname */
curl_easy_cleanup(handle); curl_easy_cleanup(handle);
@ -522,8 +518,8 @@ void fetch_curl_cache_handle(CURL *handle, char *host)
curl_handle_ring = h->r_next; curl_handle_ring = h->r_next;
curl_easy_cleanup(h->handle); curl_easy_cleanup(h->handle);
h->handle = handle; h->handle = handle;
free(h->host); lwc_string_unref(h->host);
h->host = strdup(host); h->host = lwc_string_ref(host);
} else { } else {
/* Actually, we don't want to cache any handles */ /* Actually, we don't want to cache any handles */
curl_easy_cleanup(handle); curl_easy_cleanup(handle);
@ -534,7 +530,7 @@ void fetch_curl_cache_handle(CURL *handle, char *host)
/* The table isn't full yet, so make a shiny new handle to add to the ring */ /* The table isn't full yet, so make a shiny new handle to add to the ring */
h = (struct cache_handle*)malloc(sizeof(struct cache_handle)); h = (struct cache_handle*)malloc(sizeof(struct cache_handle));
h->handle = handle; h->handle = handle;
h->host = strdup(host); h->host = lwc_string_ref(host);
RING_INSERT(curl_handle_ring, h); RING_INSERT(curl_handle_ring, h);
} }
@ -706,7 +702,7 @@ void fetch_curl_free(void *vf)
if (f->curl_handle) if (f->curl_handle)
curl_easy_cleanup(f->curl_handle); curl_easy_cleanup(f->curl_handle);
nsurl_unref(f->url); nsurl_unref(f->url);
free(f->host); lwc_string_unref(f->host);
free(f->location); free(f->location);
free(f->cookie_string); free(f->cookie_string);
free(f->realm); free(f->realm);