FirefoxUpdater.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112
  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 "FirefoxUpdater.hpp"
  20. #include "UpdateManager.hpp"
  21. #include "Logger.hpp"
  22. #include <iostream>
  23. #include <fstream>
  24. #define BUILD_ID "2010011112"
  25. #define EMPTY_RESPONSE "HTTP/1.0 200 OK\r\nServer: Apache/2.0\r\nAccept-Ranges: bytes\r\nContent-Length: 42\r\nConnection: close\r\nContent-Type: text/plain; charset=iso-8859-1\r\n\r\n<?xml version=\"1.0\"?><updates></updates>"
  26. using namespace boost::asio;
  27. ip::tcp::endpoint FirefoxUpdater::getRemoteEndpoint() {
  28. return destination;
  29. }
  30. bool FirefoxUpdater::parseMetaRequest(std::string &request) {
  31. std::vector<std::string> tokens;
  32. std::string delimiters("/");
  33. Util::tokenizeString(request, delimiters, tokens);
  34. if (tokens.size() < 9) return false;
  35. this->product = tokens.at(2);
  36. this->version = tokens.at(3);
  37. this->buildId = tokens.at(4);
  38. this->buildTarget = tokens.at(5);
  39. this->locale = tokens.at(6);
  40. this->channel = tokens.at(7);
  41. this->filename = tokens.at(8);
  42. Logger::logUpdateRequest(product, version, buildId, buildTarget,
  43. locale, channel, filename);
  44. return true;
  45. }
  46. void FirefoxUpdater::sendMetaUpdateResponse() {
  47. if (UpdateManager::getInstance()->isUpdatableRequest(buildId, buildTarget)) {
  48. std::string path;
  49. std::string line;
  50. UpdateManager::getInstance()->getUpdatePath(buildTarget, path);
  51. std::ifstream xmlDescriptor(path.c_str(), std::ios::in);
  52. if( !xmlDescriptor )
  53. throw FirefoxUpdateException();
  54. int size = boost::filesystem::file_size(path);
  55. std::ostringstream headerStream;
  56. headerStream << "HTTP/1.0 200 OK\r\n"
  57. << "Server: Apache/2.0\r\n"
  58. << "Accept-Ranges: bytes\r\n"
  59. << "Content-Length: " << size << "\r\n"
  60. << "Connection: close\r\n"
  61. << "Content-Type: text/plain; charset=iso-8859-1\r\n\r\n";
  62. std::string header = headerStream.str();
  63. SSL_write(clientSession, header.c_str(), header.length());
  64. while(getline(xmlDescriptor,line))
  65. SSL_write(clientSession, line.c_str(), line.length());
  66. return;
  67. }
  68. std::string emptyResponse(EMPTY_RESPONSE);
  69. SSL_write(clientSession, emptyResponse.c_str(), emptyResponse.size());
  70. }
  71. void FirefoxUpdater::readMetaUpdateRequest() {
  72. char buf[4096];
  73. int bytesRead;
  74. while ((bytesRead = SSL_read(clientSession, buf, sizeof(buf))) >= 0) {
  75. if (headers.process(buf, bytesRead)) {
  76. std::string &request = headers.getRequest();
  77. if (parseMetaRequest(request)) return;
  78. else throw FirefoxUpdateException();
  79. }
  80. }
  81. }
  82. void FirefoxUpdater::close() {
  83. if (closed) return;
  84. else closed = true;
  85. SSL_free(clientSession);
  86. clientSocket->close();
  87. }