asyncio_utils.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #include "asyncio_utils.h"
  2. void url_parse_host(std::string host,
  3. std::string &host_out, int &port_out,
  4. const std::string def_host, const int def_port)
  5. {
  6. std::string port;
  7. auto sep_it = std::find(host.begin(), host.end(), ':');
  8. if (sep_it == host.end())
  9. {
  10. // host
  11. if (!host.empty())
  12. {
  13. host_out = host;
  14. port_out = def_port;
  15. }
  16. else
  17. {
  18. host_out = def_host;
  19. port_out = def_port;
  20. }
  21. return;
  22. }
  23. if (sep_it == host.begin())
  24. {
  25. // :port
  26. host_out = def_host;
  27. }
  28. else
  29. {
  30. // host:port
  31. host_out.assign(host.begin(), sep_it);
  32. }
  33. port.assign(sep_it + 1, host.end());
  34. port_out = std::stoi(port);
  35. }
  36. /**
  37. * Parse ?ids=sid,cid
  38. */
  39. void url_parse_query(std::string query)
  40. {
  41. const std::string ids_end("ids=");
  42. std::string sys, comp;
  43. if (query.empty())
  44. return;
  45. auto ids_it = std::search(query.begin(), query.end(),
  46. ids_end.begin(), ids_end.end());
  47. if (ids_it == query.end())
  48. {
  49. std::cerr << "URL: unknown query arguments" << std::endl;
  50. return;
  51. }
  52. std::advance(ids_it, ids_end.length());
  53. auto comma_it = std::find(ids_it, query.end(), ',');
  54. if (comma_it == query.end())
  55. {
  56. std::cerr << "URL: no comma in ids= query" << std::endl;
  57. return;
  58. }
  59. sys.assign(ids_it, comma_it);
  60. comp.assign(comma_it + 1, query.end());
  61. }