Skip to content

Commit

Permalink
Merge branch 'master' into fix_help
Browse files Browse the repository at this point in the history
  • Loading branch information
akashrawal committed Jun 11, 2017
2 parents 0c7a2ae + 08ed508 commit 737a6d9
Show file tree
Hide file tree
Showing 14 changed files with 74 additions and 19 deletions.
4 changes: 3 additions & 1 deletion include/wget/wget.h
Expand Up @@ -652,7 +652,9 @@ typedef void (*wget_hashmap_key_destructor_t)(void *key);
typedef void (*wget_hashmap_value_destructor_t)(void *value);

WGETAPI wget_hashmap_t
*wget_hashmap_create(int max, int off, wget_hashmap_hash_t hash, wget_hashmap_compare_t cmp) G_GNUC_WGET_MALLOC;
*wget_hashmap_create(int max, wget_hashmap_hash_t hash, wget_hashmap_compare_t cmp) G_GNUC_WGET_MALLOC;
WGETAPI void
wget_hashmap_set_growth_policy(wget_hashmap_t *h, int off);
WGETAPI int
wget_hashmap_put(wget_hashmap_t *h, const void *key, size_t keysize, const void *value, size_t valuesize);
WGETAPI int
Expand Down
15 changes: 10 additions & 5 deletions libwget/hashmap.c
Expand Up @@ -68,20 +68,17 @@ struct _wget_hashmap_st {
};

// create hashmap with initial size <max>
// hashmap growth is specified by off:
// positive values: increase hashmap by <off> entries on each resize
// negative values: increase hashmap by *<-off>, e.g. -2 doubles the size on each resize
// cmp: comparison function for finding
// the hashmap plus shallow content is freed by hashmap_free()

wget_hashmap_t *wget_hashmap_create(int max, int off, wget_hashmap_hash_t hash, wget_hashmap_compare_t cmp)
wget_hashmap_t *wget_hashmap_create(int max, wget_hashmap_hash_t hash, wget_hashmap_compare_t cmp)
{
wget_hashmap_t *h = xmalloc(sizeof(wget_hashmap_t));

h->entry = xcalloc(max, sizeof(ENTRY *));
h->max = max;
h->cur = 0;
h->off = off;
h->off = -2;
h->hash = hash;
h->cmp = cmp;
h->key_destructor = free;
Expand All @@ -92,6 +89,14 @@ wget_hashmap_t *wget_hashmap_create(int max, int off, wget_hashmap_hash_t hash,
return h;
}

// hashmap growth is specified by off:
// positive values: increase hashmap by <off> entries on each resize
// negative values: increase hashmap by *<-off>, e.g. -2 doubles the size on each resize
void wget_hashmap_set_growth_policy(wget_hashmap_t *h, int off)
{
h->off = off;
}

static _GL_INLINE ENTRY * G_GNUC_WGET_NONNULL_ALL hashmap_find_entry(const wget_hashmap_t *h, const char *key, unsigned int hash, int pos)
{
ENTRY *e;
Expand Down
2 changes: 1 addition & 1 deletion libwget/hpkp.c
Expand Up @@ -198,7 +198,7 @@ wget_hpkp_db_t *wget_hpkp_db_init(wget_hpkp_db_t *hpkp_db)
else
memset(hpkp_db, 0, sizeof(*hpkp_db));

hpkp_db->entries = wget_hashmap_create(16, -2, (wget_hashmap_hash_t)_hash_hpkp, (wget_hashmap_compare_t)_compare_hpkp);
hpkp_db->entries = wget_hashmap_create(16, (wget_hashmap_hash_t)_hash_hpkp, (wget_hashmap_compare_t)_compare_hpkp);
wget_hashmap_set_key_destructor(hpkp_db->entries, (wget_hashmap_key_destructor_t)wget_hpkp_free);

/*
Expand Down
2 changes: 1 addition & 1 deletion libwget/hsts.c
Expand Up @@ -153,7 +153,7 @@ wget_hsts_db_t *wget_hsts_db_init(wget_hsts_db_t *hsts_db)
hsts_db = xmalloc(sizeof(wget_hsts_db_t));

memset(hsts_db, 0, sizeof(*hsts_db));
hsts_db->entries = wget_hashmap_create(16, -2, (wget_hashmap_hash_t)_hash_hsts, (wget_hashmap_compare_t)_compare_hsts);
hsts_db->entries = wget_hashmap_create(16, (wget_hashmap_hash_t)_hash_hsts, (wget_hashmap_compare_t)_compare_hsts);
wget_hashmap_set_key_destructor(hsts_db->entries, (wget_hashmap_key_destructor_t)wget_hsts_free);
wget_hashmap_set_value_destructor(hsts_db->entries, (wget_hashmap_value_destructor_t)wget_hsts_free);
wget_thread_mutex_init(&hsts_db->mutex);
Expand Down
2 changes: 1 addition & 1 deletion libwget/http.c
Expand Up @@ -1212,7 +1212,7 @@ wget_http_response_t *wget_http_parse_response_header(char *buf)
break;
case 'x':
if (!wget_strncasecmp_ascii(name, "X-Archive-Orig-last-modified", namelen))
resp->last_modified = wget_http_parse_full_date(s);
resp->last_modified = wget_http_parse_full_date(s);
break;
default:
break;
Expand Down
2 changes: 1 addition & 1 deletion libwget/netrc.c
Expand Up @@ -115,7 +115,7 @@ wget_netrc_db_t *wget_netrc_db_init(wget_netrc_db_t *netrc_db)

memset(netrc_db, 0, sizeof(*netrc_db));

netrc_db->machines = wget_hashmap_create(16, -2, (wget_hashmap_hash_t)_hash_netrc, (wget_hashmap_compare_t)_compare_netrc);
netrc_db->machines = wget_hashmap_create(16, (wget_hashmap_hash_t)_hash_netrc, (wget_hashmap_compare_t)_compare_netrc);
wget_hashmap_set_key_destructor(netrc_db->machines, (wget_hashmap_key_destructor_t)wget_netrc_free);
wget_hashmap_set_value_destructor(netrc_db->machines, (wget_hashmap_value_destructor_t)wget_netrc_free);

Expand Down
4 changes: 2 additions & 2 deletions libwget/ocsp.c
Expand Up @@ -149,11 +149,11 @@ wget_ocsp_db_t *wget_ocsp_db_init(wget_ocsp_db_t *ocsp_db)

memset(ocsp_db, 0, sizeof(*ocsp_db));

ocsp_db->fingerprints = wget_hashmap_create(16, -2, (wget_hashmap_hash_t)_hash_ocsp, (wget_hashmap_compare_t)_compare_ocsp);
ocsp_db->fingerprints = wget_hashmap_create(16, (wget_hashmap_hash_t)_hash_ocsp, (wget_hashmap_compare_t)_compare_ocsp);
wget_hashmap_set_key_destructor(ocsp_db->fingerprints, (wget_hashmap_key_destructor_t)wget_ocsp_free);
wget_hashmap_set_value_destructor(ocsp_db->fingerprints, (wget_hashmap_value_destructor_t)wget_ocsp_free);

ocsp_db->hosts = wget_hashmap_create(16, -2, (wget_hashmap_hash_t)_hash_ocsp, (wget_hashmap_compare_t)_compare_ocsp);
ocsp_db->hosts = wget_hashmap_create(16, (wget_hashmap_hash_t)_hash_ocsp, (wget_hashmap_compare_t)_compare_ocsp);
wget_hashmap_set_key_destructor(ocsp_db->hosts, (wget_hashmap_key_destructor_t)wget_ocsp_free);
wget_hashmap_set_value_destructor(ocsp_db->hosts, (wget_hashmap_value_destructor_t)wget_ocsp_free);

Expand Down
4 changes: 2 additions & 2 deletions libwget/stringmap.c
Expand Up @@ -63,12 +63,12 @@ static unsigned int G_GNUC_WGET_PURE hash_string_nocase(const char *key)

wget_stringmap_t *wget_stringmap_create(int max)
{
return wget_hashmap_create(max, -2, (wget_hashmap_hash_t)hash_string, (wget_hashmap_compare_t)wget_strcmp);
return wget_hashmap_create(max, (wget_hashmap_hash_t)hash_string, (wget_hashmap_compare_t)wget_strcmp);
}

wget_stringmap_t *wget_stringmap_create_nocase(int max)
{
return wget_hashmap_create(max, -2, (wget_hashmap_hash_t)hash_string_nocase, (wget_hashmap_compare_t)wget_strcasecmp);
return wget_hashmap_create(max, (wget_hashmap_hash_t)hash_string_nocase, (wget_hashmap_compare_t)wget_strcasecmp);
}

int wget_stringmap_put_noalloc(wget_stringmap_t *h, const char *key, const void *value)
Expand Down
2 changes: 1 addition & 1 deletion libwget/test_linking.c
Expand Up @@ -18,7 +18,7 @@ int main(void)
wget_strlcpy(buf, "", 0); // strlcpy.c
wget_css_parse_buffer((const char *)1, 0, NULL, NULL, NULL); // css.c
wget_decompress_close(NULL); // decompressor.c
wget_hashmap_create(0, 0, NULL, NULL); // hashmap.c
wget_hashmap_create(0, NULL, NULL); // hashmap.c
wget_fdgetline(&empty, (size_t *)1, 0); // io.c
wget_iri_parse("", NULL); // iri.c
wget_list_free((wget_list_t **)1); // list.c
Expand Down
2 changes: 1 addition & 1 deletion libwget/tls_session.c
Expand Up @@ -158,7 +158,7 @@ wget_tls_session_db_t *wget_tls_session_db_init(wget_tls_session_db_t *tls_sessi
tls_session_db = xmalloc(sizeof(wget_tls_session_db_t));

memset(tls_session_db, 0, sizeof(*tls_session_db));
tls_session_db->entries = wget_hashmap_create(16, -2, (wget_hashmap_hash_t)_hash_tls_session, (wget_hashmap_compare_t)_compare_tls_session);
tls_session_db->entries = wget_hashmap_create(16, (wget_hashmap_hash_t)_hash_tls_session, (wget_hashmap_compare_t)_compare_tls_session);
wget_hashmap_set_key_destructor(tls_session_db->entries, (wget_hashmap_key_destructor_t)wget_tls_session_free);
wget_hashmap_set_value_destructor(tls_session_db->entries, (wget_hashmap_value_destructor_t)wget_tls_session_free);
wget_thread_mutex_init(&tls_session_db->mutex);
Expand Down
2 changes: 1 addition & 1 deletion src/blacklist.c
Expand Up @@ -98,7 +98,7 @@ wget_iri_t *blacklist_add(wget_iri_t *iri)
wget_thread_mutex_lock(&mutex);

if (!blacklist) {
blacklist = wget_hashmap_create(128, -2, (wget_hashmap_hash_t)hash_iri, (wget_hashmap_compare_t)wget_iri_compare);
blacklist = wget_hashmap_create(128, (wget_hashmap_hash_t)hash_iri, (wget_hashmap_compare_t)wget_iri_compare);
wget_hashmap_set_key_destructor(blacklist, (wget_hashmap_key_destructor_t)_free_entry);
}

Expand Down
2 changes: 1 addition & 1 deletion src/host.c
Expand Up @@ -96,7 +96,7 @@ HOST *host_add(wget_iri_t *iri)
wget_thread_mutex_lock(&hosts_mutex);

if (!hosts) {
hosts = wget_hashmap_create(16, -2, (wget_hashmap_hash_t)_host_hash, (wget_hashmap_compare_t)_host_compare);
hosts = wget_hashmap_create(16, (wget_hashmap_hash_t)_host_hash, (wget_hashmap_compare_t)_host_compare);
wget_hashmap_set_key_destructor(hosts, (wget_hashmap_key_destructor_t)_free_host_entry);
}

Expand Down
2 changes: 1 addition & 1 deletion src/wget.c
Expand Up @@ -835,7 +835,7 @@ int main(int argc, const char **argv)
sigaction(SIGWINCH, &sig_action, NULL);
#endif

known_urls = wget_hashmap_create(128, -2, (wget_hashmap_hash_t)hash_url, (wget_hashmap_compare_t)strcmp);
known_urls = wget_hashmap_create(128, (wget_hashmap_hash_t)hash_url, (wget_hashmap_compare_t)strcmp);

// Initialize the plugin system
plugin_db_init();
Expand Down
48 changes: 48 additions & 0 deletions unit-tests/test.c
Expand Up @@ -2212,6 +2212,53 @@ static void test_set_proxy(void)
}
}

static void test_parse_response_header(void)
{
char *response_text = wget_strdup(
"HTTP/1.1 200 OK\r\n"\
"Server: Apache/2.2.22 (Debian)\r\n"\
"Date: Sun, 11 Jun 2017 09:45:54 GMT\r\n"\
"Content-Length: 476\r\n"\
"Connection: keep-alive\r\n"\
"X-Archive-Orig-last-modified: Sun, 25 May 2003 16:55:12 GMT\r\n"\
"Content-Type: text/plain; charset=utf-8\r\n\r\n");

wget_http_response_t *resp = wget_http_parse_response_header(response_text);

if (resp->keep_alive)
ok++;
else {
failed++;
info_printf("HTTP keep-alive Connection header could not be set.\n");
}

if (resp->content_length == 476)
ok++;
else {
failed++;
info_printf("Content-Length mismatch.\n");
}

if (!strcmp(resp->content_type, "text/plain"))
ok++;
else {
failed++;
info_printf("Content-Type mismatch.\n");
}

if (resp->last_modified == wget_http_parse_full_date("Sun, 25 May 2003 16:55:12 GMT"))
ok++;
else {
failed++;
info_printf("X-Archive-Orig-last-modified mismatch\n");
}

xfree(resp->content_type);
xfree(resp->content_type_encoding);
xfree(resp);
xfree(response_text);
}

int main(int argc, const char **argv)
{
// if VALGRIND testing is enabled, we have to call ourselves with valgrind checking
Expand Down Expand Up @@ -2270,6 +2317,7 @@ int main(int argc, const char **argv)
test_netrc();
test_robots();
test_set_proxy();
test_parse_response_header();

selftest_options() ? failed++ : ok++;

Expand Down

0 comments on commit 737a6d9

Please sign in to comment.