Logo ROOT  
Reference Guide
 
Loading...
Searching...
No Matches
TWebFile.cxx
Go to the documentation of this file.
1// @(#)root/net:$Id$
2// Author: Fons Rademakers 17/01/97
3
4/*************************************************************************
5 * Copyright (C) 1995-2000, 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//////////////////////////////////////////////////////////////////////////
13// //
14// TWebFile //
15// //
16// A TWebFile is like a normal TFile except that it reads its data //
17// via a standard apache web server. A TWebFile is a read-only file. //
18// //
19//////////////////////////////////////////////////////////////////////////
20
21#include "TWebFile.h"
22#include "TROOT.h"
23#include "TSocket.h"
24#include "Bytes.h"
25#include "TError.h"
26#include "TSystem.h"
27#include "TBase64.h"
28#include "TVirtualPerfStats.h"
29#ifdef R__SSL
30#include "TSSLSocket.h"
31#endif
32
33#include <errno.h>
34#include <stdlib.h>
35#include <string.h>
36
37#ifdef WIN32
38# ifndef EADDRINUSE
39# define EADDRINUSE 10048
40# endif
41# ifndef EISCONN
42# define EISCONN 10056
43# endif
44#endif
45
46static const char *gUserAgent = "User-Agent: ROOT-TWebFile/1.1";
47
49
51
52
53// Internal class used to manage the socket that may stay open between
54// calls when HTTP/1.1 protocol is used
56private:
57 TWebFile *fWebFile; // associated web file
58public:
61 void ReOpen();
62};
63
64////////////////////////////////////////////////////////////////////////////////
65/// Open web file socket.
66
68{
69 fWebFile = f;
70 if (!f->fSocket)
71 ReOpen();
72}
73
74////////////////////////////////////////////////////////////////////////////////
75/// Close socket in case not HTTP/1.1 protocol or when explicitly requested.
76
78{
79 if (!fWebFile->fHTTP11) {
80 delete fWebFile->fSocket;
81 fWebFile->fSocket = nullptr;
82 }
83}
84
85////////////////////////////////////////////////////////////////////////////////
86/// Re-open web file socket.
87
89{
90 if (fWebFile->fSocket) {
91 delete fWebFile->fSocket;
92 fWebFile->fSocket = nullptr;
93 }
94
95 TUrl connurl;
96 if (fWebFile->fProxy.IsValid())
97 connurl = fWebFile->fProxy;
98 else
99 connurl = fWebFile->fUrl;
100
101 for (Int_t i = 0; i < 5; i++) {
102 if (strcmp(connurl.GetProtocol(), "https") == 0) {
103#ifdef R__SSL
104 fWebFile->fSocket = new TSSLSocket(connurl.GetHost(), connurl.GetPort());
105#else
106 ::Error("TWebSocket::ReOpen", "library compiled without SSL, https not supported");
107 return;
108#endif
109 } else
110 fWebFile->fSocket = new TSocket(connurl.GetHost(), connurl.GetPort());
111
112 if (!fWebFile->fSocket || !fWebFile->fSocket->IsValid()) {
113 delete fWebFile->fSocket;
114 fWebFile->fSocket = nullptr;
115 if (gSystem->GetErrno() == EADDRINUSE || gSystem->GetErrno() == EISCONN) {
116 gSystem->Sleep(i*10);
117 } else {
118 ::Error("TWebSocket::ReOpen", "cannot connect to host %s (errno=%d)",
120 return;
121 }
122 } else
123 return;
124 }
125}
126
127
129
130////////////////////////////////////////////////////////////////////////////////
131/// Create a Web file object. A web file is the same as a read-only
132/// TFile except that it is being read via a HTTP server. The url
133/// argument must be of the form: http://host.dom.ain/file.root.
134/// The opt can be "NOPROXY", to bypass any set "http_proxy" shell
135/// variable. The proxy can be specified as (in sh, or equivalent csh):
136/// export http_proxy=http://pcsalo.cern.ch:3128
137/// The proxy can also be specified via the static method TWebFile::SetProxy().
138/// Basic authentication (AuthType Basic) is supported. The user name and
139/// passwd can be specified in the url like this:
140/// http://username:mypasswd@pcsalo.cern.ch/files/aap.root
141/// If the file specified in the URL does not exist or is not accessible
142/// the kZombie bit will be set in the TWebFile object. Use IsZombie()
143/// to see if the file is accessible. The preferred interface to this
144/// constructor is via TFile::Open().
145
146TWebFile::TWebFile(const char *url, Option_t *opt)
147 : TFile(url, strstr(opt, "_WITHOUT_GLOBALREGISTRATION") != nullptr ? "WEB_WITHOUT_GLOBALREGISTRATION" : "WEB"),
148 fSocket(0)
149{
150 TString option = opt;
152 if (option.Contains("NOPROXY", TString::kIgnoreCase))
153 fNoProxy = kTRUE;
154 CheckProxy();
155
156 Bool_t headOnly = kFALSE;
157 if (option.Contains("HEADONLY", TString::kIgnoreCase))
158 headOnly = kTRUE;
159
160 if (option == "IO")
161 return;
162
163 Init(headOnly);
164}
165
166////////////////////////////////////////////////////////////////////////////////
167/// Create a Web file object. A web file is the same as a read-only
168/// TFile except that it is being read via a HTTP server. Make sure url
169/// is a valid TUrl object.
170/// The opt can be "NOPROXY", to bypass any set "http_proxy" shell
171/// variable. The proxy can be specified as (in sh, or equivalent csh):
172/// export http_proxy=http://pcsalo.cern.ch:3128
173/// The proxy can also be specified via the static method TWebFile::SetProxy().
174/// Basic authentication (AuthType Basic) is supported. The user name and
175/// passwd can be specified in the url like this:
176/// http://username:mypasswd@pcsalo.cern.ch/files/aap.root
177/// If the file specified in the URL does not exist or is not accessible
178/// the kZombie bit will be set in the TWebFile object. Use IsZombie()
179/// to see if the file is accessible.
180
181TWebFile::TWebFile(TUrl url, Option_t *opt) : TFile(url.GetUrl(), "WEB"), fSocket(0)
182{
183 TString option = opt;
185 if (option.Contains("NOPROXY", TString::kIgnoreCase))
186 fNoProxy = kTRUE;
187 CheckProxy();
188
189 Bool_t headOnly = kFALSE;
190 if (option.Contains("HEADONLY", TString::kIgnoreCase))
191 headOnly = kTRUE;
192
193 Init(headOnly);
194}
195
196////////////////////////////////////////////////////////////////////////////////
197/// Cleanup.
198
200{
201 delete fSocket;
202 if (fFullCache) {
204 fFullCache = nullptr;
205 fFullCacheSize = 0;
206 }
207}
208
209////////////////////////////////////////////////////////////////////////////////
210/// Initialize a TWebFile object.
211
212void TWebFile::Init(Bool_t readHeadOnly)
213{
214 char buf[4];
215 int err;
216
217 fSocket = nullptr;
218 fSize = -1;
220 fHTTP11 = kFALSE;
221 fFullCache = nullptr;
222 fFullCacheSize = 0;
224
225 if ((err = GetHead()) < 0) {
226 if (readHeadOnly) {
227 fD = -1;
228 fWritten = err;
229 return;
230 }
231 if (err == -2) {
232 Error("TWebFile", "%s does not exist", fBasicUrl.Data());
233 MakeZombie();
235 return;
236 }
237 // err == -3 HEAD not supported, fall through and try ReadBuffer()
238 }
239 if (readHeadOnly) {
240 fD = -1;
241 return;
242 }
243
244 if (fIsRootFile) {
245 Seek(0);
246 if (ReadBuffer(buf, 4)) {
247 MakeZombie();
249 return;
250 }
251
252 if (strncmp(buf, "root", 4) && strncmp(buf, "PK", 2)) { // PK is zip file
253 Error("TWebFile", "%s is not a ROOT file", fBasicUrl.Data());
254 MakeZombie();
256 return;
257 }
258 }
259
261 fD = -2; // so TFile::IsOpen() will return true when in TFile::~TFile
262}
263
264////////////////////////////////////////////////////////////////////////////////
265/// Set GET command for use by ReadBuffer(s)10(), handle redirection if
266/// needed. Give full URL so Apache's virtual hosts solution works.
267
268void TWebFile::SetMsgReadBuffer10(const char *redirectLocation, Bool_t tempRedirect)
269{
270 TUrl oldUrl;
271 TString oldBasicUrl;
272
273 if (redirectLocation) {
274 if (tempRedirect) { // temp redirect
275 fUrlOrg = fUrl;
277 } else { // permanent redirect
278 fUrlOrg = "";
279 fBasicUrlOrg = "";
280 }
281
282 oldUrl = fUrl;
283 oldBasicUrl = fBasicUrl;
284
285 fUrl.SetUrl(redirectLocation);
287 fBasicUrl += "://";
289 fBasicUrl += ":";
291 fBasicUrl += "/";
293 // add query string again
294 TString rdl(redirectLocation);
295 if (rdl.Index("?") >= 0) {
296 rdl = rdl(rdl.Index("?"), rdl.Length());
297 fBasicUrl += rdl;
298 }
299 }
300
301 if (fMsgReadBuffer10 != "") {
302 // patch up existing command
303 if (oldBasicUrl != "") {
304 // change to redirection location
306 fMsgReadBuffer10.ReplaceAll(TString("Host: ")+oldUrl.GetHost(), TString("Host: ")+fUrl.GetHost());
307 } else if (fBasicUrlOrg != "") {
308 // change back from temp redirection location
311 fUrl = fUrlOrg;
313 fUrlOrg = "";
314 fBasicUrlOrg = "";
315 }
316 }
317
318 if (fBasicUrl == "") {
320 fBasicUrl += "://";
322 fBasicUrl += ":";
324 fBasicUrl += "/";
326 fBasicUrl += "?";
328 }
329
330 if (fMsgReadBuffer10 == "") {
331 fMsgReadBuffer10 = "GET ";
333 if (fHTTP11)
334 fMsgReadBuffer10 += " HTTP/1.1";
335 else
336 fMsgReadBuffer10 += " HTTP/1.0";
337 fMsgReadBuffer10 += "\r\n";
338 if (fHTTP11) {
339 fMsgReadBuffer10 += "Host: ";
341 fMsgReadBuffer10 += "\r\n";
342 }
345 fMsgReadBuffer10 += "\r\n";
346 fMsgReadBuffer10 += "Range: bytes=";
347 }
348}
349
350////////////////////////////////////////////////////////////////////////////////
351/// Check if shell var "http_proxy" has been set and should be used.
352
354{
355 if (fNoProxy)
356 return;
357
358 if (fgProxy.IsValid()) {
359 fProxy = fgProxy;
360 return;
361 }
362
363 TString proxy = gSystem->Getenv("http_proxy");
364 if (proxy != "") {
365 TUrl p(proxy);
366 if (strcmp(p.GetProtocol(), "http")) {
367 Error("CheckProxy", "protocol must be HTTP in proxy URL %s",
368 proxy.Data());
369 return;
370 }
371 fProxy = p;
372 if (gDebug > 0)
373 Info("CheckProxy", "using HTTP proxy %s", fProxy.GetUrl());
374 }
375}
376
377////////////////////////////////////////////////////////////////////////////////
378/// A TWebFile that has been correctly constructed is always considered open.
379
381{
382 return IsZombie() ? kFALSE : kTRUE;
383}
384
385////////////////////////////////////////////////////////////////////////////////
386/// Reopen a file with a different access mode, like from READ to
387/// UPDATE or from NEW, CREATE, RECREATE, UPDATE to READ. Thus the
388/// mode argument can be either "READ" or "UPDATE". The method returns
389/// 0 in case the mode was successfully modified, 1 in case the mode
390/// did not change (was already as requested or wrong input arguments)
391/// and -1 in case of failure, in which case the file cannot be used
392/// anymore. A TWebFile cannot be reopened in update mode.
393
395{
396 TString opt = mode;
397 opt.ToUpper();
398
399 if (opt != "READ" && opt != "UPDATE")
400 Error("ReOpen", "mode must be either READ or UPDATE, not %s", opt.Data());
401
402 if (opt == "UPDATE")
403 Error("ReOpen", "update mode not allowed for a TWebFile");
404
405 return 1;
406}
407
408////////////////////////////////////////////////////////////////////////////////
409/// Close a Web file. Close the socket connection and delete the cache
410/// See also the TFile::Close() function
411
413{
414 delete fSocket;
415 fSocket = nullptr;
416 if (fFullCache) {
418 fFullCache = nullptr;
419 fFullCacheSize = 0;
420 }
421 return TFile::Close(option);
422}
423
424////////////////////////////////////////////////////////////////////////////////
425/// Read specified byte range from remote file via HTTP daemon. This
426/// routine connects to the remote host, sends the request and returns
427/// the buffer. Returns kTRUE in case of error.
428
430{
431 Int_t st;
432 if ((st = ReadBufferViaCache(buf, len))) {
433 if (st == 2)
434 return kTRUE;
435 return kFALSE;
436 }
437
438 if (!fHasModRoot)
439 return ReadBuffer10(buf, len);
440
441 // Give full URL so Apache's virtual hosts solution works.
442 // Use protocol 0.9 for efficiency, we are not interested in the 1.0 headers.
443 if (fMsgReadBuffer == "") {
444 fMsgReadBuffer = "GET ";
446 fMsgReadBuffer += "?";
447 }
449 msg += fOffset;
450 msg += ":";
451 msg += len;
452 msg += "\r\n";
453
454 if (GetFromWeb(buf, len, msg) == -1)
455 return kTRUE;
456
457 fOffset += len;
458
459 return kFALSE;
460}
461
462////////////////////////////////////////////////////////////////////////////////
463/// Read specified byte range from remote file via HTTP daemon. This
464/// routine connects to the remote host, sends the request and returns
465/// the buffer. Returns kTRUE in case of error.
466
468{
469 SetOffset(pos);
470 return ReadBuffer(buf, len);
471}
472
473////////////////////////////////////////////////////////////////////////////////
474/// Read specified byte range from remote file via HTTP 1.0 daemon (without
475/// mod-root installed). This routine connects to the remote host, sends the
476/// request and returns the buffer. Returns kTRUE in case of error.
477
479{
481
483 msg += fOffset;
484 msg += "-";
485 msg += fOffset+len-1;
486 msg += "\r\n\r\n";
487
489
490 // in case when server does not support segments, let chance to recover
491 Int_t n = GetFromWeb10(buf, len, msg, 1, &apos, &len);
492 if (n == -1)
493 return kTRUE;
494 // The -2 error condition typically only happens when
495 // GetHead() failed because not implemented, in the first call to
496 // ReadBuffer() in Init(), it is not checked in ReadBuffers10().
497 if (n == -2) {
498 Error("ReadBuffer10", "%s does not exist", fBasicUrl.Data());
499 MakeZombie();
501 return kTRUE;
502 }
503
504 fOffset += len;
505
506 return kFALSE;
507}
508
509////////////////////////////////////////////////////////////////////////////////
510/// Read specified byte ranges from remote file via HTTP daemon.
511/// Reads the nbuf blocks described in arrays pos and len,
512/// where pos[i] is the seek position of block i of length len[i].
513/// Note that for nbuf=1, this call is equivalent to TFile::ReafBuffer
514/// This function is overloaded by TNetFile, TWebFile, etc.
515/// Returns kTRUE in case of failure.
516
518{
519 if (!fHasModRoot)
520 return ReadBuffers10(buf, pos, len, nbuf);
521
522 // Give full URL so Apache's virtual hosts solution works.
523 // Use protocol 0.9 for efficiency, we are not interested in the 1.0 headers.
524 if (fMsgReadBuffer == "") {
525 fMsgReadBuffer = "GET ";
527 fMsgReadBuffer += "?";
528 }
530
531 Int_t k = 0, n = 0, cnt = 0;
532 for (Int_t i = 0; i < nbuf; i++) {
533 if (n) msg += ",";
534 msg += pos[i] + fArchiveOffset;
535 msg += ":";
536 msg += len[i];
537 n += len[i];
538 cnt++;
539 if ((msg.Length() > 8000) || (cnt >= 200)) {
540 msg += "\r\n";
541 if (GetFromWeb(&buf[k], n, msg) == -1)
542 return kTRUE;
543 msg = fMsgReadBuffer;
544 k += n;
545 n = 0;
546 cnt = 0;
547 }
548 }
549
550 msg += "\r\n";
551
552 if (GetFromWeb(&buf[k], n, msg) == -1)
553 return kTRUE;
554
555 return kFALSE;
556}
557
558////////////////////////////////////////////////////////////////////////////////
559/// Read specified byte ranges from remote file via HTTP 1.0 daemon (without
560/// mod-root installed). Read the nbuf blocks described in arrays pos and len,
561/// where pos[i] is the seek position of block i of length len[i].
562/// Note that for nbuf=1, this call is equivalent to TFile::ReafBuffer
563/// This function is overloaded by TNetFile, TWebFile, etc.
564/// Returns kTRUE in case of failure.
565
567{
569
571
572 Int_t k = 0, n = 0, r, cnt = 0;
573 for (Int_t i = 0; i < nbuf; i++) {
574 if (n) msg += ",";
575 msg += pos[i] + fArchiveOffset;
576 msg += "-";
577 msg += pos[i] + fArchiveOffset + len[i] - 1;
578 n += len[i];
579 cnt++;
580 if ((msg.Length() > 8000) || (cnt >= 200) || (i+1 == nbuf)) {
581 msg += "\r\n\r\n";
582 r = GetFromWeb10(&buf[k], n, msg, cnt, pos + (i+1-cnt), len + (i+1-cnt));
583 if (r == -1)
584 return kTRUE;
585 msg = fMsgReadBuffer10;
586 k += n;
587 n = 0;
588 cnt = 0;
589 }
590 }
591
592 return kFALSE;
593}
594
595////////////////////////////////////////////////////////////////////////////////
596/// Extract requested segments from the cached content.
597/// Such cache can be produced when server suddenly returns full data instead of segments
598/// Returns -1 in case of error, 0 in case of success
599
600Int_t TWebFile::GetFromCache(char *buf, Int_t len, Int_t nseg, Long64_t *seg_pos, Int_t *seg_len)
601{
602 if (!fFullCache) return -1;
603
604 if (gDebug > 0)
605 Info("GetFromCache", "Extract %d segments total len %d from cached data", nseg, len);
606
607 Int_t curr = 0;
608 for (Int_t cnt=0;cnt<nseg;cnt++) {
609 // check that target buffer has enough space
610 if (curr + seg_len[cnt] > len) return -1;
611 // check that segment is inside cached area
612 if (fArchiveOffset + seg_pos[cnt] + seg_len[cnt] > fFullCacheSize) return -1;
613 char* src = (char*) fFullCache + fArchiveOffset + seg_pos[cnt];
614 memcpy(buf + curr, src, seg_len[cnt]);
615 curr += seg_len[cnt];
616 }
617
618 return 0;
619}
620
621////////////////////////////////////////////////////////////////////////////////
622/// Read request from web server. Returns -1 in case of error,
623/// 0 in case of success.
624
626{
627 TSocket *s;
628
629 if (!len) return 0;
630
631 Double_t start = 0;
632 if (gPerfStats) start = TTimeStamp();
633
634 TUrl connurl;
635 if (fProxy.IsValid())
636 connurl = fProxy;
637 else
638 connurl = fUrl;
639
640 if (strcmp(connurl.GetProtocol(), "https") == 0) {
641#ifdef R__SSL
642 s = new TSSLSocket(connurl.GetHost(), connurl.GetPort());
643#else
644 Error("GetFromWeb", "library compiled without SSL, https not supported");
645 return -1;
646#endif
647 } else
648 s = new TSocket(connurl.GetHost(), connurl.GetPort());
649
650 if (!s->IsValid()) {
651 Error("GetFromWeb", "cannot connect to host %s", fUrl.GetHost());
652 delete s;
653 return -1;
654 }
655
656 if (s->SendRaw(msg.Data(), msg.Length()) == -1) {
657 Error("GetFromWeb", "error sending command to host %s", fUrl.GetHost());
658 delete s;
659 return -1;
660 }
661
662 if (s->RecvRaw(buf, len) == -1) {
663 Error("GetFromWeb", "error receiving data from host %s", fUrl.GetHost());
664 delete s;
665 return -1;
666 }
667
668 // collect statistics
669 fBytesRead += len;
670 fReadCalls++;
671#ifdef R__WIN32
674#else
675 fgBytesRead += len;
676 fgReadCalls++;
677#endif
678
679 if (gPerfStats)
680 gPerfStats->FileReadEvent(this, len, start);
681
682 delete s;
683 return 0;
684}
685
686////////////////////////////////////////////////////////////////////////////////
687/// Read multiple byte range request from web server.
688/// Uses HTTP 1.0 daemon wihtout mod-root.
689/// Returns -2 in case file does not exist, -1 in case
690/// of error and 0 in case of success.
691
692Int_t TWebFile::GetFromWeb10(char *buf, Int_t len, const TString &msg, Int_t nseg, Long64_t *seg_pos, Int_t *seg_len)
693{
694 if (!len) return 0;
695
696 // if file content was cached, reuse it
697 if (fFullCache && (nseg>0))
698 return GetFromCache(buf, len, nseg, seg_pos, seg_len);
699
700 Double_t start = 0;
701 if (gPerfStats) start = TTimeStamp();
702
703 // open fSocket and close it when going out of scope
704 TWebSocket ws(this);
705
706 if (!fSocket || !fSocket->IsValid()) {
707 Error("GetFromWeb10", "cannot connect to host %s", fUrl.GetHost());
708 return -1;
709 }
710
711 if (gDebug > 0)
712 Info("GetFromWeb10", "sending HTTP request:\n%s", msg.Data());
713
714 if (fSocket->SendRaw(msg.Data(), msg.Length()) == -1) {
715 Error("GetFromWeb10", "error sending command to host %s", fUrl.GetHost());
716 return -1;
717 }
718
719 char line[8192];
720 Int_t n, ret = 0, nranges = 0, ltot = 0, redirect = 0;
721 TString boundary, boundaryEnd;
722 Long64_t first = -1, last = -1, tot, fullsize = 0;
723 TString redir;
724
725 while ((n = GetLine(fSocket, line, sizeof(line))) >= 0) {
726 if (n == 0) {
727 if (ret < 0)
728 return ret;
729 if (redirect) {
730 if (redir.IsNull()) {
731 // Some sites (s3.amazonaws.com) do not return a Location field on 301
732 Error("GetFromWeb10", "error - redirect without location from host %s", fUrl.GetHost());
733 return -1;
734 }
735
736 ws.ReOpen();
737 // set message to reflect the redirectLocation and add bytes field
739 msg_1 += fOffset;
740 msg_1 += "-";
741 msg_1 += fOffset+len-1;
742 msg_1 += "\r\n\r\n";
743 return GetFromWeb10(buf, len, msg_1);
744 }
745
746 if (first >= 0) {
747 Int_t ll = Int_t(last - first) + 1;
748 Int_t rsize;
749 if ((rsize = fSocket->RecvRaw(&buf[ltot], ll)) == -1) {
750 Error("GetFromWeb10", "error receiving data from host %s", fUrl.GetHost());
751 return -1;
752 }
753 else if (ll != rsize) {
754 Error("GetFromWeb10", "expected %d bytes, got %d", ll, rsize);
755 return -1;
756 }
757 ltot += ll;
758
759 first = -1;
760
761 if (boundary == "")
762 break; // not a multipart response
763 }
764
765 if (fullsize > 0) {
766
767 if (nseg <= 0) {
768 Error("GetFromWeb10","Need segments data to extract parts from full size %lld", fullsize);
769 return -1;
770 }
771
772 if (len > fullsize) {
773 Error("GetFromWeb10","Requested part %d longer than full size %lld", len, fullsize);
774 return -1;
775 }
776
777 if ((fFullCache == 0) && (fullsize <= GetMaxFullCacheSize())) {
778 // try to read file content into cache and than reuse it, limit cache by 2 GB
779 fFullCache = malloc(fullsize);
780 if (fFullCache != 0) {
781 if (fSocket->RecvRaw(fFullCache, fullsize) != fullsize) {
782 Error("GetFromWeb10", "error receiving data from host %s", fUrl.GetHost());
783 free(fFullCache); fFullCache = nullptr;
784 return -1;
785 }
786 fFullCacheSize = fullsize;
787 return GetFromCache(buf, len, nseg, seg_pos, seg_len);
788 }
789 // when cache allocation failed, try without cache
790 }
791
792 // check all segemnts are inside range and in sorted order
793 for (Int_t cnt=0;cnt<nseg;cnt++) {
794 if (fArchiveOffset + seg_pos[cnt] + seg_len[cnt] > fullsize) {
795 Error("GetFromWeb10","Requested segment %lld len %d is outside of full range %lld", seg_pos[cnt], seg_len[cnt], fullsize);
796 return -1;
797 }
798 if ((cnt>0) && (seg_pos[cnt-1] + seg_len[cnt-1] > seg_pos[cnt])) {
799 Error("GetFromWeb10","Requested segments are not in sorted order");
800 return -1;
801 }
802 }
803
804 Long64_t pos = 0;
805 char* curr = buf;
806 char dbuf[2048]; // dummy buffer for skip data
807
808 // now read complete file and take only requested segments into the buffer
809 for (Int_t cnt=0; cnt<nseg; cnt++) {
810 // first skip data before segment
811 while (pos < fArchiveOffset + seg_pos[cnt]) {
812 Long64_t ll = fArchiveOffset + seg_pos[cnt] - pos;
813 if (ll > Int_t(sizeof(dbuf))) ll = sizeof(dbuf);
814 if (fSocket->RecvRaw(dbuf, ll) != ll) {
815 Error("GetFromWeb10", "error receiving data from host %s", fUrl.GetHost());
816 return -1;
817 }
818 pos += ll;
819 }
820
821 // reading segment itself
822 if (fSocket->RecvRaw(curr, seg_len[cnt]) != seg_len[cnt]) {
823 Error("GetFromWeb10", "error receiving data from host %s", fUrl.GetHost());
824 return -1;
825 }
826 curr += seg_len[cnt];
827 pos += seg_len[cnt];
828 ltot += seg_len[cnt];
829 }
830
831 // now read file to the end
832 while (pos < fullsize) {
833 Long64_t ll = fullsize - pos;
834 if (ll > Int_t(sizeof(dbuf))) ll = sizeof(dbuf);
835 if (fSocket->RecvRaw(dbuf, ll) != ll) {
836 Error("GetFromWeb10", "error receiving data from host %s", fUrl.GetHost());
837 return -1;
838 }
839 pos += ll;
840 }
841
842 if (gDebug>0) Info("GetFromWeb10","Complete reading %d bytes in %d segments out of full size %lld", len, nseg, fullsize);
843
844 break;
845 }
846
847 continue;
848 }
849
850 if (gDebug > 0)
851 Info("GetFromWeb10", "header: %s", line);
852
853 if (boundaryEnd == line) {
854 if (gDebug > 0)
855 Info("GetFromWeb10", "got all headers");
856 break;
857 }
858 if (boundary == line) {
859 nranges++;
860 if (gDebug > 0)
861 Info("GetFromWeb10", "get new multipart byte range (%d)", nranges);
862 }
863
864 TString res = line;
865
866 if (res.BeginsWith("HTTP/1.")) {
867 if (res.BeginsWith("HTTP/1.1")) {
868 if (!fHTTP11)
869 fMsgReadBuffer10 = "";
870 fHTTP11 = kTRUE;
871 }
872 TString scode = res(9, 3);
873 Int_t code = scode.Atoi();
874 if (code >= 500) {
875 ret = -1;
876 TString mess = res(13, 1000);
877 Error("GetFromWeb10", "%s: %s (%d)", fBasicUrl.Data(), mess.Data(), code);
878 } else if (code >= 400) {
879 if (code == 404)
880 ret = -2; // file does not exist
881 else {
882 ret = -1;
883 TString mess = res(13, 1000);
884 Error("GetFromWeb10", "%s: %s (%d)", fBasicUrl.Data(), mess.Data(), code);
885 }
886 } else if (code >= 300) {
887 if (code == 301 || code == 303) {
888 redirect = 1; // permanent redirect
889 } else if (code == 302 || code == 307) {
890 // treat 302 as 303: permanent redirect
891 redirect = 1;
892 //redirect = 2; // temp redirect
893 } else {
894 ret = -1;
895 TString mess = res(13, 1000);
896 Error("GetFromWeb10", "%s: %s (%d)", fBasicUrl.Data(), mess.Data(), code);
897 }
898 } else if (code > 200) {
899 if (code != 206) {
900 ret = -1;
901 TString mess = res(13, 1000);
902 Error("GetFromWeb10", "%s: %s (%d)", fBasicUrl.Data(), mess.Data(), code);
903 }
904 } else if (code == 200) {
905 fullsize = -200; // make indication of code 200
906 Warning("GetFromWeb10",
907 "Server %s response with complete file, but only part of it was requested.\n"
908 "Check MaxRanges configuration parameter (if Apache is used)",
909 fUrl.GetHost());
910
911 }
912 } else if (res.BeginsWith("Content-Type: multipart")) {
913 boundary = res(res.Index("boundary=")+9, 1000);
914 if (boundary[0]=='"' && boundary[boundary.Length()-1]=='"') {
915 boundary = boundary(1,boundary.Length()-2);
916 }
917 boundary = "--" + boundary;
918 boundaryEnd = boundary + "--";
919 } else if (res.BeginsWith("Content-range:")) {
920#ifdef R__WIN32
921 sscanf(res.Data(), "Content-range: bytes %I64d-%I64d/%I64d", &first, &last, &tot);
922#else
923 sscanf(res.Data(), "Content-range: bytes %lld-%lld/%lld", &first, &last, &tot);
924#endif
925 if (fSize == -1) fSize = tot;
926 } else if (res.BeginsWith("Content-Range:")) {
927#ifdef R__WIN32
928 sscanf(res.Data(), "Content-Range: bytes %I64d-%I64d/%I64d", &first, &last, &tot);
929#else
930 sscanf(res.Data(), "Content-Range: bytes %lld-%lld/%lld", &first, &last, &tot);
931#endif
932 if (fSize == -1) fSize = tot;
933 } else if (res.BeginsWith("Content-Length:") && (fullsize == -200)) {
934#ifdef R__WIN32
935 sscanf(res.Data(), "Content-Length: %I64d", &fullsize);
936#else
937 sscanf(res.Data(), "Content-Length: %lld", &fullsize);
938#endif
939 } else if (res.BeginsWith("Location:") && redirect) {
940 redir = res(10, 1000);
941 if (redirect == 2) // temp redirect
943 else // permanent redirect
945 }
946 }
947
948 if (redirect && redir.IsNull()) {
949 Error("GetFromWeb10", "error - redirect without location from host %s", fUrl.GetHost());
950 }
951
952 if (n == -1 && fHTTP11) {
953 if (gDebug > 0)
954 Info("GetFromWeb10", "HTTP/1.1 socket closed, reopen");
955 if (fBasicUrlOrg != "") {
956 // if we have to close temp redirection, set back to original url
958 }
959 ws.ReOpen();
960 return GetFromWeb10(buf, len, msg);
961 }
962
963 if (ltot != len) {
964 Error("GetFromWeb10", "error receiving expected amount of data (got %d, expected %d) from host %s",
965 ltot, len, fUrl.GetHost());
966 return -1;
967 }
968
969 // collect statistics
970 fBytesRead += len;
971 fReadCalls++;
972#ifdef R__WIN32
975#else
976 fgBytesRead += len;
977 fgReadCalls++;
978#endif
979
980 if (gPerfStats)
981 gPerfStats->FileReadEvent(this, len, start);
982
983 return 0;
984}
985
986////////////////////////////////////////////////////////////////////////////////
987/// Set position from where to start reading.
988
990{
991 switch (pos) {
992 case kBeg:
994 break;
995 case kCur:
996 fOffset += offset;
997 break;
998 case kEnd:
999 // this option is not used currently in the ROOT code
1000 if (fArchiveOffset)
1001 Error("Seek", "seeking from end in archive is not (yet) supported");
1002 fOffset = fEND - offset; // is fEND really EOF or logical EOF?
1003 break;
1004 }
1005}
1006
1007////////////////////////////////////////////////////////////////////////////////
1008/// Return maximum file size.
1009
1011{
1012 if (!fHasModRoot || fSize >= 0)
1013 return fSize;
1014
1015 Long64_t size;
1016 char asize[64];
1017
1018 TString msg = "GET ";
1019 msg += fBasicUrl;
1020 msg += "?";
1021 msg += -1;
1022 msg += "\r\n";
1023
1024 if (const_cast<TWebFile*>(this)->GetFromWeb(asize, 64, msg) == -1)
1025 return kMaxInt;
1026
1027#ifndef R__WIN32
1028 size = atoll(asize);
1029#else
1030 size = _atoi64(asize);
1031#endif
1032
1033 fSize = size;
1034
1035 return size;
1036}
1037
1038////////////////////////////////////////////////////////////////////////////////
1039/// Get the HTTP header. Depending on the return code we can see if
1040/// the file exists and if the server uses mod_root.
1041/// Returns -1 in case of an error, -2 in case the file does not exists,
1042/// -3 in case HEAD is not supported (dCache HTTP door) and
1043/// 0 in case of success.
1044
1046{
1047 // Give full URL so Apache's virtual hosts solution works.
1048 if (fMsgGetHead == "") {
1049 fMsgGetHead = "HEAD ";
1051 if (fHTTP11)
1052 fMsgGetHead += " HTTP/1.1";
1053 else
1054 fMsgGetHead += " HTTP/1.0";
1055 fMsgGetHead += "\r\n";
1056 if (fHTTP11) {
1057 fMsgGetHead += "Host: ";
1059 fMsgGetHead += "\r\n";
1060 }
1063 fMsgGetHead += "\r\n\r\n";
1064 }
1065 TString msg = fMsgGetHead;
1066
1067 TUrl connurl;
1068 if (fProxy.IsValid())
1069 connurl = fProxy;
1070 else
1071 connurl = fUrl;
1072
1073 TSocket *s = nullptr;
1074 for (Int_t i = 0; i < 5; i++) {
1075 if (strcmp(connurl.GetProtocol(), "https") == 0) {
1076#ifdef R__SSL
1077 s = new TSSLSocket(connurl.GetHost(), connurl.GetPort());
1078#else
1079 Error("GetHead", "library compiled without SSL, https not supported");
1080 return -1;
1081#endif
1082 } else
1083 s = new TSocket(connurl.GetHost(), connurl.GetPort());
1084
1085 if (!s->IsValid()) {
1086 delete s;
1087 if (gSystem->GetErrno() == EADDRINUSE || gSystem->GetErrno() == EISCONN) {
1088 s = nullptr;
1089 gSystem->Sleep(i*10);
1090 } else {
1091 Error("GetHead", "cannot connect to host %s (errno=%d)", fUrl.GetHost(),
1092 gSystem->GetErrno());
1093 return -1;
1094 }
1095 } else
1096 break;
1097 }
1098 if (!s)
1099 return -1;
1100
1101 if (gDebug > 0) {
1102 Info("GetHead", "connected to host %s", connurl.GetHost());
1103 Info("GetHead", "sending HTTP request:\n%s", msg.Data());
1104 }
1105
1106 if (s->SendRaw(msg.Data(), msg.Length()) == -1) {
1107 Error("GetHead", "error sending command to host %s", fUrl.GetHost());
1108 delete s;
1109 return -1;
1110 }
1111
1112 char line[8192];
1113 Int_t n, ret = 0, redirect = 0;
1114 TString redir;
1115
1116 while ((n = GetLine(s, line, sizeof(line))) >= 0) {
1117 if (n == 0) {
1118 if (gDebug > 0)
1119 Info("GetHead", "got all headers");
1120 delete s;
1121 if (fBasicUrlOrg != "" && !redirect) {
1122 // set back to original url in case of temp redirect
1124 fMsgGetHead = "";
1125 }
1126 if (ret < 0)
1127 return ret;
1128 if (redirect) {
1129 if (redir.IsNull()) {
1130 // Some sites (s3.amazonaws.com) do not return a Location field on 301
1131 Error("GetHead", "error - redirect without location from host %s", fUrl.GetHost());
1132 return -1;
1133 }
1134 return GetHead();
1135 }
1136 return 0;
1137 }
1138
1139 if (gDebug > 0)
1140 Info("GetHead", "header: %s", line);
1141
1142 TString res = line;
1143 ProcessHttpHeader(res);
1144 if (res.BeginsWith("HTTP/1.")) {
1145 if (res.BeginsWith("HTTP/1.1")) {
1146 if (!fHTTP11) {
1147 fMsgGetHead = "";
1148 fMsgReadBuffer10 = "";
1149 }
1150 fHTTP11 = kTRUE;
1151 }
1152 TString scode = res(9, 3);
1153 Int_t code = scode.Atoi();
1154 if (code >= 500) {
1155 if (code == 500)
1157 else {
1158 ret = -1;
1159 TString mess = res(13, 1000);
1160 Error("GetHead", "%s: %s (%d)", fBasicUrl.Data(), mess.Data(), code);
1161 }
1162 } else if (code >= 400) {
1163 if (code == 400)
1164 ret = -3; // command not supported
1165 else if (code == 404)
1166 ret = -2; // file does not exist
1167 else {
1168 ret = -1;
1169 TString mess = res(13, 1000);
1170 Error("GetHead", "%s: %s (%d)", fBasicUrl.Data(), mess.Data(), code);
1171 }
1172 } else if (code >= 300) {
1173 if (code == 301 || code == 303)
1174 redirect = 1; // permanent redirect
1175 else if (code == 302 || code == 307)
1176 redirect = 2; // temp redirect
1177 else {
1178 ret = -1;
1179 TString mess = res(13, 1000);
1180 Error("GetHead", "%s: %s (%d)", fBasicUrl.Data(), mess.Data(), code);
1181 }
1182 } else if (code > 200) {
1183 ret = -1;
1184 TString mess = res(13, 1000);
1185 Error("GetHead", "%s: %s (%d)", fBasicUrl.Data(), mess.Data(), code);
1186 }
1187 } else if (res.BeginsWith("Content-Length:")) {
1188 TString slen = res(16, 1000);
1189 fSize = slen.Atoll();
1190 } else if (res.BeginsWith("Location:") && redirect) {
1191 redir = res(10, 1000);
1192 if (redirect == 2) // temp redirect
1193 SetMsgReadBuffer10(redir, kTRUE);
1194 else // permanent redirect
1195 SetMsgReadBuffer10(redir, kFALSE);
1196 fMsgGetHead = "";
1197 }
1198 }
1199
1200 delete s;
1201
1202 return ret;
1203}
1204
1205////////////////////////////////////////////////////////////////////////////////
1206/// Read a line from the socket. Reads at most one less than the number of
1207/// characters specified by maxsize. Reading stops when a newline character
1208/// is found, The newline (\\n) and cr (\\r), if any, are removed.
1209/// Returns -1 in case of error, or the number of characters read (>= 0)
1210/// otherwise.
1211
1213{
1214 Int_t n = GetHunk(s, line, maxsize);
1215 if (n < 0) {
1216 if (!fHTTP11 || gDebug > 0)
1217 Error("GetLine", "error receiving data from host %s", fUrl.GetHost());
1218 return -1;
1219 }
1220
1221 if (n > 0 && line[n-1] == '\n') {
1222 n--;
1223 if (n > 0 && line[n-1] == '\r')
1224 n--;
1225 line[n] = '\0';
1226 }
1227 return n;
1228}
1229
1230////////////////////////////////////////////////////////////////////////////////
1231/// Read a hunk of data from the socket, up until a terminator. The hunk is
1232/// limited by whatever the TERMINATOR callback chooses as its
1233/// terminator. For example, if terminator stops at newline, the hunk
1234/// will consist of a line of data; if terminator stops at two
1235/// newlines, it can be used to read the head of an HTTP response.
1236/// Upon determining the boundary, the function returns the data (up to
1237/// the terminator) in hunk.
1238///
1239/// In case of read error, -1 is returned. In case of having read some
1240/// data, but encountering EOF before seeing the terminator, the data
1241/// that has been read is returned, but it will (obviously) not contain the
1242/// terminator.
1243///
1244/// The TERMINATOR function is called with three arguments: the
1245/// beginning of the data read so far, the beginning of the current
1246/// block of peeked-at data, and the length of the current block.
1247/// Depending on its needs, the function is free to choose whether to
1248/// analyze all data or just the newly arrived data. If TERMINATOR
1249/// returns 0, it means that the terminator has not been seen.
1250/// Otherwise it should return a pointer to the character immediately
1251/// following the terminator.
1252///
1253/// The idea is to be able to read a line of input, or otherwise a hunk
1254/// of text, such as the head of an HTTP request, without crossing the
1255/// boundary, so that the next call to RecvRaw() etc. reads the data
1256/// after the hunk. To achieve that, this function does the following:
1257///
1258/// 1. Peek at incoming data.
1259///
1260/// 2. Determine whether the peeked data, along with the previously
1261/// read data, includes the terminator.
1262///
1263/// 3a. If yes, read the data until the end of the terminator, and
1264/// exit.
1265///
1266/// 3b. If no, read the peeked data and goto 1.
1267///
1268/// The function is careful to assume as little as possible about the
1269/// implementation of peeking. For example, every peek is followed by
1270/// a read. If the read returns a different amount of data, the
1271/// process is retried until all data arrives safely.
1272///
1273/// Reads at most one less than the number of characters specified by maxsize.
1274
1275Int_t TWebFile::GetHunk(TSocket *s, char *hunk, Int_t maxsize)
1276{
1277 if (maxsize <= 0) return 0;
1278
1279 Int_t bufsize = maxsize;
1280 Int_t tail = 0; // tail position in HUNK
1281
1282 while (1) {
1283 const char *end;
1284 Int_t pklen, rdlen, remain;
1285
1286 // First, peek at the available data.
1287 pklen = s->RecvRaw(hunk+tail, bufsize-1-tail, kPeek);
1288 if (pklen < 0) {
1289 return -1;
1290 }
1291 end = HttpTerminator(hunk, hunk+tail, pklen);
1292 if (end) {
1293 // The data contains the terminator: we'll drain the data up
1294 // to the end of the terminator.
1295 remain = end - (hunk + tail);
1296 if (remain == 0) {
1297 // No more data needs to be read.
1298 hunk[tail] = '\0';
1299 return tail;
1300 }
1301 if (bufsize - 1 < tail + remain) {
1302 Error("GetHunk", "hunk buffer too small for data from host %s (%d bytes needed)",
1303 fUrl.GetHost(), tail + remain + 1);
1304 hunk[tail] = '\0';
1305 return -1;
1306 }
1307 } else {
1308 // No terminator: simply read the data we know is (or should
1309 // be) available.
1310 remain = pklen;
1311 }
1312
1313 // Now, read the data. Note that we make no assumptions about
1314 // how much data we'll get. (Some TCP stacks are notorious for
1315 // read returning less data than the previous MSG_PEEK.)
1316 rdlen = s->RecvRaw(hunk+tail, remain, kDontBlock);
1317 if (rdlen < 0) {
1318 return -1;
1319 }
1320 tail += rdlen;
1321 hunk[tail] = '\0';
1322
1323 if (rdlen == 0) {
1324 // in case of EOF: return the data we've read.
1325 return tail;
1326 }
1327 if (end && rdlen == remain) {
1328 // The terminator was seen and the remaining data drained --
1329 // we got what we came for.
1330 return tail;
1331 }
1332
1333 // Keep looping until all the data arrives.
1334
1335 if (tail == bufsize - 1) {
1336 Error("GetHunk", "hunk buffer too small for data from host %s",
1337 fUrl.GetHost());
1338 return -1;
1339 }
1340 }
1341}
1342
1343////////////////////////////////////////////////////////////////////////////////
1344/// Determine whether [START, PEEKED + PEEKLEN) contains an HTTP new
1345/// line [\\r]\\n. If so, return the pointer to the position after the line,
1346/// otherwise return 0. This is used as callback to GetHunk(). The data
1347/// between START and PEEKED has been read and cannot be "unread"; the
1348/// data after PEEKED has only been peeked.
1349
1350const char *TWebFile::HttpTerminator(const char *start, const char *peeked,
1351 Int_t peeklen)
1352{
1353#if 0
1354 const char *p, *end;
1355
1356 // Look for "[\r]\n", and return the following position if found.
1357 // Start one char before the current to cover the possibility that
1358 // part of the terminator (e.g. "\r") arrived in the previous batch.
1359 p = peeked - start < 1 ? start : peeked - 1;
1360 end = peeked + peeklen;
1361
1362 // Check for \r\n anywhere in [p, end-2).
1363 for (; p < end - 1; p++)
1364 if (p[0] == '\r' && p[1] == '\n')
1365 return p + 2;
1366
1367 // p==end-1: check for \r\n directly preceding END.
1368 if (p[0] == '\r' && p[1] == '\n')
1369 return p + 2;
1370#else
1371 (void) start; // start unused, silence compiler
1372 if (peeked) {
1373 const char *p = (const char*) memchr(peeked, '\n', peeklen);
1374 if (p)
1375 // p+1 because the line must include '\n'
1376 return p + 1;
1377 }
1378#endif
1379 return nullptr;
1380}
1381
1382////////////////////////////////////////////////////////////////////////////////
1383/// Return basic authentication scheme, to be added to the request.
1384
1386{
1387 TString msg;
1388 if (strlen(fUrl.GetUser())) {
1389 TString auth = fUrl.GetUser();
1390 if (strlen(fUrl.GetPasswd())) {
1391 auth += ":";
1392 auth += fUrl.GetPasswd();
1393 }
1394 msg += "Authorization: Basic ";
1395 msg += TBase64::Encode(auth);
1396 msg += "\r\n";
1397 }
1398 return msg;
1399}
1400
1401////////////////////////////////////////////////////////////////////////////////
1402/// Static method setting global proxy URL.
1403
1404void TWebFile::SetProxy(const char *proxy)
1405{
1406 if (proxy && *proxy) {
1407 TUrl p(proxy);
1408 if (strcmp(p.GetProtocol(), "http")) {
1409 :: Error("TWebFile::SetProxy", "protocol must be HTTP in proxy URL %s",
1410 proxy);
1411 return;
1412 }
1413 fgProxy = p;
1414 }
1415}
1416
1417////////////////////////////////////////////////////////////////////////////////
1418/// Static method returning the global proxy URL.
1419
1421{
1422 if (fgProxy.IsValid())
1423 return fgProxy.GetUrl();
1424 return "";
1425}
1426
1427////////////////////////////////////////////////////////////////////////////////
1428/// Process the HTTP header in the argument. This method is intended to be
1429/// overwritten by subclasses that exploit the information contained in the
1430/// HTTP headers.
1431
1433{
1434}
1435
1436////////////////////////////////////////////////////////////////////////////////
1437/// Static method returning maxmimal size of full cache,
1438/// which can be preserved by file instance
1439
1441{
1442 return fgMaxFullCacheSize;
1443}
1444
1445////////////////////////////////////////////////////////////////////////////////
1446/// Static method, set maxmimal size of full cache,
1447// which can be preserved by file instance
1448
1450{
1451 fgMaxFullCacheSize = sz;
1452}
1453
1454
1455////////////////////////////////////////////////////////////////////////////////
1456/// Create helper class that allows directory access via httpd.
1457/// The name must start with '-' to bypass the TSystem singleton check.
1458
1459TWebSystem::TWebSystem() : TSystem("-http", "HTTP Helper System")
1460{
1461 SetName("http");
1462
1463 fDirp = nullptr;
1464}
1465
1466////////////////////////////////////////////////////////////////////////////////
1467/// Make a directory via httpd. Not supported.
1468
1470{
1471 return -1;
1472}
1473
1474////////////////////////////////////////////////////////////////////////////////
1475/// Open a directory via httpd. Returns an opaque pointer to a dir
1476/// structure. Returns 0 in case of error.
1477
1478void *TWebSystem::OpenDirectory(const char *)
1479{
1480 if (fDirp) {
1481 Error("OpenDirectory", "invalid directory pointer (should never happen)");
1482 fDirp = nullptr;
1483 }
1484
1485 fDirp = nullptr; // not implemented for the time being
1486
1487 return fDirp;
1488}
1489
1490////////////////////////////////////////////////////////////////////////////////
1491/// Free directory via httpd.
1492
1494{
1495 if (dirp != fDirp) {
1496 Error("FreeDirectory", "invalid directory pointer (should never happen)");
1497 return;
1498 }
1499
1500 fDirp = nullptr;
1501}
1502
1503////////////////////////////////////////////////////////////////////////////////
1504/// Get directory entry via httpd. Returns 0 in case no more entries.
1505
1506const char *TWebSystem::GetDirEntry(void *dirp)
1507{
1508 if (dirp != fDirp) {
1509 Error("GetDirEntry", "invalid directory pointer (should never happen)");
1510 return 0;
1511 }
1512
1513 return 0;
1514}
1515
1516////////////////////////////////////////////////////////////////////////////////
1517/// Get info about a file. Info is returned in the form of a FileStat_t
1518/// structure (see TSystem.h).
1519/// The function returns 0 in case of success and 1 if the file could
1520/// not be stat'ed.
1521
1523{
1524 TWebFile *f = new TWebFile(path, "HEADONLY");
1525
1526 if (f->fWritten == 0) {
1527
1528 buf.fDev = 0;
1529 buf.fIno = 0;
1530 buf.fMode = 0;
1531 buf.fUid = 0;
1532 buf.fGid = 0;
1533 buf.fSize = f->GetSize();
1534 buf.fMtime = 0;
1535 buf.fIsLink = kFALSE;
1536
1537 delete f;
1538 return 0;
1539 }
1540
1541 delete f;
1542 return 1;
1543}
1544
1545////////////////////////////////////////////////////////////////////////////////
1546/// Returns FALSE if one can access a file using the specified access mode.
1547/// Mode is the same as for the Unix access(2) function.
1548/// Attention, bizarre convention of return value!!
1549
1551{
1552 TWebFile *f = new TWebFile(path, "HEADONLY");
1553 if (f->fWritten == 0) {
1554 delete f;
1555 return kFALSE;
1556 }
1557 delete f;
1558 return kTRUE;
1559}
1560
1561////////////////////////////////////////////////////////////////////////////////
1562/// Unlink, i.e. remove, a file or directory. Returns 0 when successful,
1563/// -1 in case of failure. Not supported for httpd.
1564
1566{
1567 return -1;
1568}
#define f(i)
Definition RSha256.hxx:104
size_t size(const MatrixT &matrix)
retrieve the size of a square matrix
int Int_t
Definition RtypesCore.h:45
constexpr Int_t kMaxInt
Definition RtypesCore.h:105
constexpr Bool_t kFALSE
Definition RtypesCore.h:94
long long Long64_t
Definition RtypesCore.h:69
constexpr Bool_t kTRUE
Definition RtypesCore.h:93
const char Option_t
Definition RtypesCore.h:66
#define ClassImp(name)
Definition Rtypes.h:382
static const std::string gUserAgent
#define gDirectory
Definition TDirectory.h:384
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
Definition TError.cxx:218
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
Definition TError.cxx:185
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
Definition TError.cxx:229
winID h TVirtualViewer3D TVirtualGLPainter p
Option_t Option_t option
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h offset
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t r
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t WindowAttributes_t Float_t Float_t Float_t Int_t Int_t UInt_t UInt_t Rectangle_t Int_t Int_t Window_t TString Int_t GCValues_t GetPrimarySelectionOwner GetDisplay GetScreen GetColormap GetNativeEvent const char const char dpyName wid window const char font_name cursor keysym reg const char only_if_exist regb h Point_t winding char text const char depth char const char Int_t count const char ColorStruct_t color const char Pixmap_t Pixmap_t PictureAttributes_t attr const char char ret_data h unsigned char height h Atom_t Int_t ULong_t ULong_t unsigned char prop_list Atom_t Atom_t Atom_t Time_t UChar_t len
Option_t Option_t TPoint TPoint const char mode
Option_t Option_t TPoint TPoint const char GetTextMagnitude GetFillStyle GetLineColor GetLineWidth GetMarkerStyle GetTextAlign GetTextColor GetTextSize void char Point_t Rectangle_t src
Int_t gDebug
Definition TROOT.cxx:597
#define gROOT
Definition TROOT.h:406
@ kDontBlock
Definition TSystem.h:236
@ kPeek
Definition TSystem.h:235
EAccessMode
Definition TSystem.h:41
R__EXTERN TSystem * gSystem
Definition TSystem.h:561
#define gPerfStats
static const char * gUserAgent
Definition TWebFile.cxx:46
#define free
Definition civetweb.c:1539
#define malloc
Definition civetweb.c:1536
static TString Encode(const char *data)
Transform data into a null terminated base64 string.
Definition TBase64.cxx:107
A ROOT file is an on-disk file, usually with extension .root, that stores objects in a file-system-li...
Definition TFile.h:53
static std::atomic< Long64_t > fgBytesRead
Number of bytes read by all TFile objects.
Definition TFile.h:131
Int_t fReadCalls
Number of read calls ( not counting the cache calls )
Definition TFile.h:89
Long64_t fBytesRead
Number of bytes read from this file.
Definition TFile.h:76
static void SetFileBytesRead(Long64_t bytes=0)
Definition TFile.cxx:4605
static void SetFileReadCalls(Int_t readcalls=0)
Definition TFile.cxx:4611
TUrl fUrl
!URL of file
Definition TFile.h:110
static Long64_t GetFileBytesRead()
Static function returning the total number of bytes read from all files.
Definition TFile.cxx:4571
Int_t ReadBufferViaCache(char *buf, Int_t len)
Read buffer via cache.
Definition TFile.cxx:1886
Long64_t fArchiveOffset
!Offset at which file starts in archive
Definition TFile.h:101
virtual void Init(Bool_t create)
Initialize a TFile object.
Definition TFile.cxx:610
ERelativeTo
Definition TFile.h:199
@ kCur
Definition TFile.h:199
@ kBeg
Definition TFile.h:199
@ kEnd
Definition TFile.h:199
Int_t fD
File descriptor.
Definition TFile.h:82
Bool_t fIsRootFile
!True is this is a ROOT file, raw file otherwise
Definition TFile.h:104
virtual void SetOffset(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
Definition TFile.cxx:2250
Long64_t fOffset
!Seek offset cache
Definition TFile.h:96
Long64_t fEND
Last used byte in file.
Definition TFile.h:79
void Close(Option_t *option="") override
Close a file.
Definition TFile.cxx:947
static std::atomic< Int_t > fgReadCalls
Number of bytes read from all TFile objects.
Definition TFile.h:133
Int_t fWritten
Number of objects written so far.
Definition TFile.h:87
static Int_t GetFileReadCalls()
Static function returning the total number of read calls from all files.
Definition TFile.cxx:4588
virtual void SetName(const char *name)
Set the name of the TNamed.
Definition TNamed.cxx:140
R__ALWAYS_INLINE Bool_t IsZombie() const
Definition TObject.h:153
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Definition TObject.cxx:1005
void MakeZombie()
Definition TObject.h:53
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
Definition TSocket.cxx:898
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
Definition TSocket.cxx:620
virtual Bool_t IsValid() const
Definition TSocket.h:132
Basic string class.
Definition TString.h:139
Ssiz_t Length() const
Definition TString.h:417
Int_t Atoi() const
Return integer value of string.
Definition TString.cxx:1988
const char * Data() const
Definition TString.h:376
TString & ReplaceAll(const TString &s1, const TString &s2)
Definition TString.h:704
@ kIgnoreCase
Definition TString.h:277
void ToUpper()
Change string to upper case.
Definition TString.cxx:1195
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Definition TString.h:623
Bool_t IsNull() const
Definition TString.h:414
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Definition TString.h:651
Long64_t Atoll() const
Return long long value of string.
Definition TString.cxx:2014
Abstract base class defining a generic interface to the underlying Operating System.
Definition TSystem.h:266
static Int_t GetErrno()
Static function returning system error number.
Definition TSystem.cxx:276
virtual const char * Getenv(const char *env)
Get environment variable.
Definition TSystem.cxx:1665
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
Definition TSystem.cxx:437
The TTimeStamp encapsulates seconds and ns since EPOCH.
Definition TTimeStamp.h:45
This class represents a WWW compatible URL.
Definition TUrl.h:33
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
Definition TUrl.cxx:390
const char * GetFile() const
Definition TUrl.h:69
void SetUrl(const char *url, Bool_t defaultIsFile=kFALSE)
Parse url character string and split in its different subcomponents.
Definition TUrl.cxx:110
Bool_t IsValid() const
Definition TUrl.h:79
const char * GetUser() const
Definition TUrl.h:65
const char * GetHost() const
Definition TUrl.h:67
const char * GetPasswd() const
Definition TUrl.h:66
const char * GetOptions() const
Definition TUrl.h:71
const char * GetProtocol() const
Definition TUrl.h:64
Int_t GetPort() const
Definition TUrl.h:78
virtual Int_t GetLine(TSocket *s, char *line, Int_t maxsize)
Read a line from the socket.
virtual ~TWebFile()
Cleanup.
Definition TWebFile.cxx:199
virtual Int_t GetHead()
Get the HTTP header.
virtual Int_t GetFromWeb(char *buf, Int_t len, const TString &msg)
Read request from web server.
Definition TWebFile.cxx:625
virtual TString BasicAuthentication()
Return basic authentication scheme, to be added to the request.
Long64_t fSize
Definition TWebFile.h:42
TSocket * fSocket
Definition TWebFile.h:43
static const char * GetProxy()
Static method returning the global proxy URL.
TString fBasicUrl
Definition TWebFile.h:51
static void SetProxy(const char *url)
Static method setting global proxy URL.
virtual const char * HttpTerminator(const char *start, const char *peeked, Int_t peeklen)
Determine whether [START, PEEKED + PEEKLEN) contains an HTTP new line [\r]\n.
Long64_t GetSize() const override
Return maximum file size.
virtual Int_t GetFromCache(char *buf, Int_t len, Int_t nseg, Long64_t *seg_pos, Int_t *seg_len)
Extract requested segments from the cached content.
Definition TWebFile.cxx:600
TString fBasicUrlOrg
Definition TWebFile.h:53
Bool_t IsOpen() const override
A TWebFile that has been correctly constructed is always considered open.
Definition TWebFile.cxx:380
Bool_t fHTTP11
Definition TWebFile.h:46
virtual void CheckProxy()
Check if shell var "http_proxy" has been set and should be used.
Definition TWebFile.cxx:353
virtual Bool_t ReadBuffers10(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
Read specified byte ranges from remote file via HTTP 1.0 daemon (without mod-root installed).
Definition TWebFile.cxx:566
Bool_t ReadBuffer(char *buf, Int_t len) override
Read specified byte range from remote file via HTTP daemon.
Definition TWebFile.cxx:429
Long64_t fFullCacheSize
complete content of the file, some http server may return complete content
Definition TWebFile.h:55
Bool_t ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf) override
Read specified byte ranges from remote file via HTTP daemon.
Definition TWebFile.cxx:517
TUrl fProxy
Definition TWebFile.h:44
TString fMsgGetHead
Definition TWebFile.h:50
void Seek(Long64_t offset, ERelativeTo pos=kBeg) override
Set position from where to start reading.
Definition TWebFile.cxx:989
TString fMsgReadBuffer
Definition TWebFile.h:48
virtual Bool_t ReadBuffer10(char *buf, Int_t len)
Read specified byte range from remote file via HTTP 1.0 daemon (without mod-root installed).
Definition TWebFile.cxx:478
void Init(Bool_t readHeadOnly) override
Initialize a TWebFile object.
Definition TWebFile.cxx:212
virtual Int_t GetFromWeb10(char *buf, Int_t len, const TString &msg, Int_t nseg=0, Long64_t *seg_pos=nullptr, Int_t *seg_len=nullptr)
Read multiple byte range request from web server.
Definition TWebFile.cxx:692
TWebFile()
Definition TWebFile.h:39
static TUrl fgProxy
size of the cached content
Definition TWebFile.h:57
void * fFullCache
Definition TWebFile.h:54
static void SetMaxFullCacheSize(Long64_t sz)
Static method, set maxmimal size of full cache,.
virtual void SetMsgReadBuffer10(const char *redirectLocation=nullptr, Bool_t tempRedirect=kFALSE)
Set GET command for use by ReadBuffer(s)10(), handle redirection if needed.
Definition TWebFile.cxx:268
TString fMsgReadBuffer10
Definition TWebFile.h:49
TUrl fUrlOrg
Definition TWebFile.h:52
Int_t ReOpen(Option_t *mode) override
Reopen a file with a different access mode, like from READ to UPDATE or from NEW, CREATE,...
Definition TWebFile.cxx:394
static Long64_t GetMaxFullCacheSize()
Static method returning maxmimal size of full cache, which can be preserved by file instance.
Bool_t fHasModRoot
Definition TWebFile.h:45
virtual void ProcessHttpHeader(const TString &headerLine)
Process the HTTP header in the argument.
void Close(Option_t *option="") override
Close a Web file.
Definition TWebFile.cxx:412
virtual Int_t GetHunk(TSocket *s, char *hunk, Int_t maxsize)
Read a hunk of data from the socket, up until a terminator.
static Long64_t fgMaxFullCacheSize
Definition TWebFile.h:58
Bool_t fNoProxy
Definition TWebFile.h:47
TWebFile * fWebFile
Definition TWebFile.cxx:57
~TWebSocket()
Close socket in case not HTTP/1.1 protocol or when explicitly requested.
Definition TWebFile.cxx:77
void ReOpen()
Re-open web file socket.
Definition TWebFile.cxx:88
TWebSocket(TWebFile *f)
Open web file socket.
Definition TWebFile.cxx:67
void * OpenDirectory(const char *name) override
Open a directory via httpd.
Int_t MakeDirectory(const char *name) override
Make a directory via httpd. Not supported.
TWebSystem()
Create helper class that allows directory access via httpd.
void FreeDirectory(void *dirp) override
Free directory via httpd.
Bool_t AccessPathName(const char *path, EAccessMode mode) override
Returns FALSE if one can access a file using the specified access mode.
void * fDirp
Definition TWebFile.h:102
Int_t GetPathInfo(const char *path, FileStat_t &buf) override
Get info about a file.
const char * GetDirEntry(void *dirp) override
Get directory entry via httpd. Returns 0 in case no more entries.
Int_t Unlink(const char *path) override
Unlink, i.e.
TLine * line
const Int_t n
Definition legend1.C:16
Int_t fMode
Definition TSystem.h:125
Long64_t fSize
Definition TSystem.h:128
Long_t fDev
Definition TSystem.h:123
Int_t fGid
Definition TSystem.h:127
Long_t fMtime
Definition TSystem.h:129
Long_t fIno
Definition TSystem.h:124
Bool_t fIsLink
Definition TSystem.h:130
Int_t fUid
Definition TSystem.h:126