gdi32: Horrible hack to enable SetDIBits to work with a NULL hdc.

Huw Davies huw at codeweavers.com
Mon Sep 8 11:34:51 CDT 2008


---
 dlls/gdi32/dib.c |   13 +++++++++++--
 1 files changed, 11 insertions(+), 2 deletions(-)

diff --git a/dlls/gdi32/dib.c b/dlls/gdi32/dib.c
index 440857d..3ba2e0a 100644
--- a/dlls/gdi32/dib.c
+++ b/dlls/gdi32/dib.c
@@ -335,11 +335,19 @@ INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
     DC *dc;
     BITMAPOBJ *bitmap;
     INT result = 0;
+    HDC hdc_display = NULL;
 
     if (!(dc = get_dc_ptr( hdc )))
     {
-        if (coloruse == DIB_RGB_COLORS) FIXME( "shouldn't require a DC for DIB_RGB_COLORS\n" );
-        return 0;
+        if (coloruse == DIB_RGB_COLORS)
+        {
+            WCHAR display[] = {'D','I','S','P','L','A','Y',0};
+            hdc_display = CreateDCW(display, NULL, NULL, NULL);
+            if (!(dc = get_dc_ptr( hdc_display )))
+                return 0;
+        }
+        else
+            return 0;
     }
 
     update_dc( dc );
@@ -365,6 +373,7 @@ INT WINAPI SetDIBits( HDC hdc, HBITMAP hbitmap, UINT startscan,
  done:
     GDI_ReleaseObj( hbitmap );
     release_dc_ptr( dc );
+    if(hdc_display) DeleteDC(hdc_display);
     return result;
 }
 
-- 
1.5.4.4.532.ga6828


--------------070900010703090807050009
Content-Type: text/x-patch;
 name="focus.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="focus.diff"

diff --git a/dlls/winex11.drv/event.c b/dlls/winex11.drv/event.c
index 32befa4..01798f1 100644
--- a/dlls/winex11.drv/event.c
+++ b/dlls/winex11.drv/event.c
@@ -664,7 +664,6 @@ static void X11DRV_FocusOut( HWND hwnd, XEvent *xev )
         wine_tsx11_unlock();
     }
     if (hwnd != GetForegroundWindow()) return;
-    SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
 
     /* don't reset the foreground window, if the window which is
        getting the focus is a Wine window */
@@ -680,6 +679,8 @@ static void X11DRV_FocusOut( HWND hwnd, XEvent *xev )
 
     if (!focus_win)
     {
+        SendMessageW( hwnd, WM_CANCELMODE, 0, 0 );
+
         /* Abey : 6-Oct-99. Check again if the focus out window is the
            Foreground window, because in most cases the messages sent
            above must have already changed the foreground window, in which

--------------070900010703090807050009
Content-Type: text/x-patch;
 name="winhttp_cookies.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="winhttp_cookies.diff"

619789ecb39377331f8db651a1dddf4ce4399b8c
diff --git a/dlls/winhttp/Makefile.in b/dlls/winhttp/Makefile.in
index 5617a1d..cf188c7 100644
--- a/dlls/winhttp/Makefile.in
+++ b/dlls/winhttp/Makefile.in
@@ -8,6 +8,7 @@ IMPORTS   = wininet kernel32
 DELAYIMPORTS = crypt32
 
 C_SRCS = \
+	cookie.c \
 	handle.c \
 	main.c \
 	net.c \
diff --git a/dlls/winhttp/cookie.c b/dlls/winhttp/cookie.c
new file mode 100644
index 0000000..f0dde99
--- /dev/null
+++ b/dlls/winhttp/cookie.c
@@ -0,0 +1,280 @@
+/*
+ * Copyright 2008 Hans Leidekker for CodeWeavers
+ *
+ * This library is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * This library is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with this library; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
+ */
+
+#include "config.h"
+#include <stdarg.h>
+
+#include "wine/debug.h"
+#include "wine/list.h"
+
+#include "windef.h"
+#include "winbase.h"
+#include "winhttp.h"
+
+#include "winhttp_private.h"
+
+WINE_DEFAULT_DEBUG_CHANNEL(winhttp);
+
+static struct list domain_list = LIST_INIT(domain_list);
+
+typedef struct
+{
+    struct list entry;
+    WCHAR *name;
+    struct list cookies;
+} domain_t;
+
+typedef struct
+{
+    struct list entry;
+    WCHAR *name;
+    WCHAR *value;
+    WCHAR *path;
+} cookie_t;
+
+static domain_t *add_domain( const WCHAR *name )
+{
+    domain_t *domain;
+
+    if (!(domain = heap_alloc_zero( sizeof(domain_t) ))) return NULL;
+
+    list_init( &domain->entry );
+    list_init( &domain->cookies );
+    if (!(domain->name = heap_alloc( (strlenW( name ) + 1) * sizeof(WCHAR) )))
+    {
+        heap_free( domain );
+        return NULL;
+    }
+    strcpyW( domain->name, name );
+    list_add_tail( &domain_list, &domain->entry );
+
+    TRACE("%s\n", debugstr_w(domain->name));
+    return domain;
+}
+
+static cookie_t *find_cookie( domain_t *domain, const WCHAR *path, const WCHAR *name )
+{
+    struct list *item;
+
+    LIST_FOR_EACH(item, &domain->cookies)
+    {
+        cookie_t *cookie = LIST_ENTRY(item, cookie_t, entry);
+        if (!strcmpW( cookie->path, path ) && !strcmpiW( cookie->name, name ))
+        {
+            TRACE("found %s=%s\n", debugstr_w(cookie->name), debugstr_w(cookie->value)); 
+            return cookie;
+         }
+    }
+    return NULL;
+}
+
+static BOOL match_domain( const WCHAR *name, domain_t *domain, BOOL partial )
+{
+    TRACE("comparing %s with %s\n", debugstr_w(name), debugstr_w(domain->name));
+
+    if (partial && !strstrW( name, domain->name )) return FALSE;
+    else if (!partial && strcmpW( name, domain->name )) return FALSE;
+    return TRUE;
+}
+
+static void free_cookie( cookie_t *cookie )
+{
+    heap_free( cookie->name );
+    heap_free( cookie->value );
+    heap_free( cookie->path );
+    heap_free( cookie );
+}
+
+static void delete_cookie( cookie_t *cookie )
+{
+    list_remove( &cookie->entry );
+    free_cookie( cookie );
+}
+
+static BOOL add_cookie( cookie_t *cookie, const WCHAR *domain_name, const WCHAR *path )
+{
+    domain_t *domain = NULL;
+    cookie_t *old_cookie;
+    struct list *item;
+    WCHAR *p;
+
+    LIST_FOR_EACH(item, &domain_list)
+    {
+        domain = LIST_ENTRY(item, domain_t, entry);
+        if (match_domain( domain_name, domain, FALSE )) break;
+        domain = NULL;
+    }
+
+    if (!domain) domain = add_domain( domain_name );
+    if ((old_cookie = find_cookie( domain, path, cookie->name ))) delete_cookie( old_cookie );
+
+    if (!(cookie->path = strdupW( path ))) return FALSE;
+    if ((p = strrchrW( cookie->path, '/' )) && p != cookie->path && !p[1]) *p = 0;
+ 
+    list_add_tail( &domain->cookies, &cookie->entry );
+
+    TRACE("%s / %s <- %s = %s\n", debugstr_w(domain_name), debugstr_w(cookie->path),
+          debugstr_w(cookie->name), debugstr_w(cookie->value));
+    return TRUE;
+}
+
+static cookie_t *parse_cookie( const WCHAR *string )
+{
+    cookie_t *cookie;
+    const WCHAR *p;
+    int len;
+
+    if (!(cookie = heap_alloc_zero( sizeof(cookie) ))) return NULL;
+
+    list_init( &cookie->entry );
+
+    if (!(p = strchrW( string, '=' )))
+    {
+        WARN("no '=' in %s\n", debugstr_w(string));
+        return NULL;
+    }
+    if (p == string)
+    {
+        WARN("empty cookie name in %s\n", debugstr_w(string));
+        return NULL;
+    }
+    len = p - string;
+    if (!(cookie->name = heap_alloc( (len + 1) * sizeof(WCHAR) )))
+    {
+        heap_free( cookie );
+        return NULL;
+    }
+    memcpy( cookie->name, string, len * sizeof(WCHAR) );
+    cookie->name[len] = 0;
+
+    p++; /* skip '=' */
+    while (*p == ' ') p++;
+
+    len = strlenW( p );
+    if (!(cookie->value = heap_alloc( (len + 1) * sizeof(WCHAR) )))
+    {
+        free_cookie( cookie );
+        return NULL;
+    }
+    memcpy( cookie->value, p, len * sizeof(WCHAR) );
+    cookie->value[len] = 0;
+
+    return cookie;
+}
+
+BOOL set_cookies( const WCHAR *cookies, WCHAR *hostname, WCHAR *path )
+{
+    static const WCHAR pathW[] = {'P','a','t','h',0};
+    static const WCHAR domainW[] = {'D','o','m','a','i','n',0};
+
+    BOOL ret;
+    WCHAR *buffer, *p, *q, *r;
+    WCHAR *cookie_domain = hostname, *cookie_path = path;
+    cookie_t *cookie;
+    int len;
+
+    len = strlenW( cookies );
+    if (!(buffer = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
+    strcpyW( buffer, cookies );
+
+    p = buffer;
+    while (*p && *p != ';') p++;
+    if (*p == ';') *p++ = 0;
+    if (!(cookie = parse_cookie( buffer )))
+    {
+        heap_free( buffer );
+        return FALSE;
+    }
+    if (!*p) goto done;
+    if ((q = strstrW( p, domainW ))) /* FIXME: do real attribute parsing */
+    {
+        while (*q && *q != '=') q++;
+        if (!*q) goto done;
+
+        r = ++q;
+        while (*r && *r != ';') r++;
+        len = r - q;
+
+        if (!(cookie_domain = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto done;
+        memcpy( cookie_domain, q, len * sizeof(WCHAR) );
+        cookie_domain[len] = 0;
+        
+    }
+    if ((q = strstrW( p, pathW )))
+    {
+        while (*q && *q != '=') q++;
+        if (!*q) goto done;
+
+        r = ++q;
+        while (*r && *r != ';') r++;
+        len = r - q;
+
+        if (!(cookie_path = heap_alloc( (len + 1) * sizeof(WCHAR) ))) goto done;
+        memcpy( cookie_path, q, len * sizeof(WCHAR) );
+        cookie_path[len] = 0;
+    }
+
+done:
+    if (!(ret = add_cookie( cookie, cookie_domain, cookie_path ))) free_cookie( cookie );
+    heap_free( buffer );
+    return ret;
+}
+
+BOOL add_cookie_headers( request_t *request )
+{
+    struct list *domain_cursor;
+
+    LIST_FOR_EACH(domain_cursor, &domain_list)
+    {
+        domain_t *domain = LIST_ENTRY(domain_cursor, domain_t, entry);
+        if (match_domain( request->connect->servername, domain, TRUE ))
+        {
+            struct list *cookie_cursor;
+            TRACE("found domain %s\n", debugstr_w(domain->name));
+
+            LIST_FOR_EACH(cookie_cursor, &domain->cookies)
+            {
+                WCHAR slash[] = {'/',0}, *path;
+                cookie_t *cookie = LIST_ENTRY(cookie_cursor, cookie_t, entry);
+    
+                path = request->path ? request->path : slash;
+
+                TRACE("comparing cookie %s path %s with %s\n",
+                      debugstr_w(cookie->name), debugstr_w(cookie->path),
+                      debugstr_w(path));
+
+                if (!strcmpW( cookie->path, path ) || strstrW( path, cookie->path ) == path)
+                {
+                    const WCHAR fmt[] = {'C','o','o','k','i','e',':',' ','%','s','=','%','s',0};
+                    int len;
+                    WCHAR *header;
+
+                    len = strlenW( cookie->name ) + strlenW( fmt ) + strlenW( cookie->value );
+                    if (!(header = heap_alloc( (len + 1) * sizeof(WCHAR) ))) return FALSE;
+
+                    sprintfW( header, fmt, cookie->name, cookie->value );
+
+                    TRACE("%s\n", debugstr_w(header));
+                    add_request_headers( request, header, len, WINHTTP_ADDREQ_FLAG_ADD );
+                    heap_free( header );
+                }
+            }
+        }
+    }
+    return TRUE;
+}
diff --git a/dlls/winhttp/request.c b/dlls/winhttp/request.c
index b62a0e3..729d2ad 100644
--- a/dlls/winhttp/request.c
+++ b/dlls/winhttp/request.c
@@ -404,7 +404,7 @@ static BOOL process_header( request_t *request, LPCWSTR field, LPCWSTR value, DW
     return TRUE;
 }
 
-static BOOL add_request_headers( request_t *request, LPCWSTR headers, DWORD len, DWORD flags )
+BOOL add_request_headers( request_t *request, LPCWSTR headers, DWORD len, DWORD flags )
 {
     BOOL ret = FALSE;
     WCHAR *buffer, *p, *q;
@@ -833,6 +833,11 @@ static BOOL send_request( request_t *request, LPCWSTR headers, DWORD headers_len
         TRACE("failed to add request headers\n");
         return FALSE;
     }
+    if (!(request->hdr.flags & WINHTTP_DISABLE_COOKIES) && !add_cookie_headers( request ))
+    {
+        WARN("failed to add cookie headers\n");
+        return FALSE;
+    }
 
     if (!(ret = open_connection( request ))) goto end;
     if (!(req = build_request_string( request ))) goto end;
@@ -1281,8 +1286,7 @@ static void drain_content( request_t *request )
     }
 }
 
-/* copy cookies from response headers to request headers */
-static void add_cookies( request_t *request )
+static void record_cookies( request_t *request )
 {
     unsigned int i;
 
@@ -1290,7 +1294,7 @@ static void add_cookies( request_t *request )
     {
         if (!strcmpiW( request->headers[i].field, attr_set_cookie ) && !request->headers[i].is_request)
         {
-            process_header( request, attr_cookie, request->headers[i].value, WINHTTP_ADDREQ_FLAG_ADD, TRUE );
+            set_cookies( request->headers[i].value, request->connect->servername, request->path );
         }
     }
 }
@@ -1313,13 +1317,14 @@ static BOOL receive_response( request_t *request, BOOL async )
         if (!query_headers( request, query, NULL, &request->content_length, &size, NULL ))
             request->content_length = ~0UL;
 
+        if (!(request->hdr.disable_flags & WINHTTP_DISABLE_COOKIES)) record_cookies( request );
+
         if (status == 301 || status == 302)
         {
             if (request->hdr.disable_flags & WINHTTP_DISABLE_REDIRECTS) break;
-            drain_content( request );
 
+            drain_content( request );
             if (!(ret = handle_redirect( request ))) break;
-            if (!(request->hdr.disable_flags & WINHTTP_DISABLE_COOKIES)) add_cookies( request );
 
             clear_response_headers( request );
             ret = send_request( request, NULL, 0, NULL, 0, 0, 0, FALSE ); /* recurse synchronously */
diff --git a/dlls/winhttp/tests/winhttp.c b/dlls/winhttp/tests/winhttp.c
index 7914805..3381370 100644
--- a/dlls/winhttp/tests/winhttp.c
+++ b/dlls/winhttp/tests/winhttp.c
@@ -668,6 +668,48 @@ static void test_request_parameter_defaults(void)
     WinHttpCloseHandle(ses);
 }
 
+static void test_cookies(void)
+{
+    static const WCHAR post[] = {'P','O','S','T',0};
+    static const WCHAR query[] = {'/','c','o','o','k','i','e','t','e','s','t','p','o','s','t','.','c','g','i',0};
+    static const WCHAR crossover[] =
+        {'c','r','o','s','s','o','v','e','r','.','c','o','d','e','w','e','a','v','e','r','s','.','c','o','m',0};
+    static const WCHAR content_type[] =
+        {'C','o','n','t','e','n','t','-','T','y','p','e',':',' ','a','p','p','l','i','c','a','t','i','o','n',
+         '/','x','-','w','w','w','-','f','o','r','m','-','u','r','l','e','n','c','o','d','e','d',0};
+    static char post_data[] = "next=1";
+
+    HANDLE ses, con, req;
+    DWORD size, status;
+    BOOL ret, enable = TRUE;
+
+    ret = WinHttpSetOption(NULL, WINHTTP_OPTION_ENABLETRACING, &enable, sizeof(enable));
+
+    ses = WinHttpOpen(test_useragent, 0, NULL, NULL, 0);
+    ok(ses != NULL, "failed to open session %u\n", GetLastError());
+
+    con = WinHttpConnect(ses, crossover, 0, 0);
+    ok(con != NULL, "failed to open a connection %u\n", GetLastError());
+
+    req = WinHttpOpenRequest(con, post, query, NULL, NULL, NULL, 0);
+    ok(req != NULL, "failed to open a request %u\n", GetLastError());
+
+    ret = WinHttpSendRequest(req, content_type, ~0UL, post_data, sizeof(post_data), sizeof(post_data), 0);
+    ok(ret, "failed to send request %u\n", GetLastError());
+
+    ret = WinHttpReceiveResponse(req, NULL);
+    ok(ret, "failed to receive response %u\n", GetLastError());
+
+    size = sizeof(status);
+    ret = WinHttpQueryHeaders(req, WINHTTP_QUERY_STATUS_CODE | WINHTTP_QUERY_FLAG_NUMBER, NULL, &status, &size, NULL);
+    ok(ret, "failed unexpectedly %u\n", GetLastError());
+    ok(status == 200, "request failed unexpectedly %u\n", status);
+
+    WinHttpCloseHandle(req);
+    WinHttpCloseHandle(con);
+    WinHttpCloseHandle(ses);
+}
+
 START_TEST (winhttp)
 {
     test_OpenRequest();
@@ -677,4 +719,5 @@ START_TEST (winhttp)
     test_WinHttpAddHeaders();
     test_secure_connection();
     test_request_parameter_defaults();
+    test_cookies();
 }
diff --git a/dlls/winhttp/winhttp_private.h b/dlls/winhttp/winhttp_private.h
index 6508b38..0164dc3 100644
--- a/dlls/winhttp/winhttp_private.h
+++ b/dlls/winhttp/winhttp_private.h
@@ -190,6 +190,10 @@ BOOL netconn_secure_connect( netconn_t * );
 BOOL netconn_send( netconn_t *, const void *, size_t, int, int * );
 const void *netconn_get_certificate( netconn_t * );
 
+BOOL set_cookies( const WCHAR *, WCHAR *, WCHAR * );
+BOOL add_cookie_headers( request_t * );
+BOOL add_request_headers( request_t *, LPCWSTR, DWORD, DWORD );
+
 static inline void *heap_alloc( SIZE_T size )
 {
     return HeapAlloc( GetProcessHeap(), 0, size );

--------------070900010703090807050009
Content-Type: text/x-patch;
 name="winhttp_flags.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="winhttp_flags.diff"

diff --git a/dlls/winhttp/session.c b/dlls/winhttp/session.c
index 927c347..8625c0c 100644
--- a/dlls/winhttp/session.c
+++ b/dlls/winhttp/session.c
@@ -303,7 +303,7 @@ static BOOL request_set_option( object_header_t *hdr, DWORD option, LPVOID buffe
         DWORD disable = *(DWORD *)buffer;
 
         TRACE("0x%x\n", disable);
-        hdr->disable_flags &= disable;
+        hdr->disable_flags = disable;
         return TRUE;
     }
     case WINHTTP_OPTION_AUTOLOGON_POLICY:

--------------070900010703090807050009
Content-Type: text/x-patch;
 name="winhttp_reset_content_length.diff"
Content-Transfer-Encoding: 7bit
Content-Disposition: inline;
 filename="winhttp_reset_content_length.diff"

4a8dbb8069718098d0b7f34de9badf42e40c002a
diff --git a/dlls/winhttp/request.c b/dlls/winhttp/request.c
index c149aca..b62a0e3 100644
--- a/dlls/winhttp/request.c
+++ b/dlls/winhttp/request.c
@@ -1226,6 +1226,13 @@ static BOOL receive_data_chunked( request_t *request, void *buffer, DWORD size,
     return TRUE;
 }
 
+static void finished_reading( request_t *request )
+{
+    /* FIXME: close connection here if necessary */
+    request->content_length = ~0UL;
+    request->content_read = 0;
+}
+
 static BOOL read_data( request_t *request, void *buffer, DWORD to_read, DWORD *read, BOOL async )
 {
     static const WCHAR chunked[] = {'c','h','u','n','k','e','d',0};
@@ -1253,7 +1260,11 @@ static BOOL read_data( request_t *request, void *buffer, DWORD to_read, DWORD *r
             send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_REQUEST_ERROR, &result, sizeof(result) );
         }
     }
-    if (ret && read) *read = num_bytes;
+    if (ret)
+    {
+        if (read) *read = num_bytes;
+        if (!num_bytes) finished_reading( request );
+    }
     return ret;
 }
 
@@ -1263,6 +1274,7 @@ static void drain_content( request_t *request )
     DWORD bytes_read;
     char buffer[2048];
 
+    if (!request->content_length) return;
     for (;;)
     {
         if (!read_data( request, buffer, sizeof(buffer), &bytes_read, FALSE ) || !bytes_read) return;
@@ -1523,7 +1535,7 @@ static BOOL write_data( request_t *request, LPCVOID buffer, DWORD to_write, LPDW
 
     if (async)
     {
-        if (ret) send_callback( &request->hdr, WINHTTP_CALLBACK_FLAG_WRITE_COMPLETE, &num_bytes, sizeof(DWORD) );
+        if (ret) send_callback( &request->hdr, WINHTTP_CALLBACK_STATUS_WRITE_COMPLETE, &num_bytes, sizeof(DWORD) );
         else
         {
             WINHTTP_ASYNC_RESULT result;

--------------070900010703090807050009--



More information about the wine-devel mailing list