1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
|
#include "robots_txt.h"
#include "constants.h"
#include <library/cpp/uri/http_url.h>
#include <library/cpp/charset/ci_string.h>
#include <library/cpp/string_utils/url/url.h>
#include <util/system/maxlen.h>
#include <util/generic/yexception.h>
#include <util/generic/algorithm.h>
namespace {
TBotIdSet ConvertBotIdSet(const TSet<ui32>& botIds) noexcept {
TBotIdSet result;
for (auto id : botIds) {
result.insert(id);
}
return result;
}
} // namespace
TRobotsTxtRulesIterator::TRobotsTxtRulesIterator(const char* begin, const char* end)
: Begin(begin)
, End(end)
{
}
void TRobotsTxtRulesIterator::Next() {
while (Begin < End && *Begin)
++Begin;
while (Begin < End && !isalpha(*Begin))
++Begin;
}
bool TRobotsTxtRulesIterator::HasRule() const {
return Begin < End;
}
const char* TRobotsTxtRulesIterator::GetRule() const {
return Begin + 1;
}
TString TRobotsTxtRulesIterator::GetInitialRule() const {
auto begin = Begin + 1;
TStringBuf rule(begin, strlen(begin));
switch (*Begin) {
case 'a':
case 'd':
return rule.EndsWith('*') ? TString(rule.Chop(1)) : TString::Join(rule, '$');
default:
return TString(rule);
}
}
EDirectiveType TRobotsTxtRulesIterator::GetRuleType() const {
return CharToDirType(*Begin);
}
EDirectiveType TRobotsTxtRulesIterator::CharToDirType(char ch) {
switch (toupper(ch)) {
case 'A':
return ALLOW;
case 'C':
return CRAWL_DELAY;
case 'D':
return DISALLOW;
case 'H':
return HOST;
case 'P':
return CLEAN_PARAM;
case 'S':
return SITEMAP;
}
return UNKNOWN;
}
TRobotsTxtRulesHandlerBase::TRobotsTxtRulesHandlerBase(
TBotIdSet supportedBotIds,
int robotsMaxSize,
int maxRulesNumber,
bool saveDataForAnyBot)
: HandleErrors(false)
, SiteMaps()
, CleanParams()
, HostDirective("")
, Errors()
, AcceptedLines()
, CrossSectionAcceptedLines()
, BotIdToInfo(robotstxtcfg::max_botid)
, RobotsMaxSize(robotsMaxSize)
, MaxRulesNumber(maxRulesNumber)
, SaveDataForAnyBot(saveDataForAnyBot)
, SupportedBotIds(supportedBotIds)
{
Y_ENSURE(!supportedBotIds.empty());
if (RobotsMaxSize <= 0)
RobotsMaxSize = robots_max;
if (MaxRulesNumber <= 0)
MaxRulesNumber = max_rules_count;
ResetOptimized();
}
TRobotsTxtRulesHandlerBase::TRobotsTxtRulesHandlerBase(
const TSet<ui32>& supportedBotIds,
int robotsMaxSize,
int maxRulesNumber,
bool saveDataForAnyBot)
: TRobotsTxtRulesHandlerBase(ConvertBotIdSet(supportedBotIds), robotsMaxSize, maxRulesNumber, saveDataForAnyBot)
{}
TRobotsTxtRulesHandlerBase::~TRobotsTxtRulesHandlerBase() = default;
void TRobotsTxtRulesHandlerBase::CheckBotIdValidity(const ui32 botId) const {
if (botId >= robotstxtcfg::max_botid || !IsBotIdSupported(botId))
ythrow yexception() << "robots.txt parser requested for invalid or unsupported botId = " << botId << Endl;
;
}
int TRobotsTxtRulesHandlerBase::GetCrawlDelay(const ui32 botId, bool* realInfo) const {
const auto id = GetMappedBotId(botId, false);
if (realInfo)
*realInfo = bool(id);
return BotIdToInfo[id.GetOrElse(robotstxtcfg::id_anybot)].CrawlDelay;
}
int TRobotsTxtRulesHandlerBase::GetMinCrawlDelay(int defaultCrawlDelay) const {
int res = INT_MAX;
bool useDefault = false;
for (ui32 botId = 0; botId < robotstxtcfg::max_botid; ++botId) {
if (robotstxtcfg::IsYandexBotId(botId) && IsBotIdSupported(botId) && !IsDisallowAll(botId)) {
bool realInfo;
int curCrawlDelay = GetCrawlDelay(botId, &realInfo);
if (realInfo) {
if (curCrawlDelay == -1) {
useDefault = true;
} else {
res = Min(res, curCrawlDelay);
}
}
}
}
if (useDefault && defaultCrawlDelay < res) {
return -1;
}
if (res == INT_MAX) {
res = GetCrawlDelay(robotstxtcfg::id_anybot);
}
return res;
}
void TRobotsTxtRulesHandlerBase::SetCrawlDelay(const ui32 botId, int crawlDelay) {
CheckBotIdValidity(botId);
BotIdToInfo[botId].CrawlDelay = crawlDelay;
}
const TVector<TString> TRobotsTxtRulesHandlerBase::GetSiteMaps() const {
return TVector<TString>(SiteMaps.begin(), SiteMaps.end());
}
void TRobotsTxtRulesHandlerBase::AddSiteMap(const char* sitemap) {
SiteMaps.insert(sitemap);
}
const TVector<TString> TRobotsTxtRulesHandlerBase::GetCleanParams() const {
return TVector<TString>(CleanParams.begin(), CleanParams.end());
}
void TRobotsTxtRulesHandlerBase::AddCleanParam(const char* cleanParam) {
CleanParams.insert(cleanParam);
}
const TString& TRobotsTxtRulesHandlerBase::GetHostDirective() const {
return HostDirective;
}
void TRobotsTxtRulesHandlerBase::SetHostDirective(const char* hostDirective) {
HostDirective = hostDirective;
}
const TRobotsTxtRulesHandlerBase::TErrorVector& TRobotsTxtRulesHandlerBase::GetErrors() const {
return Errors;
}
TVector<int> TRobotsTxtRulesHandlerBase::GetAcceptedLines(const ui32 botId) const {
TVector<int> ret;
for (size_t i = 0; i < CrossSectionAcceptedLines.size(); ++i)
ret.push_back(CrossSectionAcceptedLines[i]);
bool hasLinesForBotId = false;
for (size_t i = 0; i < AcceptedLines.size(); ++i) {
if (AcceptedLines[i].first == botId) {
hasLinesForBotId = true;
break;
}
}
for (size_t i = 0; i < AcceptedLines.size(); ++i) {
if (hasLinesForBotId && AcceptedLines[i].first == botId) {
ret.push_back(AcceptedLines[i].second);
} else if (!hasLinesForBotId && AcceptedLines[i].first == robotstxtcfg::id_anybot) {
ret.push_back(AcceptedLines[i].second);
}
}
Sort(ret.begin(), ret.end());
return ret;
}
void TRobotsTxtRulesHandlerBase::AddAcceptedLine(ui32 line, const TBotIdSet& botIds, bool isCrossSection) {
if (isCrossSection) {
CrossSectionAcceptedLines.push_back(line);
return;
}
for (auto botId : botIds) {
AcceptedLines.push_back(TBotIdAcceptedLine(botId, line));
}
}
void TRobotsTxtRulesHandlerBase::SetErrorsHandling(bool handleErrors) {
HandleErrors = handleErrors;
}
bool TRobotsTxtRulesHandlerBase::IsHandlingErrors() const {
return HandleErrors;
}
EDirectiveType TRobotsTxtRulesHandlerBase::NameToDirType(const char* d) {
if (!strcmp("disallow", d))
return DISALLOW;
if (!strcmp("allow", d))
return ALLOW;
if (!strcmp("user-agent", d))
return USER_AGENT;
if (!strcmp("host", d))
return HOST;
if (!strcmp("sitemap", d))
return SITEMAP;
if (!strcmp("clean-param", d))
return CLEAN_PARAM;
if (!strcmp("crawl-delay", d))
return CRAWL_DELAY;
return UNKNOWN;
};
const char* TRobotsTxtRulesHandlerBase::DirTypeToName(EDirectiveType t) {
static const char* name[] = {"Allow", "Crawl-Delay", "Disallow", "Host", "Clean-Param", "Sitemap", "User-Agent", "Unknown"};
switch (t) {
case ALLOW:
return name[0];
case CRAWL_DELAY:
return name[1];
case DISALLOW:
return name[2];
case HOST:
return name[3];
case CLEAN_PARAM:
return name[4];
case SITEMAP:
return name[5];
case USER_AGENT:
return name[6];
case UNKNOWN:
return name[7];
}
return name[7];
};
bool TRobotsTxtRulesHandlerBase::CheckRobot(
const char* userAgent,
TBotIdSet& botIds,
const TVector<ui32>* botIdToMaxAppropriateUserAgentNameLength) const
{
TCaseInsensitiveStringBuf agent(userAgent);
for (size_t botIndex = 0; botIndex < robotstxtcfg::max_botid; ++botIndex) {
if (!IsBotIdSupported(botIndex))
continue;
bool hasRequiredAgentNamePrefix = agent.StartsWith(robotstxtcfg::GetReqPrefix(botIndex));
bool isContainedInFullName = robotstxtcfg::GetFullName(botIndex).StartsWith(agent);
bool wasMoreImportantAgent = false;
if (botIdToMaxAppropriateUserAgentNameLength)
wasMoreImportantAgent = agent.size() < (*botIdToMaxAppropriateUserAgentNameLength)[botIndex];
if (hasRequiredAgentNamePrefix && isContainedInFullName && !wasMoreImportantAgent) {
botIds.insert(botIndex);
}
}
return !botIds.empty();
}
int TRobotsTxtRulesHandlerBase::CheckRule(const char* value, int line, TRobotsTxtRulesHandlerBase* rulesHandler) {
if (!rulesHandler->IsHandlingErrors())
return 0;
if (auto len = strlen(value); len > max_rule_length) {
rulesHandler->AddError(ERROR_RULE_HUGE, line);
}
bool upper = false, suspect = false;
for (const char* r = value; *r; ++r) {
if (!upper && isupper(*r))
upper = true;
if (!suspect && !isalnum(*r) && !strchr("/_?=.-*%&~[]:;@", *r) && (*(r + 1) || *r != '$'))
suspect = true;
}
if (suspect)
rulesHandler->AddError(WARNING_SUSPECT_SYMBOL, line);
if (upper)
rulesHandler->AddError(WARNING_UPPER_REGISTER, line);
return suspect || upper;
}
void TRobotsTxtRulesHandlerBase::AddError(EFormatErrorType type, int line) {
if (!HandleErrors)
return;
Errors.push_back(std::make_pair(type, line));
}
void TRobotsTxtRulesHandlerBase::ResetOptimized() noexcept {
for (ui32 i = 0; i < OptimizedBotIdToStoredBotId.size(); ++i) {
OptimizedBotIdToStoredBotId[i] = i; // by default, every bot maps to itself
}
}
void TRobotsTxtRulesHandlerBase::Clear() {
SiteMaps.clear();
CleanParams.clear();
HostDirective = "";
if (HandleErrors) {
AcceptedLines.clear();
CrossSectionAcceptedLines.clear();
Errors.clear();
}
for (size_t botId = 0; botId < BotIdToInfo.size(); ++botId) {
BotIdToInfo[botId].CrawlDelay = -1;
}
LoadedBotIds.clear();
}
void TRobotsTxtRulesHandlerBase::ClearInternal(const ui32 botId) {
CheckBotIdValidity(botId);
BotIdToInfo[botId].CrawlDelay = -1;
TVector<TBotIdAcceptedLine> newAcceptedLines;
for (size_t i = 0; i < AcceptedLines.size(); ++i)
if (AcceptedLines[i].first != botId)
newAcceptedLines.push_back(AcceptedLines[i]);
AcceptedLines.swap(newAcceptedLines);
}
int TRobotsTxtRulesHandlerBase::CheckHost(const char* host) {
THttpURL parsed;
TString copyHost = host;
if (GetHttpPrefixSize(copyHost) == 0) {
copyHost = TString("http://") + copyHost;
}
return parsed.Parse(copyHost.data(), THttpURL::FeaturesRobot) == THttpURL::ParsedOK && parsed.GetField(THttpURL::FieldHost) != TString("");
}
int TRobotsTxtRulesHandlerBase::CheckSitemapUrl(const char* url, const char* host, TString& modifiedUrl) {
if (host != nullptr && strlen(url) > 0 && url[0] == '/') {
modifiedUrl = TString(host) + url;
} else {
modifiedUrl = url;
}
url = modifiedUrl.data();
if (strlen(url) >= URL_MAX - 8)
return 0;
THttpURL parsed;
if (parsed.Parse(url, THttpURL::FeaturesRobot) || !parsed.IsValidAbs())
return 0;
if (parsed.GetScheme() != THttpURL::SchemeHTTP && parsed.GetScheme() != THttpURL::SchemeHTTPS)
return 0;
return CheckHost(parsed.PrintS(THttpURL::FlagHostPort).data());
}
// s - is space separated pair of clean-params (separated by &) and path prefix
int TRobotsTxtRulesHandlerBase::CheckAndNormCleanParam(TString& value) {
if (value.find(' ') == TString::npos) {
value.push_back(' ');
}
const char* s = value.data();
if (!s || !*s || strlen(s) > URL_MAX / 2 - 9)
return 0;
const char* p = s;
while (*p && !isspace(*p))
++p;
for (; s != p; ++s) {
// allowed only following not alpha-numerical symbols
if (!isalnum(*s) && !strchr("+-=_&%[]{}():.", *s))
return 0;
// clean-params for prefix can be enumerated by & symbol, && not allowed syntax
if (*s == '&' && *(s + 1) == '&')
return 0;
}
const char* pathPrefix = p + 1;
while (isspace(*p))
++p;
char r[URL_MAX];
char* pr = r;
for (; *p; ++p) {
if (!isalnum(*p) && !strchr(".-/*_,;:%", *p))
return 0;
if (*p == '*')
*pr++ = '.';
if (*p == '.')
*pr++ = '\\';
*pr++ = *p;
}
*pr++ = '.';
*pr++ = '*';
*pr = 0;
TString params = value.substr(0, pathPrefix - value.data());
value = params + r;
return 1;
}
int TRobotsTxtRulesHandlerBase::ParseCrawlDelay(const char* value, int& crawlDelay) {
static const int MAX_CRAWL_DELAY = 1 << 10;
int val = 0;
const char* p = value;
for (; isdigit(*p); ++p) {
val = val * 10 + *p - '0';
if (val > MAX_CRAWL_DELAY)
return 0;
}
if (*p) {
if (*p++ != '.')
return 0;
if (strspn(p, "1234567890") != strlen(p))
return 0;
}
for (const char* s = p; s - p < 3; ++s)
val = val * 10 + (s < p + strlen(p) ? *s - '0' : 0);
crawlDelay = val;
return 1;
}
bool TRobotsTxtRulesHandlerBase::AddRuleWithErrorCheck(const ui32 botId, TStringBuf rule, char type, TRobotsTxtParser& parser) {
if (!IsBotIdSupported(botId))
return true;
if (!AddRule(botId, rule, type)) {
AddError(ERROR_ROBOTS_HUGE, parser.GetLineNumber());
AfterParse(botId);
return false;
}
return true;
}
int TRobotsTxtRulesHandlerBase::OnHost(const ui32 botId, TRobotsTxtParser& parser, const char* value, TRobotsTxtRulesHandlerBase*& rulesHandler) {
// Temporary hack for correct repacking robots.txt from new format to old
// Remove it, when robot-stable-2010-10-17 will be deployed in production
if (!IsBotIdSupported(botId))
return 0;
// end of hack
if (rulesHandler->HostDirective != "")
rulesHandler->AddError(ERROR_HOST_MULTI, parser.GetLineNumber());
else {
if (!CheckHost(value))
rulesHandler->AddError(ERROR_HOST_FORMAT, parser.GetLineNumber());
else {
rulesHandler->SetHostDirective(value);
if (!rulesHandler->AddRuleWithErrorCheck(botId, value, 'H', parser))
return 2;
}
}
return 0;
}
bool TRobotsTxtRulesHandlerBase::IsBotIdLoaded(const ui32 botId) const {
return LoadedBotIds.contains(botId);
}
bool TRobotsTxtRulesHandlerBase::IsBotIdSupported(const ui32 botId) const {
return (SaveDataForAnyBot && botId == robotstxtcfg::id_anybot) || SupportedBotIds.contains(botId);
}
ui32 TRobotsTxtRulesHandlerBase::GetNotOptimizedBotId(const ui32 botId) const {
return (botId < OptimizedBotIdToStoredBotId.size())
? OptimizedBotIdToStoredBotId[botId]
: botId;
}
TMaybe<ui32> TRobotsTxtRulesHandlerBase::GetMappedBotId(ui32 botId, bool useAny) const {
botId = GetNotOptimizedBotId(botId);
CheckBotIdValidity(botId);
if (IsBotIdLoaded(botId))
return botId;
if (useAny)
return robotstxtcfg::id_anybot;
return {};
}
|