XEmacs 21.2.47 (Zephir).
[chise/xemacs-chise.git.1] / netinstall / nio-ie5.cc
1 /*
2  * Copyright (c) 2000, 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 manage internet downloads using the
17    Internet Explorer version 5 DLLs.  To use this method, the user
18    must already have installed and configured IE5.  This module is
19    called from netio.cc, which is called from geturl.cc */
20
21 static char *cvsid = "\n%%% $Id: nio-ie5.cc,v 1.2 2001/04/12 18:22:51 michaels Exp $\n";
22
23 #include "win32.h"
24
25 #include "resource.h"
26 #include "state.h"
27 #include "dialog.h"
28 #include "msg.h"
29 #include "netio.h"
30 #include "nio-ie5.h"
31
32 static HINTERNET internet = 0;
33
34 NetIO_IE5::NetIO_IE5 (char *_url)
35   : NetIO (_url)
36 {
37   int resend = 0;
38
39   if (internet == 0)
40     {
41       HINSTANCE h = LoadLibrary ("wininet.dll");
42       if (!h)
43         {
44           note (IDS_WININET);
45           connection = 0;
46           return;
47         }
48       InternetAttemptConnect (0);
49       internet = InternetOpen ("Cygwin Setup", INTERNET_OPEN_TYPE_PRECONFIG,
50                                NULL, NULL, 0);
51     }
52
53   DWORD flags =
54     INTERNET_FLAG_DONT_CACHE |
55     INTERNET_FLAG_KEEP_CONNECTION |
56     INTERNET_FLAG_PRAGMA_NOCACHE |
57     INTERNET_FLAG_RELOAD |
58     INTERNET_FLAG_EXISTING_CONNECT |
59     INTERNET_FLAG_PASSIVE;
60
61   connection = InternetOpenUrl (internet, url, NULL, 0, flags, 0);
62
63  try_again:
64
65   if (net_user && net_passwd)
66     {
67       InternetSetOption (connection, INTERNET_OPTION_USERNAME,
68                          net_user, strlen (net_user));
69       InternetSetOption (connection, INTERNET_OPTION_PASSWORD,
70                          net_passwd, strlen (net_passwd));
71     }
72
73   if (net_proxy_user && net_proxy_passwd)
74     {
75       InternetSetOption (connection, INTERNET_OPTION_PROXY_USERNAME,
76                          net_proxy_user, strlen (net_proxy_user));
77       InternetSetOption (connection, INTERNET_OPTION_PROXY_PASSWORD,
78                          net_proxy_passwd, strlen (net_proxy_passwd));
79     }
80
81   if (resend)
82     if (!HttpSendRequest (connection, 0, 0, 0, 0))
83       connection = 0;
84
85   if (!connection)
86     {
87       if (GetLastError () == ERROR_INTERNET_EXTENDED_ERROR)
88         {
89           char buf[2000];
90           DWORD e, l=sizeof (buf);
91           InternetGetLastResponseInfo (&e, buf, &l);
92           MessageBox (0, buf, "Internet Error", 0);
93         }
94     }
95
96   DWORD type, type_s;
97   type_s = sizeof (type);
98   InternetQueryOption (connection, INTERNET_OPTION_HANDLE_TYPE,
99                        &type, &type_s);
100
101   switch (type)
102     {
103     case INTERNET_HANDLE_TYPE_HTTP_REQUEST:
104     case INTERNET_HANDLE_TYPE_CONNECT_HTTP:
105       type_s = sizeof (DWORD);
106       if (HttpQueryInfo (connection,
107                          HTTP_QUERY_STATUS_CODE | HTTP_QUERY_FLAG_NUMBER,
108                          &type, &type_s, NULL))
109         {
110           if (type == 401) /* authorization required */
111             {
112               flush_io();
113               get_auth ();
114               resend = 1;
115               goto try_again;
116             }
117           else if (type == 407) /* proxy authorization required */
118             {
119               flush_io();
120               get_proxy_auth ();
121               resend = 1;
122               goto try_again;
123             }
124           else if (type >= 300)
125             {
126               connection = 0;
127               return;
128             }
129         }
130     }
131 }
132
133 void
134 NetIO_IE5::flush_io ()
135 {
136   DWORD actual = 0;
137   char buf[1024];
138   do {
139     InternetReadFile (connection, buf, 1024, &actual);
140   } while (actual > 0);
141 }
142
143 NetIO_IE5::~NetIO_IE5 ()
144 {
145   if (connection)
146     InternetCloseHandle (connection);
147 }
148
149 int
150 NetIO_IE5::ok ()
151 {
152   return (connection == NULL) ? 0 : 1;
153 }
154
155 int
156 NetIO_IE5::read (char *buf, int nbytes)
157 {
158   DWORD actual;
159   if (InternetReadFile (connection, buf, nbytes, &actual))
160     return actual;
161   return -1;
162 }