39 # define EADDRINUSE 10048 42 # define EISCONN 10056 46 static const char *
gUserAgent =
"User-Agent: ROOT-TWebFile/1.1";
77 TWebSocket::~TWebSocket()
79 if (!fWebFile->fHTTP11) {
80 delete fWebFile->fSocket;
81 fWebFile->fSocket = 0;
88 void TWebSocket::ReOpen()
90 if (fWebFile->fSocket) {
91 delete fWebFile->fSocket;
92 fWebFile->fSocket = 0;
96 if (fWebFile->fProxy.IsValid())
97 connurl = fWebFile->fProxy;
99 connurl = fWebFile->
fUrl;
106 ::Error(
"TWebSocket::ReOpen",
"library compiled without SSL, https not supported");
112 if (!fWebFile->fSocket || !fWebFile->fSocket->IsValid()) {
114 fWebFile->fSocket = 0;
118 ::Error(
"TWebSocket::ReOpen",
"cannot connect to host %s (errno=%d)",
250 if (strncmp(buf,
"root", 4) && strncmp(buf,
"PK", 2)) {
271 if (redirectLocation) {
293 if (rdl.
Index(
"?") >= 0) {
301 if (oldBasicUrl !=
"") {
365 Error(
"CheckProxy",
"protocol must be HTTP in proxy URL %s",
397 if (opt !=
"READ" && opt !=
"UPDATE")
398 Error(
"ReOpen",
"mode must be either READ or UPDATE, not %s", opt.
Data());
401 Error(
"ReOpen",
"update mode not allowed for a TWebFile");
514 for (
Int_t i = 0; i < nbuf; i++) {
521 if ((msg.
Length() > 8000) || (
cnt >= 200)) {
555 for (
Int_t i = 0; i < nbuf; i++) {
559 msg += pos[i] + fArchiveOffset + len[i] - 1;
562 if ((msg.
Length() > 8000) || (cnt >= 200) || (i+1 == nbuf)) {
564 r =
GetFromWeb10(&buf[k],
n, msg, cnt, pos + (i+1-cnt), len + (i+1-cnt));
587 Info(
"GetFromCache",
"Extract %d segments total len %d from cached data", nseg, len);
592 if (curr + seg_len[
cnt] > len)
return -1;
596 memcpy(buf + curr, src, seg_len[
cnt]);
597 curr += seg_len[
cnt];
626 Error(
"GetFromWeb",
"library compiled without SSL, https not supported");
644 if (s->
RecvRaw(buf, len) == -1) {
694 Info(
"GetFromWeb10",
"sending HTTP request:\n%s", msg.
Data());
702 Int_t n, ret = 0, nranges = 0, ltot = 0, redirect = 0;
714 Error(
"GetFromWeb10",
"error - redirect without location from host %s",
fUrl.
GetHost());
735 else if (ll != rsize) {
736 Error(
"GetFromWeb10",
"expected %d bytes, got %d", ll, rsize);
750 Error(
"GetFromWeb10",
"Need segments data to extract parts from full size %lld", fullsize);
754 if (len > fullsize) {
755 Error(
"GetFromWeb10",
"Requested part %d longer than full size %lld", len, fullsize);
777 Error(
"GetFromWeb10",
"Requested segment %lld len %d is outside of full range %lld", seg_pos[
cnt], seg_len[cnt], fullsize);
780 if ((
cnt>0) && (seg_pos[
cnt-1] + seg_len[
cnt-1] > seg_pos[
cnt])) {
781 Error(
"GetFromWeb10",
"Requested segments are not in sorted order");
795 if (ll >
Int_t(
sizeof(dbuf))) ll =
sizeof(dbuf);
808 curr += seg_len[
cnt];
810 ltot += seg_len[
cnt];
814 while (pos < fullsize) {
816 if (ll >
Int_t(
sizeof(dbuf))) ll =
sizeof(dbuf);
824 if (
gDebug>0)
Info(
"GetFromWeb10",
"Complete reading %d bytes in %d segments out of full size %lld", len, nseg, fullsize);
833 Info(
"GetFromWeb10",
"header: %s", line);
835 if (boundaryEnd == line) {
837 Info(
"GetFromWeb10",
"got all headers");
840 if (boundary == line) {
843 Info(
"GetFromWeb10",
"get new multipart byte range (%d)", nranges);
860 }
else if (code >= 400) {
868 }
else if (code >= 300) {
869 if (code == 301 || code == 303) {
871 }
else if (code == 302 || code == 307) {
880 }
else if (code > 200) {
886 }
else if (code == 200) {
889 "Server %s response with complete file, but only part of it was requested.\n" 890 "Check MaxRanges configuration parameter (if Apache is used)",
894 }
else if (res.
BeginsWith(
"Content-Type: multipart")) {
895 boundary = res(res.
Index(
"boundary=")+9, 1000);
896 if (boundary[0]==
'"' && boundary[boundary.
Length()-1]==
'"') {
897 boundary = boundary(1,boundary.
Length()-2);
899 boundary =
"--" + boundary;
900 boundaryEnd = boundary +
"--";
901 }
else if (res.
BeginsWith(
"Content-range:")) {
903 sscanf(res.
Data(),
"Content-range: bytes %I64d-%I64d/%I64d", &first, &last, &tot);
905 sscanf(res.
Data(),
"Content-range: bytes %lld-%lld/%lld", &first, &last, &tot);
908 }
else if (res.
BeginsWith(
"Content-Range:")) {
910 sscanf(res.
Data(),
"Content-Range: bytes %I64d-%I64d/%I64d", &first, &last, &tot);
912 sscanf(res.
Data(),
"Content-Range: bytes %lld-%lld/%lld", &first, &last, &tot);
915 }
else if (res.
BeginsWith(
"Content-Length:") && (fullsize == -200)) {
917 sscanf(res.
Data(),
"Content-Length: %I64d", &fullsize);
919 sscanf(res.
Data(),
"Content-Length: %lld", &fullsize);
921 }
else if (res.
BeginsWith(
"Location:") && redirect) {
922 redir = res(10, 1000);
930 if (redirect && redir.
IsNull()) {
932 Error(
"GetFromWeb10",
"error - redirect without location from host %s",
fUrl.
GetHost());
937 Info(
"GetFromWeb10",
"HTTP/1.1 socket closed, reopen");
947 Error(
"GetFromWeb10",
"error receiving expected amount of data (got %d, expected %d) from host %s",
984 Error(
"Seek",
"seeking from end in archive is not (yet) supported");
1007 if (const_cast<TWebFile*>(
this)->
GetFromWeb(asize, 64, msg) == -1)
1011 size = atoll(asize);
1013 size = _atoi64(asize);
1057 for (
Int_t i = 0; i < 5; i++) {
1058 if (strcmp(connurl.
GetProtocol(),
"https") == 0) {
1062 Error(
"GetHead",
"library compiled without SSL, https not supported");
1085 Info(
"GetHead",
"connected to host %s", connurl.
GetHost());
1086 Info(
"GetHead",
"sending HTTP request:\n%s", msg.
Data());
1096 Int_t n, ret = 0, redirect = 0;
1099 while ((n =
GetLine(s, line,
sizeof(line))) >= 0) {
1102 Info(
"GetHead",
"got all headers");
1114 Error(
"GetHead",
"error - redirect without location from host %s",
fUrl.
GetHost());
1123 Info(
"GetHead",
"header: %s", line);
1145 }
else if (code >= 400) {
1148 else if (code == 404)
1155 }
else if (code >= 300) {
1156 if (code == 301 || code == 303)
1158 else if (code == 302 || code == 307)
1165 }
else if (code > 200) {
1170 }
else if (res.
BeginsWith(
"Content-Length:")) {
1173 }
else if (res.
BeginsWith(
"Location:") && redirect) {
1174 redir = res(10, 1000);
1204 if (n > 0 && line[n-1] ==
'\n') {
1206 if (n > 0 && line[n-1] ==
'\r')
1260 if (maxsize <= 0)
return 0;
1267 Int_t pklen, rdlen, remain;
1278 remain = end - (hunk + tail);
1284 if (bufsize - 1 < tail + remain) {
1285 Error(
"GetHunk",
"hunk buffer too small for data from host %s (%d bytes needed)",
1315 if (end && rdlen == remain) {
1323 if (tail == bufsize - 1) {
1324 Error(
"GetHunk",
"hunk buffer too small for data from host %s",
1342 const char *p, *end;
1347 p = peeked - start < 1 ? start : peeked - 1;
1348 end = peeked + peeklen;
1351 for (; p < end - 1; p++)
1352 if (p[0] ==
'\r' && p[1] ==
'\n')
1356 if (p[0] ==
'\r' && p[1] ==
'\n')
1360 const char *p = (
const char*) memchr(peeked,
'\n', peeklen);
1380 msg +=
"Authorization: Basic ";
1392 if (proxy && *proxy) {
1395 :: Error(
"TWebFile::SetProxy",
"protocol must be HTTP in proxy URL %s",
1467 Error(
"OpenDirectory",
"invalid directory pointer (should never happen)");
1481 if (dirp !=
fDirp) {
1482 Error(
"FreeDirectory",
"invalid directory pointer (should never happen)");
1494 if (dirp !=
fDirp) {
1495 Error(
"GetDirEntry",
"invalid directory pointer (should never happen)");
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).
virtual Bool_t IsValid() const
static Long64_t GetMaxFullCacheSize()
Static method returning maxmimal size of full cache, which can be preserved by file instance...
virtual void Info(const char *method, const char *msgfmt,...) const
Issue info message.
static std::atomic< Long64_t > fgBytesRead
Number of bytes read by all TFile objects.
virtual TString BasicAuthentication()
Return basic authentication scheme, to be added to the request.
virtual void SetOffset(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
This class represents a WWW compatible URL.
TString & ReplaceAll(const TString &s1, const TString &s2)
const char * GetProtocol() const
virtual void SetName(const char *name)
Set the name of the TNamed.
Long64_t fFullCacheSize
complete content of the file, some http server may return complete content
void SetUrl(const char *url, Bool_t defaultIsFile=kFALSE)
Parse url character string and split in its different subcomponents.
Int_t MakeDirectory(const char *name)
Make a directory via httpd. Not supported.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format...
virtual Int_t GetHunk(TSocket *s, char *hunk, Int_t maxsize)
Read a hunk of data from the socket, up until a terminator.
static void SetFileReadCalls(Int_t readcalls=0)
void ToUpper()
Change string to upper case.
virtual void SetMsgReadBuffer10(const char *redirectLocation=0, Bool_t tempRedirect=kFALSE)
Set GET command for use by ReadBuffer(s)10(), handle redirection if needed.
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
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.
static TUrl fgProxy
size of the cached content
virtual Int_t GetFromWeb10(char *buf, Int_t len, const TString &msg, Int_t nseg=0, Long64_t *seg_pos=0, Int_t *seg_len=0)
Read multiple byte range request from web server.
const char * GetOptions() const
Int_t fReadCalls
Number of read calls ( not counting the cache calls )
static void SetProxy(const char *url)
Static method setting global proxy URL.
virtual void ProcessHttpHeader(const TString &headerLine)
Process the HTTP header in the argument.
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
static void SetMaxFullCacheSize(Long64_t sz)
Static method, set maxmimal size of full cache,.
virtual Int_t GetLine(TSocket *s, char *line, Int_t maxsize)
Read a line from the socket.
static Int_t GetErrno()
Static function returning system error number.
const char * GetFile() const
static const char * GetProxy()
Static method returning the global proxy URL.
static TString Encode(const char *data)
Transform data into a null terminated base64 string.
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
const char * GetHost() const
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.
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
virtual Bool_t IsOpen() const
A TWebFile that has been correctly constructed is always considered open.
static Long64_t GetFileBytesRead()
Static function returning the total number of bytes read from all files.
virtual const char * Getenv(const char *env)
Get environment variable.
const char * GetDirEntry(void *dirp)
Get directory entry via httpd. Returns 0 in case no more entries.
virtual Bool_t ReadBuffers(char *buf, Long64_t *pos, Int_t *len, Int_t nbuf)
Read specified byte ranges from remote file via HTTP daemon.
Long64_t Atoll() const
Return long long value of string.
static const char * gUserAgent
void Error(const char *location, const char *msgfmt,...)
const char * GetUser() const
const char * GetPasswd() const
virtual Bool_t ReadBuffer(char *buf, Int_t len)
Read specified byte range from remote file via HTTP daemon.
Long64_t fEND
Last used byte in file.
R__EXTERN TSystem * gSystem
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
Int_t Unlink(const char *path)
Unlink, i.e.
static std::atomic< Int_t > fgReadCalls
Number of bytes read from all TFile objects.
static Int_t GetFileReadCalls()
Static function returning the total number of read calls from all files.
virtual void Init(Bool_t create)
Initialize a TFile object.
Int_t ReadBufferViaCache(char *buf, Int_t len)
Read buffer via cache.
virtual void Seek(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
void FreeDirectory(void *dirp)
Free directory via httpd.
virtual void CheckProxy()
Check if shell var "http_proxy" has been set and should be used.
static Long64_t fgMaxFullCacheSize
virtual ~TWebFile()
Cleanup.
The TTimeStamp encapsulates seconds and ns since EPOCH.
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Bool_t fIsRootFile
!True is this is a ROOT file, raw file otherwise
void * OpenDirectory(const char *name)
Open a directory via httpd.
Int_t GetPathInfo(const char *path, FileStat_t &buf)
Get info about a file.
virtual Int_t GetHead()
Get the HTTP header.
virtual void Init(Bool_t readHeadOnly)
Initialize a TWebFile object.
Int_t Atoi() const
Return integer value of string.
Long64_t fArchiveOffset
!Offset at which file starts in archive
TWebSystem()
Create helper class that allows directory access via httpd.
Int_t fWritten
Number of objects written so far.
Long64_t fOffset
!Seek offset cache
static void SetFileBytesRead(Long64_t bytes=0)
Abstract base class defining a generic interface to the underlying Operating System.
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
virtual Long64_t GetSize() const
Return maximum file size.
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)...
Long64_t fBytesRead
Number of bytes read from this file.
virtual Int_t GetFromWeb(char *buf, Int_t len, const TString &msg)
Read request from web server.
Bool_t AccessPathName(const char *path, EAccessMode mode)
Returns FALSE if one can access a file using the specified access mode.
virtual void Warning(const char *method, const char *msgfmt,...) const
Issue warning message.
virtual Int_t ReOpen(Option_t *mode)
Reopen a file with a different access mode, like from READ to UPDATE or from NEW, CREATE...
const char * Data() const