]> cygwin.com Git - cygwin-apps/setup.git/blob - netio.cc
netio.cc: fix a bug in string handling
[cygwin-apps/setup.git] / netio.cc
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 <shlwapi.h>
29
30 #include "resource.h"
31 #include "state.h"
32 #include "msg.h"
33 #include "nio-ie5.h"
34 #include "dialog.h"
35
36 int NetIO::net_method;
37 char *NetIO::net_proxy_host;
38 int NetIO::net_proxy_port;
39
40 char *NetIO::net_user;
41 char *NetIO::net_passwd;
42 char *NetIO::net_proxy_user;
43 char *NetIO::net_proxy_passwd;
44 char *NetIO::net_ftp_user;
45 char *NetIO::net_ftp_passwd;
46
47 int
48 NetIO::ok ()
49 {
50 return 0;
51 }
52
53 int
54 NetIO::read (char *buf, int nbytes)
55 {
56 return 0;
57 }
58
59 NetIO *
60 NetIO::open (char const *url, bool cachable)
61 {
62 NetIO *rv = 0;
63 std::string file_url;
64
65 enum
66 { http, https, ftp, ftps, file }
67 proto;
68 if (strncmp (url, "http://", 7) == 0)
69 proto = http;
70 else if (strncmp (url, "https://", 8) == 0)
71 proto = https;
72 else if (strncmp (url, "ftp://", 6) == 0)
73 proto = ftp;
74 else if (strncmp (url, "ftps://", 7) == 0)
75 proto = ftps;
76 else if (strncmp (url, "file://", 7) == 0)
77 {
78 proto = file;
79
80 // WinInet expects a 'legacy' file:// URL
81 // (i.e. a windows path with "file://" prepended)
82 // https://blogs.msdn.microsoft.com/freeassociations/2005/05/19/the-bizarre-and-unhappy-story-of-file-urls/
83 char path[MAX_PATH];
84 DWORD len = MAX_PATH;
85 if (S_OK == PathCreateFromUrl(url, path, &len, 0))
86 {
87 file_url = std::string("file://") + path;
88 url = file_url.c_str();
89 }
90 }
91 else
92 // treat everything else as a windows path
93 {
94 proto = file;
95 file_url = std::string("file://") + url;
96 url = file_url.c_str();
97 }
98
99 rv = new NetIO_IE5 (url, proto == file ? false : cachable);
100
101 if (rv && !rv->ok ())
102 {
103 delete rv;
104 return 0;
105 }
106
107 return rv;
108 }
109
110
111 static char **user, **passwd;
112 static int loading = 0;
113
114 static void
115 check_if_enable_ok (HWND h)
116 {
117 int e = 0;
118 if (*user)
119 e = 1;
120 EnableWindow (GetDlgItem (h, IDOK), e);
121 }
122
123 static void
124 load_dialog (HWND h)
125 {
126 loading = 1;
127 eset (h, IDC_NET_USER, *user);
128 eset (h, IDC_NET_PASSWD, *passwd);
129 check_if_enable_ok (h);
130 loading = 0;
131 }
132
133 static void
134 save_dialog (HWND h)
135 {
136 *user = eget (h, IDC_NET_USER, *user);
137 *passwd = eget (h, IDC_NET_PASSWD, *passwd);
138 if (! *passwd) {
139 *passwd = new char[1];
140 (*passwd)[0] = '\0';
141 }
142 }
143
144 static BOOL
145 auth_cmd (HWND h, int id, HWND hwndctl, UINT code)
146 {
147 switch (id)
148 {
149
150 case IDC_NET_USER:
151 case IDC_NET_PASSWD:
152 if (code == EN_CHANGE && !loading)
153 {
154 save_dialog (h);
155 check_if_enable_ok (h);
156 }
157 break;
158
159 case IDOK:
160 save_dialog (h);
161 EndDialog (h, 0);
162 break;
163
164 case IDCANCEL:
165 EndDialog (h, 1);
166 Logger ().exit (1);
167 break;
168 }
169 return 0;
170 }
171
172 static INT_PTR CALLBACK
173 auth_proc (HWND h, UINT message, WPARAM wParam, LPARAM lParam)
174 {
175 switch (message)
176 {
177 case WM_INITDIALOG:
178 load_dialog (h);
179 return FALSE;
180 case WM_COMMAND:
181 auth_cmd (h, LOWORD(wParam), (HWND)lParam, HIWORD(wParam));
182 return 0;
183 }
184 return FALSE;
185 }
186
187 static int
188 auth_common (HINSTANCE h, int id, HWND owner)
189 {
190 return DialogBox (h, MAKEINTRESOURCE (id), owner, auth_proc);
191 }
192
193 int
194 NetIO::get_auth (HWND owner)
195 {
196 user = &net_user;
197 passwd = &net_passwd;
198 return auth_common (hinstance, IDD_NET_AUTH, owner);
199 }
200
201 int
202 NetIO::get_proxy_auth (HWND owner)
203 {
204 user = &net_proxy_user;
205 passwd = &net_proxy_passwd;
206 return auth_common (hinstance, IDD_PROXY_AUTH, owner);
207 }
208
209 int
210 NetIO::get_ftp_auth (HWND owner)
211 {
212 if (net_ftp_user)
213 {
214 delete[] net_ftp_user;
215 net_ftp_user = NULL;
216 }
217 if (net_ftp_passwd)
218 {
219 delete[] net_ftp_passwd;
220 net_ftp_passwd = NULL;
221 }
222 if (!ftp_auth)
223 return IDCANCEL;
224 user = &net_ftp_user;
225 passwd = &net_ftp_passwd;
226 return auth_common (hinstance, IDD_FTP_AUTH, owner);
227 }
228
229 const char *
230 NetIO::net_method_name ()
231 {
232 switch (net_method)
233 {
234 case IDC_NET_PRECONFIG:
235 return "Preconfig";
236 case IDC_NET_DIRECT:
237 return "Direct";
238 case IDC_NET_PROXY:
239 return "Proxy";
240 default:
241 return "Unknown";
242 }
243 }
This page took 0.044995 seconds and 5 git commands to generate.