⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 parseutil.cpp

📁 这是公司移植的UPNP协议
💻 CPP
📖 第 1 页 / 共 5 页
字号:
    tempBuf = hport;}void HostPortValue::toString( xstring& s ){    s += tempBuf;}bool HostPortValue::setHostPort( const char* hostName, unsigned short port ){    xstring s;    char buf[50];    int len;    hostport_type hport;        s = hostName;    sprintf( buf, ":%d", port );    s += buf;        len = parse_hostport( (char *)(s.c_str()), s.length(), &hport );    if ( len < 0 )    {        return false;    }    tempBuf = s;    hostport = hport;    return true;}void HostPortValue::getHostPort( sockaddr_in* addr ){    *addr = hostport.IPv4address;}///////////// UriTypevoid UriType::load( Tokenizer& scanner ){    // uri is composed of identifiers and separators    LoadUri( scanner, uri, tempBuf );}void UriType::toString( xstring& s ){    s += tempBuf;}bool UriType::setUri( const char* newUriValue ){    xstring uriStr;    int len;    uri_type temp;        if ( newUriValue == NULL )    {        return false;    }        uriStr = newUriValue;        len = parse_uri( (char*)(uriStr.c_str()), uriStr.length(),        &temp );    if ( len < 0 )    {        return false;    }        uri = temp;    tempBuf = uriStr;    return true;}const char* UriType::getUri() const{    return tempBuf.c_str();}int UriType::getIPAddress( OUT sockaddr_in& address ){    if ( uri.type != ABSOLUTE )    {        return -1;    }    memcpy( &address, &uri.hostport.IPv4address, sizeof(sockaddr_in) );    return 0;}/* -------------xxxxxxxxxxxxxxxxxxxxxxxx/////////////////// NTSTypevoid NTSType::load( Tokenizer& scanner ){    xstring strVal;    ParseHeaderValue( scanner, strVal );    if ( strVal == "upnp:propchange" )        value = UPNP_PROPCHANGE;    else if ( strVal == "ssdp:alive" )        value = SSDP_ALIVE;    else if ( strVal == "ssdp:byebye" )        value = SSDP_BYEBYE;    else    {        DBG( printf("unknown nts: %s\n", strVal.c_str()); )        throw HttpParseException( "NTSType::load() unknown NTS",            scanner.getLineNum() );    }        }void NTSType::toString( xstring& s ){    if ( value == UPNP_PROPCHANGE )        s += "upnp:propchange";    else if ( value == SSDP_ALIVE )        s += "ssdp:alive";    else if ( value == SSDP_BYEBYE )        s += "ssdp:byebye";    else        assert( 0 );    // invalid NTS}  ----------------xxxxxxxxxxxxxxxxxxxxxxxxx *////////////////////////////////// RawHeaderValuevoid RawHeaderValue::load( Tokenizer& scanner ){    ParseHeaderValue( scanner, value );     }void RawHeaderValue::toString( xstring& s ){    s += value;}////////////////////////////// UnknownHeadervoid UnknownHeader::load( Tokenizer& scanner ){    ParseHeaderName( scanner, name );    SkipColonLWS( scanner );    ParseHeaderValue( scanner, value );}void UnknownHeader::toString( xstring& s ){    s += name;    s += ": ";    s += value;}//////////////////////////////// HttpHeaderHttpHeader::HttpHeader(){    value = NULL;}HttpHeader::~HttpHeader(){    delete value;}void HttpHeader::toString( xstring& s ){    if ( type != HDR_UNKNOWN )    {        const char *name = IDToName( type, HeaderNameTable, NUM_HEADERS );        s += name;        s += ": ";    }    value->toString( s );    s += "\r\n";}////////////////////////////// HttpRequestLine#define NUM_METHODS 8static SortedTableEntry HttpMethodTable[NUM_METHODS] ={    { "GET",        HTTP_GET },    { "HEAD",       HTTP_HEAD },    { "M-POST",     UPNP_MPOST },    { "M-SEARCH",   UPNP_MSEARCH },    { "NOTIFY",     UPNP_NOTIFY },    { "POST",       UPNP_POST },    { "SUBSCRIBE",  UPNP_SUBSCRIBE },    { "UNSUBSCRIBE",UPNP_UNSUBSCRIBE },};void HttpRequestLine::load( Tokenizer& scanner ){    Token* token;    SkipBlankLines( scanner );            // method **********    token = scanner.getToken();    if ( token->tokType != Token::IDENTIFIER )    {        scanner.pushBack();        throw HttpParseException( "HttpRequestLine::load() ident expected",            scanner.getLineNum() );    }    method = (UpnpMethodType) NameToID( token->s.c_str(), HttpMethodTable,        NUM_METHODS );    if ( method == -1 )    {        HttpParseException e( "HttpRequestLine::load() unknown method",            scanner.getLineNum() );        e.setErrorCode( PARSERR_UNKNOWN_METHOD );    }        token = scanner.getToken();    if ( token->tokType != Token::WHITESPACE )    {        scanner.pushBack();        throw HttpParseException( "HttpRequestLine::load() space 1",            scanner.getLineNum() );    }       // url ***********      token = scanner.getToken();    if ( token->s == '*' )    {        pathIsStar = true;      }    else    {        // get url        scanner.pushBack();        uri.load( scanner );        pathIsStar = false;    }        token = scanner.getToken();    if ( token->tokType != Token::WHITESPACE )    {        scanner.pushBack();        throw HttpParseException( "HttpRequestLine::load() space 2",            scanner.getLineNum() );    }        // http version ***********    ParseHttpVersion( scanner, majorVersion, minorVersion );        token = scanner.getToken();    if ( token->tokType == Token::WHITESPACE )    {        token = scanner.getToken();    }        if ( token->tokType != Token::CRLF )    {        scanner.pushBack();        throw HttpParseException( "RequestLine::load() bad data after vers",            scanner.getLineNum() );    }}void HttpRequestLine::toString( xstring& s ){    const char* methodStr = IDToName( method, HttpMethodTable,        NUM_METHODS );            assert( methodStr != NULL );        s += methodStr;    s += ' ';        // print uri    if ( pathIsStar )    {        s += '*';    }    else    {        uri.toString( s );    }    s += ' ';    // print vers    PrintHttpVersion( majorVersion, minorVersion, s );        s += "\r\n";}/////////////////////////////////// HttpResponseLineint HttpResponseLine::setValue( int statCode, int majorVers,    int minorVers ){    int retVal;        statusCode = statCode;    majorVersion = majorVers;    minorVersion = minorVers;        const char *description = http_GetCodeText( statusCode );    if ( description == NULL )    {        reason = "";        retVal = -1;    }    else    {        reason = description;        retVal = 0;    }        return retVal;}void HttpResponseLine::load( Tokenizer& scanner ){    Token* token;        SkipBlankLines( scanner );    ParseHttpVersion( scanner, majorVersion, minorVersion );        token = scanner.getToken();    if ( token->tokType != Token::WHITESPACE )    {        scanner.pushBack();        throw HttpParseException( "HttpResponseLine::load() space 1",            scanner.getLineNum() );    }        statusCode = loadNum( scanner, 10 );    token = scanner.getToken();    if ( token->tokType != Token::WHITESPACE )    {        scanner.pushBack();        throw HttpParseException( "HttpResponseLine::load() space 2",            scanner.getLineNum() );    }    // reason phrase            reason = "";    while ( true )    {        token = scanner.getToken();                if (token->tokType == Token::IDENTIFIER ||            token->tokType == Token::SEPARATOR ||            token->tokType == Token::WHITESPACE            )        {            reason += token->s;        }        else        {            break;        }    }        if ( token->tokType != Token::CRLF )    {        throw HttpParseException( "HttpResponseLine::load() no crlf",            scanner.getLineNum() );    }}void HttpResponseLine::toString( xstring& s ){    PrintHttpVersion( majorVersion, minorVersion, s );    s += ' ';    NumberToString( statusCode, s );    s += ' ';    s += reason;    s += "\r\n";}///////////////////////// HttpEntityHttpEntity::HttpEntity(){    init();    type = TEXT;}HttpEntity::HttpEntity( const char* file_name ){    init();    type = FILENAME;    filename = file_name;}void HttpEntity::init(){    type = TEXT;    entity = NULL;    entitySize = 0;    appendState = IDLE;    sizeInc = 20;    allocLen = 0;}HttpEntity::~HttpEntity(){    if ( appendState == APPENDING )        appendDone();            if ( type == TEXT && entity != NULL )        free( entity );}// throws OutOfMemoryException// numChars = number of chars to read from reader// return 0: OK//        -ve: network error//        return val < numChars : unable to read more data#if 0// *************************************************int HttpEntity::load( CharReader* reader, unsigned numChars ){    char buf[1024];    int numLeft = (int)numChars;    int readSize = 0;    int numRead;        if ( entity != NULL )    {        free( entity );    }        entity = (char *) malloc( 1 );  // 1 char for null terminator    entitySize = 0;        while ( numLeft > 0 )    {        readSize = MinVal<int>( numLeft, sizeof(buf) );        numRead = reader->read( buf, readSize );                if ( numRead < 0 )        {            return numRead;        }        else if ( numRead == 0 )        {            return entitySize;        }        else        {            entity = (char *)realloc( entity, entitySize + numRead );            if ( entity == NULL )            {                throw OutOfMemoryException( "HttpEntity::load()" );            }                        memcpy( &entity[entitySize], buf, numRead );            entitySize += numRead;        }        numLeft -= numRead;    }        entity[ entitySize ] = 0;        return 0;}// **********************XXXXXXXXXXXXXXXXXXXXXX ******* RIP#endifHttpEntity::EntityType HttpEntity::getType() const{    return type;}const char* HttpEntity::getFileName() const{    return filename.c_str();}// throws FileNotFoundException, OutOfMemoryExceptionvoid HttpEntity::appendInit(){    assert( appendState == IDLE );        if ( type == FILENAME )    {        fp = fopen( filename.c_str(), "wb" );        if ( fp == NULL )        {            throw FileNotFoundException( filename.c_str() );        }    }    else if ( type == TEXT )    {        entity = (char *)malloc( 1 );   // one char for null terminator        if ( entity == NULL )        {            throw OutOfMemoryException( "HttpEntity::appendInit()" );        }    }    appendState = APPENDING;    }void HttpEntity::appendDone(){    assert( appendState == APPENDING );        if ( type == FILENAME )    {        fclose( fp );    }    else if ( type == TEXT )    {        if ( entity != NULL )        {            free( entity );        }        entity = NULL;        entitySize = 0;        allocLen = 0;    }        appendState = DONE;}// return codes:// 0 : success// -1: std error; check errno// -2: not enough memory// -3: file write errorint HttpEntity::append( const char* data, unsigned datalen ){    try    {        if ( appendState == IDLE )        {            appendInit();        }            if ( type == FILENAME )        {            size_t numWritten;                        numWritten = fwrite( data, datalen, sizeof(char), fp );            if ( numWritten != sizeof(char) )                throw -3;   // file write error        }        else if ( type == TEXT )        {            increaseSizeBy( datalen );                        memcpy( &entity[entitySize], data, datalen );            entitySize += datalen;   

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -