Bredbandskollen CLI  1.2
Asynchronous network task engine
http_common.h
1 // Copyright (c) 2018 IIS (The Internet Foundation in Sweden)
2 // Written by Göran Andersson <initgoran@gmail.com>
3 
4 #pragma once
5 
6 #include <string>
7 #include <cstring>
8 #include <ctime>
9 #include <vector>
10 #include <map>
11 
12 class HttpCommon {
13 public:
14  // Return true on success.
15  static bool parseHeaders(const std::string &header, std::string &r,
16  std::multimap<std::string, std::string> &res);
17 
18  // Guess mime type, e.g. "text/html", from url/filename
19  static const char *mime_type(const std::string &file_name);
20 
21  // Return -1 on failure.
22  static time_t parseDateRfc1123(const std::string &date);
23 
24  // Remove leading and trailing whitespace:
25  static void trimWSP(std::string &s);
26 
27  // Split s into fields sepatared by sep:
28  static std::vector<std::string> split(const std::string &s,
29  const std::string &sep);
30  static bool isSubdomain(const std::string &subdomain,
31  const std::string &domain) {
32  auto s = domain.size();
33  return (subdomain.size() > s &&
34  subdomain[subdomain.size()-s-1] == '.' &&
35  subdomain.substr(subdomain.size()-s) == domain);
36  }
37  static bool isWithinPath(const std::string &uri,
38  const std::string &path) {
39  if (path.empty())
40  return (!uri.empty() && uri[0] == '/');
41  if (path != uri.substr(0, path.size()))
42  return false;
43  if (path.size() < uri.size()) {
44  return path[path.size()-1] == '/' ||
45  uri[path.size()] == '/';
46  } else if (path.size() == uri.size()) {
47  return true; // Identical.
48  } else {
49  return false;
50  }
51  }
52  // Return the path part of an uri.
53  static std::string uriPath(std::string uri) {
54  auto qpos = uri.find('?');
55  if (qpos != std::string::npos)
56  uri.erase(qpos);
57  qpos = uri.rfind('/');
58  if (qpos != std::string::npos)
59  uri.erase(qpos+1);
60  return uri;
61  }
62 };
Definition: http_common.h:12