]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/crtd_message.cc
6 #include "ssl/crtd_message.h"
14 Ssl::CrtdMessage::CrtdMessage()
15 : body_size(0), state(BEFORE_CODE
)
18 Ssl::CrtdMessage::ParseResult
Ssl::CrtdMessage::parse(const char * buffer
, size_t len
)
20 char const *current_pos
= buffer
;
21 while (current_pos
!= buffer
+ len
&& state
!= END
) {
24 if (xisspace(*current_pos
)) {
28 if (xisalpha(*current_pos
)) {
36 if (xisalnum(*current_pos
) || *current_pos
== '_') {
37 current_block
+= *current_pos
;
41 if (xisspace(*current_pos
)) {
43 current_block
.clear();
44 state
= BEFORE_LENGTH
;
51 if (xisspace(*current_pos
)) {
55 if (xisdigit(*current_pos
)) {
63 if (xisdigit(*current_pos
)) {
64 current_block
+= *current_pos
;
68 if (xisspace(*current_pos
)) {
69 body_size
= atoi(current_block
.c_str());
70 current_block
.clear();
82 if (xisspace(*current_pos
)) {
91 size_t body_len
= (static_cast<size_t>(buffer
+ len
- current_pos
) >= body_size
- current_block
.length())
92 ? body_size
- current_block
.length()
93 : static_cast<size_t>(buffer
+ len
- current_pos
);
94 current_block
+= std::string(current_pos
, body_len
);
95 current_pos
+= body_len
;
96 if (current_block
.length() == body_size
) {
100 if (current_block
.length() > body_size
) {
111 if (state
!= END
) return INCOMPLETE
;
115 std::string
const & Ssl::CrtdMessage::getBody() const { return body
; }
117 std::string
const & Ssl::CrtdMessage::getCode() const { return code
; }
119 void Ssl::CrtdMessage::setBody(std::string
const & aBody
) { body
= aBody
; }
121 void Ssl::CrtdMessage::setCode(std::string
const & aCode
) { code
= aCode
; }
124 std::string
Ssl::CrtdMessage::compose() const
126 if (code
.empty()) return std::string();
128 snprintf(buffer
, sizeof(buffer
), "%zd", body
.length());
129 return code
+ ' ' + buffer
+ ' ' + body
;
132 void Ssl::CrtdMessage::clear()
138 current_block
.clear();
141 void Ssl::CrtdMessage::parseBody(CrtdMessage::BodyParams
& map
, std::string
& other_part
) const
144 // Copy string for using it as temp buffer.
145 std::string
temp_body(body
.c_str(), body
.length());
146 char * buffer
= const_cast<char *>(temp_body
.c_str());
147 char * token
= strtok(buffer
, "\r\n");
148 while (token
!= NULL
) {
149 std::string
current_string(token
);
150 size_t equal_pos
= current_string
.find('=');
151 if (equal_pos
== std::string::npos
) {
152 size_t offset_body_part
= token
- temp_body
.c_str();
153 other_part
= std::string(body
.c_str() + offset_body_part
, body
.length() - offset_body_part
);
156 std::string
param(current_string
.c_str(), current_string
.c_str() + equal_pos
);
157 std::string
value(current_string
.c_str() + equal_pos
+ 1);
158 map
.insert(std::make_pair(param
, value
));
160 token
= strtok(NULL
, "\r\n");
164 void Ssl::CrtdMessage::composeBody(CrtdMessage::BodyParams
const & map
, std::string
const & other_part
)
167 for (BodyParams::const_iterator i
= map
.begin(); i
!= map
.end(); ++i
) {
168 if (i
!= map
.begin())
170 body
+= i
->first
+ "=" + i
->second
;
172 if (!other_part
.empty())
173 body
+= '\n' + other_part
;
176 const std::string
Ssl::CrtdMessage::code_new_certificate("new_certificate");
177 const std::string
Ssl::CrtdMessage::param_host("host");