Util.hpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899
  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. #ifndef __UTIL_H__
  20. #define __UTIL_H__
  21. #include <string>
  22. #include <sstream>
  23. #include <iostream>
  24. #include <vector>
  25. #include <list>
  26. #include <boost/asio.hpp>
  27. class UnresolvableCertificateException : public std::exception {
  28. public:
  29. virtual const char* what() const throw() {
  30. return "Could not resolve common name...";
  31. }
  32. };
  33. class Util {
  34. public:
  35. template <class T>
  36. static bool fromString(T& t,
  37. std::string& s,
  38. std::ios_base& (*f)(std::ios_base&))
  39. {
  40. trimString(s);
  41. std::istringstream iss(s);
  42. return !(iss >> f >> t).fail();
  43. }
  44. static void trimString( std::string& str) {
  45. size_t startpos = str.find_first_not_of(" \t\r\n");
  46. size_t endpos = str.find_last_not_of(" \t\r\n");
  47. if(( std::string::npos == startpos ) || ( std::string::npos == endpos))
  48. str = "";
  49. else
  50. str = str.substr( startpos, endpos-startpos+1 );
  51. }
  52. static void tokenizeString(std::string &str,
  53. std::string &delimiters,
  54. std::vector<std::string> &tokens)
  55. {
  56. std::string::size_type lastPos = str.find_first_not_of(delimiters, 0);
  57. std::string::size_type pos = str.find_first_of(delimiters, lastPos);
  58. while (std::string::npos != pos || std::string::npos != lastPos) {
  59. tokens.push_back(str.substr(lastPos, pos - lastPos));
  60. lastPos = str.find_first_not_of(delimiters, pos);
  61. pos = str.find_first_of(delimiters, lastPos);
  62. }
  63. if (lastPos != std::string::npos)
  64. tokens.push_back(str.substr(lastPos));
  65. }
  66. static void resolveName(std::string &name, std::list<boost::asio::ip::address> &results) {
  67. boost::asio::io_service io_service;
  68. boost::asio::ip::tcp::resolver resolver(io_service);
  69. boost::asio::ip::tcp::resolver::query query(name, "https");
  70. boost::asio::ip::tcp::resolver::iterator endpoint_iterator = resolver.resolve(query);
  71. boost::asio::ip::tcp::resolver::iterator end;
  72. while (endpoint_iterator != end) {
  73. // std::cout << "Resolved To: " << (*endpoint_iterator).endpoint().address().to_string() << std::endl;
  74. results.push_back((*endpoint_iterator++).endpoint().address());
  75. }
  76. if (results.empty()) throw UnresolvableCertificateException();
  77. }
  78. };
  79. #endif