23 #define NETWORK_BUFFER_SIZE 4096
24 #if NETWORK_BUFFER_SIZE > MAX_INTLENGTH_SHORT
25 #error NETWORK_BUFFER_SIZE exceeds buffered I/O length check size
36 #if defined( __WIN32__ ) && !defined( NDEBUG ) && 0
47 FILE *filePtr = ( FILE * ) stream->callbackFunction;
49 return( fread( buffer, 1, length, filePtr ) );
57 FILE *reportFile = stdout;
62 stream->transportReadFunction = transportFileFunction;
64 if( ( buffer =
clAlloc(
"testHTTP", 16384 ) ) == NULL )
66 puts(
"Out of memory." );
70 reportFile = fopen(
"r:/http_report.txt",
"w" );
71 if( reportFile == NULL )
73 printf(
"Couldn't open file for report, hit a key." );
84 for( i = 0; i <= 3965; i++ )
89 sprintf( fileName,
"d:/tmp/testcases/%08d", i );
90 filePtr = fopen( fileName,
"rb" );
93 printf(
"Failed to open file #%d, hit a key.", i );
98 memcpy( &streamCopy, stream,
sizeof(
STREAM ) );
99 memcpy( &errorInfoCopy, stream->errorInfo,
sizeof(
ERROR_INFO ) );
100 stream->callbackFunction = ( CALLBACKFUNCTION ) filePtr;
101 fprintf( reportFile,
"%04d: ", i );
102 if( reportFile != stdout )
106 printf(
"%04d ", i );
108 status = sread( stream, buffer, 16384 );
113 "%d: cryptlib error: HTTP error not detected.\n",
118 fprintf( reportFile,
"%d %s.\n", status,
119 stream->errorInfo->errorString );
121 fflush( reportFile );
122 memcpy( stream, &streamCopy,
sizeof(
STREAM ) );
123 memcpy( stream->errorInfo, &errorInfoCopy,
sizeof(
ERROR_INFO ) );
125 if( reportFile != stdout )
126 fclose( reportFile );
130 #define testHttp( stream )
156 return( errorStatus );
169 char *proxyUrlBuffer,
178 assert(
isWritePtr( proxyUrlBuffer, proxyUrlMaxLen ) );
179 assert(
isWritePtr( proxyUrlLen,
sizeof(
int ) ) );
186 memset( proxyUrlBuffer, 0,
min( 16, proxyUrlMaxLen ) );
192 if( ( hostLen == 9 && !memcmp( host,
"127.0.0.1", 9 ) ) || \
193 ( hostLen == 3 && !memcmp( host,
"::1", 3 ) ) || \
194 ( hostLen == 9 && !
strCompare( host,
"localhost", 9 ) ) || \
195 ( hostLen == 10 && !
strCompare( host,
"localhost.", 10 ) ) )
219 netStream->nFlags |= \
222 *proxyUrlLen = msgData.
length;
235 *proxyUrlLen = msgData.
length;
255 const char *url = proxyUrl;
259 assert( ( proxyUrl == NULL && proxyUrlLen == 0 ) || \
263 REQUIRES( ( proxyUrl == NULL && proxyUrlLen == 0 ) || \
264 ( proxyUrl != NULL && \
275 return( netStream->transportCheckFunction( netStream ) );
281 if( proxyUrl == NULL )
283 return( netStream->transportConnectFunction( netStream, netStream->host,
293 netStream->host, netStream->hostLen );
300 "Couldn't auto-detect HTTP proxy" ) );
314 "Invalid HTTP proxy URL" ) );
319 return( netStream->transportConnectFunction( netStream, urlInfo.host,
333 static
int initStream(
OUT STREAM *stream,
350 memset( stream, 0,
sizeof(
STREAM ) );
353 netStream->protocol = protocol;
354 netStream->port = connectInfo->port;
355 netStream->netSocket = netStream->listenSocket =
CRYPT_ERROR;
368 timeout = connectInfo->connectTimeout;
385 netStream->timeout = timeout;
389 timeout = connectInfo->timeout;
399 netStream->savedTimeout = timeout;
408 const BOOLEAN cleanupTransport )
416 REQUIRES_V( netStream->sanityCheckFunction( stream ) );
420 netStream->transportDisconnectFunction( netStream,
TRUE );
424 clFree(
"cleanupStream", netStream );
438 static
int processConnectOptions(
INOUT STREAM *stream,
444 const void *
name = connectInfo->name;
449 assert( ( urlInfo == NULL ) || \
459 urlInfo == NULL ) || \
463 connectInfo->name != NULL && urlInfo != NULL ) || \
466 connectInfo->name == NULL && \
467 ( ( connectInfo->
interface == NULL && urlInfo == NULL ) || \
468 ( connectInfo->
interface != NULL && urlInfo != NULL ) ) ) );
471 if( urlInfo != NULL )
472 memset( urlInfo, 0,
sizeof(
URL_INFO ) );
497 connectInfo->name == NULL && \
499 ( connectInfo->name != NULL && \
517 status = parseURL( urlInfo, name, nameLength, connectInfo->
port,
528 "Invalid %s name/URL",
530 "interface" :
"host" ) );
555 int netStreamDataSize = 0, status =
CRYPT_OK;
559 assert( ( urlInfo == NULL ) || \
561 assert( ( proxyUrl == NULL && proxyUrlLen == 0 ) || \
569 ( urlInfo != NULL && \
570 urlInfo->host != NULL && urlInfo->hostLen > 0 ) );
574 REQUIRES_S( ( proxyUrl == NULL && proxyUrlLen == 0 ) || \
575 ( proxyUrl != NULL && \
587 setAccessMethodTCP( netStreamTemplate );
592 setStreamLayerHTTP( netStreamTemplate );
599 #ifdef USE_CMP_TRANSPORT
600 setStreamLayerCMP( netStreamTemplate );
607 setStreamLayerDirect( netStreamTemplate );
613 setStreamLayerBuffering( netStreamTemplate, useTransportBuffering );
615 ENSURES( netStreamTemplate->sanityCheckFunction != NULL );
616 ENSURES( netStreamTemplate->writeFunction != NULL && \
617 netStreamTemplate->readFunction != NULL );
618 ENSURES( netStreamTemplate->transportConnectFunction != NULL && \
619 netStreamTemplate->transportDisconnectFunction != NULL );
620 ENSURES( netStreamTemplate->transportReadFunction != NULL && \
621 netStreamTemplate->transportWriteFunction != NULL );
622 ENSURES( netStreamTemplate->transportOKFunction != NULL && \
623 netStreamTemplate->transportCheckFunction != NULL );
624 ENSURES( netStreamTemplate->bufferedTransportReadFunction != NULL && \
625 netStreamTemplate->bufferedTransportWriteFunction != NULL );
627 ( urlInfo != NULL && \
628 urlInfo->host != NULL && urlInfo->hostLen != 0 ) || \
637 static const int fixedTimeout = 30;
674 !netStreamTemplate->transportOKFunction() )
680 "Networking subsystem not available" ) );
684 if( useTransportBuffering )
686 if( urlInfo != NULL )
687 netStreamDataSize += urlInfo->hostLen + urlInfo->locationLen;
690 if( netStreamData == NULL )
699 if( useTransportBuffering || urlInfo != NULL )
702 BYTE *netStreamDataPtr = (
BYTE * ) netStreamData + \
705 netStreamInfo->storageSize = netStreamDataSize;
706 if( useTransportBuffering )
708 stream->buffer = netStreamDataPtr;
710 netStreamInfo->writeBuffer = netStreamDataPtr + \
715 if( urlInfo != NULL )
717 netStreamInfo->host = (
char * ) netStreamDataPtr;
718 memcpy( netStreamInfo->host, urlInfo->host, urlInfo->hostLen );
719 netStreamInfo->hostLen = urlInfo->hostLen;
720 if( urlInfo->location != NULL )
722 netStreamInfo->path = (
char * ) netStreamDataPtr + \
724 memcpy( netStreamInfo->path, urlInfo->location,
725 urlInfo->locationLen );
726 netStreamInfo->pathLen = urlInfo->locationLen;
728 netStreamInfo->port = urlInfo->port;
731 stream->netStreamInfo = netStreamData;
734 status = openNetworkConnection( stream->netStreamInfo, options,
735 proxyUrl, proxyUrlLen );
747 cleanupStream( stream,
FALSE );
752 if( proxyUrl == NULL )
757 status = connectViaHttpProxy( stream, errorInfo );
769 cleanupStream( stream,
FALSE );
773 cleanupStream( stream,
FALSE );
776 "HTTP proxy support not available" ) );
794 URL_INFO urlInfo, *urlInfoPtr = NULL;
795 char proxyUrlBuffer[
MAX_DNS_SIZE + 8 ], *proxyURL = NULL;
796 int proxyUrlLen = 0,
status;
820 connectInfo->name != NULL && \
827 connectInfo->name == NULL && connectInfo->
nameLength == 0 && \
835 connectInfo->name == NULL && connectInfo->
nameLength == 0 && \
846 status = initStream( stream, &netStream, protocol, connectInfo,
FALSE );
851 urlInfoPtr = &urlInfo;
852 status = processConnectOptions( stream, &netStream, urlInfoPtr,
853 connectInfo, errorInfo );
865 status = checkForProxy( &netStream, protocol, connectInfo,
866 urlInfoPtr->host, urlInfoPtr->
hostLen,
876 proxyURL = proxyUrlBuffer;
877 proxyUrlLen = proxyUrlLength;
882 return( completeConnect( stream, &netStream, urlInfoPtr, protocol,
883 connectInfo->
options, proxyURL, proxyUrlLen,
894 URL_INFO urlInfo, *urlInfoPtr = NULL;
931 status = initStream( stream, &netStream, protocol, connectInfo,
TRUE );
936 urlInfoPtr = &urlInfo;
937 status = processConnectOptions( stream, &netStream, urlInfoPtr,
938 connectInfo, errorInfo );
943 return( completeConnect( stream, &netStream, urlInfoPtr, protocol,
957 REQUIRES_S( netStream->sanityCheckFunction( stream ) );
959 cleanupStream( stream,
TRUE );
978 return( parseURL( urlInfo, url, urlLen,
CRYPT_UNUSED, urlTypeHint,
993 REQUIRES_V( netStream->sanityCheckFunction( stream ) );
1002 ( void ) getSessionErrorInfo( netStream,
CRYPT_OK );
1023 memset( stream, 0,
sizeof(
STREAM ) );
1024 memset( errorInfo, 0,
sizeof(
ERROR_INFO ) );
1034 memset( stream, 0,
sizeof(
STREAM ) );
1035 memset( errorInfo, 0,
sizeof(
ERROR_INFO ) );
1053 memset( urlInfo, 0,
sizeof(
URL_INFO ) );
1063 memset( errorInfo, 0,
sizeof(
ERROR_INFO ) );