Crow  0.3
A C++ microframework for the web
common.h
1 #pragma once
2 
3 #include <vector>
4 #include <string>
5 #include <stdexcept>
6 #include <iostream>
7 #include "crow/utility.h"
8 
9 namespace crow
10 {
11  enum class HTTPMethod
12  {
13 #ifndef DELETE
14  DELETE = 0,
15  GET,
16  HEAD,
17  POST,
18  PUT,
19  CONNECT,
20  OPTIONS,
21  TRACE,
22  PATCH,
23  PURGE,
24 #endif
25 
26  Delete = 0,
27  Get,
28  Head,
29  Post,
30  Put,
31  Connect,
32  Options,
33  Trace,
34  Patch,
35  Purge,
36 
37 
38  InternalMethodCount,
39  // should not add an item below this line: used for array count
40  };
41 
42  inline std::string method_name(HTTPMethod method)
43  {
44  switch(method)
45  {
46  case HTTPMethod::Delete:
47  return "DELETE";
48  case HTTPMethod::Get:
49  return "GET";
50  case HTTPMethod::Head:
51  return "HEAD";
52  case HTTPMethod::Post:
53  return "POST";
54  case HTTPMethod::Put:
55  return "PUT";
56  case HTTPMethod::Connect:
57  return "CONNECT";
58  case HTTPMethod::Options:
59  return "OPTIONS";
60  case HTTPMethod::Trace:
61  return "TRACE";
62  case HTTPMethod::Patch:
63  return "PATCH";
64  case HTTPMethod::Purge:
65  return "PURGE";
66  default:
67  return "invalid";
68  }
69  return "invalid";
70  }
71 
72  enum class ParamType
73  {
74  INT,
75  UINT,
76  DOUBLE,
77  STRING,
78  PATH,
79 
80  MAX
81  };
82 
84  {
85  std::vector<int64_t> int_params;
86  std::vector<uint64_t> uint_params;
87  std::vector<double> double_params;
88  std::vector<std::string> string_params;
89 
90  void debug_print() const
91  {
92  std::cerr << "routing_params" << std::endl;
93  for(auto i:int_params)
94  std::cerr<<i <<", " ;
95  std::cerr<<std::endl;
96  for(auto i:uint_params)
97  std::cerr<<i <<", " ;
98  std::cerr<<std::endl;
99  for(auto i:double_params)
100  std::cerr<<i <<", " ;
101  std::cerr<<std::endl;
102  for(auto& i:string_params)
103  std::cerr<<i <<", " ;
104  std::cerr<<std::endl;
105  }
106 
107  template <typename T>
108  T get(unsigned) const;
109 
110  };
111 
112  template<>
113  inline int64_t routing_params::get<int64_t>(unsigned index) const
114  {
115  return int_params[index];
116  }
117 
118  template<>
119  inline uint64_t routing_params::get<uint64_t>(unsigned index) const
120  {
121  return uint_params[index];
122  }
123 
124  template<>
125  inline double routing_params::get<double>(unsigned index) const
126  {
127  return double_params[index];
128  }
129 
130  template<>
131  inline std::string routing_params::get<std::string>(unsigned index) const
132  {
133  return string_params[index];
134  }
135 }
136 
137 #ifndef CROW_MSVC_WORKAROUND
138 constexpr crow::HTTPMethod operator "" _method(const char* str, size_t /*len*/)
139 {
140  return
141  crow::black_magic::is_equ_p(str, "GET", 3) ? crow::HTTPMethod::Get :
142  crow::black_magic::is_equ_p(str, "DELETE", 6) ? crow::HTTPMethod::Delete :
143  crow::black_magic::is_equ_p(str, "HEAD", 4) ? crow::HTTPMethod::Head :
144  crow::black_magic::is_equ_p(str, "POST", 4) ? crow::HTTPMethod::Post :
145  crow::black_magic::is_equ_p(str, "PUT", 3) ? crow::HTTPMethod::Put :
146  crow::black_magic::is_equ_p(str, "OPTIONS", 7) ? crow::HTTPMethod::Options :
147  crow::black_magic::is_equ_p(str, "CONNECT", 7) ? crow::HTTPMethod::Connect :
148  crow::black_magic::is_equ_p(str, "TRACE", 5) ? crow::HTTPMethod::Trace :
149  crow::black_magic::is_equ_p(str, "PATCH", 5) ? crow::HTTPMethod::Patch :
150  crow::black_magic::is_equ_p(str, "PURGE", 5) ? crow::HTTPMethod::Purge :
151  throw std::runtime_error("invalid http method");
152 }
153 #endif
crow::routing_params
Definition: common.h:83