-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathhttpcgiserver.cpp
96 lines (77 loc) · 3 KB
/
httpcgiserver.cpp
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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
#include "httpcgiserver.h"
#include "cgidispatch.h"
#include <iostream>
using namespace std;
int HttpCgiServer::ReadRequestCallback(std::shared_ptr<Channel> ptChannel)
{
SimpleBuffer & inBuffer = ptChannel->GetInBuffer();
SimpleBuffer & outBuffer = ptChannel->GetOutBuffer();
HttpMessage & httpMessage = ptChannel->GetHttpMessage();
HttpMessage::ParseState state = httpMessage.Parse(inBuffer);
if (HttpMessage::Again == state)
{
return 1;
}
else if (HttpMessage::Error == state)
{
httpMessage.BuildErrorRespond(httpMessage.GetRespondCode(), httpMessage.GetRespondMsg(), outBuffer);
return 0;
}
/*
cout << "ParseState=" << state << endl;
cout << "httpmessage.GetMethod()=" << httpMessage.GetMethod() << endl;
cout << "httpmessage.GetUrl()=" << httpMessage.GetUrl() << endl;
cout << "httpmessage.GetHttpVersion()=" << httpMessage.GetHttpVersion() << endl;
const map<string, string> & headContent = httpMessage.GetHeadContent();
for (map<string, string>::const_iterator iter = headContent.begin(); headContent.end() != iter; ++iter)
{
cout << iter->first << "=" << iter->second << endl;
}
cout << "httpmessage.GetBody()=" << httpMessage.GetBody() << endl;
httpMessage.Reset();
//inBuffer.ReadFromBuffer(inBuffer.BufferSize());
const char * respond = "HTTP/1.1 200 OK\r\nContent-type: text/plain\r\nContent-Length:13\r\n\r\nHello World\r\n";
outBuffer.WriteToBuffer(respond, strlen(respond));
*/
int respCode;
std::string sRespMsg, sContentType, sRespBody;
BaseFactory::Url2CgiFactoryMapType url2CgiFactoryMap = BaseFactory::GetCgiFactoryMap();
BaseFactory::Url2CgiFactoryMapType::const_iterator iter = url2CgiFactoryMap.find(httpMessage.GetUrl());
if (url2CgiFactoryMap.end() == iter)
{
httpMessage.BuildErrorRespond(HttpMessage::NotFound, "Not Found This Cgi", outBuffer);
return 0;
}
std::unique_ptr<BaseCgi> ptBaseCgi = iter->second->GetNewInstance();
ptBaseCgi->Process(&httpMessage, respCode, sRespMsg, sContentType, sRespBody);
httpMessage.BuildCgiRespond(respCode, sRespMsg, sContentType, sRespBody, outBuffer);
int iRet = 0;
HttpMessage::HeadMapType & headContent = httpMessage.GetHeadContent();
if ("HTTP/1.1" == httpMessage.GetHttpVersion() || "keep-alive" == headContent["Connection"])
{
iRet = 1;
}
httpMessage.Reset();
return iRet;
}
void HttpCgiServer::WriteCallback(std::shared_ptr<Channel> ptChannel)
{
}
void HttpCgiServer::ErrorCallback(std::shared_ptr<Channel> ptChannel)
{
}
HttpCgiServer::HttpCgiServer(const char * serverIp, uint16_t port, int threadCnt /* = 0 */) : m_tcpServer(serverIp, port, threadCnt)
{
using namespace std::placeholders;
m_tcpServer.SetReadCallback(std::bind(&HttpCgiServer::ReadRequestCallback, this, _1));
m_tcpServer.SetWriteCallback(std::bind(&HttpCgiServer::WriteCallback, this, _1));
m_tcpServer.SetErrorCallback(std::bind(&HttpCgiServer::ErrorCallback, this, _1));
// cgi instance factory init
BaseFactory::Init();
// content type map init
HttpMessage::InitContentTypeMap();
}
void HttpCgiServer::Loop()
{
m_tcpServer.Loop();
}