Qt 4.8
qdeclarativexmlhttprequest.cpp
Go to the documentation of this file.
1 /****************************************************************************
2 **
3 ** Copyright (C) 2014 Digia Plc and/or its subsidiary(-ies).
4 ** Contact: http://www.qt-project.org/legal
5 **
6 ** This file is part of the QtDeclarative module of the Qt Toolkit.
7 **
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.
16 **
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.
24 **
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.
28 **
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.
36 **
37 **
38 ** $QT_END_LICENSE$
39 **
40 ****************************************************************************/
41 
42 #include "private/qdeclarativexmlhttprequest_p.h"
43 
44 #include "qdeclarativeengine.h"
45 #include "private/qdeclarativeengine_p.h"
46 #include "private/qdeclarativerefcount_p.h"
47 #include "private/qdeclarativeengine_p.h"
48 #include "private/qdeclarativeexpression_p.h"
49 #include "qdeclarativeglobal_p.h"
50 
51 #include <QtCore/qobject.h>
52 #include <QtScript/qscriptvalue.h>
53 #include <QtScript/qscriptcontext.h>
54 #include <QtScript/qscriptengine.h>
55 #include <QtNetwork/qnetworkreply.h>
56 #include <QtCore/qtextcodec.h>
57 #include <QtCore/qxmlstream.h>
58 #include <QtCore/qstack.h>
59 #include <QtCore/qdebug.h>
60 
61 #include <QtCore/QStringBuilder>
62 
63 #ifndef QT_NO_XMLSTREAMREADER
64 
65 // From DOM-Level-3-Core spec
66 // http://www.w3.org/TR/DOM-Level-3-Core/core.html
67 #define INDEX_SIZE_ERR 1
68 #define DOMSTRING_SIZE_ERR 2
69 #define HIERARCHY_REQUEST_ERR 3
70 #define WRONG_DOCUMENT_ERR 4
71 #define INVALID_CHARACTER_ERR 5
72 #define NO_DATA_ALLOWED_ERR 6
73 #define NO_MODIFICATION_ALLOWED_ERR 7
74 #define NOT_FOUND_ERR 8
75 #define NOT_SUPPORTED_ERR 9
76 #define INUSE_ATTRIBUTE_ERR 10
77 #define INVALID_STATE_ERR 11
78 #define SYNTAX_ERR 12
79 #define INVALID_MODIFICATION_ERR 13
80 #define NAMESPACE_ERR 14
81 #define INVALID_ACCESS_ERR 15
82 #define VALIDATION_ERR 16
83 #define TYPE_MISMATCH_ERR 17
84 
85 #define THROW_DOM(error, desc) \
86 { \
87  QScriptValue errorValue = context->throwError(QLatin1String(desc)); \
88  errorValue.setProperty(QLatin1String("code"), error); \
89  return errorValue; \
90 }
91 
92 #define THROW_SYNTAX(desc) \
93  return context->throwError(QScriptContext::SyntaxError, QLatin1String(desc));
94 #define THROW_REFERENCE(desc) \
95  return context->throwError(QScriptContext::ReferenceError, QLatin1String(desc));
96 
97 #define D(arg) (arg)->release()
98 #define A(arg) (arg)->addref()
99 
101 
102 DEFINE_BOOL_CONFIG_OPTION(xhrDump, QML_XHR_DUMP);
103 
104 class DocumentImpl;
105 class NodeImpl
106 {
107 public:
109  virtual ~NodeImpl() {
110  for (int ii = 0; ii < children.count(); ++ii)
111  delete children.at(ii);
112  for (int ii = 0; ii < attributes.count(); ++ii)
113  delete attributes.at(ii);
114  }
115 
116  // These numbers are copied from the Node IDL definition
117  enum Type {
118  Attr = 2,
119  CDATA = 4,
120  Comment = 8,
121  Document = 9,
124  Element = 1,
125  Entity = 6,
127  Notation = 12,
129  Text = 3
130  };
132 
135 
137 
138  void addref();
139  void release();
140 
143 
146 };
147 
149 {
150 public:
151  DocumentImpl() : root(0) { type = Document; }
152  virtual ~DocumentImpl() {
153  if (root) delete root;
154  }
155 
159 
161 
164 };
165 
167 {
168 public:
169  // JS API
170  static QScriptValue length(QScriptContext *context, QScriptEngine *engine);
171 
172  // C++ API
175 
176  NamedNodeMap();
177  NamedNodeMap(const NamedNodeMap &);
178  ~NamedNodeMap();
179  bool isNull();
180 
183 private:
184  NamedNodeMap &operator=(const NamedNodeMap &);
185 };
186 
188 {
189 public:
191 
192  virtual QueryFlags queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id);
193  virtual QScriptValue property(const QScriptValue &object, const QScriptString &name, uint id);
194 };
195 
196 class NodeList
197 {
198 public:
199  // JS API
200  static QScriptValue length(QScriptContext *context, QScriptEngine *engine);
201 
202  // C++ API
205 
206  NodeList();
207  NodeList(const NodeList &);
208  ~NodeList();
209  bool isNull();
210 
212 private:
213  NodeList &operator=(const NodeList &);
214 };
215 
217 {
218 public:
220  virtual QueryFlags queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id);
221  virtual QScriptValue property(const QScriptValue &object, const QScriptString &name, uint id);
222 };
223 
224 class Node
225 {
226 public:
227  // JS API
228  static QScriptValue nodeName(QScriptContext *context, QScriptEngine *engine);
229  static QScriptValue nodeValue(QScriptContext *context, QScriptEngine *engine);
230  static QScriptValue nodeType(QScriptContext *context, QScriptEngine *engine);
231 
232  static QScriptValue parentNode(QScriptContext *context, QScriptEngine *engine);
233  static QScriptValue childNodes(QScriptContext *context, QScriptEngine *engine);
234  static QScriptValue firstChild(QScriptContext *context, QScriptEngine *engine);
235  static QScriptValue lastChild(QScriptContext *context, QScriptEngine *engine);
236  static QScriptValue previousSibling(QScriptContext *context, QScriptEngine *engine);
237  static QScriptValue nextSibling(QScriptContext *context, QScriptEngine *engine);
238  static QScriptValue attributes(QScriptContext *context, QScriptEngine *engine);
239 
240  //static QScriptValue ownerDocument(QScriptContext *context, QScriptEngine *engine);
241  //static QScriptValue namespaceURI(QScriptContext *context, QScriptEngine *engine);
242  //static QScriptValue prefix(QScriptContext *context, QScriptEngine *engine);
243  //static QScriptValue localName(QScriptContext *context, QScriptEngine *engine);
244  //static QScriptValue baseURI(QScriptContext *context, QScriptEngine *engine);
245  //static QScriptValue textContent(QScriptContext *context, QScriptEngine *engine);
246 
247  // C++ API
250 
251  Node();
252  Node(const Node &o);
253  ~Node();
254  bool isNull() const;
255 
257 
258 private:
259  Node &operator=(const Node &);
260 };
261 
262 class Element : public Node
263 {
264 public:
265  // C++ API
267 };
268 
269 class Attr : public Node
270 {
271 public:
272  // JS API
273  static QScriptValue name(QScriptContext *context, QScriptEngine *engine);
274  static QScriptValue specified(QScriptContext *context, QScriptEngine *engine);
275  static QScriptValue value(QScriptContext *context, QScriptEngine *engine);
276  static QScriptValue ownerElement(QScriptContext *context, QScriptEngine *engine);
277  static QScriptValue schemaTypeInfo(QScriptContext *context, QScriptEngine *engine);
278  static QScriptValue isId(QScriptContext *context, QScriptEngine *engine);
279 
280  // C++ API
282 };
283 
284 class CharacterData : public Node
285 {
286 public:
287  // JS API
288  static QScriptValue length(QScriptContext *context, QScriptEngine *engine);
289 
290  // C++ API
292 };
293 
294 class Text : public CharacterData
295 {
296 public:
297  // JS API
298  static QScriptValue isElementContentWhitespace(QScriptContext *context, QScriptEngine *engine);
299  static QScriptValue wholeText(QScriptContext *context, QScriptEngine *engine);
300 
301  // C++ API
303 };
304 
305 class CDATA : public Text
306 {
307 public:
308  // C++ API
310 };
311 
312 class Document : public Node
313 {
314 public:
315  // JS API
316  static QScriptValue xmlVersion(QScriptContext *context, QScriptEngine *engine);
317  static QScriptValue xmlEncoding(QScriptContext *context, QScriptEngine *engine);
318  static QScriptValue xmlStandalone(QScriptContext *context, QScriptEngine *engine);
319  static QScriptValue documentElement(QScriptContext *context, QScriptEngine *engine);
320 
321  // C++ API
323  static QScriptValue load(QScriptEngine *engine, const QByteArray &data);
324 };
325 
327 
331 
333 
335 {
336  A(document);
337 }
338 
340 {
341  D(document);
342 }
343 
345 {
346  Node node = qscriptvalue_cast<Node>(context->thisObject());
347  if (node.isNull()) return engine->undefinedValue();
348 
349  switch (node.d->type) {
350  case NodeImpl::Document:
351  return QScriptValue(QLatin1String("#document"));
352  case NodeImpl::CDATA:
353  return QScriptValue(QLatin1String("#cdata-section"));
354  case NodeImpl::Text:
355  return QScriptValue(QLatin1String("#text"));
356  default:
357  return QScriptValue(node.d->name);
358  }
359 }
360 
362 {
363  Node node = qscriptvalue_cast<Node>(context->thisObject());
364  if (node.isNull()) return engine->undefinedValue();
365 
366  if (node.d->type == NodeImpl::Document ||
367  node.d->type == NodeImpl::DocumentFragment ||
368  node.d->type == NodeImpl::DocumentType ||
369  node.d->type == NodeImpl::Element ||
370  node.d->type == NodeImpl::Entity ||
371  node.d->type == NodeImpl::EntityReference ||
372  node.d->type == NodeImpl::Notation)
373  return engine->nullValue();
374 
375  return QScriptValue(node.d->data);
376 }
377 
379 {
380  Node node = qscriptvalue_cast<Node>(context->thisObject());
381  if (node.isNull()) return engine->undefinedValue();
382  return QScriptValue(node.d->type);
383 }
384 
386 {
387  Node node = qscriptvalue_cast<Node>(context->thisObject());
388  if (node.isNull()) return engine->undefinedValue();
389 
390  if (node.d->parent) return Node::create(engine, node.d->parent);
391  else return engine->nullValue();
392 }
393 
395 {
396  Node node = qscriptvalue_cast<Node>(context->thisObject());
397  if (node.isNull()) return engine->undefinedValue();
398 
399  return NodeList::create(engine, node.d);
400 }
401 
403 {
404  Node node = qscriptvalue_cast<Node>(context->thisObject());
405  if (node.isNull()) return engine->undefinedValue();
406 
407  if (node.d->children.isEmpty()) return engine->nullValue();
408  else return Node::create(engine, node.d->children.first());
409 }
410 
412 {
413  Node node = qscriptvalue_cast<Node>(context->thisObject());
414  if (node.isNull()) return engine->undefinedValue();
415 
416  if (node.d->children.isEmpty()) return engine->nullValue();
417  else return Node::create(engine, node.d->children.last());
418 }
419 
421 {
422  Node node = qscriptvalue_cast<Node>(context->thisObject());
423  if (node.isNull()) return engine->undefinedValue();
424 
425  if (!node.d->parent) return engine->nullValue();
426 
427  for (int ii = 0; ii < node.d->parent->children.count(); ++ii) {
428  if (node.d->parent->children.at(ii) == node.d) {
429  if (ii == 0) return engine->nullValue();
430  else return Node::create(engine, node.d->parent->children.at(ii - 1));
431  }
432  }
433 
434  return engine->nullValue();
435 }
436 
438 {
439  Node node = qscriptvalue_cast<Node>(context->thisObject());
440  if (node.isNull()) return engine->undefinedValue();
441 
442  if (!node.d->parent) return engine->nullValue();
443 
444  for (int ii = 0; ii < node.d->parent->children.count(); ++ii) {
445  if (node.d->parent->children.at(ii) == node.d) {
446  if ((ii + 1) == node.d->parent->children.count()) return engine->nullValue();
447  else return Node::create(engine, node.d->parent->children.at(ii + 1));
448  }
449  }
450 
451  return engine->nullValue();
452 }
453 
455 {
456  Node node = qscriptvalue_cast<Node>(context->thisObject());
457  if (node.isNull()) return engine->undefinedValue();
458 
459  if (node.d->type != NodeImpl::Element)
460  return engine->nullValue();
461  else
462  return NamedNodeMap::create(engine, node.d, &node.d->attributes);
463 }
464 
466 {
467  QScriptValue proto = engine->newObject();
468 
472  proto.setProperty(QLatin1String("parentNode"), engine->newFunction(parentNode), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
473  proto.setProperty(QLatin1String("childNodes"), engine->newFunction(childNodes), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
474  proto.setProperty(QLatin1String("firstChild"), engine->newFunction(firstChild), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
476  proto.setProperty(QLatin1String("previousSibling"), engine->newFunction(previousSibling), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
477  proto.setProperty(QLatin1String("nextSibling"), engine->newFunction(nextSibling), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
479 
480  return proto;
481 }
482 
484 {
485  QScriptValue instance = engine->newObject();
486 
487  switch (data->type) {
488  case NodeImpl::Attr:
489  instance.setPrototype(Attr::prototype(engine));
490  break;
491  case NodeImpl::Comment:
492  case NodeImpl::Document:
495  case NodeImpl::Entity:
497  case NodeImpl::Notation:
499  return QScriptValue();
500  case NodeImpl::CDATA:
501  instance.setPrototype(CDATA::prototype(engine));
502  break;
503  case NodeImpl::Text:
504  instance.setPrototype(Text::prototype(engine));
505  break;
506  case NodeImpl::Element:
507  instance.setPrototype(Element::prototype(engine));
508  break;
509  }
510 
511  Node node;
512  node.d = data;
513  if (data) A(data);
514 
515  return engine->newVariant(instance, QVariant::fromValue(node));
516 }
517 
519 {
520  QScriptValue proto = engine->newObject();
521  proto.setPrototype(Node::prototype(engine));
522 
524 
525  return proto;
526 }
527 
529 {
530  QScriptValue proto = engine->newObject();
531  proto.setPrototype(Node::prototype(engine));
532 
535  proto.setProperty(QLatin1String("ownerElement"), engine->newFunction(ownerElement), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
536 
537  return proto;
538 }
539 
541 {
542  Node node = qscriptvalue_cast<Node>(context->thisObject());
543  if (node.isNull()) return engine->undefinedValue();
544 
545  return QScriptValue(node.d->name);
546 }
547 
549 {
550  Node node = qscriptvalue_cast<Node>(context->thisObject());
551  if (node.isNull()) return engine->undefinedValue();
552 
553  return QScriptValue(node.d->data);
554 }
555 
557 {
558  Node node = qscriptvalue_cast<Node>(context->thisObject());
559  if (node.isNull()) return engine->undefinedValue();
560 
561  return Node::create(engine, node.d->parent);
562 }
563 
565 {
566  Node node = qscriptvalue_cast<Node>(context->thisObject());
567  if (node.isNull()) return engine->undefinedValue();
568 
569  return QScriptValue(node.d->data.length());
570 }
571 
573 {
574  QScriptValue proto = engine->newObject();
575  proto.setPrototype(Node::prototype(engine));
576 
579 
580  return proto;
581 }
582 
584 {
585  Node node = qscriptvalue_cast<Node>(context->thisObject());
586  if (node.isNull()) return engine->undefinedValue();
587 
588  return node.d->data.trimmed().isEmpty();
589 }
590 
592 {
593  Node node = qscriptvalue_cast<Node>(context->thisObject());
594  if (node.isNull()) return engine->undefinedValue();
595 
596  return node.d->data;
597 }
598 
600 {
601  QScriptValue proto = engine->newObject();
602  proto.setPrototype(CharacterData::prototype(engine));
603 
604  proto.setProperty(QLatin1String("isElementContentWhitespace"), engine->newFunction(isElementContentWhitespace), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
606 
607  return proto;
608 }
609 
611 {
612  QScriptValue proto = engine->newObject();
613  proto.setPrototype(Text::prototype(engine));
614  return proto;
615 }
616 
618 {
619  QScriptValue proto = engine->newObject();
620  proto.setPrototype(Node::prototype(engine));
621 
625  proto.setProperty(QLatin1String("documentElement"), engine->newFunction(documentElement), QScriptValue::ReadOnly | QScriptValue::PropertyGetter);
626 
627  return proto;
628 }
629 
631 {
632  Q_ASSERT(engine);
633 
634  DocumentImpl *document = 0;
635  QStack<NodeImpl *> nodeStack;
636 
637  QXmlStreamReader reader(data);
638 
639  while (!reader.atEnd()) {
640  switch (reader.readNext()) {
642  break;
644  break;
646  Q_ASSERT(!document);
647  document = new DocumentImpl;
648  document->document = document;
649  document->version = reader.documentVersion().toString();
650  document->encoding = reader.documentEncoding().toString();
651  document->isStandalone = reader.isStandaloneDocument();
652  break;
654  break;
656  {
657  Q_ASSERT(document);
658  NodeImpl *node = new NodeImpl;
659  node->document = document;
660  node->namespaceUri = reader.namespaceUri().toString();
661  node->name = reader.name().toString();
662  if (nodeStack.isEmpty()) {
663  document->root = node;
664  } else {
665  node->parent = nodeStack.top();
666  node->parent->children.append(node);
667  }
668  nodeStack.append(node);
669 
670  foreach (const QXmlStreamAttribute &a, reader.attributes()) {
671  NodeImpl *attr = new NodeImpl;
672  attr->document = document;
673  attr->type = NodeImpl::Attr;
674  attr->namespaceUri = a.namespaceUri().toString();
675  attr->name = a.name().toString();
676  attr->data = a.value().toString();
677  attr->parent = node;
678  node->attributes.append(attr);
679  }
680  }
681  break;
683  nodeStack.pop();
684  break;
686  {
687  NodeImpl *node = new NodeImpl;
688  node->document = document;
689  node->type = reader.isCDATA()?NodeImpl::CDATA:NodeImpl::Text;
690  node->parent = nodeStack.top();
691  node->parent->children.append(node);
692  node->data = reader.text().toString();
693  }
694  break;
696  break;
698  break;
700  break;
702  break;
703  }
704  }
705 
706  if (!document || reader.hasError()) {
707  if (document) D(document);
708  return engine->nullValue();
709  }
710 
711  QScriptValue instance = engine->newObject();
712  instance.setPrototype(Document::prototype(engine));
713  Node documentNode;
714  documentNode.d = document;
715  return engine->newVariant(instance, QVariant::fromValue(documentNode));
716 }
717 
719 : d(0)
720 {
721 }
722 
723 Node::Node(const Node &o)
724 : d(o.d)
725 {
726  if (d) A(d);
727 }
728 
730 {
731  if (d) D(d);
732 }
733 
734 bool Node::isNull() const
735 {
736  return d == 0;
737 }
738 
740 {
742  if (map.isNull()) return engine->undefinedValue();
743 
744  return QScriptValue(map.list->count());
745 }
746 
748 {
749  QScriptValue proto = engine->newObject();
750 
752 
753  return proto;
754 }
755 
757 {
758  QScriptValue instance = engine->newObject();
759  instance.setPrototype(NamedNodeMap::prototype(engine));
760 
762  map.d = data;
763  map.list = list;
764  if (data) A(data);
765 
766  instance.setData(engine->newVariant(QVariant::fromValue(map)));
767 
770 
771  instance.setScriptClass(QDeclarativeScriptEngine::get(engine)->namedNodeMapClass);
772 
773  return instance;
774 }
775 
777 : d(0), list(0)
778 {
779 }
780 
782 : d(o.d), list(o.list)
783 {
784  if (d) A(d);
785 }
786 
788 {
789  if (d) D(d);
790 }
791 
793 {
794  return d == 0;
795 }
796 
798 {
800  if (list.isNull()) return engine->undefinedValue();
801 
802  return QScriptValue(list.d->children.count());
803 }
804 
806 {
807  QScriptValue proto = engine->newObject();
808 
810 
811  return proto;
812 }
813 
815 {
816  QScriptValue instance = engine->newObject();
817  instance.setPrototype(NodeList::prototype(engine));
818 
819  NodeList list;
820  list.d = data;
821  if (data) A(data);
822 
823  instance.setData(engine->newVariant(QVariant::fromValue(list)));
824 
827 
828  instance.setScriptClass(QDeclarativeScriptEngine::get(engine)->nodeListClass);
829 
830  return instance;
831 }
832 
834 : d(0)
835 {
836 }
837 
839 : d(o.d)
840 {
841  if (d) A(d);
842 }
843 
845 {
846  if (d) D(d);
847 }
848 
850 {
851  return d == 0;
852 }
853 
854 NamedNodeMapClass::QueryFlags NamedNodeMapClass::queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id)
855 {
856  if (!(flags & HandlesReadAccess))
857  return 0;
858 
860  Q_ASSERT(!map.isNull());
861 
862  bool ok = false;
863  QString nameString = name.toString();
864  uint index = nameString.toUInt(&ok);
865  if (ok) {
866  if ((uint)map.list->count() <= index)
867  return 0;
868 
869  *id = index;
870  return HandlesReadAccess;
871  } else {
872  for (int ii = 0; ii < map.list->count(); ++ii) {
873  if (map.list->at(ii) && map.list->at(ii)->name == nameString) {
874  *id = ii;
875  return HandlesReadAccess;
876  }
877  }
878  }
879 
880  return 0;
881 }
882 
884 {
886  return Node::create(engine(), map.list->at(id));
887 }
888 
889 NodeListClass::QueryFlags NodeListClass::queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id)
890 {
891  if (!(flags & HandlesReadAccess))
892  return 0;
893 
894  bool ok = false;
895  uint index = name.toString().toUInt(&ok);
896  if (!ok)
897  return 0;
898 
899  NodeList list = qscriptvalue_cast<NodeList>(object.data());
900  if (list.isNull() || (uint)list.d->children.count() <= index)
901  return 0; // ### I think we're meant to raise an exception
902 
903  *id = index;
904  return HandlesReadAccess;
905 }
906 
908 {
909  NodeList list = qscriptvalue_cast<NodeList>(object.data());
910  return Node::create(engine(), list.d->children.at(id));
911 }
912 
914 {
915  Node document = qscriptvalue_cast<Node>(context->thisObject());
916  if (document.isNull() || document.d->type != NodeImpl::Document) return engine->undefinedValue();
917 
918  return Node::create(engine, static_cast<DocumentImpl *>(document.d)->root);
919 }
920 
922 {
923  Node document = qscriptvalue_cast<Node>(context->thisObject());
924  if (document.isNull() || document.d->type != NodeImpl::Document) return engine->undefinedValue();
925 
926  return QScriptValue(static_cast<DocumentImpl *>(document.d)->isStandalone);
927 }
928 
930 {
931  Node document = qscriptvalue_cast<Node>(context->thisObject());
932  if (document.isNull() || document.d->type != NodeImpl::Document) return engine->undefinedValue();
933 
934  return QScriptValue(static_cast<DocumentImpl *>(document.d)->version);
935 }
936 
938 {
939  Node document = qscriptvalue_cast<Node>(context->thisObject());
940  if (document.isNull() || document.d->type != NodeImpl::Document) return engine->undefinedValue();
941 
942  return QScriptValue(static_cast<DocumentImpl *>(document.d)->encoding);
943 }
944 
946 {
947 Q_OBJECT
948 public:
949  enum State { Unsent = 0,
950  Opened = 1, HeadersReceived = 2,
951  Loading = 3, Done = 4 };
952 
954  virtual ~QDeclarativeXMLHttpRequest();
955 
956  bool sendFlag() const;
957  bool errorFlag() const;
958  quint32 readyState() const;
959  int replyStatus() const;
960  QString replyStatusText() const;
961 
962  QScriptValue open(QScriptValue *me, const QString &, const QUrl &);
963 
964  void addHeader(const QString &, const QString &);
965  QString header(const QString &name);
966  QString headers();
967  QScriptValue send(QScriptValue *me, const QByteArray &);
968  QScriptValue abort(QScriptValue *me);
969 
970  QString responseBody();
971  const QByteArray & rawResponseBody() const;
972  bool receivedXml() const;
973 private slots:
974  void downloadProgress(qint64);
976  void finished();
977 
978 private:
979  void requestFromUrl(const QUrl &url);
980 
989 
992  HeadersList m_headersList;
993  void fillHeadersList();
994 
995  bool m_gotXml;
999 #ifndef QT_NO_TEXTCODEC
1000  QTextCodec* findTextCodec() const;
1001 #endif
1002  void readEncoding();
1003 
1004  QScriptValue m_me; // Set to the data object while a send() is ongoing (to access the callback)
1005 
1006  QScriptValue dispatchCallback(QScriptValue *me);
1007  void printError(const QScriptValue&);
1008 
1013  void destroyNetwork();
1014 
1017 };
1018 
1020 : m_state(Unsent), m_errorFlag(false), m_sendFlag(false),
1021  m_redirectCount(0), m_gotXml(false), m_textCodec(0), m_network(0), m_nam(manager)
1022 {
1023 }
1024 
1026 {
1027  destroyNetwork();
1028 }
1029 
1031 {
1032  return m_sendFlag;
1033 }
1034 
1036 {
1037  return m_errorFlag;
1038 }
1039 
1041 {
1042  return m_state;
1043 }
1044 
1046 {
1047  return m_status;
1048 }
1049 
1051 {
1052  return m_statusText;
1053 }
1054 
1056 {
1057  destroyNetwork();
1058  m_sendFlag = false;
1059  m_errorFlag = false;
1061  m_method = method;
1062  m_url = url;
1063  m_state = Opened;
1064  return dispatchCallback(me);
1065 }
1066 
1068 {
1069  QByteArray utfname = name.toUtf8();
1070 
1071  if (m_request.hasRawHeader(utfname)) {
1072  m_request.setRawHeader(utfname, m_request.rawHeader(utfname) + ',' + value.toUtf8());
1073  } else {
1074  m_request.setRawHeader(utfname, value.toUtf8());
1075  }
1076 }
1077 
1079 {
1080  QByteArray utfname = name.toLower().toUtf8();
1081 
1082  foreach (const HeaderPair &header, m_headersList) {
1083  if (header.first == utfname)
1084  return QString::fromUtf8(header.second);
1085  }
1086  return QString();
1087 }
1088 
1090 {
1091  QString ret;
1092 
1093  foreach (const HeaderPair &header, m_headersList) {
1094  if (ret.length())
1095  ret.append(QLatin1String("\r\n"));
1096  ret = ret % QString::fromUtf8(header.first) % QLatin1String(": ")
1097  % QString::fromUtf8(header.second);
1098  }
1099  return ret;
1100 }
1101 
1103 {
1104  QList<QByteArray> headerList = m_network->rawHeaderList();
1105 
1106  m_headersList.clear();
1107  foreach (const QByteArray &header, headerList) {
1108  HeaderPair pair (header.toLower(), m_network->rawHeader(header));
1109  if (pair.first == "set-cookie" ||
1110  pair.first == "set-cookie2")
1111  continue;
1112 
1113  m_headersList << pair;
1114  }
1115 }
1116 
1118 {
1119  QNetworkRequest request = m_request;
1120  request.setUrl(url);
1121  if(m_method == QLatin1String("POST") ||
1122  m_method == QLatin1String("PUT")) {
1124  if (var.isValid()) {
1125  QString str = var.toString();
1126  int charsetIdx = str.indexOf(QLatin1String("charset="));
1127  if (charsetIdx == -1) {
1128  // No charset - append
1129  if (!str.isEmpty()) str.append(QLatin1Char(';'));
1130  str.append(QLatin1String("charset=UTF-8"));
1131  } else {
1132  charsetIdx += 8;
1133  int n = 0;
1134  int semiColon = str.indexOf(QLatin1Char(';'), charsetIdx);
1135  if (semiColon == -1) {
1136  n = str.length() - charsetIdx;
1137  } else {
1138  n = semiColon - charsetIdx;
1139  }
1140 
1141  str.replace(charsetIdx, n, QLatin1String("UTF-8"));
1142  }
1144  } else {
1146  QLatin1String("text/plain;charset=UTF-8"));
1147  }
1148  }
1149 
1150  if (xhrDump()) {
1151  qWarning().nospace() << "XMLHttpRequest: " << qPrintable(m_method) << " " << qPrintable(url.toString());
1152  if (!m_data.isEmpty()) {
1153  qWarning().nospace() << " "
1155  }
1156  }
1157 
1158  if (m_method == QLatin1String("GET"))
1159  m_network = networkAccessManager()->get(request);
1160  else if (m_method == QLatin1String("HEAD"))
1161  m_network = networkAccessManager()->head(request);
1162  else if(m_method == QLatin1String("POST"))
1163  m_network = networkAccessManager()->post(request, m_data);
1164  else if(m_method == QLatin1String("PUT"))
1165  m_network = networkAccessManager()->put(request, m_data);
1166  else if (m_method == QLatin1String("DELETE"))
1168 
1170  this, SLOT(downloadProgress(qint64)));
1174  this, SLOT(finished()));
1175 }
1176 
1178 {
1179  m_errorFlag = false;
1180  m_sendFlag = true;
1181  m_redirectCount = 0;
1182  m_data = data;
1183  m_me = *me;
1184 
1186 
1187  return QScriptValue();
1188 }
1189 
1191 {
1192  destroyNetwork();
1194  m_errorFlag = true;
1196 
1197  if (!(m_state == Unsent ||
1198  (m_state == Opened && !m_sendFlag) ||
1199  m_state == Done)) {
1200 
1201  m_state = Done;
1202  m_sendFlag = false;
1203  QScriptValue cbv = dispatchCallback(me);
1204  if (cbv.isError()) return cbv;
1205  }
1206 
1207  m_state = Unsent;
1208  return QScriptValue();
1209 }
1210 
1212 {
1213  Q_UNUSED(bytes)
1214  m_status =
1216  m_statusText =
1218 
1219  // ### We assume if this is called the headers are now available
1220  if (m_state < HeadersReceived) {
1222  fillHeadersList ();
1224  if (cbv.isError()) printError(cbv);
1225  }
1226 
1227  bool wasEmpty = m_responseEntityBody.isEmpty();
1229  if (wasEmpty && !m_responseEntityBody.isEmpty()) {
1230  m_state = Loading;
1232  if (cbv.isError()) printError(cbv);
1233  }
1234 }
1235 
1237 {
1238  Q_UNUSED(error)
1239  m_status =
1241  m_statusText =
1243 
1245 
1247  m_data.clear();
1248  destroyNetwork();
1249 
1250  if (error == QNetworkReply::ContentAccessDenied ||
1255  m_state = Loading;
1257  if (cbv.isError()) printError(cbv);
1258  } else {
1259  m_errorFlag = true;
1260  }
1261 
1262  m_state = Done;
1264  if (cbv.isError()) printError(cbv);
1265 }
1266 
1267 #define XMLHTTPREQUEST_MAXIMUM_REDIRECT_RECURSION 15
1269 {
1270  m_redirectCount++;
1273  if (redirect.isValid()) {
1274  QUrl url = m_network->url().resolved(redirect.toUrl());
1275  if (url.scheme().toLower() != QLatin1String("file")) {
1276  // See http://www.ietf.org/rfc/rfc2616.txt, section 10.3.4 "303 See Other":
1277  // Result of 303 redirection should be a new "GET" request.
1279  if (code.isValid() && code.toInt() == 303 && m_method != QLatin1String("GET"))
1280  m_method = QLatin1String("GET");
1281  destroyNetwork();
1282  requestFromUrl(url);
1283  return;
1284  }
1285  }
1286  }
1287 
1288  m_status =
1290  m_statusText =
1292 
1293  if (m_state < HeadersReceived) {
1295  fillHeadersList ();
1297  if (cbv.isError()) printError(cbv);
1298  }
1300  readEncoding();
1301 
1302  if (xhrDump()) {
1303  qWarning().nospace() << "XMLHttpRequest: RESPONSE " << qPrintable(m_url.toString());
1304  if (!m_responseEntityBody.isEmpty()) {
1305  qWarning().nospace() << " "
1307  }
1308  }
1309 
1310 
1311  m_data.clear();
1312  destroyNetwork();
1313  if (m_state < Loading) {
1314  m_state = Loading;
1316  if (cbv.isError()) printError(cbv);
1317  }
1318  m_state = Done;
1320  if (cbv.isError()) printError(cbv);
1321 
1322  m_me = QScriptValue();
1323 }
1324 
1325 
1327 {
1328  foreach (const HeaderPair &header, m_headersList) {
1329  if (header.first == "content-type") {
1330  int separatorIdx = header.second.indexOf(';');
1331  if (separatorIdx == -1) {
1332  m_mime == header.second;
1333  } else {
1334  m_mime = header.second.mid(0, separatorIdx);
1335  int charsetIdx = header.second.indexOf("charset=");
1336  if (charsetIdx != -1) {
1337  charsetIdx += 8;
1338  separatorIdx = header.second.indexOf(';', charsetIdx);
1339  m_charset = header.second.mid(charsetIdx, separatorIdx >= 0 ? separatorIdx : header.second.length());
1340  }
1341  }
1342  break;
1343  }
1344  }
1345 
1346  if (m_mime.isEmpty() || m_mime == "text/xml" || m_mime == "application/xml" || m_mime.endsWith("+xml"))
1347  m_gotXml = true;
1348 }
1349 
1351 {
1352  return m_gotXml;
1353 }
1354 
1355 
1356 #ifndef QT_NO_TEXTCODEC
1358 {
1359  QTextCodec *codec = 0;
1360 
1361  if (!m_charset.isEmpty())
1363 
1364  if (!codec && m_gotXml) {
1366  reader.readNext();
1368  }
1369 
1370  if (!codec && m_mime == "text/html")
1372 
1373  if (!codec)
1375 
1376  if (!codec)
1377  codec = QTextCodec::codecForName("UTF-8");
1378  return codec;
1379 }
1380 #endif
1381 
1382 
1384 {
1385 #ifndef QT_NO_TEXTCODEC
1386  if (!m_textCodec)
1388  if (m_textCodec)
1390 #endif
1391 
1393 }
1394 
1396 {
1397  return m_responseEntityBody;
1398 }
1399 
1401 {
1402  QScriptValue v = me->property(QLatin1String("callback"));
1403  return v.call();
1404 }
1405 
1407 {
1411 }
1412 
1414 {
1415  if (m_network) {
1416  m_network->disconnect();
1418  m_network = 0;
1419  }
1420 }
1421 
1422 // XMLHttpRequest methods
1424 {
1425  QScriptValue dataObject = context->thisObject().data();
1427  if (!request)
1428  THROW_REFERENCE("Not an XMLHttpRequest object");
1429 
1430  if (context->argumentCount() < 2 || context->argumentCount() > 5)
1431  THROW_DOM(SYNTAX_ERR, "Incorrect argument count");
1432 
1433  // Argument 0 - Method
1434  QString method = context->argument(0).toString().toUpper();
1435  if (method != QLatin1String("GET") &&
1436  method != QLatin1String("PUT") &&
1437  method != QLatin1String("HEAD") &&
1438  method != QLatin1String("POST") &&
1439  method != QLatin1String("DELETE"))
1440  THROW_DOM(SYNTAX_ERR, "Unsupported HTTP method type");
1441 
1442 
1443  // Argument 1 - URL
1444  QUrl url = QUrl::fromEncoded(context->argument(1).toString().toUtf8());
1445 
1446  if (url.isRelative()) {
1447  url = QDeclarativeScriptEngine::get(engine)->resolvedUrl(context,url);
1448  }
1449 
1450  // Argument 2 - async (optional)
1451  if (context->argumentCount() > 2 && !context->argument(2).toBoolean())
1452  THROW_DOM(NOT_SUPPORTED_ERR, "Synchronous XMLHttpRequest calls are not supported");
1453 
1454 
1455  // Argument 3/4 - user/pass (optional)
1456  QString username, password;
1457  if (context->argumentCount() > 3)
1458  username = context->argument(3).toString();
1459  if (context->argumentCount() > 4)
1460  password = context->argument(4).toString();
1461 
1462 
1463  // Clear the fragment (if any)
1464  url.setFragment(QString());
1465  // Set username/password
1466  if (!username.isNull()) url.setUserName(username);
1467  if (!password.isNull()) url.setPassword(password);
1468 
1469  return request->open(&dataObject, method, url);
1470 }
1471 
1473 {
1475  if (!request)
1476  THROW_REFERENCE("Not an XMLHttpRequest object");
1477 
1478  if (context->argumentCount() != 2)
1479  THROW_DOM(SYNTAX_ERR, "Incorrect argument count");
1480 
1481 
1482  if (request->readyState() != QDeclarativeXMLHttpRequest::Opened ||
1483  request->sendFlag())
1484  THROW_DOM(INVALID_STATE_ERR, "Invalid state");
1485 
1486 
1487  QString name = context->argument(0).toString();
1488  QString value = context->argument(1).toString();
1489 
1490  // ### Check that name and value are well formed
1491 
1492  QString nameUpper = name.toUpper();
1493  if (nameUpper == QLatin1String("ACCEPT-CHARSET") ||
1494  nameUpper == QLatin1String("ACCEPT-ENCODING") ||
1495  nameUpper == QLatin1String("CONNECTION") ||
1496  nameUpper == QLatin1String("CONTENT-LENGTH") ||
1497  nameUpper == QLatin1String("COOKIE") ||
1498  nameUpper == QLatin1String("COOKIE2") ||
1499  nameUpper == QLatin1String("CONTENT-TRANSFER-ENCODING") ||
1500  nameUpper == QLatin1String("DATE") ||
1501  nameUpper == QLatin1String("EXPECT") ||
1502  nameUpper == QLatin1String("HOST") ||
1503  nameUpper == QLatin1String("KEEP-ALIVE") ||
1504  nameUpper == QLatin1String("REFERER") ||
1505  nameUpper == QLatin1String("TE") ||
1506  nameUpper == QLatin1String("TRAILER") ||
1507  nameUpper == QLatin1String("TRANSFER-ENCODING") ||
1508  nameUpper == QLatin1String("UPGRADE") ||
1509  nameUpper == QLatin1String("USER-AGENT") ||
1510  nameUpper == QLatin1String("VIA") ||
1511  nameUpper.startsWith(QLatin1String("PROXY-")) ||
1512  nameUpper.startsWith(QLatin1String("SEC-")))
1513  return engine->undefinedValue();
1514 
1515  request->addHeader(nameUpper, value);
1516 
1517  return engine->undefinedValue();
1518 }
1519 
1521 {
1522  QScriptValue dataObject = context->thisObject().data();
1524  if (!request)
1525  THROW_REFERENCE("Not an XMLHttpRequest object");
1526 
1528  THROW_DOM(INVALID_STATE_ERR, "Invalid state");
1529 
1530  if (request->sendFlag())
1531  THROW_DOM(INVALID_STATE_ERR, "Invalid state");
1532 
1533  QByteArray data;
1534  if (context->argumentCount() > 0)
1535  data = context->argument(0).toString().toUtf8();
1536 
1537  return request->send(&dataObject, data);
1538 }
1539 
1541 {
1542  QScriptValue dataObject = context->thisObject().data();
1544  if (!request)
1545  THROW_REFERENCE("Not an XMLHttpRequest object");
1546 
1547  return request->abort(&dataObject);
1548 }
1549 
1551 {
1552  Q_UNUSED(engine)
1554  if (!request)
1555  THROW_REFERENCE("Not an XMLHttpRequest object");
1556 
1557  if (context->argumentCount() != 1)
1558  THROW_DOM(SYNTAX_ERR, "Incorrect argument count");
1559 
1560  if (request->readyState() != QDeclarativeXMLHttpRequest::Loading &&
1563  THROW_DOM(INVALID_STATE_ERR, "Invalid state");
1564 
1565  QString headerName = context->argument(0).toString();
1566 
1567  return QScriptValue(request->header(headerName));
1568 }
1569 
1571 {
1572  Q_UNUSED(engine)
1574  if (!request)
1575  THROW_REFERENCE("Not an XMLHttpRequest object");
1576 
1577  if (context->argumentCount() != 0)
1578  THROW_DOM(SYNTAX_ERR, "Incorrect argument count");
1579 
1580  if (request->readyState() != QDeclarativeXMLHttpRequest::Loading &&
1583  THROW_DOM(INVALID_STATE_ERR, "Invalid state");
1584 
1585  return QScriptValue(request->headers());
1586 }
1587 
1588 // XMLHttpRequest properties
1590 {
1591  Q_UNUSED(engine)
1593  if (!request)
1594  THROW_REFERENCE("Not an XMLHttpRequest object");
1595 
1596  return QScriptValue(request->readyState());
1597 }
1598 
1600 {
1601  Q_UNUSED(engine)
1603  if (!request)
1604  THROW_REFERENCE("Not an XMLHttpRequest object");
1605 
1606  if (request->readyState() == QDeclarativeXMLHttpRequest::Unsent ||
1608  THROW_DOM(INVALID_STATE_ERR, "Invalid state");
1609 
1610  if (request->errorFlag())
1611  return QScriptValue(0);
1612  else
1613  return QScriptValue(request->replyStatus());
1614 }
1615 
1617 {
1618  Q_UNUSED(engine)
1620  if (!request)
1621  THROW_REFERENCE("Not an XMLHttpRequest object");
1622 
1623  if (request->readyState() == QDeclarativeXMLHttpRequest::Unsent ||
1625  THROW_DOM(INVALID_STATE_ERR, "Invalid state");
1626 
1627  if (request->errorFlag())
1628  return QScriptValue(0);
1629  else
1630  return QScriptValue(request->replyStatusText());
1631 }
1632 
1634 {
1635  Q_UNUSED(engine)
1637  if (!request)
1638  THROW_REFERENCE("Not an XMLHttpRequest object");
1639 
1640  if (request->readyState() != QDeclarativeXMLHttpRequest::Loading &&
1642  return QScriptValue(QString());
1643  else
1644  return QScriptValue(request->responseBody());
1645 }
1646 
1648 {
1650  if (!request)
1651  THROW_REFERENCE("Not an XMLHttpRequest object");
1652 
1653  if (!request->receivedXml() ||
1656  return engine->nullValue();
1657  else
1658  return Document::load(engine, request->rawResponseBody());
1659 }
1660 
1662 {
1663  Q_UNUSED(engine);
1664  QScriptValue dataObject = context->thisObject().data();
1666  if (!request)
1667  THROW_REFERENCE("Not an XMLHttpRequest object");
1668 
1669  if (context->argumentCount()) {
1670  QScriptValue v = context->argument(0);
1671  dataObject.setProperty(QLatin1String("callback"), v);
1672  return v;
1673  } else {
1674  return dataObject.property(QLatin1String("callback"));
1675  }
1676 }
1677 
1678 // Constructor
1680 {
1681  if (context->isCalledAsConstructor()) {
1683  }
1684  return engine->undefinedValue();
1685 }
1686 
1688 {
1689  QScriptValue prototype = engine->newObject();
1690 
1691  // Methods
1692  prototype.setProperty(QLatin1String("open"), engine->newFunction(qmlxmlhttprequest_open, 2));
1693  prototype.setProperty(QLatin1String("setRequestHeader"), engine->newFunction(qmlxmlhttprequest_setRequestHeader, 2));
1694  prototype.setProperty(QLatin1String("send"), engine->newFunction(qmlxmlhttprequest_send));
1695  prototype.setProperty(QLatin1String("abort"), engine->newFunction(qmlxmlhttprequest_abort));
1696  prototype.setProperty(QLatin1String("getResponseHeader"), engine->newFunction(qmlxmlhttprequest_getResponseHeader, 1));
1697  prototype.setProperty(QLatin1String("getAllResponseHeaders"), engine->newFunction(qmlxmlhttprequest_getAllResponseHeaders));
1698 
1699  // Read-only properties
1706 
1707  // State values
1713 
1714  // Constructor
1715  QScriptValue constructor = engine->newFunction(qmlxmlhttprequest_new, prototype);
1721  engine->globalObject().setProperty(QLatin1String("XMLHttpRequest"), constructor);
1722 
1723  // DOM Exception
1724  QScriptValue domExceptionPrototype = engine->newObject();
1742 
1743  engine->globalObject().setProperty(QLatin1String("DOMException"), domExceptionPrototype);
1744 }
1745 
1747 
1748 #endif // QT_NO_XMLSTREAMREADER
1749 
1750 #include <qdeclarativexmlhttprequest.moc>
The QVariant class acts like a union for the most common Qt data types.
Definition: qvariant.h:92
QScriptValue newFunction(FunctionSignature signature, int length=0)
Creates a QScriptValue that wraps a native (C++) function.
#define THROW_REFERENCE(desc)
double d
Definition: qnumeric_p.h:62
The QScriptContext class represents a Qt Script function invocation.
virtual QueryFlags queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id)
Queries this script class for how access to the property with the given name of the given object shou...
QString toString() const
Returns a copy of the string reference as a QString object.
Definition: qstring.cpp:8653
void setUrl(const QUrl &url)
Sets the URL this network request is referring to to be url.
#define INVALID_ACCESS_ERR
static QScriptValue prototype(QScriptEngine *)
static QScriptValue qmlxmlhttprequest_getAllResponseHeaders(QScriptContext *context, QScriptEngine *engine)
QScriptValue property(const QString &name, const ResolveFlags &mode=ResolvePrototype) const
Returns the value of this QScriptValue&#39;s property with the given name, using the given mode to resolv...
static QScriptValue nextSibling(QScriptContext *context, QScriptEngine *engine)
#define NAMESPACE_ERR
static QScriptValue create(QScriptEngine *, NodeImpl *)
T qscriptvalue_cast(const QScriptValue &)
static QScriptValue prototype(QScriptEngine *)
#define QT_END_NAMESPACE
This macro expands to.
Definition: qglobal.h:90
bool isCalledAsConstructor() const
Returns true if the function was called as a constructor (e.g.
static QScriptValue qmlxmlhttprequest_new(QScriptContext *context, QScriptEngine *engine)
QNetworkReply * head(const QNetworkRequest &request)
Posts a request to obtain the network headers for request and returns a new QNetworkReply object whic...
bool hasRawHeader(const QByteArray &headerName) const
Returns true if the raw header headerName is present in this network request.
static QScriptValue isElementContentWhitespace(QScriptContext *context, QScriptEngine *engine)
NodeListClass(QScriptEngine *engine)
static QScriptValue prototype(QScriptEngine *)
The QScriptClass class provides an interface for defining custom behavior of (a class of) Qt Script o...
Definition: qscriptclass.h:43
static QScriptValue xmlStandalone(QScriptContext *context, QScriptEngine *engine)
static QScriptValue qmlxmlhttprequest_statusText(QScriptContext *context, QScriptEngine *engine)
QString toString(FormattingOptions options=None) const
Returns the human-displayable string representation of the URL.
Definition: qurl.cpp:5896
QNetworkReply * put(const QNetworkRequest &request, QIODevice *data)
Uploads the contents of data to the destination request and returnes a new QNetworkReply object that ...
QByteArray & append(char c)
Appends the character ch to this byte array.
const QByteArray & rawResponseBody() const
QVariant attribute(QNetworkRequest::Attribute code) const
Returns the attribute associated with the code code.
QByteArray rawHeader(const QByteArray &headerName) const
Returns the raw form of header headerName.
static QScriptValue qmlxmlhttprequest_getResponseHeader(QScriptContext *context, QScriptEngine *engine)
static QByteArray headerName(QNetworkRequest::KnownHeaders header)
bool isCDATA() const
Returns true if the reader reports characters that stem from a CDATA section; otherwise returns false...
DEFINE_BOOL_CONFIG_OPTION(xhrDump, QML_XHR_DUMP)
QByteArray toUtf8() const Q_REQUIRED_RESULT
Returns a UTF-8 representation of the string as a QByteArray.
Definition: qstring.cpp:4074
QDeclarativeXMLHttpRequest(QNetworkAccessManager *manager)
#define error(msg)
QString & replace(int i, int len, QChar after)
Definition: qstring.cpp:2005
bool isError() const
Returns true if this QScriptValue is an object of the Error class; otherwise returns false...
The QByteArray class provides an array of bytes.
Definition: qbytearray.h:135
static Expression::Ptr create(Expression *const expr, const YYLTYPE &sourceLocator, const ParserContext *const parseInfo)
int length() const
Returns the number of characters in this string.
Definition: qstring.h:696
QString toUpper() const Q_REQUIRED_RESULT
Returns an uppercase copy of the string.
Definition: qstring.cpp:5483
#define SLOT(a)
Definition: qobjectdefs.h:226
static QScriptValue qmlxmlhttprequest_onreadystatechange(QScriptContext *context, QScriptEngine *engine)
T1 first
Definition: qpair.h:65
QString toString() const
Returns the string value of this QScriptValue, as defined in ECMA-262 section 9.8, "ToString".
QNetworkReply * deleteResource(const QNetworkRequest &request)
Sends a request to delete the resource identified by the URL of request.
QStringRef value() const
Returns the attribute&#39;s value.
Definition: qxmlstream.h:156
T2 second
Definition: qpair.h:66
QScriptValue globalObject() const
Returns this engine&#39;s Global Object.
static QTextCodec * codecForHtml(const QByteArray &ba)
Tries to detect the encoding of the provided snippet of HTML in the given byte array, ba, by checking the BOM (Byte Order Mark) and the content-type meta header and returns a QTextCodec instance that is capable of decoding the html to unicode.
QString toString() const
Returns the variant as a QString if the variant has type() String , Bool , ByteArray ...
Definition: qvariant.cpp:2270
NetworkError
Indicates all possible error conditions found during the processing of the request.
Definition: qnetworkreply.h:70
static QScriptValue name(QScriptContext *context, QScriptEngine *engine)
bool startsWith(const QString &s, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Returns true if the string starts with s; otherwise returns false.
Definition: qstring.cpp:3734
QByteArray toLower() const
Returns a lowercase copy of the byte array.
void error(QNetworkReply::NetworkError)
QLatin1String(DBUS_INTERFACE_DBUS))) Q_GLOBAL_STATIC_WITH_ARGS(QString
The QStack class is a template class that provides a stack.
Definition: qcontainerfwd.h:63
QStringRef name() const
Returns the local name of a StartElement, EndElement, or an EntityReference.
long ASN1_INTEGER_get ASN1_INTEGER * a
int count(const T &t) const
Returns the number of occurrences of value in the list.
Definition: qlist.h:891
QDeclarativeGuard< QNetworkReply > m_network
QObject * toQObject() const
If this QScriptValue is a QObject, returns the QObject pointer that the QScriptValue represents; othe...
bool atEnd() const
Returns true if the reader has read until the end of the XML document, or if an error() has occurred ...
Definition: qxmlstream.cpp:590
The QUrl class provides a convenient interface for working with URLs.
Definition: qurl.h:61
The QString class provides a Unicode character string.
Definition: qstring.h:83
#define A(arg)
T * qobject_cast(QObject *object)
Definition: qobject.h:375
bool isNull() const
#define Q_ASSERT(cond)
Definition: qglobal.h:1823
The QObject class is the base class of all Qt objects.
Definition: qobject.h:111
static QScriptValue nodeName(QScriptContext *context, QScriptEngine *engine)
static QDeclarativeEnginePrivate * get(QDeclarativeEngine *e)
bool isRelative() const
Returns true if the URL is relative; otherwise returns false.
Definition: qurl.cpp:5880
QByteArray toByteArray() const
Returns the variant as a QByteArray if the variant has type() ByteArray or String (converted using QS...
Definition: qvariant.cpp:2383
#define XMLHTTPREQUEST_MAXIMUM_REDIRECT_RECURSION
static QScriptValue prototype(QScriptEngine *)
#define INDEX_SIZE_ERR
bool isEmpty() const
Returns true if the list contains no items; otherwise returns false.
Definition: qlist.h:152
static QDeclarativeScriptEngine * get(QScriptEngine *e)
The QScriptString class acts as a handle to "interned" strings in a QScriptEngine.
Definition: qscriptstring.h:38
int toInt(bool *ok=0) const
Returns the variant as an int if the variant has type() Int , Bool , ByteArray , Char ...
Definition: qvariant.cpp:2625
void printError(const QScriptValue &)
T pop()
Removes the top item from the stack and returns it.
Definition: qstack.h:67
The QXmlStreamAttribute class represents a single XML attribute.
Definition: qxmlstream.h:135
QList< NodeImpl * > attributes
static QScriptValue nodeValue(QScriptContext *context, QScriptEngine *engine)
QStringRef namespaceUri() const
Returns the namespaceUri of a StartElement or EndElement.
#define SIGNAL(a)
Definition: qobjectdefs.h:227
void append(const T &t)
Inserts value at the end of the list.
Definition: qlist.h:507
The QScriptEngine class provides an environment for evaluating Qt Script code.
static QScriptValue attributes(QScriptContext *context, QScriptEngine *engine)
QFuture< void > map(Sequence &sequence, MapFunction function)
#define QT_BEGIN_NAMESPACE
This macro expands to.
Definition: qglobal.h:89
void qt_add_qmlxmlhttprequest(QScriptEngine *engine)
QVariant header(KnownHeaders header) const
Returns the value of the known network header header if it is present in this request.
static QScriptValue qmlxmlhttprequest_status(QScriptContext *context, QScriptEngine *engine)
#define VALIDATION_ERR
QString trimmed() const Q_REQUIRED_RESULT
Returns a string that has whitespace removed from the start and the end.
Definition: qstring.cpp:4506
static QScriptValue documentElement(QScriptContext *context, QScriptEngine *engine)
static bool connect(const QObject *sender, const char *signal, const QObject *receiver, const char *member, Qt::ConnectionType=Qt::AutoConnection)
Creates a connection of the given type from the signal in the sender object to the method in the rece...
Definition: qobject.cpp:2580
#define NOT_FOUND_ERR
bool isEmpty() const
Returns true if the string has no characters; otherwise returns false.
Definition: qstring.h:704
static QScriptValue childNodes(QScriptContext *context, QScriptEngine *engine)
#define D(arg)
static QScriptValue qmlxmlhttprequest_setRequestHeader(QScriptContext *context, QScriptEngine *engine)
QNetworkAccessManager * networkAccessManager()
#define INUSE_ATTRIBUTE_ERR
const char * name
bool hasError() const
Returns true if an error has occurred, otherwise false.
Definition: qxmlstream.h:396
const T & at(int i) const
Returns the item at index position i in the list.
Definition: qlist.h:468
#define SYNTAX_ERR
QScriptEngine * engine() const
Returns the QScriptEngine that created this QScriptValue, or 0 if this QScriptValue is invalid or the...
static QScriptValue ownerElement(QScriptContext *context, QScriptEngine *engine)
static QString fromUtf8(const char *, int size=-1)
Returns a QString initialized with the first size bytes of the UTF-8 string str.
Definition: qstring.cpp:4302
void append(const T &t)
Inserts value at the end of the vector.
Definition: qvector.h:573
void addHeader(const QString &, const QString &)
Q_CORE_EXPORT void qWarning(const char *,...)
static const char * data(const QByteArray &arr)
unsigned int uint
Definition: qglobal.h:996
int indexOf(QChar c, int from=0, Qt::CaseSensitivity cs=Qt::CaseSensitive) const
Definition: qstring.cpp:2838
The QLatin1String class provides a thin wrapper around an US-ASCII/Latin-1 encoded string literal...
Definition: qstring.h:654
static QTextCodec * codec(MYSQL *mysql)
Definition: qsql_mysql.cpp:220
QScriptValue newObject()
Creates a QtScript object of class Object.
QList< NodeImpl * > * list
#define NO_MODIFICATION_ALLOWED_ERR
static QScriptValue xmlVersion(QScriptContext *context, QScriptEngine *engine)
QList< QByteArray > rawHeaderList() const
Returns a list of headers fields that were sent by the remote server, in the order that they were sen...
The QDeclarativeError class encapsulates a QML error.
static QScriptValue qmlxmlhttprequest_responseText(QScriptContext *context, QScriptEngine *engine)
void setScriptClass(QScriptClass *scriptClass)
Sets the custom script class of this script object to scriptClass.
static QScriptValue length(QScriptContext *context, QScriptEngine *engine)
void clear()
Removes all items from the list.
Definition: qlist.h:764
static QScriptValue prototype(QScriptEngine *)
QStringRef name() const
Returns the attribute&#39;s local name.
Definition: qxmlstream.h:149
__int64 qint64
Definition: qglobal.h:942
The State element defines configurations of objects and properties.
virtual QScriptValue property(const QScriptValue &object, const QScriptString &name, uint id)
Returns the value of the property with the given name of the given object.
#define NO_DATA_ALLOWED_ERR
static QVariant fromValue(const T &value)
Returns a QVariant containing a copy of value.
Definition: qvariant.h:336
bool toBoolean() const
Use toBool() instead.
void setHeader(KnownHeaders header, const QVariant &value)
Sets the value of the known header header to be value, overriding any previously set headers...
static QScriptValue parentNode(QScriptContext *context, QScriptEngine *engine)
QNetworkReply * get(const QNetworkRequest &request)
Posts a request to obtain the contents of the target request and returns a new QNetworkReply object o...
QUrl toUrl() const
Returns the variant as a QUrl if the variant has type() Url ; otherwise returns an invalid QUrl...
Definition: qvariant.cpp:2528
The QNetworkAccessManager class allows the application to send network requests and receive replies...
QStringRef documentEncoding() const
If the state() is StartDocument , this function returns the encoding string as specified in the XML d...
void setFragment(const QString &fragment)
Sets the fragment of the URL to fragment.
Definition: qurl.cpp:5669
static QScriptValue previousSibling(QScriptContext *context, QScriptEngine *engine)
#define Q_OBJECT
Definition: qobjectdefs.h:157
#define Q_DECLARE_METATYPE(TYPE)
This macro makes the type Type known to QMetaType as long as it provides a public default constructor...
Definition: qmetatype.h:265
int argumentCount() const
Returns the number of arguments passed to the function in this invocation.
static QScriptValue qmlxmlhttprequest_abort(QScriptContext *context, QScriptEngine *)
QPair< QByteArray, QByteArray > HeaderPair
QScriptValue newQObject(QObject *object, ValueOwnership ownership=QtOwnership, const QObjectWrapOptions &options=0)
Creates a QtScript object that wraps the given QObject object, using the given ownership.
QUrl resolvedUrl(QScriptContext *context, const QUrl &url)
TokenType readNext()
Reads the next token and returns its type.
Definition: qxmlstream.cpp:623
QList< NodeImpl * > children
T & first()
Returns a reference to the first item in the list.
Definition: qlist.h:282
bool isNull() const
Returns true if this string is null; otherwise returns false.
Definition: qstring.h:505
static bool disconnect(const QObject *sender, const char *signal, const QObject *receiver, const char *member)
Disconnects signal in object sender from method in object receiver.
Definition: qobject.cpp:2895
static QByteArray prototype(const QList< QByteArray > &parameterTypes, const QList< QByteArray > &parameterNames, bool *ok)
Definition: qaxserver.cpp:685
QString toUnicode(const QByteArray &) const
Converts a from the encoding of this codec to Unicode, and returns the result in a QString...
QScriptValue data() const
Returns the internal data of this QScriptValue object.
void setRawHeader(const QByteArray &headerName, const QByteArray &value)
Sets the header headerName to be of value headerValue.
QString header(const QString &name)
QString scheme() const
Returns the scheme of the URL.
Definition: qurl.cpp:4550
void setProperty(const QString &name, const QScriptValue &value, const PropertyFlags &flags=KeepExistingFlags)
Sets the value of this QScriptValue&#39;s property with the given name to the given value.
QString & append(QChar c)
Definition: qstring.cpp:1777
static QScriptValue firstChild(QScriptContext *context, QScriptEngine *engine)
#define NOT_SUPPORTED_ERR
NamedNodeMapClass(QScriptEngine *engine)
#define INVALID_STATE_ERR
void setPrototype(const QScriptValue &prototype)
If this QScriptValue is an object, sets the internal prototype (__proto__ property) of this object to...
#define load(x)
QStringRef documentVersion() const
If the state() is StartDocument , this function returns the version string as specified in the XML de...
QByteArray rawHeader(const QByteArray &headerName) const
Returns the raw contents of the header headerName as sent by the remote server.
static QScriptValue lastChild(QScriptContext *context, QScriptEngine *engine)
QScriptValue dispatchCallback(QScriptValue *me)
#define THROW_DOM(error, desc)
static QScriptValue wholeText(QScriptContext *context, QScriptEngine *engine)
QString toLower() const Q_REQUIRED_RESULT
Returns a lowercase copy of the string.
Definition: qstring.cpp:5389
QScriptValue open(QScriptValue *me, const QString &, const QUrl &)
T & last()
Returns a reference to the last item in the list.
Definition: qlist.h:284
unsigned int quint32
Definition: qglobal.h:938
static QScriptValue create(QScriptEngine *, NodeImpl *)
static QScriptValue nodeType(QScriptContext *context, QScriptEngine *engine)
QScriptValue abort(QScriptValue *me)
uint toUInt(bool *ok=0, int base=10) const
Returns the string converted to an unsigned int using base base, which is 10 by default and must be b...
Definition: qstring.cpp:6120
const char * property
Definition: qwizard.cpp:138
QScriptValue send(QScriptValue *me, const QByteArray &)
#define TYPE_MISMATCH_ERR
virtual QScriptValue property(const QScriptValue &object, const QScriptString &name, uint id)
Returns the value of the property with the given name of the given object.
static QScriptValue qmlxmlhttprequest_readyState(QScriptContext *context, QScriptEngine *engine)
static QTextCodec * codecForName(const QByteArray &name)
Searches all installed QTextCodec objects and returns the one which best matches name; the match is c...
QByteArray readAll()
Reads all available data from the device, and returns it as a QByteArray.
Definition: qiodevice.cpp:1025
static QScriptValue xmlEncoding(QScriptContext *context, QScriptEngine *engine)
virtual QueryFlags queryProperty(const QScriptValue &object, const QScriptString &name, QueryFlags flags, uint *id)
Queries this script class for how access to the property with the given name of the given object shou...
The QNetworkRequest class holds a request to be sent with QNetworkAccessManager.
static QScriptValue length(QScriptContext *context, QScriptEngine *engine)
quint16 index
static void exceptionToError(QScriptEngine *, QDeclarativeError &)
static QScriptValue prototype(QScriptEngine *)
static QTextCodec * codecForUtfText(const QByteArray &ba)
Tries to detect the encoding of the provided snippet ba by using the BOM (Byte Order Mark) and return...
static QScriptValue prototype(QScriptEngine *)
QNetworkReply * post(const QNetworkRequest &request, QIODevice *data)
Sends an HTTP POST request to the destination specified by request and returns a new QNetworkReply ob...
static QScriptValue create(QScriptEngine *, NodeImpl *, QList< NodeImpl *> *)
static QScriptValue qmlxmlhttprequest_send(QScriptContext *context, QScriptEngine *)
bool isEmpty() const
Returns true if the byte array has size 0; otherwise returns false.
Definition: qbytearray.h:421
static QScriptValue length(QScriptContext *context, QScriptEngine *engine)
bool isEmpty() const
Returns true if the vector has size 0; otherwise returns false.
Definition: qvector.h:139
QScriptValue undefinedValue()
Returns a QScriptValue of the primitive type Undefined.
static QScriptValue prototype(QScriptEngine *)
The QXmlStreamReader class provides a fast parser for reading well-formed XML via a simple streaming ...
Definition: qxmlstream.h:290
static QScriptValue qmlxmlhttprequest_responseXML(QScriptContext *context, QScriptEngine *engine)
static QScriptValue value(QScriptContext *context, QScriptEngine *engine)
QUrl url() const
Returns the URL of the content downloaded or uploaded.
static QUrl fromEncoded(const QByteArray &url)
Parses input and returns the corresponding QUrl.
Definition: qurl.cpp:5964
static QScriptValue qmlxmlhttprequest_open(QScriptContext *context, QScriptEngine *engine)
The QTextCodec class provides conversions between text encodings.
Definition: qtextcodec.h:62
#define slots
Definition: qobjectdefs.h:68
QStringRef text() const
Returns the text of Characters , Comment , DTD , or EntityReference.
bool isStandaloneDocument() const
Returns true if this document has been declared standalone in the XML declaration; otherwise returns ...
QUrl resolved(const QUrl &relative) const
Returns the result of the merge of this URL with relative.
Definition: qurl.cpp:5819
bool isValid() const
Returns true if the storage type of this variant is not QVariant::Invalid; otherwise returns false...
Definition: qvariant.h:485
QScriptValue thisObject() const
Returns the `this&#39; object associated with this QScriptContext.
#define INVALID_CHARACTER_ERR
#define HIERARCHY_REQUEST_ERR
void warning(const QDeclarativeError &)
QString toString() const
Returns the string that this QScriptString represents, or a null string if this QScriptString is not ...
void setUserName(const QString &userName)
Sets the URL&#39;s user name to userName.
Definition: qurl.cpp:4648
#define qPrintable(string)
Definition: qglobal.h:1750
The QScriptValue class acts as a container for the Qt Script data types.
Definition: qscriptvalue.h:57
#define Q_UNUSED(x)
Indicates to the compiler that the parameter with the specified name is not used in the body of a fun...
Definition: qglobal.h:1729
QScriptValue call(const QScriptValue &thisObject=QScriptValue(), const QScriptValueList &args=QScriptValueList())
Calls this QScriptValue as a function, using thisObject as the `this&#39; object in the function call...
void deleteLater()
Schedules this object for deletion.
Definition: qobject.cpp:2145
QScriptValue argument(int index) const
Returns the function argument at the given index.
The QLatin1Char class provides an 8-bit ASCII/Latin-1 character.
Definition: qchar.h:55
QXmlStreamAttributes attributes() const
Returns the attributes of a StartElement.
void setPassword(const QString &password)
Sets the URL&#39;s password to password.
Definition: qurl.cpp:4736
void clear()
Clears the contents of the byte array and makes it empty.
QScriptValue newVariant(const QVariant &value)
Creates a QtScript object holding the given variant value.
QScriptValue nullValue()
Returns a QScriptValue of the primitive type Null.
static bool isNull(const QVariant::Private *d)
Definition: qvariant.cpp:300
int open(const char *, int,...)
void setData(const QScriptValue &data)
Sets the internal data of this QScriptValue object.
bool endsWith(const QByteArray &a) const
Returns true if this byte array ends with byte array ba; otherwise returns false. ...
The QList class is a template class that provides lists.
Definition: qdatastream.h:62
#define WRONG_DOCUMENT_ERR
The Text item allows you to add formatted text to a scene.
T & top()
Returns a reference to the stack&#39;s top item.
Definition: qstack.h:72
#define DOMSTRING_SIZE_ERR
static QScriptValue prototype(QScriptEngine *)
static QScriptValue load(QScriptEngine *engine, const QByteArray &data)
#define INVALID_MODIFICATION_ERR
DocumentImpl * document
QStringRef namespaceUri() const
Returns the attribute&#39;s resolved namespaceUri, or an empty string reference if the attribute does not...
Definition: qxmlstream.h:148