rikitiki  v0.1.67
Build C++ web server modules that allow easy routing and deployment.
connContext.h
1 /* Copyright (C) 2012-2013 Justin Berger
2  The full license is available in the LICENSE file at the root of this project and is also available at http://opensource.org/licenses/MIT. */
3 
4 #pragma once
5 #include <map>
6 #include <string>
7 #include "server.h"
8 #include "http_statuses.h"
9 #include <mxcomp/reflection.h>
10 #include "content_handler.h"
11 namespace rikitiki {
12  class Server;
13  class ConnContext;
14 
15  typedef std::pair<std::string,std::string> stringpair;
16 
21  struct Header : public stringpair {
22  Header(const std::string& name, const std::string& value);
23  };
24 
29  struct PostContent : public stringpair {
30  PostContent(const std::string& name, const std::string& value);
31  };
32 
37  struct Cookie : public stringpair {
38  Cookie(const std::string& name, const std::string& value,
39  const std::string& Domain = "", const std::string& Path = "",
40  const std::string& Expires = "", bool secure = false, bool httpOnly = false);
41  };
42 
46  struct Response {
47  ContentType::t ResponseType;
48 
49  std::vector<Header> headers;
50  const HttpStatus* status;
51  std::stringstream response;
52  void reset();
53  Response();
54  template <class T>
55 
56  auto operator <<(const T& obj) -> decltype( instance_of<std::stringstream>::value << obj, instance_of<Response&>::value)
57  { response << obj; return *this;}
58 
59  Response& operator <<(rikitiki::ContentType::t t);
60  Response& operator <<(const rikitiki::Cookie& t);
61  Response& operator <<(const rikitiki::HttpStatus& t);
62  Response& operator <<(const rikitiki::Header& t);
63  };
64 
68  template <typename T1, typename T2>
69  struct multimap : public std::multimap<T1, T2> {
70  T2& operator[](const T1&);
71  };
72 
74  typedef std::map<std::string, std::string> QueryStringCollection;
76  typedef std::map<std::string, std::string> CookieCollection;
77 
85  class ConnContext {
86  public:
87  enum Method {
88  ANY = 0, GET = 1, POST = 2, HEAD = 3, PUT = 4, DELETE = 5, TRACE = 6, OPTIONS = 7, CONNECT = 8, PATCH = 9, OTHER
89  };
90  protected:
91  bool mappedPost, mappedQs, mappedHeaders, mappedCookies, mappedPayload, mappedContentType;
92  PostCollection _post;
93  QueryStringCollection _qs;
94  HeaderCollection _headers;
95  CookieCollection _cookies;
96  std::multimap<double, ContentType::t>* _accepts;
97  ContentType::t _contentType;
98  std::string _payload;
99 
100  Method _method;
101 
102  virtual void FillAccepts();
103  virtual void FillContentType();
104  virtual void FillPayload() = 0;
105  virtual void FillPost();
106 
107  virtual void FillQueryString() = 0;
108  virtual void FillHeaders() = 0;
109  virtual void FillRequestMethod() = 0;
110  virtual void FillCookies();
111 
112  friend class Server;
113  virtual void writeResponse() = 0;
114  ConnContext(const Server*);
115  ConnContext();
116  public:
121  HeaderCollection::value_type& AddRequestHeader(const char*, const char*);
122  virtual ~ConnContext();
123  const Server* server;
124  std::multimap<double, ContentType::t>& Accepts();
125  PostCollection& Post();
126  ContentType::t ContentType();
127  QueryStringCollection& QueryString();
128  HeaderCollection& Headers();
129  CookieCollection& Cookies();
130  std::string& Payload();
131 
132  virtual const char* URI() = 0;
133  bool handled;
134  Method RequestMethod();
135 
136  ConnContext& operator <<(std::function<void(std::ostream&)>);
137  template <class T> auto operator <<(const T& obj) -> decltype( instance_of<Response>::value << obj, (ConnContext&)*(ConnContext*)0) ;
138 
139  template <class T> auto operator <<(T&) -> decltype(valid_conversions<T>::Out::Instance(), instance_of<ConnContext>::value);
140  template <class T> auto operator >>(T&) -> decltype(valid_conversions<T>::In ::Instance(), instance_of<ConnContext>::value);
141  Response response;
142  };
143  void mapContents(std::string& raw_content, PostCollection& post);
144  void mapQueryString(const char* _qs, QueryStringCollection& qs);
145  ConnContext::Method strToMethod(const char* method);
146 
147  ConnContext& operator>>(ConnContext&, std::string& t);
148 #include "connContext.tcc"
149 }
150 
151 
152 
153 //#include "ctemplate/connContext_ext.h"