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
|
/*
* Copyright (C) 2008 Apple Inc. All Rights Reserved.
*
* Redistribution and use in source and binary forms, with or without
* modification, are permitted provided that the following conditions
* are met:
* 1. Redistributions of source code must retain the above copyright
* notice, this list of conditions and the following disclaimer.
* 2. Redistributions in binary form must reproduce the above copyright
* notice, this list of conditions and the following disclaimer in the
* documentation and/or other materials provided with the distribution.
*
* THIS SOFTWARE IS PROVIDED BY APPLE INC. ``AS IS'' AND ANY
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE INC. OR
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
*/
#if USE(PLUGIN_HOST_PROCESS)
#import "HostedNetscapePluginStream.h"
#import "NetscapePluginHostProxy.h"
#import "NetscapePluginInstanceProxy.h"
#import "WebFrameInternal.h"
#import "WebHostedNetscapePluginView.h"
#import "WebKitErrorsPrivate.h"
#import "WebKitPluginHost.h"
#import "WebKitSystemInterface.h"
#import "WebNSURLExtras.h"
#import "WebNSURLRequestExtras.h"
#import <WebCore/DocumentLoader.h>
#import <WebCore/Frame.h>
#import <WebCore/FrameLoader.h>
#import <WebCore/SecurityOrigin.h>
#import <WebCore/WebCoreURLResponse.h>
#import <wtf/RefCountedLeakCounter.h>
using namespace WebCore;
namespace WebKit {
#ifndef NDEBUG
static WTF::RefCountedLeakCounter hostedNetscapePluginStreamCounter("HostedNetscapePluginStream");
#endif
HostedNetscapePluginStream::HostedNetscapePluginStream(NetscapePluginInstanceProxy* instance, uint32_t streamID, NSURLRequest *request)
: m_instance(instance)
, m_streamID(streamID)
, m_isTerminated(false)
, m_request(AdoptNS, [request mutableCopy])
, m_requestURL([request URL])
, m_frameLoader(0)
{
if (SecurityOrigin::shouldHideReferrer([request URL], core([instance->pluginView() webFrame])->loader()->outgoingReferrer()))
[m_request.get() _web_setHTTPReferrer:nil];
#ifndef NDEBUG
hostedNetscapePluginStreamCounter.increment();
#endif
}
HostedNetscapePluginStream::HostedNetscapePluginStream(NetscapePluginInstanceProxy* instance, WebCore::FrameLoader* frameLoader)
: m_instance(instance)
, m_streamID(1)
, m_isTerminated(false)
, m_frameLoader(frameLoader)
{
#ifndef NDEBUG
hostedNetscapePluginStreamCounter.increment();
#endif
}
HostedNetscapePluginStream::~HostedNetscapePluginStream()
{
#ifndef NDEBUG
hostedNetscapePluginStreamCounter.decrement();
#endif
}
void HostedNetscapePluginStream::startStreamWithResponse(NSURLResponse *response)
{
didReceiveResponse(0, response);
}
void HostedNetscapePluginStream::startStream(NSURL *responseURL, long long expectedContentLength, NSDate *lastModifiedDate, NSString *mimeType, NSData *headers)
{
m_responseURL = responseURL;
m_mimeType = mimeType;
char* mimeTypeUTF8 = const_cast<char*>([mimeType UTF8String]);
int mimeTypeUTF8Length = mimeTypeUTF8 ? strlen (mimeTypeUTF8) + 1 : 0;
const char *url = [responseURL _web_URLCString];
int urlLength = url ? strlen(url) + 1 : 0;
_WKPHStartStream(m_instance->hostProxy()->port(),
m_instance->pluginID(),
m_streamID,
const_cast<char*>(url), urlLength,
expectedContentLength,
[lastModifiedDate timeIntervalSince1970],
mimeTypeUTF8, mimeTypeUTF8Length,
const_cast<char*>(reinterpret_cast<const char*>([headers bytes])), [headers length]);
}
void HostedNetscapePluginStream::didReceiveData(WebCore::NetscapePlugInStreamLoader*, const char* bytes, int length)
{
_WKPHStreamDidReceiveData(m_instance->hostProxy()->port(),
m_instance->pluginID(),
m_streamID,
const_cast<char*>(bytes), length);
}
void HostedNetscapePluginStream::didFinishLoading(WebCore::NetscapePlugInStreamLoader*)
{
_WKPHStreamDidFinishLoading(m_instance->hostProxy()->port(),
m_instance->pluginID(),
m_streamID);
m_instance->disconnectStream(this);
}
void HostedNetscapePluginStream::didReceiveResponse(NetscapePlugInStreamLoader*, const ResourceResponse& response)
{
NSURLResponse *r = response.nsURLResponse();
NSMutableData *theHeaders = nil;
long long expectedContentLength = [r expectedContentLength];
if ([r isKindOfClass:[NSHTTPURLResponse class]]) {
NSHTTPURLResponse *httpResponse = (NSHTTPURLResponse *)r;
theHeaders = [NSMutableData dataWithCapacity:1024];
// FIXME: it would be nice to be able to get the raw HTTP header block.
// This includes the HTTP version, the real status text,
// all headers in their original order and including duplicates,
// and all original bytes verbatim, rather than sent through Unicode translation.
// Unfortunately NSHTTPURLResponse doesn't provide access at that low a level.
[theHeaders appendBytes:"HTTP " length:5];
char statusStr[10];
long statusCode = [httpResponse statusCode];
snprintf(statusStr, sizeof(statusStr), "%ld", statusCode);
[theHeaders appendBytes:statusStr length:strlen(statusStr)];
[theHeaders appendBytes:" OK\n" length:4];
// HACK: pass the headers through as UTF-8.
// This is not the intended behavior; we're supposed to pass original bytes verbatim.
// But we don't have the original bytes, we have NSStrings built by the URL loading system.
// It hopefully shouldn't matter, since RFC2616/RFC822 require ASCII-only headers,
// but surely someone out there is using non-ASCII characters, and hopefully UTF-8 is adequate here.
// It seems better than NSASCIIStringEncoding, which will lose information if non-ASCII is used.
NSDictionary *headerDict = [httpResponse allHeaderFields];
NSArray *keys = [[headerDict allKeys] sortedArrayUsingSelector:@selector(caseInsensitiveCompare:)];
NSEnumerator *i = [keys objectEnumerator];
NSString *k;
while ((k = [i nextObject]) != nil) {
NSString *v = [headerDict objectForKey:k];
[theHeaders appendData:[k dataUsingEncoding:NSUTF8StringEncoding]];
[theHeaders appendBytes:": " length:2];
[theHeaders appendData:[v dataUsingEncoding:NSUTF8StringEncoding]];
[theHeaders appendBytes:"\n" length:1];
}
// If the content is encoded (most likely compressed), then don't send its length to the plugin,
// which is only interested in the decoded length, not yet known at the moment.
// <rdar://problem/4470599> tracks a request for -[NSURLResponse expectedContentLength] to incorporate this logic.
NSString *contentEncoding = (NSString *)[[(NSHTTPURLResponse *)r allHeaderFields] objectForKey:@"Content-Encoding"];
if (contentEncoding && ![contentEncoding isEqualToString:@"identity"])
expectedContentLength = -1;
[theHeaders appendBytes:"\0" length:1];
}
startStream([r URL], expectedContentLength, WKGetNSURLResponseLastModifiedDate(r), [r MIMEType], theHeaders);
}
NPReason HostedNetscapePluginStream::reasonForError(NSError *error)
{
if (!error)
return NPRES_DONE;
if ([[error domain] isEqualToString:NSURLErrorDomain] && [error code] == NSURLErrorCancelled)
return NPRES_USER_BREAK;
return NPRES_NETWORK_ERR;
}
void HostedNetscapePluginStream::didFail(WebCore::NetscapePlugInStreamLoader*, const WebCore::ResourceError& error)
{
if (NetscapePluginHostProxy* hostProxy = m_instance->hostProxy())
_WKPHStreamDidFail(hostProxy->port(), m_instance->pluginID(), m_streamID, reasonForError(error));
m_instance->disconnectStream(this);
}
bool HostedNetscapePluginStream::wantsAllStreams() const
{
// FIXME: Implement.
return false;
}
void HostedNetscapePluginStream::start()
{
ASSERT(m_request);
ASSERT(!m_frameLoader);
ASSERT(!m_loader);
m_loader = NetscapePlugInStreamLoader::create(core([m_instance->pluginView() webFrame]), this);
m_loader->setShouldBufferData(false);
m_loader->documentLoader()->addPlugInStreamLoader(m_loader.get());
m_loader->load(m_request.get());
}
void HostedNetscapePluginStream::stop()
{
ASSERT(!m_frameLoader);
if (!m_loader->isDone())
m_loader->cancel(m_loader->cancelledError());
}
void HostedNetscapePluginStream::cancelLoad(NPReason reason)
{
cancelLoad(errorForReason(reason));
}
void HostedNetscapePluginStream::cancelLoad(NSError *error)
{
if (m_frameLoader) {
ASSERT(!m_loader);
DocumentLoader* documentLoader = m_frameLoader->activeDocumentLoader();
if (documentLoader && documentLoader->isLoadingMainResource())
documentLoader->cancelMainResourceLoad(error);
return;
}
if (!m_loader->isDone()) {
// Cancelling the load will disconnect the stream so there's no need to do it explicitly.
m_loader->cancel(error);
} else
m_instance->disconnectStream(this);
}
NSError *HostedNetscapePluginStream::pluginCancelledConnectionError() const
{
return [[[NSError alloc] _initWithPluginErrorCode:WebKitErrorPlugInCancelledConnection
contentURL:m_responseURL ? m_responseURL.get() : m_requestURL.get()
pluginPageURL:nil
pluginName:[[m_instance->pluginView() pluginPackage] pluginInfo].name
MIMEType:m_mimeType.get()] autorelease];
}
NSError *HostedNetscapePluginStream::errorForReason(NPReason reason) const
{
if (reason == NPRES_DONE)
return nil;
if (reason == NPRES_USER_BREAK)
return [NSError _webKitErrorWithDomain:NSURLErrorDomain
code:NSURLErrorCancelled
URL:m_responseURL ? m_responseURL.get() : m_requestURL.get()];
return pluginCancelledConnectionError();
}
} // namespace WebKit
#endif // USE(PLUGIN_HOST_PROCESS)
|