Merge remote branch 'gvdb/master'
[platform/upstream/glib.git] / gio / win32 / gwinhttpfileoutputstream.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 "gio/gcancellable.h"
30 #include "gio/gioerror.h"
31 #include "gwinhttpfileoutputstream.h"
32 #include "glibintl.h"
33
34 struct _GWinHttpFileOutputStream
35 {
36   GFileOutputStream parent_instance;
37
38   GWinHttpFile *file;
39   HINTERNET connection;
40   goffset offset;
41 };
42
43 struct _GWinHttpFileOutputStreamClass
44 {
45   GFileOutputStreamClass parent_class;
46 };
47
48 #define g_winhttp_file_output_stream_get_type _g_winhttp_file_output_stream_get_type
49 G_DEFINE_TYPE (GWinHttpFileOutputStream, g_winhttp_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM);
50
51 static gssize     g_winhttp_file_output_stream_write      (GOutputStream     *stream,
52                                                            const void        *buffer,
53                                                            gsize              count,
54                                                            GCancellable      *cancellable,
55                                                            GError           **error);
56
57 static void
58 g_winhttp_file_output_stream_finalize (GObject *object)
59 {
60   GWinHttpFileOutputStream *winhttp_stream;
61
62   winhttp_stream = G_WINHTTP_FILE_OUTPUT_STREAM (object);
63
64   if (winhttp_stream->connection != NULL)
65     G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (winhttp_stream->connection);
66
67   G_OBJECT_CLASS (g_winhttp_file_output_stream_parent_class)->finalize (object);
68 }
69
70 static void
71 g_winhttp_file_output_stream_class_init (GWinHttpFileOutputStreamClass *klass)
72 {
73   GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
74   GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
75
76   gobject_class->finalize = g_winhttp_file_output_stream_finalize;
77
78   stream_class->write_fn = g_winhttp_file_output_stream_write;
79 }
80
81 static void
82 g_winhttp_file_output_stream_init (GWinHttpFileOutputStream *info)
83 {
84 }
85
86 /*
87  * g_winhttp_file_output_stream_new:
88  * @file: the GWinHttpFile being read
89  * @connection: handle to the HTTP connection, as from WinHttpConnect()
90  * @request: handle to the HTTP request, as from WinHttpOpenRequest
91  *
92  * Returns: #GFileOutputStream for the given request
93  */
94 GFileOutputStream *
95 _g_winhttp_file_output_stream_new (GWinHttpFile *file,
96                                    HINTERNET     connection)
97 {
98   GWinHttpFileOutputStream *stream;
99
100   stream = g_object_new (G_TYPE_WINHTTP_FILE_OUTPUT_STREAM, NULL);
101
102   stream->file = file;
103   stream->connection = connection;
104   stream->offset = 0;
105
106   return G_FILE_OUTPUT_STREAM (stream);
107 }
108
109 static gssize
110 g_winhttp_file_output_stream_write (GOutputStream  *stream,
111                                     const void     *buffer,
112                                     gsize           count,
113                                     GCancellable   *cancellable,
114                                     GError        **error)
115 {
116   GWinHttpFileOutputStream *winhttp_stream = G_WINHTTP_FILE_OUTPUT_STREAM (stream);
117   HINTERNET request;
118   char *headers;
119   wchar_t *wheaders;
120   DWORD bytes_written;
121
122   request = G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpOpenRequest
123     (winhttp_stream->connection,
124      L"PUT",
125      winhttp_stream->file->url.lpszUrlPath,
126      NULL,
127      WINHTTP_NO_REFERER,
128      NULL,
129      winhttp_stream->file->url.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
130
131   if (request == NULL)
132     {
133       _g_winhttp_set_error (error, GetLastError (), "PUT request");
134
135       return -1;
136     }
137
138   headers = g_strdup_printf ("Content-Range: bytes %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT "/*\r\n",
139                              winhttp_stream->offset, winhttp_stream->offset + count);
140   wheaders = g_utf8_to_utf16 (headers, -1, NULL, NULL, NULL);
141   g_free (headers);
142
143   if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpSendRequest
144       (request,
145        wheaders, -1,
146        NULL, 0,
147        count,
148        0))
149     {
150       _g_winhttp_set_error (error, GetLastError (), "PUT request");
151
152       G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (request);
153       g_free (wheaders);
154
155       return -1;
156     }
157
158   g_free (wheaders);
159
160   if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpWriteData
161       (request, buffer, count, &bytes_written))
162     {
163       _g_winhttp_set_error (error, GetLastError (), "PUT request");
164
165       G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (request);
166
167       return -1;
168     }
169
170   winhttp_stream->offset += bytes_written;
171
172   if (!_g_winhttp_response (winhttp_stream->file->vfs,
173                             request,
174                             error,
175                             "PUT request"))
176     {
177       G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (request);
178
179       return -1;
180     }
181
182   G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (request);
183
184   return bytes_written;
185 }