File: WinInetStorage.cxx

package info (click to toggle)
opensp 1.5.2-15.2
  • links: PTS
  • area: main
  • in suites: forky, sid, trixie
  • size: 9,168 kB
  • sloc: cpp: 65,784; ansic: 17,124; sh: 11,193; xml: 2,704; makefile: 895; perl: 561; yacc: 288; sed: 16
file content (234 lines) | stat: -rw-r--r-- 5,277 bytes parent folder | download | duplicates (22)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
// Copyright (c) 1996 James Clark
// See the file COPYING for copying permission.

#include "splib.h"

#ifdef SP_WININET

#include "WinInetStorage.h"
#include "WinInetStorageMessages.h"
#include "RewindStorageObject.h"
#include "UnivCharsetDesc.h"
#include "MessageArg.h"
#include "MessageBuilder.h"
#include "macros.h"

#define STRICT
#include <windows.h>
#include <wininet.h>

#ifdef SP_NAMESPACE
namespace SP_NAMESPACE {
#endif

static UnivCharsetDesc::Range range = { 0, 128, 0 };
static CharsetInfo iso646Charset(UnivCharsetDesc(&range, 1));

String<char> toAscii(const StringC &buf)
{
  String<char> s;
  for (size_t i = 0; i < buf.size(); i++)
    s += buf[i];
  s += '\0';
  return s;
}
class Win32MessageArg : public MessageArg {
public:
  Win32MessageArg(DWORD n) : n_(n) { }
  MessageArg *copy() const { return new Win32MessageArg(*this); }
  void append(MessageBuilder &) const;
private:
  DWORD n_;
};

void Win32MessageArg::append(MessageBuilder &builder) const
{
  void *msg;
  if (!FormatMessageA(FORMAT_MESSAGE_FROM_SYSTEM
		      |FORMAT_MESSAGE_ALLOCATE_BUFFER,
		      0,
		      n_,
		      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		      (LPSTR)&msg,
		      0,
		      0)) {
    // FIXME interpret common internet messages here
    builder.appendNumber(n_);
    return;
  }
  String<Char> s;
  for (char *tem = (char *)msg; *tem; tem++)
    s += *tem;
  LocalFree(msg);
  builder.appendChars(s.data(), s.size());
}

class WinInetStorageObject : public RewindStorageObject {
public:
  WinInetStorageObject(HINTERNET fd, Boolean mayRewind, const StringC &url);
  ~WinInetStorageObject();
  Boolean read(char *buf, size_t bufSize, Messenger &mgr, size_t &nread);
  Boolean seekToStart(Messenger &);
private:
  WinInetStorageObject(const WinInetStorageObject &); // undefined
  void operator=(const WinInetStorageObject &); // undefined
  Boolean eof_;
  HINTERNET fd_;
  StringC url_;
};

WinInetStorageManager::WinInetStorageManager(const char *type)
: type_(type), IdStorageManager(&iso646Charset), session_(0)
{
}

WinInetStorageManager::~WinInetStorageManager()
{
  if (session_) {
    InternetCloseHandle(session_);
    session_ = 0;
  }
}

const char *WinInetStorageManager::type() const
{
  return type_;
}

Boolean WinInetStorageManager::initSession()
{
  if (!session_) {
    session_ = InternetOpenA("SP",
                             INTERNET_OPEN_TYPE_PRECONFIG,
			     0,
			     0,
			     0);
  }
  return 1;
}

Boolean WinInetStorageManager::guessIsId(const StringC &id,
				     const CharsetInfo &charset) const
{
  if (id.size() < 8)
    return 0;
  size_t i = 0;
  // guess other schemes supported by download protocols
  for (const char *s = "http://"; *s; s++, i++)
    if (id[i] != charset.execToDesc(*s)
	&& (!islower(*s) || id[i] != charset.execToDesc(toupper(*s))))
      return 0;
  return 1;
}

StorageObject *WinInetStorageManager::makeStorageObject(const StringC &specId,
							const StringC &baseId,
							Boolean,
							Boolean mayRewind,
						        Messenger &mgr,
						        StringC &id)
{
  if (!initSession())
    return 0;
  id = specId;
  resolveRelative(baseId, id, 0);
  String<char> tem(toAscii(id));
  HINTERNET fd = InternetOpenUrlA(session_, tem.data(), 0, 0, 0, 0); 
  if (!fd) {
    DWORD err = GetLastError();
    mgr.message(WinInetStorageMessages::cannotOpen,
                StringMessageArg(id),
		Win32MessageArg(err));
    return 0;
  }
  // FIXME report an error
  return new WinInetStorageObject(fd, mayRewind, id);
}

Boolean WinInetStorageManager::resolveRelative(const StringC &baseId,
					       StringC &id,
					       Boolean) const
{
  DWORD bufSize = baseId.size() + id.size() + 1;
  char *buf = new char[bufSize];
  String<char> baseIdA (toAscii(baseId));
  String<char> idA(toAscii(id));
  if (InternetCombineUrlA(baseIdA.data(),
			  idA.data(),
			  buf,
			  &bufSize,
			  0)) {
    id.resize(0);
    for (size_t i = 0; i < bufSize; i++)
      id += buf[i];
    delete [] buf;
    return 1;
  }
  delete [] buf;
  return 0;
}

Boolean WinInetStorageManager::transformNeutral(StringC &str, Boolean fold,
						Messenger &) const
{
  if (fold)
    for (size_t i = 0; i < str.size(); i++) {
      Char c = str[i];
      if (c <= (unsigned char)-1)
	str[i] = tolower(str[i]);
    }
  return 1;
}


WinInetStorageObject::WinInetStorageObject(HINTERNET fd,
					   Boolean mayRewind,
					   const StringC &url)
: RewindStorageObject(mayRewind, 0), fd_(fd), url_(url), eof_(0)
{
}

WinInetStorageObject::~WinInetStorageObject()
{
  if (fd_ != 0) {
    (void)InternetCloseHandle(fd_);
    fd_ = 0;
  }
}

Boolean WinInetStorageObject::read(char *buf, size_t bufSize, Messenger &mgr,
				   size_t &nread)
{
  if (readSaved(buf, bufSize, nread))
    return 1;
  if (fd_ == 0 || eof_)
    return 0;
  DWORD n;
  if (!InternetReadFile(fd_, buf, bufSize, &n)) {
    DWORD err = GetLastError();
    mgr.message(WinInetStorageMessages::readFailed,
                StringMessageArg(url_),
		Win32MessageArg(err));
    return 0;
  }
  if (n) {
    nread = n;
    return 1;
  }
  eof_ = 1;
  InternetCloseHandle(fd_);
  fd_ = 0;
  return 0;
}

Boolean WinInetStorageObject::seekToStart(Messenger &)
{
  CANNOT_HAPPEN();
  return 0;
}

#ifdef SP_NAMESPACE
}
#endif

#endif /* SP_WININET */