void HTTPParseResponseLine(STREAM *S, HTTPInfoStruct *Info, char *Header) { char *ptr; if (Info->Flags & HTTP_DEBUG) fprintf(stderr,"RESPONSE: %s\n",Header); if (strncmp(Header,"HTTP/",5)==0) { ptr=strchr(Header,' '); ptr++; Info->ResponseCode=CopyStrLen(Info->ResponseCode,ptr,3); STREAMSetValue(S,"HTTP:ResponseCode",Info->ResponseCode); if (StrLen(Info->ResponseCode)) { if (*Info->ResponseCode=='3') { //No longer a flag, HTTP Redirect is now just a response code //Info->Flags |= HTTP_REDIRECT; } if (strcmp(Info->ResponseCode,"401")==0) { if (Info->Authorization) Info->Authorization->Flags |= HTTP_AUTH_BASIC; } if (strcmp(Info->ResponseCode,"407")==0) { if (Info->ProxyAuthorization) Info->ProxyAuthorization->Flags |= HTTP_PROXY_AUTH; } } } }
int pmatch_repeat(char **P_PtrPtr, char **S_PtrPtr, char *S_End, int *Flags) { char *SubPattern=NULL, *sp_ptr, *ptr; int count=0, i, val=0, result=MATCH_FAIL; ptr=*P_PtrPtr; while ( ((**P_PtrPtr) != '}') && ((**P_PtrPtr) != '\0') ) (*P_PtrPtr)++; if ((**P_PtrPtr)=='\0') return(MATCH_FAIL); SubPattern=CopyStrLen(SubPattern,ptr,*P_PtrPtr - ptr); sp_ptr=strchr(SubPattern,'|'); if (sp_ptr) { *sp_ptr='\0'; sp_ptr++; count=atoi(SubPattern); for (i=0; i < count; i++) { val=0; result=pmatch_search(sp_ptr, S_PtrPtr, S_End, NULL, NULL, &val); if (result==MATCH_CONT) result=MATCH_FAIL; if (result==MATCH_FAIL) break; } } (*S_PtrPtr)--; (*P_PtrPtr)++; DestroyString(SubPattern); return(result); }
void HTTPReadHeaders(STREAM *S, HTTPInfoStruct *Info) { char *Tempstr=NULL, *ptr; ListClear(Info->ServerHeaders,DestroyString); Info->ContentLength=0; //Not needed //Info->RedirectPath=CopyStr(Info->RedirectPath,""); Info->Flags &= ~(HTTP_CHUNKED | HTTP_GZIP | HTTP_DEFLATE); Tempstr=STREAMReadLine(Tempstr,S); if (Tempstr) { if (Info->Flags & HTTP_DEBUG) fprintf(stderr,"RESPONSE: %s\n",Tempstr); if (strncmp(Tempstr,"HTTP/",5)==0) { ptr=strchr(Tempstr,' '); ptr++; Info->ResponseCode=CopyStrLen(Info->ResponseCode,ptr,3); STREAMSetValue(S,"HTTP:ResponseCode",Info->ResponseCode); } Tempstr=STREAMReadLine(Tempstr,S); } while (Tempstr) { StripTrailingWhitespace(Tempstr); if (StrLen(Tempstr)==0) break; HTTPParseHeader(S, Info,Tempstr); Tempstr=STREAMReadLine(Tempstr,S); } //Handle Response Codes if (StrLen(Info->ResponseCode)) { if (*Info->ResponseCode=='3') { //No longer a flag, HTTP Redirect is now just a response code //Info->Flags |= HTTP_REDIRECT; } if (strcmp(Info->ResponseCode,"401")==0) { if (Info->Authorization) Info->Authorization->Flags |= HTTP_AUTH_BASIC; } if (strcmp(Info->ResponseCode,"407")==0) { if (Info->ProxyAuthorization) Info->ProxyAuthorization->Flags |= HTTP_PROXY_AUTH; } } S->BytesRead=0; DestroyString(Tempstr); }
int AuthShadowFile(HTTPSession *Session) { char *sptr, *eptr, *Salt=NULL, *Digest=NULL; int result=FALSE; #ifdef HAVE_SHADOW_H #include <shadow.h> struct spwd *pass_struct=NULL; pass_struct=getspnam(Session->UserName); if (pass_struct==NULL) return(USER_UNKNOWN); sptr=pass_struct->sp_pwdp; #ifdef HAVE_LIBCRYPT // this is an md5 password if ( (StrLen(sptr) > 4) && (strncmp(sptr,"$1$",3)==0) ) { eptr=strchr(sptr+3,'$'); Salt=CopyStrLen(Salt,sptr,eptr-sptr); Digest=CopyStr(Digest, crypt(Session->Password,Salt)); if (sptr && (strcmp(Digest,sptr)==0) ) { result=TRUE; } } else { // assume old des crypt password sptr=crypt(Session->Password,pass_struct->sp_pwdp); if (sptr && (strcmp(pass_struct->sp_pwdp, sptr)==0)) { result=TRUE; } } AuthenticationsTried=CatStr(AuthenticationsTried,"shadow "); #endif if (result && (Settings.Flags & FLAG_LOG_VERBOSE)) { LogToFile(Settings.LogPath,"AUTH: UserName '%s' Authenticated via /etc/shadow.",Session->UserName); } #endif DestroyString(Salt); DestroyString(Digest); return(result); }
int AuthShadowFile(TSession *Session) { char *sptr, *eptr, *Salt=NULL, *Digest=NULL; int result=FALSE; #ifdef HAVE_SHADOW_H #include <shadow.h> struct spwd *pass_struct=NULL; AuthenticationsTried=CatStr(AuthenticationsTried,"shadow "); pass_struct=getspnam(Session->User); if (pass_struct==NULL) return(USER_UNKNOWN); sptr=pass_struct->sp_pwdp; #ifdef HAVE_LIBCRYPT // this is an md5 password if ( (StrLen(sptr) > 4) && (strncmp(sptr,"$1$",3)==0) ) { eptr=strchr(sptr+3,'$'); Salt=CopyStrLen(Salt,sptr,eptr-sptr); Digest=CopyStr(Digest, crypt(Session->Passwd,Salt)); if (strcmp(Digest,sptr)==0) { result=TRUE; } } else { // assume old des crypt password if (Session->Passwd) { sptr=crypt(Session->Passwd,pass_struct->sp_pwdp); if (sptr && (strcmp(pass_struct->sp_pwdp, sptr)==0)) { result=TRUE; } } } #endif if (result) Session->RealUser=CopyStr(Session->RealUser,Session->User); #endif DestroyString(Salt); DestroyString(Digest); return(result); }
void ExtractVarsReadVar(const char **Fmt, const char **Msg, ListNode *Vars) { const char *FmtPtr, *MsgPtr; char *VarName=NULL; int len=0; ListNode *Node; FmtPtr=*Fmt; if (*FmtPtr=='(') FmtPtr++; while (*FmtPtr != ')') { VarName=AddCharToBuffer(VarName,len,*FmtPtr); len++; FmtPtr++; } if (*FmtPtr==')') FmtPtr++; MsgPtr=*Msg; while ((*MsgPtr !=0) && (*MsgPtr != *FmtPtr)) { if (*MsgPtr=='"') { do { MsgPtr++; } while ((*MsgPtr != '"') && (*MsgPtr != 0)); } MsgPtr++; } Node=ListFindNamedItem(Vars,VarName); if (Node) Node->Item=(void *) CopyStrLen((char *) Node->Item, *Msg, MsgPtr-*Msg); else Node=ListAddNamedItem(Vars,VarName,CopyStrLen(NULL, *Msg, MsgPtr-*Msg)); *Fmt=FmtPtr; *Msg=MsgPtr; DestroyString(VarName); }
char *ExtractVarsGetLiteralString(char *Buffer, const char *InStr) { char *RetStr; const char *ptr; RetStr=Buffer; ptr=InStr; while ((*ptr !=0) && (*ptr !='$') && (*ptr !='?') && (*ptr !='*')) ptr++; RetStr=CopyStrLen(Buffer,InStr,ptr-InStr); return(RetStr); }
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); }
int EscapeSequenceOSC(char **text, char *end) { char *text_start=NULL, *text_end=NULL; char *Str=NULL; //first character will be ']' (*text)++; switch (**text) { //set title bar case '0': case '1': case '2': (*text)++; if (**text==';') text_start=(*text)+1; while ((*text < end) && (**text != '\007')) (*text)++; if (*text==end) return(ES_PART); text_end=*text; if (**text =='\007') (*text)++; if (GlobalFlags & FLAG_STRIP_XTITLE) { if (text_start) { Str=CopyStrLen(Str, text_start, text_end-text_start); SetVar(Vars,"crayon_xtitle",Str); DestroyString(Str); } return(ES_STRIP); } if (GlobalFlags & FLAG_STRIP_ANSI) return(ES_STRIP); return(ES_OKAY); break; default: while ((*text < end) && (**text != '\007') && (**text != '\x1b')) (*text)++; if (*text==end) return(ES_PART); if (**text =='\007') (*text)++; if (GlobalFlags & FLAG_STRIP_ANSI) return(ES_STRIP); return(ES_OKAY); break; } }
int strntol(const char **ptr, int len, int radix, long *value) { const char *end; char *Tempstr=NULL; end=*ptr; if (! ptr_incr(&end, len)) { *ptr=end; return(FALSE); } Tempstr=CopyStrLen(Tempstr,*ptr,len); strlwr(Tempstr); *value=strtol(Tempstr,NULL,radix); DestroyString(Tempstr); *ptr=end; return(TRUE); }
int MakeDirPath(char *Path, int DirMask) { char *ptr; char *Tempstr=NULL; int result; ptr=Path; if (*ptr=='/') ptr++; ptr=strchr(ptr, '/'); while (ptr) { Tempstr=CopyStrLen(Tempstr,Path,ptr-Path); result=mkdir(Tempstr, DirMask); if ((result==-1) && (errno != EEXIST)) break; ptr=strchr(++ptr, '/'); } DestroyString(Tempstr); if (result==0) return(TRUE); return(FALSE); }
//colors can be either a color name or a forecolor/backcolor pair static int ParseColor(const char *ColorString) { const char *ptr; int val=0, Attrib=0; char *Token=NULL; ptr=strchr(ColorString,'/'); if (ptr) { Token=CopyStrLen(Token,ColorString,ptr-ColorString); val=MatchTokenFromList(ptr+1,Colors,0); if (val > 0) Attrib=val << 8; } else Token=CopyStr(Token, ColorString); val=MatchTokenFromList(Token,Colors,0); if (val > -1) Attrib |= val; Destroy(Token); return(Attrib); }
// Token will either be a test (with an operation like '<' '=' etc) or a list of // actions (colors, uppercase) to apply // this is used elsewhere, so not static const char *ParseActionToken(const char *Operations, TCrayon *Crayon) { char *Token=NULL; const char *ptr, *nptr; TCrayon *Action=NULL; int val=0; //Do this before anything else! ptr=Operations; Action=NewCrayonAction(Crayon,0); while (StrLen(ptr)) { nptr=GetToken(ptr,"\\S",&Token,GETTOKEN_QUOTES); switch (*Token) { case '=': case '!': case '<': case '>': if (StrValid(Action->Op)) Action=NewCrayonAction(Crayon,0); Action->Op=CopyStrLen(Action->Op, Token, 1); Action->Value=atof(Token+1); Action->String=CopyStr(Action->String, Token+1); Action->Type=CRAYON_COMPARATOR; break; default: nptr=ParseAttribs(ptr,Crayon,&Action); break; } ptr=nptr; } Destroy(Token); return(ptr); }
void HTTPParseCookie(HTTPInfoStruct *Info, char *Str) { char *startptr, *endptr; char *Tempstr=NULL; ListNode *Curr; int len; startptr=Str; while (*startptr==' ') startptr++; endptr=strchr(startptr,';'); if (endptr==NULL) endptr=startptr+strlen(Str); // if (( *endptr==';') || (*endptr=='\r') ) endptr--; Tempstr=CopyStrLen(Tempstr,startptr,endptr-startptr); Curr=ListGetNext(Cookies); endptr=strchr(Tempstr,'='); len=endptr-Tempstr; len--; while (Curr !=NULL) { if (strncmp(Curr->Item,Tempstr,len)==0) { Curr->Item=CopyStr(Curr->Item,Tempstr); DestroyString(Tempstr); return; } Curr=ListGetNext(Curr); } if (! Cookies) Cookies=ListCreate(); ListAddItem(Cookies,(void *)CopyStr(NULL,Tempstr)); DestroyString(Tempstr); }
char *HTTPUnQuote(char *RetBuff, char *Str) { char *RetStr=NULL, *Token=NULL, *ptr; int olen=0, ilen; RetStr=CopyStr(RetStr,""); ilen=StrLen(Str); for (ptr=Str; ptr < (Str+ilen); ptr++) { switch (*ptr) { case '+': RetStr=AddCharToBuffer(RetStr,olen,' '); olen++; break; case '%': ptr++; Token=CopyStrLen(Token,ptr,2); ptr++; //not +=2, as we will increment again RetStr=AddCharToBuffer(RetStr,olen,strtol(Token,NULL,16) & 0xFF); olen++; break; default: RetStr=AddCharToBuffer(RetStr,olen,*ptr); olen++; break; } } DestroyString(Token); return(RetStr); }
int MakeDirPath(const char *Path, int DirMask) { const char *ptr; char *Tempstr=NULL; int result=-1; ptr=Path; if (*ptr=='/') ptr++; ptr=strchr(ptr, '/'); while (ptr) { Tempstr=CopyStrLen(Tempstr,Path,ptr-Path); result=mkdir(Tempstr, DirMask); if ((result==-1) && (errno != EEXIST)) { RaiseError(ERRFLAG_ERRNO, "MakeDirPath", "cannot mkdir '%s'",Tempstr); break; } ptr=strchr(++ptr, '/'); } DestroyString(Tempstr); if (result==0) return(TRUE); return(FALSE); }
char *FormatFancyDirItem(char *RetStr, int count, TPathItem *File, const char *MimeIconsURL, int Flags) { char *Tempstr=NULL, *FileType=NULL, *DateStr=NULL, *DisplayName=NULL, *Interact=NULL; char *Comment=NULL, *CheckBox=NULL, *ptr; char *bgcolor; ListNode *Vars; Vars=ListCreate(); LoadFileProperties(File->Path, Vars); /* ptr=GetVar(Vars,"comment"); if (StrLen(ptr)) { Comment=MCopyStr(Comment," title=\"",ptr,"\" ",NULL); } else Comment=CopyStr(Comment,""); */ Comment=FormatFancyDirComment(Comment, Vars); if ((count % 2)==0) bgcolor="#FFFFFF"; else bgcolor="#CCCCCC"; if ((Now - File->Mtime) < 60) { DateStr=FormatStr(DateStr,"<font color=red>%d seconds ago</font>",Now - File->Mtime); } else DateStr=CopyStr(DateStr,GetDateStrFromSecs("%Y/%m/%d %H:%M:%S",File->Mtime,NULL)); FileType=FormatFileType(FileType, File, Vars, MimeIconsURL); //Okay, start building the actual table row RetStr=MCatStr(RetStr, "<tr bgcolor=\"",bgcolor,"\">",NULL); Interact=CopyStr(Interact,""); CheckBox=CopyStr(CheckBox,""); if (strcmp(File->Name,"..")==0) { DisplayName=CopyStr(DisplayName,".. (Parent Directory)"); if (Settings.DirListFlags & DIR_INTERACTIVE) CheckBox=CopyStr(CheckBox,"<td align=\"center\"> </td>"); } else { if (Settings.DisplayNameLen && (StrLen(File->Name) > Settings.DisplayNameLen)) { DisplayName=CopyStrLen(DisplayName,File->Name,Settings.DisplayNameLen); DisplayName=CatStr(DisplayName,"..."); } else DisplayName=CopyStr(DisplayName,File->Name); if (Settings.DirListFlags & DIR_INTERACTIVE) { if (Flags & SELECT_ALL) CheckBox=MCatStr(CheckBox,"<td align=\"center\"><input type=\"checkbox\" name=\"selected\" value=\"",File->Name,"\" checked /></td>",NULL); else CheckBox=MCatStr(CheckBox,"<td align=\"center\"><input type=\"checkbox\" name=\"selected\" value=\"",File->Name,"\" /></td>",NULL); //Interaction string will be added to end of line Interact=MCatStr(Interact,"<input type='submit' name='edit:",File->URL,"' value='Edit' /> ",NULL); Interact=MCatStr(Interact,"<input type='submit' name='del:",File->URL,"' value='Del' /> ",NULL); //one day, but not yet //if (strncasecmp(File->ContentType,"audio/",6)==0) Interact=MCatStr(Interact,"<input type=\"button\" onclick=\"javascript: addaudio('",File->URL,"');\" value=\"Play\" /> ",NULL); } } Tempstr=FormatStr(Tempstr,"%s<td title=\"%s\">%s</td><td><a href=\"%s\" title=\"%s\">%s</a></td><td align=right> %s</td><td align=right> %s</td><td align=center>%s</td>",CheckBox,Comment,FileType,File->URL, File->Path, DisplayName, DateStr, GetHumanReadableDataQty((double) File->Size,0), Interact); //Append it all to our output RetStr=MCatStr(RetStr,Tempstr,"</tr>\r\n",NULL); DestroyString(DisplayName); DestroyString(FileType); DestroyString(Interact); DestroyString(Comment); DestroyString(Tempstr); DestroyString(DateStr); ListDestroy(Vars,DestroyString); return(RetStr); }
int Login(TSession *Session) { char *Tempstr=NULL; int result, RetVal=FALSE; time_t Duration, Start, Now, LastActivity; Session->User=CopyStr(Session->User,NULL); Session->Password=CopyStr(Session->Password,NULL); alarm(60); //Clear out any crap Tempstr=SetStrLen(Tempstr,4096); result=TelnetReadBytes(Session->S, Tempstr, 4096, TNRB_ECHO | TNRB_NOPTY | TNRB_NONBLOCK); while (StrLen(Session->User)==0) { time(&LastActivity); if (Settings.IdleTimeout > 0) STREAMSetTimeout(Session->S, Settings.IdleTimeout); if (Settings.Flags & FLAG_CHALLENGE) { Session->Challenge=GenerateSalt(Session->Challenge, 24); Tempstr=MCopyStr(Tempstr, "Challenge/Response String: ", Session->Challenge, "\r\n", NULL); STREAMWriteLine(Tempstr, Session->S); } STREAMWriteLine("login: "******"Password: "******"\r\n",Session->S); if ((Settings.Flags & FLAG_LOCALONLY) && (! StrLen(Session->ClientMAC))) { syslog(Settings.ErrorLogLevel,"%s@%s NOT LOCAL. Denying Login.",Session->User,Session->ClientIP); } else if (Settings.Flags & FLAG_HONEYPOT){ //Original syslog(Settings.ErrorLogLevel,"%s@%s login denied (honeypot mode): user=%s pass=%s",Session->User,Session->ClientIP,Session->User,Session->Password); //Eric Wedaa added the following line to log to the LongTail honeypot consolidation server openlog("ptelnetd",LOG_PID|LOG_NDELAY,LOG_AUTH); syslog(Settings.ErrorLogLevel,"IP: %s TelnetLog: Username: %s Password: %s",Session->ClientIP,Session->User,Session->Password); alarm(60); } else if ( (! (Session->Flags & FLAG_DENYAUTH)) && (Authenticate(Session)) ) RetVal=TRUE; //Now that we've used the password, blank it from memory! result=StrLen(Session->Password); if (result > 0) memset(Session->Password,0,result); DestroyString(Tempstr); //STREAMDisassociateFromFD(S); return(RetVal); }
void ParseURL(const char *URL, char **Proto, char **Host, char **Port, char **User, char **Password, char **Path, char **Args) { const char *ptr; char *Token=NULL, *tProto=NULL, *aptr; //we might not return a protocol! if (Proto) *Proto=CopyStr(*Proto,""); //Even if they pass NULL for protocol, we need to take a copy for use in //the 'guess the port' section below ptr=strchr(URL,':'); if (ptr) { tProto=CopyStrLen(tProto,URL,ptr-URL); strlwr(tProto); aptr=strchr(tProto, '.'); if (aptr) { //protocol name is not allowed to contain '.', so this must be a hostname or //ip address. ptr=URL; } else { if (Proto) *Proto=CopyStr(*Proto,tProto); ptr++; //some number of '//' follow protocol while (*ptr=='/') ptr++; } ptr=GetToken(ptr,"/",&Token,0); ParseHostDetails(Token,Host,Port,User,Password); } else ptr=URL; while (ptr && (*ptr=='/')) ptr++; if (ptr) { if (Path) { *Path=MCopyStr(*Path,"/",ptr,NULL); //Only split the HTTP CGI arguments from the document path if we were //asked to return the args seperately if (Args) { aptr=strchr(*Path,'?'); if (! aptr) aptr=strchr(*Path,'#'); if (aptr) { *aptr='\0'; aptr++; *Args=CopyStr(*Args,aptr); } } } } //the 'GetToken' call will have thrown away the '/' at the start of the path //add it back in if (Port && (! StrValid(*Port)) && StrValid(tProto)) { if (strcmp(tProto,"http")==0) *Port=CopyStr(*Port,"80"); else if (strcmp(tProto,"https")==0) *Port=CopyStr(*Port,"443"); else if (strcmp(tProto,"ssh")==0) *Port=CopyStr(*Port,"22"); else if (strcmp(tProto,"ftp")==0) *Port=CopyStr(*Port,"21"); else if (strcmp(tProto,"telnet")==0) *Port=CopyStr(*Port,"23"); else if (strcmp(tProto,"smtp")==0) *Port=CopyStr(*Port,"25"); else if (strcmp(tProto,"mailto")==0) *Port=CopyStr(*Port,"25"); } DestroyString(Token); DestroyString(tProto); }
//This accepts a single pattern to match against. If that includes a space, it is thought to be a filename //with a space in. To supply multiple patterns look at "ProcessPutFiles" below. int InternalTransferFiles(TTransferContext *Ctx, char *Pattern) { ListNode *SrcItems=NULL, *DestItems=NULL, *Curr, *Node; TFileInfo *FI, *NewFI, *DestFI; int result=FALSE, IncludeResult=0; char *Tempstr=NULL, *ptr; int val; //if we've been given a path to something a few directories down, then do this if (Ctx->CmdFlags & FLAG_CMD_FULLPATH) { ptr=strrchr(Pattern,'/'); if (StrLen(ptr) > 1) { Tempstr=CopyStrLen(Tempstr,Pattern,ptr-Pattern); ptr++; result=InternalTransferDir(Ctx, Tempstr, ptr); DestroyString(Tempstr); return(result); } } SrcItems=ListCreate(); SrcItems->Flags |= LIST_FLAG_CASE; if (Ctx->CmdFlags & FLAG_CMD_SYNC) { DestItems=ListCreate(); DestItems->Flags |= LIST_FLAG_CASE; FileStoreLoadDir(Ctx->DestFS,"*",DestItems, LIST_REFRESH | Ctx->CompareLevel); FileStoreLoadDir(Ctx->SrcFS,Pattern,SrcItems, LIST_REFRESH | Ctx->CompareLevel); } else FileStoreLoadDir(Ctx->SrcFS,Pattern,SrcItems, LIST_REFRESH); Curr=ListGetNext(SrcItems); if (! Curr) result=FALSE; while (Curr) { FI=(TFileInfo *) Curr->Item; Node=ListFindNamedItem(DestItems,FI->Name); if (Node) DestFI=(TFileInfo *) Node->Item; else DestFI=NULL; if (FileIncluded(FI,Ctx->IncludePattern,Ctx->ExcludePattern,Ctx->CmdFlags, Ctx->Vars)) { IncludeResult=FileNeedsTransfer(Ctx, FI, DestFI); switch (IncludeResult) { case TRUE: if (FI->Type==FTYPE_DIR) { result=InternalTransferDir(Ctx, FI->Name, "*"); if (result != TRANSFER_OKAY) Ctx->Failed++; } else { if ((FI->Type==FTYPE_LINK)) { if (Ctx->DestFS->Settings & FS_SYMLINKS) result=Ctx->DestFS->Symlink(Ctx->DestFS, FI->Name, FI->EditPath); else result=InternalTransferDir(Ctx, FI->Name, "*"); } else result=InternalCopyFile(Ctx, FI); HandleTransferResult(Ctx, FI, result); if (result >= TRANSFER_OKAY) Ctx->Transferred++; else Ctx->Failed++; } break; case SKIP_EXISTS: if (Ctx->CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"INFO: '%s' exists, skipping.",FI->Name); break; case SKIP_AGE: if (Ctx->CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"INFO: '%s' doesn't match required age, skipping.",FI->Name); break; case SKIP_FILETYPE: if (Ctx->CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"INFO: '%s' unsupported filetype, skipping.",FI->Name); break; } Ctx->Total++; } if (result==ERR_CANCEL) break; Curr=ListGetNext(Curr); } ListDestroy(DestItems,FileInfoDestroy); ListDestroy(SrcItems,FileInfoDestroy); DestroyString(Tempstr); return(result); }
int ProcessCompare(TFileStore *DiskFS, TFileStore *FS, char *Command, char *Pattern, int CmdFlags, ListNode *Vars) { ListNode *Items=NULL, *DestinationItems=NULL, *Curr, *Node; char *Tempstr=NULL, *ptr; char *IncludePattern=NULL, *ExcludePattern=NULL; int result=CMP_FAIL, Flags=0, Checked=0, Differs=0, val; int CompareLevel=0; TFileInfo *FI, *RemoteFI; //if we've been given a path to something a few directories down, then do this ptr=strrchr(Pattern,'/'); if (ptr) { Tempstr=CopyStrLen(Tempstr,Pattern,ptr-Pattern); ptr++; result=InternalProcessCompareDir(DiskFS, FS, Command, Tempstr, ptr, CmdFlags, Vars); DestroyString(Tempstr); return(result); } DestinationItems=ListCreate(); FileStoreLoadDir(FS,Pattern,DestinationItems, LIST_REFRESH); IncludePattern=CopyStr(IncludePattern,GetVar(Vars,"IncludePattern")); ExcludePattern=CopyStr(ExcludePattern,GetVar(Vars,"ExcludePattern")); CompareLevel=atoi(GetVar(Vars,"CMP:CompareLevel")); if (CmdFlags & FLAG_CMD_VERBOSE) HandleEventMessage(Settings.Flags,"INFO: Comparing files by %s",GetCompareLevelName(CompareLevel)); Items=ListCreate(); DiskFS->LoadDir(DiskFS,Pattern,Items,CompareLevel); Curr=ListGetNext(Items); while (Curr) { FI=(TFileInfo *) Curr->Item; if (FileIncluded(FI,IncludePattern,ExcludePattern,CmdFlags, Vars)) { Node=ListFindNamedItem(DestinationItems,FI->Name); if (Node) RemoteFI=(TFileInfo *) Node->Item; else RemoteFI=NULL; result=FileStoreCompareFile(FS,DiskFS,RemoteFI,FI,CompareLevel); Tempstr=CopyStr(Tempstr,FI->Name); Checked++; if ((result == CMP_DIR) && (CmdFlags & FLAG_CMD_RECURSE)) InternalProcessCompareDir(DiskFS, FS, Command, FI->Name, "*", CmdFlags, Vars); else { HandleCompareResult(FI, CmdFlags, CompareLevel, GetVar(Vars,"Hook"), result); if (result < CompareLevel) Differs++; } } Curr=ListGetNext(Curr); } Curr=ListGetNext(DestinationItems); while (Curr) { FI=(TFileInfo *) Curr->Item; if (FileIncluded(FI,IncludePattern,ExcludePattern,CmdFlags, Vars)) { if (! ListFindNamedItem(Items,FI->Name)) { Tempstr=CopyStr(Tempstr,""); HandleCompareResult(FI, CmdFlags, CompareLevel, GetVar(Vars,"Hook"), CMP_REMOTE); Differs++; Checked++; } } Curr=ListGetNext(Curr); } val=atoi(GetVar(Vars,"CMP:Differs")) + Differs; Tempstr=FormatStr(Tempstr,"%d",val); SetVar(Vars,"CMP:Differs",Tempstr); val=atoi(GetVar(Vars,"CMP:Checked")) + Checked; Tempstr=FormatStr(Tempstr,"%d",val); SetVar(Vars,"CMP:Checked",Tempstr); ListDestroy(DestinationItems,FileInfoDestroy); ListDestroy(Items,FileInfoDestroy); DestroyString(ExcludePattern); DestroyString(IncludePattern); DestroyString(Tempstr); return(result); }
void HTTPParseHeader(STREAM *S, HTTPInfoStruct *Info, char *Header) { char *Token=NULL, *Tempstr=NULL; int count=0; int result; char *ptr; if (Info->Flags & HTTP_DEBUG) fprintf(stderr,"HEADER: %s\n",Header); ptr=GetToken(Header,":",&Token,0); while (isspace(*ptr)) ptr++; Tempstr=MCopyStr(Tempstr,"HTTP:",Token,NULL); STREAMSetValue(S,Tempstr,ptr); ListAddNamedItem(Info->ServerHeaders,Token,CopyStr(NULL,ptr)); if (StrLen(Token) && StrLen(ptr)) { switch (*Token) { case 'C': case 'c': if (strcasecmp(Token,"Content-length")==0) { Info->ContentLength=atoi(ptr); } else if (strcasecmp(Token,"Content-type")==0) { Info->ContentType=CopyStr(Info->ContentType,ptr); } else if (strcasecmp(Token,"Connection")==0) { if (strcasecmp(ptr,"Close")==0) Info->Flags &= ~HTTP_KEEPALIVE; } else if ((strcasecmp(Token,"Content-Encoding")==0) ) { if (! (Info->Flags & HTTP_NODECODE)) { strlwr(ptr); if ( (strcmp(ptr,"gzip")==0) || (strcmp(ptr,"x-gzip")==0) ) { Info->Flags |= HTTP_GZIP; } if ( (strcmp(ptr,"deflate")==0) ) { Info->Flags |= HTTP_DEFLATE; } } } break; case 'D': case 'd': if (strcasecmp(Token,"Date")==0) Info->Timestamp=CopyStr(Info->Timestamp,ptr); break; case 'L': case 'l': if (strcasecmp(Token,"Location")==0) { if ( (strncasecmp(ptr,"http:",5)==0) || (strncasecmp(ptr,"https:",6)==0) ) { Info->RedirectPath=CopyStr(Info->RedirectPath,ptr); } else Info->RedirectPath=FormatStr(Info->RedirectPath,"http://%s:%d%s",Info->Host,Info->Port,ptr); } break; case 'W': case 'w': if (strcasecmp(Token,"WWW-Authenticate")==0) HTTPHandleWWWAuthenticate(Info,ptr); break; case 'S': case 's': if (strcasecmp(Token,"Set-Cookie")==0) HTTPParseCookie(Info,ptr); else if (strcasecmp(Token,"Status")==0) { //'Status' overrides the response Info->ResponseCode=CopyStrLen(Info->ResponseCode,ptr,3); STREAMSetValue(S,"HTTP:ResponseCode",Info->ResponseCode); } break; case 'T': case 't': if ( (strcasecmp(Token,"Transfer-Encoding")==0) ) { if (! (Info->Flags & HTTP_NODECODE)) { strlwr(ptr); if (strstr(ptr,"chunked")) { Info->Flags |= HTTP_CHUNKED; } } } break; } } DestroyString(Token); DestroyString(Tempstr); }
//Display progress of download void DisplayProgress(char *FullTitle, char *Format, double bytes_read, double DocSize, int PrintName) { double Percent=0, Bps=0, ETAsecs=0; char *HUDocSize=NULL, *BpsStr=NULL, *ETAStr=NULL, *Title=NULL; static time_t SpeedStart=0, LastDisplay=0; static double PrevBytesRead=0; time_t Now; time(&Now); if (bytes_read==0) SpeedStart=Now; if (CheckForKeyboardInput()) PrintName=TRUE; Title=CopyStrLen(Title,FullTitle,30); Title=CatStr(Title,"..."); if (! (Flags & FLAG_QUIET)) { if (PrintName) fprintf(stderr,"\nGetting: %s Size: %s Format: %s\n",Title,GetHumanReadableDataQty(DocSize,0), Format); } if ((Now != SpeedStart) && (Now != LastDisplay)) { BpsStr=CopyStr(BpsStr,""); if (SpeedStart > 0) { Bps=(bytes_read - PrevBytesRead) / (Now-SpeedStart); BpsStr=MCopyStr(BpsStr,GetHumanReadableDataQty(Bps,0),"/s ",NULL); } if (DocSize) { HUDocSize=CopyStr(HUDocSize,GetHumanReadableDataQty(DocSize,0)); Percent=bytes_read * 100.0 / DocSize; if (! (Flags & FLAG_QUIET)) { if (bytes_read > 0) { ETAsecs=(DocSize-bytes_read) / Bps; ETAStr=FormatStr(ETAStr,"%d:%02d",(int) ETAsecs/60, (int) ETAsecs % 60); } else ETAStr=CopyStr(ETAStr,"??:??"); fprintf(stderr," Progress: %0.2f%% %s of %s %s ETA: %s \r",Percent,GetHumanReadableDataQty(bytes_read,0),HUDocSize,BpsStr,ETAStr); } sprintf(CmdLine,"%s %0.2f%% %s \0",ProgName,Percent,Title); if ((PlayerPid==0) && (Percent > PlayerLaunchPercent) && (Player)) LaunchPlayer(); } else { if (! (Flags & FLAG_QUIET)) fprintf(stderr," Progress: %s %s \r",GetHumanReadableDataQty((double) bytes_read,0),BpsStr); sprintf(CmdLine,"%s %s \0",ProgName,Title); } fflush(NULL); if (Now - SpeedStart > 5) { SpeedStart=Now; PrevBytesRead=bytes_read; } LastDisplay=Now; } DestroyString(HUDocSize); DestroyString(BpsStr); DestroyString(Title); }