📄 res.c
字号:
... non-matching record ... User-Agent: * ... matching record, but will be pruned later ... We have to respect `User-Agent' at the beginning of each new record simply because we don't know if we're going to encounter "Wget" among the agents or not. Hence, match_user_agent is called when record_count != 0. But if record_count is 0, we have to keep calling it until it matches, and if that happens, we must not call it any more, until the next record. Hence the other part of the condition. */ if (record_count != 0 || user_agent_applies == false) match_user_agent (value_b, value_e - value_b, &user_agent_applies, &user_agent_exact); if (user_agent_exact) found_exact = true; record_count = 0; } else if (FIELD_IS ("allow")) { if (user_agent_applies) { add_path (specs, value_b, value_e, true, user_agent_exact); } ++record_count; } else if (FIELD_IS ("disallow")) { if (user_agent_applies) { bool allowed = false; if (value_b == value_e) /* Empty "disallow" line means everything is *allowed*! */ allowed = true; add_path (specs, value_b, value_e, allowed, user_agent_exact); } ++record_count; } else { DEBUGP (("Ignoring unknown field at line %d", line_count)); goto next; } next: p = lineend_real; ++line_count; } if (found_exact) { /* We've encountered an exactly matching user-agent. Throw out all the stuff with user-agent: *. */ prune_non_exact (specs); } else if (specs->size > specs->count) { /* add_path normally over-allocates specs->paths. Reallocate it to the correct size in order to conserve some memory. */ specs->paths = xrealloc (specs->paths, specs->count * sizeof (struct path_info)); specs->size = specs->count; } return specs;}/* The same like res_parse, but first map the FILENAME into memory, and then parse it. */struct robot_specs *res_parse_from_file (const char *filename){ struct robot_specs *specs; struct file_memory *fm = read_file (filename); if (!fm) { logprintf (LOG_NOTQUIET, _("Cannot open %s: %s"), filename, strerror (errno)); return NULL; } specs = res_parse (fm->content, fm->length); read_file_free (fm); return specs;}static voidfree_specs (struct robot_specs *specs){ int i; for (i = 0; i < specs->count; i++) xfree (specs->paths[i].path); xfree_null (specs->paths); xfree (specs);}/* Matching of a path according to the specs. *//* If C is '%' and (ptr[1], ptr[2]) form a hexadecimal number, and if that number is not a numerical representation of '/', decode C and advance the pointer. */#define DECODE_MAYBE(c, ptr) do { \ if (c == '%' && ISXDIGIT (ptr[1]) && ISXDIGIT (ptr[2])) \ { \ char decoded = X2DIGITS_TO_NUM (ptr[1], ptr[2]); \ if (decoded != '/') \ { \ c = decoded; \ ptr += 2; \ } \ } \} while (0)/* The inner matching engine: return true if RECORD_PATH matches URL_PATH. The rules for matching are described at <http://www.robotstxt.org/wc/norobots-rfc.txt>, section 3.2.2. */static boolmatches (const char *record_path, const char *url_path){ const char *rp = record_path; const char *up = url_path; for (; ; ++rp, ++up) { char rc = *rp; char uc = *up; if (!rc) return true; if (!uc) return false; DECODE_MAYBE(rc, rp); DECODE_MAYBE(uc, up); if (rc != uc) return false; }}/* Iterate through all paths in SPECS. For the first one that matches, return its allow/reject status. If none matches, retrieval is by default allowed. */boolres_match_path (const struct robot_specs *specs, const char *path){ int i; if (!specs) return true; for (i = 0; i < specs->count; i++) if (matches (specs->paths[i].path, path)) { bool allowedp = specs->paths[i].allowedp; DEBUGP (("%s path %s because of rule `%s'.\n", allowedp ? "Allowing" : "Rejecting", path, specs->paths[i].path)); return allowedp; } return true;}/* Registering the specs. */static struct hash_table *registered_specs;/* Stolen from cookies.c. */#define SET_HOSTPORT(host, port, result) do { \ int HP_len = strlen (host); \ result = alloca (HP_len + 1 + numdigit (port) + 1); \ memcpy (result, host, HP_len); \ result[HP_len] = ':'; \ number_to_string (result + HP_len + 1, port); \} while (0)/* Register RES specs that below to server on HOST:PORT. They will later be retrievable using res_get_specs. */voidres_register_specs (const char *host, int port, struct robot_specs *specs){ struct robot_specs *old; char *hp, *hp_old; SET_HOSTPORT (host, port, hp); if (!registered_specs) registered_specs = make_nocase_string_hash_table (0); if (hash_table_get_pair (registered_specs, hp, &hp_old, &old)) { if (old) free_specs (old); hash_table_put (registered_specs, hp_old, specs); } else { hash_table_put (registered_specs, xstrdup (hp), specs); }}/* Get the specs that belong to HOST:PORT. */struct robot_specs *res_get_specs (const char *host, int port){ char *hp; SET_HOSTPORT (host, port, hp); if (!registered_specs) return NULL; return hash_table_get (registered_specs, hp);}/* Loading the robots file. */#define RES_SPECS_LOCATION "/robots.txt"/* Retrieve the robots.txt from the server root of the server that serves URL. The file will be named according to the currently active rules, and the file name will be returned in *file. Return true if robots were retrieved OK, false otherwise. */boolres_retrieve_file (const char *url, char **file){ uerr_t err; char *robots_url = uri_merge (url, RES_SPECS_LOCATION); int saved_ts_val = opt.timestamping; int saved_sp_val = opt.spider; logputs (LOG_VERBOSE, _("Loading robots.txt; please ignore errors.\n")); *file = NULL; opt.timestamping = false; opt.spider = false; err = retrieve_url (robots_url, file, NULL, NULL, NULL, false); opt.timestamping = saved_ts_val; opt.spider = saved_sp_val; xfree (robots_url); if (err != RETROK && *file != NULL) { /* If the file is not retrieved correctly, but retrieve_url allocated the file name, deallocate is here so that the caller doesn't have to worry about it. */ xfree (*file); *file = NULL; } return err == RETROK;}boolis_robots_txt_url (const char *url){ char *robots_url = uri_merge (url, RES_SPECS_LOCATION); bool ret = are_urls_equal (url, robots_url); xfree (robots_url); return ret;}voidres_cleanup (void){ if (registered_specs) { hash_table_iterator iter; for (hash_table_iterate (registered_specs, &iter); hash_table_iter_next (&iter); ) { xfree (iter.key); free_specs (iter.value); } hash_table_destroy (registered_specs); registered_specs = NULL; }}#ifdef TESTINGconst char *test_is_robots_txt_url(){ int i; struct { char *url; bool expected_result; } test_array[] = { { "http://www.yoyodyne.com/robots.txt", true }, { "http://www.yoyodyne.com/somepath/", false }, { "http://www.yoyodyne.com/somepath/robots.txt", false }, }; for (i = 0; i < sizeof(test_array)/sizeof(test_array[0]); ++i) { mu_assert ("test_is_robots_txt_url: wrong result", is_robots_txt_url (test_array[i].url) == test_array[i].expected_result); } return NULL;}#endif /* TESTING *//* * vim: et ts=2 sw=2 */
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -