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

📄 server.cpp

📁 这是公司移植的UPNP协议
💻 CPP
📖 第 1 页 / 共 3 页
字号:
    }    aliasOut = resourceName;    try    {        HttpEntity* entity;        xstring contentType;        xstring contentSubtype;        time_t lastModified;        gAliasList.grabEntity( resourceName.c_str(), &entity,            contentType, contentSubtype, lastModified );        assert( entity != NULL );        assert( entity->type == HttpEntity::TEXT );        response.isRequest = false;        //response.addContentLengthHeader( entity->getEntityLen() );        response.addNumTypeHeader( HDR_CONTENT_LENGTH,            entity->getEntityLen() );        response.addContentTypeHeader( contentType.c_str(),            contentSubtype.c_str() );        //response.addLastModifiedHeader( lastModified );        response.addDateTypeHeader( HDR_LAST_MODIFIED, lastModified );        if ( request.requestLine.method == HTTP_GET )        {            response.entity.setTextPtrEntity( entity->getEntity(),                entity->getEntityLen() );        }        else        {            response.entity.type = HttpEntity::EMPTY;        }    }    catch ( AliasedListException& /* e */ )    {        retCode = -1;    }    return retCode;}////////////////////////////////////////////// throws OutOfMemoryExceptionstatic void AddContentType( IN const char* fileName,    OUT HttpMessage& response ){    char *extPtr;    char *type = NULL;    char *subtype = NULL;    int index;    // find last dot    bool found = false;            extPtr = strrchr( fileName, '.' );    if ( extPtr != NULL )    {        extPtr++;   // skip dot                index = SearchExtension( extPtr, MediaTypeList, NUM_MEDIA_TYPES );        if ( index != -1 )        {            type = MediaTypeList[index].type;            subtype = MediaTypeList[index].subtype;            found = true;        }    }            if ( !found )    {        type = ApplicationStr;        subtype = "octet-stream";    }            response.addContentTypeHeader( type, subtype );}////////////////////////////////////////////// throws HttpServerException//    HTTP_HTTP_VERSION_NOT_SUPPORTEDstatic void GetReplyVersion( IN HttpRequestLine& reqLine,    OUT int& majorVersion, OUT int& minorVersion ){    int reqMajor, reqMinor;    int respMajor = 1, respMinor = 1;    reqMajor = reqLine.majorVersion;    reqMinor = reqLine.minorVersion;    if ( reqMajor == 0 )    {        respMajor = reqMajor;        if ( reqMinor != 9 )        {            HttpServerException e("can't handle http version" );            e.setErrorCode( HTTP_HTTP_VERSION_NOT_SUPPORTED );            throw e;        }        respMinor = reqMinor;    }    else if ( reqMajor == 1 )    {        respMajor = reqMajor;        if ( reqMinor == 0 )        {            respMinor = reqMinor;        }        else        {            respMinor = 1;        }    }    else    {        respMajor = 1;        respMinor = 1;    }        majorVersion = respMajor;    minorVersion = respMinor;}////////////////////////////////////////////void free_alias( IN bool usingAlias, IN const char* alias ){    if ( !usingAlias )    {        return;    }    try    {        gAliasList.releaseEntity( alias );    }    catch ( AliasedListException& /* e */ )    {        assert( 0 );    // aliased entity not found    }}////////////////////////////////////////////// processes GET and HEAD methods// throws HttpServerException//    HTTP_BAD_REQUEST//    HTTP_NOT_FOUND//    HTTP_INTERNAL_SERVER_ERROR//    HTTP_NOT_IMPLEMENTED//    HTTP_HTTP_VERSION_NOT_SUPPORTEDstatic void ProcessRequest( IN HttpMessage& request,    OUT HttpMessage& response, OUT bool& usingAlias,    OUT xstring& aliasOut ){    int method;    int respMajor, respMinor;    usingAlias = false;        try    {        // check method        method = request.requestLine.method;        if ( !(method == HTTP_GET || method == HTTP_HEAD) )        {            throw HTTP_NOT_IMPLEMENTED; // unknown method        }                GetReplyVersion( request.requestLine, respMajor, respMinor );        xstring filename;        int fileLength;        time_t lastModified;        usingAlias = true;        if ( TryToGetAlias(request, response, aliasOut) != 0 )        {            // alias not found -- try files            usingAlias = false;            GetFilename( request, filename, fileLength, lastModified );            AddContentType( filename.c_str(), response );                        // add std response headers                    // content-length            //response.addContentLengthHeader( fileLength );            response.addNumTypeHeader( HDR_CONTENT_LENGTH, fileLength );            // last modified            //response.addLastModifiedHeader( lastModified );            response.addDateTypeHeader( HDR_LAST_MODIFIED, lastModified );            if ( method == HTTP_GET )            {                // set http body to be sent                response.entity.type = HttpEntity::FILENAME;                response.entity.filename = filename;            }            else            {                response.entity.type = HttpEntity::EMPTY;            }        }        // date        //response.addDateHeader();        response.addDateTypeHeader( HDR_DATE, time(NULL) );                // server        response.addServerHeader();        // connection close        const char *idents[] = {"close"};        response.addIdentListHeader( HDR_CONNECTION, idents, 1 );        response.responseLine.setValue( HTTP_OK, respMajor, respMinor );        response.isRequest = false;    }    catch ( int errCode )    {        HttpServerException e;        e.setErrorCode( errCode );        throw e;    }    DBG(        UpnpPrintf( UPNP_INFO, MSERV, __FILE__, __LINE__,            "ProcessRequest(): done\n"); )}////////////////////////////////////////////static void HandleError( IN HttpMessage& request, IN int errCode,    IN int sockfd ){    HttpMessage response;    int respMajor, respMinor;        response.isRequest = false;    // if request is invalid, don't use it to get version number    if ( errCode != HTTP_HTTP_VERSION_NOT_SUPPORTED &&         errCode != HTTP_BAD_REQUEST                &&         errCode != HTTP_INTERNAL_SERVER_ERROR       )    {        GetReplyVersion( request.requestLine, respMajor, respMinor );    }    else    {        respMajor = 1;        respMinor = 1;    }        response.responseLine.setValue( errCode, respMajor, respMinor );    if ( errCode == HTTP_HTTP_VERSION_NOT_SUPPORTED )    {        char *reason = "<html><body><h2>Supports HTTP versions 0.9, 1.0 and 1.1</h2></body></html>";        response.entity.append( reason, strlen(reason) );        response.entity.type = HttpEntity::TEXT;        response.addNumTypeHeader( HDR_CONTENT_LENGTH, strlen(reason) );        response.addContentTypeHeader( "text", "html" );    }    else    {        xstring htmlDoc;        xstring numStr;        IntToStr( response.responseLine.statusCode, numStr );        htmlDoc = "<html><body><h1>";        htmlDoc += numStr;        htmlDoc += ' ';        htmlDoc += response.responseLine.reason;        htmlDoc += "</h1></body></html>";        response.addNumTypeHeader( HDR_CONTENT_LENGTH, htmlDoc.length() );        response.entity.type = HttpEntity::TEXT;        response.entity.append( htmlDoc.c_str(), htmlDoc.length() );    }    response.addDateTypeHeader( HDR_DATE, time(NULL) );    response.addServerHeader();    http_SendMessage( sockfd, response );}////////////////////////////////////////////int http_ServerCallback( IN HttpMessage& request, int sockfd ){    HttpMessage response;    bool usingAlias = false;    xstring alias;        try    {        if ( gServerActive == false )        {            HttpServerException e("HTTP GET Server Inactive");            e.setErrorCode( HTTP_INTERNAL_SERVER_ERROR );            throw e;        }        ProcessRequest( request, response, usingAlias, alias );                http_SendMessage( sockfd, response );    }    catch ( HttpServerException& e )    {        //DBG( e.print(); )        DBG(            UpnpPrintf( UPNP_INFO, MSERV, __FILE__, __LINE__,                "http status: %d\n", e.getErrorCode()); )                HandleError( request, e.getErrorCode(), sockfd );    }    catch ( ... )    {    }    close( sockfd );    free_alias( usingAlias, alias.c_str() );    return 0;}////////////////////////////////////////////void http_OldServerCallback( IN const char* msg, int sockfd ){    HttpMessage request;    int code;    code = request.loadRequest( msg );    if ( code < 0 )    {        if ( code == HTTP_E_BAD_MSG_FORMAT )        {            HandleError( request, HTTP_BAD_REQUEST, sockfd );        }        else if ( code == HTTP_E_OUT_OF_MEMORY )        {            HandleError( request, HTTP_INTERNAL_SERVER_ERROR, sockfd );        }        close( sockfd );        return;    }    http_ServerCallback( request, sockfd );}////////////////////////////////////////////// returns://    0//    HTTP_E_OUT_OF_MEMORYint http_AddAlias( IN const char* aliasRelURL, IN HttpEntity* entity,    OUT xstring& actualAlias ){    int retCode;    assert( aliasRelURL != NULL );    assert( entity != NULL );    try    {        gAliasList.addEntity( aliasRelURL, entity, actualAlias );        retCode = 0;    }    catch ( OutOfMemoryException& /* e */ )    {        retCode = HTTP_E_OUT_OF_MEMORY;    }    return retCode;}////////////////////////////////////////////int http_RemoveAlias( IN const char* alias ){    int retCode;    try    {        gAliasList.releaseEntity( alias, true );        retCode = 0;        // removed    }    catch ( AliasedListException& /* e */ )    {        retCode = -1;       // not found    }    return retCode;}#endif#endif

⌨️ 快捷键说明

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