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 "gwinhttpfileoutputstream.h"
36 struct _GWinHttpFileOutputStream
38 GFileOutputStream parent_instance;
45 struct _GWinHttpFileOutputStreamClass
47 GFileOutputStreamClass parent_class;
50 #define g_winhttp_file_output_stream_get_type _g_winhttp_file_output_stream_get_type
51 G_DEFINE_TYPE (GWinHttpFileOutputStream, g_winhttp_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
53 static gssize g_winhttp_file_output_stream_write (GOutputStream *stream,
56 GCancellable *cancellable,
60 g_winhttp_file_output_stream_finalize (GObject *object)
62 GWinHttpFileOutputStream *winhttp_stream;
64 winhttp_stream = G_WINHTTP_FILE_OUTPUT_STREAM (object);
66 if (winhttp_stream->connection != NULL)
67 G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpCloseHandle (winhttp_stream->connection);
69 G_OBJECT_CLASS (g_winhttp_file_output_stream_parent_class)->finalize (object);
73 g_winhttp_file_output_stream_class_init (GWinHttpFileOutputStreamClass *klass)
75 GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
76 GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
78 gobject_class->finalize = g_winhttp_file_output_stream_finalize;
80 stream_class->write_fn = g_winhttp_file_output_stream_write;
84 g_winhttp_file_output_stream_init (GWinHttpFileOutputStream *info)
89 * g_winhttp_file_output_stream_new:
90 * @file: the GWinHttpFile being read
91 * @connection: handle to the HTTP connection, as from WinHttpConnect()
92 * @request: handle to the HTTP request, as from WinHttpOpenRequest
94 * Returns: #GFileOutputStream for the given request
97 _g_winhttp_file_output_stream_new (GWinHttpFile *file,
100 GWinHttpFileOutputStream *stream;
102 stream = g_object_new (G_TYPE_WINHTTP_FILE_OUTPUT_STREAM, NULL);
105 stream->connection = connection;
108 return G_FILE_OUTPUT_STREAM (stream);
112 g_winhttp_file_output_stream_write (GOutputStream *stream,
115 GCancellable *cancellable,
118 GWinHttpFileOutputStream *winhttp_stream = G_WINHTTP_FILE_OUTPUT_STREAM (stream);
124 request = G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpOpenRequest
125 (winhttp_stream->connection,
127 winhttp_stream->file->url.lpszUrlPath,
131 winhttp_stream->file->url.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
135 char *emsg = _g_winhttp_error_message (GetLastError ());
137 g_set_error (error, G_IO_ERROR,
145 headers = g_strdup_printf ("Content-Range: bytes %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT "/*\r\n",
146 winhttp_stream->offset, winhttp_stream->offset + count);
147 wheaders = g_utf8_to_utf16 (headers, -1, NULL, NULL, NULL);
150 if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpSendRequest
157 char *emsg = _g_winhttp_error_message (GetLastError ());
159 g_set_error (error, G_IO_ERROR,
164 G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpCloseHandle (request);
172 if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpWriteData
173 (request, buffer, count, &bytes_written))
175 char *emsg = _g_winhttp_error_message (GetLastError ());
176 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
181 G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpCloseHandle (request);
186 winhttp_stream->offset += bytes_written;
188 if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpReceiveResponse
191 char *emsg = _g_winhttp_error_message (GetLastError ());
192 g_set_error (error, G_IO_ERROR, G_IO_ERROR_FAILED,
197 G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpCloseHandle (request);
201 G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->pWinHttpCloseHandle (request);
203 return bytes_written;