📄 res.c
字号:
/* Support for Robot Exclusion Standard (RES). Copyright (C) 2001, 2006, 2007, 2008 Free Software Foundation, Inc.This file is part of Wget.This program is free software; you can redistribute it and/or modifyit under the terms of the GNU General Public License as published bythe Free Software Foundation; either version 3 of the License, or (atyour option) any later version.This program is distributed in the hope that it will be useful, butWITHOUT ANY WARRANTY; without even the implied warranty ofMERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNUGeneral Public License for more details.You should have received a copy of the GNU General Public Licensealong with Wget. If not, see <http://www.gnu.org/licenses/>.Additional permission under GNU GPL version 3 section 7If you modify this program, or any covered work, by linking orcombining it with the OpenSSL project's OpenSSL library (or amodified version of that library), containing parts covered by theterms of the OpenSSL or SSLeay licenses, the Free Software Foundationgrants you additional permission to convey the resulting work.Corresponding Source for a non-source form of such a combinationshall include the source code for the parts of OpenSSL used as wellas that of the covered work. *//* This file implements the Robot Exclusion Standard (RES). RES is a simple protocol that enables site admins to signalize to the web crawlers that certain parts of the site should not be accessed. All the admin needs to do is create a "robots.txt" file in the web server root, and use simple commands to allow or disallow access to certain parts of the site. The first specification was written by Martijn Koster in 1994, and is still available at <http://www.robotstxt.org/wc/norobots.html>. In 1996, Martijn wrote an Internet Draft specifying an improved RES specification; however, that work was apparently abandoned since the draft has expired in 1997 and hasn't been replaced since. The draft is available at <http://www.robotstxt.org/wc/norobots-rfc.html>. This file implements RES as specified by the draft. Note that this only handles the "robots.txt" support. The META tag that controls whether the links should be followed is handled in `html-url.c'. Known deviations: * The end-of-line comment recognition is more in the spirit of the Bourne Shell (as specified by RES-1994). That means that "foo#bar" is taken literally, whereas "foo #bar" is interpreted as "foo". The Draft apparently specifies that both should be interpreted as "foo". * We don't recognize sole CR as the line ending. * We don't implement expiry mechanism for /robots.txt specs. I consider it non-necessary for a relatively short-lived application such as Wget. Besides, it is highly questionable whether anyone deploys the recommended expiry scheme for robots.txt. Entry points are functions res_parse, res_parse_from_file, res_match_path, res_register_specs, res_get_specs, and res_retrieve_file. */#ifdef HAVE_CONFIG_H# include <config.h>#endif#include <stdio.h>#include <stdlib.h>#include <string.h>#include <errno.h>#include <assert.h>#include "wget.h"#include "utils.h"#include "hash.h"#include "url.h"#include "retr.h"#include "res.h"#ifdef TESTING#include "test.h"#endifstruct path_info { char *path; bool allowedp; bool user_agent_exact_p;};struct robot_specs { int count; int size; struct path_info *paths;};/* Parsing the robot spec. *//* Check whether AGENT (a string of length LENGTH) equals "wget" or "*". If it is either of them, *matches is set to one. If it is "wget", *exact_match is set to one. */static voidmatch_user_agent (const char *agent, int length, bool *matches, bool *exact_match){ if (length == 1 && *agent == '*') { *matches = true; *exact_match = false; } else if (BOUNDED_EQUAL_NO_CASE (agent, agent + length, "wget")) { *matches = true; *exact_match = true; } else { *matches = false; *exact_match = false; }}/* Add a path specification between PATH_B and PATH_E as one of the paths in SPECS. */static voidadd_path (struct robot_specs *specs, const char *path_b, const char *path_e, bool allowedp, bool exactp){ struct path_info pp; if (path_b < path_e && *path_b == '/') /* Our path representation doesn't use a leading slash, so remove one from theirs. */ ++path_b; pp.path = strdupdelim (path_b, path_e); pp.allowedp = allowedp; pp.user_agent_exact_p = exactp; ++specs->count; if (specs->count > specs->size) { if (specs->size == 0) specs->size = 1; else specs->size <<= 1; specs->paths = xrealloc (specs->paths, specs->size * sizeof (struct path_info)); } specs->paths[specs->count - 1] = pp;}/* Recreate SPECS->paths with only those paths that have user_agent_exact_p set to true. */static voidprune_non_exact (struct robot_specs *specs){ struct path_info *newpaths; int i, j, cnt; cnt = 0; for (i = 0; i < specs->count; i++) if (specs->paths[i].user_agent_exact_p) ++cnt; newpaths = xnew_array (struct path_info, cnt); for (i = 0, j = 0; i < specs->count; i++) if (specs->paths[i].user_agent_exact_p) newpaths[j++] = specs->paths[i]; assert (j == cnt); xfree (specs->paths); specs->paths = newpaths; specs->count = cnt; specs->size = cnt;}#define EOL(p) ((p) >= lineend)#define SKIP_SPACE(p) do { \ while (!EOL (p) && ISSPACE (*p)) \ ++p; \} while (0)#define FIELD_IS(string_literal) \ BOUNDED_EQUAL_NO_CASE (field_b, field_e, string_literal)/* Parse textual RES specs beginning with SOURCE of length LENGTH. Return a specs objects ready to be fed to res_match_path. The parsing itself is trivial, but creating a correct SPECS object is trickier than it seems, because RES is surprisingly byzantine if you attempt to implement it correctly. A "record" is a block of one or more `User-Agent' lines followed by one or more `Allow' or `Disallow' lines. Record is accepted by Wget if one of the `User-Agent' lines was "wget", or if the user agent line was "*". After all the lines have been read, we examine whether an exact ("wget") user-agent field was specified. If so, we delete all the lines read under "User-Agent: *" blocks because we have our own Wget-specific blocks. This enables the admin to say: User-Agent: * Disallow: / User-Agent: google User-Agent: wget Disallow: /cgi-bin This means that to Wget and to Google, /cgi-bin is disallowed, whereas for all other crawlers, everything is disallowed. res_parse is implemented so that the order of records doesn't matter. In the case above, the "User-Agent: *" could have come after the other one. */struct robot_specs *res_parse (const char *source, int length){ int line_count = 1; const char *p = source; const char *end = source + length; /* true if last applicable user-agent field matches Wget. */ bool user_agent_applies = false; /* true if last applicable user-agent field *exactly* matches Wget. */ bool user_agent_exact = false; /* whether we ever encountered exact user agent. */ bool found_exact = false; /* count of allow/disallow lines in the current "record", i.e. after the last `user-agent' instructions. */ int record_count = 0; struct robot_specs *specs = xnew0 (struct robot_specs); while (1) { const char *lineend, *lineend_real; const char *field_b, *field_e; const char *value_b, *value_e; if (p == end) break; lineend_real = memchr (p, '\n', end - p); if (lineend_real) ++lineend_real; else lineend_real = end; lineend = lineend_real; /* Before doing anything else, check whether the line is empty or comment-only. */ SKIP_SPACE (p); if (EOL (p) || *p == '#') goto next; /* Make sure the end-of-line comments are respected by setting lineend to a location preceding the first comment. Real line ending remains in lineend_real. */ for (lineend = p; lineend < lineend_real; lineend++) if ((lineend == p || ISSPACE (*(lineend - 1))) && *lineend == '#') break; /* Ignore trailing whitespace in the same way. */ while (lineend > p && ISSPACE (*(lineend - 1))) --lineend; assert (!EOL (p)); field_b = p; while (!EOL (p) && (ISALNUM (*p) || *p == '-')) ++p; field_e = p; SKIP_SPACE (p); if (field_b == field_e || EOL (p) || *p != ':') { DEBUGP (("Ignoring malformed line %d", line_count)); goto next; } ++p; /* skip ':' */ 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
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -