extern size_t RequestReadBLOB( NETFILE *fp, MonObjectType obj, unsigned char **ret, size_t *size) { unsigned char *buff; size_t red; ENTER_FUNC; red = 0; *ret = NULL; RequestBLOB(fp,BLOB_READ); ON_IO_ERROR(fp,badio); SendObject(fp,obj); ON_IO_ERROR(fp,badio); if ( RecvPacketClass(fp) == BLOB_OK ) { *size = RecvLength(fp); ON_IO_ERROR(fp,badio); if ( *size > 0 ) { buff = xmalloc(*size); red = Recv(fp,buff,*size); *ret = buff; } } badio: LEAVE_FUNC; return (red); }
//Wait and get something from socket until separator bool SocketHandler::GetLine( string &lineT, string separator, int timeout ) { lineT = ""; string TempLine; string::size_type Position; do { if ( Recv( TempLine, false, timeout ) == false ) { return false; } } while ( (Position = TempLine.find( separator )) == string::npos ); TempLine = ""; if ( RecvLength( TempLine, Position + separator.size() ) == false ) { return false; } lineT = TempLine.erase( Position ); return true; }
extern void RecvString(NETFILE *fp, char *str) { size_t size; size = RecvLength(fp); Recv(fp, str, size); str[size] = 0; }
extern void RecvLBS(NETFILE *fp, LargeByteString *lbs) { size_t size; size = RecvLength(fp); LBS_ReserveSize(lbs, size, FALSE); if (size > 0) { Recv(fp, LBS_Body(lbs), size); } }
extern char *RecvStringNew(NETFILE *fp) { size_t size; char *ret; size = RecvLength(fp); ret = xmalloc(size + 1); Recv(fp, ret, size); ret[size] = 0; return ret; }
extern void RecvnString(NETFILE *fp, size_t size, char *str) { size_t lsize; lsize = RecvLength(fp); if (size >= lsize) { size = lsize; Recv(fp, str, size); str[size] = 0; } else { CloseNet(fp); Message("error size mismatch !"); } }
extern size_t RequestWriteBLOB( NETFILE *fp, MonObjectType obj, unsigned char *buff, size_t size) { size_t wrote; ENTER_FUNC; wrote = 0; RequestBLOB(fp,BLOB_WRITE); ON_IO_ERROR(fp,badio); SendObject(fp,obj); ON_IO_ERROR(fp,badio); if ( RecvPacketClass(fp) == BLOB_OK ) { SendLength(fp,size); ON_IO_ERROR(fp,badio); if ( size > 0 ) { Send(fp,buff,size); ON_IO_ERROR(fp,badio); wrote = RecvLength(fp); ON_IO_ERROR(fp,badio); } } badio: LEAVE_FUNC; return (wrote); }
extern Bool RequestExportBLOB( NETFILE *fp, MonObjectType obj, char *fname) { Bool rc; char buff[SIZE_BUFF]; FILE *fpf; size_t size , left; ENTER_FUNC; rc = FALSE; RequestBLOB(fp,BLOB_EXPORT); ON_IO_ERROR(fp,badio); SendObject(fp,obj); ON_IO_ERROR(fp,badio); if ( RecvPacketClass(fp) == BLOB_OK ) { if ( ( fpf = Fopen(fname,"w") ) != NULL ) { fchmod(fileno(fpf),0600); left = RecvLength(fp); while ( left > 0 ) { size = ( left > SIZE_BUFF ) ? SIZE_BUFF : left; Recv(fp,buff,size); ON_IO_ERROR(fp,badio); fwrite(buff,size,1,fpf); left -= size; } fclose(fpf); rc = TRUE; } else { Warning("could not open for write: %s", fname); } } badio: LEAVE_FUNC; return (rc); }
//Read part of Body ssize_t HTTPHandler::ReadBodyPart( string &bodyT, bool Chunked ) { bodyT = ""; ssize_t count; if ( Chunked == false ) { if ( (count = SocketHandler::Recv( bodyT, true, -1 )) < 0) { return -1; } return count; } // Handle Chunked encoding, return one chunk string Temp = ""; int received = 0; int read; unsigned int csize; string::size_type position, hpos; // Read header for(;;) { read = SocketHandler::Recv( Temp, false, -1 ); if ( read < 1 ) return -1; if ( (position = Temp.find("\r\n")) != string::npos ) { if ( position == 0 ) { SocketHandler::RecvLength( Temp, 2 ); Temp = ""; continue; } string UTemp = UpperCase(Temp); hpos = UTemp.find_first_not_of("0123456789ABCDEF"); // Read max 8 hex = 4gb if ( hpos == string::npos || hpos < 1 || hpos > 7 ) { LogFile::ErrorMessage("Invalid Chunked-header received\n"); return -1; } if ( sscanf( UTemp.substr(0, hpos).c_str(), "%x", &csize ) != 1 ) { LogFile::ErrorMessage("Invalid Chunked-header received\n"); return -1; } SocketHandler::RecvLength( Temp, position + 2 ); break; } received += read; if ( received > 256 ) { LogFile::ErrorMessage("Too large Chunked-header received (>256)\n"); return -1; } } // All chunks received? if ( csize == 0 ) return 0; // Does someone send over 1MB chunks? if ( csize > 1048576 ) { LogFile::ErrorMessage("Too large Chunked-body size received (>1MB)\n"); return -1; } if ( RecvLength( bodyT, csize ) == false ) return -1; return csize; }