Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
THttpCallArg.cxx
Go to the documentation of this file.
1// $Id$
2// Author: Sergey Linev 21/05/2015
3
4/*************************************************************************
5 * Copyright (C) 1995-2013, Rene Brun and Fons Rademakers. *
6 * All rights reserved. *
7 * *
8 * For the licensing terms see $ROOTSYS/LICENSE. *
9 * For the list of contributors see $ROOTSYS/README/CREDITS. *
10 *************************************************************************/
11
12#include "THttpCallArg.h"
13
14#include <string.h>
15#include "RZip.h"
16#include "THttpWSEngine.h"
17
18//////////////////////////////////////////////////////////////////////////
19// //
20// THttpCallArg //
21// //
22// Contains arguments for single HTTP call //
23// Must be used in THttpEngine to process incoming http requests //
24// //
25//////////////////////////////////////////////////////////////////////////
26
28
29////////////////////////////////////////////////////////////////////////////////
30/// destructor
31
33{
34}
35
36////////////////////////////////////////////////////////////////////////////////
37/// method used to get or set http header in the string buffer
38/// Header has following format:
39/// field1 : value1\\r\\n
40/// field2 : value2\\r\\n
41/// Such format corresponds to header format in HTTP requests
42
43TString THttpCallArg::AccessHeader(TString &buf, const char *name, const char *value, Bool_t doing_set)
44{
45 if (name == 0)
46 return TString();
47
48 Int_t curr = 0;
49
50 while (curr < buf.Length() - 2) {
51
52 Int_t next = buf.Index("\r\n", curr);
53 if (next == kNPOS)
54 break; // should never happen
55
56 if (buf.Index(name, curr) != curr) {
57 curr = next + 2;
58 continue;
59 }
60
61 if ((value == 0) && doing_set) {
62 // special case - empty value means that field must be removed completely
63 buf.Remove(curr, next - curr + 2);
64 return TString();
65 }
66
67 curr += strlen(name);
68 while ((curr < next) && (buf[curr] != ':'))
69 curr++;
70 curr++;
71 while ((curr < next) && (buf[curr] == ' '))
72 curr++;
73
74 if (value == 0)
75 return buf(curr, next - curr);
76 buf.Remove(curr, next - curr);
77 buf.Insert(curr, value);
78 return TString(value);
79 }
80
81 if (value == 0)
82 return TString();
83
84 buf.Append(TString::Format("%s: %s\r\n", name, value));
85 return TString(value);
86}
87
88////////////////////////////////////////////////////////////////////////////////
89/// method used to counter number of headers or returns name of specified header
90
92{
93 Int_t curr(0), cnt(0);
94
95 while (curr < buf.Length() - 2) {
96
97 Int_t next = buf.Index("\r\n", curr);
98 if (next == kNPOS)
99 break; // should never happen
100
101 if (cnt == number) {
102 // we should extract name of header
103 Int_t separ = curr + 1;
104 while ((separ < next) && (buf[separ] != ':'))
105 separ++;
106 return buf(curr, separ - curr);
107 }
108
109 curr = next + 2;
110 cnt++;
111 }
112
113 // return total number of headers
114 if (number == -1111)
115 return TString::Format("%d", cnt);
116 return TString();
117}
118
119
120////////////////////////////////////////////////////////////////////////////////
121/// Set content as text.
122/// Content will be copied by THttpCallArg
123void THttpCallArg::SetContent(const char *cont)
124{
125 if (cont)
126 fContent = cont;
127 else
128 fContent.clear();
129}
130
131////////////////////////////////////////////////////////////////////////////////
132/// Set text or binary content directly
133/// After method call argument cont will be in undefined state
134
135void THttpCallArg::SetContent(std::string &&cont)
136{
137 fContent = cont;
138}
139
140////////////////////////////////////////////////////////////////////////////////
141/// Set content type as "text/plain"
142
144{
145 SetContentType("text/plain");
146}
147
148////////////////////////////////////////////////////////////////////////////////
149/// Set content type as "text/plain" and also assigns content
150/// After method call argument \param txt will be in undefined state
151
152void THttpCallArg::SetTextContent(std::string &&txt)
153{
154 SetText();
155 fContent = txt;
156}
157
158////////////////////////////////////////////////////////////////////////////////
159/// Set content type as "text/xml"
160
162{
163 SetContentType("text/xml");
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Set content type as "text/xml" and also assigns content
168/// After method call argument \param xml will be in undefined state
169
170void THttpCallArg::SetXmlContent(std::string &&xml)
171{
172 SetXml();
173 fContent = xml;
174}
175
176////////////////////////////////////////////////////////////////////////////////
177/// Set content type as "application/json"
178
180{
181 SetContentType("application/json");
182}
183
184////////////////////////////////////////////////////////////////////////////////
185/// Set content type as "application/json" and also assigns content
186/// After method call argument \param json will be in undefined state
187
188void THttpCallArg::SetJsonContent(std::string &&json)
189{
190 SetJson();
191 fContent = json;
192}
193
194////////////////////////////////////////////////////////////////////////////////
195/// Set content type as "application/x-binary"
196
198{
199 SetContentType("application/x-binary");
200}
201
202////////////////////////////////////////////////////////////////////////////////
203/// Set content type as "application/x-binary" and also assigns content
204/// After method call argument \param bin will be in undefined state
205
206void THttpCallArg::SetBinaryContent(std::string &&bin)
207{
208 SetBinary();
209 fContent = bin;
210}
211
212////////////////////////////////////////////////////////////////////////////////
213/// \deprecated Use signature with std::string
214/// Set data, posted with the request
215/// If make_copy==kFALSE, data will be released with free(data) call
216
217void THttpCallArg::SetPostData(void *data, Long_t length, Bool_t make_copy)
218{
219 fPostData.resize(length);
220
221 if (data && length) {
222 std::copy((const char *)data, (const char *)data + length, fPostData.begin());
223 if (!make_copy) free(data); // it supposed to get ownership over the buffer
224 }
225}
226
227////////////////////////////////////////////////////////////////////////////////
228/// set data, which is posted with the request
229/// Although std::string is used, not only text data can be assigned -
230/// std::string can contain any sequence of symbols
231
232void THttpCallArg::SetPostData(std::string &&data)
233{
234 fPostData = data;
235}
236
237////////////////////////////////////////////////////////////////////////////////
238/// Assign websocket identifier from the engine
239
241{
242 SetWSId(fWSEngine->GetId());
243}
244
245////////////////////////////////////////////////////////////////////////////////
246/// takeout websocket handle with HTTP call
247/// can be done only once
248
249std::shared_ptr<THttpWSEngine> THttpCallArg::TakeWSEngine()
250{
251 auto res = fWSEngine;
252 fWSEngine.reset();
253 return res;
254}
255
256////////////////////////////////////////////////////////////////////////////////
257/// Replace all occurrences of \param from by \param to in content
258/// Used only internally
259
260void THttpCallArg::ReplaceAllinContent(const std::string &from, const std::string &to, bool once)
261{
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);
265 if (once) return;
266 start_pos += to.length(); // Handles case where 'to' is a substring of 'from'
267 }
268}
269
270////////////////////////////////////////////////////////////////////////////////
271/// set complete path of requested http element
272/// For instance, it could be "/folder/subfolder/get.bin"
273/// Here "/folder/subfolder/" is element path and "get.bin" requested file.
274/// One could set path and file name separately
275
276void THttpCallArg::SetPathAndFileName(const char *fullpath)
277{
280
281 if (fullpath == 0)
282 return;
283
284 const char *rslash = strrchr(fullpath, '/');
285 if (rslash == 0) {
286 fFileName = fullpath;
287 } else {
288 while ((fullpath != rslash) && (*fullpath == '/'))
289 fullpath++;
290 fPathName.Append(fullpath, rslash - fullpath);
291 if (fPathName == "/")
293 fFileName = rslash + 1;
294 }
295}
296
297////////////////////////////////////////////////////////////////////////////////
298/// return specified header
299
301{
302 if ((name == 0) || (*name == 0))
303 return TString();
304
305 if (strcmp(name, "Content-Type") == 0)
306 return fContentType;
307 if (strcmp(name, "Content-Length") == 0)
308 return TString::Format("%ld", GetContentLength());
309
310 return AccessHeader(fHeader, name);
311}
312
313////////////////////////////////////////////////////////////////////////////////
314/// Set name: value pair to reply header
315/// Content-Type field handled separately - one should use SetContentType() method
316/// Content-Length field cannot be set at all;
317
318void THttpCallArg::AddHeader(const char *name, const char *value)
319{
320 if ((name == 0) || (*name == 0) || (strcmp(name, "Content-Length") == 0))
321 return;
322
323 if (strcmp(name, "Content-Type") == 0)
324 SetContentType(value);
325 else
326 AccessHeader(fHeader, name, value, kTRUE);
327}
328
329////////////////////////////////////////////////////////////////////////////////
330/// Set CacheControl http header to disable browser caching
331
333{
334 AddHeader("Cache-Control", "private, no-cache, no-store, must-revalidate, max-age=0, proxy-revalidate, s-maxage=0");
335}
336
337////////////////////////////////////////////////////////////////////////////////
338/// Fills HTTP header, which can be send at the beggining of reply on the http request
339/// \param name is HTTP protocol name (default "HTTP/1.1")
340
341std::string THttpCallArg::FillHttpHeader(const char *name)
342{
343 std::string hdr(name ? name : "HTTP/1.1");
344
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");
349 else
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"
354 "%s\r\n",
356
357 return hdr;
358}
359
360////////////////////////////////////////////////////////////////////////////////
361/// compress reply data with gzip compression
362
364{
365 char *objbuf = (char *)GetContent();
366 Long_t objlen = GetContentLength();
367
368 unsigned long objcrc = R__crc32(0, NULL, 0);
369 objcrc = R__crc32(objcrc, (const unsigned char *)objbuf, objlen);
370
371 // 10 bytes (ZIP header), compressed data, 8 bytes (CRC and original length)
372 Int_t buflen = 10 + objlen + 8;
373 if (buflen < 512)
374 buflen = 512;
375
376 std::string buffer;
377 buffer.resize(buflen);
378
379 char *bufcur = (char *)buffer.data();
380
381 *bufcur++ = 0x1f; // first byte of ZIP identifier
382 *bufcur++ = 0x8b; // second byte of ZIP identifier
383 *bufcur++ = 0x08; // compression method
384 *bufcur++ = 0x00; // FLAG - empty, no any file names
385 *bufcur++ = 0; // empty timestamp
386 *bufcur++ = 0; //
387 *bufcur++ = 0; //
388 *bufcur++ = 0; //
389 *bufcur++ = 0; // XFL (eXtra FLags)
390 *bufcur++ = 3; // OS 3 means Unix
391 // strcpy(bufcur, "item.json");
392 // bufcur += strlen("item.json")+1;
393
394 char dummy[8];
395 memcpy(dummy, bufcur - 6, 6);
396
397 // R__memcompress fills first 6 bytes with own header, therefore just overwrite them
398 unsigned long ziplen = R__memcompress(bufcur - 6, objlen + 6, objbuf, objlen);
399
400 memcpy(bufcur - 6, dummy, 6);
401
402 bufcur += (ziplen - 6); // jump over compressed data (6 byte is extra ROOT header)
403
404 // write CRC32
405 *bufcur++ = objcrc & 0xff;
406 *bufcur++ = (objcrc >> 8) & 0xff;
407 *bufcur++ = (objcrc >> 16) & 0xff;
408 *bufcur++ = (objcrc >> 24) & 0xff;
409
410 // write original data length
411 *bufcur++ = objlen & 0xff;
412 *bufcur++ = (objlen >> 8) & 0xff;
413 *bufcur++ = (objlen >> 16) & 0xff;
414 *bufcur++ = (objlen >> 24) & 0xff;
415
416 buffer.resize(bufcur - (char *)buffer.data());
417
418 SetContent(std::move(buffer));
419
420 SetEncoding("gzip");
421
422 return kTRUE;
423}
424
425////////////////////////////////////////////////////////////////////////////////
426/// method used to notify condition which waiting when operation will complete
427/// Condition notified only if not-postponed state is set
428
430{
431 if (!fNotifyFlag && !IsPostponed()) {
433 HttpReplied();
434 }
435}
436
437////////////////////////////////////////////////////////////////////////////////
438/// virtual method to inform object that http request is processed
439/// Normally condition is notified and waiting thread will be awaked
440/// One could reimplement this method in sub-class
441
443{
444 fCond.notify_one();
445}
const Ssiz_t kNPOS
Definition RtypesCore.h:115
long Long_t
Definition RtypesCore.h:54
const Bool_t kTRUE
Definition RtypesCore.h:91
#define ClassImp(name)
Definition Rtypes.h:364
char name[80]
Definition TGX11.cxx:110
char * Form(const char *fmt,...)
#define free
Definition civetweb.c:1539
std::string fPostData
! data received with post request - text - or binary
Bool_t fNotifyFlag
! indicate that notification called
Bool_t CompressWithGzip()
compress reply data with gzip compression
void SetJson()
Set content type as "application/json".
TString GetHeader(const char *name)
return specified header
std::condition_variable fCond
! condition used to wait for processing
void SetPostData(void *data, Long_t length, Bool_t make_copy=kFALSE)
std::shared_ptr< THttpWSEngine > TakeWSEngine()
takeout websocket handle with HTTP call can be done only once
void AddHeader(const char *name, const char *value)
Set name: value pair to reply header Content-Type field handled separately - one should use SetConten...
void SetText()
Set content type as "text/plain".
void SetTextContent(std::string &&txt)
Set content type as "text/plain" and also assigns content After method call argument.
virtual void HttpReplied()
virtual method to inform object that http request is processed Normally condition is notified and wai...
void ReplaceAllinContent(const std::string &from, const std::string &to, bool once=false)
Replace all occurrences of.
TString fPathName
! item path
std::shared_ptr< THttpWSEngine > fWSEngine
! web-socket engine, which supplied to run created web socket
void NotifyCondition()
method used to notify condition which waiting when operation will complete Condition notified only if...
void SetPathAndFileName(const char *fullpath)
set complete path of requested http element For instance, it could be "/folder/subfolder/get....
TString fContentType
! type of content
Long_t GetContentLength() const
Bool_t Is404() const
virtual ~THttpCallArg()
destructor
TString CountHeader(const TString &buf, Int_t number=-1111) const
method used to counter number of headers or returns name of specified header
const void * GetContent() const
void SetBinary()
Set content type as "application/x-binary".
void AddNoCacheHeader()
Set CacheControl http header to disable browser caching.
TString AccessHeader(TString &buf, const char *name, const char *value=nullptr, Bool_t doing_set=kFALSE)
method used to get or set http header in the string buffer Header has following format: field1 : valu...
void SetXml()
Set content type as "text/xml".
void SetContent(const char *cont)
Set content as text.
Bool_t IsPostponed() const
void SetWSId(UInt_t id)
set web-socket id
TString fFileName
! file name
std::string fContent
! content - text or binary
void SetBinaryContent(std::string &&bin)
Set content type as "application/x-binary" and also assigns content After method call argument.
void SetContentType(const char *typ)
set content type like "text/xml" or "application/json"
void SetEncoding(const char *typ)
Set Content-Encoding header like gzip.
void SetJsonContent(std::string &&json)
Set content type as "application/json" and also assigns content After method call argument.
const char * GetContentType() const
void AssignWSId()
Assign websocket identifier from the engine.
void SetXmlContent(std::string &&xml)
Set content type as "text/xml" and also assigns content After method call argument.
std::string FillHttpHeader(const char *header=nullptr)
Fills HTTP header, which can be send at the beggining of reply on the http request.
TString fHeader
! response header like ContentEncoding, Cache-Control and so on
Basic string class.
Definition TString.h:136
Ssiz_t Length() const
Definition TString.h:410
TString & Insert(Ssiz_t pos, const char *s)
Definition TString.h:649
void Clear()
Clear string without changing its capacity.
Definition TString.cxx:1196
const char * Data() const
Definition TString.h:369
TString & Remove(Ssiz_t pos)
Definition TString.h:673
TString & Append(const char *cs)
Definition TString.h:564
static TString Format(const char *fmt,...)
Static method which formats a string using a printf style format descriptor and return a TString.
Definition TString.cxx:2331
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:639