gio/: fully remove gioalias hacks
[platform/upstream/glib.git] / gio / win32 / gwinhttpfileinputstream.c
1 /* GIO - GLib Input, Output and Streaming Library
2  *
3  * Copyright (C) 2006-2007 Red Hat, Inc.
4  * Copyright (C) 2008 Novell, Inc.
5  *
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.
10  *
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.
15  *
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.
20  *
21  * Author: Alexander Larsson <alexl@redhat.com>
22  * Author: Tor Lillqvist <tml@novell.com>
23  */
24
25 #include "config.h"
26
27 #include <glib.h>
28
29 #include "gcancellable.h"
30 #include "gioerror.h"
31 #include "gwinhttpfileinputstream.h"
32 #include "glibintl.h"
33
34 struct _GWinHttpFileInputStream
35 {
36   GFileInputStream parent_instance;
37
38   GWinHttpFile *file;
39   gboolean request_sent;
40   HINTERNET connection;
41   HINTERNET request;
42 };
43
44 struct _GWinHttpFileInputStreamClass
45 {
46   GFileInputStreamClass parent_class;
47 };
48
49 #define g_winhttp_file_input_stream_get_type _g_winhttp_file_input_stream_get_type
50 G_DEFINE_TYPE (GWinHttpFileInputStream, g_winhttp_file_input_stream, G_TYPE_FILE_INPUT_STREAM);
51
52 static gssize g_winhttp_file_input_stream_read (GInputStream    *stream,
53                                                 void            *buffer,
54                                                 gsize            count,
55                                                 GCancellable    *cancellable,
56                                                 GError         **error);
57
58 static gboolean g_winhttp_file_input_stream_close (GInputStream  *stream,
59                                                    GCancellable  *cancellable,
60                                                    GError       **error);
61
62 static void
63 g_winhttp_file_input_stream_finalize (GObject *object)
64 {
65   GWinHttpFileInputStream *winhttp_stream;
66
67   winhttp_stream = G_WINHTTP_FILE_INPUT_STREAM (object);
68
69   if (winhttp_stream->request != NULL)
70     G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (winhttp_stream->request);
71   if (winhttp_stream->connection != NULL)
72     G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (winhttp_stream->connection);
73
74   g_object_unref (winhttp_stream->file);
75   winhttp_stream->file = NULL;
76
77   G_OBJECT_CLASS (g_winhttp_file_input_stream_parent_class)->finalize (object);
78 }
79
80 static void
81 g_winhttp_file_input_stream_class_init (GWinHttpFileInputStreamClass *klass)
82 {
83   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
84   GInputStreamClass *stream_class = G_INPUT_STREAM_CLASS (klass);
85
86   gobject_class->finalize = g_winhttp_file_input_stream_finalize;
87
88   stream_class->read_fn = g_winhttp_file_input_stream_read;
89   stream_class->close_fn = g_winhttp_file_input_stream_close;
90 }
91
92 static void
93 g_winhttp_file_input_stream_init (GWinHttpFileInputStream *info)
94 {
95 }
96
97 /*
98  * g_winhttp_file_input_stream_new:
99  * @file: the GWinHttpFile being read
100  * @connection: handle to the HTTP connection, as from WinHttpConnect()
101  * @request: handle to the HTTP request, as from WinHttpOpenRequest
102  *
103  * Returns: #GFileInputStream for the given request
104  */
105 GFileInputStream *
106 _g_winhttp_file_input_stream_new (GWinHttpFile *file,
107                                   HINTERNET     connection,
108                                   HINTERNET     request)
109 {
110   GWinHttpFileInputStream *stream;
111
112   stream = g_object_new (G_TYPE_WINHTTP_FILE_INPUT_STREAM, NULL);
113
114   stream->file = g_object_ref (file);
115   stream->request_sent = FALSE;
116   stream->connection = connection;
117   stream->request = request;
118
119   return G_FILE_INPUT_STREAM (stream);
120 }
121
122 static gssize
123 g_winhttp_file_input_stream_read (GInputStream  *stream,
124                                   void          *buffer,
125                                   gsize          count,
126                                   GCancellable  *cancellable,
127                                   GError       **error)
128 {
129   GWinHttpFileInputStream *winhttp_stream = G_WINHTTP_FILE_INPUT_STREAM (stream);
130   DWORD bytes_read;
131
132   if (!winhttp_stream->request_sent)
133     {
134       if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpSendRequest
135           (winhttp_stream->request,
136            NULL, 0,
137            NULL, 0,
138            0,
139            0))
140         {
141           _g_winhttp_set_error (error, GetLastError (), "GET request");
142
143           return -1;
144         }
145
146       if (!_g_winhttp_response (winhttp_stream->file->vfs,
147                                 winhttp_stream->request,
148                                 error,
149                                 "GET request"))
150         return -1;
151
152       winhttp_stream->request_sent = TRUE;
153     }
154
155   if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpReadData
156       (winhttp_stream->request, buffer, count, &bytes_read))
157     {
158       _g_winhttp_set_error (error, GetLastError (), "GET request");
159
160       return -1;
161     }
162
163   return bytes_read;
164 }
165
166 static gboolean 
167 g_winhttp_file_input_stream_close (GInputStream         *stream,
168                                    GCancellable         *cancellable,
169                                    GError              **error)
170 {
171   GWinHttpFileInputStream *winhttp_stream = G_WINHTTP_FILE_INPUT_STREAM (stream);
172
173   if (winhttp_stream->connection != NULL)
174     G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (winhttp_stream->connection);
175   winhttp_stream->connection = NULL;
176   return TRUE;
177 }