summaryrefslogtreecommitdiffstats
path: root/contrib/expat/xmlwf/xmlwin32url.cxx
blob: bbfcce22c964357b7991d3e1dc74b739ed7ca320 (plain)
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
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
#include "expat.h"
#ifdef XML_UNICODE
#define UNICODE
#endif
#include <windows.h>
#include <urlmon.h>
#include <wininet.h>
#include <stdio.h>
#include <tchar.h>
#include "xmlurl.h"
#include "xmlmime.h"

static int
processURL(XML_Parser parser, IMoniker *baseMoniker, const XML_Char *url);

typedef void (*StopHandler)(void *, HRESULT);

class Callback : public IBindStatusCallback {
public:
  // IUnknown methods
  STDMETHODIMP QueryInterface(REFIID,void **);
  STDMETHODIMP_(ULONG) AddRef();
  STDMETHODIMP_(ULONG) Release();
  // IBindStatusCallback methods
  STDMETHODIMP OnStartBinding(DWORD, IBinding *);
  STDMETHODIMP GetPriority(LONG *);
  STDMETHODIMP OnLowResource(DWORD);
  STDMETHODIMP OnProgress(ULONG, ULONG, ULONG, LPCWSTR);
  STDMETHODIMP OnStopBinding(HRESULT, LPCWSTR);
  STDMETHODIMP GetBindInfo(DWORD *, BINDINFO *);
  STDMETHODIMP OnDataAvailable(DWORD, DWORD, FORMATETC *, STGMEDIUM *);
  STDMETHODIMP OnObjectAvailable(REFIID, IUnknown *);
  Callback(XML_Parser, IMoniker *, StopHandler, void *);
  ~Callback();
  int externalEntityRef(const XML_Char *context,
                        const XML_Char *systemId, const XML_Char *publicId);
private:
  XML_Parser parser_;
  IMoniker *baseMoniker_;
  DWORD totalRead_;
  ULONG ref_;
  IBinding *pBinding_;
  StopHandler stopHandler_;
  void *stopArg_;
};

STDMETHODIMP_(ULONG)
Callback::AddRef()
{ 
  return ref_++;
}

STDMETHODIMP_(ULONG)
Callback::Release()
{ 
  if (--ref_ == 0) {
    delete this;
    return 0;
  }
  return ref_;
}

STDMETHODIMP
Callback::QueryInterface(REFIID riid, void** ppv)
{ 
  if (IsEqualGUID(riid, IID_IUnknown))
    *ppv = (IUnknown *)this;
  else if (IsEqualGUID(riid, IID_IBindStatusCallback))
    *ppv = (IBindStatusCallback *)this;
  else
    return E_NOINTERFACE;
  ((LPUNKNOWN)*ppv)->AddRef();
  return S_OK;
}

STDMETHODIMP
Callback::OnStartBinding(DWORD, IBinding* pBinding)
{
  pBinding_ = pBinding;
  pBinding->AddRef();
  return S_OK;
}

STDMETHODIMP
Callback::GetPriority(LONG *)
{
  return E_NOTIMPL;
}

STDMETHODIMP
Callback::OnLowResource(DWORD)
{
  return E_NOTIMPL;
}

STDMETHODIMP
Callback::OnProgress(ULONG, ULONG, ULONG, LPCWSTR)
{
  return S_OK;
}

STDMETHODIMP
Callback::OnStopBinding(HRESULT hr, LPCWSTR szError)
{
  if (pBinding_) {
    pBinding_->Release();
    pBinding_ = 0;
  }
  if (baseMoniker_) {
    baseMoniker_->Release();
    baseMoniker_ = 0;
  }
  stopHandler_(stopArg_, hr);
  return S_OK;
}

STDMETHODIMP
Callback::GetBindInfo(DWORD* pgrfBINDF, BINDINFO* pbindinfo)
{
  *pgrfBINDF = BINDF_ASYNCHRONOUS;
  return S_OK;
}

static void
reportError(XML_Parser parser)
{
  int code = XML_GetErrorCode(parser);
  const XML_Char *message = XML_ErrorString(code);
  if (message)
    _ftprintf(stderr, _T("%s:%d:%ld: %s\n"),
	     XML_GetBase(parser),
	     XML_GetErrorLineNumber(parser),
	     XML_GetErrorColumnNumber(parser),
	     message);
  else
    _ftprintf(stderr, _T("%s: (unknown message %d)\n"),
              XML_GetBase(parser), code);
}

STDMETHODIMP
Callback::OnDataAvailable(DWORD grfBSCF,
                          DWORD dwSize,
                          FORMATETC *pfmtetc,
                          STGMEDIUM* pstgmed)
{
  if (grfBSCF & BSCF_FIRSTDATANOTIFICATION) {
    IWinInetHttpInfo *hp;
    HRESULT hr = pBinding_->QueryInterface(IID_IWinInetHttpInfo,
                                           (void **)&hp);
    if (SUCCEEDED(hr)) {
      char contentType[1024];
      DWORD bufSize = sizeof(contentType);
      DWORD flags = 0;
      contentType[0] = 0;
      hr = hp->QueryInfo(HTTP_QUERY_CONTENT_TYPE, contentType,
                         &bufSize, 0, NULL);
      if (SUCCEEDED(hr)) {
	char charset[CHARSET_MAX];
	getXMLCharset(contentType, charset);
	if (charset[0]) {
#ifdef XML_UNICODE
	  XML_Char wcharset[CHARSET_MAX];
	  XML_Char *p1 = wcharset;
	  const char *p2 = charset;
	  while ((*p1++ = (unsigned char)*p2++) != 0)
	    ;
	  XML_SetEncoding(parser_, wcharset);
#else
	  XML_SetEncoding(parser_, charset);
#endif
	}
      }
      hp->Release();
    }
  }
  if (!parser_)
    return E_ABORT;
  if (pstgmed->tymed == TYMED_ISTREAM) {
    while (totalRead_ < dwSize) {
#define READ_MAX (64*1024)
      DWORD nToRead = dwSize - totalRead_;
      if (nToRead > READ_MAX)
	nToRead = READ_MAX;
      void *buf = XML_GetBuffer(parser_, nToRead);
      if (!buf) {
	_ftprintf(stderr, _T("out of memory\n"));
	return E_ABORT;
      }
      DWORD nRead;
      HRESULT hr = pstgmed->pstm->Read(buf, nToRead, &nRead);
      if (SUCCEEDED(hr)) {
	totalRead_ += nRead;
	if (!XML_ParseBuffer(parser_,
			     nRead,
			     (grfBSCF & BSCF_LASTDATANOTIFICATION) != 0
			     && totalRead_ == dwSize)) {
	  reportError(parser_);
	  return E_ABORT;
	}
      }
    }
  }
  return S_OK;
}

STDMETHODIMP
Callback::OnObjectAvailable(REFIID, IUnknown *)
{
  return S_OK;
}

int
Callback::externalEntityRef(const XML_Char *context,
                            const XML_Char *systemId,
                            const XML_Char *publicId)
{
  XML_Parser entParser = XML_ExternalEntityParserCreate(parser_, context, 0);
  XML_SetBase(entParser, systemId);
  int ret = processURL(entParser, baseMoniker_, systemId);
  XML_ParserFree(entParser);
  return ret;
}

Callback::Callback(XML_Parser parser, IMoniker *baseMoniker,
                   StopHandler stopHandler, void *stopArg)
: parser_(parser),
  baseMoniker_(baseMoniker),
  ref_(0),
  pBinding_(0),
  totalRead_(0),
  stopHandler_(stopHandler),
  stopArg_(stopArg)
{
  if (baseMoniker_)
    baseMoniker_->AddRef();
}

Callback::~Callback()
{
  if (pBinding_)
    pBinding_->Release();
  if (baseMoniker_)
    baseMoniker_->Release();
}

static int
externalEntityRef(void *arg,
                  const XML_Char *context,
                  const XML_Char *base,
                  const XML_Char *systemId,
                  const XML_Char *publicId)
{
  return ((Callback *)arg)->externalEntityRef(context, systemId, publicId);
}


static HRESULT
openStream(XML_Parser parser,
           IMoniker *baseMoniker,
           const XML_Char *uri,
           StopHandler stopHandler, void *stopArg)
{
  if (!XML_SetBase(parser, uri))
    return E_OUTOFMEMORY;
  HRESULT hr;
  IMoniker *m;
#ifdef XML_UNICODE
  hr = CreateURLMoniker(0, uri, &m);
#else
  LPWSTR uriw = new wchar_t[strlen(uri) + 1];
  for (int i = 0;; i++) {
    uriw[i] = uri[i];
    if (uriw[i] == 0)
      break;
  }
  hr = CreateURLMoniker(baseMoniker, uriw, &m);
  delete [] uriw;
#endif
  if (FAILED(hr))
    return hr;
  IBindStatusCallback *cb = new Callback(parser, m, stopHandler, stopArg);
  XML_SetExternalEntityRefHandler(parser, externalEntityRef);
  XML_SetExternalEntityRefHandlerArg(parser, cb);
  cb->AddRef();
  IBindCtx *b;
  if (FAILED(hr = CreateAsyncBindCtx(0, cb, 0, &b))) {
    cb->Release();
    m->Release();
    return hr;
  }
  cb->Release();
  IStream *pStream;
  hr = m->BindToStorage(b, 0, IID_IStream, (void **)&pStream);
  if (SUCCEEDED(hr)) {
    if (pStream)
      pStream->Release();
  }
  if (hr == MK_S_ASYNCHRONOUS)
    hr = S_OK;
  m->Release();
  b->Release();
  return hr;
}

struct QuitInfo {
  const XML_Char *url;
  HRESULT hr;
  int stop;
};

static void
winPerror(const XML_Char *url, HRESULT hr)
{
  LPVOID buf;
  if (FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
		    | FORMAT_MESSAGE_FROM_HMODULE,
		    GetModuleHandleA("urlmon.dll"),
		    hr,
		    MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		    (LPTSTR) &buf,
		    0,
		    NULL)
      || FormatMessage(FORMAT_MESSAGE_ALLOCATE_BUFFER
		      | FORMAT_MESSAGE_FROM_SYSTEM,
		      0,
		      hr,
		      MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
		      (LPTSTR) &buf,
		      0,
		      NULL)) {
    /* The system error messages seem to end with a newline. */
    _ftprintf(stderr, _T("%s: %s"), url, buf);
    fflush(stderr);
    LocalFree(buf);
  }
  else
    _ftprintf(stderr, _T("%s: error %x\n"), url, hr);
}

static void
threadQuit(void *p, HRESULT hr)
{
  QuitInfo *qi = (QuitInfo *)p;
  qi->hr = hr;
  qi->stop = 1;
}

extern "C"
int
XML_URLInit(void)
{
  return SUCCEEDED(CoInitialize(0));
}

extern "C"
void
XML_URLUninit(void)
{
  CoUninitialize();
}

static int
processURL(XML_Parser parser, IMoniker *baseMoniker,
           const XML_Char *url)
{
  QuitInfo qi;
  qi.stop = 0;
  qi.url = url;

  XML_SetBase(parser, url);
  HRESULT hr = openStream(parser, baseMoniker, url, threadQuit, &qi);
  if (FAILED(hr)) {
    winPerror(url, hr);
    return 0;
  }
  else if (FAILED(qi.hr)) {
    winPerror(url, qi.hr);
    return 0;
  }
  MSG msg;
  while (!qi.stop && GetMessage (&msg, NULL, 0, 0)) {
    TranslateMessage (&msg);
    DispatchMessage (&msg);
  }
  return 1;
}

extern "C"
int
XML_ProcessURL(XML_Parser parser,
               const XML_Char *url,
               unsigned flags)
{
  return processURL(parser, 0, url);
}
OpenPOWER on IntegriCloud