Beispiel #1
0
PEGASUS_NAMESPACE_BEGIN

/**

    Encapsulates the XML request in an HTTP M-POST or POST request message.
    Generates the appropriate HTTP extension headers corresponding to the
    XML request, in accordance with Specifications for CIM Operations over
    HTTP, Version 1.0, Section 3.  This method should be called only when
    the current parser location is the xml declaration.  If the xml
    declaration is not found, but the first token in the input is
    HTTP_METHOD_MPOST or HTTP_METHOD_POST, it is assumed that the request is
    already encapsulated in an HTTP request, and the message is returned
    unchanged.  No attempt is made to validate the complete HTTP request.
    If the useMPost parameter is TRUE, the headers are generated for an
    M-POST request.  Otherwise, the headers are generated for a POST
    request.  If the useHTTP11 parameter is TRUE, the headers are generated
    for an HTTP/1.1 request.  Otherwise, the headers are generated for an
    HTTP/1.0 request.  The combination of useMPost true and useHTTP11 false
    is invalid, but this function does not check for this case.  The XML
    request is examined only as much as necessary to generate the required
    headers.  This method does not attempt to validate the entire XML request.

    @param   parser              XmlParser instance corresponding to the
                                 XML request
    @param   hostName            host name to be used in HTTP Host header
    @param   useMPost            Boolean indicating that headers should be
                                 generated for an M-POST request
    @param   useHTTP11           Boolean indicating that headers should be
                                 generated for an HTTP/1.1 request
    @param   content             Buffer containing XML request
    @param   httpHeaders         Buffer returning the HTTP headers

    @return  Buffer containing the XML request encapsulated in an
             HTTP request message

    @exception  XmlValidationError  if the XML input is invalid
    @exception  XmlSemanticError    if the XML input contains a semantic error
    @exception  WbemExecException   if the input contains neither XML nor HTTP
                                    M-POST or POST method request

 */
Buffer XMLProcess::encapsulate( XmlParser& parser,
                                       const String& hostName,
                                       Boolean useMPost,
                                       Boolean useHTTP11,
                                       Buffer& content,
                                       Buffer& httpHeaders
                                       )
throw (XmlValidationError, XmlSemanticError, WbemExecException,
               XmlException, Exception)
{
    XmlEntry                     entry;
    Buffer                    message;
    String                       messageId;
    const char*                  cimVersion            = 0;
    const char*                  dtdVersion            = 0;
    String                       protocolVersion;
    CIMName                      className;
    CIMName                      methodName;
    CIMObjectPath                objectName;
    Buffer                    encoded;
    String                    objPath;
    Array<CIMKeyBinding>         keyBindings;
    Boolean                      multireq              = false;
    static Uint32                BUFFERSIZE            = 1024;
    Boolean hasXmlDeclaration = false;

    //
    //  xml declaration
    //
    if (!(hasXmlDeclaration = XmlReader::testXmlDeclaration (parser, entry)))
    {
        //
        //  No xml declaration
        //  Request may already be encapsulated in HTTP
        //  Check for HTTP method
        //
        char tmp [8];
        char* tmpp = & (tmp [0]);
        strncpy (tmpp, entry.text, 8);
        tmpp[7] = 0;
#if !defined(PEGASUS_COMPILER_MSVC) && !defined(PEGASUS_OS_ZOS)
        char *last;
        char* p = strtok_r (tmpp, HTTP_SP, &last);
#else
        char* p = strtok (tmpp, HTTP_SP);
#endif
        if (p != NULL)
        {
            if ((strcmp (p, HTTP_METHOD_MPOST) == 0) ||
                (strcmp (p, HTTP_METHOD_POST) == 0) ||
            //  This is a special request used for testing.
            //  It includes the HTTP header.
            //  Return the message as is.
                (strcmp (p, HTTP_METHOD_BOGUS) == 0))
            {
                return content;
            }
        }
     }

#ifdef PEGASUS_ENABLE_PROTOCOL_WSMAN

     // Check if the next tag is a SOAP envelope.
     if (parser.next(entry) &&
             entry.type == XmlEntry::START_TAG &&
             strcmp(entry.localName, "Envelope") == 0)
     {
          //
          //  Set WSMAN headers and content.
          //
          message << HTTP_METHOD_POST << HTTP_SP
                  << "/wsman" << HTTP_SP
                  << HTTP_PROTOCOL << HTTP_VERSION_11 << HTTP_CRLF;
          message << HEADER_NAME_HOST << HEADER_SEPARATOR
                  << HTTP_SP << hostName << HTTP_CRLF;
          message << HEADER_NAME_CONTENTTYPE << HEADER_SEPARATOR
                  << HTTP_SP << WSMAN_HEADER_VALUE_CONTENTTYPE
                  << HTTP_CRLF;
          message << HEADER_NAME_CONTENTLENGTH << HEADER_SEPARATOR
                  << HTTP_SP << (Uint32)content.size () << HTTP_CRLF;

          httpHeaders << message;
          message << HTTP_CRLF;
          message << content;
          return message;
     }
     else
     {
         parser.putBack(entry);
     }

#endif

    if (!hasXmlDeclaration)
    {
        //
        //  Input contains neither XML declaration nor HTTP M-POST or
        //  POST method request
        //
        throw WbemExecException(WbemExecException::INVALID_INPUT);
    }

    //
    //  CIM element
    //
    XmlReader::getCimStartTag (parser, cimVersion, dtdVersion);


    //
    //  MESSAGE element
    //
    if (!XmlReader::getMessageStartTag (parser, messageId, protocolVersion))
    {
        throw XmlValidationError(parser.getLine(), "expected MESSAGE element");

        // l10n TODO
        //MessageLoaderParms mlParms(
        //  "Server.CIMOperationRequestDecoder.EXPECTED_MESSAGE_ELEMENT",
        //  "expected MESSAGE element");
        //throw XmlValidationError(parser.getLine(), mlParms);
    }

    //
    //  MULTIREQ or SIMPLEREQ element
    //
    if (XmlReader::testStartTag (parser, entry, XML_ELEMENT_MULTIREQ))
    {
        multireq = true;
    }
    else if (!XmlReader::testStartTag (parser, entry, XML_ELEMENT_SIMPLEREQ))
    {
        // l10n TODO
        throw XmlValidationError (parser.getLine (), MISSING_ELEMENT_REQ);
    }

    //
    //  SIMPLEREQ element
    //
    else
    {
        //
        //  IMETHODCALL element
        //
        if (XmlReader::testStartTag (parser, entry, XML_ELEMENT_IMETHODCALL))
        {
            //
            //  Get NAME attribute of IMETHODCALL element
            //
            methodName = XmlReader::getCimNameAttribute (parser.getLine (),
                entry, XML_ELEMENT_IMETHODCALL);

            //
            //  Construct the object path from the LOCALNAMESPACEPATH
            //  subelements (NAMESPACE elements)
            //
            String namespaceName;

            if (!XmlReader::getLocalNameSpacePathElement(parser, namespaceName))
            {
                throw XmlValidationError(parser.getLine(),
                    "expected LOCALNAMESPACEPATH element");

                // l10n TODO
                //MessageLoaderParms mlParms(
                //    "Server.CIMOperationRequestDecoder."
                //        "EXPECTED_LOCALNAMESPACEPATH_ELEMENT",
                //    "expected LOCALNAMESPACEPATH element");
                //throw XmlValidationError(parser.getLine(),mlParms);
            }
            objPath.append(namespaceName);
        }

        //
        //  METHODCALL element
        //
        else if (XmlReader::testStartTag (parser, entry,
            XML_ELEMENT_METHODCALL))
        {
            //
            //  Get NAME attribute of METHODCALL element
            //
            methodName = XmlReader::getCimNameAttribute (parser.getLine (),
                entry, XML_ELEMENT_METHODCALL);

            //
            //  LOCALCLASSPATH or LOCALINSTANCEPATH element
            //
            if (XmlReader::getLocalClassPathElement (parser, objectName))
            {
                objPath.append(objectName.toString());
            }
            else if (XmlReader::getLocalInstancePathElement (parser,
                objectName))
            {
                objPath.append(objectName.toString());
            }
            else
            {
                // l10n TODO - Use CIMOperationRequestDecoder message when it
                // is available (or perhaps use Common.XmlReader.
                // EXPECTED_LOCALINSTANCEPATH_OR_LOCALCLASSPATH_ELEMENT)
                throw XmlValidationError (parser.getLine (),
                    MISSING_ELEMENT_LOCALPATH);
            }
        }
        else
        {
            throw XmlValidationError(parser.getLine(),
                "expected IMETHODCALL or METHODCALL element");

            // l10n TODO
            //MessageLoaderParms mlParms(
            // "Server.CIMOperationRequestDecoder.EXPECTED_IMETHODCALL_ELEMENT",
            // "expected IMETHODCALL or METHODCALL element");
            //throw XmlValidationError(parser.getLine(),mlParms);
        }
    }

    //
    //  Set headers
    //
    message.reserveCapacity (BUFFERSIZE);

    //
    //  Generate header prefix
    //
    srand(TimeValue::getCurrentTime().toMilliseconds() & 0xFFFFFFFF);
    char nn[3];
    nn[0] = '0' + (rand() % 10);
    nn[1] = '0' + (rand() % 10);
    nn[2] = 0;

    if (useMPost)
    {
        message << HTTP_METHOD_MPOST << HTTP_SP << HTTP_REQUEST_URI_CIMOM
                << HTTP_SP << HTTP_PROTOCOL << HTTP_VERSION_11 << HTTP_CRLF;
    }
    else
    {
        message << HTTP_METHOD_POST << HTTP_SP << HTTP_REQUEST_URI_CIMOM
                << HTTP_SP << HTTP_PROTOCOL;
        if (useHTTP11)
        {
            message << HTTP_VERSION_11;
        }
        else
        {
            message << HTTP_VERSION_10;
        }
        message << HTTP_CRLF;
    }
    message << HEADER_NAME_HOST << HEADER_SEPARATOR << HTTP_SP << hostName
            << HTTP_CRLF;
    message << HEADER_NAME_CONTENTTYPE << HEADER_SEPARATOR << HTTP_SP
            << HEADER_VALUE_CONTENTTYPE
            << HTTP_CRLF;
    message << HEADER_NAME_CONTENTLENGTH << HEADER_SEPARATOR << HTTP_SP
            << (Uint32)content.size () << HTTP_CRLF;

    if (useMPost)
    {
        message << HEADER_NAME_MAN << HEADER_SEPARATOR << HTTP_SP
                << HEADER_VALUE_MAN << nn << HTTP_CRLF;
        message << nn << HEADER_PREFIX_DELIMITER
                << HEADER_NAME_CIMPROTOCOLVERSION << HEADER_SEPARATOR
                << HTTP_SP << protocolVersion << HTTP_CRLF;
        message << nn << HEADER_PREFIX_DELIMITER << HEADER_NAME_CIMOPERATION
                << HEADER_SEPARATOR << HTTP_SP << HEADER_VALUE_CIMOPERATION
                << HTTP_CRLF;
        if (multireq)
        {
            message << nn << HEADER_PREFIX_DELIMITER << HEADER_NAME_CIMBATCH
                << HEADER_SEPARATOR << HTTP_CRLF;
        }
        else
        {
            message << nn << HEADER_PREFIX_DELIMITER << HEADER_NAME_CIMMETHOD
                << HEADER_SEPARATOR << HTTP_SP
                << XmlWriter::encodeURICharacters(methodName.getString())
                << HTTP_CRLF;
            message << nn << HEADER_PREFIX_DELIMITER << HEADER_NAME_CIMOBJECT
                << HEADER_SEPARATOR << HTTP_SP
                << XmlWriter::encodeURICharacters(objPath) << HTTP_CRLF;
        }
    }
    else
    {
        message << HEADER_NAME_CIMPROTOCOLVERSION << HEADER_SEPARATOR
                << HTTP_SP << protocolVersion << HTTP_CRLF;
        message << HEADER_NAME_CIMOPERATION << HEADER_SEPARATOR << HTTP_SP
                << HEADER_VALUE_CIMOPERATION << HTTP_CRLF;
        if (multireq)
        {
            message << HEADER_NAME_CIMBATCH << HEADER_SEPARATOR << HTTP_CRLF;
        }
        else
        {
            message << HEADER_NAME_CIMMETHOD << HEADER_SEPARATOR << HTTP_SP
                    << XmlWriter::encodeURICharacters(methodName.getString())
                    << HTTP_CRLF;
            message << HEADER_NAME_CIMOBJECT << HEADER_SEPARATOR << HTTP_SP
                    << XmlWriter::encodeURICharacters(objPath) << HTTP_CRLF;
        }
    }

    httpHeaders << message;
    message << HTTP_CRLF;
    message << content;

    return message;
}
/**

    Executes the command using HTTP.  A CIM request encoded in XML is read
    from the input, and encapsulated in an HTTP request message.  A channel
    is obtained for an HTTP connection, and the message is written to the
    channel.  The response is written to the specified outPrintWriter, and
    consists of the CIM response encoded in XML.

    @param   outPrintWriter     the ostream to which output should be
                                written
    @param   errPrintWriter     the ostream to which error output should be
                                written

    @exception  WbemExecException  if an error is encountered in executing
                                   the command

 */
void WbemExecCommand::_executeHttp (ostream& outPrintWriter,
                                    ostream& errPrintWriter)
{
    Uint32                       size;
    Buffer                    content;
    Buffer                    contentCopy;
    Buffer                    message;
    Buffer                    httpHeaders;
    Buffer                    httpResponse;
#ifdef PEGASUS_WMIMAPPER
    WMIWbemExecClient client;
#else
    WbemExecClient client;
#endif

    client.setTimeout( _timeout );

    //
    //  Check for invalid combination of options
    //  The M-POST method may not be used with HTTP/1.0
    //
    if ((!_useHTTP11) && (_useMPost))
    {
        throw WbemExecException(WbemExecException::MPOST_HTTP10_INVALID);
    }

    //
    //  If no hostName specified
    //  Default to local host
    //
    if (!_hostNameSet)
    {
      _hostName = System::getHostName();
    }
    if( !_portNumberSet )
    {
        if( _useSSL )
        {
            _portNumber = System::lookupPort( WBEM_HTTPS_SERVICE_NAME,
                              WBEM_DEFAULT_HTTPS_PORT );
        }
        else
        {
            _portNumber = System::lookupPort( WBEM_HTTP_SERVICE_NAME,
                              WBEM_DEFAULT_HTTP_PORT );
        }
        char buffer[32];
        sprintf( buffer, "%lu", (unsigned long) _portNumber );
        _portNumberStr = buffer;
    }

    //
    //  Get XML request from input file
    //
    if (_inputFilePathSet)
    {
        //
        //  Check that input file exists
        //
        if (!FileSystem::exists (_inputFilePath))
        {
            throw WbemExecException(WbemExecException::INPUT_FILE_NONEXISTENT);
        }

        //
        //  Check that input file is readable
        //
        if (!FileSystem::canRead (_inputFilePath))
        {
            throw WbemExecException(WbemExecException::INPUT_FILE_NOT_READABLE);
        }

        //
        //  Check that file is not empty
        //
        FileSystem::getFileSize (_inputFilePath, size);
        if (size == 0)
        {
            throw WbemExecException(WbemExecException::NO_INPUT);
        }

        //
        //  Read from input file
        //
        try
        {
            FileSystem::loadFileToMemory (content, _inputFilePath);
        }
        catch (const CannotOpenFile&)
        {
            throw WbemExecException(WbemExecException::INPUT_FILE_CANNOT_OPEN);
        }
    }
    else
    {
        //
        //  Read from cin
        //
        //  (GetLine is defined in Pegasus/Common/String.[h,cpp], but is
        //  not a class member.)
        //
        String line;

        while (GetLine (cin, line))
        {
            content << line << '\n';
        }

        if (content.size () == 0)
        {
            //
            //  No input
            //
            throw WbemExecException(WbemExecException::NO_INPUT);
        }
    }

    //
    //  Make a copy of the content because the XmlParser constructor
    //  modifies the text
    //
    contentCopy << content;

    XmlParser parser ((char*) contentCopy.getData ());

    try
    {
        _connectToServer( client, outPrintWriter );

        //
        //  Encapsulate XML request in an HTTP request
        //

        String hostName;
        if (_hostNameSet && _hostName.size())
        {
            hostName = _hostName + String(":") + _portNumberStr;
        }

        message = XMLProcess::encapsulate( parser, hostName,
                                           _useMPost, _useHTTP11,
                                           content, httpHeaders );

        if (_debugOutput1)
        {
            outPrintWriter << message.getData () << endl;
        }
    }
    catch (const XmlException& xe)
    {
        throw WbemExecException(
            WbemExecException::INVALID_XML, xe.getMessage());
    }
    catch (const WbemExecException&)
    {
        throw;
    }
    catch (const Exception& ex)
    {
        throw WbemExecException(
            WbemExecException::CONNECT_FAIL, ex.getMessage());
    }

    try
    {
        httpResponse = client.issueRequest( message );
    }
    catch (const ConnectionTimeoutException&)
    {
        throw WbemExecException(WbemExecException::TIMED_OUT);
    }
    catch (const UnauthorizedAccess& ex)
    {
        throw WbemExecException(
            WbemExecException::CONNECT_FAIL, ex.getMessage());
    }
    catch (const Exception& ex)
    {
        throw WbemExecException(
            WbemExecException::CONNECT_FAIL, ex.getMessage());
    }

    //
    // Process the response message
    //
    _handleResponse( httpResponse, outPrintWriter, errPrintWriter );
}