a simple multithreaded webserver
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

78 lines
1.9 KiB

  1. #include "url_parser.hh"
  2. #include <stdexcept>
  3. #include <boost/spirit/include/qi.hpp>
  4. #include <boost/spirit/include/phoenix.hpp>
  5. namespace qi = boost::spirit::qi;
  6. BOOST_FUSION_ADAPT_STRUCT(
  7. pEp::URL,
  8. (std::string, schema)
  9. (std::string, username)
  10. (std::string, password)
  11. (std::string, host)
  12. (unsigned , port)
  13. (std::string, path_and_query)
  14. (std::string, fragment)
  15. )
  16. namespace pEp
  17. {
  18. template <typename Iterator>
  19. struct UriGrammar : qi::grammar<Iterator, URL()>
  20. {
  21. typedef unsigned long ulong;
  22. UriGrammar() : UriGrammar::base_type(url)
  23. {
  24. schema = *(qi::char_ - ':' - '/');
  25. username = *(qi::char_ - ':' - '@');
  26. password = *(qi::char_ - '@');
  27. host = *(qi::char_ - '/' - ':');
  28. path_and_query = *(qi::char_ - '#');
  29. fragment = *(qi::char_);
  30. url = schema
  31. >> "://" >> -( username
  32. >> -(':' >> password) >> '@'
  33. )
  34. >> host
  35. >> (':' >> qi::uint_[ boost::phoenix::at_c<4>(qi::_val) = qi::_1 ])
  36. >> -(path_and_query) // >> -('?' >> params[ boost::phoenix::at_c<6>(qi::_val) = qi::_1 ] )
  37. >> -('#' >> fragment);
  38. };
  39. qi::rule<Iterator, URL()> url;
  40. qi::rule<Iterator, std::string()> schema, username, password, host, path_and_query, fragment;
  41. };
  42. URL parse_url(const char* begin, const char* end)
  43. {
  44. static const UriGrammar<const char*> G;
  45. URL url;
  46. const char* begin2 = begin;
  47. const bool okay = boost::spirit::qi::parse(begin2, end, G, url);
  48. if(!okay)
  49. {
  50. throw std::runtime_error("Cannot parse URL <" + std::string(begin,end) + "> !");
  51. }
  52. // stolen from pEpMIME's ascii_tolower():
  53. for(char& c : url.schema)
  54. {
  55. if(c>='A' && c<='Z')
  56. {
  57. c += 32;
  58. }
  59. }
  60. return url;
  61. }
  62. } // end of namespace pEp