]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * Copyright (c) 2000, 2001, Red Hat, Inc. | |
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 | ||
20 | #include "netio.h" | |
21 | ||
22 | #include "LogFile.h" | |
23 | ||
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" | |
31 | #include "nio-file.h" | |
32 | #include "nio-ie5.h" | |
33 | #include "nio-http.h" | |
34 | #include "nio-ftp.h" | |
35 | #include "dialog.h" | |
36 | ||
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 | ||
48 | NetIO::NetIO (char const *Purl) | |
49 | { | |
50 | set_url (Purl); | |
51 | } | |
52 | ||
53 | NetIO::~NetIO () | |
54 | { | |
55 | if (url) | |
56 | delete[] url; | |
57 | if (proto) | |
58 | delete[] proto; | |
59 | if (host) | |
60 | delete[] host; | |
61 | if (path) | |
62 | delete[] path; | |
63 | } | |
64 | ||
65 | void | |
66 | NetIO::set_url (char const *Purl) | |
67 | { | |
68 | char *bp, *ep, c; | |
69 | ||
70 | file_size = 0; | |
71 | url = new char[strlen (Purl) + 1]; | |
72 | strcpy (url, Purl); | |
73 | proto = 0; | |
74 | host = 0; | |
75 | port = 0; | |
76 | path = 0; | |
77 | ||
78 | bp = url; | |
79 | ep = strstr (bp, "://"); | |
80 | if (!ep) | |
81 | { | |
82 | path = strdup (url); | |
83 | return; | |
84 | } | |
85 | ||
86 | *ep = 0; | |
87 | proto = new char [strlen (bp)+1]; | |
88 | strcpy (proto, bp); | |
89 | *ep = ':'; | |
90 | bp = ep + 3; | |
91 | ||
92 | ep = bp + strcspn (bp, ":/"); | |
93 | c = *ep; | |
94 | *ep = 0; | |
95 | host = new char [strlen (bp) + 1]; | |
96 | strcpy (host, bp); | |
97 | *ep = c; | |
98 | ||
99 | if (*ep == ':') | |
100 | { | |
101 | port = atoi (ep + 1); | |
102 | ep = strchr (ep, '/'); | |
103 | } | |
104 | ||
105 | if (*ep) | |
106 | { | |
107 | path = new char [strlen (ep)+1]; | |
108 | strcpy (path, ep); | |
109 | } | |
110 | } | |
111 | ||
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 * | |
125 | NetIO::open (char const *url, bool cachable) | |
126 | { | |
127 | NetIO *rv = 0; | |
128 | enum | |
129 | { http, https, ftp, ftps, file } | |
130 | proto; | |
131 | if (strncmp (url, "http://", 7) == 0) | |
132 | proto = http; | |
133 | else if (strncmp (url, "https://", 8) == 0) | |
134 | proto = https; | |
135 | else if (strncmp (url, "ftp://", 6) == 0) | |
136 | proto = ftp; | |
137 | else if (strncmp (url, "ftps://", 7) == 0) | |
138 | proto = ftps; | |
139 | else | |
140 | proto = file; | |
141 | ||
142 | if (proto == file) | |
143 | rv = new NetIO_File (url); | |
144 | else if (net_method == IDC_NET_IE5) | |
145 | rv = new NetIO_IE5 (url, false, cachable); | |
146 | else if (net_method == IDC_NET_PROXY) | |
147 | rv = new NetIO_HTTP (url); | |
148 | else if (net_method == IDC_NET_DIRECT) | |
149 | rv = new NetIO_IE5 (url, true, cachable); | |
150 | else if (net_method == IDC_NET_DIRECT_LEGACY) | |
151 | { | |
152 | switch (proto) | |
153 | { | |
154 | case http: | |
155 | rv = new NetIO_HTTP (url); | |
156 | break; | |
157 | case ftp: | |
158 | rv = new NetIO_FTP (url); | |
159 | break; | |
160 | case file: | |
161 | rv = new NetIO_File (url); | |
162 | break; | |
163 | default: | |
164 | mbox (NULL, "Protocol not handled by legacy URL handler", "Cygwin Setup", MB_OK); | |
165 | } | |
166 | } | |
167 | ||
168 | if (rv && !rv->ok ()) | |
169 | { | |
170 | delete rv; | |
171 | return 0; | |
172 | } | |
173 | ||
174 | return rv; | |
175 | } | |
176 | ||
177 | ||
178 | static char **user, **passwd; | |
179 | static int loading = 0; | |
180 | ||
181 | static void | |
182 | check_if_enable_ok (HWND h) | |
183 | { | |
184 | int e = 0; | |
185 | if (*user) | |
186 | e = 1; | |
187 | EnableWindow (GetDlgItem (h, IDOK), e); | |
188 | } | |
189 | ||
190 | static void | |
191 | load_dialog (HWND h) | |
192 | { | |
193 | loading = 1; | |
194 | eset (h, IDC_NET_USER, *user); | |
195 | eset (h, IDC_NET_PASSWD, *passwd); | |
196 | check_if_enable_ok (h); | |
197 | loading = 0; | |
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); | |
205 | if (! *passwd) { | |
206 | *passwd = new char[1]; | |
207 | passwd[0] = '\0'; | |
208 | } | |
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: | |
219 | if (code == EN_CHANGE && !loading) | |
220 | { | |
221 | save_dialog (h); | |
222 | check_if_enable_ok (h); | |
223 | } | |
224 | break; | |
225 | ||
226 | case IDOK: | |
227 | save_dialog (h); | |
228 | EndDialog (h, 0); | |
229 | break; | |
230 | ||
231 | case IDCANCEL: | |
232 | EndDialog (h, 1); | |
233 | Logger ().exit (1); | |
234 | break; | |
235 | } | |
236 | return 0; | |
237 | } | |
238 | ||
239 | static INT_PTR CALLBACK | |
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: | |
248 | auth_cmd (h, LOWORD(wParam), (HWND)lParam, HIWORD(wParam)); | |
249 | return 0; | |
250 | } | |
251 | return FALSE; | |
252 | } | |
253 | ||
254 | static int | |
255 | auth_common (HINSTANCE h, int id, HWND owner) | |
256 | { | |
257 | return DialogBox (h, MAKEINTRESOURCE (id), owner, auth_proc); | |
258 | } | |
259 | ||
260 | int | |
261 | NetIO::get_auth (HWND owner) | |
262 | { | |
263 | user = &net_user; | |
264 | passwd = &net_passwd; | |
265 | return auth_common (hinstance, IDD_NET_AUTH, owner); | |
266 | } | |
267 | ||
268 | int | |
269 | NetIO::get_proxy_auth (HWND owner) | |
270 | { | |
271 | user = &net_proxy_user; | |
272 | passwd = &net_proxy_passwd; | |
273 | return auth_common (hinstance, IDD_PROXY_AUTH, owner); | |
274 | } | |
275 | ||
276 | int | |
277 | NetIO::get_ftp_auth (HWND owner) | |
278 | { | |
279 | if (net_ftp_user) | |
280 | { | |
281 | delete[] net_ftp_user; | |
282 | net_ftp_user = NULL; | |
283 | } | |
284 | if (net_ftp_passwd) | |
285 | { | |
286 | delete[] net_ftp_passwd; | |
287 | net_ftp_passwd = NULL; | |
288 | } | |
289 | if (!ftp_auth) | |
290 | return IDCANCEL; | |
291 | user = &net_ftp_user; | |
292 | passwd = &net_ftp_passwd; | |
293 | return auth_common (hinstance, IDD_FTP_AUTH, owner); | |
294 | } | |
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 | } |