version 1.106, 2007/09/11 17:56:33
|
version 1.116, 2008/02/26 19:15:05
|
|
|
| |
~HTTPAcceptorRep() | ~HTTPAcceptorRep() |
{ | { |
|
closeSocket(); |
delete address; | delete address; |
} | } |
|
|
|
void closeSocket() |
|
{ |
|
Socket::close(socket); |
|
} |
|
|
struct sockaddr* address; | struct sockaddr* address; |
| |
SocketLength address_size; | SocketLength address_size; |
|
|
_connectionType(connectionType), | _connectionType(connectionType), |
_portNumber(portNumber), | _portNumber(portNumber), |
_sslcontext(sslcontext), | _sslcontext(sslcontext), |
_sslContextObjectLock(sslContextObjectLock) |
_sslContextObjectLock(sslContextObjectLock), |
|
_idleConnectionTimeoutSeconds(0) |
{ | { |
Socket::initializeInterface(); | Socket::initializeInterface(); |
| |
|
|
MessageLoaderParms parms("Common.HTTPAcceptor.FAILED_CREATE_SOCKET", | MessageLoaderParms parms("Common.HTTPAcceptor.FAILED_CREATE_SOCKET", |
"Failed to create socket"); | "Failed to create socket"); |
PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, | PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, |
"HTTPAcceptor::_bind _rep->socket < 0"); |
"HTTPAcceptor::_bind: createSocket() _rep->socket failed"); |
throw BindFailedException(parms); | throw BindFailedException(parms); |
} | } |
| |
|
Socket::disableBlocking(_rep->socket); |
| |
// set the close-on-exec bit for this file handle. | // set the close-on-exec bit for this file handle. |
// any unix that forks needs this bit set. | // any unix that forks needs this bit set. |
|
|
if (setsockopt(_rep->socket, SOL_SOCKET, SO_REUSEADDR, | if (setsockopt(_rep->socket, SOL_SOCKET, SO_REUSEADDR, |
(char *)&opt, sizeof(opt)) < 0) | (char *)&opt, sizeof(opt)) < 0) |
{ | { |
Socket::close(_rep->socket); |
|
delete _rep; | delete _rep; |
_rep = 0; | _rep = 0; |
MessageLoaderParms parms("Common.HTTPAcceptor.FAILED_SET_SOCKET_OPTION", | MessageLoaderParms parms("Common.HTTPAcceptor.FAILED_SET_SOCKET_OPTION", |
|
|
// | // |
if (::bind(_rep->socket, _rep->address, _rep->address_size) < 0) | if (::bind(_rep->socket, _rep->address, _rep->address_size) < 0) |
{ | { |
Socket::close(_rep->socket); |
MessageLoaderParms parms( |
|
"Common.HTTPAcceptor.FAILED_BIND_SOCKET_DETAIL", |
|
"Failed to bind socket on port $0: $1.", |
|
_portNumber, PEGASUS_SYSTEM_NETWORK_ERRORMSG_NLS); |
|
|
delete _rep; | delete _rep; |
_rep = 0; | _rep = 0; |
MessageLoaderParms parms("Common.HTTPAcceptor.FAILED_BIND_SOCKET", |
|
"Failed to bind socket"); |
|
PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, | PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, |
"HTTPAcceptor::_bind: Failed to bind socket."); | "HTTPAcceptor::_bind: Failed to bind socket."); |
throw BindFailedException(parms); | throw BindFailedException(parms); |
|
|
S_IRGRP | S_IWGRP | S_IXGRP | | S_IRGRP | S_IWGRP | S_IXGRP | |
S_IROTH | S_IWOTH | S_IXOTH ) < 0 ) | S_IROTH | S_IWOTH | S_IXOTH ) < 0 ) |
{ | { |
Socket::close(_rep->socket); |
MessageLoaderParms parms( |
|
"Common.HTTPAcceptor.FAILED_SET_LDS_FILE_OPTION", |
|
"Failed to set permission on local domain socket {0}: {1}.", |
|
PEGASUS_LOCAL_DOMAIN_SOCKET_PATH, |
|
PEGASUS_SYSTEM_ERRORMSG_NLS ); |
|
|
delete _rep; | delete _rep; |
_rep = 0; | _rep = 0; |
MessageLoaderParms parms("Common.HTTPAcceptor.FAILED_BIND_SOCKET", |
|
"Failed to bind socket"); |
|
PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, | PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, |
"HTTPAcceptor::_bind: Failed to set domain socket " | "HTTPAcceptor::_bind: Failed to set domain socket " |
"permissions."); | "permissions."); |
|
|
| |
//int const _maxConnectionQueueLength = 15; | //int const _maxConnectionQueueLength = 15; |
| |
if (listen(_rep->socket, _maxConnectionQueueLength) < 0) |
if (::listen(_rep->socket, _maxConnectionQueueLength) < 0) |
{ | { |
Socket::close(_rep->socket); |
MessageLoaderParms parms( |
|
"Common.HTTPAcceptor.FAILED_LISTEN_SOCKET", |
|
"Failed to listen on socket {0}: {1}.", |
|
(int)_rep->socket,PEGASUS_SYSTEM_NETWORK_ERRORMSG_NLS ); |
|
|
delete _rep; | delete _rep; |
_rep = 0; | _rep = 0; |
MessageLoaderParms parms("Common.HTTPAcceptor.FAILED_BIND_SOCKET", |
|
"Failed to bind socket"); |
|
PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, | PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, |
"HTTPAcceptor::_bind: Failed to bind socket(1)."); | "HTTPAcceptor::_bind: Failed to bind socket(1)."); |
throw BindFailedException(parms); | throw BindFailedException(parms); |
|
|
_rep->socket, | _rep->socket, |
SocketMessage::READ | SocketMessage::EXCEPTION, | SocketMessage::READ | SocketMessage::EXCEPTION, |
getQueueId(), | getQueueId(), |
Monitor::ACCEPTOR))) |
MonitorEntry::TYPE_ACCEPTOR))) |
{ | { |
Socket::close(_rep->socket); |
|
delete _rep; | delete _rep; |
_rep = 0; | _rep = 0; |
MessageLoaderParms parms( | MessageLoaderParms parms( |
|
|
//_monitor->unsolicitSocketMessages(_rep->socket); | //_monitor->unsolicitSocketMessages(_rep->socket); |
| |
// close the socket | // close the socket |
Socket::close(_rep->socket); |
_rep->closeSocket(); |
// Unlink Local Domain Socket Bug# 3312 | // Unlink Local Domain Socket Bug# 3312 |
if (_connectionType == LOCAL_CONNECTION) | if (_connectionType == LOCAL_CONNECTION) |
{ | { |
|
|
// unregister the socket | // unregister the socket |
_monitor->unsolicitSocketMessages(_rep->socket); | _monitor->unsolicitSocketMessages(_rep->socket); |
// close the socket | // close the socket |
Socket::close(_rep->socket); |
_rep->closeSocket(); |
// Unlink Local Domain Socket Bug# 3312 |
// Unlink Local Domain Socket |
if (_connectionType == LOCAL_CONNECTION) | if (_connectionType == LOCAL_CONNECTION) |
{ | { |
#ifndef PEGASUS_DISABLE_LOCAL_DOMAIN_SOCKET | #ifndef PEGASUS_DISABLE_LOCAL_DOMAIN_SOCKET |
|
|
if (_rep) | if (_rep) |
{ | { |
AutoMutex autoMut(_rep->_connection_mut); | AutoMutex autoMut(_rep->_connection_mut); |
if (_rep->connections.size() > 0) |
for (Uint32 i = 0, n = _rep->connections.size(); i < n; i++) |
{ | { |
HTTPConnection* connection = _rep->connections[0]; |
HTTPConnection* connection = _rep->connections[i]; |
count = connection->getRequestCount(); |
if (connection->isResponsePending()) |
|
{ |
|
count++; |
|
} |
} | } |
} | } |
return count; | return count; |
|
|
_socketWriteTimeout = socketWriteTimeout; | _socketWriteTimeout = socketWriteTimeout; |
} | } |
| |
|
void HTTPAcceptor::setIdleConnectionTimeout(Uint32 idleConnectionTimeoutSeconds) |
|
{ |
|
_idleConnectionTimeoutSeconds = idleConnectionTimeoutSeconds; |
|
} |
|
|
void HTTPAcceptor::unbind() | void HTTPAcceptor::unbind() |
{ | { |
if (_rep) | if (_rep) |
{ | { |
_portNumber = 0; | _portNumber = 0; |
Socket::close(_rep->socket); |
_rep->closeSocket(); |
| |
if (_connectionType == LOCAL_CONNECTION) | if (_connectionType == LOCAL_CONNECTION) |
{ | { |
|
|
#endif | #endif |
} | } |
| |
SocketHandle socket; |
// It is not necessary to handle EINTR errors from this accept() call. |
#ifdef PEGASUS_OS_TYPE_WINDOWS |
// An EINTR error should not occur on a non-blocking socket. If the |
socket = accept(_rep->socket, accept_address, &address_size); |
// listen socket is blocking and EINTR occurs, the new socket connection |
#else |
// is not accepted here. |
while ( |
|
((socket = accept(_rep->socket, accept_address, &address_size)) == -1) |
// EAGAIN errors are also not handled here. An EAGAIN error should not |
&& (errno == EINTR)) |
// occur after select() indicates that the listen socket is available for |
; |
// reading. If the accept() fails with an EAGAIN error code, a new |
#endif |
// connection is not accepted here. |
|
|
|
SocketHandle socket = accept(_rep->socket, accept_address, &address_size); |
| |
if (socket == PEGASUS_SOCKET_ERROR) | if (socket == PEGASUS_SOCKET_ERROR) |
{ | { |
|
|
return; | return; |
} | } |
| |
|
// Use an AutoPtr to ensure the socket handle is closed on exception |
|
AutoPtr<SocketHandle, CloseSocketHandle> socketPtr(&socket); |
|
|
|
#ifndef PEGASUS_OS_TYPE_WINDOWS |
|
// We need to ensure that the socket number is not higher than |
|
// what fits into FD_SETSIZE, because we else won't be able to select on it |
|
// and won't ever communicate correct on that socket. |
|
if (socket >= FD_SETSIZE) |
|
{ |
|
// the remote connection is invalid, destroy client address. |
|
delete accept_address; |
|
|
|
Logger::put(Logger::STANDARD_LOG, System::CIMSERVER, Logger::TRACE, |
|
"HTTPAcceptor out of available sockets. " |
|
"Closing connection to the new client."); |
|
|
|
PEG_TRACE( |
|
(TRC_DISCARDED_DATA, |
|
Tracer::LEVEL2, |
|
"accept() returned too large socket number %d.", |
|
socket)); |
|
|
|
return; |
|
} |
|
#endif |
|
|
String ipAddress; | String ipAddress; |
| |
if (_connectionType == LOCAL_CONNECTION) | if (_connectionType == LOCAL_CONNECTION) |
|
|
PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, | PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, |
"HTTPAcceptor: getnameinfo() failed"); | "HTTPAcceptor: getnameinfo() failed"); |
delete accept_address; | delete accept_address; |
Socket::close(socket); |
|
return; | return; |
} | } |
ipAddress = ipBuffer; | ipAddress = ipBuffer; |
|
|
| |
SharedPtr<MP_Socket> mp_socket(new MP_Socket( | SharedPtr<MP_Socket> mp_socket(new MP_Socket( |
socket, _sslcontext, _sslContextObjectLock, ipAddress)); | socket, _sslcontext, _sslContextObjectLock, ipAddress)); |
|
// mp_socket now has responsibility for closing the socket handle |
|
socketPtr.release(); |
| |
|
mp_socket->disableBlocking(); |
mp_socket->setSocketWriteTimeout(_socketWriteTimeout); | mp_socket->setSocketWriteTimeout(_socketWriteTimeout); |
| |
// Perform the SSL handshake, if applicable. Make the socket non-blocking |
// Perform the SSL handshake, if applicable. |
// for this operation so we can send it back to the Monitor's select() loop |
|
// if it takes a while. |
|
| |
mp_socket->disableBlocking(); |
|
Sint32 socketAcceptStatus = mp_socket->accept(); | Sint32 socketAcceptStatus = mp_socket->accept(); |
mp_socket->enableBlocking(); |
|
| |
if (socketAcceptStatus < 0) | if (socketAcceptStatus < 0) |
{ | { |
PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, | PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, |
"HTTPAcceptor: SSL_accept() failed"); | "HTTPAcceptor: SSL_accept() failed"); |
mp_socket->close(); |
|
return; | return; |
} | } |
| |
// Create a new connection and add it to the connection list: | // Create a new connection and add it to the connection list: |
| |
HTTPConnection* connection = new HTTPConnection(_monitor, mp_socket, |
AutoPtr<HTTPConnection> connection(new HTTPConnection( |
ipAddress, this, static_cast<MessageQueue *>(_outputMessageQueue)); |
_monitor, |
|
mp_socket, |
|
ipAddress, |
|
this, |
|
static_cast<MessageQueue *>(_outputMessageQueue))); |
|
|
|
if (_idleConnectionTimeoutSeconds) |
|
{ |
|
connection->_idleConnectionTimeoutSeconds = |
|
_idleConnectionTimeoutSeconds; |
|
Time::gettimeofday(&connection->_idleStartTime); |
|
} |
| |
if (socketAcceptStatus == 0) | if (socketAcceptStatus == 0) |
{ | { |
PEG_TRACE_CSTRING(TRC_HTTP, Tracer::LEVEL2, | PEG_TRACE_CSTRING(TRC_HTTP, Tracer::LEVEL2, |
"HTTPAcceptor: SSL_accept() pending"); | "HTTPAcceptor: SSL_accept() pending"); |
connection->_acceptPending = true; | connection->_acceptPending = true; |
|
Time::gettimeofday(&connection->_acceptPendingStartTime); |
} | } |
| |
// Solicit events on this new connection's socket: | // Solicit events on this new connection's socket: |
|
|
if (-1 == (index = _monitor->solicitSocketMessages( | if (-1 == (index = _monitor->solicitSocketMessages( |
connection->getSocket(), | connection->getSocket(), |
SocketMessage::READ | SocketMessage::EXCEPTION, | SocketMessage::READ | SocketMessage::EXCEPTION, |
connection->getQueueId(), Monitor::CONNECTION)) ) |
connection->getQueueId(), MonitorEntry::TYPE_CONNECTION)) ) |
{ | { |
// ATTN-DE-P2-2003100503::TODO::Need to enhance code to return |
|
// an error message to Client application. |
|
PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, | PEG_TRACE_CSTRING(TRC_DISCARDED_DATA, Tracer::LEVEL2, |
"HTTPAcceptor::_acceptConnection: Attempt to allocate entry in " | "HTTPAcceptor::_acceptConnection: Attempt to allocate entry in " |
"_entries table failed."); | "_entries table failed."); |
delete connection; |
|
Socket::close(socket); |
|
return; | return; |
} | } |
| |
// Save the socket for cleanup later: |
|
connection->_entry_index = index; | connection->_entry_index = index; |
AutoMutex autoMut(_rep->_connection_mut); | AutoMutex autoMut(_rep->_connection_mut); |
_rep->connections.append(connection); |
_rep->connections.append(connection.get()); |
|
connection.release(); |
} | } |
| |
PEGASUS_NAMESPACE_END | PEGASUS_NAMESPACE_END |