1 /* GIO - GLib Input, Output and Streaming Library
3 * Copyright (C) 2006-2007 Red Hat, Inc.
4 * Copyright (C) 2008 Novell, Inc.
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General
17 * Public License along with this library; if not, write to the
18 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
19 * Boston, MA 02111-1307, USA.
21 * Author: Alexander Larsson <alexl@redhat.com>
22 * Author: Tor Lillqvist <tml@novell.com>
29 #include "gcancellable.h"
31 #include "gwinhttpfileinputstream.h"
36 struct _GWinHttpFileInputStream
38 GFileInputStream parent_instance;
41 gboolean request_sent;
46 struct _GWinHttpFileInputStreamClass
48 GFileInputStreamClass parent_class;
51 #define g_winhttp_file_input_stream_get_type _g_winhttp_file_input_stream_get_type
52 G_DEFINE_TYPE (GWinHttpFileInputStream, g_winhttp_file_input_stream, G_TYPE_FILE_INPUT_STREAM);
54 static gssize g_winhttp_file_input_stream_read (GInputStream *stream,
57 GCancellable *cancellable,
61 g_winhttp_file_input_stream_finalize (GObject *object)
63 GWinHttpFileInputStream *winhttp_stream;
65 winhttp_stream = G_WINHTTP_FILE_INPUT_STREAM (object);
67 if (winhttp_stream->request != NULL)
68 G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (winhttp_stream->request);
69 if (winhttp_stream->connection != NULL)
70 G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (winhttp_stream->connection);
72 G_OBJECT_CLASS (g_winhttp_file_input_stream_parent_class)->finalize (object);
76 g_winhttp_file_input_stream_class_init (GWinHttpFileInputStreamClass *klass)
78 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
79 GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
81 gobject_class->finalize = g_winhttp_file_input_stream_finalize;
83 stream_class->read_fn = g_winhttp_file_input_stream_read;
87 g_winhttp_file_input_stream_init (GWinHttpFileInputStream *info)
92 * g_winhttp_file_input_stream_new:
93 * @file: the GWinHttpFile being read
94 * @connection: handle to the HTTP connection, as from WinHttpConnect()
95 * @request: handle to the HTTP request, as from WinHttpOpenRequest
97 * Returns: #GFileInputStream for the given request
100 _g_winhttp_file_input_stream_new (GWinHttpFile *file,
101 HINTERNET connection,
104 GWinHttpFileInputStream *stream;
106 stream = g_object_new (G_TYPE_WINHTTP_FILE_INPUT_STREAM, NULL);
109 stream->request_sent = FALSE;
110 stream->connection = connection;
111 stream->request = request;
113 return G_FILE_INPUT_STREAM (stream);
117 g_winhttp_file_input_stream_read (GInputStream *stream,
120 GCancellable *cancellable,
123 GWinHttpFileInputStream *winhttp_stream = G_WINHTTP_FILE_INPUT_STREAM (stream);
126 if (!winhttp_stream->request_sent)
128 if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpSendRequest
129 (winhttp_stream->request,
135 _g_winhttp_set_error (error, GetLastError (), "GET request");
140 if (!_g_winhttp_response (winhttp_stream->file->vfs,
141 winhttp_stream->request,
146 winhttp_stream->request_sent = TRUE;
149 if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpReadData
150 (winhttp_stream->request, buffer, count, &bytes_read))
152 _g_winhttp_set_error (error, GetLastError (), "GET request");