Commit c5026298 authored by Jacek Caban's avatar Jacek Caban Committed by Alexandre Julliard

winhttp: Allocate netconn_t separately from request_t.

Once persistent connections will be supported, life time of connection may exceed life time of request object. Signed-off-by: 's avatarJacek Caban <jacek@codeweavers.com> Signed-off-by: 's avatarHans Leidekker <hans@codeweavers.com> Signed-off-by: 's avatarAlexandre Julliard <julliard@winehq.org>
parent c06104c6
......@@ -306,20 +306,19 @@ void netconn_unload( void )
#endif
}
BOOL netconn_connected( netconn_t *conn )
{
return (conn->socket != -1);
}
BOOL netconn_create( netconn_t *conn, int domain, int type, int protocol )
netconn_t *netconn_create( int domain, int type, int protocol )
{
netconn_t *conn;
conn = heap_alloc_zero(sizeof(*conn));
if (!conn) return NULL;
if ((conn->socket = socket( domain, type, protocol )) == -1)
{
WARN("unable to create socket (%s)\n", strerror(errno));
set_last_error( sock_get_error( errno ) );
return FALSE;
heap_free(conn);
return NULL;
}
return TRUE;
return conn;
}
BOOL netconn_close( netconn_t *conn )
......@@ -329,19 +328,12 @@ BOOL netconn_close( netconn_t *conn )
if (conn->secure)
{
heap_free( conn->peek_msg_mem );
conn->peek_msg_mem = NULL;
conn->peek_msg = NULL;
conn->peek_len = 0;
heap_free(conn->ssl_buf);
conn->ssl_buf = NULL;
heap_free(conn->extra_buf);
conn->extra_buf = NULL;
conn->extra_len = 0;
DeleteSecurityContext(&conn->ssl_ctx);
conn->secure = FALSE;
}
res = closesocket( conn->socket );
conn->socket = -1;
heap_free(conn);
if (res == -1)
{
set_last_error( sock_get_error( errno ) );
......@@ -575,7 +567,6 @@ static BOOL send_ssl_chunk(netconn_t *conn, const void *msg, size_t size)
BOOL netconn_send( netconn_t *conn, const void *msg, size_t len, int *sent )
{
if (!netconn_connected( conn )) return FALSE;
if (conn->secure)
{
const BYTE *ptr = msg;
......@@ -699,7 +690,6 @@ static BOOL read_ssl_chunk(netconn_t *conn, void *buf, SIZE_T buf_size, SIZE_T *
BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd )
{
*recvd = 0;
if (!netconn_connected( conn )) return FALSE;
if (!len) return TRUE;
if (conn->secure)
......@@ -756,13 +746,7 @@ BOOL netconn_recv( netconn_t *conn, void *buf, size_t len, int flags, int *recvd
ULONG netconn_query_data_available( netconn_t *conn )
{
if(!netconn_connected(conn))
return 0;
if(conn->secure)
return conn->peek_len;
return 0;
return conn->secure ? conn->peek_len : 0;
}
DWORD netconn_set_timeout( netconn_t *netconn, BOOL send, int value )
......
......@@ -551,7 +551,7 @@ static WCHAR *build_request_path( request_t *request )
static const WCHAR http[] = { 'h','t','t','p',0 };
static const WCHAR https[] = { 'h','t','t','p','s',0 };
static const WCHAR fmt[] = { '%','s',':','/','/','%','s',0 };
LPCWSTR scheme = request->netconn.secure ? https : http;
LPCWSTR scheme = (request->netconn ? request->netconn->secure : (request->hdr.flags & WINHTTP_FLAG_SECURE)) ? https : http;
int len;
len = strlenW( scheme ) + strlenW( request->connect->hostname );
......@@ -956,7 +956,7 @@ static BOOL secure_proxy_connect( request_t *request )
{
int len = strlen( req_ascii ), bytes_sent;
ret = netconn_send( &request->netconn, req_ascii, len, &bytes_sent );
ret = netconn_send( request->netconn, req_ascii, len, &bytes_sent );
heap_free( req_ascii );
if (ret)
ret = read_reply( request );
......@@ -993,6 +993,7 @@ static WCHAR *addr_to_str( struct sockaddr *addr )
static BOOL open_connection( request_t *request )
{
netconn_t *netconn;
connect_t *connect;
WCHAR *addressW = NULL;
INTERNET_PORT port;
......@@ -1000,7 +1001,7 @@ static BOOL open_connection( request_t *request )
struct sockaddr *saddr;
DWORD len;
if (netconn_connected( &request->netconn )) goto done;
if (request->netconn) goto done;
connect = request->connect;
port = connect->serverport ? connect->serverport : (request->hdr.flags & WINHTTP_FLAG_SECURE ? 443 : 80);
......@@ -1024,16 +1025,16 @@ static BOOL open_connection( request_t *request )
send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_CONNECTING_TO_SERVER, addressW, 0 );
if (!netconn_create( &request->netconn, saddr->sa_family, SOCK_STREAM, 0 ))
if (!(netconn = netconn_create( saddr->sa_family, SOCK_STREAM, 0 )))
{
heap_free( addressW );
return FALSE;
}
netconn_set_timeout( &request->netconn, TRUE, request->send_timeout );
netconn_set_timeout( &request->netconn, FALSE, request->recv_timeout );
if (!netconn_connect( &request->netconn, saddr, slen, request->connect_timeout ))
netconn_set_timeout( netconn, TRUE, request->send_timeout );
netconn_set_timeout( netconn, FALSE, request->recv_timeout );
if (!netconn_connect( netconn, saddr, slen, request->connect_timeout ))
{
netconn_close( &request->netconn );
netconn_close( netconn );
heap_free( addressW );
return FALSE;
}
......@@ -1048,13 +1049,14 @@ static BOOL open_connection( request_t *request )
return FALSE;
}
}
if (!netconn_secure_connect( &request->netconn, connect->hostname, request->security_flags ))
if (!netconn_secure_connect( netconn, connect->hostname, request->security_flags ))
{
netconn_close( &request->netconn );
netconn_close( netconn );
heap_free( addressW );
return FALSE;
}
}
request->netconn = netconn;
send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_CONNECTED_TO_SERVER, addressW, strlenW(addressW) + 1 );
......@@ -1069,10 +1071,11 @@ done:
void close_connection( request_t *request )
{
if (!netconn_connected( &request->netconn )) return;
if (!request->netconn) return;
send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_CLOSING_CONNECTION, 0, 0 );
netconn_close( &request->netconn );
netconn_close( request->netconn );
request->netconn = NULL;
send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_CONNECTION_CLOSED, 0, 0 );
}
......@@ -1138,7 +1141,7 @@ static BOOL read_more_data( request_t *request, int maxlen, BOOL notify )
if (notify) send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_RECEIVING_RESPONSE, NULL, 0 );
ret = netconn_recv( &request->netconn, request->read_buf + request->read_size,
ret = netconn_recv( request->netconn, request->read_buf + request->read_size,
maxlen - request->read_size, 0, &len );
if (notify) send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_RESPONSE_RECEIVED, &len, sizeof(len) );
......@@ -1385,13 +1388,13 @@ static BOOL send_request( request_t *request, LPCWSTR headers, DWORD headers_len
send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_SENDING_REQUEST, NULL, 0 );
ret = netconn_send( &request->netconn, req_ascii, len, &bytes_sent );
ret = netconn_send( request->netconn, req_ascii, len, &bytes_sent );
heap_free( req_ascii );
if (!ret) goto end;
if (optional_len)
{
if (!netconn_send( &request->netconn, optional, optional_len, &bytes_sent )) goto end;
if (!netconn_send( request->netconn, optional, optional_len, &bytes_sent )) goto end;
request->optional = optional;
request->optional_len = optional_len;
len += optional_len;
......@@ -2167,7 +2170,7 @@ static BOOL read_reply( request_t *request )
WCHAR *versionW, *status_textW, *raw_headers;
WCHAR status_codeW[4]; /* sizeof("nnn") */
if (!netconn_connected( &request->netconn )) return FALSE;
if (!request->netconn) return FALSE;
do
{
......@@ -2353,8 +2356,8 @@ static BOOL handle_redirect( request_t *request, DWORD status )
connect->hostport = port;
if (!(ret = set_server_for_hostname( connect, hostname, port ))) goto end;
netconn_close( &request->netconn );
if (!(ret = netconn_init( &request->netconn ))) goto end;
netconn_close( request->netconn );
request->netconn = NULL;
request->content_length = request->content_read = 0;
request->read_pos = request->read_size = 0;
request->read_chunked = request->read_chunked_eof = FALSE;
......@@ -2505,14 +2508,14 @@ static BOOL query_data_available( request_t *request, DWORD *available, BOOL asy
if (end_of_read_data( request )) goto done;
count = get_available_data( request );
if (!request->read_chunked)
count += netconn_query_data_available( &request->netconn );
if (!request->read_chunked && request->netconn)
count += netconn_query_data_available( request->netconn );
if (!count)
{
refill_buffer( request, async );
count = get_available_data( request );
if (!request->read_chunked)
count += netconn_query_data_available( &request->netconn );
if (!request->read_chunked && request->netconn)
count += netconn_query_data_available( request->netconn );
}
done:
......@@ -2625,7 +2628,7 @@ static BOOL write_data( request_t *request, LPCVOID buffer, DWORD to_write, LPDW
BOOL ret;
int num_bytes;
ret = netconn_send( &request->netconn, buffer, to_write, &num_bytes );
ret = netconn_send( request->netconn, buffer, to_write, &num_bytes );
if (async)
{
......
......@@ -709,7 +709,7 @@ static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buf
{
case WINHTTP_OPTION_SECURITY_FLAGS:
{
DWORD flags;
DWORD flags = 0;
int bits;
if (!buffer || *buflen < sizeof(flags))
......@@ -722,13 +722,16 @@ static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buf
flags = 0;
if (hdr->flags & WINHTTP_FLAG_SECURE) flags |= SECURITY_FLAG_SECURE;
flags |= request->security_flags;
bits = netconn_get_cipher_strength( &request->netconn );
if (bits >= 128)
flags |= SECURITY_FLAG_STRENGTH_STRONG;
else if (bits >= 56)
flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
else
flags |= SECURITY_FLAG_STRENGTH_WEAK;
if (request->netconn)
{
bits = netconn_get_cipher_strength( request->netconn );
if (bits >= 128)
flags |= SECURITY_FLAG_STRENGTH_STRONG;
else if (bits >= 56)
flags |= SECURITY_FLAG_STRENGTH_MEDIUM;
else
flags |= SECURITY_FLAG_STRENGTH_WEAK;
}
*(DWORD *)buffer = flags;
*buflen = sizeof(flags);
return TRUE;
......@@ -744,7 +747,7 @@ static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buf
return FALSE;
}
if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
if (!request->netconn || !(cert = netconn_get_certificate( request->netconn ))) return FALSE;
*(CERT_CONTEXT **)buffer = (CERT_CONTEXT *)cert;
*buflen = sizeof(cert);
return TRUE;
......@@ -763,7 +766,7 @@ static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buf
set_last_error( ERROR_INSUFFICIENT_BUFFER );
return FALSE;
}
if (!(cert = netconn_get_certificate( &request->netconn ))) return FALSE;
if (!request->netconn || !(cert = netconn_get_certificate( request->netconn ))) return FALSE;
ci->ftExpiry = cert->pCertInfo->NotAfter;
ci->ftStart = cert->pCertInfo->NotBefore;
......@@ -778,7 +781,7 @@ static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buf
else
ci->lpszSignatureAlgName = NULL;
ci->lpszEncryptionAlgName = NULL;
ci->dwKeySize = netconn_get_cipher_strength( &request->netconn );
ci->dwKeySize = request->netconn ? netconn_get_cipher_strength( request->netconn ) : 0;
CertFreeCertificateContext( cert );
*buflen = sizeof(*ci);
......@@ -793,7 +796,7 @@ static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buf
return FALSE;
}
*(DWORD *)buffer = netconn_get_cipher_strength( &request->netconn );
*(DWORD *)buffer = request->netconn ? netconn_get_cipher_strength( request->netconn ) : 0;
*buflen = sizeof(DWORD);
return TRUE;
}
......@@ -810,12 +813,12 @@ static BOOL request_query_option( object_header_t *hdr, DWORD option, LPVOID buf
set_last_error( ERROR_INSUFFICIENT_BUFFER );
return FALSE;
}
if (!netconn_connected( &request->netconn ))
if (!request->netconn)
{
set_last_error( ERROR_WINHTTP_INCORRECT_HANDLE_STATE );
return FALSE;
}
if (getsockname( request->netconn.socket, &local, &len )) return FALSE;
if (getsockname( request->netconn->socket, &local, &len )) return FALSE;
if (!convert_sockaddr( &local, &info->LocalAddress )) return FALSE;
if (!convert_sockaddr( remote, &info->RemoteAddress )) return FALSE;
info->cbSize = sizeof(*info);
......@@ -1106,7 +1109,6 @@ HINTERNET WINAPI WinHttpOpenRequest( HINTERNET hconnect, LPCWSTR verb, LPCWSTR o
request->connect = connect;
list_add_head( &connect->hdr.children, &request->hdr.entry );
if (!netconn_init( &request->netconn )) goto end;
request->resolve_timeout = connect->session->resolve_timeout;
request->connect_timeout = connect->session->connect_timeout;
request->send_timeout = connect->session->send_timeout;
......@@ -2089,10 +2091,10 @@ BOOL WINAPI WinHttpSetTimeouts( HINTERNET handle, int resolve, int connect, int
if (receive < 0) receive = 0;
request->recv_timeout = receive;
if (netconn_connected( &request->netconn ))
if (request->netconn)
{
if (netconn_set_timeout( &request->netconn, TRUE, send )) ret = FALSE;
if (netconn_set_timeout( &request->netconn, FALSE, receive )) ret = FALSE;
if (netconn_set_timeout( request->netconn, TRUE, send )) ret = FALSE;
if (netconn_set_timeout( request->netconn, FALSE, receive )) ret = FALSE;
}
break;
......
......@@ -190,7 +190,7 @@ typedef struct
LPWSTR raw_headers;
void *optional;
DWORD optional_len;
netconn_t netconn;
netconn_t *netconn;
DWORD security_flags;
int resolve_timeout;
int connect_timeout;
......@@ -283,8 +283,7 @@ void close_connection( request_t * ) DECLSPEC_HIDDEN;
BOOL netconn_close( netconn_t * ) DECLSPEC_HIDDEN;
BOOL netconn_connect( netconn_t *, const struct sockaddr *, unsigned int, int ) DECLSPEC_HIDDEN;
BOOL netconn_connected( netconn_t * ) DECLSPEC_HIDDEN;
BOOL netconn_create( netconn_t *, int, int, int ) DECLSPEC_HIDDEN;
netconn_t *netconn_create( int, int, int ) DECLSPEC_HIDDEN;
BOOL netconn_init( netconn_t * ) DECLSPEC_HIDDEN;
void netconn_unload( void ) DECLSPEC_HIDDEN;
ULONG netconn_query_data_available( netconn_t * ) DECLSPEC_HIDDEN;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment