1 /****************************************************************************
3 ** Copyright (C) 2012 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
6 ** This file is part of the QtQml module of the Qt Toolkit.
8 ** $QT_BEGIN_LICENSE:LGPL$
9 ** Commercial License Usage
10 ** Licensees holding valid commercial Qt licenses may use this file in
11 ** accordance with the commercial license agreement provided with the
12 ** Software or, alternatively, in accordance with the terms contained in
13 ** a written agreement between you and Digia. For licensing terms and
14 ** conditions see http://qt.digia.com/licensing. For further information
15 ** use the contact form at http://qt.digia.com/contact-us.
17 ** GNU Lesser General Public License Usage
18 ** Alternatively, this file may be used under the terms of the GNU Lesser
19 ** General Public License version 2.1 as published by the Free Software
20 ** Foundation and appearing in the file LICENSE.LGPL included in the
21 ** packaging of this file. Please review the following information to
22 ** ensure the GNU Lesser General Public License version 2.1 requirements
23 ** will be met: http://www.gnu.org/licenses/old-licenses/lgpl-2.1.html.
25 ** In addition, as a special exception, Digia gives you certain additional
26 ** rights. These rights are described in the Digia Qt LGPL Exception
27 ** version 1.1, included in the file LGPL_EXCEPTION.txt in this package.
29 ** GNU General Public License Usage
30 ** Alternatively, this file may be used under the terms of the GNU
31 ** General Public License version 3.0 as published by the Free Software
32 ** Foundation and appearing in the file LICENSE.GPL included in the
33 ** packaging of this file. Please review the following information to
34 ** ensure the GNU General Public License version 3.0 requirements will be
35 ** met: http://www.gnu.org/copyleft/gpl.html.
40 ****************************************************************************/
42 #include "qqmlxmlhttprequest_p.h"
44 #include <private/qv8engine_p.h>
46 #include "qqmlengine.h"
47 #include "qqmlengine_p.h"
48 #include <private/qqmlrefcount_p.h>
49 #include "qqmlengine_p.h"
50 #include "qqmlexpression_p.h"
51 #include "qqmlglobal_p.h"
52 #include <private/qv8domerrors_p.h>
54 #include <QtCore/qobject.h>
55 #include <QtQml/qjsvalue.h>
56 #include <QtQml/qjsengine.h>
57 #include <QtNetwork/qnetworkreply.h>
58 #include <QtCore/qtextcodec.h>
59 #include <QtCore/qxmlstream.h>
60 #include <QtCore/qstack.h>
61 #include <QtCore/qdebug.h>
63 #ifndef QT_NO_XMLSTREAMREADER
65 #define V8THROW_REFERENCE(string) { \
66 v8::ThrowException(v8::Exception::ReferenceError(v8::String::New(string))); \
67 return v8::Handle<v8::Value>(); \
70 #define D(arg) (arg)->release()
71 #define A(arg) (arg)->addref()
75 DEFINE_BOOL_CONFIG_OPTION(xhrDump, QML_XHR_DUMP);
77 struct QQmlXMLHttpRequestData {
78 QQmlXMLHttpRequestData();
79 ~QQmlXMLHttpRequestData();
81 v8::Persistent<v8::Function> nodeFunction;
83 v8::Persistent<v8::Object> namedNodeMapPrototype;
84 v8::Persistent<v8::Object> nodeListPrototype;
85 v8::Persistent<v8::Object> nodePrototype;
86 v8::Persistent<v8::Object> elementPrototype;
87 v8::Persistent<v8::Object> attrPrototype;
88 v8::Persistent<v8::Object> characterDataPrototype;
89 v8::Persistent<v8::Object> textPrototype;
90 v8::Persistent<v8::Object> cdataPrototype;
91 v8::Persistent<v8::Object> documentPrototype;
93 v8::Local<v8::Object> newNode();
96 static inline QQmlXMLHttpRequestData *xhrdata(QV8Engine *engine)
98 return (QQmlXMLHttpRequestData *)engine->xmlHttpRequestData();
101 static v8::Local<v8::Object> constructMeObject(v8::Handle<v8::Object> thisObj, QV8Engine *e)
103 v8::Local<v8::Object> meObj = v8::Object::New();
104 meObj->Set(v8::String::New("ThisObject"), thisObj);
105 meObj->Set(v8::String::New("ActivationObject"), e->qmlScope(e->callingContext(), 0));
109 QQmlXMLHttpRequestData::QQmlXMLHttpRequestData()
113 QQmlXMLHttpRequestData::~QQmlXMLHttpRequestData()
115 qPersistentDispose(nodeFunction);
116 qPersistentDispose(namedNodeMapPrototype);
117 qPersistentDispose(nodeListPrototype);
118 qPersistentDispose(nodePrototype);
119 qPersistentDispose(elementPrototype);
120 qPersistentDispose(attrPrototype);
121 qPersistentDispose(characterDataPrototype);
122 qPersistentDispose(textPrototype);
123 qPersistentDispose(cdataPrototype);
124 qPersistentDispose(documentPrototype);
127 v8::Local<v8::Object> QQmlXMLHttpRequestData::newNode()
129 if (nodeFunction.IsEmpty()) {
130 v8::Local<v8::FunctionTemplate> ft = v8::FunctionTemplate::New();
131 ft->InstanceTemplate()->SetHasExternalResource(true);
132 nodeFunction = qPersistentNew<v8::Function>(ft->GetFunction());
135 return nodeFunction->NewInstance();
144 NodeImpl() : type(Element), document(0), parent(0) {}
145 virtual ~NodeImpl() {
146 for (int ii = 0; ii < children.count(); ++ii)
147 delete children.at(ii);
148 for (int ii = 0; ii < attributes.count(); ++ii)
149 delete attributes.at(ii);
152 // These numbers are copied from the Node IDL definition
158 DocumentFragment = 11,
164 ProcessingInstruction = 7,
169 QString namespaceUri;
177 DocumentImpl *document;
180 QList<NodeImpl *> children;
181 QList<NodeImpl *> attributes;
184 class DocumentImpl : public QQmlRefCount, public NodeImpl
187 DocumentImpl() : root(0) { type = Document; }
188 virtual ~DocumentImpl() {
189 if (root) delete root;
198 void addref() { QQmlRefCount::addref(); }
199 void release() { QQmlRefCount::release(); }
206 static v8::Handle<v8::Value> length(v8::Local<v8::String>, const v8::AccessorInfo& args);
207 static v8::Handle<v8::Value> indexed(uint32_t index, const v8::AccessorInfo& info);
208 static v8::Handle<v8::Value> named(v8::Local<v8::String> property, const v8::AccessorInfo& args);
211 static v8::Handle<v8::Object> prototype(QV8Engine *);
212 static v8::Handle<v8::Value> create(QV8Engine *, NodeImpl *, QList<NodeImpl *> *);
219 static v8::Handle<v8::Value> length(v8::Local<v8::String>, const v8::AccessorInfo& args);
220 static v8::Handle<v8::Value> indexed(uint32_t index, const v8::AccessorInfo& info);
223 static v8::Handle<v8::Object> prototype(QV8Engine *);
224 static v8::Handle<v8::Value> create(QV8Engine *, NodeImpl *);
231 static v8::Handle<v8::Value> nodeName(v8::Local<v8::String>, const v8::AccessorInfo& args);
232 static v8::Handle<v8::Value> nodeValue(v8::Local<v8::String>, const v8::AccessorInfo& args);
233 static v8::Handle<v8::Value> nodeType(v8::Local<v8::String>, const v8::AccessorInfo& args);
235 static v8::Handle<v8::Value> parentNode(v8::Local<v8::String>, const v8::AccessorInfo& args);
236 static v8::Handle<v8::Value> childNodes(v8::Local<v8::String>, const v8::AccessorInfo& args);
237 static v8::Handle<v8::Value> firstChild(v8::Local<v8::String>, const v8::AccessorInfo& args);
238 static v8::Handle<v8::Value> lastChild(v8::Local<v8::String>, const v8::AccessorInfo& args);
239 static v8::Handle<v8::Value> previousSibling(v8::Local<v8::String>, const v8::AccessorInfo& args);
240 static v8::Handle<v8::Value> nextSibling(v8::Local<v8::String>, const v8::AccessorInfo& args);
241 static v8::Handle<v8::Value> attributes(v8::Local<v8::String>, const v8::AccessorInfo& args);
243 //static v8::Handle<v8::Value> ownerDocument(v8::Local<v8::String>, const v8::AccessorInfo& args);
244 //static v8::Handle<v8::Value> namespaceURI(v8::Local<v8::String>, const v8::AccessorInfo& args);
245 //static v8::Handle<v8::Value> prefix(v8::Local<v8::String>, const v8::AccessorInfo& args);
246 //static v8::Handle<v8::Value> localName(v8::Local<v8::String>, const v8::AccessorInfo& args);
247 //static v8::Handle<v8::Value> baseURI(v8::Local<v8::String>, const v8::AccessorInfo& args);
248 //static v8::Handle<v8::Value> textContent(v8::Local<v8::String>, const v8::AccessorInfo& args);
251 static v8::Handle<v8::Object> prototype(QV8Engine *);
252 static v8::Handle<v8::Value> create(QV8Engine *, NodeImpl *);
262 Node &operator=(const Node &);
265 class Element : public Node
269 static v8::Handle<v8::Object> prototype(QV8Engine *);
272 class Attr : public Node
276 static v8::Handle<v8::Value> name(v8::Local<v8::String>, const v8::AccessorInfo& args);
277 static v8::Handle<v8::Value> specified(v8::Local<v8::String>, const v8::AccessorInfo& args);
278 static v8::Handle<v8::Value> value(v8::Local<v8::String>, const v8::AccessorInfo& args);
279 static v8::Handle<v8::Value> ownerElement(v8::Local<v8::String>, const v8::AccessorInfo& args);
280 static v8::Handle<v8::Value> schemaTypeInfo(v8::Local<v8::String>, const v8::AccessorInfo& args);
281 static v8::Handle<v8::Value> isId(v8::Local<v8::String>, const v8::AccessorInfo& args);
284 static v8::Handle<v8::Object> prototype(QV8Engine *);
287 class CharacterData : public Node
291 static v8::Handle<v8::Value> length(v8::Local<v8::String>, const v8::AccessorInfo& args);
294 static v8::Handle<v8::Object> prototype(QV8Engine *);
297 class Text : public CharacterData
301 static v8::Handle<v8::Value> isElementContentWhitespace(v8::Local<v8::String>, const v8::AccessorInfo& args);
302 static v8::Handle<v8::Value> wholeText(v8::Local<v8::String>, const v8::AccessorInfo& args);
305 static v8::Handle<v8::Object> prototype(QV8Engine *);
308 class CDATA : public Text
312 static v8::Handle<v8::Object> prototype(QV8Engine *);
315 class Document : public Node
319 static v8::Handle<v8::Value> xmlVersion(v8::Local<v8::String>, const v8::AccessorInfo& args);
320 static v8::Handle<v8::Value> xmlEncoding(v8::Local<v8::String>, const v8::AccessorInfo& args);
321 static v8::Handle<v8::Value> xmlStandalone(v8::Local<v8::String>, const v8::AccessorInfo& args);
322 static v8::Handle<v8::Value> documentElement(v8::Local<v8::String>, const v8::AccessorInfo& args);
325 static v8::Handle<v8::Object> prototype(QV8Engine *);
326 static v8::Handle<v8::Value> load(QV8Engine *engine, const QByteArray &data);
331 class QQmlDOMNodeResource : public QV8ObjectResource, public Node
333 V8_RESOURCE_TYPE(DOMNodeType);
335 QQmlDOMNodeResource(QV8Engine *e);
337 QList<NodeImpl *> *list; // Only used in NamedNodeMap
340 QQmlDOMNodeResource::QQmlDOMNodeResource(QV8Engine *e)
341 : QV8ObjectResource(e), list(0)
347 Q_DECLARE_METATYPE(Node)
348 Q_DECLARE_METATYPE(NodeList)
349 Q_DECLARE_METATYPE(NamedNodeMap)
353 void NodeImpl::addref()
358 void NodeImpl::release()
363 v8::Handle<v8::Value> Node::nodeName(v8::Local<v8::String>, const v8::AccessorInfo &args)
365 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
366 if (!r) return v8::Undefined();
367 QV8Engine *engine = V8ENGINE();
369 switch (r->d->type) {
370 case NodeImpl::Document:
371 return v8::String::New("#document");
372 case NodeImpl::CDATA:
373 return v8::String::New("#cdata-section");
375 return v8::String::New("#text");
377 return engine->toString(r->d->name);
381 v8::Handle<v8::Value> Node::nodeValue(v8::Local<v8::String>, const v8::AccessorInfo &args)
383 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
384 if (!r) return v8::Undefined();
385 QV8Engine *engine = V8ENGINE();
387 if (r->d->type == NodeImpl::Document ||
388 r->d->type == NodeImpl::DocumentFragment ||
389 r->d->type == NodeImpl::DocumentType ||
390 r->d->type == NodeImpl::Element ||
391 r->d->type == NodeImpl::Entity ||
392 r->d->type == NodeImpl::EntityReference ||
393 r->d->type == NodeImpl::Notation)
396 return engine->toString(r->d->data);
399 v8::Handle<v8::Value> Node::nodeType(v8::Local<v8::String>, const v8::AccessorInfo &args)
401 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
402 if (!r) return v8::Undefined();
403 return v8::Integer::New(r->d->type);
406 v8::Handle<v8::Value> Node::parentNode(v8::Local<v8::String>, const v8::AccessorInfo &args)
408 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
409 if (!r) return v8::Undefined();
410 QV8Engine *engine = V8ENGINE();
412 if (r->d->parent) return Node::create(engine, r->d->parent);
413 else return v8::Null();
416 v8::Handle<v8::Value> Node::childNodes(v8::Local<v8::String>, const v8::AccessorInfo &args)
418 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
419 if (!r) return v8::Undefined();
420 QV8Engine *engine = V8ENGINE();
422 return NodeList::create(engine, r->d);
425 v8::Handle<v8::Value> Node::firstChild(v8::Local<v8::String>, const v8::AccessorInfo &args)
427 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
428 if (!r) return v8::Undefined();
429 QV8Engine *engine = V8ENGINE();
431 if (r->d->children.isEmpty()) return v8::Null();
432 else return Node::create(engine, r->d->children.first());
435 v8::Handle<v8::Value> Node::lastChild(v8::Local<v8::String>, const v8::AccessorInfo &args)
437 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
438 if (!r) return v8::Undefined();
439 QV8Engine *engine = V8ENGINE();
441 if (r->d->children.isEmpty()) return v8::Null();
442 else return Node::create(engine, r->d->children.last());
445 v8::Handle<v8::Value> Node::previousSibling(v8::Local<v8::String>, const v8::AccessorInfo &args)
447 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
448 if (!r) return v8::Undefined();
449 QV8Engine *engine = V8ENGINE();
451 if (!r->d->parent) return v8::Null();
453 for (int ii = 0; ii < r->d->parent->children.count(); ++ii) {
454 if (r->d->parent->children.at(ii) == r->d) {
455 if (ii == 0) return v8::Null();
456 else return Node::create(engine, r->d->parent->children.at(ii - 1));
463 v8::Handle<v8::Value> Node::nextSibling(v8::Local<v8::String>, const v8::AccessorInfo &args)
465 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
466 if (!r) return v8::Undefined();
467 QV8Engine *engine = V8ENGINE();
469 if (!r->d->parent) return v8::Null();
471 for (int ii = 0; ii < r->d->parent->children.count(); ++ii) {
472 if (r->d->parent->children.at(ii) == r->d) {
473 if ((ii + 1) == r->d->parent->children.count()) return v8::Null();
474 else return Node::create(engine, r->d->parent->children.at(ii + 1));
481 v8::Handle<v8::Value> Node::attributes(v8::Local<v8::String>, const v8::AccessorInfo &args)
483 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
484 if (!r) return v8::Undefined();
485 QV8Engine *engine = V8ENGINE();
487 if (r->d->type != NodeImpl::Element)
490 return NamedNodeMap::create(engine, r->d, &r->d->attributes);
493 v8::Handle<v8::Object> Node::prototype(QV8Engine *engine)
495 QQmlXMLHttpRequestData *d = xhrdata(engine);
496 if (d->nodePrototype.IsEmpty()) {
497 d->nodePrototype = qPersistentNew<v8::Object>(v8::Object::New());
498 d->nodePrototype->SetAccessor(v8::String::New("nodeName"), nodeName,
499 0, v8::External::Wrap(engine));
500 d->nodePrototype->SetAccessor(v8::String::New("nodeValue"), nodeValue,
501 0, v8::External::Wrap(engine));
502 d->nodePrototype->SetAccessor(v8::String::New("nodeType"), nodeType,
503 0, v8::External::Wrap(engine));
504 d->nodePrototype->SetAccessor(v8::String::New("parentNode"), parentNode,
505 0, v8::External::Wrap(engine));
506 d->nodePrototype->SetAccessor(v8::String::New("childNodes"), childNodes,
507 0, v8::External::Wrap(engine));
508 d->nodePrototype->SetAccessor(v8::String::New("firstChild"), firstChild,
509 0, v8::External::Wrap(engine));
510 d->nodePrototype->SetAccessor(v8::String::New("lastChild"), lastChild,
511 0, v8::External::Wrap(engine));
512 d->nodePrototype->SetAccessor(v8::String::New("previousSibling"), previousSibling,
513 0, v8::External::Wrap(engine));
514 d->nodePrototype->SetAccessor(v8::String::New("nextSibling"), nextSibling,
515 0, v8::External::Wrap(engine));
516 d->nodePrototype->SetAccessor(v8::String::New("attributes"), attributes,
517 0, v8::External::Wrap(engine));
518 engine->freezeObject(d->nodePrototype);
520 return d->nodePrototype;
523 v8::Handle<v8::Value> Node::create(QV8Engine *engine, NodeImpl *data)
525 QQmlXMLHttpRequestData *d = xhrdata(engine);
526 v8::Local<v8::Object> instance = d->newNode();
528 switch (data->type) {
530 instance->SetPrototype(Attr::prototype(engine));
532 case NodeImpl::Comment:
533 case NodeImpl::Document:
534 case NodeImpl::DocumentFragment:
535 case NodeImpl::DocumentType:
536 case NodeImpl::Entity:
537 case NodeImpl::EntityReference:
538 case NodeImpl::Notation:
539 case NodeImpl::ProcessingInstruction:
540 return v8::Undefined();
541 case NodeImpl::CDATA:
542 instance->SetPrototype(CDATA::prototype(engine));
545 instance->SetPrototype(Text::prototype(engine));
547 case NodeImpl::Element:
548 instance->SetPrototype(Element::prototype(engine));
552 QQmlDOMNodeResource *r = new QQmlDOMNodeResource(engine);
555 instance->SetExternalResource(r);
560 v8::Handle<v8::Object> Element::prototype(QV8Engine *engine)
562 QQmlXMLHttpRequestData *d = xhrdata(engine);
563 if (d->elementPrototype.IsEmpty()) {
564 d->elementPrototype = qPersistentNew<v8::Object>(v8::Object::New());
565 d->elementPrototype->SetPrototype(Node::prototype(engine));
566 d->elementPrototype->SetAccessor(v8::String::New("tagName"), nodeName,
567 0, v8::External::Wrap(engine));
568 engine->freezeObject(d->elementPrototype);
570 return d->elementPrototype;
573 v8::Handle<v8::Object> Attr::prototype(QV8Engine *engine)
575 QQmlXMLHttpRequestData *d = xhrdata(engine);
576 if (d->attrPrototype.IsEmpty()) {
577 d->attrPrototype = qPersistentNew<v8::Object>(v8::Object::New());
578 d->attrPrototype->SetPrototype(Node::prototype(engine));
579 d->attrPrototype->SetAccessor(v8::String::New("name"), name,
580 0, v8::External::Wrap(engine));
581 d->attrPrototype->SetAccessor(v8::String::New("value"), value,
582 0, v8::External::Wrap(engine));
583 d->attrPrototype->SetAccessor(v8::String::New("ownerElement"), ownerElement,
584 0, v8::External::Wrap(engine));
585 engine->freezeObject(d->attrPrototype);
587 return d->attrPrototype;
590 v8::Handle<v8::Value> Attr::name(v8::Local<v8::String>, const v8::AccessorInfo &args)
592 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
593 if (!r) return v8::Undefined();
594 QV8Engine *engine = V8ENGINE();
596 return engine->toString(r->d->name);
599 v8::Handle<v8::Value> Attr::value(v8::Local<v8::String>, const v8::AccessorInfo &args)
601 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
602 if (!r) return v8::Undefined();
603 QV8Engine *engine = V8ENGINE();
605 return engine->toString(r->d->data);
608 v8::Handle<v8::Value> Attr::ownerElement(v8::Local<v8::String>, const v8::AccessorInfo &args)
610 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
611 if (!r) return v8::Undefined();
612 QV8Engine *engine = V8ENGINE();
614 return Node::create(engine, r->d->parent);
617 v8::Handle<v8::Value> CharacterData::length(v8::Local<v8::String>, const v8::AccessorInfo &args)
619 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
620 if (!r) return v8::Undefined();
621 QV8Engine *engine = V8ENGINE();
623 return v8::Integer::New(r->d->data.length());
626 v8::Handle<v8::Object> CharacterData::prototype(QV8Engine *engine)
628 QQmlXMLHttpRequestData *d = xhrdata(engine);
629 if (d->characterDataPrototype.IsEmpty()) {
630 d->characterDataPrototype = qPersistentNew<v8::Object>(v8::Object::New());
631 d->characterDataPrototype->SetPrototype(Node::prototype(engine));
632 d->characterDataPrototype->SetAccessor(v8::String::New("data"), nodeValue,
633 0, v8::External::Wrap(engine));
634 d->characterDataPrototype->SetAccessor(v8::String::New("length"), length,
635 0, v8::External::Wrap(engine));
636 engine->freezeObject(d->characterDataPrototype);
638 return d->characterDataPrototype;
641 v8::Handle<v8::Value> Text::isElementContentWhitespace(v8::Local<v8::String>, const v8::AccessorInfo &args)
643 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
644 if (!r) return v8::Undefined();
645 QV8Engine *engine = V8ENGINE();
647 return v8::Boolean::New(r->d->data.trimmed().isEmpty());
650 v8::Handle<v8::Value> Text::wholeText(v8::Local<v8::String>, const v8::AccessorInfo &args)
652 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
653 if (!r) return v8::Undefined();
654 QV8Engine *engine = V8ENGINE();
656 return engine->toString(r->d->data);
659 v8::Handle<v8::Object> Text::prototype(QV8Engine *engine)
661 QQmlXMLHttpRequestData *d = xhrdata(engine);
662 if (d->textPrototype.IsEmpty()) {
663 d->textPrototype = qPersistentNew<v8::Object>(v8::Object::New());
664 d->textPrototype->SetPrototype(CharacterData::prototype(engine));
665 d->textPrototype->SetAccessor(v8::String::New("isElementContentWhitespace"), isElementContentWhitespace,
666 0, v8::External::Wrap(engine));
667 d->textPrototype->SetAccessor(v8::String::New("wholeText"), wholeText,
668 0, v8::External::Wrap(engine));
669 engine->freezeObject(d->textPrototype);
671 return d->textPrototype;
674 v8::Handle<v8::Object> CDATA::prototype(QV8Engine *engine)
676 QQmlXMLHttpRequestData *d = xhrdata(engine);
677 if (d->cdataPrototype.IsEmpty()) {
678 d->cdataPrototype = qPersistentNew<v8::Object>(v8::Object::New());
679 d->cdataPrototype->SetPrototype(Text::prototype(engine));
680 engine->freezeObject(d->cdataPrototype);
682 return d->cdataPrototype;
685 v8::Handle<v8::Object> Document::prototype(QV8Engine *engine)
687 QQmlXMLHttpRequestData *d = xhrdata(engine);
688 if (d->documentPrototype.IsEmpty()) {
689 d->documentPrototype = qPersistentNew<v8::Object>(v8::Object::New());
690 d->documentPrototype->SetPrototype(Node::prototype(engine));
691 d->documentPrototype->SetAccessor(v8::String::New("xmlVersion"), xmlVersion,
692 0, v8::External::Wrap(engine));
693 d->documentPrototype->SetAccessor(v8::String::New("xmlEncoding"), xmlEncoding,
694 0, v8::External::Wrap(engine));
695 d->documentPrototype->SetAccessor(v8::String::New("xmlStandalone"), xmlStandalone,
696 0, v8::External::Wrap(engine));
697 d->documentPrototype->SetAccessor(v8::String::New("documentElement"), documentElement,
698 0, v8::External::Wrap(engine));
699 engine->freezeObject(d->documentPrototype);
701 return d->documentPrototype;
704 v8::Handle<v8::Value> Document::load(QV8Engine *engine, const QByteArray &data)
708 DocumentImpl *document = 0;
709 QStack<NodeImpl *> nodeStack;
711 QXmlStreamReader reader(data);
713 while (!reader.atEnd()) {
714 switch (reader.readNext()) {
715 case QXmlStreamReader::NoToken:
717 case QXmlStreamReader::Invalid:
719 case QXmlStreamReader::StartDocument:
721 document = new DocumentImpl;
722 document->document = document;
723 document->version = reader.documentVersion().toString();
724 document->encoding = reader.documentEncoding().toString();
725 document->isStandalone = reader.isStandaloneDocument();
727 case QXmlStreamReader::EndDocument:
729 case QXmlStreamReader::StartElement:
732 NodeImpl *node = new NodeImpl;
733 node->document = document;
734 node->namespaceUri = reader.namespaceUri().toString();
735 node->name = reader.name().toString();
736 if (nodeStack.isEmpty()) {
737 document->root = node;
739 node->parent = nodeStack.top();
740 node->parent->children.append(node);
742 nodeStack.append(node);
744 foreach (const QXmlStreamAttribute &a, reader.attributes()) {
745 NodeImpl *attr = new NodeImpl;
746 attr->document = document;
747 attr->type = NodeImpl::Attr;
748 attr->namespaceUri = a.namespaceUri().toString();
749 attr->name = a.name().toString();
750 attr->data = a.value().toString();
752 node->attributes.append(attr);
756 case QXmlStreamReader::EndElement:
759 case QXmlStreamReader::Characters:
761 NodeImpl *node = new NodeImpl;
762 node->document = document;
763 node->type = reader.isCDATA()?NodeImpl::CDATA:NodeImpl::Text;
764 node->parent = nodeStack.top();
765 node->parent->children.append(node);
766 node->data = reader.text().toString();
769 case QXmlStreamReader::Comment:
771 case QXmlStreamReader::DTD:
773 case QXmlStreamReader::EntityReference:
775 case QXmlStreamReader::ProcessingInstruction:
780 if (!document || reader.hasError()) {
781 if (document) D(document);
785 v8::Local<v8::Object> instance = xhrdata(engine)->newNode();
786 QQmlDOMNodeResource *r = new QQmlDOMNodeResource(engine);
788 instance->SetExternalResource(r);
789 instance->SetPrototype(Document::prototype(engine));
798 Node::Node(const Node &o)
809 bool Node::isNull() const
814 v8::Handle<v8::Value> NamedNodeMap::length(v8::Local<v8::String>, const v8::AccessorInfo &args)
816 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
817 if (!r) return v8::Undefined();
818 QV8Engine *engine = V8ENGINE();
820 return v8::Integer::New(r->list->count());
823 v8::Handle<v8::Value> NamedNodeMap::indexed(uint32_t index, const v8::AccessorInfo& args)
825 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
826 if (!r || !r->list) return v8::Undefined();
827 QV8Engine *engine = V8ENGINE();
829 if ((int)index < r->list->count()) {
830 return Node::create(engine, r->list->at(index));
832 return v8::Undefined();
836 v8::Handle<v8::Value> NamedNodeMap::named(v8::Local<v8::String> property, const v8::AccessorInfo& args)
838 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
839 if (!r || !r->list) return v8::Undefined();
840 QV8Engine *engine = V8ENGINE();
842 QString str = engine->toString(property);
843 for (int ii = 0; ii < r->list->count(); ++ii) {
844 if (r->list->at(ii)->name == str) {
845 return Node::create(engine, r->list->at(ii));
849 return v8::Undefined();
852 v8::Handle<v8::Object> NamedNodeMap::prototype(QV8Engine *engine)
854 QQmlXMLHttpRequestData *d = xhrdata(engine);
855 if (d->namedNodeMapPrototype.IsEmpty()) {
856 v8::Local<v8::ObjectTemplate> ot = v8::ObjectTemplate::New();
857 ot->SetAccessor(v8::String::New("length"), length, 0, v8::External::Wrap(engine));
858 ot->SetIndexedPropertyHandler(indexed, 0, 0, 0, 0, v8::External::Wrap(engine));
859 ot->SetFallbackPropertyHandler(named, 0, 0, 0, 0, v8::External::Wrap(engine));
860 d->namedNodeMapPrototype = qPersistentNew<v8::Object>(ot->NewInstance());
861 engine->freezeObject(d->namedNodeMapPrototype);
863 return d->namedNodeMapPrototype;
866 v8::Handle<v8::Value> NamedNodeMap::create(QV8Engine *engine, NodeImpl *data, QList<NodeImpl *> *list)
868 QQmlXMLHttpRequestData *d = xhrdata(engine);
869 v8::Local<v8::Object> instance = d->newNode();
870 instance->SetPrototype(NamedNodeMap::prototype(engine));
871 QQmlDOMNodeResource *r = new QQmlDOMNodeResource(engine);
875 instance->SetExternalResource(r);
879 v8::Handle<v8::Value> NodeList::indexed(uint32_t index, const v8::AccessorInfo& args)
881 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
882 if (!r) return v8::Undefined();
883 QV8Engine *engine = V8ENGINE();
885 if ((int)index < r->d->children.count()) {
886 return Node::create(engine, r->d->children.at(index));
888 return v8::Undefined();
892 v8::Handle<v8::Value> NodeList::length(v8::Local<v8::String>, const v8::AccessorInfo& args)
894 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
895 if (!r) return v8::Undefined();
896 QV8Engine *engine = V8ENGINE();
898 return v8::Integer::New(r->d->children.count());
901 v8::Handle<v8::Object> NodeList::prototype(QV8Engine *engine)
903 QQmlXMLHttpRequestData *d = xhrdata(engine);
904 if (d->nodeListPrototype.IsEmpty()) {
905 v8::Local<v8::ObjectTemplate> ot = v8::ObjectTemplate::New();
906 ot->SetAccessor(v8::String::New("length"), length, 0, v8::External::Wrap(engine));
907 ot->SetIndexedPropertyHandler(indexed, 0, 0, 0, 0, v8::External::Wrap(engine));
908 d->nodeListPrototype = qPersistentNew<v8::Object>(ot->NewInstance());
909 engine->freezeObject(d->nodeListPrototype);
911 return d->nodeListPrototype;
914 v8::Handle<v8::Value> NodeList::create(QV8Engine *engine, NodeImpl *data)
916 QQmlXMLHttpRequestData *d = xhrdata(engine);
917 v8::Local<v8::Object> instance = d->newNode();
918 instance->SetPrototype(NodeList::prototype(engine));
919 QQmlDOMNodeResource *r = new QQmlDOMNodeResource(engine);
922 instance->SetExternalResource(r);
926 v8::Handle<v8::Value> Document::documentElement(v8::Local<v8::String>, const v8::AccessorInfo& args)
928 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
929 if (!r || r->d->type != NodeImpl::Document) return v8::Undefined();
930 QV8Engine *engine = V8ENGINE();
932 return Node::create(engine, static_cast<DocumentImpl *>(r->d)->root);
935 v8::Handle<v8::Value> Document::xmlStandalone(v8::Local<v8::String>, const v8::AccessorInfo& args)
937 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
938 if (!r || r->d->type != NodeImpl::Document) return v8::Undefined();
939 QV8Engine *engine = V8ENGINE();
941 return v8::Boolean::New(static_cast<DocumentImpl *>(r->d)->isStandalone);
944 v8::Handle<v8::Value> Document::xmlVersion(v8::Local<v8::String>, const v8::AccessorInfo& args)
946 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
947 if (!r || r->d->type != NodeImpl::Document) return v8::Undefined();
948 QV8Engine *engine = V8ENGINE();
950 return engine->toString(static_cast<DocumentImpl *>(r->d)->version);
953 v8::Handle<v8::Value> Document::xmlEncoding(v8::Local<v8::String>, const v8::AccessorInfo& args)
955 QQmlDOMNodeResource *r = v8_resource_cast<QQmlDOMNodeResource>(args.This());
956 if (!r || r->d->type != NodeImpl::Document) return v8::Undefined();
957 QV8Engine *engine = V8ENGINE();
959 return engine->toString(static_cast<DocumentImpl *>(r->d)->encoding);
962 class QQmlXMLHttpRequest : public QObject, public QV8ObjectResource
965 V8_RESOURCE_TYPE(XMLHttpRequestType)
967 enum State { Unsent = 0,
968 Opened = 1, HeadersReceived = 2,
969 Loading = 3, Done = 4 };
971 QQmlXMLHttpRequest(QV8Engine *engine, QNetworkAccessManager *manager);
972 virtual ~QQmlXMLHttpRequest();
974 bool sendFlag() const;
975 bool errorFlag() const;
976 quint32 readyState() const;
977 int replyStatus() const;
978 QString replyStatusText() const;
980 v8::Handle<v8::Value> open(v8::Handle<v8::Object> me, const QString &, const QUrl &);
981 v8::Handle<v8::Value> send(v8::Handle<v8::Object> me, const QByteArray &);
982 v8::Handle<v8::Value> abort(v8::Handle<v8::Object> me);
984 void addHeader(const QString &, const QString &);
985 QString header(const QString &name);
989 QString responseBody();
990 const QByteArray & rawResponseBody() const;
991 bool receivedXml() const;
994 void error(QNetworkReply::NetworkError);
998 void requestFromUrl(const QUrl &url);
1005 QByteArray m_responseEntityBody;
1007 int m_redirectCount;
1009 typedef QPair<QByteArray, QByteArray> HeaderPair;
1010 typedef QList<HeaderPair> HeadersList;
1011 HeadersList m_headersList;
1012 void fillHeadersList();
1016 QByteArray m_charset;
1017 QTextCodec *m_textCodec;
1018 #ifndef QT_NO_TEXTCODEC
1019 QTextCodec* findTextCodec() const;
1021 void readEncoding();
1023 v8::Handle<v8::Object> getMe() const;
1024 void setMe(v8::Handle<v8::Object> me);
1025 v8::Persistent<v8::Object> m_me;
1027 void dispatchCallback(v8::Handle<v8::Object> me);
1028 void printError(v8::Handle<v8::Message>);
1031 QString m_statusText;
1032 QNetworkRequest m_request;
1033 QQmlGuard<QNetworkReply> m_network;
1034 void destroyNetwork();
1036 QNetworkAccessManager *m_nam;
1037 QNetworkAccessManager *networkAccessManager() { return m_nam; }
1040 QQmlXMLHttpRequest::QQmlXMLHttpRequest(QV8Engine *engine, QNetworkAccessManager *manager)
1041 : QV8ObjectResource(engine), m_state(Unsent), m_errorFlag(false), m_sendFlag(false),
1042 m_redirectCount(0), m_gotXml(false), m_textCodec(0), m_network(0), m_nam(manager)
1046 QQmlXMLHttpRequest::~QQmlXMLHttpRequest()
1051 bool QQmlXMLHttpRequest::sendFlag() const
1056 bool QQmlXMLHttpRequest::errorFlag() const
1061 quint32 QQmlXMLHttpRequest::readyState() const
1066 int QQmlXMLHttpRequest::replyStatus() const
1071 QString QQmlXMLHttpRequest::replyStatusText() const
1073 return m_statusText;
1076 v8::Handle<v8::Value> QQmlXMLHttpRequest::open(v8::Handle<v8::Object> me, const QString &method,
1081 m_errorFlag = false;
1082 m_responseEntityBody = QByteArray();
1087 dispatchCallback(me);
1088 if (tc.HasCaught()) printError(tc.Message());
1089 return v8::Undefined();
1092 void QQmlXMLHttpRequest::addHeader(const QString &name, const QString &value)
1094 QByteArray utfname = name.toUtf8();
1096 if (m_request.hasRawHeader(utfname)) {
1097 m_request.setRawHeader(utfname, m_request.rawHeader(utfname) + ',' + value.toUtf8());
1099 m_request.setRawHeader(utfname, value.toUtf8());
1103 QString QQmlXMLHttpRequest::header(const QString &name)
1105 QByteArray utfname = name.toLower().toUtf8();
1107 foreach (const HeaderPair &header, m_headersList) {
1108 if (header.first == utfname)
1109 return QString::fromUtf8(header.second);
1114 QString QQmlXMLHttpRequest::headers()
1118 foreach (const HeaderPair &header, m_headersList) {
1120 ret.append(QLatin1String("\r\n"));
1121 ret = ret % QString::fromUtf8(header.first) % QLatin1String(": ")
1122 % QString::fromUtf8(header.second);
1127 void QQmlXMLHttpRequest::fillHeadersList()
1129 QList<QByteArray> headerList = m_network->rawHeaderList();
1131 m_headersList.clear();
1132 foreach (const QByteArray &header, headerList) {
1133 HeaderPair pair (header.toLower(), m_network->rawHeader(header));
1134 if (pair.first == "set-cookie" ||
1135 pair.first == "set-cookie2")
1138 m_headersList << pair;
1142 void QQmlXMLHttpRequest::requestFromUrl(const QUrl &url)
1144 QNetworkRequest request = m_request;
1145 request.setUrl(url);
1146 if(m_method == QLatin1String("POST") ||
1147 m_method == QLatin1String("PUT")) {
1148 QVariant var = request.header(QNetworkRequest::ContentTypeHeader);
1149 if (var.isValid()) {
1150 QString str = var.toString();
1151 int charsetIdx = str.indexOf(QLatin1String("charset="));
1152 if (charsetIdx == -1) {
1153 // No charset - append
1154 if (!str.isEmpty()) str.append(QLatin1Char(';'));
1155 str.append(QLatin1String("charset=UTF-8"));
1159 int semiColon = str.indexOf(QLatin1Char(';'), charsetIdx);
1160 if (semiColon == -1) {
1161 n = str.length() - charsetIdx;
1163 n = semiColon - charsetIdx;
1166 str.replace(charsetIdx, n, QLatin1String("UTF-8"));
1168 request.setHeader(QNetworkRequest::ContentTypeHeader, str);
1170 request.setHeader(QNetworkRequest::ContentTypeHeader,
1171 QLatin1String("text/plain;charset=UTF-8"));
1176 qWarning().nospace() << "XMLHttpRequest: " << qPrintable(m_method) << ' ' << qPrintable(url.toString());
1177 if (!m_data.isEmpty()) {
1178 qWarning().nospace() << " "
1179 << qPrintable(QString::fromUtf8(m_data));
1183 if (m_method == QLatin1String("GET"))
1184 m_network = networkAccessManager()->get(request);
1185 else if (m_method == QLatin1String("HEAD"))
1186 m_network = networkAccessManager()->head(request);
1187 else if (m_method == QLatin1String("POST"))
1188 m_network = networkAccessManager()->post(request, m_data);
1189 else if (m_method == QLatin1String("PUT"))
1190 m_network = networkAccessManager()->put(request, m_data);
1191 else if (m_method == QLatin1String("DELETE"))
1192 m_network = networkAccessManager()->deleteResource(request);
1194 QObject::connect(m_network, SIGNAL(readyRead()),
1195 this, SLOT(readyRead()));
1196 QObject::connect(m_network, SIGNAL(error(QNetworkReply::NetworkError)),
1197 this, SLOT(error(QNetworkReply::NetworkError)));
1198 QObject::connect(m_network, SIGNAL(finished()),
1199 this, SLOT(finished()));
1202 v8::Handle<v8::Value> QQmlXMLHttpRequest::send(v8::Handle<v8::Object> me, const QByteArray &data)
1204 m_errorFlag = false;
1206 m_redirectCount = 0;
1211 requestFromUrl(m_url);
1213 return v8::Undefined();
1216 v8::Handle<v8::Value> QQmlXMLHttpRequest::abort(v8::Handle<v8::Object> me)
1219 m_responseEntityBody = QByteArray();
1221 m_request = QNetworkRequest();
1223 if (!(m_state == Unsent ||
1224 (m_state == Opened && !m_sendFlag) ||
1230 dispatchCallback(me);
1231 if (tc.HasCaught()) printError(tc.Message());
1236 return v8::Undefined();
1239 v8::Handle<v8::Object> QQmlXMLHttpRequest::getMe() const
1244 void QQmlXMLHttpRequest::setMe(v8::Handle<v8::Object> me)
1246 qPersistentDispose(m_me);
1249 m_me = qPersistentNew<v8::Object>(me);
1252 void QQmlXMLHttpRequest::readyRead()
1254 v8::HandleScope handle_scope;
1257 m_network->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
1259 QString::fromUtf8(m_network->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray());
1261 // ### We assume if this is called the headers are now available
1262 if (m_state < HeadersReceived) {
1263 m_state = HeadersReceived;
1266 dispatchCallback(m_me);
1267 if (tc.HasCaught()) printError(tc.Message());
1270 bool wasEmpty = m_responseEntityBody.isEmpty();
1271 m_responseEntityBody.append(m_network->readAll());
1272 if (wasEmpty && !m_responseEntityBody.isEmpty())
1275 dispatchCallback(m_me);
1276 if (tc.HasCaught()) printError(tc.Message());
1279 static const char *errorToString(QNetworkReply::NetworkError error)
1281 int idx = QNetworkReply::staticMetaObject.indexOfEnumerator("NetworkError");
1282 if (idx == -1) return "EnumLookupFailed";
1284 QMetaEnum e = QNetworkReply::staticMetaObject.enumerator(idx);
1286 const char *name = e.valueToKey(error);
1287 if (!name) return "EnumLookupFailed";
1291 void QQmlXMLHttpRequest::error(QNetworkReply::NetworkError error)
1293 v8::HandleScope handle_scope;
1296 m_network->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
1298 QString::fromUtf8(m_network->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray());
1300 m_request = QNetworkRequest();
1305 qWarning().nospace() << "XMLHttpRequest: ERROR " << qPrintable(m_url.toString());
1306 qWarning().nospace() << " " << error << ' ' << errorToString(error) << ' ' << m_statusText;
1309 if (error == QNetworkReply::ContentAccessDenied ||
1310 error == QNetworkReply::ContentOperationNotPermittedError ||
1311 error == QNetworkReply::ContentNotFoundError ||
1312 error == QNetworkReply::AuthenticationRequiredError ||
1313 error == QNetworkReply::ContentReSendError ||
1314 error == QNetworkReply::UnknownContentError) {
1317 dispatchCallback(m_me);
1318 if (tc.HasCaught()) printError(tc.Message());
1321 m_responseEntityBody = QByteArray();
1327 dispatchCallback(m_me);
1328 if (tc.HasCaught()) printError(tc.Message());
1331 #define XMLHTTPREQUEST_MAXIMUM_REDIRECT_RECURSION 15
1332 void QQmlXMLHttpRequest::finished()
1334 v8::HandleScope handle_scope;
1337 if (m_redirectCount < XMLHTTPREQUEST_MAXIMUM_REDIRECT_RECURSION) {
1338 QVariant redirect = m_network->attribute(QNetworkRequest::RedirectionTargetAttribute);
1339 if (redirect.isValid()) {
1340 QUrl url = m_network->url().resolved(redirect.toUrl());
1342 requestFromUrl(url);
1348 m_network->attribute(QNetworkRequest::HttpStatusCodeAttribute).toInt();
1350 QString::fromUtf8(m_network->attribute(QNetworkRequest::HttpReasonPhraseAttribute).toByteArray());
1352 if (m_state < HeadersReceived) {
1353 m_state = HeadersReceived;
1356 dispatchCallback(m_me);
1357 if (tc.HasCaught()) printError(tc.Message());
1359 m_responseEntityBody.append(m_network->readAll());
1363 qWarning().nospace() << "XMLHttpRequest: RESPONSE " << qPrintable(m_url.toString());
1364 if (!m_responseEntityBody.isEmpty()) {
1365 qWarning().nospace() << " "
1366 << qPrintable(QString::fromUtf8(m_responseEntityBody));
1372 if (m_state < Loading) {
1375 dispatchCallback(m_me);
1376 if (tc.HasCaught()) printError(tc.Message());
1381 dispatchCallback(m_me);
1382 if (tc.HasCaught()) printError(tc.Message());
1384 setMe(v8::Handle<v8::Object>());
1388 void QQmlXMLHttpRequest::readEncoding()
1390 foreach (const HeaderPair &header, m_headersList) {
1391 if (header.first == "content-type") {
1392 int separatorIdx = header.second.indexOf(';');
1393 if (separatorIdx == -1) {
1394 m_mime = header.second;
1396 m_mime = header.second.mid(0, separatorIdx);
1397 int charsetIdx = header.second.indexOf("charset=");
1398 if (charsetIdx != -1) {
1400 separatorIdx = header.second.indexOf(';', charsetIdx);
1401 m_charset = header.second.mid(charsetIdx, separatorIdx >= 0 ? separatorIdx : header.second.length());
1408 if (m_mime.isEmpty() || m_mime == "text/xml" || m_mime == "application/xml" || m_mime.endsWith("+xml"))
1412 bool QQmlXMLHttpRequest::receivedXml() const
1418 #ifndef QT_NO_TEXTCODEC
1419 QTextCodec* QQmlXMLHttpRequest::findTextCodec() const
1421 QTextCodec *codec = 0;
1423 if (!m_charset.isEmpty())
1424 codec = QTextCodec::codecForName(m_charset);
1426 if (!codec && m_gotXml) {
1427 QXmlStreamReader reader(m_responseEntityBody);
1429 codec = QTextCodec::codecForName(reader.documentEncoding().toString().toUtf8());
1432 if (!codec && m_mime == "text/html")
1433 codec = QTextCodec::codecForHtml(m_responseEntityBody, 0);
1436 codec = QTextCodec::codecForUtfText(m_responseEntityBody, 0);
1439 codec = QTextCodec::codecForName("UTF-8");
1445 QString QQmlXMLHttpRequest::responseBody()
1447 #ifndef QT_NO_TEXTCODEC
1449 m_textCodec = findTextCodec();
1451 return m_textCodec->toUnicode(m_responseEntityBody);
1454 return QString::fromUtf8(m_responseEntityBody);
1457 const QByteArray &QQmlXMLHttpRequest::rawResponseBody() const
1459 return m_responseEntityBody;
1462 // Requires a TryCatch scope
1463 void QQmlXMLHttpRequest::dispatchCallback(v8::Handle<v8::Object> me)
1466 v8::Context::Scope scope(engine->context());
1468 if (me.IsEmpty() || me->IsNull()) {
1469 v8::ThrowException(v8::Exception::Error(v8::String::New("Unable to dispatch QQmlXmlHttpRequest callback: invalid object")));
1473 if (me->Get(v8::String::New("ThisObject")).IsEmpty()) {
1474 v8::ThrowException(v8::Exception::Error(v8::String::New("QQmlXMLHttpRequest: internal error: empty ThisObject")));
1478 v8::Local<v8::Object> thisObj = me->Get(v8::String::New("ThisObject"))->ToObject();
1479 v8::Local<v8::Value> callback = thisObj->Get(v8::String::New("onreadystatechange"));
1480 if (!callback->IsFunction()) {
1481 // not an error, but no onreadystatechange function to call.
1485 if (me->Get(v8::String::New("ActivationObject")).IsEmpty()) {
1486 v8::ThrowException(v8::Exception::Error(v8::String::New("QQmlXMLHttpRequest: internal error: empty ActivationObject")));
1490 v8::Local<v8::Object> activationObject = me->Get(v8::String::New("ActivationObject"))->ToObject();
1491 QQmlContextData *callingContext = engine->contextWrapper()->context(activationObject);
1492 if (callingContext) {
1493 v8::Local<v8::Function> f = v8::Local<v8::Function>::Cast(callback);
1494 f->Call(activationObject, 0, 0); // valid activation object.
1497 // if the callingContext object is no longer valid, then it has been
1498 // deleted explicitly (e.g., by a Loader deleting the itemContext when
1499 // the source is changed). We do nothing in this case, as the evaluation
1503 // Must have a handle scope
1504 void QQmlXMLHttpRequest::printError(v8::Handle<v8::Message> message)
1506 v8::Context::Scope scope(engine->context());
1509 QQmlExpressionPrivate::exceptionToError(message, error);
1510 QQmlEnginePrivate::warning(QQmlEnginePrivate::get(engine->engine()), error);
1513 void QQmlXMLHttpRequest::destroyNetwork()
1516 m_network->disconnect();
1517 m_network->deleteLater();
1522 // XMLHttpRequest methods
1523 static v8::Handle<v8::Value> qmlxmlhttprequest_open(const v8::Arguments &args)
1525 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(args.This());
1527 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1529 if (args.Length() < 2 || args.Length() > 5)
1530 V8THROW_DOM(DOMEXCEPTION_SYNTAX_ERR, "Incorrect argument count");
1532 QV8Engine *engine = r->engine;
1534 // Argument 0 - Method
1535 QString method = engine->toString(args[0]).toUpper();
1536 if (method != QLatin1String("GET") &&
1537 method != QLatin1String("PUT") &&
1538 method != QLatin1String("HEAD") &&
1539 method != QLatin1String("POST") &&
1540 method != QLatin1String("DELETE"))
1541 V8THROW_DOM(DOMEXCEPTION_SYNTAX_ERR, "Unsupported HTTP method type");
1544 QUrl url = QUrl(engine->toString(args[1]));
1546 if (url.isRelative())
1547 url = engine->callingContext()->resolvedUrl(url);
1549 // Argument 2 - async (optional)
1550 if (args.Length() > 2 && !args[2]->BooleanValue())
1551 V8THROW_DOM(DOMEXCEPTION_NOT_SUPPORTED_ERR, "Synchronous XMLHttpRequest calls are not supported");
1553 // Argument 3/4 - user/pass (optional)
1554 QString username, password;
1555 if (args.Length() > 3)
1556 username = engine->toString(args[3]);
1557 if (args.Length() > 4)
1558 password = engine->toString(args[4]);
1560 // Clear the fragment (if any)
1561 url.setFragment(QString());
1563 // Set username/password
1564 if (!username.isNull()) url.setUserName(username);
1565 if (!password.isNull()) url.setPassword(password);
1567 return r->open(constructMeObject(args.This(), engine), method, url);
1570 static v8::Handle<v8::Value> qmlxmlhttprequest_setRequestHeader(const v8::Arguments &args)
1572 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(args.This());
1574 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1576 if (args.Length() != 2)
1577 V8THROW_DOM(DOMEXCEPTION_SYNTAX_ERR, "Incorrect argument count");
1579 if (r->readyState() != QQmlXMLHttpRequest::Opened || r->sendFlag())
1580 V8THROW_DOM(DOMEXCEPTION_INVALID_STATE_ERR, "Invalid state");
1582 QV8Engine *engine = r->engine;
1584 QString name = engine->toString(args[0]);
1585 QString value = engine->toString(args[1]);
1587 // ### Check that name and value are well formed
1589 QString nameUpper = name.toUpper();
1590 if (nameUpper == QLatin1String("ACCEPT-CHARSET") ||
1591 nameUpper == QLatin1String("ACCEPT-ENCODING") ||
1592 nameUpper == QLatin1String("CONNECTION") ||
1593 nameUpper == QLatin1String("CONTENT-LENGTH") ||
1594 nameUpper == QLatin1String("COOKIE") ||
1595 nameUpper == QLatin1String("COOKIE2") ||
1596 nameUpper == QLatin1String("CONTENT-TRANSFER-ENCODING") ||
1597 nameUpper == QLatin1String("DATE") ||
1598 nameUpper == QLatin1String("EXPECT") ||
1599 nameUpper == QLatin1String("HOST") ||
1600 nameUpper == QLatin1String("KEEP-ALIVE") ||
1601 nameUpper == QLatin1String("REFERER") ||
1602 nameUpper == QLatin1String("TE") ||
1603 nameUpper == QLatin1String("TRAILER") ||
1604 nameUpper == QLatin1String("TRANSFER-ENCODING") ||
1605 nameUpper == QLatin1String("UPGRADE") ||
1606 nameUpper == QLatin1String("USER-AGENT") ||
1607 nameUpper == QLatin1String("VIA") ||
1608 nameUpper.startsWith(QLatin1String("PROXY-")) ||
1609 nameUpper.startsWith(QLatin1String("SEC-")))
1610 return v8::Undefined();
1612 r->addHeader(name, value);
1614 return v8::Undefined();
1617 static v8::Handle<v8::Value> qmlxmlhttprequest_send(const v8::Arguments &args)
1619 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(args.This());
1621 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1623 QV8Engine *engine = r->engine;
1625 if (r->readyState() != QQmlXMLHttpRequest::Opened ||
1627 V8THROW_DOM(DOMEXCEPTION_INVALID_STATE_ERR, "Invalid state");
1630 if (args.Length() > 0)
1631 data = engine->toString(args[0]).toUtf8();
1633 return r->send(constructMeObject(args.This(), engine), data);
1636 static v8::Handle<v8::Value> qmlxmlhttprequest_abort(const v8::Arguments &args)
1638 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(args.This());
1640 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1642 return r->abort(constructMeObject(args.This(), r->engine));
1645 static v8::Handle<v8::Value> qmlxmlhttprequest_getResponseHeader(const v8::Arguments &args)
1647 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(args.This());
1649 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1651 QV8Engine *engine = r->engine;
1653 if (args.Length() != 1)
1654 V8THROW_DOM(DOMEXCEPTION_SYNTAX_ERR, "Incorrect argument count");
1656 if (r->readyState() != QQmlXMLHttpRequest::Loading &&
1657 r->readyState() != QQmlXMLHttpRequest::Done &&
1658 r->readyState() != QQmlXMLHttpRequest::HeadersReceived)
1659 V8THROW_DOM(DOMEXCEPTION_INVALID_STATE_ERR, "Invalid state");
1661 return engine->toString(r->header(engine->toString(args[0])));
1664 static v8::Handle<v8::Value> qmlxmlhttprequest_getAllResponseHeaders(const v8::Arguments &args)
1666 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(args.This());
1668 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1670 QV8Engine *engine = r->engine;
1672 if (args.Length() != 0)
1673 V8THROW_DOM(DOMEXCEPTION_SYNTAX_ERR, "Incorrect argument count");
1675 if (r->readyState() != QQmlXMLHttpRequest::Loading &&
1676 r->readyState() != QQmlXMLHttpRequest::Done &&
1677 r->readyState() != QQmlXMLHttpRequest::HeadersReceived)
1678 V8THROW_DOM(DOMEXCEPTION_INVALID_STATE_ERR, "Invalid state");
1680 return engine->toString(r->headers());
1683 // XMLHttpRequest properties
1684 static v8::Handle<v8::Value> qmlxmlhttprequest_readyState(v8::Local<v8::String> /* property */,
1685 const v8::AccessorInfo& info)
1687 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(info.This());
1689 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1691 return v8::Integer::NewFromUnsigned(r->readyState());
1694 static v8::Handle<v8::Value> qmlxmlhttprequest_status(v8::Local<v8::String> /* property */,
1695 const v8::AccessorInfo& info)
1697 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(info.This());
1699 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1701 if (r->readyState() == QQmlXMLHttpRequest::Unsent ||
1702 r->readyState() == QQmlXMLHttpRequest::Opened)
1703 V8THROW_DOM(DOMEXCEPTION_INVALID_STATE_ERR, "Invalid state");
1706 return v8::Integer::New(0);
1708 return v8::Integer::New(r->replyStatus());
1711 static v8::Handle<v8::Value> qmlxmlhttprequest_statusText(v8::Local<v8::String> /* property */,
1712 const v8::AccessorInfo& info)
1714 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(info.This());
1716 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1718 QV8Engine *engine = r->engine;
1720 if (r->readyState() == QQmlXMLHttpRequest::Unsent ||
1721 r->readyState() == QQmlXMLHttpRequest::Opened)
1722 V8THROW_DOM(DOMEXCEPTION_INVALID_STATE_ERR, "Invalid state");
1725 return engine->toString(QString());
1727 return engine->toString(r->replyStatusText());
1730 static v8::Handle<v8::Value> qmlxmlhttprequest_responseText(v8::Local<v8::String> /* property */,
1731 const v8::AccessorInfo& info)
1733 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(info.This());
1735 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1737 QV8Engine *engine = r->engine;
1739 if (r->readyState() != QQmlXMLHttpRequest::Loading &&
1740 r->readyState() != QQmlXMLHttpRequest::Done)
1741 return engine->toString(QString());
1743 return engine->toString(r->responseBody());
1746 static v8::Handle<v8::Value> qmlxmlhttprequest_responseXML(v8::Local<v8::String> /* property */,
1747 const v8::AccessorInfo& info)
1749 QQmlXMLHttpRequest *r = v8_resource_cast<QQmlXMLHttpRequest>(info.This());
1751 V8THROW_REFERENCE("Not an XMLHttpRequest object");
1753 if (!r->receivedXml() ||
1754 (r->readyState() != QQmlXMLHttpRequest::Loading &&
1755 r->readyState() != QQmlXMLHttpRequest::Done)) {
1758 return Document::load(r->engine, r->rawResponseBody());
1762 static v8::Handle<v8::Value> qmlxmlhttprequest_new(const v8::Arguments &args)
1764 if (args.IsConstructCall()) {
1765 QV8Engine *engine = V8ENGINE();
1766 QQmlEnginePrivate *ep = QQmlEnginePrivate::get(engine->engine());
1768 QQmlXMLHttpRequest *r = new QQmlXMLHttpRequest(engine, engine->networkAccessManager());
1769 args.This()->SetExternalResource(r);
1773 return v8::Undefined();
1777 #define NEWFUNCTION(function) v8::FunctionTemplate::New(function)->GetFunction()
1779 void qt_rem_qmlxmlhttprequest(QV8Engine * /* engine */, void *d)
1781 QQmlXMLHttpRequestData *data = (QQmlXMLHttpRequestData *)d;
1785 void *qt_add_qmlxmlhttprequest(QV8Engine *engine)
1787 v8::PropertyAttribute attributes = (v8::PropertyAttribute)(v8::ReadOnly | v8::DontEnum | v8::DontDelete);
1790 v8::Local<v8::FunctionTemplate> xmlhttprequest = v8::FunctionTemplate::New(qmlxmlhttprequest_new,
1791 v8::External::Wrap(engine));
1792 xmlhttprequest->InstanceTemplate()->SetHasExternalResource(true);
1795 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("open"), NEWFUNCTION(qmlxmlhttprequest_open), attributes);
1796 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("setRequestHeader"), NEWFUNCTION(qmlxmlhttprequest_setRequestHeader), attributes);
1797 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("send"), NEWFUNCTION(qmlxmlhttprequest_send), attributes);
1798 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("abort"), NEWFUNCTION(qmlxmlhttprequest_abort), attributes);
1799 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("getResponseHeader"), NEWFUNCTION(qmlxmlhttprequest_getResponseHeader), attributes);
1800 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("getAllResponseHeaders"), NEWFUNCTION(qmlxmlhttprequest_getAllResponseHeaders), attributes);
1802 // Read-only properties
1803 xmlhttprequest->PrototypeTemplate()->SetAccessor(v8::String::New("readyState"), qmlxmlhttprequest_readyState, 0, v8::Handle<v8::Value>(), v8::DEFAULT, attributes);
1804 xmlhttprequest->PrototypeTemplate()->SetAccessor(v8::String::New("status"),qmlxmlhttprequest_status, 0, v8::Handle<v8::Value>(), v8::DEFAULT, attributes);
1805 xmlhttprequest->PrototypeTemplate()->SetAccessor(v8::String::New("statusText"),qmlxmlhttprequest_statusText, 0, v8::Handle<v8::Value>(), v8::DEFAULT, attributes);
1806 xmlhttprequest->PrototypeTemplate()->SetAccessor(v8::String::New("responseText"),qmlxmlhttprequest_responseText, 0, v8::Handle<v8::Value>(), v8::DEFAULT, attributes);
1807 xmlhttprequest->PrototypeTemplate()->SetAccessor(v8::String::New("responseXML"),qmlxmlhttprequest_responseXML, 0, v8::Handle<v8::Value>(), v8::DEFAULT, attributes);
1810 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("UNSENT"), v8::Integer::New(0), attributes);
1811 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("OPENED"), v8::Integer::New(1), attributes);
1812 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("HEADERS_RECEIVED"), v8::Integer::New(2), attributes);
1813 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("LOADING"), v8::Integer::New(3), attributes);
1814 xmlhttprequest->PrototypeTemplate()->Set(v8::String::New("DONE"), v8::Integer::New(4), attributes);
1817 xmlhttprequest->Set(v8::String::New("UNSENT"), v8::Integer::New(0), attributes);
1818 xmlhttprequest->Set(v8::String::New("OPENED"), v8::Integer::New(1), attributes);
1819 xmlhttprequest->Set(v8::String::New("HEADERS_RECEIVED"), v8::Integer::New(2), attributes);
1820 xmlhttprequest->Set(v8::String::New("LOADING"), v8::Integer::New(3), attributes);
1821 xmlhttprequest->Set(v8::String::New("DONE"), v8::Integer::New(4), attributes);
1822 engine->global()->Set(v8::String::New("XMLHttpRequest"), xmlhttprequest->GetFunction());
1824 QQmlXMLHttpRequestData *data = new QQmlXMLHttpRequestData;
1830 #endif // QT_NO_XMLSTREAMREADER
1832 #include <qqmlxmlhttprequest.moc>