22 #include "wsrequest.h" 23 #include "urlencoder.h" 31 WSRequest::WSRequest(
const std::string& server,
unsigned port)
36 , m_service_method(HRM_GET)
37 , m_charset(REQUEST_STD_CHARSET)
39 , m_contentType(CT_FORM)
45 RequestAcceptEncoding(
true);
48 WSRequest::WSRequest(
const std::string& server,
unsigned port,
bool secureURI)
51 , m_secure_uri(secureURI)
53 , m_service_method(HRM_GET)
54 , m_charset(REQUEST_STD_CHARSET)
56 , m_contentType(CT_FORM)
60 RequestAcceptEncoding(
true);
63 WSRequest::WSRequest(
const URIParser& uri, HRM_t method)
66 , m_service_method(method)
67 , m_charset(REQUEST_STD_CHARSET)
69 , m_contentType(CT_FORM)
73 m_server.assign(uri.Host());
74 if (uri.Scheme() && strncmp(uri.Scheme(),
"https", 5) == 0)
77 m_port = uri.Port() ? uri.Port() : 443;
80 m_port = uri.Port() ? uri.Port() : 80;
84 m_service_url.append(uri.Path());
87 RequestAcceptEncoding(
true);
90 WSRequest::~WSRequest()
94 void WSRequest::RequestService(
const std::string& url, HRM_t method)
97 m_service_method = method;
100 void WSRequest::RequestAccept(CT_t contentType)
102 m_accept = contentType;
105 void WSRequest::RequestAcceptEncoding(
bool yesno)
109 SetHeader(
"Accept-Encoding",
"gzip, deflate");
111 SetHeader(
"Accept-Encoding",
"");
114 SetHeader(
"Accept-Encoding",
"");
118 void WSRequest::SetUserAgent(
const std::string& value)
123 void WSRequest::SetContentParam(
const std::string& param,
const std::string& value)
125 if (m_contentType != CT_FORM)
127 if (!m_contentData.empty())
128 m_contentData.append(
"&");
129 m_contentData.append(param).append(
"=").append(urlencode(value));
132 void WSRequest::SetContentCustom(CT_t contentType,
const char *content)
134 m_contentType = contentType;
135 m_contentData = content;
138 void WSRequest::SetHeader(
const std::string& field,
const std::string& value)
140 m_headers[field] = value;
143 void WSRequest::ClearContent()
145 m_contentData.clear();
146 m_contentType = CT_FORM;
149 void WSRequest::MakeMessage(std::string& msg)
const 151 switch (m_service_method)
157 MakeMessagePOST(msg);
160 MakeMessageHEAD(msg);
163 MakeMessageHEAD(msg,
"SUBSCRIBE");
165 case HRM_UNSUBSCRIBE:
166 MakeMessageHEAD(msg,
"UNSUBSCRIBE");
169 MakeMessagePOST(msg,
"NOTIFY");
176 void WSRequest::MakeMessageGET(std::string& msg,
const char* method)
const 182 msg.append(method).append(
" ").append(m_service_url);
183 if (!m_contentData.empty())
184 msg.append(
"?").append(m_contentData);
185 msg.append(
" " REQUEST_PROTOCOL
"\r\n");
186 sprintf(buf,
"%u", m_port);
187 msg.append(
"Host: ").append(m_server).append(
":").append(buf).append(
"\r\n");
188 if (m_userAgent.empty())
189 msg.append(
"User-Agent: " REQUEST_USER_AGENT
"\r\n");
191 msg.append(
"User-Agent: ").append(m_userAgent).append(
"\r\n");
192 msg.append(
"Connection: " REQUEST_CONNECTION
"\r\n");
193 if (m_accept != CT_NONE)
194 msg.append(
"Accept: ").append(MimeFromContentType(m_accept)).append(
"\r\n");
195 msg.append(
"Accept-Charset: ").append(m_charset).append(
"\r\n");
196 for (std::map<std::string, std::string>::const_iterator it = m_headers.begin(); it != m_headers.end(); ++it)
197 msg.append(it->first).append(
": ").append(it->second).append(
"\r\n");
201 void WSRequest::MakeMessagePOST(std::string& msg,
const char* method)
const 204 size_t content_len = m_contentData.size();
208 msg.append(method).append(
" ").append(m_service_url).append(
" " REQUEST_PROTOCOL
"\r\n");
209 sprintf(buf,
"%u", m_port);
210 msg.append(
"Host: ").append(m_server).append(
":").append(buf).append(
"\r\n");
211 if (m_userAgent.empty())
212 msg.append(
"User-Agent: " REQUEST_USER_AGENT
"\r\n");
214 msg.append(
"User-Agent: ").append(m_userAgent).append(
"\r\n");
215 msg.append(
"Connection: " REQUEST_CONNECTION
"\r\n");
216 if (m_accept != CT_NONE)
217 msg.append(
"Accept: ").append(MimeFromContentType(m_accept)).append(
"\r\n");
218 msg.append(
"Accept-Charset: ").append(m_charset).append(
"\r\n");
221 sprintf(buf,
"%lu", (
unsigned long)content_len);
222 msg.append(
"Content-Type: ").append(MimeFromContentType(m_contentType));
223 msg.append(
"; charset=" REQUEST_STD_CHARSET
"\r\n");
224 msg.append(
"Content-Length: ").append(buf).append(
"\r\n");
226 for (std::map<std::string, std::string>::const_iterator it = m_headers.begin(); it != m_headers.end(); ++it)
227 msg.append(it->first).append(
": ").append(it->second).append(
"\r\n");
230 msg.append(m_contentData);
233 void WSRequest::MakeMessageHEAD(std::string& msg,
const char* method)
const 239 msg.append(method).append(
" ").append(m_service_url);
240 if (!m_contentData.empty())
241 msg.append(
"?").append(m_contentData);
242 msg.append(
" " REQUEST_PROTOCOL
"\r\n");
243 sprintf(buf,
"%u", m_port);
244 msg.append(
"Host: ").append(m_server).append(
":").append(buf).append(
"\r\n");
245 if (m_userAgent.empty())
246 msg.append(
"User-Agent: " REQUEST_USER_AGENT
"\r\n");
248 msg.append(
"User-Agent: ").append(m_userAgent).append(
"\r\n");
249 msg.append(
"Connection: " REQUEST_CONNECTION
"\r\n");
250 if (m_accept != CT_NONE)
251 msg.append(
"Accept: ").append(MimeFromContentType(m_accept)).append(
"\r\n");
252 msg.append(
"Accept-Charset: ").append(m_charset).append(
"\r\n");
253 for (std::map<std::string, std::string>::const_iterator it = m_headers.begin(); it != m_headers.end(); ++it)
254 msg.append(it->first).append(
": ").append(it->second).append(
"\r\n");