GCC Code Coverage Report
Directory: cvmfs/ Exec Total Coverage
File: cvmfs/stratum_agent/uri_map.h Lines: 0 10 0.0 %
Date: 2019-02-03 02:48:13 Branches: 0 2 0.0 %

Line Branch Exec Source
1
/**
2
 * This file is part of the CernVM File System
3
 */
4
5
#ifndef CVMFS_STRATUM_AGENT_URI_MAP_H_
6
#define CVMFS_STRATUM_AGENT_URI_MAP_H_
7
8
#include <stdint.h>
9
10
#include <string>
11
#include <vector>
12
13
#include "mongoose.h"
14
#include "pathspec/pathspec.h"
15
16
17
/**
18
 * Captures the request path and the HTTP request method of an HTTP request,
19
 * which is enough information to route the request to a handler.
20
 */
21
class WebRequest {
22
 public:
23
  enum Verb {
24
    kGet,
25
    kPut,
26
    kPost,
27
    kDelete,
28
    kUnknown,
29
  };
30
31
  explicit WebRequest(const struct mg_request_info *req);
32
  WebRequest(const std::string &uri, Verb verb) : verb_(verb), uri_(uri) { }
33
34
  Verb verb() const { return verb_; }
35
  std::string uri() const { return uri_; }
36
37
 private:
38
  WebRequest() : verb_(kUnknown) { }
39
40
  Verb verb_;
41
  std::string uri_;
42
};
43
44
45
class WebReply {
46
 public:
47
  enum Code {
48
    k200,
49
    k400,
50
    k404,
51
    k405,
52
    k500
53
  };
54
55
  static void Send(Code code, const std::string &msg,
56
                   struct mg_connection *conn);
57
};
58
59
60
/**
61
 * Abstract base class for a request handler.  Requests are identified by a
62
 * 64bit id.  Every request translates into one OnRequest call.
63
 */
64
class UriHandler {
65
 public:
66
  virtual ~UriHandler() { }
67
  virtual void OnRequest(const struct mg_request_info *req_info,
68
                         struct mg_connection *conn) = 0;
69
};
70
71
72
/**
73
 * Registeres handlers for URI path specifications and returns handler for
74
 * concrete requests.  Matching of requests is done in the order of
75
 * specification registration.
76
 */
77
class UriMap {
78
 public:
79
  void Register(const WebRequest &request, UriHandler *handler);
80
  UriHandler *Route(const WebRequest &request);
81
  bool IsKnownUri(const std::string &uri);
82
  void Clear();
83
84
 private:
85
  struct Match {
86
    Match(const Pathspec &p, const WebRequest::Verb v, UriHandler *h)
87
      : uri_spec(p), verb(v), handler(h) { }
88
    Pathspec uri_spec;
89
    WebRequest::Verb verb;
90
    UriHandler *handler;
91
  };
92
93
  std::vector<Match> rules_;
94
};
95
96
#endif  // CVMFS_STRATUM_AGENT_URI_MAP_H_