Blame gio/win32/gwinhttpfileoutputstream.c

Packit ae235b
/* GIO - GLib Input, Output and Streaming Library
Packit ae235b
 *
Packit ae235b
 * Copyright (C) 2006-2007 Red Hat, Inc.
Packit ae235b
 * Copyright (C) 2008 Novell, Inc.
Packit ae235b
 *
Packit ae235b
 * This library is free software; you can redistribute it and/or
Packit ae235b
 * modify it under the terms of the GNU Lesser General Public
Packit ae235b
 * License as published by the Free Software Foundation; either
Packit ae235b
 * version 2.1 of the License, or (at your option) any later version.
Packit ae235b
 *
Packit ae235b
 * This library is distributed in the hope that it will be useful,
Packit ae235b
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
Packit ae235b
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
Packit ae235b
 * Lesser General Public License for more details.
Packit ae235b
 *
Packit ae235b
 * You should have received a copy of the GNU Lesser General
Packit ae235b
 * Public License along with this library; if not, see <http://www.gnu.org/licenses/>.
Packit ae235b
 *
Packit ae235b
 * Author: Alexander Larsson <alexl@redhat.com>
Packit ae235b
 * Author: Tor Lillqvist <tml@novell.com>
Packit ae235b
 */
Packit ae235b
Packit ae235b
#include "config.h"
Packit ae235b
Packit ae235b
#include <glib.h>
Packit ae235b
Packit ae235b
#include "gio/gcancellable.h"
Packit ae235b
#include "gio/gioerror.h"
Packit ae235b
#include "gwinhttpfileoutputstream.h"
Packit ae235b
#include "glibintl.h"
Packit ae235b
Packit ae235b
struct _GWinHttpFileOutputStream
Packit ae235b
{
Packit ae235b
  GFileOutputStream parent_instance;
Packit ae235b
Packit ae235b
  GWinHttpFile *file;
Packit ae235b
  HINTERNET connection;
Packit ae235b
  goffset offset;
Packit ae235b
};
Packit ae235b
Packit ae235b
struct _GWinHttpFileOutputStreamClass
Packit ae235b
{
Packit ae235b
  GFileOutputStreamClass parent_class;
Packit ae235b
};
Packit ae235b
Packit ae235b
#define g_winhttp_file_output_stream_get_type _g_winhttp_file_output_stream_get_type
Packit ae235b
G_DEFINE_TYPE (GWinHttpFileOutputStream, g_winhttp_file_output_stream, G_TYPE_FILE_OUTPUT_STREAM)
Packit ae235b
Packit ae235b
static gssize     g_winhttp_file_output_stream_write      (GOutputStream     *stream,
Packit ae235b
                                                           const void        *buffer,
Packit ae235b
                                                           gsize              count,
Packit ae235b
                                                           GCancellable      *cancellable,
Packit ae235b
                                                           GError           **error);
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_winhttp_file_output_stream_finalize (GObject *object)
Packit ae235b
{
Packit ae235b
  GWinHttpFileOutputStream *winhttp_stream;
Packit ae235b
Packit ae235b
  winhttp_stream = G_WINHTTP_FILE_OUTPUT_STREAM (object);
Packit ae235b
Packit ae235b
  if (winhttp_stream->connection != NULL)
Packit ae235b
    G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (winhttp_stream->connection);
Packit ae235b
Packit ae235b
  G_OBJECT_CLASS (g_winhttp_file_output_stream_parent_class)->finalize (object);
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_winhttp_file_output_stream_class_init (GWinHttpFileOutputStreamClass *klass)
Packit ae235b
{
Packit ae235b
  GObjectClass *gobject_class = G_OBJECT_CLASS (klass);
Packit ae235b
  GOutputStreamClass *stream_class = G_OUTPUT_STREAM_CLASS (klass);
Packit ae235b
Packit ae235b
  gobject_class->finalize = g_winhttp_file_output_stream_finalize;
Packit ae235b
Packit ae235b
  stream_class->write_fn = g_winhttp_file_output_stream_write;
Packit ae235b
}
Packit ae235b
Packit ae235b
static void
Packit ae235b
g_winhttp_file_output_stream_init (GWinHttpFileOutputStream *info)
Packit ae235b
{
Packit ae235b
}
Packit ae235b
Packit ae235b
/*
Packit ae235b
 * g_winhttp_file_output_stream_new:
Packit ae235b
 * @file: the GWinHttpFile being read
Packit ae235b
 * @connection: handle to the HTTP connection, as from WinHttpConnect()
Packit ae235b
 * @request: handle to the HTTP request, as from WinHttpOpenRequest
Packit ae235b
 *
Packit ae235b
 * Returns: #GFileOutputStream for the given request
Packit ae235b
 */
Packit ae235b
GFileOutputStream *
Packit ae235b
_g_winhttp_file_output_stream_new (GWinHttpFile *file,
Packit ae235b
                                   HINTERNET     connection)
Packit ae235b
{
Packit ae235b
  GWinHttpFileOutputStream *stream;
Packit ae235b
Packit ae235b
  stream = g_object_new (G_TYPE_WINHTTP_FILE_OUTPUT_STREAM, NULL);
Packit ae235b
Packit ae235b
  stream->file = file;
Packit ae235b
  stream->connection = connection;
Packit ae235b
  stream->offset = 0;
Packit ae235b
Packit ae235b
  return G_FILE_OUTPUT_STREAM (stream);
Packit ae235b
}
Packit ae235b
Packit ae235b
static gssize
Packit ae235b
g_winhttp_file_output_stream_write (GOutputStream  *stream,
Packit ae235b
                                    const void     *buffer,
Packit ae235b
                                    gsize           count,
Packit ae235b
                                    GCancellable   *cancellable,
Packit ae235b
                                    GError        **error)
Packit ae235b
{
Packit ae235b
  GWinHttpFileOutputStream *winhttp_stream = G_WINHTTP_FILE_OUTPUT_STREAM (stream);
Packit ae235b
  HINTERNET request;
Packit ae235b
  char *headers;
Packit ae235b
  wchar_t *wheaders;
Packit ae235b
  DWORD bytes_written;
Packit ae235b
Packit ae235b
  request = G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpOpenRequest
Packit ae235b
    (winhttp_stream->connection,
Packit ae235b
     L"PUT",
Packit ae235b
     winhttp_stream->file->url.lpszUrlPath,
Packit ae235b
     NULL,
Packit ae235b
     WINHTTP_NO_REFERER,
Packit ae235b
     NULL,
Packit ae235b
     winhttp_stream->file->url.nScheme == INTERNET_SCHEME_HTTPS ? WINHTTP_FLAG_SECURE : 0);
Packit ae235b
Packit ae235b
  if (request == NULL)
Packit ae235b
    {
Packit ae235b
      _g_winhttp_set_error (error, GetLastError (), "PUT request");
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  headers = g_strdup_printf ("Content-Range: bytes %" G_GINT64_FORMAT "-%" G_GINT64_FORMAT "/*\r\n",
Packit ae235b
                             winhttp_stream->offset, winhttp_stream->offset + count);
Packit ae235b
  wheaders = g_utf8_to_utf16 (headers, -1, NULL, NULL, NULL);
Packit ae235b
  g_free (headers);
Packit ae235b
Packit ae235b
  if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpSendRequest
Packit ae235b
      (request,
Packit ae235b
       wheaders, -1,
Packit ae235b
       NULL, 0,
Packit ae235b
       count,
Packit ae235b
       0))
Packit ae235b
    {
Packit ae235b
      _g_winhttp_set_error (error, GetLastError (), "PUT request");
Packit ae235b
Packit ae235b
      G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (request);
Packit ae235b
      g_free (wheaders);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  g_free (wheaders);
Packit ae235b
Packit ae235b
  if (!G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpWriteData
Packit ae235b
      (request, buffer, count, &bytes_written))
Packit ae235b
    {
Packit ae235b
      _g_winhttp_set_error (error, GetLastError (), "PUT request");
Packit ae235b
Packit ae235b
      G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (request);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  winhttp_stream->offset += bytes_written;
Packit ae235b
Packit ae235b
  if (!_g_winhttp_response (winhttp_stream->file->vfs,
Packit ae235b
                            request,
Packit ae235b
                            error,
Packit ae235b
                            "PUT request"))
Packit ae235b
    {
Packit ae235b
      G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (request);
Packit ae235b
Packit ae235b
      return -1;
Packit ae235b
    }
Packit ae235b
Packit ae235b
  G_WINHTTP_VFS_GET_CLASS (winhttp_stream->file->vfs)->funcs->pWinHttpCloseHandle (request);
Packit ae235b
Packit ae235b
  return bytes_written;
Packit ae235b
}