FirefoxAddonUpdater.cpp 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. /*
  2. * Copyright (c) 2002-2009 Moxie Marlinspike
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of the GNU General Public License as
  6. * published by the Free Software Foundation; either version 3 of the
  7. * License, or (at your option) any later version.
  8. *
  9. * This program is distributed in the hope that it will be useful, but
  10. * WITHOUT ANY WARRANTY; without even the implied warranty of
  11. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. * General Public License for more details.
  13. *
  14. * You should have received a copy of the GNU General Public License
  15. * along with this program; if not, write to the Free Software
  16. * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307
  17. * USA
  18. */
  19. #include "FirefoxAddonUpdater.hpp"
  20. #include "UpdateManager.hpp"
  21. #include "Logger.hpp"
  22. std::string FirefoxAddonUpdater::incrementVersion(std::string &version) {
  23. int dot = version.rfind(".");
  24. std::string prefix;
  25. std::string postfix;
  26. int postfixValue;
  27. if (dot == std::string::npos) {
  28. prefix = "";
  29. postfix = version;
  30. } else {
  31. prefix = version.substr(0, dot+1);
  32. postfix = version.substr(dot+1);
  33. }
  34. std::istringstream postStream(postfix);
  35. postStream >> postfixValue;
  36. postfixValue++;
  37. std::ostringstream incrementedVersion;
  38. incrementedVersion << prefix << postfixValue;
  39. return incrementedVersion.str();
  40. }
  41. std::string FirefoxAddonUpdater::truncateVersion(std::string &version) {
  42. int dot = version.rfind(".");
  43. if (dot == std::string::npos || dot+1 == version.length()) {
  44. return version;
  45. } else {
  46. return version.substr(0, dot);
  47. }
  48. }
  49. void FirefoxAddonUpdater::sendMetaUpdateResponse() {
  50. std::string path = UpdateManager::getInstance()->getAddonPath();
  51. std::string sha = UpdateManager::getInstance()->getAddonHash();
  52. std::string version = requestArguments["version"];
  53. std::string newVersion = incrementVersion(version);
  54. std::string appVersion = requestArguments["appVersion"];
  55. std::string minVersion = truncateVersion(appVersion);
  56. Logger::logAddonUpdate(requestArguments["id"]);
  57. std::ostringstream headerStream;
  58. std::ostringstream responseStream;
  59. responseStream << "<?xml version=\"1.0\"?>"
  60. << "<RDF:RDF xmlns:RDF=\"http://www.w3.org/1999/02/22-rdf-syntax-ns#\" "
  61. << "xmlns:em=\"http://www.mozilla.org/2004/em-rdf#\">"
  62. << "<RDF:Description about=\"urn:mozilla:extension:" << requestArguments["id"] << "\">"
  63. << "<em:updates><RDF:Seq><RDF:li resource=\"urn:mozilla:extension:" << requestArguments["id"] << ":"
  64. << newVersion << "\"/>"
  65. << "</RDF:Seq></em:updates></RDF:Description>"
  66. << "<RDF:Description about=\"urn:mozilla:extension:" << requestArguments["id"] << ":" << newVersion << "\">"
  67. << "<em:version>" << newVersion << "</em:version>"
  68. << "<em:targetApplication><RDF:Description><em:id>" << requestArguments["appID"] << "</em:id>"
  69. << "<em:minVersion>" << minVersion << "</em:minVersion>"
  70. << "<em:maxVersion>" << requestArguments["maxAppVersion"] << "</em:maxVersion>"
  71. << "<em:updateLink>" << path << "</em:updateLink>"
  72. << "<em:updateInfoURL>http://www.thoughtcrime.org</em:updateInfoURL>"
  73. << "<em:updateHash>sha256:" << sha << "</em:updateHash>"
  74. << "</RDF:Description></em:targetApplication></RDF:Description></RDF:RDF>";
  75. std::string response = responseStream.str();
  76. headerStream << "HTTP/1.0 200 OK\r\n"
  77. << "Server: Apache/2.0\r\n"
  78. << "Accept-Ranges: bytes\r\n"
  79. << "Content-Length: " << response.length() << "\r\n"
  80. << "Connection: close\r\n"
  81. << "Content-Type: text/plain; charset=iso-8859-1\r\n\r\n";
  82. std::string header = headerStream.str();
  83. SSL_write(clientSession, header.c_str(), header.length());
  84. SSL_write(clientSession, response.c_str(), response.length());
  85. }
  86. bool FirefoxAddonUpdater::parseArguments(std::string &request, std::vector<std::string> &arguments) {
  87. int questionIndex = request.find('?');
  88. if (questionIndex == std::string::npos)
  89. return false;
  90. if (questionIndex+1 == request.length())
  91. return false;
  92. std::string argumentString = request.substr(questionIndex+1);
  93. std::string delimiters("&");
  94. Util::tokenizeString(argumentString, delimiters, arguments);
  95. return true;
  96. }
  97. bool FirefoxAddonUpdater::parseMetaRequest(std::string &request) {
  98. std::vector<std::string> arguments;
  99. if (!parseArguments(request, arguments))
  100. return false;
  101. if (arguments.size() < 10)
  102. return false;
  103. std::vector<std::string>::iterator iter;
  104. for (iter = arguments.begin(); iter != arguments.end(); iter++) {
  105. std::string argument = *iter;
  106. int equalsIndex = argument.find('=');
  107. if (equalsIndex == std::string::npos ||
  108. equalsIndex+1 == argument.length())
  109. continue;
  110. std::string key = argument.substr(0, equalsIndex);
  111. std::string value = argument.substr(equalsIndex+1);
  112. requestArguments[key] = value;
  113. }
  114. return true;
  115. }