]>
Commit | Line | Data |
---|---|---|
23c9e63c | 1 | /* |
85553593 | 2 | * Copyright (c) 2000, 2001, Red Hat, Inc. |
23c9e63c DD |
3 | * |
4 | * This program is free software; you can redistribute it and/or modify | |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
8 | * | |
9 | * A copy of the GNU General Public License can be found at | |
10 | * http://www.gnu.org/ | |
11 | * | |
12 | * Written by DJ Delorie <dj@cygnus.com> | |
13 | * | |
14 | */ | |
15 | ||
16 | /* The purpose of this file is to coordinate the various access | |
17 | methods known to setup. To add a new method, create a pair of | |
18 | nio-*.[ch] files and add the logic to NetIO::open here */ | |
19 | ||
a77b6167 MB |
20 | #include "netio.h" |
21 | ||
5fa64c3c | 22 | #include "LogFile.h" |
a77b6167 | 23 | |
23c9e63c DD |
24 | #include <stdio.h> |
25 | #include <stdlib.h> | |
26 | #include <string.h> | |
27 | ||
28 | #include "resource.h" | |
29 | #include "state.h" | |
30 | #include "msg.h" | |
23c9e63c DD |
31 | #include "nio-file.h" |
32 | #include "nio-ie5.h" | |
4a83b7b0 DD |
33 | #include "nio-http.h" |
34 | #include "nio-ftp.h" | |
4e8ff53f | 35 | #include "dialog.h" |
23c9e63c | 36 | |
071f7af3 MB |
37 | int NetIO::net_method; |
38 | char *NetIO::net_proxy_host; | |
39 | int NetIO::net_proxy_port; | |
40 | ||
41 | char *NetIO::net_user; | |
42 | char *NetIO::net_passwd; | |
43 | char *NetIO::net_proxy_user; | |
44 | char *NetIO::net_proxy_passwd; | |
45 | char *NetIO::net_ftp_user; | |
46 | char *NetIO::net_ftp_passwd; | |
47 | ||
f5d45c3b | 48 | NetIO::NetIO (char const *Purl) |
4a83b7b0 DD |
49 | { |
50 | set_url (Purl); | |
51 | } | |
52 | ||
53 | NetIO::~NetIO () | |
54 | { | |
55 | if (url) | |
5e0464a1 | 56 | delete[] url; |
4a83b7b0 | 57 | if (proto) |
5e0464a1 | 58 | delete[] proto; |
4a83b7b0 | 59 | if (host) |
5e0464a1 | 60 | delete[] host; |
4a83b7b0 | 61 | if (path) |
5e0464a1 | 62 | delete[] path; |
4a83b7b0 DD |
63 | } |
64 | ||
65 | void | |
341988b9 | 66 | NetIO::set_url (char const *Purl) |
23c9e63c DD |
67 | { |
68 | char *bp, *ep, c; | |
69 | ||
70 | file_size = 0; | |
5e0464a1 RC |
71 | url = new char[strlen (Purl) + 1]; |
72 | strcpy (url, Purl); | |
23c9e63c DD |
73 | proto = 0; |
74 | host = 0; | |
75 | port = 0; | |
76 | path = 0; | |
77 | ||
78 | bp = url; | |
79 | ep = strstr (bp, "://"); | |
80 | if (!ep) | |
81 | { | |
dbfe3c19 | 82 | path = strdup (url); |
23c9e63c DD |
83 | return; |
84 | } | |
85 | ||
86 | *ep = 0; | |
5e0464a1 RC |
87 | proto = new char [strlen (bp)+1]; |
88 | strcpy (proto, bp); | |
23c9e63c | 89 | *ep = ':'; |
b24c88b3 | 90 | bp = ep + 3; |
23c9e63c | 91 | |
4a83b7b0 | 92 | ep = bp + strcspn (bp, ":/"); |
23c9e63c DD |
93 | c = *ep; |
94 | *ep = 0; | |
5e0464a1 RC |
95 | host = new char [strlen (bp) + 1]; |
96 | strcpy (host, bp); | |
23c9e63c DD |
97 | *ep = c; |
98 | ||
99 | if (*ep == ':') | |
100 | { | |
b24c88b3 | 101 | port = atoi (ep + 1); |
23c9e63c DD |
102 | ep = strchr (ep, '/'); |
103 | } | |
104 | ||
105 | if (*ep) | |
5e0464a1 RC |
106 | { |
107 | path = new char [strlen (ep)+1]; | |
108 | strcpy (path, ep); | |
109 | } | |
23c9e63c DD |
110 | } |
111 | ||
23c9e63c DD |
112 | int |
113 | NetIO::ok () | |
114 | { | |
115 | return 0; | |
116 | } | |
117 | ||
118 | int | |
119 | NetIO::read (char *buf, int nbytes) | |
120 | { | |
121 | return 0; | |
122 | } | |
123 | ||
124 | NetIO * | |
ca875ed3 | 125 | NetIO::open (char const *url, bool cachable) |
23c9e63c DD |
126 | { |
127 | NetIO *rv = 0; | |
b24c88b3 | 128 | enum |
ca875ed3 | 129 | { http, https, ftp, ftps, file } |
b24c88b3 | 130 | proto; |
23c9e63c DD |
131 | if (strncmp (url, "http://", 7) == 0) |
132 | proto = http; | |
ca875ed3 JT |
133 | else if (strncmp (url, "https://", 8) == 0) |
134 | proto = https; | |
23c9e63c DD |
135 | else if (strncmp (url, "ftp://", 6) == 0) |
136 | proto = ftp; | |
ca875ed3 JT |
137 | else if (strncmp (url, "ftps://", 7) == 0) |
138 | proto = ftps; | |
23c9e63c DD |
139 | else |
140 | proto = file; | |
141 | ||
142 | if (proto == file) | |
143 | rv = new NetIO_File (url); | |
144 | else if (net_method == IDC_NET_IE5) | |
ca875ed3 | 145 | rv = new NetIO_IE5 (url, false, cachable); |
23c9e63c | 146 | else if (net_method == IDC_NET_PROXY) |
f5d45c3b | 147 | rv = new NetIO_HTTP (url); |
4a83b7b0 | 148 | else if (net_method == IDC_NET_DIRECT) |
ca875ed3 JT |
149 | rv = new NetIO_IE5 (url, true, cachable); |
150 | else if (net_method == IDC_NET_DIRECT_LEGACY) | |
4a83b7b0 DD |
151 | { |
152 | switch (proto) | |
153 | { | |
154 | case http: | |
155 | rv = new NetIO_HTTP (url); | |
156 | break; | |
157 | case ftp: | |
f5d45c3b | 158 | rv = new NetIO_FTP (url); |
4a83b7b0 | 159 | break; |
b24c88b3 RC |
160 | case file: |
161 | rv = new NetIO_File (url); | |
162 | break; | |
ca875ed3 JT |
163 | default: |
164 | mbox (NULL, "Protocol not handled by legacy URL handler", "Cygwin Setup", MB_OK); | |
4a83b7b0 DD |
165 | } |
166 | } | |
23c9e63c | 167 | |
ca875ed3 | 168 | if (rv && !rv->ok ()) |
23c9e63c DD |
169 | { |
170 | delete rv; | |
171 | return 0; | |
172 | } | |
173 | ||
174 | return rv; | |
175 | } | |
4e8ff53f DD |
176 | |
177 | ||
178 | static char **user, **passwd; | |
348860fa | 179 | static int loading = 0; |
4e8ff53f DD |
180 | |
181 | static void | |
182 | check_if_enable_ok (HWND h) | |
183 | { | |
184 | int e = 0; | |
ed6137e5 | 185 | if (*user) |
4e8ff53f DD |
186 | e = 1; |
187 | EnableWindow (GetDlgItem (h, IDOK), e); | |
188 | } | |
189 | ||
190 | static void | |
191 | load_dialog (HWND h) | |
192 | { | |
348860fa | 193 | loading = 1; |
4e8ff53f DD |
194 | eset (h, IDC_NET_USER, *user); |
195 | eset (h, IDC_NET_PASSWD, *passwd); | |
196 | check_if_enable_ok (h); | |
348860fa | 197 | loading = 0; |
4e8ff53f DD |
198 | } |
199 | ||
200 | static void | |
201 | save_dialog (HWND h) | |
202 | { | |
203 | *user = eget (h, IDC_NET_USER, *user); | |
204 | *passwd = eget (h, IDC_NET_PASSWD, *passwd); | |
ed6137e5 MB |
205 | if (! *passwd) { |
206 | *passwd = new char[1]; | |
207 | passwd[0] = '\0'; | |
208 | } | |
4e8ff53f DD |
209 | } |
210 | ||
211 | static BOOL | |
212 | auth_cmd (HWND h, int id, HWND hwndctl, UINT code) | |
213 | { | |
214 | switch (id) | |
215 | { | |
216 | ||
217 | case IDC_NET_USER: | |
218 | case IDC_NET_PASSWD: | |
348860fa DD |
219 | if (code == EN_CHANGE && !loading) |
220 | { | |
221 | save_dialog (h); | |
222 | check_if_enable_ok (h); | |
223 | } | |
4e8ff53f DD |
224 | break; |
225 | ||
226 | case IDOK: | |
227 | save_dialog (h); | |
228 | EndDialog (h, 0); | |
229 | break; | |
230 | ||
231 | case IDCANCEL: | |
232 | EndDialog (h, 1); | |
5fa64c3c | 233 | Logger ().exit (1); |
4e8ff53f DD |
234 | break; |
235 | } | |
b24c88b3 | 236 | return 0; |
4e8ff53f DD |
237 | } |
238 | ||
e08abe3f | 239 | static INT_PTR CALLBACK |
4e8ff53f DD |
240 | auth_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam) |
241 | { | |
242 | switch (message) | |
243 | { | |
244 | case WM_INITDIALOG: | |
245 | load_dialog (h); | |
246 | return FALSE; | |
247 | case WM_COMMAND: | |
4875ac88 MB |
248 | auth_cmd (h, LOWORD(wParam), (HWND)lParam, HIWORD(wParam)); |
249 | return 0; | |
4e8ff53f DD |
250 | } |
251 | return FALSE; | |
252 | } | |
253 | ||
254 | static int | |
ab57ceaa | 255 | auth_common (HINSTANCE h, int id, HWND owner) |
4e8ff53f | 256 | { |
ab57ceaa | 257 | return DialogBox (h, MAKEINTRESOURCE (id), owner, auth_proc); |
4e8ff53f DD |
258 | } |
259 | ||
260 | int | |
ab57ceaa | 261 | NetIO::get_auth (HWND owner) |
4e8ff53f DD |
262 | { |
263 | user = &net_user; | |
264 | passwd = &net_passwd; | |
ab57ceaa | 265 | return auth_common (hinstance, IDD_NET_AUTH, owner); |
4e8ff53f DD |
266 | } |
267 | ||
268 | int | |
ab57ceaa | 269 | NetIO::get_proxy_auth (HWND owner) |
4e8ff53f DD |
270 | { |
271 | user = &net_proxy_user; | |
272 | passwd = &net_proxy_passwd; | |
ab57ceaa | 273 | return auth_common (hinstance, IDD_PROXY_AUTH, owner); |
4e8ff53f | 274 | } |
85553593 CV |
275 | |
276 | int | |
ab57ceaa | 277 | NetIO::get_ftp_auth (HWND owner) |
85553593 CV |
278 | { |
279 | if (net_ftp_user) | |
280 | { | |
5e0464a1 | 281 | delete[] net_ftp_user; |
85553593 CV |
282 | net_ftp_user = NULL; |
283 | } | |
284 | if (net_ftp_passwd) | |
285 | { | |
5e0464a1 | 286 | delete[] net_ftp_passwd; |
85553593 CV |
287 | net_ftp_passwd = NULL; |
288 | } | |
289 | if (!ftp_auth) | |
290 | return IDCANCEL; | |
291 | user = &net_ftp_user; | |
292 | passwd = &net_ftp_passwd; | |
ab57ceaa | 293 | return auth_common (hinstance, IDD_FTP_AUTH, owner); |
85553593 | 294 | } |
ca875ed3 JT |
295 | |
296 | const char * | |
297 | NetIO::net_method_name () | |
298 | { | |
299 | switch (net_method) | |
300 | { | |
301 | case IDC_NET_IE5: | |
302 | return "IE5"; | |
303 | case IDC_NET_DIRECT: | |
304 | return "Direct"; | |
305 | case IDC_NET_PROXY: | |
306 | return "Proxy"; | |
307 | case IDC_NET_DIRECT_LEGACY: | |
308 | return "Direct (legacy)"; | |
309 | default: | |
310 | return "Unknown"; | |
311 | } | |
312 | } |