int HashFile(char **Return, const char *Type, const char *Path, int Encoding) { HASH *Hash; STREAM *S; char *Tempstr=NULL; int result; S=STREAMOpen(Path,"r"); if (! S) return(FALSE); Hash=HashInit(Type); if (! Hash) { STREAMClose(S); return(FALSE); } Tempstr=SetStrLen(Tempstr,4096); result=STREAMReadBytes(S,Tempstr,4096); while (result !=EOF) { Hash->Update(Hash, Tempstr, result); result=STREAMReadBytes(S,Tempstr,4096); } DestroyString(Tempstr); STREAMClose(S); result=HashFinish(Hash, Encoding, Return); return(result); }
int SMTPSendMailFile(const char *Sender, const char *Recipient, const char *Path, int Flags) { char *Tempstr=NULL; STREAM *S, *F; int result=FALSE; F=STREAMOpen(Path, "r"); if (F) { S=SMTPConnect(Sender, Recipient, Flags); if (S) { STREAMSendFile(F, S, 0, SENDFILE_LOOP); STREAMWriteLine("\r\n.\r\n", S); SMTPInteract("", S); SMTPInteract("QUIT\r\n", S); result=TRUE; STREAMClose(S); } STREAMClose(F); } else RaiseError(0,"SMTPSendMailFile","Failed to open file for sending"); DestroyString(Tempstr); return(result); }
void ListNativeFile(char *Path) { STREAM *S; char *Tempstr=NULL, *Token=NULL, *ptr; S=STREAMOpenFile(Settings.AuthPath,SF_RDONLY); if (S) { Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { StripTrailingWhitespace(Tempstr); ptr=GetToken(Tempstr,":",&Token,0); printf("%s ",Token); ptr=GetToken(ptr,":",&Token,0); //passtype ptr=GetToken(ptr,":",&Token,0); //password ptr=GetToken(ptr,":",&Token,0); //realuser printf("RealUser=%s ",Token); ptr=GetToken(ptr,":",&Token,0); //homedir printf("Dir=%s %s\n",Token,ptr); Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); } DestroyString(Tempstr); DestroyString(Token); }
STREAM *ConnectManagerAddClient(char *Host, int Port, int Flags, char *Name, CONNECT_FUNC OnConnect, ONDATA_FUNC OnData) { STREAM *S; TConnectManagerItem *Item; if (! ConnectManClients) ConnectManClients=ListCreate(); S=STREAMCreate(); if (! STREAMConnectToHost(S,Host,Port,Flags)) { STREAMClose(S); return(NULL); } Item=(TConnectManagerItem *) calloc(1,sizeof(TConnectManagerItem)); Item->OnConnect=OnConnect; Item->OnData=OnData; Item->Data=(void *) S; Item->Name=CopyStr(Item->Name,Name); Item->Host=CopyStr(Item->Host,Host); Item->Port=Port; if (Item->OnConnect && STREAMIsConnected(S)) Item->OnConnect(Item); ListAddItem(ConnectManClients,Item); return(S); }
void ListNativeFile(STREAM *Out, char *Path) { STREAM *S; char *Tempstr=NULL, *Token=NULL, *SendStr=NULL, *ptr; S=STREAMOpenFile(Settings.AuthFile,O_RDONLY); if (S) { Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { StripTrailingWhitespace(Tempstr); ptr=GetToken(Tempstr,":",&Token,0); SendStr=MCopyStr(SendStr,Token," ",NULL); ptr=GetToken(ptr,":",&Token,0); //passtype ptr=GetToken(ptr,":",&Token,0); //password ptr=GetToken(ptr,":",&Token,0); //realuser SendStr=MCatStr(SendStr,"realuser="******" ",NULL); ptr=GetToken(ptr,":",&Token,0); //homedir SendStr=MCatStr(SendStr,"homedir=",Token," ",NULL); SendStr=MCatStr(SendStr,ptr,"\n",NULL); STREAMWriteLine(SendStr,Out); Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); } STREAMFlush(Out); DestroyString(Tempstr); DestroyString(SendStr); DestroyString(Token); }
ListNode *ConfigFileLoadFileStores(char *Path) { STREAM *S; TFileStore *FS=NULL; char *Tempstr=NULL, *Token=NULL, *ptr; if (! FileStores) FileStores=ListCreate(); S=STREAMOpenFile(Path,O_RDONLY); if (S) { Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { StripTrailingWhitespace(Tempstr); ptr=GetToken(Tempstr," ",&Token,0); if (strcmp(Token,"FileStore")==0) { FS=ConfigFileReadFileStore(S, ptr); ListAddNamedItem(FileStores,FS->Name,FS); } Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); } DestroyString(Tempstr); DestroyString(Token); return(FileStores); }
int OAuthLoad(OAUTH *Ctx, const char *ReqName, const char *Path) { STREAM *S; char *Tempstr=NULL, *Token=NULL, *Name=NULL; const char *ptr; int result=FALSE; if (StrValid(ReqName)) Name=CopyStr(Name, ReqName); else Name=CopyStr(Name, Ctx->Name); Ctx->AccessToken=CopyStr(Ctx->AccessToken, ""); Ctx->RefreshToken=CopyStr(Ctx->RefreshToken, ""); if (! StrValid(Path)) S=STREAMOpen(Ctx->SavePath,"rl"); else S=STREAMOpen(Path,"rl"); if (S) { Tempstr=STREAMReadLine(Tempstr, S); while (Tempstr) { ptr=GetToken(Tempstr, "\\S", &Token, GETTOKEN_QUOTES); if (strcmp(Token, Name)==0) { OAuthParse(Ctx, Tempstr); if (StrValid(Ctx->AccessToken) || StrValid(Ctx->RefreshToken)) result=TRUE; } Tempstr=STREAMReadLine(Tempstr, S); } STREAMClose(S); } DestroyString(Tempstr); DestroyString(Token); DestroyString(Name); return(result); }
int PipeCommandProcessorClose(TProcessingModule *ProcMod) { STREAMClose((STREAM *) ProcMod->Data); ProcMod->Data=NULL; return(TRUE); }
int DownloadM3U(char *URL, char *Title, int Flags) { char *Tempstr=NULL, *ID=NULL, *Doc=NULL, *ptr; int Port=0, BytesRead=0, len=0, count=0; int RetVal=FALSE; ListNode *Items, *Curr; int M3UType=M3U_PLAYLIST; STREAM *Con; if (Flags & FLAG_DEBUG) fprintf(stderr,"M3U STREAM: %s\n",URL); Items=ListCreate(); Con=ConnectAndRetryUntilDownload(URL, 0, 0); if (Con) { Tempstr=STREAMReadLine(Tempstr,Con); while (Tempstr) { StripTrailingWhitespace(Tempstr); StripLeadingWhitespace(Tempstr); if (Flags & (FLAG_DEBUG2 | FLAG_DEBUG3)) fprintf(stderr,"%s\n",Tempstr); if (StrLen(Tempstr)) { if (strncmp("#EXT-X-STREAM-INF",Tempstr,StrLen("#EXT-X-STREAM-INF"))==0) { RetVal=M3UStreamInfo(Con,Title,URL,Tempstr,Flags); M3UType=M3U_STREAMINFO; } else if (strncmp("#EXT-X-MEDIA-SEQUENCE",Tempstr,StrLen("#EXT-X-MEDIA-SEQUENCE"))==0) M3UType=M3U_PLAYLIST; else if (*Tempstr != '#') { if (strncasecmp(Tempstr,"http",4) !=0) { Doc=CopyStr(Doc,URL); ptr=strrchr(Doc,'/'); if (ptr) *ptr='\0'; Doc=MCatStr(Doc,"/",Tempstr,NULL); } else Doc=CopyStr(Doc,Tempstr); ListAddItem(Items,CopyStr(NULL,Doc)); } } Tempstr=STREAMReadLine(Tempstr,Con); } STREAMClose(Con); if (M3UType == M3U_PLAYLIST) RetVal=DownloadStream(URL, Title, Items, Flags); } ListDestroy(Items,DestroyString); DestroyString(Tempstr); DestroyString(Doc); DestroyString(ID); return(RetVal); }
int OAuthSave(OAUTH *Ctx, const char *Path) { STREAM *S; const char *Fields[]= {"client_id","client_secret","access_token","refresh_token",NULL}; const char *ptr; char *Tempstr=NULL; int i; if (! StrValid(Path)) { if (! StrValid(Ctx->SavePath)) return(FALSE); S=STREAMOpen(Ctx->SavePath,"aEL"); } else S=STREAMOpen(Path,"aEL"); if (S) { Tempstr=MCopyStr(Tempstr, "'", Ctx->Name,"' ",NULL); for (i=0; Fields[i] !=NULL; i++) { ptr=GetVar(Ctx->Vars,Fields[i]); if (StrValid(ptr)) Tempstr=MCatStr(Tempstr, Fields[i], "='", ptr, "' ",NULL); } Tempstr=CatStr(Tempstr,"\n"); STREAMWriteLine(Tempstr, S); STREAMClose(S); } DestroyString(Tempstr); return(TRUE); }
STREAM *MemcachedConnect(const char *Server) { if (StrLen(Server)) { if (MCS) STREAMClose(MCS); MCS=STREAMCreate(); if (! STREAMTCPConnect(MCS, Server, 11211,0,0, 0)) { STREAMClose(MCS); MCS=NULL; } } return(MCS); }
int LogFileAppendTempLog(const char *LogPath, const char *TmpLogPath) { TLogFile *LogFile; char *Tempstr=NULL; STREAM *S; int result=FALSE; LogFile=LogFileGetEntry(LogPath); LogFileClose(TmpLogPath); S=STREAMOpenFile(TmpLogPath,SF_RDONLY); if (LogFile && S) { STREAMLock(LogFile->S,LOCK_EX); Tempstr=STREAMReadLine(Tempstr,S); while(Tempstr) { STREAMWriteLine(Tempstr,LogFile->S); Tempstr=STREAMReadLine(Tempstr,S); } if (LogFile->Flags & LOGFILE_FLUSH) STREAMFlush(LogFile->S); STREAMLock(LogFile->S,LOCK_UN); unlink(TmpLogPath); result=TRUE; } if (S) STREAMClose(S); DestroyString(Tempstr); return(result); }
void HTTPServerRecieveURL(STREAM *S,HTTPSession *Heads) { STREAM *Doc; struct stat FileStat; char *Buffer=NULL, *Tempstr=NULL; int BuffSize=4096; Doc=STREAMOpenFile(Heads->Path, SF_CREAT | SF_TRUNC | SF_WRONLY); if (! Doc) HTTPServerSendHTML(S, Heads, "403 Forbidden","Can't open document for write."); else { fchmod(Doc->in_fd,0660); Buffer=SetStrLen(Buffer,BuffSize); STREAMSendFile(S,Doc,Heads->ContentSize, SENDFILE_KERNEL | SENDFILE_LOOP); STREAMClose(Doc); stat(Heads->Path,&FileStat); LogToFile(Settings.LogPath,"%s@%s (%s) uploaded %s (%d bytes)",Heads->UserName,Heads->ClientHost,Heads->ClientIP,Heads->Path,FileStat.st_size); HTTPServerSendHTML(S, Heads, "201 Created",""); } DestroyString(Tempstr); DestroyString(Buffer); }
int SMTPSendMail(const char *Sender, const char *Recipient, const char *Subject, const char *Body, int Flags) { char *Tempstr=NULL; STREAM *S; int result=FALSE; S=SMTPConnect(Sender, Recipient, Flags); if (S) { if (! (Flags & SMTP_NOHEADER)) { Tempstr=MCopyStr(Tempstr,"Date: ", GetDateStr("%a, %d %b %Y %H:%M:%S", NULL), "\r\n", NULL); Tempstr=MCatStr(Tempstr,"From: ", Sender, "\r\n", NULL); Tempstr=MCatStr(Tempstr,"To: ", Recipient, "\r\n", NULL); Tempstr=MCatStr(Tempstr,"Subject: ", Subject, "\r\n\r\n", NULL); STREAMWriteLine(Tempstr, S); } STREAMWriteLine(Body, S); STREAMWriteLine("\r\n.\r\n", S); SMTPInteract("", S); SMTPInteract("QUIT\r\n", S); result=TRUE; STREAMClose(S); } DestroyString(Tempstr); return(result); }
void HTTPServerHandleStream(STREAM *Output, HTTPSession *Session, char *SearchPath, int SendData) { char *Tempstr=NULL; HTTPSession *Response; ListNode *Vars; STREAM *S; glob_t Glob; int i; Vars=ListCreate(); SetVar(Vars,"ContentType","audio/mpeg"); Response=FileSendCreateSession("", Session, Vars, 0); HTTPServerSendHeaders(Output, Response, FALSE); STREAMFlush(Output); Tempstr=MCopyStr(Tempstr,SearchPath,"/*",NULL); glob(Tempstr,0,0,&Glob); LogToFile(Settings.LogPath,"Stream from Dir: %s, %d files",SearchPath,Glob.gl_pathc); for (i=0; i < Glob.gl_pathc; i++) { S=STREAMOpenFile(Glob.gl_pathv[i],SF_RDONLY); if (S) { IcecastSendData(S, Output, 4096000); STREAMClose(S); } } globfree(&Glob); DestroyString(Tempstr); ListDestroy(Vars,DestroyString); }
void GetClientHardwareAddress(TSession *Session) { STREAM *S; char *Tempstr=NULL, *Token=NULL, *ptr; S=STREAMOpenFile("/proc/net/arp",O_RDONLY); if (S) { Tempstr=STREAMReadLine(Tempstr,S); Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { ptr=GetToken(Tempstr,"\\S",&Token,0); if (strcmp(Token,Session->ClientIP)==0) { //HW Type ptr=GetToken(ptr,"\\S",&Token,0); //Flags ptr=GetToken(ptr,"\\S",&Token,0); //MAC ptr=GetToken(ptr,"\\S",&Session->ClientMAC,0); } Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); } DestroyString(Tempstr); DestroyString(Token); }
char *OAuthGetAccessToken(char *RetStr, const char *URL, const char *Key, const char *RequestToken) { STREAM *S; char *Tempstr=NULL, *Name=NULL, *Value=NULL, *ptr; int len; Tempstr=MCopyStr(Tempstr,"consumer_key=",Key,"&code=",RequestToken,NULL); S=HTTPMethod("POST",URL,"","","application/x-www-form-urlencoded; charset=UTF-8",Tempstr,StrLen(Tempstr)); if (S) { STREAMReadToString(S, &Tempstr, &len, NULL); ptr=GetNameValuePair(Tempstr,"&","=",&Name,&Value); while (ptr) { if (strcmp(Name,"access_token")==0) RetStr=CopyStr(RetStr,Value); ptr=GetNameValuePair(ptr,"&","=",&Name,&Value); } STREAMClose(S); } DestroyString(Name); DestroyString(Value); DestroyString(Tempstr); return(RetStr); }
STREAM *HTTPSetupConnection(HTTPInfoStruct *Info, int ForceHTTPS) { char *Proto=NULL, *Host=NULL, *Token=NULL; int Port=0, Flags=0; STREAM *S; S=STREAMCreate(); if (Info->Flags & HTTP_PROXY) { if (! Info->ProxyAuthorization) { Info->ProxyAuthorization=(HTTPAuthStruct *) calloc(1,sizeof(HTTPAuthStruct)); } ParseURL(Info->Proxy, &Proto, &Host, &Token, &Info->ProxyAuthorization->Logon, &Info->ProxyAuthorization->Password,NULL,NULL); Port=atoi(Token); if (ForceHTTPS) Proto=CopyStr(Proto,"https"); if (strcasecmp(Proto,"https")==0) Flags |= CONNECT_SSL; } else { Host=CopyStr(Host,Info->Host); Port=Info->Port; if (Info->Flags & HTTP_SSL) Flags |= CONNECT_SSL; if (ForceHTTPS) { Flags |= CONNECT_SSL; } if (Port==0) { if (Flags & CONNECT_SSL) Port=443; else Port=80; } } if (Info->Flags & HTTP_TUNNEL) STREAMAddConnectionHop(S,Info->Proxy); if (STREAMConnectToHost(S,Host,Port,Flags)) { HTTPSendHeaders(S,Info); } else { STREAMClose(S); S=NULL; } Info->S=S; DestroyString(Token); DestroyString(Proto); DestroyString(Host); return(S); }
void HTTPServerSendM3U(STREAM *S, HTTPSession *Session, char *Path, int NoOfFiles, TPathItem **Files) { char *Tempstr=NULL, *M3U=NULL, *URL=NULL, *Salt=NULL, *AccessToken=NULL, *ptr; ListNode *Vars; STREAM *F; int i; M3U=CopyStr(M3U,"#EXTM3U\n"); for (i=0; i < NoOfFiles; i++) { if (InFileTypeList(Files[i]->Path,Settings.M3UFileTypes)) { //Examine file for Artist/title information Vars=ListCreate(); F=STREAMOpenFile(Files[i]->Path, SF_RDONLY); if (F) { MediaReadDetails(F, Vars); STREAMClose(F); } ptr=GetVar(Vars, "Media-title"); if (StrLen(ptr)) { //#EXTINF - extra info - length (seconds), title Tempstr=CopyStr(Tempstr, GetVar(Vars, "Media-artist")); if (! StrLen(Tempstr)) Tempstr=CopyStr(Tempstr,"unknown-artist"); M3U=MCatStr(M3U,"#EXTINF: -1, ", Tempstr, "-", GetVar(Vars,"Media-title"),"\n",NULL); } //Actually supply the URL M3U=CatStr(M3U,Files[i]->URL); //if we are supporting access token authentication, supply that if (AuthenticateExamineMethods(Settings.AuthMethods, FALSE) & AUTH_ACCESSTOKEN) { GenerateRandomBytes(&Salt,24,ENCODE_HEX); AccessToken=MakeAccessToken(AccessToken, Session->UserName, Salt, Session->ClientIP, Files[i]->URL); M3U=MCatStr(M3U,"?AccessToken=",AccessToken,NULL); } ListDestroy(Vars,DestroyString); M3U=CatStr(M3U,"\n"); } } Tempstr=MCopyStr(Tempstr,Path,".m3u",NULL); SetVar(Session->Headers,"Content-disposition",Tempstr); HTTPServerSendResponse(S, Session, "200 OK","audio/x-mpegurl",M3U); DestroyString(AccessToken); DestroyString(Tempstr); DestroyString(Salt); DestroyString(URL); DestroyString(M3U); }
int CopyLocalItem(char *From, char *To) { glob_t Glob; struct stat FStat; char *Tempstr=NULL, *ptr; int i, RetVal=EFAULT; STREAM *In=NULL, *Out=NULL; stat(From,&FStat); if (S_ISDIR(FStat.st_mode)) { mkdir(To,FStat.st_mode); Tempstr=MCopyStr(Tempstr, From, "/*", NULL); glob(Tempstr, 0, 0, &Glob); for (i=0; i < Glob.gl_pathc; i++) { ptr=strrchr(Glob.gl_pathv[i],'/'); if (! ptr) ptr=Glob.gl_pathv[i]; Tempstr=MCopyStr(Tempstr, To, ptr, NULL); CopyLocalItem(Glob.gl_pathv[i],Tempstr); } RetVal=0; globfree(&Glob); } else { In=STREAMOpenFile(From,SF_RDONLY); if (In) { Out=STREAMOpenFile(To, SF_CREAT| SF_WRONLY | SF_TRUNC); if (Out) RetVal=STREAMSendFile(In, Out, 0, SENDFILE_KERNEL | SENDFILE_LOOP); } } //as In and Out are NULL if not opened, it's safe to close them //here as STREAMClose will ignore a NULL argument STREAMClose(In); STREAMClose(Out); DestroyString(Tempstr); return(RetVal); }
int DisplayAvailableFormats(ListNode *Vars, char *Formats, int ShowSize) { char *URL=NULL, *Token=NULL, *TokenID=NULL, *Tempstr=NULL, *ptr; STREAM *S; int result=TRUE; fprintf(stderr, "\nFormats available for this Movie: "); ptr=GetToken(Formats," ",&Token,0); while (ptr) { if (StrLen(Token)) TokenID=MCopyStr(TokenID,"item:",Token,NULL); URL=CopyStr(URL,GetVar(Vars,TokenID)); if (strcmp(Token,"reference") !=0) { fprintf(stderr,"%s",Token); if (ShowSize) { S=HTTPMethod("HEAD",URL,NULL,NULL); if (S) { Tempstr=CopyStr(Tempstr,STREAMGetValue(S,"HTTP:ResponseCode")); if (strcmp(Tempstr,"403") ==0) { printf("\nERROR: %s response for %s\n",Tempstr,URL); result=FALSE; break; } else if (strcmp(Tempstr,"200")==0) { Tempstr=CopyStr(Tempstr,STREAMGetValue(S,"HTTP:Content-length")); fprintf(stderr, " (%s)",GetHumanReadableDataQty(strtod(Tempstr,NULL),FALSE)); STREAMClose(S); } } } } fprintf(stderr,", "); ptr=GetToken(ptr," ",&Token,0); } fprintf(stderr,"\n\n",Tempstr); DestroyString(Token); DestroyString(TokenID); DestroyString(Tempstr); DestroyString(URL); return(result); }
int AuthNativeFile(HTTPSession *Session, int HTTPDigest, char **RealUser, char **HomeDir, char **UserSettings) { STREAM *S; char *Tempstr=NULL, *ptr; char *Name=NULL, *Pass=NULL, *PasswordType=NULL, *Trash=NULL; int RetVal=USER_UNKNOWN; S=STREAMOpenFile(Settings.AuthPath,SF_RDONLY); if (! S) return(USER_UNKNOWN); Tempstr=STREAMReadLine(Tempstr,S); while (Tempstr) { StripTrailingWhitespace(Tempstr); ptr=GetToken(Tempstr,":",&Name,0); if (strcasecmp(Name,Session->UserName)==0) { ptr=GetToken(ptr,":",&PasswordType,0); ptr=GetToken(ptr,":",&Pass,0); if (RealUser) ptr=GetToken(ptr,":",RealUser,0); else ptr=GetToken(ptr,":",&Trash,0); if (HomeDir) ptr=GetToken(ptr,":",HomeDir,0); else ptr=GetToken(ptr,":",&Trash,0); if (UserSettings) ptr=GetToken(ptr,":",UserSettings,0); else ptr=GetToken(ptr,":",&Trash,0); RetVal=FALSE; if (HTTPDigest) RetVal=NativeFileCheckHTTPDigestAuth(Session, PasswordType, Pass, Session->Password); else RetVal=NativeFileCheckPassword(Name,PasswordType,Pass,Session->Password); break; } Tempstr=STREAMReadLine(Tempstr,S); } STREAMClose(S); if ((RetVal==TRUE) && (Settings.Flags & FLAG_LOG_VERBOSE)) LogToFile(Settings.LogPath,"AUTH: UserName '%s' Authenticated via %s.",Session->UserName,Settings.AuthPath); AuthenticationsTried=CatStr(AuthenticationsTried,"native "); DestroyString(Name); DestroyString(Pass); DestroyString(Tempstr); DestroyString(PasswordType); return(RetVal); }
int FileCopyWithProgress(const char *SrcPath, const char *DestPath, DATA_PROGRESS_CALLBACK Callback) { STREAM *Src; int result; Src=STREAMOpen(SrcPath,"r"); if (! Src) return(FALSE); if (Callback) STREAMAddProgressCallback(Src,Callback); result=STREAMCopy(Src, DestPath); STREAMClose(Src); return(result); }
int M3UStreamDownload(STREAM *ManifestCon, const char *URL, const char *Title) { STREAM *Con=NULL; char *Tempstr=NULL, *BasePath=NULL, *Line=NULL; const char *ptr; ListNode *Segments, *Curr; int result; double BytesRead=0; Segments=ListCreate(); ptr=strrchr(URL, '/'); if (ptr) { BasePath=CopyStrLen(BasePath, URL, ptr - URL); Line=STREAMReadLine(Line,ManifestCon); while (Line) { StripLeadingWhitespace(Line); StripTrailingWhitespace(Line); if (*Line != '#') { Tempstr=MCopyStr(Tempstr, BasePath, "/", Line, NULL); ListAddItem(Segments, CopyStr(NULL, Tempstr)); } Line=STREAMReadLine(Line,ManifestCon); } OpenOutputFiles(Title, URL, &BytesRead); Tempstr=SetStrLen(Tempstr,BUFSIZ); Curr=ListGetNext(Segments); while (Curr) { Con=ConnectAndRetryUntilDownload(Curr->Item, 0, 0); if (Con) { TransferItem(Con, Title, URL, "m3u8-stream", 0, 0, &BytesRead, FALSE); STREAMClose(Con); } Curr=ListGetNext(Curr); } CloseOutputFiles(); } ListDestroy(Segments, DestroyString); DestroyString(Tempstr); DestroyString(BasePath); DestroyString(Line); }
void LogFileClose(const char *Path) { ListNode *Node; TLogFile *LogFile; Node=ListFindNamedItem(LogFiles,Path); if (Node) { LogFile=(TLogFile *) Node->Item; ListDeleteNode(Node); DestroyString(LogFile->Path); STREAMClose(LogFile->S); free(LogFile); } }
int CopyURL(HTTPSession *Session, char *From, char *To) { char *Tempstr=NULL, *Host=NULL, *PortStr=NULL, *FromPath=NULL, *ToPath=NULL, *User=NULL, *Password=NULL; int RetVal=EFAULT; STREAM *In=NULL, *Out=NULL; ParseURL(To, &Tempstr, &Host, &Tempstr, NULL, NULL, &ToPath, NULL); if ((access(ToPath,F_OK)==0) && (! (Session->Flags & SESSION_OVERWRITE))) RetVal=EEXIST; //If the TO Host is local if (IsLocalHost(Session,Host)) { ParseURL(From, &Tempstr, &Host, &PortStr, &User, &Password, &FromPath, NULL); if (! IsLocalHost(Session,Host)) { In=HTTPGet(From,User,Password); if (In) Out=STREAMOpenFile(ToPath,SF_CREAT|SF_WRONLY|SF_TRUNC); if (Out) RetVal=STREAMSendFile(In, Out, 0, SENDFILE_KERNEL | SENDFILE_LOOP); STREAMClose(In); STREAMClose(Out); } else RetVal=CopyLocalItem(FromPath, ToPath); } DestroyString(User); DestroyString(Password); DestroyString(Tempstr); DestroyString(Host); DestroyString(PortStr); DestroyString(FromPath); DestroyString(ToPath); return(RetVal); }
int IDriveLoadDir(TFileStore *FS, char *InPattern, ListNode *Items, int Flags) { int result; char *Tempstr=NULL, *XML=NULL; char *TagName=NULL, *TagData=NULL, *ptr; TFileInfo *FI; HTTPInfoStruct *Info; ListNode *Vars; Tempstr=MCopyStr(Tempstr,"https://",FS->Host,"/evs/browseFolder?uid=",FS->Logon,"&pwd=",FS->Passwd,"&p=",FS->CurrDir,NULL); FS->S=HTTPMethod("POST",Tempstr,"","","","",0); Tempstr=STREAMReadDocument(Tempstr, FS->S, TRUE); ptr=XMLGetTag(Tempstr,NULL,&TagName,&TagData); while (ptr) { if (strcmp(TagName,"item")==0) { FI=IDriveReadFileEntry(TagData); if (Items) ListAddNamedItem(Items,FI->Name,FI); } ptr=XMLGetTag(ptr,NULL,&TagName,&TagData); } STREAMClose(FS->S); FS->S=NULL; Tempstr=MCopyStr(Tempstr,"https://",FS->Host,"/evs/getAccountQuota?uid=",FS->Logon,"&pwd=",FS->Passwd,NULL); FS->S=HTTPMethod("POST",Tempstr,"","","","",0); Tempstr=STREAMReadDocument(Tempstr, FS->S, TRUE); Vars=ListCreate(); IDriveParseResponse(Tempstr,Vars); FS->BytesAvailable=strtod(GetVar(Vars,"totalquota"),NULL); FS->BytesUsed=strtod(GetVar(Vars,"usedquota"),NULL); ListDestroy(Vars,DestroyString); DestroyString(TagName); DestroyString(TagData); DestroyString(Tempstr); DestroyString(XML); return(TRUE); }
int DownloadStream(char *URL, char *Title, ListNode *Items, int Flags) { STREAM *Con=NULL, *S=NULL; ListNode *Curr; char *Tempstr=NULL, *ptr; char *Token=NULL; int Port; double len=0, ApproxDocSize=0, BytesRead=0; Curr=ListGetNext(Items); if (Flags & (FLAG_TEST | FLAG_TEST_SITES) ) { if (Flags & FLAG_TEST) fprintf(stderr,"TEST MODE: would have downloaded '%s' url=%s\n",Title,Curr->Item); if (Curr) return(TRUE); return(FALSE); } OpenOutputFiles(Title,URL,&BytesRead); while (Curr) { if (strncmp((char *) Curr->Item,"http:",5)==0) Tempstr=CopyStr(Tempstr,(char *) Curr->Item); else { Tempstr=CopyStr(Tempstr,URL); ptr=strrchr(Tempstr,'/'); if (ptr) *ptr='\0'; Tempstr=MCatStr(Tempstr,"/",(char *) Curr->Item,NULL); } Con=ConnectAndRetryUntilDownload(Tempstr, 0, 0); if (Con) { ptr=STREAMGetValue(Con,"HTTP:content-length"); if (ptr) len=atof(ptr); if (ApproxDocSize==0) ApproxDocSize=ListSize(Items) * len; TransferItem(Con, Title, Curr->Item, "", len, ApproxDocSize, &BytesRead, BytesRead==0); STREAMClose(Con); } Curr=ListGetNext(Curr); } CloseOutputFiles(); DestroyString(Tempstr); DestroyString(Token); return(TRUE); }
int IDriveOpen(TFileStore *FS) { char *Tempstr=NULL, *TagName=NULL, *TagData=NULL, *ptr; ListNode *Vars; STREAM *S; int result=FALSE; if (! FS) { return(FALSE); } //Always require login for IDrive if (! StrLen(FS->Passwd)) RequestAuthDetails(&FS->Logon,&FS->Passwd); time(&StartTime); HTTPSetFlags(HTTP_NODECODE); //Get Address of the server that stores my data FS->CurrDir=CopyStr(FS->CurrDir,"/"); if (StrLen(FS->InitDir)) FS->ChDir(FS,FS->InitDir); Tempstr=MCopyStr(Tempstr,"https://evs.idrive.com/evs/getServerAddress?uid=",FS->Logon,"&pwd=",FS->Passwd,NULL); S=HTTPMethod("POST",Tempstr,"","","","",0); if (S) { Tempstr=STREAMReadDocument(Tempstr, S, TRUE); Vars=ListCreate(); IDriveParseResponse(Tempstr, Vars); FS->Host=CopyStr(FS->Host,GetVar(Vars,"webApiServer")); PrintConnectionDetails(FS,S); STREAMClose(S); result=TRUE; } ListDestroy(Vars,DestroyString); DestroyString(Tempstr); DestroyString(TagData); DestroyString(TagName); return(result); }
void HTTPServerSendFile(STREAM *S, HTTPSession *Session, char *Path, ListNode *Vars, int Flags) { STREAM *Doc; HTTPSession *Response; char *Buffer=NULL, *Tempstr=NULL; int ICYInterval=4096000; Doc=STREAMOpenFile(Path, SF_RDONLY); if (! Doc) HTTPServerSendHTML(S, Session, "403 Forbidden","You don't have permission for that."); else { if (Session) { LogToFile(Settings.LogPath,"%s@%s (%s) downloading %s (%s bytes)",Session->UserName,Session->ClientHost,Session->ClientIP,Path,GetVar(Vars,"FileSize")); } Response=FileSendCreateSession(Path, Session, Vars, ICYInterval); MediaReadDetails(Doc,Vars); HTTPServerFormatExtraHeaders(Response,Vars); HTTPServerSendHeaders(S, Response, Flags); if (Response->Flags & SESSION_ENCODE_GZIP) { STREAMAddStandardDataProcessor(S,"compression","gzip","CompressionLevel=1"); } if (Flags & HEADERS_SENDFILE) { if (Session->Flags & SESSION_ICECAST) IcecastSendData(Doc, S, ICYInterval); else STREAMSendFile(Doc, S, 0, SENDFILE_KERNEL | SENDFILE_LOOP); } /* If HTTPServerSendHeaders set SESSION_REUSE then set that in the Session object if (Response->Flags & SESSION_REUSE) Session->Flags |= SESSION_REUSE; else Session->Flags &= ~SESSION_REUSE; */ STREAMClose(Doc); HTTPSessionDestroy(Response); } DestroyString(Buffer); DestroyString(Tempstr); }