util.cc 52 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027202820292030203120322033203420352036203720382039204020412042204320442045204620472048204920502051205220532054
  1. /* <!-- copyright */
  2. /*
  3. * aria2 - The high speed download utility
  4. *
  5. * Copyright (C) 2006 Tatsuhiro Tsujikawa
  6. *
  7. * This program is free software; you can redistribute it and/or modify
  8. * it under the terms of the GNU General Public License as published by
  9. * the Free Software Foundation; either version 2 of the License, or
  10. * (at your option) any later version.
  11. *
  12. * This program is distributed in the hope that it will be useful,
  13. * but WITHOUT ANY WARRANTY; without even the implied warranty of
  14. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  15. * GNU General Public License for more details.
  16. *
  17. * You should have received a copy of the GNU General Public License
  18. * along with this program; if not, write to the Free Software
  19. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
  20. *
  21. * In addition, as a special exception, the copyright holders give
  22. * permission to link the code of portions of this program with the
  23. * OpenSSL library under certain conditions as described in each
  24. * individual source file, and distribute linked combinations
  25. * including the two.
  26. * You must obey the GNU General Public License in all respects
  27. * for all of the code used other than OpenSSL. If you modify
  28. * file(s) with this exception, you may extend this exception to your
  29. * version of the file(s), but you are not obligated to do so. If you
  30. * do not wish to do so, delete this exception statement from your
  31. * version. If you delete this exception statement from all source
  32. * files in the program, then also delete it here.
  33. */
  34. /* copyright --> */
  35. #include "util.h"
  36. #ifdef __sun
  37. // For opensolaris, just include signal.h which includes sys/signal.h
  38. #ifdef HAVE_SIGNAL_H
  39. # include <signal.h>
  40. #endif // HAVE_SIGNAL_H
  41. #else // !__sun
  42. #ifdef HAVE_SYS_SIGNAL_H
  43. # include <sys/signal.h>
  44. #endif // HAVE_SYS_SIGNAL_H
  45. #ifdef HAVE_SIGNAL_H
  46. # include <signal.h>
  47. #endif // HAVE_SIGNAL_H
  48. #endif // !__sun
  49. #include <sys/types.h>
  50. #ifdef HAVE_PWD_H
  51. # include <pwd.h>
  52. #endif // HAVE_PWD_H
  53. #include <cerrno>
  54. #include <cassert>
  55. #include <cstring>
  56. #include <cstdio>
  57. #include <cstdlib>
  58. #include <sstream>
  59. #include <ostream>
  60. #include <algorithm>
  61. #include <fstream>
  62. #include <iomanip>
  63. #include "SimpleRandomizer.h"
  64. #include "File.h"
  65. #include "Randomizer.h"
  66. #include "a2netcompat.h"
  67. #include "BitfieldMan.h"
  68. #include "DefaultDiskWriter.h"
  69. #include "FatalException.h"
  70. #include "FileEntry.h"
  71. #include "A2STR.h"
  72. #include "array_fun.h"
  73. #include "bitfield.h"
  74. #include "DownloadHandlerConstants.h"
  75. #include "RequestGroup.h"
  76. #include "LogFactory.h"
  77. #include "Logger.h"
  78. #include "Option.h"
  79. #include "DownloadContext.h"
  80. #include "BufferedFile.h"
  81. #include "SocketCore.h"
  82. #include "Lock.h"
  83. #ifdef ENABLE_MESSAGE_DIGEST
  84. # include "MessageDigest.h"
  85. # include "message_digest_helper.h"
  86. #endif // ENABLE_MESSAGE_DIGEST
  87. // For libc6 which doesn't define ULLONG_MAX properly because of broken limits.h
  88. #ifndef ULLONG_MAX
  89. # define ULLONG_MAX 18446744073709551615ULL
  90. #endif // ULLONG_MAX
  91. namespace aria2 {
  92. #ifdef __MINGW32__
  93. namespace {
  94. int utf8ToWChar(wchar_t* out, size_t outLength, const char* src)
  95. {
  96. return MultiByteToWideChar(CP_UTF8, 0, src, -1, out, outLength);
  97. }
  98. } // namespace
  99. namespace {
  100. int ansiToWChar(wchar_t* out, size_t outLength, const char* src)
  101. {
  102. return MultiByteToWideChar(CP_ACP, 0, src, -1, out, outLength);
  103. }
  104. } // namespace
  105. namespace {
  106. int wCharToUtf8(char* out, size_t outLength, const wchar_t* src)
  107. {
  108. return WideCharToMultiByte(CP_UTF8, 0, src, -1, out, outLength, 0, 0);
  109. }
  110. } // namespace
  111. namespace {
  112. int wCharToAnsi(char* out, size_t outLength, const wchar_t* src)
  113. {
  114. return WideCharToMultiByte(CP_ACP, 0, src, -1, out, outLength, 0, 0);
  115. }
  116. } // namespace
  117. std::wstring utf8ToWChar(const char* src)
  118. {
  119. int len = utf8ToWChar(0, 0, src);
  120. if(len <= 0) {
  121. abort();
  122. }
  123. auto buf = make_unique<wchar_t[]>((size_t)len);
  124. len = utf8ToWChar(buf.get(), len, src);
  125. if(len <= 0) {
  126. abort();
  127. } else {
  128. return buf.get();
  129. }
  130. }
  131. std::wstring utf8ToWChar(const std::string& src)
  132. {
  133. return utf8ToWChar(src.c_str());
  134. }
  135. std::string utf8ToNative(const std::string& src)
  136. {
  137. std::wstring wsrc = utf8ToWChar(src);
  138. int len = wCharToAnsi(0, 0, wsrc.c_str());
  139. if(len <= 0) {
  140. abort();
  141. }
  142. auto buf = make_unique<char[]>((size_t)len);
  143. len = wCharToAnsi(buf.get(), len, wsrc.c_str());
  144. if(len <= 0) {
  145. abort();
  146. } else {
  147. return buf.get();
  148. }
  149. }
  150. std::string wCharToUtf8(const std::wstring& wsrc)
  151. {
  152. int len = wCharToUtf8(0, 0, wsrc.c_str());
  153. if(len <= 0) {
  154. abort();
  155. }
  156. auto buf = make_unique<char[]>((size_t)len);
  157. len = wCharToUtf8(buf.get(), len, wsrc.c_str());
  158. if(len <= 0) {
  159. abort();
  160. } else {
  161. return buf.get();
  162. }
  163. }
  164. std::string nativeToUtf8(const std::string& src)
  165. {
  166. int len = ansiToWChar(0, 0, src.c_str());
  167. if(len <= 0) {
  168. abort();
  169. }
  170. auto buf = make_unique<wchar_t[]>((size_t)len);
  171. len = ansiToWChar(buf.get(), len, src.c_str());
  172. if(len <= 0) {
  173. abort();
  174. } else {
  175. return wCharToUtf8(std::wstring(buf.get()));
  176. }
  177. }
  178. #endif // __MINGW32__
  179. namespace util {
  180. const char DEFAULT_STRIP_CHARSET[] = "\r\n\t ";
  181. std::string strip(const std::string& str, const char* chars)
  182. {
  183. std::pair<std::string::const_iterator,
  184. std::string::const_iterator> p =
  185. stripIter(str.begin(), str.end(), chars);
  186. return std::string(p.first, p.second);
  187. }
  188. std::string itos(int64_t value, bool comma)
  189. {
  190. bool flag = false;
  191. std::string str;
  192. if(value < 0) {
  193. if(value == INT64_MIN) {
  194. if(comma) {
  195. str = "-9,223,372,036,854,775,808";
  196. } else {
  197. str = "-9223372036854775808";
  198. }
  199. return str;
  200. }
  201. flag = true;
  202. value = -value;
  203. }
  204. str = uitos(value, comma);
  205. if(flag) {
  206. str.insert(str.begin(), '-');
  207. }
  208. return str;
  209. }
  210. int64_t difftv(struct timeval tv1, struct timeval tv2) {
  211. if((tv1.tv_sec < tv2.tv_sec) ||
  212. ((tv1.tv_sec == tv2.tv_sec) && (tv1.tv_usec < tv2.tv_usec))) {
  213. return 0;
  214. }
  215. return ((int64_t)(tv1.tv_sec-tv2.tv_sec)*1000000+
  216. tv1.tv_usec-tv2.tv_usec);
  217. }
  218. int32_t difftvsec(struct timeval tv1, struct timeval tv2) {
  219. if(tv1.tv_sec < tv2.tv_sec) {
  220. return 0;
  221. }
  222. return tv1.tv_sec-tv2.tv_sec;
  223. }
  224. std::string replace(const std::string& target, const std::string& oldstr, const std::string& newstr) {
  225. if(target.empty() || oldstr.empty()) {
  226. return target;
  227. }
  228. std::string result;
  229. std::string::size_type p = 0;
  230. std::string::size_type np = target.find(oldstr);
  231. while(np != std::string::npos) {
  232. result.append(target.begin()+p, target.begin()+np);
  233. result += newstr;
  234. p = np+oldstr.size();
  235. np = target.find(oldstr, p);
  236. }
  237. result.append(target.begin()+p, target.end());
  238. return result;
  239. }
  240. bool isAlpha(const char c)
  241. {
  242. return ('A' <= c && c <= 'Z') || ('a' <= c && c <= 'z');
  243. }
  244. bool isDigit(const char c)
  245. {
  246. return '0' <= c && c <= '9';
  247. }
  248. bool isHexDigit(const char c)
  249. {
  250. return isDigit(c) || ('A' <= c && c <= 'F') || ('a' <= c && c <= 'f');
  251. }
  252. bool isHexDigit(const std::string& s)
  253. {
  254. for (const auto& c : s) {
  255. if(!isHexDigit(c)) {
  256. return false;
  257. }
  258. }
  259. return true;
  260. }
  261. bool inRFC3986ReservedChars(const char c)
  262. {
  263. static const char reserved[] = {
  264. ':' , '/' , '?' , '#' , '[' , ']' , '@',
  265. '!' , '$' , '&' , '\'' , '(' , ')',
  266. '*' , '+' , ',' , ';' , '=' };
  267. return std::find(std::begin(reserved), std::end(reserved), c)
  268. != std::end(reserved);
  269. }
  270. bool inRFC3986UnreservedChars(const char c)
  271. {
  272. static const char unreserved[] = { '-', '.', '_', '~' };
  273. return isAlpha(c) || isDigit(c) ||
  274. std::find(std::begin(unreserved), std::end(unreserved), c)
  275. != std::end(unreserved);
  276. }
  277. bool inRFC2978MIMECharset(const char c)
  278. {
  279. static const char chars[] = {
  280. '!', '#', '$', '%', '&',
  281. '\'', '+', '-', '^', '_',
  282. '`', '{', '}', '~'
  283. };
  284. return isAlpha(c) || isDigit(c) ||
  285. std::find(std::begin(chars), std::end(chars), c) != std::end(chars);
  286. }
  287. bool inRFC2616HttpToken(const char c)
  288. {
  289. static const char chars[] = {
  290. '!', '#', '$', '%', '&', '\'', '*', '+', '-', '.',
  291. '^', '_', '`', '|', '~'
  292. };
  293. return isAlpha(c) || isDigit(c) ||
  294. std::find(std::begin(chars), std::end(chars), c) != std::end(chars);
  295. }
  296. bool inRFC5987AttrChar(const char c)
  297. {
  298. return inRFC2616HttpToken(c) && c != '*' && c != '\'' && c != '%';
  299. }
  300. // Returns nonzero if |c| is in ISO/IEC 8859-1 character set.
  301. bool isIso8859p1(unsigned char c)
  302. {
  303. return (0x20u <= c && c <= 0x7eu) || 0xa0u <= c;
  304. }
  305. bool isLws(const char c)
  306. {
  307. return c == ' ' || c == '\t';
  308. }
  309. bool isCRLF(const char c)
  310. {
  311. return c == '\r' || c == '\n';
  312. }
  313. namespace {
  314. inline static
  315. bool isUtf8Tail(unsigned char ch)
  316. {
  317. return in(ch, 0x80u, 0xbfu);
  318. }
  319. inline static
  320. bool inPercentEncodeMini(const unsigned char c)
  321. {
  322. return c > 0x20 && c < 0x7fu &&
  323. // Chromium escapes following characters. Firefox4 escapes more.
  324. c != '"' && c != '<' && c != '>';
  325. }
  326. } // namespace
  327. bool isUtf8(const std::string& str)
  328. {
  329. for(std::string::const_iterator s = str.begin(), eos = str.end(); s != eos;
  330. ++s) {
  331. unsigned char firstChar = *s;
  332. // See ABNF in http://tools.ietf.org/search/rfc3629#section-4
  333. if(in(firstChar, 0x20u, 0x7eu) ||
  334. firstChar == 0x08u || // \b
  335. firstChar == 0x09u || // \t
  336. firstChar == 0x0au || // \n
  337. firstChar == 0x0cu || // \f
  338. firstChar == 0x0du // \r
  339. ) {
  340. // UTF8-1 (without ctrl chars)
  341. } else if(in(firstChar, 0xc2u, 0xdfu)) {
  342. // UTF8-2
  343. if(++s == eos || !isUtf8Tail(*s)) {
  344. return false;
  345. }
  346. } else if(0xe0u == firstChar) {
  347. // UTF8-3
  348. if(++s == eos || !in(static_cast<unsigned char>(*s), 0xa0u, 0xbfu) ||
  349. ++s == eos || !isUtf8Tail(*s)) {
  350. return false;
  351. }
  352. } else if(in(firstChar, 0xe1u, 0xecu) || in(firstChar, 0xeeu, 0xefu)) {
  353. // UTF8-3
  354. if(++s == eos || !isUtf8Tail(*s) ||
  355. ++s == eos || !isUtf8Tail(*s)) {
  356. return false;
  357. }
  358. } else if(0xedu == firstChar) {
  359. // UTF8-3
  360. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x80u, 0x9fu) ||
  361. ++s == eos || !isUtf8Tail(*s)) {
  362. return false;
  363. }
  364. } else if(0xf0u == firstChar) {
  365. // UTF8-4
  366. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x90u, 0xbfu) ||
  367. ++s == eos || !isUtf8Tail(*s) ||
  368. ++s == eos || !isUtf8Tail(*s)) {
  369. return false;
  370. }
  371. } else if(in(firstChar, 0xf1u, 0xf3u)) {
  372. // UTF8-4
  373. if(++s == eos || !isUtf8Tail(*s) ||
  374. ++s == eos || !isUtf8Tail(*s) ||
  375. ++s == eos || !isUtf8Tail(*s)) {
  376. return false;
  377. }
  378. } else if(0xf4u == firstChar) {
  379. // UTF8-4
  380. if(++s == eos || !in(static_cast<unsigned char>(*s), 0x80u, 0x8fu) ||
  381. ++s == eos || !isUtf8Tail(*s) ||
  382. ++s == eos || !isUtf8Tail(*s)) {
  383. return false;
  384. }
  385. } else {
  386. return false;
  387. }
  388. }
  389. return true;
  390. }
  391. std::string percentEncode(const unsigned char* target, size_t len)
  392. {
  393. std::string dest;
  394. for(size_t i = 0; i < len; ++i) {
  395. if(inRFC3986UnreservedChars(target[i])) {
  396. dest += target[i];
  397. } else {
  398. dest.append(fmt("%%%02X", target[i]));
  399. }
  400. }
  401. return dest;
  402. }
  403. std::string percentEncode(const std::string& target)
  404. {
  405. if (std::find_if_not(target.begin(), target.end(),
  406. inRFC3986UnreservedChars) == target.end()) {
  407. return target;
  408. }
  409. return percentEncode(reinterpret_cast<const unsigned char*>(target.c_str()),
  410. target.size());
  411. }
  412. std::string percentEncodeMini(const std::string& src)
  413. {
  414. if (std::find_if_not(src.begin(), src.end(), inPercentEncodeMini) ==
  415. src.end()) {
  416. return src;
  417. }
  418. std::string result;
  419. for (auto c: src) {
  420. if(!inPercentEncodeMini(c)) {
  421. result += fmt("%%%02X", static_cast<unsigned char>(c));
  422. } else {
  423. result += c;
  424. }
  425. }
  426. return result;
  427. }
  428. std::string torrentPercentEncode(const unsigned char* target, size_t len) {
  429. std::string dest;
  430. for(size_t i = 0; i < len; ++i) {
  431. if(isAlpha(target[i]) || isDigit(target[i])) {
  432. dest += target[i];
  433. } else {
  434. dest.append(fmt("%%%02X", target[i]));
  435. }
  436. }
  437. return dest;
  438. }
  439. std::string torrentPercentEncode(const std::string& target)
  440. {
  441. return torrentPercentEncode
  442. (reinterpret_cast<const unsigned char*>(target.c_str()), target.size());
  443. }
  444. std::string percentDecode
  445. (std::string::const_iterator first, std::string::const_iterator last)
  446. {
  447. std::string result;
  448. for(; first != last; ++first) {
  449. if(*first == '%') {
  450. if(first+1 != last && first+2 != last &&
  451. isHexDigit(*(first+1)) && isHexDigit(*(first+2))) {
  452. result += hexCharToUInt(*(first+1))*16+hexCharToUInt(*(first+2));
  453. first += 2;
  454. } else {
  455. result += *first;
  456. }
  457. } else {
  458. result += *first;
  459. }
  460. }
  461. return result;
  462. }
  463. std::string toHex(const unsigned char* src, size_t len) {
  464. std::string out(len*2, '\0');
  465. std::string::iterator o = out.begin();
  466. const unsigned char* last = src+len;
  467. for(const unsigned char* i = src; i != last; ++i) {
  468. *o = (*i >> 4);
  469. *(o+1) = (*i)&0x0fu;
  470. for(int j = 0; j < 2; ++j) {
  471. if(*o < 10) {
  472. *o += '0';
  473. } else {
  474. *o += 'a'-10;
  475. }
  476. ++o;
  477. }
  478. }
  479. return out;
  480. }
  481. std::string toHex(const char* src, size_t len)
  482. {
  483. return toHex(reinterpret_cast<const unsigned char*>(src), len);
  484. }
  485. std::string toHex(const std::string& src)
  486. {
  487. return toHex(reinterpret_cast<const unsigned char*>(src.c_str()), src.size());
  488. }
  489. unsigned int hexCharToUInt(unsigned char ch)
  490. {
  491. if('a' <= ch && ch <= 'f') {
  492. ch -= 'a';
  493. ch += 10;
  494. } else if('A' <= ch && ch <= 'F') {
  495. ch -= 'A';
  496. ch += 10;
  497. } else if('0' <= ch && ch <= '9') {
  498. ch -= '0';
  499. } else {
  500. ch = 255;
  501. }
  502. return ch;
  503. }
  504. std::string secfmt(time_t sec) {
  505. time_t tsec = sec;
  506. std::string str;
  507. if(sec >= 3600) {
  508. str = fmt("%" PRId64 "h", static_cast<int64_t>(sec/3600));
  509. sec %= 3600;
  510. }
  511. if(sec >= 60) {
  512. str += fmt("%dm", static_cast<int>(sec/60));
  513. sec %= 60;
  514. }
  515. if(sec || tsec == 0) {
  516. str += fmt("%ds", static_cast<int>(sec));
  517. }
  518. return str;
  519. }
  520. namespace {
  521. template<typename T, typename F>
  522. bool parseLong(T& res, F f, const std::string& s, int base)
  523. {
  524. if(s.empty()) {
  525. return false;
  526. }
  527. char* endptr;
  528. errno = 0;
  529. res = f(s.c_str(), &endptr, base);
  530. if(errno == ERANGE) {
  531. return false;
  532. }
  533. if(*endptr != '\0') {
  534. for(const char* i = endptr, *eoi = s.c_str()+s.size(); i < eoi; ++i) {
  535. if(!isspace(*i)) {
  536. return false;
  537. }
  538. }
  539. }
  540. return true;
  541. }
  542. } // namespace
  543. bool parseIntNoThrow(int32_t& res, const std::string& s, int base)
  544. {
  545. long int t;
  546. if(parseLong(t, strtol, s, base) &&
  547. t >= std::numeric_limits<int32_t>::min() &&
  548. t <= std::numeric_limits<int32_t>::max()) {
  549. res = t;
  550. return true;
  551. } else {
  552. return false;
  553. }
  554. }
  555. bool parseUIntNoThrow(uint32_t& res, const std::string& s, int base)
  556. {
  557. long int t;
  558. if(parseLong(t, strtol, s, base) &&
  559. t >= 0 &&
  560. t <= std::numeric_limits<int32_t>::max()) {
  561. res = t;
  562. return true;
  563. } else {
  564. return false;
  565. }
  566. }
  567. bool parseLLIntNoThrow(int64_t& res, const std::string& s, int base)
  568. {
  569. long long int t;
  570. if(parseLong(t, strtoll, s, base) &&
  571. t >= std::numeric_limits<int64_t>::min() &&
  572. t <= std::numeric_limits<int64_t>::max()) {
  573. res = t;
  574. return true;
  575. } else {
  576. return false;
  577. }
  578. }
  579. void parseIntSegments(SegList<int>& sgl, const std::string& src)
  580. {
  581. for(std::string::const_iterator i = src.begin(), eoi = src.end(); i != eoi;) {
  582. std::string::const_iterator j = std::find(i, eoi, ',');
  583. if(j == i) {
  584. ++i;
  585. continue;
  586. }
  587. std::string::const_iterator p = std::find(i, j, '-');
  588. if(p == j) {
  589. int a;
  590. if(parseIntNoThrow(a, std::string(i, j))) {
  591. sgl.add(a, a+1);
  592. } else {
  593. throw DL_ABORT_EX(fmt("Bad range %s", std::string(i, j).c_str()));
  594. }
  595. } else if(p == i || p+1 == j) {
  596. throw DL_ABORT_EX(fmt(MSG_INCOMPLETE_RANGE, std::string(i, j).c_str()));
  597. } else {
  598. int a, b;
  599. if(parseIntNoThrow(a, std::string(i, p)) &&
  600. parseIntNoThrow(b, (std::string(p+1, j)))) {
  601. sgl.add(a, b+1);
  602. } else {
  603. throw DL_ABORT_EX(fmt("Bad range %s", std::string(i, j).c_str()));
  604. }
  605. }
  606. if(j == eoi) {
  607. break;
  608. }
  609. i = j+1;
  610. }
  611. }
  612. namespace {
  613. void computeHeadPieces
  614. (std::vector<size_t>& indexes,
  615. const std::vector<std::shared_ptr<FileEntry> >& fileEntries,
  616. size_t pieceLength,
  617. int64_t head)
  618. {
  619. if(head == 0) {
  620. return;
  621. }
  622. for (const auto& fi: fileEntries) {
  623. if(fi->getLength() == 0) {
  624. continue;
  625. }
  626. const size_t lastIndex = (fi->getOffset() +
  627. std::min(head, fi->getLength()) - 1
  628. ) / pieceLength;
  629. for(size_t idx = fi->getOffset() / pieceLength; idx <= lastIndex; ++idx) {
  630. indexes.push_back(idx);
  631. }
  632. }
  633. }
  634. } // namespace
  635. namespace {
  636. void computeTailPieces
  637. (std::vector<size_t>& indexes,
  638. const std::vector<std::shared_ptr<FileEntry> >& fileEntries,
  639. size_t pieceLength,
  640. int64_t tail)
  641. {
  642. if(tail == 0) {
  643. return;
  644. }
  645. for (const auto& fi: fileEntries) {
  646. if(fi->getLength() == 0) {
  647. continue;
  648. }
  649. int64_t endOffset = fi->getLastOffset();
  650. size_t fromIndex = (endOffset - 1 - (std::min(tail, fi->getLength()) - 1)) /
  651. pieceLength;
  652. const size_t toIndex = (endOffset - 1) / pieceLength;
  653. while (fromIndex <= toIndex) {
  654. indexes.push_back(fromIndex++);
  655. }
  656. }
  657. }
  658. } // namespace
  659. void parsePrioritizePieceRange
  660. (std::vector<size_t>& result, const std::string& src,
  661. const std::vector<std::shared_ptr<FileEntry> >& fileEntries,
  662. size_t pieceLength,
  663. int64_t defaultSize)
  664. {
  665. std::vector<size_t> indexes;
  666. std::vector<Scip> parts;
  667. splitIter(src.begin(), src.end(), std::back_inserter(parts), ',', true);
  668. for (const auto& i: parts) {
  669. if(util::streq(i.first, i.second, "head")) {
  670. computeHeadPieces(indexes, fileEntries, pieceLength, defaultSize);
  671. }
  672. else if(util::startsWith(i.first, i.second, "head=")) {
  673. std::string sizestr(i.first + 5, i.second);
  674. computeHeadPieces(indexes, fileEntries, pieceLength,
  675. std::max((int64_t)0, getRealSize(sizestr)));
  676. }
  677. else if(util::streq(i.first, i.second, "tail")) {
  678. computeTailPieces(indexes, fileEntries, pieceLength, defaultSize);
  679. }
  680. else if(util::startsWith(i.first, i.second, "tail=")) {
  681. std::string sizestr(i.first + 5, i.second);
  682. computeTailPieces(indexes, fileEntries, pieceLength,
  683. std::max((int64_t)0, getRealSize(sizestr)));
  684. }
  685. else {
  686. throw DL_ABORT_EX(fmt("Unrecognized token %s",
  687. std::string(i.first, i.second).c_str()));
  688. }
  689. }
  690. std::sort(indexes.begin(), indexes.end());
  691. indexes.erase(std::unique(indexes.begin(), indexes.end()), indexes.end());
  692. result.insert(result.end(), indexes.begin(), indexes.end());
  693. }
  694. // Converts ISO/IEC 8859-1 string to UTF-8 string. If there is a
  695. // character not in ISO/IEC 8859-1, returns empty string.
  696. std::string iso8859p1ToUtf8(const char* src, size_t len)
  697. {
  698. std::string dest;
  699. for(const char* p = src, *last = src+len; p != last; ++p) {
  700. unsigned char c = *p;
  701. if(0xa0u <= c) {
  702. if(c <= 0xbfu) {
  703. dest += 0xc2u;
  704. } else {
  705. dest += 0xc3u;
  706. }
  707. dest += c&(~0x40u);
  708. } else if(0x80u <= c && c <= 0x9fu) {
  709. return "";
  710. } else {
  711. dest += c;
  712. }
  713. }
  714. return dest;
  715. }
  716. std::string iso8859p1ToUtf8(const std::string& src)
  717. {
  718. return iso8859p1ToUtf8(src.c_str(), src.size());
  719. }
  720. /* Start of utf8 dfa */
  721. /* Copyright (c) 2008-2010 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  722. * See http://bjoern.hoehrmann.de/utf-8/decoder/dfa/ for details.
  723. *
  724. * Copyright (c) 2008-2009 Bjoern Hoehrmann <bjoern@hoehrmann.de>
  725. *
  726. * Permission is hereby granted, free of charge, to any person
  727. * obtaining a copy of this software and associated documentation
  728. * files (the "Software"), to deal in the Software without
  729. * restriction, including without limitation the rights to use, copy,
  730. * modify, merge, publish, distribute, sublicense, and/or sell copies
  731. * of the Software, and to permit persons to whom the Software is
  732. * furnished to do so, subject to the following conditions:
  733. *
  734. * The above copyright notice and this permission notice shall be
  735. * included in all copies or substantial portions of the Software.
  736. *
  737. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
  738. * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
  739. * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
  740. * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
  741. * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
  742. * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
  743. * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
  744. * SOFTWARE.
  745. */
  746. #define UTF8_ACCEPT 0
  747. #define UTF8_REJECT 12
  748. static const uint8_t utf8d[] = {
  749. /*
  750. * The first part of the table maps bytes to character classes that
  751. * to reduce the size of the transition table and create bitmasks.
  752. */
  753. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  754. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  755. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  756. 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0, 0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,0,
  757. 1,1,1,1,1,1,1,1,1,1,1,1,1,1,1,1, 9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,9,
  758. 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7, 7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,7,
  759. 8,8,2,2,2,2,2,2,2,2,2,2,2,2,2,2, 2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,2,
  760. 10,3,3,3,3,3,3,3,3,3,3,3,3,4,3,3, 11,6,6,6,5,8,8,8,8,8,8,8,8,8,8,8,
  761. /*
  762. * The second part is a transition table that maps a combination
  763. * of a state of the automaton and a character class to a state.
  764. */
  765. 0,12,24,36,60,96,84,12,12,12,48,72, 12,12,12,12,12,12,12,12,12,12,12,12,
  766. 12, 0,12,12,12,12,12, 0,12, 0,12,12, 12,24,12,12,12,12,12,24,12,24,12,12,
  767. 12,12,12,12,12,12,12,24,12,12,12,12, 12,24,12,12,12,12,12,12,12,24,12,12,
  768. 12,12,12,12,12,12,12,36,12,36,12,12, 12,36,12,12,12,12,12,36,12,36,12,12,
  769. 12,36,12,12,12,12,12,12,12,12,12,12,
  770. };
  771. static uint32_t
  772. utf8dfa(uint32_t* state, uint32_t* codep, uint32_t byte) {
  773. uint32_t type = utf8d[byte];
  774. *codep = (*state != UTF8_ACCEPT) ?
  775. (byte & 0x3fu) | (*codep << 6) :
  776. (0xff >> type) & (byte);
  777. *state = utf8d[256 + *state + type];
  778. return *state;
  779. }
  780. /* End of utf8 dfa */
  781. typedef enum {
  782. CD_BEFORE_DISPOSITION_TYPE,
  783. CD_AFTER_DISPOSITION_TYPE,
  784. CD_DISPOSITION_TYPE,
  785. CD_BEFORE_DISPOSITION_PARM_NAME,
  786. CD_AFTER_DISPOSITION_PARM_NAME,
  787. CD_DISPOSITION_PARM_NAME,
  788. CD_BEFORE_VALUE,
  789. CD_AFTER_VALUE,
  790. CD_QUOTED_STRING,
  791. CD_TOKEN,
  792. CD_BEFORE_EXT_VALUE,
  793. CD_CHARSET,
  794. CD_LANGUAGE,
  795. CD_VALUE_CHARS,
  796. CD_VALUE_CHARS_PCT_ENCODED1,
  797. CD_VALUE_CHARS_PCT_ENCODED2
  798. } content_disposition_parse_state;
  799. typedef enum {
  800. CD_FILENAME_FOUND = 1,
  801. CD_EXT_FILENAME_FOUND = 1 << 1
  802. } content_disposition_parse_flag;
  803. typedef enum {
  804. CD_ENC_UNKNOWN,
  805. CD_ENC_UTF8,
  806. CD_ENC_ISO_8859_1
  807. } content_disposition_charset;
  808. ssize_t parse_content_disposition(char *dest, size_t destlen,
  809. const char **charsetp, size_t *charsetlenp,
  810. const char *in, size_t len)
  811. {
  812. const char *p = in, *eop = in + len, *mark_first = nullptr, *mark_last = nullptr;
  813. int state = CD_BEFORE_DISPOSITION_TYPE;
  814. int in_file_parm = 0;
  815. int flags = 0;
  816. int quoted_seen = 0;
  817. int charset = 0;
  818. /* To suppress warnings */
  819. char *dp = dest;
  820. size_t dlen = destlen;
  821. uint32_t dfa_state = 0;
  822. uint32_t dfa_code = 0;
  823. uint8_t pctval = 0;
  824. *charsetp = nullptr;
  825. *charsetlenp = 0;
  826. for(; p != eop; ++p) {
  827. switch(state) {
  828. case CD_BEFORE_DISPOSITION_TYPE:
  829. if(inRFC2616HttpToken(*p)) {
  830. state = CD_DISPOSITION_TYPE;
  831. } else if(!isLws(*p)) {
  832. return -1;
  833. }
  834. break;
  835. case CD_AFTER_DISPOSITION_TYPE:
  836. case CD_DISPOSITION_TYPE:
  837. if(*p == ';') {
  838. state = CD_BEFORE_DISPOSITION_PARM_NAME;
  839. } else if(isLws(*p)) {
  840. state = CD_AFTER_DISPOSITION_TYPE;
  841. } else if(state == CD_AFTER_DISPOSITION_TYPE ||
  842. !inRFC2616HttpToken(*p)) {
  843. return -1;
  844. }
  845. break;
  846. case CD_BEFORE_DISPOSITION_PARM_NAME:
  847. if(inRFC2616HttpToken(*p)) {
  848. mark_first = p;
  849. state = CD_DISPOSITION_PARM_NAME;
  850. } else if(!isLws(*p)) {
  851. return -1;
  852. }
  853. break;
  854. case CD_AFTER_DISPOSITION_PARM_NAME:
  855. case CD_DISPOSITION_PARM_NAME:
  856. if(*p == '=') {
  857. if(state == CD_DISPOSITION_PARM_NAME) {
  858. mark_last = p;
  859. }
  860. in_file_parm = 0;
  861. if(strieq(mark_first, mark_last, "filename*")) {
  862. if((flags & CD_EXT_FILENAME_FOUND) == 0) {
  863. in_file_parm = 1;
  864. } else {
  865. return -1;
  866. }
  867. state = CD_BEFORE_EXT_VALUE;
  868. } else if(strieq(mark_first, mark_last, "filename")) {
  869. if(flags & CD_FILENAME_FOUND) {
  870. return -1;
  871. }
  872. if((flags & CD_EXT_FILENAME_FOUND) == 0) {
  873. in_file_parm = 1;
  874. }
  875. state = CD_BEFORE_VALUE;
  876. } else {
  877. /* ext-token must be characters in token, followed by "*" */
  878. if(mark_first != mark_last-1 && *(mark_last-1) == '*') {
  879. state = CD_BEFORE_EXT_VALUE;
  880. } else {
  881. state = CD_BEFORE_VALUE;
  882. }
  883. }
  884. if(in_file_parm) {
  885. dp = dest;
  886. dlen = destlen;
  887. }
  888. } else if(isLws(*p)) {
  889. mark_last = p;
  890. state = CD_AFTER_DISPOSITION_PARM_NAME;
  891. } else if(state == CD_AFTER_DISPOSITION_PARM_NAME ||
  892. !inRFC2616HttpToken(*p)) {
  893. return -1;
  894. }
  895. break;
  896. case CD_BEFORE_VALUE:
  897. if(*p == '"') {
  898. quoted_seen = 0;
  899. state = CD_QUOTED_STRING;
  900. } else if(inRFC2616HttpToken(*p)) {
  901. if(in_file_parm) {
  902. if(dlen == 0) {
  903. return -1;
  904. } else {
  905. *dp++ = *p;
  906. --dlen;
  907. }
  908. }
  909. state = CD_TOKEN;
  910. } else if(!isLws(*p)) {
  911. return -1;
  912. }
  913. break;
  914. case CD_AFTER_VALUE:
  915. if(*p == ';') {
  916. state = CD_BEFORE_DISPOSITION_PARM_NAME;
  917. } else if(!isLws(*p)) {
  918. return -1;
  919. }
  920. break;
  921. case CD_QUOTED_STRING:
  922. if(*p == '\\' && quoted_seen == 0) {
  923. quoted_seen = 1;
  924. } else if(*p == '"' && quoted_seen == 0) {
  925. if(in_file_parm) {
  926. flags |= CD_FILENAME_FOUND;
  927. }
  928. state = CD_AFTER_VALUE;
  929. } else {
  930. /* TEXT which is OCTET except CTLs, but including LWS. We only
  931. accept ISO-8859-1 chars. */
  932. quoted_seen = 0;
  933. if(!isIso8859p1(*p)) {
  934. return -1;
  935. }
  936. if(in_file_parm) {
  937. if(dlen == 0) {
  938. return -1;
  939. } else {
  940. *dp++ = *p;
  941. --dlen;
  942. }
  943. }
  944. }
  945. break;
  946. case CD_TOKEN:
  947. if(inRFC2616HttpToken(*p)) {
  948. if(in_file_parm) {
  949. if(dlen == 0) {
  950. return -1;
  951. } else {
  952. *dp++ = *p;
  953. --dlen;
  954. }
  955. }
  956. } else if(*p == ';') {
  957. if(in_file_parm) {
  958. flags |= CD_FILENAME_FOUND;
  959. }
  960. state = CD_BEFORE_DISPOSITION_PARM_NAME;
  961. } else if(isLws(*p)) {
  962. if(in_file_parm) {
  963. flags |= CD_FILENAME_FOUND;
  964. }
  965. state = CD_AFTER_VALUE;
  966. } else {
  967. return -1;
  968. }
  969. break;
  970. case CD_BEFORE_EXT_VALUE:
  971. if(*p == '\'') {
  972. /* Empty charset is not allowed */
  973. return -1;
  974. } else if(inRFC2978MIMECharset(*p)) {
  975. mark_first = p;
  976. state = CD_CHARSET;
  977. } else if(!isLws(*p)) {
  978. return -1;
  979. }
  980. break;
  981. case CD_CHARSET:
  982. if(*p == '\'') {
  983. mark_last = p;
  984. *charsetp = mark_first;
  985. *charsetlenp = mark_last - mark_first;
  986. if(strieq(mark_first, mark_last, "utf-8")) {
  987. charset = CD_ENC_UTF8;
  988. dfa_state = UTF8_ACCEPT;
  989. dfa_code = 0;
  990. } else if(strieq(mark_first, mark_last, "iso-8859-1")) {
  991. charset = CD_ENC_ISO_8859_1;
  992. } else {
  993. charset = CD_ENC_UNKNOWN;
  994. }
  995. state = CD_LANGUAGE;
  996. } else if(!inRFC2978MIMECharset(*p)) {
  997. return -1;
  998. }
  999. break;
  1000. case CD_LANGUAGE:
  1001. if(*p == '\'') {
  1002. if(in_file_parm) {
  1003. dp = dest;
  1004. dlen = destlen;
  1005. }
  1006. state = CD_VALUE_CHARS;
  1007. } else if(*p != '-' && !isAlpha(*p) && !isDigit(*p)) {
  1008. return -1;
  1009. }
  1010. break;
  1011. case CD_VALUE_CHARS:
  1012. if(inRFC5987AttrChar(*p)) {
  1013. if(charset == CD_ENC_UTF8) {
  1014. if(utf8dfa(&dfa_state, &dfa_code, *p) == UTF8_REJECT) {
  1015. return -1;
  1016. }
  1017. }
  1018. if(in_file_parm) {
  1019. if(dlen == 0) {
  1020. return -1;
  1021. } else {
  1022. *dp++ = *p;
  1023. --dlen;
  1024. }
  1025. }
  1026. } else if(*p == '%') {
  1027. if(in_file_parm) {
  1028. if(dlen == 0) {
  1029. return -1;
  1030. }
  1031. }
  1032. pctval = 0;
  1033. state = CD_VALUE_CHARS_PCT_ENCODED1;
  1034. } else if(*p == ';' || isLws(*p)) {
  1035. if(charset == CD_ENC_UTF8 && dfa_state != UTF8_ACCEPT) {
  1036. return -1;
  1037. }
  1038. if(in_file_parm) {
  1039. flags |= CD_EXT_FILENAME_FOUND;
  1040. }
  1041. if(*p == ';') {
  1042. state = CD_BEFORE_DISPOSITION_PARM_NAME;
  1043. } else {
  1044. state = CD_AFTER_VALUE;
  1045. }
  1046. } else if(!inRFC5987AttrChar(*p)) {
  1047. return -1;
  1048. }
  1049. break;
  1050. case CD_VALUE_CHARS_PCT_ENCODED1:
  1051. if(isHexDigit(*p)) {
  1052. pctval |= hexCharToUInt(*p) << 4;
  1053. state = CD_VALUE_CHARS_PCT_ENCODED2;
  1054. } else {
  1055. return -1;
  1056. }
  1057. break;
  1058. case CD_VALUE_CHARS_PCT_ENCODED2:
  1059. if(isHexDigit(*p)) {
  1060. pctval |= hexCharToUInt(*p);
  1061. if(charset == CD_ENC_UTF8) {
  1062. if(utf8dfa(&dfa_state, &dfa_code, pctval) == UTF8_REJECT) {
  1063. return -1;
  1064. }
  1065. } else if(charset == CD_ENC_ISO_8859_1) {
  1066. if(!isIso8859p1(pctval)) {
  1067. return -1;
  1068. }
  1069. }
  1070. if(in_file_parm) {
  1071. *dp++ = pctval;
  1072. --dlen;
  1073. }
  1074. state = CD_VALUE_CHARS;
  1075. } else {
  1076. return -1;
  1077. }
  1078. break;
  1079. }
  1080. }
  1081. switch(state) {
  1082. case CD_BEFORE_DISPOSITION_TYPE:
  1083. case CD_AFTER_DISPOSITION_TYPE:
  1084. case CD_DISPOSITION_TYPE:
  1085. case CD_AFTER_VALUE:
  1086. case CD_TOKEN:
  1087. return destlen-dlen;
  1088. case CD_VALUE_CHARS:
  1089. if(charset == CD_ENC_UTF8 && dfa_state != UTF8_ACCEPT) {
  1090. return -1;
  1091. }
  1092. return destlen - dlen;
  1093. default:
  1094. return -1;
  1095. }
  1096. }
  1097. std::string getContentDispositionFilename(const std::string& header)
  1098. {
  1099. char cdval[1024];
  1100. size_t cdvallen = sizeof(cdval);
  1101. const char* charset;
  1102. size_t charsetlen;
  1103. ssize_t rv = parse_content_disposition(cdval, cdvallen,
  1104. &charset, &charsetlen,
  1105. header.c_str(), header.size());
  1106. if(rv == -1) {
  1107. return "";
  1108. }
  1109. std::string res;
  1110. if(!charset || strieq(charset, charset+charsetlen, "iso-8859-1")) {
  1111. res = iso8859p1ToUtf8(cdval, rv);
  1112. } else {
  1113. res.assign(cdval, rv);
  1114. }
  1115. if(!detectDirTraversal(res) &&
  1116. res.find_first_of("/\\") == std::string::npos) {
  1117. return res;
  1118. }
  1119. return "";
  1120. }
  1121. std::string toUpper(std::string src)
  1122. {
  1123. uppercase(src);
  1124. return src;
  1125. }
  1126. std::string toLower(std::string src) {
  1127. lowercase(src);
  1128. return src;
  1129. }
  1130. void uppercase(std::string& s)
  1131. {
  1132. std::transform(s.begin(), s.end(), s.begin(), toUpperChar);
  1133. }
  1134. void lowercase(std::string& s)
  1135. {
  1136. std::transform(s.begin(), s.end(), s.begin(), toLowerChar);
  1137. }
  1138. char toUpperChar(char c)
  1139. {
  1140. if('a' <= c && c <= 'z') {
  1141. c += 'A'-'a';
  1142. }
  1143. return c;
  1144. }
  1145. char toLowerChar(char c)
  1146. {
  1147. if('A' <= c && c <= 'Z') {
  1148. c += 'a'-'A';
  1149. }
  1150. return c;
  1151. }
  1152. bool isNumericHost(const std::string& name)
  1153. {
  1154. struct addrinfo hints;
  1155. struct addrinfo* res;
  1156. memset(&hints, 0, sizeof(hints));
  1157. hints.ai_family = AF_UNSPEC;
  1158. hints.ai_flags = AI_NUMERICHOST;
  1159. if(getaddrinfo(name.c_str(), nullptr, &hints, &res)) {
  1160. return false;
  1161. }
  1162. freeaddrinfo(res);
  1163. return true;
  1164. }
  1165. #if _WIN32
  1166. namespace {
  1167. static Lock win_signal_lock;
  1168. static signal_handler_t win_int_handler = nullptr;
  1169. static signal_handler_t win_term_handler = nullptr;
  1170. static void win_ign_handler(int) {}
  1171. static BOOL WINAPI HandlerRoutine(DWORD ctrlType)
  1172. {
  1173. void(*handler)(int) = nullptr;
  1174. switch (ctrlType) {
  1175. case CTRL_C_EVENT:
  1176. case CTRL_BREAK_EVENT:
  1177. {
  1178. // Handler will be called on a new/different thread.
  1179. LockGuard lg(win_signal_lock);
  1180. handler = win_int_handler;
  1181. }
  1182. if (handler) {
  1183. handler(SIGINT);
  1184. return TRUE;
  1185. }
  1186. return FALSE;
  1187. case CTRL_LOGOFF_EVENT:
  1188. case CTRL_CLOSE_EVENT:
  1189. case CTRL_SHUTDOWN_EVENT:
  1190. {
  1191. // Handler will be called on a new/different thread.
  1192. LockGuard lg(win_signal_lock);
  1193. handler = win_term_handler;;
  1194. }
  1195. if (handler) {
  1196. handler(SIGTERM);
  1197. return TRUE;
  1198. }
  1199. return FALSE;
  1200. }
  1201. return FALSE;
  1202. }
  1203. }
  1204. #endif
  1205. void setGlobalSignalHandler(int sig, sigset_t* mask, signal_handler_t handler,
  1206. int flags) {
  1207. #if _WIN32
  1208. if (sig == SIGINT || sig == SIGTERM) {
  1209. // Handler will be called on a new/different thread.
  1210. LockGuard lg(win_signal_lock);
  1211. if (handler == SIG_DFL) {
  1212. handler = nullptr;
  1213. }
  1214. else if (handler == SIG_IGN) {
  1215. handler = win_ign_handler;
  1216. }
  1217. // Not yet in use: add console handler.
  1218. if (handler && !win_int_handler && !win_term_handler) {
  1219. ::SetConsoleCtrlHandler(HandlerRoutine, TRUE);
  1220. }
  1221. if (sig == SIGINT) {
  1222. win_int_handler = handler;
  1223. }
  1224. else {
  1225. win_term_handler = handler;
  1226. }
  1227. // No handlers set: remove.
  1228. if (!win_int_handler && !win_term_handler) {
  1229. ::SetConsoleCtrlHandler(HandlerRoutine, FALSE);
  1230. }
  1231. return;
  1232. }
  1233. #endif
  1234. #ifdef HAVE_SIGACTION
  1235. struct sigaction sigact;
  1236. sigact.sa_handler = handler;
  1237. sigact.sa_flags = flags;
  1238. sigact.sa_mask = *mask;
  1239. sigaction(sig, &sigact, nullptr);
  1240. #else
  1241. signal(sig, handler);
  1242. #endif // HAVE_SIGACTION
  1243. }
  1244. std::string getHomeDir()
  1245. {
  1246. const char* p = getenv("HOME");
  1247. if (p) {
  1248. return p;
  1249. }
  1250. #ifdef __MINGW32__
  1251. p = getenv("USERPROFILE");
  1252. if (p) {
  1253. return p;
  1254. }
  1255. p = getenv("HOMEDRIVE");
  1256. if (p) {
  1257. std::string homeDir = p;
  1258. p = getenv("HOMEPATH");
  1259. if (p) {
  1260. homeDir += p;
  1261. return homeDir;
  1262. }
  1263. }
  1264. #elif HAVE_PWD_H
  1265. passwd* pw = getpwuid(geteuid());
  1266. if(pw && pw->pw_dir) {
  1267. return pw->pw_dir;
  1268. }
  1269. #endif // HAVE_PWD_H
  1270. return A2STR::NIL;
  1271. }
  1272. int64_t getRealSize(const std::string& sizeWithUnit)
  1273. {
  1274. std::string::size_type p = sizeWithUnit.find_first_of("KMkm");
  1275. std::string size;
  1276. int32_t mult = 1;
  1277. if(p == std::string::npos) {
  1278. size = sizeWithUnit;
  1279. } else {
  1280. switch(sizeWithUnit[p]) {
  1281. case 'K':
  1282. case 'k':
  1283. mult = 1024;
  1284. break;
  1285. case 'M':
  1286. case 'm':
  1287. mult = 1024*1024;
  1288. break;
  1289. }
  1290. size.assign(sizeWithUnit.begin(), sizeWithUnit.begin()+p);
  1291. }
  1292. int64_t v;
  1293. if(!parseLLIntNoThrow(v, size) || v < 0) {
  1294. throw DL_ABORT_EX(fmt("Bad or negative value detected: %s",
  1295. sizeWithUnit.c_str()));
  1296. }
  1297. if(INT64_MAX/mult < v) {
  1298. throw DL_ABORT_EX(fmt(MSG_STRING_INTEGER_CONVERSION_FAILURE,
  1299. "overflow/underflow"));
  1300. }
  1301. return v*mult;
  1302. }
  1303. std::string abbrevSize(int64_t size)
  1304. {
  1305. static const char* UNITS[] = { "", "Ki", "Mi", "Gi" };
  1306. int64_t t = size;
  1307. size_t uidx = 0;
  1308. int r = 0;
  1309. while(t >= 1024 && uidx+1 < sizeof(UNITS)/sizeof(UNITS[0])) {
  1310. lldiv_t d = lldiv(t, 1024);
  1311. t = d.quot;
  1312. r = d.rem;
  1313. ++uidx;
  1314. }
  1315. if(uidx+1 < sizeof(UNITS)/sizeof(UNITS[0]) && t >= 922) {
  1316. ++uidx;
  1317. r = t;
  1318. t = 0;
  1319. }
  1320. std::string res;
  1321. res += itos(t, true);
  1322. if(t < 10 && uidx > 0) {
  1323. res += ".";
  1324. res += itos(r*10/1024);
  1325. }
  1326. res += UNITS[uidx];
  1327. return res;
  1328. }
  1329. void sleep(long seconds) {
  1330. #if defined(HAVE_WINSOCK2_H)
  1331. ::Sleep(seconds * 1000);
  1332. #elif HAVE_SLEEP
  1333. ::sleep(seconds);
  1334. #elif defined(HAVE_USLEEP)
  1335. ::usleep(seconds * 1000000);
  1336. #else
  1337. # error no sleep function is available (nanosleep?)
  1338. #endif
  1339. }
  1340. void usleep(long microseconds) {
  1341. #ifdef HAVE_USLEEP
  1342. ::usleep(microseconds);
  1343. #elif defined(HAVE_WINSOCK2_H)
  1344. LARGE_INTEGER current, freq, end;
  1345. static enum {GET_FREQUENCY, GET_MICROSECONDS, SKIP_MICROSECONDS} state = GET_FREQUENCY;
  1346. if (state == GET_FREQUENCY) {
  1347. if (QueryPerformanceFrequency(&freq))
  1348. state = GET_MICROSECONDS;
  1349. else
  1350. state = SKIP_MICROSECONDS;
  1351. }
  1352. long msec = microseconds / 1000;
  1353. microseconds %= 1000;
  1354. if (state == GET_MICROSECONDS && microseconds) {
  1355. QueryPerformanceCounter(&end);
  1356. end.QuadPart += (freq.QuadPart * microseconds) / 1000000;
  1357. while (QueryPerformanceCounter(&current) && (current.QuadPart <= end.QuadPart))
  1358. /* noop */ ;
  1359. }
  1360. if (msec)
  1361. Sleep(msec);
  1362. #else
  1363. #error no usleep function is available (nanosleep?)
  1364. #endif
  1365. }
  1366. void mkdirs(const std::string& dirpath)
  1367. {
  1368. File dir(dirpath);
  1369. if(!dir.mkdirs()) {
  1370. int errNum = errno;
  1371. if(!dir.isDir()) {
  1372. throw DL_ABORT_EX3(errNum, fmt(EX_MAKE_DIR, dir.getPath().c_str(),
  1373. safeStrerror(errNum).c_str()),
  1374. error_code::DIR_CREATE_ERROR);
  1375. }
  1376. }
  1377. }
  1378. void convertBitfield(BitfieldMan* dest, const BitfieldMan* src)
  1379. {
  1380. size_t numBlock = dest->countBlock();
  1381. for(size_t index = 0; index < numBlock; ++index) {
  1382. if(src->isBitSetOffsetRange((int64_t)index*dest->getBlockLength(),
  1383. dest->getBlockLength())) {
  1384. dest->setBit(index);
  1385. }
  1386. }
  1387. }
  1388. std::string toString(const std::shared_ptr<BinaryStream>& binaryStream)
  1389. {
  1390. std::stringstream strm;
  1391. char data[2048];
  1392. while(1) {
  1393. int32_t dataLength = binaryStream->readData
  1394. (reinterpret_cast<unsigned char*>(data), sizeof(data), strm.tellp());
  1395. strm.write(data, dataLength);
  1396. if(dataLength == 0) {
  1397. break;
  1398. }
  1399. }
  1400. return strm.str();
  1401. }
  1402. #ifdef HAVE_POSIX_MEMALIGN
  1403. /**
  1404. * In linux 2.6, alignment and size should be a multiple of 512.
  1405. */
  1406. void* allocateAlignedMemory(size_t alignment, size_t size)
  1407. {
  1408. void* buffer;
  1409. int res;
  1410. if((res = posix_memalign(&buffer, alignment, size)) != 0) {
  1411. throw FATAL_EXCEPTION(fmt("Error in posix_memalign: %s",
  1412. util::safeStrerror(res).c_str()));
  1413. }
  1414. return buffer;
  1415. }
  1416. #endif // HAVE_POSIX_MEMALIGN
  1417. std::pair<std::string, uint16_t>
  1418. getNumericNameInfo(const struct sockaddr* sockaddr, socklen_t len)
  1419. {
  1420. char host[NI_MAXHOST];
  1421. char service[NI_MAXSERV];
  1422. int s = getnameinfo(sockaddr, len, host, NI_MAXHOST, service, NI_MAXSERV,
  1423. NI_NUMERICHOST|NI_NUMERICSERV);
  1424. if(s != 0) {
  1425. throw DL_ABORT_EX(fmt("Failed to get hostname and port. cause: %s",
  1426. gai_strerror(s)));
  1427. }
  1428. return std::pair<std::string, uint16_t>(host, atoi(service)); // TODO
  1429. }
  1430. std::string htmlEscape(const std::string& src)
  1431. {
  1432. std::string dest;
  1433. dest.reserve(src.size());
  1434. auto j = std::begin(src);
  1435. for(auto i = std::begin(src); i != std::end(src); ++i) {
  1436. char ch = *i;
  1437. const char *repl;
  1438. if(ch == '<') {
  1439. repl = "&lt;";
  1440. } else if(ch == '>') {
  1441. repl = "&gt;";
  1442. } else if(ch == '&') {
  1443. repl = "&amp;";
  1444. } else if(ch == '\'') {
  1445. repl = "&#39;";
  1446. } else if(ch == '"') {
  1447. repl = "&quot;";
  1448. } else {
  1449. continue;
  1450. }
  1451. dest.append(j, i);
  1452. j = i + 1;
  1453. dest += repl;
  1454. }
  1455. dest.append(j, std::end(src));
  1456. return dest;
  1457. }
  1458. std::pair<size_t, std::string>
  1459. parseIndexPath(const std::string& line)
  1460. {
  1461. auto p = divide(std::begin(line), std::end(line), '=');
  1462. uint32_t index;
  1463. if(!parseUIntNoThrow(index, std::string(p.first.first, p.first.second))) {
  1464. throw DL_ABORT_EX("Bad path index");
  1465. }
  1466. if(p.second.first == p.second.second) {
  1467. throw DL_ABORT_EX(fmt("Path with index=%u is empty.", index));
  1468. }
  1469. return std::make_pair(index, std::string(p.second.first, p.second.second));
  1470. }
  1471. std::vector<std::pair<size_t, std::string> > createIndexPaths(std::istream& i)
  1472. {
  1473. std::vector<std::pair<size_t, std::string> > indexPaths;
  1474. std::string line;
  1475. while(getline(i, line)) {
  1476. indexPaths.push_back(parseIndexPath(line));
  1477. }
  1478. return indexPaths;
  1479. }
  1480. namespace {
  1481. void generateRandomDataRandom(unsigned char* data, size_t length)
  1482. {
  1483. const auto& rd = SimpleRandomizer::getInstance();
  1484. rd->getRandomBytes(data, length);
  1485. }
  1486. } // namespace
  1487. namespace {
  1488. void generateRandomDataUrandom
  1489. (unsigned char* data, size_t length, std::ifstream& devUrand)
  1490. {
  1491. devUrand.read(reinterpret_cast<char*>(data), length);
  1492. }
  1493. } // namespace
  1494. void generateRandomData(unsigned char* data, size_t length)
  1495. {
  1496. #ifdef __MINGW32__
  1497. generateRandomDataRandom(data, length);
  1498. #else // !__MINGW32__
  1499. static int method = -1;
  1500. static std::ifstream devUrand;
  1501. if(method == 0) {
  1502. generateRandomDataUrandom(data, length, devUrand);
  1503. } else if(method == 1) {
  1504. generateRandomDataRandom(data, length);
  1505. } else {
  1506. devUrand.open("/dev/urandom");
  1507. if(devUrand) {
  1508. method = 0;
  1509. } else {
  1510. method = 1;
  1511. }
  1512. generateRandomData(data, length);
  1513. }
  1514. #endif // !__MINGW32__
  1515. }
  1516. bool saveAs
  1517. (const std::string& filename, const std::string& data, bool overwrite)
  1518. {
  1519. if(!overwrite && File(filename).exists()) {
  1520. return false;
  1521. }
  1522. std::string tempFilename = filename;
  1523. tempFilename += "__temp";
  1524. {
  1525. BufferedFile fp(tempFilename.c_str(), BufferedFile::WRITE);
  1526. if(!fp) {
  1527. return false;
  1528. }
  1529. if(fp.write(data.data(), data.size()) != data.size()) {
  1530. return false;
  1531. }
  1532. if(fp.close() == EOF) {
  1533. return false;
  1534. }
  1535. }
  1536. return File(tempFilename).renameTo(filename);
  1537. }
  1538. std::string applyDir(const std::string& dir, const std::string& relPath)
  1539. {
  1540. std::string s;
  1541. if(dir.empty()) {
  1542. s = "./";
  1543. s += relPath;
  1544. }
  1545. else {
  1546. s = dir;
  1547. if(dir == "/") {
  1548. s += relPath;
  1549. }
  1550. else {
  1551. s += "/";
  1552. s += relPath;
  1553. }
  1554. }
  1555. #ifdef __MINGW32__
  1556. for(std::string::iterator i = s.begin(), eoi = s.end(); i != eoi; ++i) {
  1557. if(*i == '\\') {
  1558. *i = '/';
  1559. }
  1560. }
  1561. #endif // __MINGW32__
  1562. return s;
  1563. }
  1564. std::string fixTaintedBasename(const std::string& src)
  1565. {
  1566. return escapePath(replace(src, "/", "%2F"));
  1567. }
  1568. void generateRandomKey(unsigned char* key)
  1569. {
  1570. #ifdef ENABLE_MESSAGE_DIGEST
  1571. unsigned char bytes[40];
  1572. generateRandomData(bytes, sizeof(bytes));
  1573. message_digest::digest(key, 20, MessageDigest::sha1().get(), bytes,
  1574. sizeof(bytes));
  1575. #else // !ENABLE_MESSAGE_DIGEST
  1576. generateRandomData(key, 20);
  1577. #endif // !ENABLE_MESSAGE_DIGEST
  1578. }
  1579. // Returns true is given numeric ipv4addr is in Private Address Space.
  1580. //
  1581. // From Section.3 RFC1918
  1582. // 10.0.0.0 - 10.255.255.255 (10/8 prefix)
  1583. // 172.16.0.0 - 172.31.255.255 (172.16/12 prefix)
  1584. // 192.168.0.0 - 192.168.255.255 (192.168/16 prefix)
  1585. bool inPrivateAddress(const std::string& ipv4addr)
  1586. {
  1587. if(util::startsWith(ipv4addr, "10.") ||
  1588. util::startsWith(ipv4addr, "192.168.")) {
  1589. return true;
  1590. }
  1591. if(util::startsWith(ipv4addr, "172.")) {
  1592. for(int i = 16; i <= 31; ++i) {
  1593. std::string t(fmt("%d.", i));
  1594. if(util::startsWith(ipv4addr.begin()+4, ipv4addr.end(),
  1595. t.begin(), t.end())) {
  1596. return true;
  1597. }
  1598. }
  1599. }
  1600. return false;
  1601. }
  1602. bool detectDirTraversal(const std::string& s)
  1603. {
  1604. if(s.empty()) {
  1605. return false;
  1606. }
  1607. for (auto c : s) {
  1608. unsigned char ch = c;
  1609. if (in(ch, 0x00u, 0x1fu) || ch == 0x7fu) {
  1610. return true;
  1611. }
  1612. }
  1613. return s == "." || s == ".." || s[0] == '/' ||
  1614. util::startsWith(s, "./") ||
  1615. util::startsWith(s, "../") ||
  1616. s.find("/../") != std::string::npos ||
  1617. s.find("/./") != std::string::npos ||
  1618. s[s.size()-1] == '/' ||
  1619. util::endsWith(s, "/.") ||
  1620. util::endsWith(s, "/..");
  1621. }
  1622. std::string escapePath(const std::string& s)
  1623. {
  1624. // We don't escape '/' because we use it as a path separator.
  1625. #ifdef __MINGW32__
  1626. static const char WIN_INVALID_PATH_CHARS[] =
  1627. { '"', '*', ':', '<', '>', '?', '\\', '|' };
  1628. #endif // __MINGW32__
  1629. std::string d;
  1630. for(auto cc: s) {
  1631. unsigned char c = cc;
  1632. if(in(c, 0x00u, 0x1fu) || c == 0x7fu
  1633. #ifdef __MINGW32__
  1634. || std::find(std::begin(WIN_INVALID_PATH_CHARS),
  1635. std::end(WIN_INVALID_PATH_CHARS),
  1636. c) != std::end(WIN_INVALID_PATH_CHARS)
  1637. #endif // __MINGW32__
  1638. ){
  1639. d += fmt("%%%02X", c);
  1640. } else {
  1641. d += c;
  1642. }
  1643. }
  1644. return d;
  1645. }
  1646. bool inSameCidrBlock
  1647. (const std::string& ip1, const std::string& ip2, size_t bits)
  1648. {
  1649. unsigned char s1[16], s2[16];
  1650. size_t len1, len2;
  1651. if((len1 = net::getBinAddr(s1, ip1)) == 0 ||
  1652. (len2 = net::getBinAddr(s2, ip2)) == 0 ||
  1653. len1 != len2) {
  1654. return false;
  1655. }
  1656. if(bits == 0) {
  1657. return true;
  1658. }
  1659. if(bits > 8*len1) {
  1660. bits = 8*len1;
  1661. }
  1662. int last = (bits-1)/8;
  1663. for(int i = 0; i < last; ++i) {
  1664. if(s1[i] != s2[i]) {
  1665. return false;
  1666. }
  1667. }
  1668. unsigned char mask = bitfield::lastByteMask(bits);
  1669. return (s1[last] & mask) == (s2[last] & mask);
  1670. }
  1671. namespace {
  1672. void executeHook
  1673. (const std::string& command,
  1674. a2_gid_t gid,
  1675. size_t numFiles,
  1676. const std::string& firstFilename)
  1677. {
  1678. const std::string gidStr = GroupId::toHex(gid);
  1679. const std::string numFilesStr = util::uitos(numFiles);
  1680. #ifndef __MINGW32__
  1681. A2_LOG_INFO(fmt("Executing user command: %s %s %s %s",
  1682. command.c_str(),
  1683. gidStr.c_str(),
  1684. numFilesStr.c_str(),
  1685. firstFilename.c_str()));
  1686. pid_t cpid = fork();
  1687. if (cpid > 0) {
  1688. // child!
  1689. execlp(command.c_str(),
  1690. command.c_str(),
  1691. gidStr.c_str(),
  1692. numFilesStr.c_str(),
  1693. firstFilename.c_str(),
  1694. reinterpret_cast<char*>(0));
  1695. perror(("Could not execute user command: "+command).c_str());
  1696. _exit(EXIT_FAILURE);
  1697. return;
  1698. }
  1699. if(cpid == -1) {
  1700. A2_LOG_ERROR("fork() failed. Cannot execute user command.");
  1701. }
  1702. return;
  1703. #else // __MINGW32__
  1704. PROCESS_INFORMATION pi;
  1705. STARTUPINFOW si;
  1706. memset(&si, 0, sizeof (si));
  1707. si.cb = sizeof(STARTUPINFO);
  1708. memset(&pi, 0, sizeof (pi));
  1709. bool batch = util::iendsWith(command, ".bat");
  1710. std::string cmdline;
  1711. std::string cmdexe;
  1712. // XXX batch handling, in particular quoting, correct?
  1713. if(batch) {
  1714. const char* p = getenv("windir");
  1715. if(p) {
  1716. cmdexe = p;
  1717. cmdexe += "\\system32\\cmd.exe";
  1718. } else {
  1719. A2_LOG_INFO("Failed to get windir environment variable."
  1720. " Executing batch file will fail.");
  1721. // TODO Might be useless.
  1722. cmdexe = "cmd.exe";
  1723. }
  1724. cmdline += "/C \"";
  1725. }
  1726. cmdline += "\"";
  1727. cmdline += command;
  1728. cmdline += "\"";
  1729. cmdline += " ";
  1730. cmdline += gidStr;
  1731. cmdline += " ";
  1732. cmdline += numFilesStr;
  1733. cmdline += " \"";
  1734. cmdline += firstFilename;
  1735. cmdline += "\"";
  1736. if(batch) {
  1737. cmdline += "\"";
  1738. }
  1739. int cmdlineLen = utf8ToWChar(0, 0, cmdline.c_str());
  1740. assert(cmdlineLen > 0);
  1741. auto wcharCmdline = std::unique_ptr<wchar_t[]>(new wchar_t[cmdlineLen]);
  1742. cmdlineLen = utf8ToWChar(wcharCmdline.get(), cmdlineLen, cmdline.c_str());
  1743. assert(cmdlineLen > 0);
  1744. A2_LOG_INFO(fmt("Executing user command: %s", cmdline.c_str()));
  1745. DWORD rc = CreateProcessW(batch ? utf8ToWChar(cmdexe).c_str() : nullptr,
  1746. wcharCmdline.get(),
  1747. nullptr,
  1748. nullptr,
  1749. true,
  1750. 0,
  1751. nullptr,
  1752. 0,
  1753. &si,
  1754. &pi);
  1755. if(!rc) {
  1756. A2_LOG_ERROR("CreateProcess() failed. Cannot execute user command.");
  1757. }
  1758. return;
  1759. #endif
  1760. }
  1761. } // namespace
  1762. void executeHookByOptName
  1763. (const std::shared_ptr<RequestGroup>& group, const Option* option,
  1764. PrefPtr pref)
  1765. {
  1766. executeHookByOptName(group.get(), option, pref);
  1767. }
  1768. void executeHookByOptName
  1769. (const RequestGroup* group, const Option* option, PrefPtr pref)
  1770. {
  1771. const std::string& cmd = option->get(pref);
  1772. if(!cmd.empty()) {
  1773. const std::shared_ptr<DownloadContext> dctx = group->getDownloadContext();
  1774. std::string firstFilename;
  1775. size_t numFiles = 0;
  1776. if(!group->inMemoryDownload()) {
  1777. std::shared_ptr<FileEntry> file = dctx->getFirstRequestedFileEntry();
  1778. if(file) {
  1779. firstFilename = file->getPath();
  1780. }
  1781. numFiles = dctx->countRequestedFileEntry();
  1782. }
  1783. executeHook(cmd, group->getGID(), numFiles, firstFilename);
  1784. }
  1785. }
  1786. std::string createSafePath
  1787. (const std::string& dir, const std::string& filename)
  1788. {
  1789. return util::applyDir(dir, util::isUtf8(filename) ?
  1790. util::fixTaintedBasename(filename) :
  1791. util::escapePath(util::percentEncode(filename))
  1792. );
  1793. }
  1794. std::string encodeNonUtf8(const std::string& s)
  1795. {
  1796. return util::isUtf8(s)?s:util::percentEncode(s);
  1797. }
  1798. std::string makeString(const char* str)
  1799. {
  1800. if(!str) {
  1801. return A2STR::NIL;
  1802. }
  1803. return str;
  1804. }
  1805. std::string safeStrerror(int errNum)
  1806. {
  1807. return makeString(strerror(errNum));
  1808. }
  1809. bool noProxyDomainMatch
  1810. (const std::string& hostname,
  1811. const std::string& domain)
  1812. {
  1813. if(!domain.empty() && domain[0] == '.' && !util::isNumericHost(hostname)) {
  1814. return util::endsWith(hostname, domain);
  1815. }
  1816. return hostname == domain;
  1817. }
  1818. bool tlsHostnameMatch(const std::string& pattern, const std::string& hostname)
  1819. {
  1820. std::string::const_iterator ptWildcard = std::find(pattern.begin(),
  1821. pattern.end(),
  1822. '*');
  1823. if(ptWildcard == pattern.end()) {
  1824. return strieq(pattern.begin(), pattern.end(),
  1825. hostname.begin(), hostname.end());
  1826. }
  1827. std::string::const_iterator ptLeftLabelEnd = std::find(pattern.begin(),
  1828. pattern.end(),
  1829. '.');
  1830. bool wildcardEnabled = true;
  1831. // Do case-insensitive match. At least 2 dots are required to enable
  1832. // wildcard match. Also wildcard must be in the left-most label.
  1833. // Don't attempt to match a presented identifier where the wildcard
  1834. // character is embedded within an A-label.
  1835. if(ptLeftLabelEnd == pattern.end() ||
  1836. std::find(ptLeftLabelEnd+1, pattern.end(), '.') == pattern.end() ||
  1837. ptLeftLabelEnd < ptWildcard ||
  1838. istartsWith(pattern, "xn--")) {
  1839. wildcardEnabled = false;
  1840. }
  1841. if(!wildcardEnabled) {
  1842. return strieq(pattern.begin(), pattern.end(),
  1843. hostname.begin(), hostname.end());
  1844. }
  1845. std::string::const_iterator hnLeftLabelEnd = std::find(hostname.begin(),
  1846. hostname.end(),
  1847. '.');
  1848. if(!strieq(ptLeftLabelEnd, pattern.end(), hnLeftLabelEnd, hostname.end())) {
  1849. return false;
  1850. }
  1851. // Perform wildcard match. Here '*' must match at least one
  1852. // character.
  1853. if(hnLeftLabelEnd - hostname.begin() < ptLeftLabelEnd - pattern.begin()) {
  1854. return false;
  1855. }
  1856. return istartsWith(hostname.begin(), hnLeftLabelEnd, pattern.begin(),
  1857. ptWildcard) &&
  1858. iendsWith(hostname.begin(), hnLeftLabelEnd, ptWildcard + 1,
  1859. ptLeftLabelEnd);
  1860. }
  1861. bool strieq(const std::string& a, const char* b)
  1862. {
  1863. return strieq(a.begin(), a.end(), b);
  1864. }
  1865. bool strieq(const std::string& a, const std::string& b)
  1866. {
  1867. return strieq(a.begin(), a.end(), b.begin(), b.end());
  1868. }
  1869. bool startsWith(const std::string& a, const char* b)
  1870. {
  1871. return startsWith(a.begin(), a.end(), b);
  1872. }
  1873. bool startsWith(const std::string& a, const std::string& b)
  1874. {
  1875. return startsWith(a.begin(), a.end(), b.begin(), b.end());
  1876. }
  1877. bool istartsWith(const std::string& a, const char* b)
  1878. {
  1879. return istartsWith(a.begin(), a.end(), b);
  1880. }
  1881. bool endsWith(const std::string& a, const char* b)
  1882. {
  1883. return endsWith(a.begin(), a.end(), b, b+strlen(b));
  1884. }
  1885. bool endsWith(const std::string& a, const std::string& b)
  1886. {
  1887. return endsWith(a.begin(), a.end(), b.begin(), b.end());
  1888. }
  1889. bool iendsWith(const std::string& a, const char* b)
  1890. {
  1891. return iendsWith(a.begin(), a.end(), b, b+strlen(b));
  1892. }
  1893. bool iendsWith(const std::string& a, const std::string& b)
  1894. {
  1895. return iendsWith(a.begin(), a.end(), b.begin(), b.end());
  1896. }
  1897. bool strless(const char* a, const char* b)
  1898. {
  1899. return strcmp(a, b) < 0;
  1900. }
  1901. } // namespace util
  1902. } // namespace aria2