1 /* Hfetch - Download a file from a web server.
2 * Copyright (C) 2000 David Helder
3 *
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2.1 of the License, or (at your option) any later version.
8 *
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
13
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the Free Software
16 * Foundation, Inc., 51 Franklin Street, Fifth Floor,
17 * Boston, MA 02110-1301 USA
18 */
19
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <glib.h>
24 #include <gnet.h>
25
26
27 #define DELIM ":/"
28
29 void usage(void);
30 void hfetch(gchar* server, gint port, gchar* filename);
31
32
33
34 int
main(int argc,char ** argv)35 main(int argc, char** argv)
36 {
37 gchar* server = NULL;
38 gchar* port_str = NULL;
39 gint port = 80;
40 gchar* filename = NULL;
41 gchar* p = NULL;
42
43 gnet_init ();
44
45 if (argc != 2)
46 {
47 usage();
48 exit(EXIT_FAILURE);
49 }
50
51 p = argv[1];
52
53 /* Strip off the prefix */
54 if (strncmp("http://", p, sizeof("http://") - 1) == 0)
55 {
56 p = &p[sizeof("http://") - 1];
57 }
58
59 /* Read in the server */
60 server = p;
61 while (*p != 0 && *p != ':' && *p != '/') ++p;
62 if ((p - server) == 0) { usage(); exit(EXIT_FAILURE); }
63 server = g_strndup(server, p - server);
64
65 /* Read in the port */
66 if (*p == ':')
67 {
68 port_str = ++p;
69 while (*p != 0 && *p != '/') ++p;
70 if ((p - port_str) == 0) { usage(); exit(EXIT_FAILURE); }
71 port_str = g_strndup(port_str, p - port_str);
72 port = atoi(port_str);
73 }
74
75 /* Read in the file */
76 if (*p == 0)
77 {
78 filename = g_strdup("/");
79 }
80 else
81 {
82 filename = g_strdup(p);
83 }
84
85 /* g_print ("url = %s\n", argv[1]); */
86 /* g_print ("server = %s\n", server); */
87 /* g_print ("port = %d\n", port); */
88 /* g_print ("file = %s\n", filename); */
89
90
91 hfetch(server, port, filename);
92
93 g_free(server);
94 g_free(port_str);
95 g_free(filename);
96
97 return 0;
98 }
99
100
101 void
usage(void)102 usage(void)
103 {
104 g_print ("usage: hfetch <url>\n");
105 }
106
107
108 void
hfetch(gchar * server,gint port,gchar * filename)109 hfetch(gchar* server, gint port, gchar* filename)
110 {
111 GInetAddr* addr;
112 GTcpSocket* socket;
113 GIOChannel* iochannel;
114 gchar* command;
115 gchar buffer[1024];
116 GIOError error;
117 gsize n;
118
119
120 /* Create the address */
121 addr = gnet_inetaddr_new(server, port);
122 g_assert (addr != NULL);
123
124 /* Create the socket */
125 socket = gnet_tcp_socket_new(addr);
126 gnet_inetaddr_delete (addr);
127 g_assert (socket != NULL);
128
129 /* Get the IOChannel */
130 iochannel = gnet_tcp_socket_get_io_channel(socket);
131 g_assert (iochannel != NULL);
132
133 /* Send the command */
134 command = g_strconcat("GET ", filename, "\n", NULL);
135 error = gnet_io_channel_writen(iochannel, command, strlen(command), &n);
136 g_free(command);
137
138 if (error != G_IO_ERROR_NONE)
139 {
140 g_warning("Write error: %d\n", error);
141 }
142
143 /* Read the output */
144 while (1)
145 {
146 error = g_io_channel_read(iochannel, buffer, sizeof(buffer), &n);
147 if (error != G_IO_ERROR_NONE)
148 {
149 g_warning("Read error: %d\n", error);
150 break;
151 }
152
153 if (n == 0)
154 break;
155
156 fwrite(buffer, n, 1, stdout);
157 }
158
159 gnet_tcp_socket_delete(socket);
160 }
161
162