]>
git.ipfire.org Git - thirdparty/squid.git/blob - src/ssl/crtd_message.cc
6 #include "ssl/gadgets.h"
7 #include "ssl/crtd_message.h"
15 Ssl::CrtdMessage::CrtdMessage()
16 : body_size(0), state(BEFORE_CODE
)
19 Ssl::CrtdMessage::ParseResult
Ssl::CrtdMessage::parse(const char * buffer
, size_t len
)
21 char const *current_pos
= buffer
;
22 while (current_pos
!= buffer
+ len
&& state
!= END
) {
25 if (xisspace(*current_pos
)) {
29 if (xisalpha(*current_pos
)) {
37 if (xisalnum(*current_pos
) || *current_pos
== '_') {
38 current_block
+= *current_pos
;
42 if (xisspace(*current_pos
)) {
44 current_block
.clear();
45 state
= BEFORE_LENGTH
;
52 if (xisspace(*current_pos
)) {
56 if (xisdigit(*current_pos
)) {
64 if (xisdigit(*current_pos
)) {
65 current_block
+= *current_pos
;
69 if (xisspace(*current_pos
)) {
70 body_size
= atoi(current_block
.c_str());
71 current_block
.clear();
83 if (xisspace(*current_pos
)) {
92 size_t body_len
= (static_cast<size_t>(buffer
+ len
- current_pos
) >= body_size
- current_block
.length())
93 ? body_size
- current_block
.length()
94 : static_cast<size_t>(buffer
+ len
- current_pos
);
95 current_block
+= std::string(current_pos
, body_len
);
96 current_pos
+= body_len
;
97 if (current_block
.length() == body_size
) {
101 if (current_block
.length() > body_size
) {
112 if (state
!= END
) return INCOMPLETE
;
116 std::string
const & Ssl::CrtdMessage::getBody() const { return body
; }
118 std::string
const & Ssl::CrtdMessage::getCode() const { return code
; }
120 void Ssl::CrtdMessage::setBody(std::string
const & aBody
) { body
= aBody
; }
122 void Ssl::CrtdMessage::setCode(std::string
const & aCode
) { code
= aCode
; }
125 std::string
Ssl::CrtdMessage::compose() const
127 if (code
.empty()) return std::string();
129 snprintf(buffer
, sizeof(buffer
), "%zd", body
.length());
130 return code
+ ' ' + buffer
+ ' ' + body
;
133 void Ssl::CrtdMessage::clear()
139 current_block
.clear();
142 void Ssl::CrtdMessage::parseBody(CrtdMessage::BodyParams
& map
, std::string
& other_part
) const
145 // Copy string for using it as temp buffer.
146 std::string
temp_body(body
.c_str(), body
.length());
147 char * buffer
= const_cast<char *>(temp_body
.c_str());
148 char * token
= strtok(buffer
, "\r\n");
149 while (token
!= NULL
) {
150 std::string
current_string(token
);
151 size_t equal_pos
= current_string
.find('=');
152 if (equal_pos
== std::string::npos
) {
153 size_t offset_body_part
= token
- temp_body
.c_str();
154 other_part
= std::string(body
.c_str() + offset_body_part
, body
.length() - offset_body_part
);
157 std::string
param(current_string
.c_str(), current_string
.c_str() + equal_pos
);
158 std::string
value(current_string
.c_str() + equal_pos
+ 1);
159 map
.insert(std::make_pair(param
, value
));
161 token
= strtok(NULL
, "\r\n");
165 void Ssl::CrtdMessage::composeBody(CrtdMessage::BodyParams
const & map
, std::string
const & other_part
)
168 for (BodyParams::const_iterator i
= map
.begin(); i
!= map
.end(); i
++) {
169 if (i
!= map
.begin())
171 body
+= i
->first
+ "=" + i
->second
;
173 if (!other_part
.empty())
174 body
+= '\n' + other_part
;
177 const std::string
Ssl::CrtdMessage::code_new_certificate("new_certificate");
178 const std::string
Ssl::CrtdMessage::param_host("host");
179 const std::string
Ssl::CrtdMessage::param_SetValidAfter(Ssl::CertAdaptAlgorithmStr
[algSetValidAfter
]);
180 const std::string
Ssl::CrtdMessage::param_SetValidBefore(Ssl::CertAdaptAlgorithmStr
[algSetValidBefore
]);
181 const std::string
Ssl::CrtdMessage::param_SetCommonName(Ssl::CertAdaptAlgorithmStr
[algSetCommonName
]);
182 const std::string
Ssl::CrtdMessage::param_Sign("Sign");