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 |
---|
3 | Virtual File System. |
---|
4 | |
---|
5 | Copyright (C) 1999 Free Software Foundation |
---|
6 | |
---|
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. |
---|
11 | |
---|
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. |
---|
16 | |
---|
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. |
---|
21 | |
---|
22 | Author: Ian McKellar <yakk@yakk.net> |
---|
23 | */ |
---|
24 | |
---|
25 | #include <config.h> |
---|
26 | |
---|
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> |
---|
32 | #include <stdio.h> |
---|
33 | #include <stdlib.h> |
---|
34 | #include <string.h> |
---|
35 | #include <unistd.h> |
---|
36 | |
---|
37 | enum { |
---|
38 | SSL, SOCKET, SOCKETBUFFER |
---|
39 | } abstraction = SOCKETBUFFER; |
---|
40 | |
---|
41 | static void |
---|
42 | show_result (GnomeVFSResult result, |
---|
43 | const gchar *what, |
---|
44 | const gchar *host, |
---|
45 | gint port) |
---|
46 | { |
---|
47 | fprintf (stderr, "%s `%s:%d': %s\n", |
---|
48 | what, host, port, gnome_vfs_result_to_string (result)); |
---|
49 | if (result != GNOME_VFS_OK) |
---|
50 | exit (1); |
---|
51 | } |
---|
52 | |
---|
53 | #define HTTP_REQUEST "GET / HTTP/1.0\r\n\r\n" |
---|
54 | |
---|
55 | int |
---|
56 | main (int argc, char **argv) |
---|
57 | { |
---|
58 | GnomeVFSResult result = GNOME_VFS_OK; |
---|
59 | gchar buffer[1024]; |
---|
60 | gchar *host; |
---|
61 | gint port; |
---|
62 | GnomeVFSFileSize bytes_read; |
---|
63 | GnomeVFSSSL *ssl = NULL; |
---|
64 | GnomeVFSSocket *socket = NULL; |
---|
65 | GnomeVFSSocketBuffer *socketbuffer = NULL; |
---|
66 | |
---|
67 | if (argc != 3) { |
---|
68 | printf ("Usage: %s <host> <port>\n", argv[0]); |
---|
69 | return 1; |
---|
70 | } |
---|
71 | |
---|
72 | host = argv[1]; |
---|
73 | port = atoi (argv[2]); |
---|
74 | |
---|
75 | if (port <= 0) { |
---|
76 | printf ("Invalid port\n"); |
---|
77 | return 1; |
---|
78 | } |
---|
79 | |
---|
80 | if (! gnome_vfs_init ()) { |
---|
81 | fprintf (stderr, "Cannot initialize gnome-vfs.\n"); |
---|
82 | return 1; |
---|
83 | } |
---|
84 | |
---|
85 | switch (abstraction) { |
---|
86 | case SOCKETBUFFER: |
---|
87 | g_print ("Testing GnomeVFSSocketBuffer"); |
---|
88 | case SOCKET: |
---|
89 | g_print (" and GnomeVFSSocket"); |
---|
90 | case SSL: |
---|
91 | g_print (" and GnomeVFSSSL"); |
---|
92 | } |
---|
93 | g_print (".\n"); |
---|
94 | |
---|
95 | result = gnome_vfs_ssl_create (&ssl, host, port, NULL); |
---|
96 | |
---|
97 | show_result (result, "ssl_create", host, port); |
---|
98 | |
---|
99 | if (ssl == NULL) { |
---|
100 | fprintf (stderr, "couln't connect\n"); |
---|
101 | return -1; |
---|
102 | } |
---|
103 | |
---|
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"); |
---|
108 | return -1; |
---|
109 | } |
---|
110 | |
---|
111 | if (abstraction == SOCKETBUFFER) { |
---|
112 | socketbuffer = gnome_vfs_socket_buffer_new (socket); |
---|
113 | if (socketbuffer == NULL) { |
---|
114 | fprintf (stderr, |
---|
115 | "couldn't create socketbuffer object\n"); |
---|
116 | return -1; |
---|
117 | } |
---|
118 | } |
---|
119 | } |
---|
120 | |
---|
121 | switch (abstraction) { |
---|
122 | case SSL: |
---|
123 | result = gnome_vfs_ssl_write (ssl, HTTP_REQUEST, |
---|
124 | strlen(HTTP_REQUEST), &bytes_read, |
---|
125 | NULL); |
---|
126 | break; |
---|
127 | case SOCKET: |
---|
128 | result = gnome_vfs_socket_write (socket, HTTP_REQUEST, |
---|
129 | strlen(HTTP_REQUEST), &bytes_read, |
---|
130 | NULL); |
---|
131 | break; |
---|
132 | case SOCKETBUFFER: |
---|
133 | result = gnome_vfs_socket_buffer_write (socketbuffer, |
---|
134 | HTTP_REQUEST, strlen(HTTP_REQUEST), |
---|
135 | &bytes_read, |
---|
136 | NULL); |
---|
137 | gnome_vfs_socket_buffer_flush (socketbuffer, NULL); |
---|
138 | break; |
---|
139 | } |
---|
140 | |
---|
141 | show_result (result, "write", host, port); |
---|
142 | |
---|
143 | while( result==GNOME_VFS_OK ) { |
---|
144 | switch (abstraction) { |
---|
145 | case SSL: |
---|
146 | result = gnome_vfs_ssl_read (ssl, buffer, |
---|
147 | sizeof buffer - 1, &bytes_read, |
---|
148 | NULL); |
---|
149 | break; |
---|
150 | case SOCKET: |
---|
151 | result = gnome_vfs_socket_read (socket, buffer, |
---|
152 | sizeof buffer - 1, &bytes_read, |
---|
153 | NULL); |
---|
154 | break; |
---|
155 | case SOCKETBUFFER: |
---|
156 | result = gnome_vfs_socket_buffer_read ( |
---|
157 | socketbuffer, buffer, |
---|
158 | sizeof buffer - 1, &bytes_read, |
---|
159 | NULL); |
---|
160 | break; |
---|
161 | } |
---|
162 | show_result (result, "read", host, port); |
---|
163 | |
---|
164 | buffer[bytes_read] = 0; |
---|
165 | write (1,buffer,bytes_read); |
---|
166 | if(!bytes_read) break; |
---|
167 | } |
---|
168 | |
---|
169 | switch (abstraction) { |
---|
170 | case SSL: |
---|
171 | gnome_vfs_ssl_destroy (ssl, NULL); |
---|
172 | break; |
---|
173 | case SOCKET: |
---|
174 | gnome_vfs_socket_close (socket, NULL); |
---|
175 | break; |
---|
176 | case SOCKETBUFFER: |
---|
177 | gnome_vfs_socket_buffer_destroy (socketbuffer, TRUE, NULL); |
---|
178 | break; |
---|
179 | } |
---|
180 | |
---|
181 | return 0; |
---|
182 | } |
---|