📄 wbemexeccommand.cpp
字号:
@return false = client response has been received */void WbemExecCommand::_handleResponse( Buffer responseMessage, ostream& oStream, ostream& eStream ){ String startLine; Array<HTTPHeader> headers; Uint32 contentLength; Uint32 contentOffset = 0; HTTPMessage httpMessage(responseMessage, 0); Boolean needsAuthentication = false; httpMessage.parse(startLine, headers, contentLength); if( contentLength > 0 ) { contentOffset = responseMessage.size() - contentLength; } else { contentOffset = responseMessage.size(); } String httpVersion; Uint32 statusCode; String reasonPhrase; Boolean parsableMessage = HTTPMessage::parseStatusLine( startLine, httpVersion, statusCode, reasonPhrase); if (!parsableMessage || (statusCode != HTTP_STATUSCODE_OK)) { // Received an HTTP error response // Output the HTTP error message and exit for (Uint32 i = 0; i < contentOffset; i++) { oStream << responseMessage[i]; } oStream.flush(); if( contentLength > 0 ) { _printContent( oStream, responseMessage, contentOffset ); } exit( 1 ); } // // Received a valid HTTP response from the server. // if (_debugOutput2) { for (Uint32 i = 0; i < contentOffset; i++) { oStream << responseMessage[i]; } oStream.flush(); } _printContent( oStream, responseMessage, contentOffset );}/** 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; WbemExecClient client; 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); content.append ('\0'); } 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'; } content.append ('\0'); if (content.size () <= 1) { // // 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 = String::EMPTY; 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 );}/** Parses the command line, validates the options, and sets instance variables based on the option arguments. @param argc the number of command line arguments @param argv the string vector of command line arguments @exception CommandFormatException if an error is encountered in parsing the command line */void WbemExecCommand::setCommand (Uint32 argc, char* argv []){ Uint32 i = 0; Uint32 c = 0; String httpVersion = String (); String httpMethod = String (); String timeoutStr = String (); String GetOptString = String (); getoopt getOpts; _operationType = OPERATION_TYPE_UNINITIALIZED; // // Construct GetOptString // GetOptString.append (_OPTION_HOSTNAME); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_PORTNUMBER); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_HTTPVERSION); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_HTTPMETHOD); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_SSL); GetOptString.append (_OPTION_TIMEOUT); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_USERNAME); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_PASSWORD); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); GetOptString.append (_OPTION_DEBUG); GetOptString.append (getoopt::GETOPT_ARGUMENT_DESIGNATOR); // // Initialize and parse getOpts // getOpts = getoopt (); getOpts.addFlagspec (GetOptString); //PEP#167 - adding long flag for options : 'help' and 'version' getOpts.addLongFlagspec(LONG_HELP,getoopt::NOARG); getOpts.addLongFlagspec(LONG_VERSION,getoopt::NOARG); getOpts.parse (argc, argv); if (getOpts.hasErrors ()) { throw CommandFormatException(getOpts.getErrorStrings()[0]); } // // Get options and arguments from the command line // for (i = getOpts.first (); i < getOpts.last (); i++) { if (getOpts[i].getType () == Optarg::LONGFLAG) { if (getOpts[i].getopt () == LONG_HELP) { if (_operationType != OPERATION_TYPE_UNINITIALIZED) { String param = String (LONG_HELP); // // More than one operation option was found // throw UnexpectedOptionException(param); } _operationType = OPERATION_TYPE_HELP; } else if (getOpts[i].getopt () == LONG_VERSION) { if (_operationType != OPERATION_TYPE_UNINITIALIZED) { String param = String (LONG_VERSION); // // More than one operation option was found // throw UnexpectedOptionException(param); } _operationType = OPERATION_TYPE_VERSION; } } else if (getOpts [i].getType () == Optarg::REGULAR) { // // _inputFilePath is the only non-option argument // if (_inputFilePathSet) { // // more than one _inputFilePath argument was found // throw UnexpectedArgumentException(getOpts[i].Value()); } _inputFilePath = getOpts [i].Value (); _inputFilePathSet = true; } else /* getOpts [i].getType () == FLAG */ { c = getOpts [i].getopt () [0]; switch (c) { case _OPTION_HOSTNAME: { if (getOpts.isSet (_OPTION_HOSTNAME) > 1) { // // More than one hostname option was found // throw DuplicateOptionException(_OPTION_HOSTNAME); } _hostName = getOpts [i].Value (); _hostNameSet = true; break; }
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -