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