⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 res.c

📁 wget (command line browser) source code
💻 C
📖 第 1 页 / 共 2 页
字号:
      SKIP_SPACE (p);      value_b = p;      while (!EOL (p))	++p;      value_e = p;      /* Finally, we have a syntactically valid line. */      if (FIELD_IS ("user-agent"))	{	  /* We have to support several cases:	     --previous records--	     User-Agent: foo	     User-Agent: Wget	     User-Agent: bar	     ... matching record ...	     User-Agent: baz	     User-Agent: qux	     ... 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 == 0)	    match_user_agent (value_b, value_e - value_b,			      &user_agent_applies, &user_agent_exact);	  if (user_agent_exact)	    found_exact = 1;	  record_count = 0;	}      else if (FIELD_IS ("allow"))	{	  if (user_agent_applies)	    {	      add_path (specs, value_b, value_e, 1, user_agent_exact);	    }	  ++record_count;	}      else if (FIELD_IS ("disallow"))	{	  if (user_agent_applies)	    {	      int allowed = 0;	      if (value_b == value_e)		/* Empty "disallow" line means everything is		   *allowed*!  */		allowed = 1;	      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);  FREE_MAYBE (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 non-zero 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 intmatches (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 1;      if (!uc)	return 0;      DECODE_MAYBE(rc, rp);      DECODE_MAYBE(uc, up);      if (rc != uc)	return 0;    }}/* 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.  */intres_match_path (const struct robot_specs *specs, const char *path){  int i;  if (!specs)    return 1;  for (i = 0; i < specs->count; i++)    if (matches (specs->paths[i].path, path))      {	int 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 1;}/* 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);  /* Required to shut up the compiler. */  old    = NULL;  hp_old = NULL;  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 non-zero if robots were retrieved OK, zero otherwise.  */intres_retrieve_file (const char *url, char **file){  uerr_t err;  char *robots_url = uri_merge (url, RES_SPECS_LOCATION);  logputs (LOG_VERBOSE, _("Loading robots.txt; please ignore errors.\n"));  *file = NULL;  err = retrieve_url (robots_url, file, NULL, NULL, NULL);  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;}static intcleanup_hash_table_mapper (void *key, void *value, void *arg_ignored){  xfree (key);  free_specs (value);  return 0;}voidres_cleanup (void){  if (registered_specs)    {      hash_table_map (registered_specs, cleanup_hash_table_mapper, NULL);      hash_table_destroy (registered_specs);      registered_specs = NULL;    }}

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -