-
Notifications
You must be signed in to change notification settings - Fork 10
/
httpmessage.h
84 lines (72 loc) · 1.97 KB
/
httpmessage.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
71
72
73
74
75
76
77
78
79
80
81
82
83
84
#pragma once
#ifndef HTTPMESSAGE_H
#define HTTPMESSAGE_H
#include <string>
#include <map>
#include <algorithm>
#include "simplebuffer.h"
#include "base/log.h"
class HttpMessage
{
public:
typedef std::map<std::string, std::string> HeadMapType;
typedef std::map<std::string, std::string> ContentTypeMapType;
enum ParseRequestType
{
ParseRequestLine = 1,
ParseRequestHead,
ParseRequestBody
};
typedef enum ParseState
{
Again = 100, // more data need
Error, // parse data gets error
Done // parse a complete message
} ParseState;
enum RespondType
{
Correct = 200,
BadReq = 400, // bad request
NotFound = 404, // not found
SerError = 500, // internal server error
MeNotIm = 501 // method not implement
};
enum MultiPartFormDataParse
{
Boundary = 1000,
ContentDisposition,
DataContentTypeEnd,
RealBodyData,
RealDone
};
public:
HttpMessage();
const std::string & GetMethod() const;
const std::string & GetUrl() const;
const std::string & GetHttpVersion() const;
const std::string & GetBody() const;
const HeadMapType & GetHeadContent() const;
HeadMapType & GetHeadContent();
RespondType GetRespondCode() const;
const std::string & GetRespondMsg() const;
ParseState Parse(SimpleBuffer & inBuffer);
void Reset();
void BuildErrorRespond(int errCode, const std::string & errMsg, SimpleBuffer & outBuffer);
// Fix Me Content-Type json/text should change.
void BuildCgiRespond(int respCode, const std::string & sRespMsg, const std::string & sContentType, const std::string & sRespBody, SimpleBuffer & outBuffer);
static void InitContentTypeMap();
static ContentTypeMapType & GetContentTypeMap();
private:
ParseRequestType m_parseStage;
MultiPartFormDataParse m_parseMultiFormStage;
RespondType m_respondCode;
std::string m_respondMsg;
std::string m_method;
std::string m_url;
std::string m_httpVersion;
HeadMapType m_headContent;
std::string m_bodyStr;
size_t m_realDataContentLength;
static ContentTypeMapType m_contentTypeMap;
};
#endif