GraphChi  0.1
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Macros
mongcpp.h
Go to the documentation of this file.
1 
2 // Copyright (c) 2010 Piotr Likus
3 // Name: mongcpp.h
4 // Project: mongoose
5 // Purpose: C++ wrapper for mongoose.
6 // Author: Piotr Likus
7 // Modified by:
8 // Created: 15/12/2010
9 // Licence: MIT
11 
12 #ifndef _MONGCPP_H__
13 #define _MONGCPP_H__
14 
15 // ----------------------------------------------------------------------------
16 // Description
17 // ----------------------------------------------------------------------------
21 
22 // ----------------------------------------------------------------------------
23 // Headers
24 // ----------------------------------------------------------------------------
25 #include <cstddef>
26 #include <cstdlib>
27 
28 #include "mongoose.h"
29 
30 #include <string>
31 #include <map>
32 #include <vector>
33 #include <memory>
34 
35 namespace mongoose
36 {
37 
38 // ----------------------------------------------------------------------------
39 // Simple type definitions
40 // ----------------------------------------------------------------------------
41 enum MongooseRequestMethodCode {
42  rmcUndef,
43  rmcGet,
44  rmcPost,
45  rmcHead,
46  rmcPut,
47  rmcDelete,
48  rmcTrace,
49  rmcOptions
50 };
51 
52 typedef std::map<std::string, std::string> ServerOptionSet;
53 typedef std::vector<std::string> ServerOptionList;
54 typedef std::vector<const char *> ServerOptionStorage;
55 typedef std::map<std::string, std::string> RequestValueSet;
56 typedef std::map<std::string, int> ResponseValueIndex;
57 typedef std::vector< std::pair<std::string, std::string> > ResponseValueList;
58 typedef enum mg_event ServerHandlingEvent;
59 typedef std::map<std::string, MongooseRequestMethodCode> MethodMap;
60 typedef std::auto_ptr<MethodMap> MethodMapGuard;
61 
62 // ----------------------------------------------------------------------------
63 // Forward class definitions
64 // ----------------------------------------------------------------------------
65 
66 // ----------------------------------------------------------------------------
67 // Constants
68 // ----------------------------------------------------------------------------
69 
70 // ----------------------------------------------------------------------------
71 // Class definitions
72 // ----------------------------------------------------------------------------
73 
75 public:
76  MongooseConnection(struct mg_connection *conn);
77  virtual ~MongooseConnection();
78  int write(const void *buf, size_t len);
79  int write(const std::string &text);
80  int read(void *buf, size_t len);
81  void sendAuthorizationRequest(const std::string &nonce = "");
82  bool getHeader(const std::string &name, std::string &output) const;
83  bool getCookie(const std::string &name, std::string &output) const;
84 protected:
85  struct mg_connection *getInfo();
86 protected:
87  struct mg_connection *m_conn;
88 };
89 
91 public:
92  MongooseRequest(struct mg_connection *conn, mg_request_info* info);
93  virtual ~MongooseRequest();
94  const std::string getRequestMethod() const;
95  MongooseRequestMethodCode getRequestMethodCode() const;
96  const std::string getUri() const;
97  const std::string getHttpVersion() const;
98  const std::string getQueryString() const;
99  const std::string readQueryString() const;
100  const std::string getRemoteUser() const;
101  const std::string getLogMessage() const;
102  long getRemoteIp() const;
103  int getRemotePort() const;
104  int getStatusCode() const;
105  bool isSsl() const;
106  bool getVar(const std::string &name, std::string &output) const;
107  static MongooseRequestMethodCode methodTextToCode(const std::string &text);
108 protected:
109  mg_request_info* getInfo() const;
110 protected:
111  mg_request_info* m_info;
112  struct mg_connection *m_conn;
113 };
114 
116 public:
117  MongooseResponse(struct mg_connection *conn);
118  virtual ~MongooseResponse();
119  virtual void write();
120  void setStatus(int code, const std::string &statusDesc = "", const std::string &httpVer = "");
121  void setSetCookie(const std::string &name, const std::string &value);
122  void setLocation(const std::string &value = "");
123  void setContentType(const std::string &value = "");
124  bool getHeaderValue(const std::string &name, std::string &output);
125  void addHeaderValue(const std::string &name, const std::string &value);
126  void setHeaderValue(const std::string &name, const std::string &value);
127  void setConnectionAlive(bool keepAlive = false);
128  void setCacheDisabled();
129  void addHeader();
130  void addContent(const std::string &text, bool addLen = true);
131  void addText(const std::string &text);
132  void addTextLine(const std::string &text);
133  static const char *getHttpStatusDesc(int statusCode);
134 protected:
135  ResponseValueList *prepareHeaderValues();
136  void addHeaderValueToText(const std::string &name, const std::string &value);
137 protected:
138  struct mg_connection *m_conn;
139  std::string m_text;
140  std::auto_ptr<ResponseValueList> m_headerValues;
141  std::auto_ptr<ResponseValueIndex> m_headerValuesIndex;
142  std::auto_ptr<std::string> m_statusText;
143 };
144 
146 public:
147  // construct
148  MongooseServer();
149  virtual ~MongooseServer();
150  // attributes
151  void setOptions(const ServerOptionSet &options);
152  void setOption(const std::string &name, const std::string &value);
153  void getOptions(ServerOptionSet &options) const;
154  bool getOptionValue(const std::string &name, std::string &value) const;
155  void getOptionValue(const std::string &name, std::string &value, const std::string &defValue) const;
156  static void getValidOptions(ServerOptionList &output);
157  static std::string getVersion();
158  static void calcMD5(const std::string &text, std::string &output);
159  static MongooseRequestMethodCode methodTextToCode(const std::string &text);
160  // run
161  virtual void init();
162  void start();
163  void stop();
164  bool isRunning();
165  virtual void *handleEvent(ServerHandlingEvent eventCode,
166  struct mg_connection *conn,
167  const struct mg_request_info *request_info);
168 protected:
169  virtual bool handleEvent(ServerHandlingEvent eventCode, MongooseConnection &connection, const MongooseRequest &request, MongooseResponse &response);
170  virtual MongooseConnection *newConnection(struct mg_connection *conn);
171  virtual MongooseRequest *newRequest(struct mg_connection *conn, const struct mg_request_info *request);
172  virtual MongooseResponse *newResponse(struct mg_connection *conn);
173  void checkStopped();
174  const char **prepareOptions();
175  void unprepareOptions();
176  void checkMethodMap();
177 protected:
178  ServerOptionSet m_options;
179  ServerOptionStorage m_optionStorage;
180  bool m_statusRunning;
181  bool m_prepared;
182  struct mg_context *m_ctx;
183  static MethodMapGuard m_methodMap;
184 };
185 
186 }
187 
188 #endif // _MONGCPP_H__