forked from chenshuo/muduo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathfastcgi.h
70 lines (59 loc) · 1.79 KB
/
fastcgi.h
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
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
#ifndef MUDUO_EXAMPLES_FASTCGI_FASTCGI_H
#define MUDUO_EXAMPLES_FASTCGI_FASTCGI_H
#include <muduo/net/TcpConnection.h>
#include <map>
using muduo::string;
// one FastCgiCodec per TcpConnection
// both lighttpd and nginx do not implement multiplexing,
// so there is no concurrent requests of one connection.
class FastCgiCodec : boost::noncopyable
{
public:
typedef std::map<string, string> ParamMap;
typedef boost::function<void (const muduo::net::TcpConnectionPtr& conn,
ParamMap&,
muduo::net::Buffer*)> Callback;
explicit FastCgiCodec(const Callback& cb)
: cb_(cb),
gotRequest_(false),
keepConn_(false)
{
}
void onMessage(const muduo::net::TcpConnectionPtr& conn,
muduo::net::Buffer* buf,
muduo::Timestamp receiveTime)
{
parseRequest(buf);
if (gotRequest_)
{
cb_(conn, params_, &stdin_);
stdin_.retrieveAll();
paramsStream_.retrieveAll();
params_.clear();
gotRequest_ = false;
if (!keepConn_)
{
conn->shutdown();
}
}
}
static void respond(muduo::net::Buffer* response);
private:
struct RecordHeader;
bool parseRequest(muduo::net::Buffer* buf);
bool onBeginRequest(const RecordHeader& header, const muduo::net::Buffer* buf);
void onStdin(const char* content, uint16_t length);
bool onParams(const char* content, uint16_t length);
bool parseAllParams();
uint32_t readLen();
static void endStdout(muduo::net::Buffer* buf);
static void endRequest(muduo::net::Buffer* buf);
Callback cb_;
bool gotRequest_;
bool keepConn_;
muduo::net::Buffer stdin_;
muduo::net::Buffer paramsStream_;
ParamMap params_;
const static unsigned kRecordHeader;
};
#endif // MUDUO_EXAMPLES_FASTCGI_FASTCGI_H