forked from jankaluza/hamlog
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathreply.cpp
115 lines (101 loc) · 2.83 KB
/
reply.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
/**
* Hamlog
*
* Copyright (C) 2011, Jan Kaluza <[email protected]>
*
* This program is free software; you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation; either version 2 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program; if not, write to the Free Software
* Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02111-1301 USA
*/
#include "reply.h"
#include <boost/bind.hpp>
#include <iostream>
#include <boost/foreach.hpp>
#include <boost/lexical_cast.hpp>
#include <boost/algorithm/string.hpp>
namespace HamLog {
Reply::Reply(Status status, const std::string &content_type)
: m_status(status), m_async(false) {
m_headers.resize(2);
m_headers[0].name = "Content-Type";
m_headers[0].value = content_type;
m_headers[1].name = "Content-Length";
m_headers[1].value = "0";
}
bool Reply::hasHeader(const std::string &name) {
BOOST_FOREACH(Header &header, m_headers) {
if (header.name == name)
return true;
}
return false;
}
std::string Reply::getHeader(const std::string &name) {
BOOST_FOREACH(Header &header, m_headers) {
if (header.name == name) {
return header.value;
}
}
return "";
}
void Reply::dump() {
std::cout << toString();
}
std::string Reply::toString() {
std::string buffers;
switch (m_status) {
case ok:
buffers += ("HTTP/1.1 200 OK\r\n");
break;
case moved_permanently:
buffers += ("HTTP/1.0 301 Moved Permanently\r\n");
break;
case moved_temporarily:
buffers += ("HTTP/1.0 302 Moved Temporarily\r\n");
break;
case not_modified:
buffers += ("HTTP/1.0 304 Not Modified\r\n");
break;
case bad_request:
buffers += ("HTTP/1.0 400 Bad Request\r\n");
break;
case unauthorized:
buffers += ("HTTP/1.0 401 Unauthorized\r\n");
break;
case forbidden:
buffers += ("HTTP/1.0 403 Forbidden\r\n");
break;
case not_found:
buffers += ("HTTP/1.0 404 Not Found\r\n");
break;
case internal_server_error:
buffers += ("HTTP/1.0 500 Internal Server Error\r\n");
break;
default:
buffers += ("HTTP/1.0 500 Internal Server Error\r\n");
}
m_headers[1].value = boost::lexical_cast<std::string>(m_content.size());
BOOST_FOREACH(Header &header, m_headers) {
buffers += (header.name);
buffers += (": ");
buffers += (header.value);
buffers += ("\r\n");
}
buffers += ("\r\n");
if (!m_content.empty()) {
boost::replace_all(m_content, "\r", "");
buffers += (m_content);
buffers += ("\r\n");
}
return buffers;
}
}