27 ClassImp(THttpCallArg);
32 THttpCallArg::~THttpCallArg()
43 TString THttpCallArg::AccessHeader(TString &buf,
const char *name,
const char *value, Bool_t doing_set)
50 while (curr < buf.Length() - 2) {
52 Int_t next = buf.Index(
"\r\n", curr);
56 if (buf.Index(name, curr) != curr) {
61 if ((value == 0) && doing_set) {
63 buf.Remove(curr, next - curr + 2);
68 while ((curr < next) && (buf[curr] !=
':'))
71 while ((curr < next) && (buf[curr] ==
' '))
75 return buf(curr, next - curr);
76 buf.Remove(curr, next - curr);
77 buf.Insert(curr, value);
78 return TString(value);
84 buf.Append(TString::Format(
"%s: %s\r\n", name, value));
85 return TString(value);
91 TString THttpCallArg::CountHeader(
const TString &buf, Int_t number)
const
93 Int_t curr(0), cnt(0);
95 while (curr < buf.Length() - 2) {
97 Int_t next = buf.Index(
"\r\n", curr);
103 Int_t separ = curr + 1;
104 while ((separ < next) && (buf[separ] !=
':'))
106 return buf(curr, separ - curr);
115 return TString::Format(
"%d", cnt);
123 void THttpCallArg::SetContent(
const char *cont)
135 void THttpCallArg::SetContent(std::string &&cont)
143 void THttpCallArg::SetText()
145 SetContentType(
"text/plain");
152 void THttpCallArg::SetTextContent(std::string &&txt)
161 void THttpCallArg::SetXml()
163 SetContentType(
"text/xml");
170 void THttpCallArg::SetXmlContent(std::string &&xml)
179 void THttpCallArg::SetJson()
181 SetContentType(
"application/json");
188 void THttpCallArg::SetJsonContent(std::string &&json)
197 void THttpCallArg::SetBinary()
199 SetContentType(
"application/x-binary");
206 void THttpCallArg::SetBinaryContent(std::string &&bin)
217 void THttpCallArg::SetPostData(
void *data, Long_t length, Bool_t make_copy)
219 fPostData.resize(length);
221 if (data && length) {
222 std::copy((
const char *)data, (
const char *)data + length, fPostData.begin());
223 if (!make_copy) free(data);
232 void THttpCallArg::SetPostData(std::string &&data)
240 void THttpCallArg::AssignWSId()
242 SetWSId(fWSEngine->GetId());
249 std::shared_ptr<THttpWSEngine> THttpCallArg::TakeWSEngine()
251 auto res = fWSEngine;
260 void THttpCallArg::ReplaceAllinContent(
const std::string &from,
const std::string &to,
bool once)
262 std::size_t start_pos = 0;
263 while((start_pos = fContent.find(from, start_pos)) != std::string::npos) {
264 fContent.replace(start_pos, from.length(), to);
266 start_pos += to.length();
276 void THttpCallArg::SetPathAndFileName(
const char *fullpath)
284 const char *rslash = strrchr(fullpath,
'/');
286 fFileName = fullpath;
288 while ((fullpath != rslash) && (*fullpath ==
'/'))
290 fPathName.Append(fullpath, rslash - fullpath);
291 if (fPathName ==
"/")
293 fFileName = rslash + 1;
300 TString THttpCallArg::GetHeader(
const char *name)
302 if ((name == 0) || (*name == 0))
305 if (strcmp(name,
"Content-Type") == 0)
307 if (strcmp(name,
"Content-Length") == 0)
308 return TString::Format(
"%ld", GetContentLength());
310 return AccessHeader(fHeader, name);
318 void THttpCallArg::AddHeader(
const char *name,
const char *value)
320 if ((name == 0) || (*name == 0) || (strcmp(name,
"Content-Length") == 0))
323 if (strcmp(name,
"Content-Type") == 0)
324 SetContentType(value);
326 AccessHeader(fHeader, name, value, kTRUE);
332 void THttpCallArg::AddNoCacheHeader()
334 AddHeader(
"Cache-Control",
"private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0");
341 std::string THttpCallArg::FillHttpHeader(
const char *name)
343 std::string hdr(name ? name :
"HTTP/1.1");
345 if ((fContentType.Length() == 0) || Is404())
346 hdr.append(
" 404 Not Found\r\n"
347 "Content-Length: 0\r\n"
348 "Connection: close\r\n\r\n");
350 hdr.append(Form(
" 200 OK\r\n"
351 "Content-Type: %s\r\n"
352 "Connection: keep-alive\r\n"
353 "Content-Length: %ld\r\n"
355 GetContentType(), GetContentLength(), fHeader.Data()));
363 Bool_t THttpCallArg::CompressWithGzip()
365 char *objbuf = (
char *)GetContent();
366 Long_t objlen = GetContentLength();
368 unsigned long objcrc = R__crc32(0, NULL, 0);
369 objcrc = R__crc32(objcrc, (
const unsigned char *)objbuf, objlen);
372 Int_t buflen = 10 + objlen + 8;
377 buffer.resize(buflen);
379 char *bufcur = (
char *)buffer.data();
395 memcpy(dummy, bufcur - 6, 6);
398 unsigned long ziplen = R__memcompress(bufcur - 6, objlen + 6, objbuf, objlen);
400 memcpy(bufcur - 6, dummy, 6);
402 bufcur += (ziplen - 6);
405 *bufcur++ = objcrc & 0xff;
406 *bufcur++ = (objcrc >> 8) & 0xff;
407 *bufcur++ = (objcrc >> 16) & 0xff;
408 *bufcur++ = (objcrc >> 24) & 0xff;
411 *bufcur++ = objlen & 0xff;
412 *bufcur++ = (objlen >> 8) & 0xff;
413 *bufcur++ = (objlen >> 16) & 0xff;
414 *bufcur++ = (objlen >> 24) & 0xff;
416 buffer.resize(bufcur - (
char *)buffer.data());
418 SetContent(std::move(buffer));
429 void THttpCallArg::NotifyCondition()
431 if (!fNotifyFlag && !IsPostponed()) {
442 void THttpCallArg::HttpReplied()