1 /* -*- Mode: C; indent-tabs-mode: t; c-basic-offset: 8; tab-width: 8 -*- */
2 /* test-sync.c - Test program for synchronous operation of the GNOME
5 Copyright (C) 1999 Free Software Foundation
7 The Gnome Library is free software; you can redistribute it and/or
8 modify it under the terms of the GNU Library General Public License as
9 published by the Free Software Foundation; either version 2 of the
10 License, or (at your option) any later version.
12 The Gnome Library is distributed in the hope that it will be useful,
13 but WITHOUT ANY WARRANTY; without even the implied warranty of
14 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 Library General Public License for more details.
17 You should have received a copy of the GNU Library General Public
18 License along with the Gnome Library; see the file COPYING.LIB. If not,
19 write to the Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20 Boston, MA 02111-1307, USA.
22 Author: Ian McKellar <yakk@yakk.net>
27 #include <glib/gmessages.h>
28 #include <libgnomevfs/gnome-vfs-init.h>
29 #include <libgnomevfs/gnome-vfs-socket-buffer.h>
30 #include <libgnomevfs/gnome-vfs-socket.h>
31 #include <libgnomevfs/gnome-vfs-ssl.h>
38 SSL, SOCKET, SOCKETBUFFER
39 } abstraction = SOCKETBUFFER;
42 show_result (GnomeVFSResult result,
47 fprintf (stderr, "%s `%s:%d': %s\n",
48 what, host, port, gnome_vfs_result_to_string (result));
49 if (result != GNOME_VFS_OK)
53 #define HTTP_REQUEST "GET / HTTP/1.0\r\n\r\n"
56 main (int argc, char **argv)
58 GnomeVFSResult result = GNOME_VFS_OK;
62 GnomeVFSFileSize bytes_read;
63 GnomeVFSSSL *ssl = NULL;
64 GnomeVFSSocket *socket = NULL;
65 GnomeVFSSocketBuffer *socketbuffer = NULL;
68 printf ("Usage: %s <host> <port>\n", argv[0]);
73 port = atoi (argv[2]);
76 printf ("Invalid port\n");
80 if (! gnome_vfs_init ()) {
81 fprintf (stderr, "Cannot initialize gnome-vfs.\n");
85 switch (abstraction) {
87 g_print ("Testing GnomeVFSSocketBuffer");
89 g_print (" and GnomeVFSSocket");
91 g_print (" and GnomeVFSSSL");
95 result = gnome_vfs_ssl_create (&ssl, host, port);
97 show_result (result, "ssl_create", host, port);
100 fprintf (stderr, "couln't connect\n");
104 if (abstraction >= SOCKET) {
105 socket = gnome_vfs_ssl_to_socket (ssl);
106 if (socket == NULL) {
107 fprintf (stderr, "couldn't create socket object\n");
111 if (abstraction == SOCKETBUFFER) {
112 socketbuffer = gnome_vfs_socket_buffer_new (socket);
113 if (socketbuffer == NULL) {
115 "couldn't create socketbuffer object\n");
121 switch (abstraction) {
123 result = gnome_vfs_ssl_write (ssl, HTTP_REQUEST,
124 strlen(HTTP_REQUEST), &bytes_read);
127 result = gnome_vfs_socket_write (socket, HTTP_REQUEST,
128 strlen(HTTP_REQUEST), &bytes_read);
131 result = gnome_vfs_socket_buffer_write (socketbuffer,
132 HTTP_REQUEST, strlen(HTTP_REQUEST),
134 gnome_vfs_socket_buffer_flush (socketbuffer);
138 show_result (result, "write", host, port);
140 while( result==GNOME_VFS_OK ) {
141 switch (abstraction) {
143 result = gnome_vfs_ssl_read (ssl, buffer,
144 sizeof buffer - 1, &bytes_read);
147 result = gnome_vfs_socket_read (socket, buffer,
148 sizeof buffer - 1, &bytes_read);
151 result = gnome_vfs_socket_buffer_read (
152 socketbuffer, buffer,
153 sizeof buffer - 1, &bytes_read);
156 show_result (result, "read", host, port);
158 buffer[bytes_read] = 0;
159 write (1,buffer,bytes_read);
160 if(!bytes_read) break;
163 switch (abstraction) {
165 gnome_vfs_ssl_destroy (ssl);
168 gnome_vfs_socket_close (socket);
171 gnome_vfs_socket_buffer_destroy (socketbuffer, TRUE);