39# define EADDRINUSE 10048
46static const char *
gUserAgent =
"User-Agent: ROOT-TWebFile/1.1";
77TWebSocket::~TWebSocket()
79 if (!fWebFile->fHTTP11) {
80 delete fWebFile->fSocket;
81 fWebFile->fSocket = 0;
88void 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;
101 for (
Int_t i = 0; i < 5; i++) {
106 ::Error(
"TWebSocket::ReOpen",
"library compiled without SSL, https not supported");
112 if (!fWebFile->fSocket || !fWebFile->fSocket->IsValid()) {
113 delete fWebFile->fSocket;
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");
513 Int_t k = 0,
n = 0, cnt = 0;
514 for (
Int_t i = 0; i < nbuf; i++) {
521 if ((msg.
Length() > 8000) || (cnt >= 200)) {
554 Int_t k = 0,
n = 0,
r, cnt = 0;
555 for (
Int_t i = 0; i < nbuf; i++) {
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);
590 for (
Int_t cnt=0;cnt<nseg;cnt++) {
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);
775 for (
Int_t cnt=0;cnt<nseg;cnt++) {
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");
791 for (
Int_t cnt=0; cnt<nseg; cnt++) {
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");
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;
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;
1262 Int_t bufsize = maxsize;
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)",
1310 if (end && rdlen == remain) {
1318 if (tail == bufsize - 1) {
1319 Error(
"GetHunk",
"hunk buffer too small for data from host %s",
1337 const char *p, *end;
1342 p = peeked - start < 1 ? start : peeked - 1;
1343 end = peeked + peeklen;
1346 for (; p < end - 1; p++)
1347 if (p[0] ==
'\r' && p[1] ==
'\n')
1351 if (p[0] ==
'\r' && p[1] ==
'\n')
1355 const char *p = (
const char*) memchr(peeked,
'\n', peeklen);
1375 msg +=
"Authorization: Basic ";
1387 if (proxy && *proxy) {
1390 :: Error(
"TWebFile::SetProxy",
"protocol must be HTTP in proxy URL %s",
1462 Error(
"OpenDirectory",
"invalid directory pointer (should never happen)");
1476 if (dirp !=
fDirp) {
1477 Error(
"FreeDirectory",
"invalid directory pointer (should never happen)");
1489 if (dirp !=
fDirp) {
1490 Error(
"GetDirEntry",
"invalid directory pointer (should never happen)");
1507 if (
f->fWritten == 0) {
1514 buf.
fSize =
f->GetSize();
1534 if (
f->fWritten == 0) {
static const std::string gUserAgent
void Info(const char *location, const char *msgfmt,...)
Use this function for informational messages.
void Error(const char *location, const char *msgfmt,...)
Use this function in case an error occurred.
void Warning(const char *location, const char *msgfmt,...)
Use this function in warning situations.
R__EXTERN TSystem * gSystem
static const char * gUserAgent
static TString Encode(const char *data)
Transform data into a null terminated base64 string.
A ROOT file is a suite of consecutive data records (TKey instances) with a well defined format.
static std::atomic< Long64_t > fgBytesRead
Number of bytes read by all TFile objects.
Int_t fReadCalls
Number of read calls ( not counting the cache calls )
Long64_t fBytesRead
Number of bytes read from this file.
static void SetFileBytesRead(Long64_t bytes=0)
static void SetFileReadCalls(Int_t readcalls=0)
static Long64_t GetFileBytesRead()
Static function returning the total number of bytes read from all files.
Int_t ReadBufferViaCache(char *buf, Int_t len)
Read buffer via cache.
Long64_t fArchiveOffset
!Offset at which file starts in archive
virtual void Init(Bool_t create)
Initialize a TFile object.
Bool_t fIsRootFile
!True is this is a ROOT file, raw file otherwise
virtual void SetOffset(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
Long64_t fOffset
!Seek offset cache
Long64_t fEND
Last used byte in file.
static std::atomic< Int_t > fgReadCalls
Number of bytes read from all TFile objects.
Int_t fWritten
Number of objects written so far.
static Int_t GetFileReadCalls()
Static function returning the total number of read calls from all files.
virtual void SetName(const char *name)
Set the name of the TNamed.
R__ALWAYS_INLINE Bool_t IsZombie() const
virtual void Error(const char *method, const char *msgfmt,...) const
Issue error message.
virtual Int_t RecvRaw(void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Receive a raw buffer of specified length bytes.
virtual Int_t SendRaw(const void *buffer, Int_t length, ESendRecvOptions opt=kDefault)
Send a raw buffer of specified length.
virtual Bool_t IsValid() const
Int_t Atoi() const
Return integer value of string.
const char * Data() const
TString & ReplaceAll(const TString &s1, const TString &s2)
void ToUpper()
Change string to upper case.
Bool_t BeginsWith(const char *s, ECaseCompare cmp=kExact) const
Bool_t Contains(const char *pat, ECaseCompare cmp=kExact) const
Ssiz_t Index(const char *pat, Ssiz_t i=0, ECaseCompare cmp=kExact) const
Long64_t Atoll() const
Return long long value of string.
Abstract base class defining a generic interface to the underlying Operating System.
static Int_t GetErrno()
Static function returning system error number.
virtual const char * Getenv(const char *env)
Get environment variable.
virtual void Sleep(UInt_t milliSec)
Sleep milliSec milli seconds.
The TTimeStamp encapsulates seconds and ns since EPOCH.
This class represents a WWW compatible URL.
const char * GetUrl(Bool_t withDeflt=kFALSE) const
Return full URL.
const char * GetFile() const
void SetUrl(const char *url, Bool_t defaultIsFile=kFALSE)
Parse url character string and split in its different subcomponents.
const char * GetUser() const
const char * GetHost() const
const char * GetPasswd() const
const char * GetOptions() const
const char * GetProtocol() const
virtual Int_t GetLine(TSocket *s, char *line, Int_t maxsize)
Read a line from the socket.
virtual ~TWebFile()
Cleanup.
virtual Int_t GetHead()
Get the HTTP header.
virtual Int_t ReOpen(Option_t *mode)
Reopen a file with a different access mode, like from READ to UPDATE or from NEW, CREATE,...
virtual Int_t GetFromWeb(char *buf, Int_t len, const TString &msg)
Read request from web server.
virtual TString BasicAuthentication()
Return basic authentication scheme, to be added to the request.
virtual void SetMsgReadBuffer10(const char *redirectLocation=0, Bool_t tempRedirect=kFALSE)
Set GET command for use by ReadBuffer(s)10(), handle redirection if needed.
virtual void Seek(Long64_t offset, ERelativeTo pos=kBeg)
Set position from where to start reading.
static const char * GetProxy()
Static method returning the global proxy URL.
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.
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.
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.
virtual void CheckProxy()
Check if shell var "http_proxy" has been set and should be used.
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 fFullCacheSize
complete content of the file, some http server may return complete content
virtual Bool_t ReadBuffer(char *buf, Int_t len)
Read specified byte range from remote file via HTTP daemon.
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 void Init(Bool_t readHeadOnly)
Initialize a TWebFile object.
virtual Long64_t GetSize() const
Return maximum file size.
virtual Bool_t IsOpen() const
A TWebFile that has been correctly constructed is always considered open.
static TUrl fgProxy
size of the cached content
static void SetMaxFullCacheSize(Long64_t sz)
Static method, set maxmimal size of full cache,.
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.
static Long64_t GetMaxFullCacheSize()
Static method returning maxmimal size of full cache, which can be preserved by file instance.
virtual void ProcessHttpHeader(const TString &headerLine)
Process the HTTP header in the argument.
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
void * OpenDirectory(const char *name)
Open a directory via httpd.
TWebSystem()
Create helper class that allows directory access via httpd.
Bool_t AccessPathName(const char *path, EAccessMode mode)
Returns FALSE if one can access a file using the specified access mode.
void FreeDirectory(void *dirp)
Free directory via httpd.
const char * GetDirEntry(void *dirp)
Get directory entry via httpd. Returns 0 in case no more entries.
Int_t MakeDirectory(const char *name)
Make a directory via httpd. Not supported.
Int_t GetPathInfo(const char *path, FileStat_t &buf)
Get info about a file.
Int_t Unlink(const char *path)
Unlink, i.e.