-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbasic_http.h
More file actions
52 lines (39 loc) · 1.47 KB
/
basic_http.h
File metadata and controls
52 lines (39 loc) · 1.47 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
// File: basic_http.h
// Description: ---
// Notes: ---
// Author: Haust <wyy123_2008@qq.com>
// Revision: 2015-11-19 by Haust
#pragma once
#include "mongoose.h"
#include <map>
#include <string>
#include <functional>
class BasicHttp {
public:
using handler = std::function<void(std::string, std::string)>;
public:
virtual ~BasicHttp(){};
//typedef void (*handler)(std::string query, std::string body);
void Init(uint32_t port);
bool Start();
bool Close();
bool RegisterHandler(std::string uri, handler f);
void UnRegisterHandler(std::string uri);
void Loop(int milli);
void SendReply(std::string uri, std::string reply);
void SendError(std::string uri, int errcode, std::string reply);
protected:
using handler_map = std::map<std::string, handler>;
// this define means that if get reply it will reply the first of the requests in the multimap which has the same uri
// it may has a lot of other problems. so this defined can instead by uri+query_string as the map key, like this:
// using connnection_map = std::map<std::string, mg_connection*>;
using connection_map = std::multimap<std::string, mg_connection*>;
private:
static void EvHandler(struct mg_connection* nc, int ev, void* ev_data);
static void HandleRequst(struct mg_connection* nc, int ev, void *ev_data);
public:
static handler_map _handlers;
static connection_map _connections;
char _port[11];
struct mg_mgr _mgr;
};