示例#1
0
char *join( char **inStrings, int inNumParts, char *inGlue ) {
    StringBufferOutputStream *outStream = new StringBufferOutputStream();

    for( int i=0; i<inNumParts - 1; i++ ) {
        outStream->writeString( inStrings[i] );
        outStream->writeString( inGlue );
        }
    // no glue after last string
    outStream->writeString( inStrings[ inNumParts - 1 ] );

    char *returnString = outStream->getString();

    delete outStream;

    return returnString;
    }
示例#2
0
WebRequest::WebRequest( const char *inMethod, const char *inURL,
                        const char *inBody, const char *inProxy )
        : mError( false ), mURL( stringDuplicate( inURL ) ),
          mRequest( NULL ), mRequestPosition( -1 ),
          mResultReady( false ), mResult( NULL ),
          mSock( NULL ) {
        
    
    
    const char *startString = "http://";

    char *urlCopy = stringDuplicate( inURL );

    
    char *urlStart = stringLocateIgnoreCase(  urlCopy, startString );

    char *serverStart;
    
    if( urlStart == NULL ) {
        // no http:// at start of URL
        serverStart = urlCopy;
        }
    else {
        serverStart = &( urlStart[ strlen( startString ) ] );
        }
    
    // find the first occurrence of "/", which is the end of the
    // server name

    char *serverNameCopy;
    char *requestHostNameCopy = stringDuplicate( serverStart );
    
    if( inProxy != NULL ) {
        serverNameCopy = stringDuplicate( inProxy );
        }
    else {
        // will be same as parsed, request host name copy later 
        serverNameCopy = NULL;
        }
    
    

    char *getPath;

    if( inProxy != NULL ) {
        // for proxy, pass entire URL as method target
        getPath = (char*)inURL;
        }
    else {
        // for direct connection, pass only file sub-path from URL
        getPath = strstr( serverStart, "/" );
        }
    
    
    char *hostNameEnd = strstr( requestHostNameCopy, "/" );
    if( hostNameEnd == NULL ) {
        hostNameEnd = &( requestHostNameCopy[ strlen( requestHostNameCopy ) ] );
        
        if( inProxy == NULL ) {
            getPath = (char *)"/";
            }
        }

    // terminate the url here to extract the host name and port
    hostNameEnd[0] = '\0';

    if( inProxy == NULL ) {
        serverNameCopy = stringDuplicate( requestHostNameCopy );
        }

    
    int portNumber = 80;

    // look for a port number
    char *colon = strstr( serverNameCopy, ":" );
    if( colon != NULL ) {
        char *portNumberString = &( colon[1] );
                
        int numRead = sscanf( portNumberString, "%d",
                              & portNumber );
        if( numRead != 1 ) {
            portNumber = 80;
            }

        // terminate the name here so port isn't taken as part
        // of the address
        colon[0] = '\0';
        }

    mSuppliedAddress = new HostAddress(
        stringDuplicate( serverNameCopy ),
        portNumber );

    mNumericalAddress = NULL;



    mLookupThread = NULL;
    

    
    // launch right into name lookup
    mLookupThread = new LookupThread( mSuppliedAddress );
    

    mSock = NULL;
    
        
    // compose the request into a buffered stream
    StringBufferOutputStream tempStream;

    tempStream.writeString( inMethod );
    tempStream.writeString( " " );
    tempStream.writeString( getPath );
    tempStream.writeString( " HTTP/1.0\r\n" );
    tempStream.writeString( "Host: " );
    tempStream.writeString( requestHostNameCopy );
    tempStream.writeString( "\r\n" );
        
    if( inBody != NULL ) {
        char *lengthString = autoSprintf( "Content-Length: %d\r\n",
                                          strlen( inBody ) );
        tempStream.writeString( lengthString );
        delete [] lengthString;
        tempStream.writeString(
            "Content-Type: application/x-www-form-urlencoded\r\n\r\n" );
            
        tempStream.writeString( inBody );
        }
    else {
        tempStream.writeString( "\r\n" );
        }
        
    mRequest = tempStream.getString();
    mRequestPosition = 0;

        
        
    delete [] serverNameCopy;
    delete [] requestHostNameCopy;
    
    delete [] urlCopy;    
    }