-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathHttpHeader.cpp
130 lines (111 loc) · 3.48 KB
/
HttpHeader.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
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
#include <sstream>
#include <string.h>
#include <cstdlib>
#include <iostream>
#include "HttpHeader.h"
using namespace std;
HttpHeader::HttpHeader()
{
parserHeader();
}
HttpHeader::HttpHeader(string headerStr) : _headerStr(headerStr)
{
parserHeader();
}
void HttpHeader::setHeaderStr(string headStr)
{
_headerStr = headStr;
parserHeader();
}
// Status-Line = HTTP-Version SP Status-Code SP Reason-Phrase CRLF
void HttpHeader::parserHeader()
{
_statusCode = -1;
_contentLength = -1;
_location = "";
_contentEncoding = "";
_contentType = "";
if(_headerStr.empty())
return;
string headerStr = _headerStr;
// extract StatusCode:
string::size_type lineEnd = headerStr.find("\r\n");
string statusLine = headerStr.substr(0, lineEnd);
istringstream iss(statusLine);
string httpVersion;
iss >> httpVersion;
iss >> _statusCode;
string::size_type lineBegin = lineEnd + strlen("\r\n");
while(lineBegin < headerStr.size()
&& ((lineEnd = headerStr.find("\r\n", lineBegin)) != string::npos)) {
string line = headerStr.substr(lineBegin, lineEnd - lineBegin);
lineBegin = lineEnd + strlen("\r\n");
if(line.empty())
continue;
//cout << "line:" << line << endl;
string::size_type pos;
string::size_type pos2;
// extract ContentLength:
if((pos = line.find("Content-Length: ")) != string::npos) {
pos += strlen("Content-Length: ");
_contentLength = atoi(line.substr(pos).c_str());
}
// extract Location:
else if((pos = line.find("Location: ")) != string::npos) {
pos += strlen("Location: ");
_location = line.substr(pos);
}
// extract Content-Encoding:
else if((pos = line.find("Content-Encoding: ")) != string::npos) {
pos += strlen("Content-Encoding: ");
_contentEncoding = line.substr(pos);
}
// extract Content-Type:
else if((pos = line.find("Content-Type: ")) != string::npos) {
pos += strlen("Content-Type: ");
if((pos2 = line.find(';')) != string::npos) {
pos2-=pos;
_contentType = line.substr(pos,pos2);
}
else _contentType = line.substr(pos);
}
// extract Connection:
else if((pos = line.find("Connection: ")) != string::npos) {
pos += strlen("Connection: ");
_connection = line.substr(pos);
}
else{
}
}
}
HttpHeader::~HttpHeader()
{
}
string HttpHeader::getHeaderStr()
{
return _headerStr;
}
int HttpHeader::getStatusCode()
{
return _statusCode;
}
string HttpHeader::getLocation()
{
return _location;
}
string HttpHeader::getContentType()
{
return _contentType;
}
int HttpHeader::getContentLength()
{
return _contentLength;
}
string HttpHeader::getContentEncoding()
{
return _contentEncoding;
}
string HttpHeader::getConnection()
{
return _connection;
}