Example #1
0
// Read the OpenWith file
void startup_read_openwith(void)
{
	APTR file;

	// Lock list
	lock_listlock(&GUI->open_with_list,1);

	// Open file
	if (file=OpenBuf("DOpus5:System/OpenWith",MODE_OLDFILE,4000))
	{
		char line[256];
		short len;

		// Read lines
		while ((len=ReadBufLine(file,line,255))>0)
		{
			struct Node *node;

			// Allocate node
			if (node=AllocMemH(global_memory_pool,sizeof(struct Node)+len+1))
			{
				// Initialise node and copy string
				node->ln_Name=(char *)(node+1);
				strcpy(node->ln_Name,line);

				// Add to Open With list
				AddTail((struct List *)&GUI->open_with_list,node);
			}
		}

		// Close file
		CloseBuf(file);
	}

	// Unlock list
	unlock_listlock(&GUI->open_with_list);
}
Example #2
0
int HttpDownload( const char *pHost, int nPort, const char *pFile, char **pBuffer, int *pSize, int (*callback)(int, int) )
{
	int					s;
	struct sockaddr_in	addr;
	int					nRet, nRecv, nReqOK, nContentSize, nBreak, nRead;
	int					nTotal;

	char				strGet[256];
	char				strBuf[4096];
	char				strLine[1024];
	char				*pStr, *pLine;

	s = socket( AF_INET, SOCK_STREAM, IPPROTO_TCP );
	if( s < 0 ) {
#ifdef _DEBUG
		printf("HttpDownload: socket() failed!\n");
#endif
		return HTTP_ERROR_SOCKET;
	}

	memset( &addr, 0, sizeof(addr) );

	addr.sin_family			= AF_INET;
	addr.sin_port			= htons(nPort);
	addr.sin_addr.s_addr	= inet_addr( pHost );

	// not an ip address, try to resolve the hostname
	if( addr.sin_addr.s_addr == INADDR_NONE ) {
		if( gethostbyname( (char*) pHost, &addr.sin_addr ) != 0 ) {
#ifdef _DEBUG
			printf("HttpDownload: Could not resolve hostname : %s\n", pHost);
#endif
			return HTTP_ERROR_RESOLVE;
		}
	}

	nRet = connect( s, (struct sockaddr*)&addr, sizeof(addr) );
	if( nRet < 0 ) {
#ifdef _DEBUG
		printf("HttpDownload: connect() failed!\n");
#endif
		disconnect(s);
		return HTTP_ERROR_CONNECT;
	}

	// prepare GET request
	sprintf( strGet,"GET %s HTTP/1.0\r\n\r\n", pFile );

	nRet = send( s, strGet, strlen(strGet), 0 );
	if( nRet < 0 ) {
#ifdef _DEBUG
		printf("HttpDownload: send() failed!\n");
#endif
		disconnect(s);
		return HTTP_ERROR_SEND;
	}

	// read HTTP header response
	nRecv			= 0;
	nReqOK			= 0;
	nContentSize	= 0;
	nBreak			= 0;

	// read header in 1024 chunks
	while(1) {
		nRet = recv( s, strBuf + nRecv, sizeof(strBuf) - 1 - nRecv, 0 );

		if( nRet < 0 || nRet == 0 ) {
#ifdef _DEBUG
			printf("HttpDownload: connection was reset!\n");
#endif
			disconnect(s);
			return HTTP_ERROR_RECV;
		}

		strBuf[nRet] = 0;
		pStr = strBuf;

		while(1) {
			nRead = ReadBufLine( &pStr, strLine );

			if( nRead == 0 ) {
				memcpy( strBuf, pStr, pStr - strBuf );
				nRecv = pStr - strBuf;
				break;
			}

			// end of HTTP header
			if( strLine[0] == '\r' && strLine[1] == '\n' ) {

				// remaining bytes are of file already
				nRecv = nRet - (pStr - strBuf);
				memcpy( strBuf, pStr, nRecv );

				// break out of loop
				nBreak = 1;
				break;
			}

			// check for HTTP error code...make this properly?
			if( strstr( strLine, "HTTP/1." ) ) {
				if( strstr( strLine, "200 OK" ) )
					nReqOK = 1;
			}
			else if( strstr( strLine, "Content-Length:" ) ) {

				// this is the file size in bytes
				pLine = strLine;
				pLine += strlen("Content-Length:");

				// skip whitespaces
				while( *pLine == ' ' || *pLine == '\t' )
					pLine++;

				nContentSize = atoi(pLine);
			}
		}

		// HTTP header end reached
		if( nBreak )
			break;
	}

	// file doesn't exist or some other HTTP error occured
	if( !nReqOK ) {
#ifdef _DEBUG
		printf("HttpDownload: nReqOK != 200 OK\n");
#endif
		disconnect(s);
		return HTTP_ERROR_BADREQ;
	}

	if( !nContentSize ) {
#ifdef _DEBUG
		printf("HttpDownload: (Notice) HTTP header did not contain Content-Length field!\n");
#endif
	}

	// check if it's not too big
	if( nContentSize > HTTP_MAX_FILESIZE ) {
#ifdef _DEBUG
		printf("HttpDownload: file size is too big (%i)!\n", nContentSize);
#endif
		disconnect(s);
		return HTTP_ERROR_SIZE;
	}

	*pBuffer	= NULL;
	nTotal		= 0;

	// nRecv is the number of bytes already belonging to the file that
	// were in the last strBuf chunk of the HTTP header.
	if( nRecv > 0 ) {
		*pBuffer = (char*) realloc( *pBuffer, nRecv );
		if( !(*pBuffer) ) {
#ifdef _DEBUG
			printf("HttpDownload: realloc failed for pBuffer!\n");
#endif
			disconnect(s);
			return HTTP_ERROR_MEMORY;
		}
		memcpy( *pBuffer, strBuf, nRecv );
		nTotal = nRecv;
	}

	// can start receiving the file now
	while( (nRet = recv( s, strBuf, sizeof(strBuf), 0 )) ) {

		if( nRet < 0 ) {
			printf("HttpDownload: recv() failed\n");

			free(*pBuffer);
			disconnect(s);
			return HTTP_ERROR_RECV;
		}

		// don't let the file get too big
		if( (nTotal + nRet) > HTTP_MAX_FILESIZE ) {
#ifdef _DEBUG
			printf("HttpDownload: file size exceeded HTTP_MAX_FILESIZE!\n");
#endif
			free(*pBuffer);
			disconnect(s);
			return HTTP_ERROR_SIZE;
		}

		*pBuffer = (char*) realloc( *pBuffer, nTotal + nRet );
		if( !(*pBuffer) ) {
#ifdef _DEBUG
			printf("HttpDownload: realloc failed for pBuffer!\n");
#endif
			disconnect(s);
			return HTTP_ERROR_MEMORY;
		}
		
		memcpy( (*pBuffer) + nTotal, strBuf, nRet );

		// total count of bytes received
		nTotal += nRet;

		// if a callback was specified call it
		if( callback )
			callback( nTotal, nContentSize );
	}

#ifdef _DEBUG
	if( (nContentSize > 0) && (nTotal == nContentSize) )
		printf("HttpDownload : This is good\n");
#endif

	*pSize = nTotal;

	disconnect(s);
	return 1;
}