loadsettings.cc 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // -*- mode: c++; tab-width: 4; indent-tabs-mode: t; eval: (progn (c-set-style "stroustrup") (c-set-offset 'innamespace 0)); -*-
  2. // vi:set ts=4 sts=4 sw=4 noet :
  3. //
  4. // Copyright 2010, 2012 wkhtmltopdf authors
  5. //
  6. // This file is part of wkhtmltopdf.
  7. //
  8. // wkhtmltopdf is free software: you can redistribute it and/or modify
  9. // it under the terms of the GNU Lesser General Public License as published by
  10. // the Free Software Foundation, either version 3 of the License, or
  11. // (at your option) any later version.
  12. //
  13. // wkhtmltopdf is distributed in the hope that it will be useful,
  14. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  15. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  16. // GNU General Public License for more details.
  17. //
  18. // You should have received a copy of the GNU Lesser General Public License
  19. // along with wkhtmltopdf. If not, see <http://www.gnu.org/licenses/>.
  20. #ifdef _MSC_VER
  21. #define strcasecmp _stricmp
  22. #endif
  23. #include "loadsettings.hh"
  24. #include <QMap>
  25. #include <stdexcept>
  26. namespace wkhtmltopdf {
  27. namespace settings {
  28. QList<QString> LoadPage::mediaFilesExtensions = QList<QString> ()
  29. << "css"
  30. << "js"
  31. << "png"
  32. << "jpg"
  33. << "jpeg"
  34. << "gif";
  35. LoadPage::LoadErrorHandling strToLoadErrorHandling(const char * s, bool * ok) {
  36. if (ok) *ok = true;
  37. if (!strcasecmp(s, "abort")) return LoadPage::abort;
  38. if (!strcasecmp(s, "skip")) return LoadPage::skip;
  39. if (!strcasecmp(s, "ignore")) return LoadPage::ignore;
  40. *ok = false;
  41. return LoadPage::abort;
  42. }
  43. QString loadErrorHandlingToStr(LoadPage::LoadErrorHandling leh) {
  44. switch (leh) {
  45. case LoadPage::abort: return "abort";
  46. case LoadPage::skip: return "skip";
  47. case LoadPage::ignore: return "ignore";
  48. }
  49. throw std::logic_error("Internal error in loadErrorHandlingToStr");
  50. }
  51. /*!
  52. Read proxy settings from a string, the grammar is described in the manual
  53. \param proxy the proxy string to parse
  54. \param ok If supplied indicates whether the proxy was valid
  55. */
  56. Proxy strToProxy(const char * proxy, bool * ok) {
  57. Proxy p;
  58. if (ok) *ok=true;
  59. //Allow users to use no proxy, even if one is specified in the env
  60. if (!strcmp(proxy,"none")) {
  61. p.host = "";
  62. return p;
  63. }
  64. p.type = QNetworkProxy::HttpProxy;
  65. //Read proxy type bit "http://" or "socks5://"
  66. if (!strncmp(proxy,"http://",7)) {
  67. proxy += 7;
  68. } else if (!strncmp(proxy,"socks5://",9)) {
  69. p.type = QNetworkProxy::Socks5Proxy;
  70. proxy += 9;
  71. }
  72. //Read username and password
  73. char * val = (char *) strchr(proxy,'@');
  74. p.user = p.password = "";
  75. if (val != NULL) {
  76. p.user = QString(proxy).left(val-proxy);
  77. proxy = val+1;
  78. int idx = p.user.indexOf(':');
  79. if (idx != -1) {
  80. p.password = p.user.mid(idx+1);
  81. p.user = p.user.left(idx);
  82. }
  83. }
  84. //Read hostname and port
  85. val = (char *) strchr(proxy,':');
  86. p.port = 1080; //Default proxy port
  87. if (val == NULL) p.host = proxy;
  88. else {
  89. p.port = QString(val+1).toInt(ok);
  90. if (p.port < 0 || p.port > 65535) {
  91. p.port = 1080;
  92. *ok = false;
  93. }
  94. p.host = QString(proxy).left(val-proxy);
  95. }
  96. if (ok && p.host.size() == 0) *ok = false;
  97. return p;
  98. }
  99. QString proxyToStr(const Proxy & p) {
  100. QString res="";
  101. if (p.type == QNetworkProxy::HttpProxy)
  102. res += "http://";
  103. else if (p.type == QNetworkProxy::Socks5Proxy)
  104. res += "socks5://";
  105. if (!p.user.isEmpty()) {
  106. res += "@" + p.user;
  107. if (!p.password.isEmpty()) res += ":" + p.password;
  108. }
  109. res += p.host;
  110. if (!p.host.isEmpty()) res += ":" + p.port;
  111. return res;
  112. }
  113. Proxy::Proxy():
  114. type(QNetworkProxy::NoProxy),
  115. port(-1),
  116. host(),
  117. user(),
  118. password() {}
  119. LoadGlobal::LoadGlobal():
  120. cookieJar("") {}
  121. LoadPage::LoadPage():
  122. jsdelay(200),
  123. windowStatus(""),
  124. cacheDir(""),
  125. zoomFactor(1.0),
  126. repeatCustomHeaders(false),
  127. blockLocalFileAccess(false),
  128. stopSlowScripts(true),
  129. debugJavascript(false),
  130. loadErrorHandling(abort),
  131. mediaLoadErrorHandling(ignore) {};
  132. }
  133. }